/* * Created on: Jul 28, 2024 */ #include #include "ui.h" #include "adc.h" #include "btn.h" #include "config.h" #include "led.h" #include "ledprog_pep.h" #include "ledprog_rgb.h" #define MODE_RUN 0 #define MODE_PROGRAM 1 #define MODE_PARAMETER 2 #define UI_CONF_SAVE_TIMEOUT 512 #define UI_PROG_RUNTIME_MIN (128*15) // 15 seconds #define UI_PROG_RUNTIME_MAX (128*120) // 120 seconds #define PROG_REPEAT 0x80 static const uint8_t led_gc_map[] = { 48, 40, 34, 32, // 0 = maybe outside, or really bright inside. 1-3 = indoors 30, 28, 27, 25, // 4-7 = indoors 23, 21, 19, 18, // 8-11 = indoors normal 17, 16, 15, 13, // 12 = dimmest normal, 13 = darker, 14-15 darker still 11, 9, 7, 5, // 16-19 = from night room light to computer light 0 // 20 = outside mode turns off the LEDs }; static uint8_t mode = MODE_RUN; static uint8_t tick = 0; static uint16_t save_delay = 0; void ui_btn_push_cb(uint8_t idx) { } void ui_btn_hold_cb(uint8_t idx) { switch (idx) { case 0: { // left pepper hat userconf.pep_prog_ena_map ^= PROG_REPEAT; break; } case 1: { // right pepper hat userconf.rgb_prog_ena_map ^= PROG_REPEAT; break; } } } void ui_btn_release_cb(uint8_t idx) { uint8_t update; switch (idx) { case 0: { // left pepper hat update = userconf.pep_prog_ena_map & ~(PROG_REPEAT); update++; if (update > 5) update = 0; userconf.pep_prog_ena_map = update | (userconf.pep_prog_ena_map & PROG_REPEAT); ledprog_pep_init(); break; } case 1: { // right pepper hat update = userconf.rgb_prog_ena_map & ~(PROG_REPEAT); update++; if (update > 3) update = 0; userconf.rgb_prog_ena_map = update | (userconf.rgb_prog_ena_map & PROG_REPEAT); ledprog_rgb_init(); break; } } save_delay = UI_CONF_SAVE_TIMEOUT; } void ui_init() { btn[0].hold = 1200 >> 1; btn[0].repeat = 0; // (1000 / 20) >> 1; btn[0].cb_push = ui_btn_push_cb; btn[0].cb_hold = ui_btn_hold_cb; btn[0].cb_release = ui_btn_release_cb; btn[1].hold = 1200 >> 1; btn[1].repeat = 0; btn[1].cb_push = ui_btn_push_cb; btn[1].cb_hold = ui_btn_hold_cb; btn[1].cb_release = ui_btn_release_cb; } void ui_render() { uint8_t w; tick++; uint8_t prog_pep_idx = userconf.pep_prog_ena_map & ~(PROG_REPEAT); uint8_t prog_rgb_idx = userconf.rgb_prog_ena_map & ~(PROG_REPEAT); switch (mode) { case MODE_RUN: { // run programs if (ledprog_pep[prog_pep_idx]) { ledprog_pep[prog_pep_idx](tick); } if (ledprog_rgb[prog_rgb_idx]) { ledprog_rgb[prog_rgb_idx](tick); } // check flash save if (save_delay) { save_delay--; if (!save_delay) { userconf_save(); } } break; } case MODE_PROGRAM: { break; } case MODE_PARAMETER: { break; } } // set LED current based on brightness w = adc_get_lsens_coarse(); if (w > 19) w = 19; if (!w) { // are you outside? why? it's too fucking hot // we'll shut down when in the presence of big nuclear fire if (adc_get_lsens() < 400) { // yup, outside or in a spotlight at 3ft away or something w = 20; } } is31fl3729_set_global_current(FL3729_ADDR, led_gc_map[w]); // temporary: testing // ledprog_pep[prog_run](tick); /* if ((tick & 3) == 3) { tmp++; tmp &= 0x3f; for (uint8_t i = 0; i < 64; i++) { if (i == tmp) led.all[i] = 255; else led.all[i] = 0; } led_matrix_is_updated(); } */ }