- For esp8266 (lightswitch w/o touchscreen), set tft.enable=false - For esp32 (lightswitch w/ touchscreen), set it to true. Clean up mos.yml flags per architecture type. - Button is always 0,2 - Relay0 at 16 for ESP8266, as that's NodeMCU v2's onboard LED. - Relay0 at 13 for ESP32, as that's Featherwing's onboard LED
84 lines
2.4 KiB
C
84 lines
2.4 KiB
C
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
#include "mgos.h"
|
|
#include "mgos_mqtt.h"
|
|
#include "mongoose-touch.h"
|
|
#include "fonts/FreeSerifBold9pt7b.h"
|
|
#include "fonts/FreeMonoBold9pt7b.h"
|
|
#include "mgos_config.h"
|
|
#include "buttons.h"
|
|
#include "relays.h"
|
|
#include "mqtt.h"
|
|
#include "rpc.h"
|
|
|
|
struct screen_t *s_screen = NULL;
|
|
|
|
static void touch_handler(struct mgos_stmpe610_event_data *ed) {
|
|
struct widget_t *widget;
|
|
|
|
if (!ed) return;
|
|
LOG(LL_INFO, ("Touch %s at (%d,%d) pressure=%d, length=%d", ed->direction==TOUCH_UP?"UP":"DOWN", ed->x, ed->y, ed->z, ed->length));
|
|
|
|
// If the backlight is inactive, grab the TOUCH_DOWN and TOUCH_UP events.
|
|
// Do nothing on TOUCH_DOWN, but reactivate the backlight by calling
|
|
// backlight_keepalive() upon TOUCH_UP. This way, the first touch will
|
|
// wake up the UI, but not send events to widgets and things.
|
|
if (!backlight_active()) {
|
|
if (ed->direction==TOUCH_DOWN) {
|
|
LOG(LL_INFO, ("Ignoring touch event, backlight is inactive"));
|
|
return;
|
|
}
|
|
LOG(LL_INFO, ("Ignoring touch event, but setting backlight on"));
|
|
backlight_keepalive();
|
|
return;
|
|
}
|
|
|
|
backlight_keepalive();
|
|
|
|
widget = screen_widget_find_by_xy(s_screen, ed->x, ed->y);
|
|
|
|
if (ed->direction==TOUCH_DOWN) {
|
|
if (widget && widget->handler)
|
|
widget->handler(EV_WIDGET_TOUCH_DOWN, widget, ed);
|
|
} else {
|
|
if (widget && widget->handler)
|
|
widget->handler(EV_WIDGET_TOUCH_UP, widget, ed);
|
|
}
|
|
}
|
|
|
|
static void tft_init(void)
|
|
{
|
|
mgos_ili9341_set_dimensions(mgos_sys_config_get_tft_width(), mgos_sys_config_get_tft_height());
|
|
mgos_ili9341_set_rotation(mgos_sys_config_get_tft_orientation());
|
|
mgos_stmpe610_set_handler(touch_handler);
|
|
mgos_stmpe610_set_dimensions(mgos_ili9341_get_screenWidth(), mgos_ili9341_get_screenHeight());
|
|
|
|
mgos_ili9341_set_fgcolor(0,0,0);
|
|
mgos_ili9341_fillScreen();
|
|
|
|
s_screen = screen_create_from_file("/screen_main.json", widget_default_ev, NULL);
|
|
if (!s_screen) {
|
|
LOG(LL_ERROR, ("Could not load screen"));
|
|
return;
|
|
}
|
|
screen_add_default_widgets(s_screen);
|
|
screen_widget_broadcast(s_screen, EV_WIDGET_DRAW, NULL);
|
|
LOG(LL_INFO, ("Screen '%s' has %d widgets", s_screen->name, screen_get_num_widgets(s_screen)));
|
|
|
|
backlight_init();
|
|
}
|
|
|
|
enum mgos_app_init_result mgos_app_init(void)
|
|
{
|
|
buttons_init(mgos_sys_config_get_app_buttons());
|
|
relays_init(mgos_sys_config_get_app_relays());
|
|
mqtt_init();
|
|
rpc_init();
|
|
|
|
if (mgos_sys_config_get_tft_enable())
|
|
tft_init();
|
|
|
|
return MGOS_APP_INIT_SUCCESS;
|
|
}
|