doma 13_11_2025

This commit is contained in:
Priec
2025-11-13 12:19:18 +01:00
parent 7e7f4b471a
commit 983c503caa
25 changed files with 699 additions and 0 deletions

58
hod5a/main.cpp Normal file
View File

@@ -0,0 +1,58 @@
// main.cpp
#include "mbed.h"
#define MAXIMUM_BUFFER_SIZE 32
DigitalOut led1(LED1);
static BufferedSerial serial_port(USBTX, USBRX);
class Led {
private:
Ticker ticker;
bool ledState;
public:
Led() : ledState(false) {
led1 = ledState;
}
void setBlinkPeriod(float period) {
ticker.detach();
ticker.attach(callback(this, &Led::tickerHandler), period / 2.0f);
}
void tickerHandler() {
ledState = !ledState;
led1 = ledState;
}
};
int main()
{
serial_port.set_baud(9600);
serial_port.set_format(
/* bits */ 8,
/* parity */ BufferedSerial::None,
/* stop bit */ 1
);
char buf[MAXIMUM_BUFFER_SIZE] = {0};
Led ledController;
ledController.setBlinkPeriod(1.0f);
while (1) {
if (uint32_t num = serial_port.read(buf, sizeof(buf))) {
if (buf[0] >= '0' && buf[0] <= '9') {
float period = buf[0] - '0';
if (period > 0) {
ledController.setBlinkPeriod(period);
} else {
ledController.setBlinkPeriod(0.1f);
}
}
serial_port.write(buf, num);
}
ThisThread::sleep_for(100ms);
}
}