doma 13_11_2025
This commit is contained in:
58
hod5a/main.cpp
Normal file
58
hod5a/main.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user