blinky with 3 colors

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Thread.h"
00003 
00004 // For FRDM board K64F
00005 #if defined TARGET_K64F
00006 #define button_pressed 0
00007 #define led_on 0
00008 #define led_off 1
00009 #define USERBUTTON SW2
00010 #define RED_LED LED1
00011 #define BLUE_LED LED2
00012 #define GREEN_LED LED3
00013 #endif
00014 
00015 // For NUCLEO-F429ZI
00016 #if defined TARGET_STM32F429ZI
00017 #define button_pressed 1
00018 #define led_on 1
00019 #define led_off 0
00020 #define USERBUTTON USER_BUTTON
00021 #define RED_LED LED3
00022 #define BLUE_LED LED2
00023 #define GREEN_LED LED1
00024 #endif
00025 
00026 DigitalOut red_led(RED_LED, led_off); // RED
00027 DigitalOut green_led(GREEN_LED, led_off); // GREEN
00028 DigitalOut blue_led(BLUE_LED, led_off); // BLUE
00029 DigitalIn button(USERBUTTON);
00030 
00031 // main() runs in its own thread in the OS
00032 // (note the calls to Thread::wait below for delays)
00033 
00034 // Connect to serial USB
00035 Serial pc(USBTX, USBRX, 115200);
00036 
00037 void led_thread() {
00038     uint16_t color = 0;
00039     while (true) {
00040         color++;
00041         color = color & 0x7;
00042         red_led = color & 0x1;
00043         green_led = color & 0x2;
00044         pc.printf("Color is: %d\r\n", color);
00045         Thread::wait(1000);
00046     }
00047 }
00048 
00049 
00050 int main() {
00051     //Create a thread to execute the function led_thread
00052     Thread::Thread thread1;
00053     thread1.start(&led_thread);
00054     uint8_t b_reg = 0;
00055         
00056     // Main thread
00057     while (true) {
00058         if (button_pressed == button) {
00059            b_reg = b_reg << 1;
00060            b_reg |= 1;
00061            if ((b_reg & 0x17) == 0x17) {
00062               blue_led = led_on;
00063               pc.putc('*');
00064             } else {
00065                 pc.putc('-');
00066             }
00067         }
00068         else {
00069            b_reg = 0;
00070            blue_led = led_off;
00071         }
00072         Thread::wait(100);
00073         
00074     }
00075 }
00076