Sikil

main.cpp

Committer:
maetugr
Date:
2021-01-15
Revision:
0:ca4ecd424807

File content as of revision 0:ca4ecd424807:

/* Sikil
 */

#include "mbed.h"
#include "platform/mbed_thread.h"

int main()
{
    DigitalOut led(LED1);
    Serial computer(USBTX, USBRX);
    
    computer.baud(9600);
    computer.printf("Press 'u' to turn LED brightness up, 'd' to turn it down\n");
    
    float brightness{1.f};
    static constexpr int duty_cycle_ms{10};

    while (true) {
        if (computer.readable()) {
            char letter = computer.getc();
        
            if((letter == 'u') && (brightness < 1.f)) {
                brightness += 0.1f;
                led = brightness;
            }
            
            if((letter == 'd') && (brightness > 0.f)) {
                brightness -= 0.1f;
                led = brightness;
            }
        }

        led = 1;
        thread_sleep_for(brightness * duty_cycle_ms);
        led = 0;
        thread_sleep_for((1.f - brightness) * duty_cycle_ms);
    } 
}