add function to display hour
This commit is contained in:
parent
2a7d921d1a
commit
c40e097015
48
main.c
48
main.c
|
@ -38,6 +38,14 @@
|
|||
/* flag set in interrupts to be processed in main task */
|
||||
volatile bool button_flag = false; // button has been pressed
|
||||
|
||||
// the number of ticks in one second
|
||||
#define TICKS_PER_SECOND 255
|
||||
/* I can't do the following in define because how defines are stored would cause an integer overflow in the arithmetics */
|
||||
const uint32_t ticks_second = TICKS_PER_SECOND; // the number of ticks in one second
|
||||
const uint32_t ticks_minute = 60*TICKS_PER_SECOND; // the number of ticks in one minute
|
||||
const uint32_t ticks_hour = 60*60*TICKS_PER_SECOND; // the number of ticks in one hour
|
||||
const uint32_t ticks_midday = 12*60*60*TICKS_PER_SECOND; // the number of ticks in one midday (12 hours)
|
||||
|
||||
uint8_t clock_leds[WS2812B_LEDS*3] = {0}; // RGB values for the WS2812b clock LEDs
|
||||
|
||||
/* default output (i.e. for printf) */
|
||||
|
@ -87,7 +95,7 @@ void led_toggle(void)
|
|||
}
|
||||
|
||||
/* switch off all clock LEDs */
|
||||
static void leds_clear(void)
|
||||
static void clock_clear(void)
|
||||
{
|
||||
// set all colors of all LEDs to 0
|
||||
for (uint16_t i=0; i<LENGTH(clock_leds); i++) {
|
||||
|
@ -96,16 +104,35 @@ static void leds_clear(void)
|
|||
}
|
||||
|
||||
/* set hours mark on clock LEDs */
|
||||
static void leds_hours(void)
|
||||
static void clock_hours(void)
|
||||
{
|
||||
for (uint8_t hour=0; hour<12; hour++) {
|
||||
uint16_t led = LENGTH(clock_leds)/3/12*hour;
|
||||
uint16_t led = WS2812B_LEDS/12*hour;
|
||||
clock_leds[led*3+0] = 0xff;
|
||||
clock_leds[led*3+1] = 0xff;
|
||||
clock_leds[led*3+2] = 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
/* set hour progress on clock LEDs */
|
||||
static void clock_hour(uint32_t time)
|
||||
{
|
||||
time %= ticks_midday; // the LED strip can only display 12 hours
|
||||
uint32_t led_hour = WS2812B_LEDS*time/ticks_midday; // scale to number of LEDs
|
||||
if (led_hour<WS2812B_LEDS) {
|
||||
clock_leds[led_hour*3+0] = 0;
|
||||
clock_leds[led_hour*3+1] = 0;
|
||||
clock_leds[led_hour*3+2] = 0xff;
|
||||
}
|
||||
/*
|
||||
for (uint16_t led=0; led<hour; led++) {
|
||||
clock_leds[led*3+0] = 0;
|
||||
clock_leds[led*3+1] = 0;
|
||||
clock_leds[led*3+2] = 0xff;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/* set the LEDs */
|
||||
static void leds_set(void)
|
||||
{
|
||||
|
@ -141,11 +168,15 @@ int main(void)
|
|||
#endif
|
||||
|
||||
ws2812b_setup(); // setup WS2812b LEDs
|
||||
leds_clear(); // clear LED values
|
||||
clock_clear(); // clear clock LEDs
|
||||
|
||||
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
|
||||
led_on(); // switch on LED to indicate setup completed
|
||||
|
||||
clock_hour(6*ticks_hour);
|
||||
leds_set(); // set the colors of all LEDs
|
||||
ws2812b_transmit(); // transmit set color
|
||||
|
||||
bool action = false; // if an action has been performed don't go to sleep
|
||||
button_flag = false; // reset button flag
|
||||
char c; // to store received character
|
||||
|
@ -184,11 +215,14 @@ int main(void)
|
|||
clock_leds[1] = 0;
|
||||
clock_leds[2] = 0xff;
|
||||
break;
|
||||
case 'h': // show hours
|
||||
leds_hours();
|
||||
case 'm': // show hour marks
|
||||
clock_hours();
|
||||
break;
|
||||
case 'h': // show hours
|
||||
clock_hour(6*ticks_hour);
|
||||
break;
|
||||
default: // set no color
|
||||
leds_clear();
|
||||
clock_clear();
|
||||
break;
|
||||
}
|
||||
leds_set(); // set the colors of all LEDs
|
||||
|
|
Loading…
Reference in New Issue