diff --git a/include/mongoose-touch.h b/include/mongoose-touch.h index 6434f38..c0dc523 100644 --- a/include/mongoose-touch.h +++ b/include/mongoose-touch.h @@ -17,6 +17,8 @@ void widget_topbar_ev(int ev, struct widget_t *w, void *ev_data); void widget_name_ev(int ev, struct widget_t *w, void *ev_data); +void widget_battery_ev(int ev, struct widget_t *w, void *ev_data); + void widget_default_ev(int ev, struct widget_t *w, void *ev_data); diff --git a/mos.yml b/mos.yml index 7ed30f5..d15c473 100644 --- a/mos.yml +++ b/mos.yml @@ -23,6 +23,7 @@ filesystem: - fs config_schema: + - ["sys.esp32_adc_vref", 3300] # ADC Vref is 3.3V - ["wifi.ap.enable", false] - ["wifi.sta.enable", true] - ["wifi.sta.ssid", "dapches-iot"] @@ -48,6 +49,7 @@ libs: - origin: https://github.com/mongoose-os-libs/rpc-uart - origin: https://github.com/mongoose-os-libs/prometheus-metrics - origin: https://github.com/mongoose-os-libs/pwm + - origin: https://github.com/mongoose-os-libs/adc - origin: libs/lobo-spi - origin: libs/ili9341 - origin: libs/stmpe610 diff --git a/src/main.c b/src/main.c index 4456873..bc9d26e 100644 --- a/src/main.c +++ b/src/main.c @@ -70,19 +70,24 @@ void tft_demo(void) return; } - w = widget_create("name", 0, 0, 198, 20); + w = widget_create("name", 0, 0, 185, 20); widget_set_handler(w, widget_name_ev, NULL); screen_widget_add(screen, w); - w = widget_create("network", 198, 0, 22, 20); + w = widget_create("network", 185, 0, 22, 20); widget_set_handler(w, widget_network_ev, NULL); screen_widget_add(screen, w); - w = widget_create("wifi", 220, 0, 20, 20); + w = widget_create("wifi", 207, 0, 20, 20); widget_set_handler(w, widget_wifi_ev, NULL); widget_set_timer(w, 5000); screen_widget_add(screen, w); + w = widget_create("battery", 227, 0, 13, 20); + widget_set_handler(w, widget_battery_ev, NULL); + widget_set_timer(w, 10000); + screen_widget_add(screen, w); + w = widget_create("time", 240, 0, 80, 20); widget_set_handler(w, widget_time_ev, NULL); widget_set_timer(w, 1000); diff --git a/src/widget_battery.c b/src/widget_battery.c new file mode 100644 index 0000000..b54e71d --- /dev/null +++ b/src/widget_battery.c @@ -0,0 +1,62 @@ +#include "mgos.h" +#include "mgos_adc.h" +#include "tft.h" +#include "mongoose-touch.h" + + +#define WIDGET_BATTERY_ADC_PIN 35 + +// Returns millivolts - Huzzah32 has a 1:1 voltage divider on I35 (A13) +// so it returns half of the battery voltage. LiPo's are full at 4200mV +// and critically empty at 3000mV. +static int widget_battery_getvoltage() { + // Value is between 0..4096 which maps to 0..6600mV + return (6600*mgos_adc_read(WIDGET_BATTERY_ADC_PIN))/4096; +} + +static void widget_battery_render(struct widget_t *w, void *ev_data) { + int mvolts; + color_t color; + uint8_t h; + + if (!w) + return; + + mgos_ili9341_setclipwin(w->x, w->y, w->x+w->w, w->y+w->h); + + mvolts=widget_battery_getvoltage(); + color=ILI9341_RED; + if (mvolts<3200) mvolts=3200; + if (mvolts>3500) color=ILI9341_YELLOW; + if (mvolts>3900) color=ILI9341_GREEN; + if (mvolts>4200) mvolts=4200; + h=12-12*(mvolts-3200)/(4200-3200); + + mgos_ili9341_fillRect(5, 2, 3, 2, color); + mgos_ili9341_fillRect(2, 4, 9, 14, color); + mgos_ili9341_fillRect(3, 5, 7, h, ILI9341_BLACK); + mgos_ili9341_resetclipwin(); + + (void) ev_data; +} + +void widget_battery_ev(int ev, struct widget_t *w, void *ev_data) { + if (!w) + return; + + switch(ev) { + case EV_WIDGET_CREATE: + LOG(LL_INFO, ("Monitoring LiPo voltage on gpio=%d", WIDGET_BATTERY_ADC_PIN)); + mgos_adc_enable(WIDGET_BATTERY_ADC_PIN); + case EV_WIDGET_DRAW: + case EV_WIDGET_REDRAW: + case EV_WIDGET_TIMER: + widget_battery_render(w, ev_data); + break; + case EV_WIDGET_TOUCH_UP: + case EV_WIDGET_TOUCH_DOWN: + case EV_WIDGET_DESTROY: + default: // EV_WIDGET_NONE + break; + } +}