add clock hours mark demo

This commit is contained in:
King Kévin 2016-02-18 11:34:08 +01:00
parent a5818ebe07
commit 2a7d921d1a
1 changed files with 47 additions and 6 deletions

53
main.c
View File

@ -35,8 +35,10 @@
#include "usb_cdcacm.h" // USB CDC ACM utilities
#include "led_ws2812b.h" // WS2812b LEDs utilities
/* flag set in interrupts to be processed in main taks */
volatile bool button_flag = false; // button has been presse
/* flag set in interrupts to be processed in main task */
volatile bool button_flag = false; // button has been pressed
uint8_t clock_leds[WS2812B_LEDS*3] = {0}; // RGB values for the WS2812b clock LEDs
/* default output (i.e. for printf) */
int _write(int file, char *ptr, int len)
@ -84,6 +86,34 @@ void led_toggle(void)
gpio_toggle(LED_PORT, LED_PIN);
}
/* switch off all clock LEDs */
static void leds_clear(void)
{
// set all colors of all LEDs to 0
for (uint16_t i=0; i<LENGTH(clock_leds); i++) {
clock_leds[i] = 0;
}
}
/* set hours mark on clock LEDs */
static void leds_hours(void)
{
for (uint8_t hour=0; hour<12; hour++) {
uint16_t led = LENGTH(clock_leds)/3/12*hour;
clock_leds[led*3+0] = 0xff;
clock_leds[led*3+1] = 0xff;
clock_leds[led*3+2] = 0xff;
}
}
/* set the LEDs */
static void leds_set(void)
{
for (uint16_t i=0; i<LENGTH(clock_leds)/3; i++) {
ws2812b_set_rgb(i,clock_leds[i*3+0],clock_leds[i*3+1],clock_leds[i*3+2]);
}
}
int main(void)
{
SCB_VTOR = (uint32_t) 0x08002000; // relocate vector table because of the bootloader
@ -111,6 +141,7 @@ int main(void)
#endif
ws2812b_setup(); // setup WS2812b LEDs
leds_clear(); // clear LED values
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
led_on(); // switch on LED to indicate setup completed
@ -139,18 +170,28 @@ int main(void)
printf("%c",c); // echo receive character
switch (c) {
case 'r': // set red color
ws2812b_set_rgb(0,0xff,0,0);
clock_leds[0] = 0xff;
clock_leds[1] = 0;
clock_leds[2] = 0;
break;
case 'g': // set green color
ws2812b_set_rgb(0,0,0xff,0);
clock_leds[0] = 0;
clock_leds[1] = 0xff;
clock_leds[2] = 0;
break;
case 'b': // set blue color
ws2812b_set_rgb(0,0,0,0xff);
clock_leds[0] = 0;
clock_leds[1] = 0;
clock_leds[2] = 0xff;
break;
case 'h': // show hours
leds_hours();
break;
default: // set no color
ws2812b_set_rgb(0,0,0,0);
leds_clear();
break;
}
leds_set(); // set the colors of all LEDs
ws2812b_transmit(); // transmit set color
}
while (button_flag) {