138 lines
3.7 KiB
C++
138 lines
3.7 KiB
C++
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "mgos.h"
|
|
#include "common/cs_dbg.h"
|
|
#include "common/json_utils.h"
|
|
#include "common/platform.h"
|
|
#include "frozen/frozen.h"
|
|
#include "mgos_rpc.h"
|
|
#include "common/cs_file.h"
|
|
#include "mjs.h"
|
|
|
|
#include <Arduino.h>
|
|
#include "Adafruit_SSD1306dj.h"
|
|
|
|
#include "Battery.h"
|
|
#include "Ui.h"
|
|
#include "ClockClient.h"
|
|
#include "appDebug.h"
|
|
|
|
static Battery battery = Battery();
|
|
static ClockClient clockClient = ClockClient();
|
|
static Ui ui = Ui();
|
|
|
|
#if CS_PLATFORM == CS_P_ESP8266
|
|
/* On ESP-12E there is a blue LED connected to GPIO2 (aka U1TX). */
|
|
#define LED_GPIO 2
|
|
#define BUTTON_GPIO 0 /* Usually a "Flash" button. */
|
|
#define BUTTON_PULL MGOS_GPIO_PULL_UP
|
|
#define BUTTON_EDGE MGOS_GPIO_INT_EDGE_POS
|
|
#elif CS_PLATFORM == CS_P_ESP32
|
|
/* Unfortunately, there is no LED on DevKitC, so this is random GPIO. */
|
|
#define LED_GPIO 17
|
|
#define BUTTON_GPIO 0 /* Usually a "Flash" button. */
|
|
#define BUTTON_PULL MGOS_GPIO_PULL_UP
|
|
#define BUTTON_EDGE MGOS_GPIO_INT_EDGE_POS
|
|
#endif
|
|
|
|
|
|
|
|
static void blink_timer_cb(void *arg) {
|
|
bool current_level = mgos_gpio_toggle(LED_GPIO);
|
|
// LOG(LL_INFO, ("%s", (current_level ? "Tick" : "Tock")));
|
|
(void) arg;
|
|
(void) current_level;
|
|
}
|
|
|
|
static void button_cb(int pin, void *arg) {
|
|
LOG(LL_INFO, ("Click!"));
|
|
(void) pin;
|
|
(void) arg;
|
|
}
|
|
|
|
static void getBattery_handler(struct mg_rpc_request_info *ri, void *cb_arg,
|
|
struct mg_rpc_frame_info *fi, struct mg_str args) {
|
|
struct mbuf fb;
|
|
struct json_out out = JSON_OUT_MBUF(&fb);
|
|
|
|
mbuf_init(&fb, 20);
|
|
|
|
json_printf(&out, "{load: %d, voltage_mV: %d}", batteryGetPercentage(), batteryGetVoltage_mV());
|
|
|
|
mgos_gpio_toggle(LED_GPIO);
|
|
|
|
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
|
ri = NULL;
|
|
|
|
mbuf_free(&fb);
|
|
|
|
(void) cb_arg;
|
|
(void) fi;
|
|
(void) args;
|
|
}
|
|
|
|
int get_led_gpio_pin(void) {
|
|
return LED_GPIO;
|
|
}
|
|
|
|
enum mgos_app_init_result mgos_app_init(void) {
|
|
LOG(LL_DEBUG, ("Starting app %s", mgos_sys_config_get_app_title()));
|
|
|
|
// if (!init_fastclock_client(mgos_get_mgr())) return MGOS_APP_INIT_ERROR;
|
|
|
|
/* Set up the blinky timer. */
|
|
mgos_gpio_set_mode(LED_GPIO, MGOS_GPIO_MODE_OUTPUT);
|
|
mgos_set_timer(1000 /* ms */, true /* repeat */, blink_timer_cb, NULL);
|
|
|
|
/* Set up a button handler */
|
|
mgos_gpio_set_button_handler(BUTTON_GPIO, BUTTON_PULL, BUTTON_EDGE,
|
|
50 /* debounce_ms */, button_cb, NULL);
|
|
|
|
/* Set up RPC */
|
|
struct mg_rpc *c = mgos_rpc_get_global();
|
|
mg_rpc_add_handler(c, "ClockClient.GetBattery", "{num: %d}", getBattery_handler, NULL);
|
|
|
|
/* Initialize JavaScript engine */
|
|
int mem1, mem2, mem3;
|
|
mem1 = mgos_get_free_heap_size();
|
|
struct mjs *mjs = mjs_create();
|
|
mem2 = mgos_get_free_heap_size();
|
|
mjs_set_ffi_resolver(mjs, mgos_dlsym);
|
|
mjs_err_t err = mjs_exec_file(mjs, "init.js", 1, NULL);
|
|
if (err != MJS_OK) {
|
|
mjs_print_error(mjs, stdout, NULL, 1 /* print_stack_trace */);
|
|
}
|
|
mem3 = mgos_get_free_heap_size();
|
|
LOG(LL_INFO, ("mJS memory stat: before init: %d after init: %d after init.js: %d", mem1, mem2, mem3));
|
|
|
|
/* Startup app components */
|
|
ui.begin();
|
|
clockClient.begin();
|
|
clockClient.addClockChangeCallback(ui.setTime);
|
|
|
|
// not necessary to call the loop, as this is done by the Arduino lib
|
|
// mgos_timer_callback(1000, MGOS_TIMER_REPEAT, loop, NULL);
|
|
|
|
return MGOS_APP_INIT_SUCCESS;
|
|
}
|
|
|
|
void checkBattery() {
|
|
static int lastBatteryReadOn = 0;
|
|
|
|
if (millis() - lastBatteryReadOn > 30000) {
|
|
lastBatteryReadOn = millis();
|
|
// debug.out("Battery="); debug.out(battery.getPercentage()); debug.outln(" %");
|
|
LOG(LL_DEBUG, ("Battery=%d%%, %dmV", battery.getPercentage(), battery.getVoltage_mV()));
|
|
logHeap();
|
|
}
|
|
|
|
}
|
|
|
|
static void loop(void *args) {
|
|
checkBattery();
|
|
clockClient.loop();
|
|
ui.loop();
|
|
}
|
|
|