leddie/firmware/lib/system.c

86 lines
1.4 KiB
C

#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#include "system.h"
#include "pm.h"
extern void app_init();
extern enum app_return app_mainloop();
struct cRGB led[STRIPLEN];
void display_update() {
ws2812_setleds(led, STRIPLEN);
}
void display_clear() {
memset(led, 0, STRIPLEN * 3);
}
void system_init()
{
pm_disable_periphery();
button_init();
TIFR = 0xFF; // Clear all interrupt flags
pm_init();
pm_axl_on();
_delay_ms(50);
mma8653_init();
while (HAL_ASSERTED_N(BUTTON_N)) // Prevent boot while button is pressed
_delay_ms(25);
timers_init();
}
enum BOOT_COLOR {RED, BLUE, GREEN};
void boot_animation(enum BOOT_COLOR color_offset)
{
_delay_ms(10);
display_clear();
for(int i = 0; i<STRIPLEN; ++i) {
*(&led[i].r + color_offset) = 0x10;
display_update();
_delay_ms(15);
*(&led[i].r + color_offset) = 0x0;
}
display_update();
_delay_ms(10);
}
void system_boot()
{
system_init();
pm_led_on_auto();
boot_animation(GREEN);
app_init();
}
int main()
{
enum app_return ar;
system_boot();
while (1) {
while (1) {
ar = app_mainloop();
if (ar != RUN)
break;
// TODO: Handle reset instead of suspend
// TODO: wdt reset
}
boot_animation(RED);
pm_suspend();
system_boot();
}
pm_reset();
}
void systick_cb()
{
pm_led_on_auto(); // Turn off LED DC-DC converter when USB power is connected
button_systick_cb();
}