// main.cpp

#include "mbed.h"

DigitalOut led(LED1);
Ticker ticker;

void toggle_led() { led = !led; }

int main() {
    BufferedSerial serial(USBTX, USBRX);
    serial.set_baud(9600);

    char buf[32];
    char msg[] = "Zadajte cislicu 1-9:\r\n";
    serial.write(msg, sizeof(msg));

    // Use explicit chrono type for full compatibility
    ticker.attach(&toggle_led, std::chrono::seconds(1));

    while (1) {
        if (uint32_t num = serial.read(buf, sizeof(buf))) {
            char c = buf[0];
            if (c >= '1' && c <= '9') {
                int period = c - '0';
                ticker.detach();
                ticker.attach(&toggle_led, std::chrono::seconds(period));
            }
        }
        ThisThread::sleep_for(100ms);
    }
}
