This commit is contained in:
Filipriec
2025-11-06 13:51:47 +01:00
parent 7e7f4b471a
commit 915c573b27
8 changed files with 358 additions and 0 deletions

59
hod5a/main.cpp Normal file
View File

@@ -0,0 +1,59 @@
// main.cpp
#include "mbed.h"
#define MAXIMUM_BUFFER_SIZE 32
class Led {
private:
DigitalOut led;
Ticker ticker;
float period;
public:
Led(PinName pin) : led(pin), period(1.0) {
led = 0;
}
void setBlinkPeriod(float period) {
this->period = period;
ticker.detach();
if (period > 0) {
auto us = std::chrono::microseconds(static_cast<long long>(period * 1000000));
ticker.attach(callback(this, &Led::tickerHandler), us);
}
}
void tickerHandler() {
led = !led;
}
};
int main() {
Led myLed(LED1);
static BufferedSerial serial_port(USBTX, USBRX);
serial_port.set_baud(9600);
serial_port.set_format(8, BufferedSerial::None, 1);
char buf[MAXIMUM_BUFFER_SIZE] = {0};
char msg[] = "Zadaj cislicu 0-9:\r\n";
serial_port.write(msg, sizeof(msg));
while(1) {
if (uint32_t num = serial_port.read(buf, sizeof(buf))) {
char c = buf[0];
if (c >= '0' && c <= '9') {
int digit = c - '0';
float newPeriod = (float)digit;
myLed.setBlinkPeriod(newPeriod);
serial_port.write(buf, num);
}
}
ThisThread::sleep_for(150ms);
}
}