// main.cpp
#include "mbed.h"

#define MAXIMUM_BUFFER_SIZE 32

PwmOut led_pwm(LED1);
DigitalIn  button(PC_13);

static BufferedSerial serial_port(USBTX, USBRX);

int num_pressed(int num_presses) {
    num_presses += 1;

    return num_presses;
}

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};

    int num_presses = 0;
    while (1) {

        if (uint32_t num = serial_port.read(buf, sizeof(buf))) {
            num_presses += num_pressed(num_presses);

            // Echo the input back to the terminal.
            serial_port.write(buf, num_presses);

            while (num == 0) {
                ThisThread::sleep_for(10ms);
            }
        }

        ThisThread::sleep_for(150ms);
    }
}
