dc32-flames-addon/firmware/user/src/ui.c

145 lines
3.1 KiB
C

/*
* Created on: Jul 28, 2024
*/
#include <stdint.h>
#include "ui.h"
#include "adc.h"
#include "btn.h"
#include "config.h"
#include "led.h"
#define MODE_RUN 0
#define MODE_PROGRAM 1
#define MODE_PARAMETER 2
#define UI_CONF_SAVE_TIMEOUT 384
#define UI_PROG_RUNTIME_MIN (128*15) // 15 seconds
#define UI_PROG_RUNTIME_MAX (128*120) // 120 seconds
#define PROG_RANDOM 0x80
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: { // BTN_UP, upper programs
userconf.top_prog_ena_map ^= PROG_RANDOM;
break;
}
case 1: { // BTN_DN, lower programs
userconf.bot_prog_ena_map ^= PROG_RANDOM;
break;
}
}
*/
}
void ui_btn_release_cb(uint8_t idx)
{
uint8_t update;
switch (idx) {
case 0: { // BTN_UP, upper programs
update = userconf.top_prog_ena_map & ~(PROG_RANDOM);
update++;
if (update >= (sizeof(ledprog_flames) / 4)) update = 0;
userconf.top_prog_ena_map = update | (userconf.top_prog_ena_map & PROG_RANDOM);
ledprog_top_init();
break;
}
case 1: { // BTN_DN, lower programs
update = userconf.bot_prog_ena_map & ~(PROG_RANDOM);
update++;
if (update >= (sizeof(ledprog_boeing) / 4)) update = 0;
userconf.bot_prog_ena_map = update | (userconf.bot_prog_ena_map & PROG_RANDOM);
ledprog_bot_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_top_idx = userconf.top_prog_ena_map & ~(PROG_RANDOM);
uint8_t prog_bot_idx = userconf.bot_prog_ena_map & ~(PROG_RANDOM);
switch (mode) {
case MODE_RUN: {
// run programs
if (ledprog_flames[prog_top_idx]) {
ledprog_flames[prog_top_idx](tick);
}
if (ledprog_boeing[prog_bot_idx]) {
ledprog_boeing[prog_bot_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 ambient light level
w = adc_get_lsens_coarse();
w = adc_get_brightness(w);
// is31fl3729_set_global_current(FL3729_ADDR, w);
}