From d9692552279f0952e153cb6919d29c01e6c31bad Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Thu, 12 Apr 2018 10:59:07 +0200 Subject: [PATCH] Refactoring, cleanup, removing debugging prints. --- README.md | 42 +++- deps/ca-bundle | 1 + deps/cron | 1 + deps/crontab | 1 + deps/dash | 1 + deps/http-server | 1 + deps/i2c | 1 + deps/jstore | 1 + deps/location | 1 + deps/mongoose | 1 + deps/mongoose-os | 1 + deps/mqtt | 1 + deps/ota-http-client | 1 + deps/ota-shadow | 1 + deps/rpc-common | 1 + deps/rpc-loopback | 1 + deps/rpc-mqtt | 1 + deps/rpc-service-config | 1 + deps/rpc-service-cron | 1 + deps/rpc-service-fs | 1 + deps/rpc-service-ota | 1 + deps/rpc-uart | 1 + deps/shadow | 1 + deps/sntp | 1 + deps/spi | 1 + deps/wifi | 1 + mos.yml | 1 + src/QR204.h | 6 +- src/main.c | 424 ++-------------------------------------- 29 files changed, 86 insertions(+), 412 deletions(-) create mode 160000 deps/ca-bundle create mode 160000 deps/cron create mode 160000 deps/crontab create mode 160000 deps/dash create mode 160000 deps/http-server create mode 160000 deps/i2c create mode 160000 deps/jstore create mode 160000 deps/location create mode 160000 deps/mongoose create mode 160000 deps/mongoose-os create mode 160000 deps/mqtt create mode 160000 deps/ota-http-client create mode 160000 deps/ota-shadow create mode 160000 deps/rpc-common create mode 160000 deps/rpc-loopback create mode 160000 deps/rpc-mqtt create mode 160000 deps/rpc-service-config create mode 160000 deps/rpc-service-cron create mode 160000 deps/rpc-service-fs create mode 160000 deps/rpc-service-ota create mode 160000 deps/rpc-uart create mode 160000 deps/shadow create mode 160000 deps/sntp create mode 160000 deps/spi create mode 160000 deps/wifi diff --git a/README.md b/README.md index 1b34d6d..089ef4b 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,13 @@ This is about a device that can be called by WLAN / TCP/IP and prints on a cheap thermo printer using paper rolls. Hardware used: -- ESP-CPU: - -- Wemos ESP32 LoLin (any ESP32 should work), we are using UART1 (GPIO26=Tx) - -- Wemos ESP8266 (any should work), we are using UART1 Tx only (GPIO2=Tx) -- GOOJPRT QR204 Micro Embedded Printer with RS232/TTL and USB-Interface - -- TX, RX, GND connected to ESP32 - -- POWER connected to seperate power supply (+5V/GND) +- ESP-CPU: + - Wemos ESP32 LoLin (any ESP32 should work), we are using UART1 (GPIO26=Tx) + - Wemos ESP8266 (any should work), we are using UART1 Tx only (GPIO2=Tx) +- GOOJPRT QR204 Micro Embedded Printer with RS232/TTL and USB-Interface + - TX, RX, GND connected to ESP32 + - POWER connected to seperate power supply (+5V/GND) ## How to install this app @@ -21,3 +21,33 @@ Hardware used:

+ +## Interface description / API usage + +As Mongoose-OS is used as platform for this software, all Mongoose-OS supported RPC-Mechanisms are supported, which are: + +- USB: Easiest is to start browser based UI ("> mos ui") or by following command line:
+*mos --port /dev/ttyUSB0 call TP.Print '{"text": "Text to be printed"}'* +- MQTT: *mos --port mqtt://my.mqtt.server:1883/esp32_6732ac call TP.Print '{"text": "Text to be printed"}'* +- WS (WebSocket): *mos --port ws://192.168.0.206/rpc call TP.Print '{"text": "Text to be printed"}'* +- RESTful HTTP call: *curl -d '{"text": "Text to be printed"}' 192.168.0.206/rpc/TP.Print* + +## Printed Text Formatting + +The text to be printed is formatted using markdown notation (not fully supported, just "like markdown"). You may use following formattings: + +- lines starting with # are used as headlines, printed with double width and height +- inline formattings used: + - "\*" character is used to mark begin and end of emphasized strings, which are printed bold. Example: *Text to be \*emphasized** + - "\_" underline character is used to mark begin and end of underlined text. Example: *Text to be \_underlined_* + - Characters can be escaped using the "\\" character. This might be changed by configuration (config parameter tp.escapeCharacter, see Config-RPC service) + +## Configuration + +You may use the Mongoose-OS configuration service to change the behaviour and interface of this module. E.g. use *"> mos --port /dev/USB0 config-get"* to get the current configuration and *"> mos --port /dev/USB0 config-set key=value"* to set a configuration value. + +The only specific configuration is everything that is defined under the "tp" key. MQTT, Wifi, debugging and other settings may be changed and are available from Mongoose-OS anyway. A shortcut to set the Wifi settings is: *"> mos --port /dev/USB0 wifi WIFI_NAME WIFI_PASSWORD"*. + +## OTA + +The firmware may be updated using OTA services offered by Mongoose-OS (see https://mongoose-os.com/docs/book/ota.html). diff --git a/deps/ca-bundle b/deps/ca-bundle new file mode 160000 index 0000000..22b41ef --- /dev/null +++ b/deps/ca-bundle @@ -0,0 +1 @@ +Subproject commit 22b41ef50babf5f0dacf6f54832741a5b9e16ae4 diff --git a/deps/cron b/deps/cron new file mode 160000 index 0000000..fd6a685 --- /dev/null +++ b/deps/cron @@ -0,0 +1 @@ +Subproject commit fd6a6853d2a69cab4f09fcdd5c40d7570e6b42fb diff --git a/deps/crontab b/deps/crontab new file mode 160000 index 0000000..c10d620 --- /dev/null +++ b/deps/crontab @@ -0,0 +1 @@ +Subproject commit c10d62042dbba008328183c962907fdc0b93790e diff --git a/deps/dash b/deps/dash new file mode 160000 index 0000000..fd2fc40 --- /dev/null +++ b/deps/dash @@ -0,0 +1 @@ +Subproject commit fd2fc406df1944d3f619d14f75cc4508a4e97a4f diff --git a/deps/http-server b/deps/http-server new file mode 160000 index 0000000..f489eab --- /dev/null +++ b/deps/http-server @@ -0,0 +1 @@ +Subproject commit f489eabc4f7bc1c98f277ec8783eaec0491ba67b diff --git a/deps/i2c b/deps/i2c new file mode 160000 index 0000000..2189718 --- /dev/null +++ b/deps/i2c @@ -0,0 +1 @@ +Subproject commit 2189718e635e3e91b97d9b173b7f1f01416811c3 diff --git a/deps/jstore b/deps/jstore new file mode 160000 index 0000000..b2067da --- /dev/null +++ b/deps/jstore @@ -0,0 +1 @@ +Subproject commit b2067da60b487c1f3598321caab3db8695a141d2 diff --git a/deps/location b/deps/location new file mode 160000 index 0000000..5e8644b --- /dev/null +++ b/deps/location @@ -0,0 +1 @@ +Subproject commit 5e8644bbd64118b15a7c876fb4d61ae51858b1e4 diff --git a/deps/mongoose b/deps/mongoose new file mode 160000 index 0000000..7d696f6 --- /dev/null +++ b/deps/mongoose @@ -0,0 +1 @@ +Subproject commit 7d696f6bbadf4bd69919f18de4293df5aee22750 diff --git a/deps/mongoose-os b/deps/mongoose-os new file mode 160000 index 0000000..bac8bbf --- /dev/null +++ b/deps/mongoose-os @@ -0,0 +1 @@ +Subproject commit bac8bbfa561f71072b5285fcc0745df5ceab8c5b diff --git a/deps/mqtt b/deps/mqtt new file mode 160000 index 0000000..0ff92f3 --- /dev/null +++ b/deps/mqtt @@ -0,0 +1 @@ +Subproject commit 0ff92f39f10e001833e1215f9ffab25cb99109fa diff --git a/deps/ota-http-client b/deps/ota-http-client new file mode 160000 index 0000000..544c8a7 --- /dev/null +++ b/deps/ota-http-client @@ -0,0 +1 @@ +Subproject commit 544c8a7f130f4438c8d3b547df673c5ea5011263 diff --git a/deps/ota-shadow b/deps/ota-shadow new file mode 160000 index 0000000..af98060 --- /dev/null +++ b/deps/ota-shadow @@ -0,0 +1 @@ +Subproject commit af9806062060bca2ca99e50fe3997f947b9974bd diff --git a/deps/rpc-common b/deps/rpc-common new file mode 160000 index 0000000..ab63fe4 --- /dev/null +++ b/deps/rpc-common @@ -0,0 +1 @@ +Subproject commit ab63fe41f2f1edae6a35f1a27efc2668dbb36cac diff --git a/deps/rpc-loopback b/deps/rpc-loopback new file mode 160000 index 0000000..a9adcc3 --- /dev/null +++ b/deps/rpc-loopback @@ -0,0 +1 @@ +Subproject commit a9adcc32ab43c755243b25b9e3980a7d4be793df diff --git a/deps/rpc-mqtt b/deps/rpc-mqtt new file mode 160000 index 0000000..7376383 --- /dev/null +++ b/deps/rpc-mqtt @@ -0,0 +1 @@ +Subproject commit 73763832318897ab4af64e42916ccc14e7fcfe65 diff --git a/deps/rpc-service-config b/deps/rpc-service-config new file mode 160000 index 0000000..354f394 --- /dev/null +++ b/deps/rpc-service-config @@ -0,0 +1 @@ +Subproject commit 354f394ea6142cf7958d1c03464326177cc90a4f diff --git a/deps/rpc-service-cron b/deps/rpc-service-cron new file mode 160000 index 0000000..346e701 --- /dev/null +++ b/deps/rpc-service-cron @@ -0,0 +1 @@ +Subproject commit 346e701e7ea8c3b786cf0efe2c9fc1feb7a28da7 diff --git a/deps/rpc-service-fs b/deps/rpc-service-fs new file mode 160000 index 0000000..fec1c77 --- /dev/null +++ b/deps/rpc-service-fs @@ -0,0 +1 @@ +Subproject commit fec1c77df6513717037bafefd3cc70e1969a8dd7 diff --git a/deps/rpc-service-ota b/deps/rpc-service-ota new file mode 160000 index 0000000..257799c --- /dev/null +++ b/deps/rpc-service-ota @@ -0,0 +1 @@ +Subproject commit 257799cc350bbfa209ba0d554a38a5d6b89e08c1 diff --git a/deps/rpc-uart b/deps/rpc-uart new file mode 160000 index 0000000..1d4c01f --- /dev/null +++ b/deps/rpc-uart @@ -0,0 +1 @@ +Subproject commit 1d4c01f1932ebf49b8d4b8d54fa8b74f393432f9 diff --git a/deps/shadow b/deps/shadow new file mode 160000 index 0000000..a01a100 --- /dev/null +++ b/deps/shadow @@ -0,0 +1 @@ +Subproject commit a01a100eebda63201ddb85f68ad496c72382d34e diff --git a/deps/sntp b/deps/sntp new file mode 160000 index 0000000..2df8902 --- /dev/null +++ b/deps/sntp @@ -0,0 +1 @@ +Subproject commit 2df8902be660d5875e204b8e92686b125a49e6cb diff --git a/deps/spi b/deps/spi new file mode 160000 index 0000000..d956323 --- /dev/null +++ b/deps/spi @@ -0,0 +1 @@ +Subproject commit d956323bcadc0caf1302b183cd010ed8dfa8ccc9 diff --git a/deps/wifi b/deps/wifi new file mode 160000 index 0000000..2444a42 --- /dev/null +++ b/deps/wifi @@ -0,0 +1 @@ +Subproject commit 2444a4268be2e9dec28363a247743199298cd611 diff --git a/mos.yml b/mos.yml index e369317..6dc92ce 100644 --- a/mos.yml +++ b/mos.yml @@ -35,6 +35,7 @@ config_schema: # - ["i2c.scl_gpio", 5] # D1 - ["tp", "o", {title: "Thermo printer controller settings"}] - ["tp.escapeCharacter", "i", 27, {title: "ASCII code of character, that is to be used as escape character in text interpretation"}] + - ["tp.printSystemInfoOnStartup", "b", false, {title: "Prints some technical info about CPU/System resources after boot"}] # - ["mqtt.enable", true] # - ["mqtt.server", "mqtt.pmpark.de:1883"] # - ["mqtt.user", "default"] diff --git a/src/QR204.h b/src/QR204.h index df50ee3..598df30 100644 --- a/src/QR204.h +++ b/src/QR204.h @@ -1,6 +1,6 @@ -#ifndef __QR204_H -#define __QR204_H +#ifndef QR204_H_included +#define QR204_H_included #include "mgos.h" #include "mgos_system.h" @@ -22,5 +22,5 @@ extern void tp_reset_mode(uint8_t removeModes); extern void tp_reverse_feed(uint8_t numLines); extern void tp_linefeed(uint8_t numLines); extern void tp_print_text(char *text); -#endif +#endif diff --git a/src/main.c b/src/main.c index 546a58f..f46b720 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,7 @@ #include "QR204.h" -static bool doPrint = false; +static bool doPrintInTimer = false; #if CS_PLATFORM == CS_P_ESP32 #define UART_NO 1 @@ -42,34 +42,27 @@ static void timer_cb(void *arg) { * Note: do not use mgos_uart_write to output to console UART (0 in our case). * It will work, but output may be scrambled by console debug output. */ - printf("Timer loop!\n"); + // printf("Timer loop!\n"); - if (doPrint) { + if (doPrintInTimer) { tp_reset_mode(TP_MODE_ALL); tp_print(UART_NO, "Timer loop!\n"); tp_set_mode(TP_MODE_UNDERLINED); tp_print(UART_NO, "0,123456789\n"); + #if CS_PLATFORM == CS_P_ESP32 + uint32_t hall = hall_sens_read(); + uint8_t temp = temprature_sens_read(); + esp_chip_info_t ci; + esp_chip_info(&ci); + tp_print(UART_NO, "t=%.1f C, h=%ld mH, tasks=%d\n", (float) (temp-32)/1.8 - tempOffset, hall, uxTaskGetNumberOfTasks()); + #elif CS_PLATFORM == CS_P_ESP8266 + tp_print(UART_NO, + "mem=%d kB, free=%d kB, fs=%d kB\n", + (int) mgos_get_heap_size()/1024, + (int) mgos_get_free_heap_size()/1024, + (int) mgos_get_fs_size()/1024); + #endif } - /* tp_print(UART_NO, "- -\n"); - tp_print(UART_NO, " - - \n"); - tp_reverse_feed(2); - tp_print(UART_NO, " - - \n"); - tp_print(UART_NO, "- -\n");*/ - - -#if CS_PLATFORM == CS_P_ESP32 - uint32_t hall = hall_sens_read(); - uint8_t temp = temprature_sens_read(); - esp_chip_info_t ci; - esp_chip_info(&ci); - tp_print(UART_NO, "t=%.1f C, h=%ld mH, tasks=%d\n", (float) (temp-32)/1.8 - tempOffset, hall, uxTaskGetNumberOfTasks()); -#elif CS_PLATFORM == CS_P_ESP8266 - tp_print(UART_NO, - "mem=%d kB, free=%d kB, fs=%d kB\n", - (int) mgos_get_heap_size()/1024, - (int) mgos_get_free_heap_size()/1024, - (int) mgos_get_fs_size()/1024); -#endif (void) arg; } @@ -147,389 +140,14 @@ enum mgos_app_init_result mgos_app_init(void) { mg_rpc_add_handler(c, "TP.Print", "{text: %Q}", rpc_tpPrint, NULL); tp_reset_mode(TP_MODE_ALL); + /* tp_print(UART_NO, "TEST Print\r"); tp_print(UART_NO, "----------\n"); - printSystemInfo(); + */ + if (mgos_sys_config_get_tp_printSystemInfoOnStartup()) { + printSystemInfo(); + } return MGOS_APP_INIT_SUCCESS; } - -/* -------------------------------------------------------------------- */ - -/* ======================================= - -static void recalcTimings() { - flashLightRampUp_deltaSpeed = (flashLightSpeed * motorUpdateTime_msec) / motorRampUpTime_msec; - flashLightRampDown_deltaSpeed = (flashLightSpeed * motorUpdateTime_msec) / motorRampDownTime_msec; -} - -static void pubStatus(const char *statusString, double percentage) { - struct mbuf fb; - struct json_out out = JSON_OUT_MBUF(&fb); - - if (!mqttConnected) return; - - mbuf_init(&fb, 30); - if (mgos_mqtt_global_connect()) { - json_printf(&out, "{statusString: %Q, speed: %f}", statusString, percentage); - mgos_mqtt_pub(pubStatusTopic, fb.buf, fb.len, 0, true); - } - -} - -static void motor_timer_cb(void *arg) { - static bool stopped = false; - - switch (motorStatus) { - case MotorStatus_Off: - wemos_motor_stop(M1); - break; - case MotorStatus_On: - wemos_motor_setmotor(M1, motorDirection, pwm); - break; - case MotorStatus_RampUp: - pwm += flashLightRampUp_deltaSpeed; - if (pwm >= flashLightSpeed) { - LOG(LL_INFO, ("MotorStatus_RampUp: Speed target reached")); - pwm = flashLightSpeed; - motorStatus = MotorStatus_On; - } - wemos_motor_setmotor(M1, motorDirection, pwm); - // LOG(LL_INFO, ("M1, dir=%d, pwm=%f", motorDirection, pwm)); - break; - case MotorStatus_RampDown: - pwm -= flashLightRampDown_deltaSpeed; - if (pwm <= flashLightTargetSpeed) { - if (pwm <= 0.0) { - motorStatus = MotorStatus_Off; - pwm = 0.0; - wemos_motor_stop(M1); - // change direction for next time, when motor turns on again - if (motorDirection == _CW) { - motorDirection = _CCW; - } else { - motorDirection = _CW; - } - } else { - pwm = flashLightTargetSpeed; - LOG(LL_INFO, ("MotorStatus_RampDown: Speed target reached")); - motorStatus = MotorStatus_On; - wemos_motor_setmotor(M1, motorDirection, pwm); - } - } else { - wemos_motor_setmotor(M1, motorDirection, pwm); - // LOG(LL_INFO, ("M1, dir=%d, pwm=%f", motorDirection, pwm)); - } - break; - case MotorStatus_DemoMode: - default: - if (pwm > flashLightSpeed + 5.0) { - pwm = 0.0; // start again - stopped = false; - } else { - pwm += 0.1; - if (!stopped) { - wemos_motor_setmotor(M1, motorDirection, pwm); - LOG(LL_INFO, ("M1, dir=%d, pwm=%f", motorDirection, pwm)); - pubStatus("on", pwm); - - if (pwm > flashLightSpeed) { - // wemos_motor_setmotor(M1, _STOP, 0.0); - wemos_motor_setmotor(M1, _STANDBY, pwm); - stopped = true; - LOG(LL_INFO, ("Stopped/Standby")); - pubStatus("off", 0); - } - } - } - break; - } - (void) arg; -} - -static void flashLightOn() { - LOG(LL_INFO, ("FlashLight ON\n")); - flashLightTargetSpeed = flashLightSpeed; - motorStatus = MotorStatus_RampUp; // this starts the motor on next timer callback - pubStatus("on", flashLightSpeed); -} - -static void rpc_flashLightOn(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); - - flashLightOn(); - mbuf_init(&fb, 100); - json_printf(&out, "{result: 0, resultString: %Q}", "OK"); - mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf); - ri = NULL; - mbuf_free(&fb); - - (void) cb_arg; - (void) fi; - (void) args; -} - -static void cron_flashLightOn(struct mg_str action, struct mg_str payload, void *userdata) { - LOG(LL_INFO, ("Crontab flashLightOn fired")); - flashLightOn(); - (void) action; - (void) payload; - (void) userdata; -} - -static void flashLightOff() { - LOG(LL_INFO, ("FlashLight OFF\n")); - flashLightTargetSpeed = 0.0; - motorStatus = MotorStatus_RampDown; // this stops the motor on next timer callback - pubStatus("off", 0); -} - -static void rpc_flashLightOff(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); - - flashLightOff(); - mbuf_init(&fb, 100); - json_printf(&out, "{result: 0, resultString: %Q}", "OK"); - mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf); - ri = NULL; - mbuf_free(&fb); - - (void) cb_arg; - (void) fi; - (void) args; -} - -static void cron_flashLightOff(struct mg_str action, struct mg_str payload, void *userdata) { - LOG(LL_INFO, ("Crontab flashLightOff fired")); - flashLightOff(); - (void) action; - (void) payload; - (void) userdata; -} - -static void cron_init() { - mgos_crontab_register_handler(mg_mk_str("FlashLightOn"), cron_flashLightOn, NULL); - mgos_crontab_register_handler(mg_mk_str("FlashLightOff"), cron_flashLightOff, NULL); -} - -static void flashLightSetSpeed(uint16_t newSpeed) { - flashLightTargetSpeed = (double) newSpeed; - flashLightSpeed = flashLightTargetSpeed; - if (pwm < flashLightTargetSpeed) { - motorStatus = MotorStatus_RampUp; - } else if (pwm > flashLightTargetSpeed) { - motorStatus = MotorStatus_RampDown; - } -} - -static void rpc_flashLightSetSpeed(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, 100); - - int speed = 0; - - if (json_scanf(args.p, args.len, ri->args_fmt, &speed) == 1) { - printf("FlashLight set speed to %d\n", speed); - json_printf(&out, "{result: 0, resultString: %Q, speed: %d}", "OK", speed); - flashLightSetSpeed(speed); - } else { - json_printf(&out, "{error: %Q}", "speed is required"); - } - - mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf); - ri = NULL; - mbuf_free(&fb); - recalcTimings(); - - (void) cb_arg; - (void) fi; - (void) args; -} - -static void flashLightSetRampupTime(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, 100); - - uint16_t rampUpTime_msec = 0; - - if (json_scanf(args.p, args.len, ri->args_fmt, &rampUpTime_msec) == 1) { - motorRampUpTime_msec = rampUpTime_msec; - printf("FlashLight set motor ramp up time to %d\n", rampUpTime_msec); - json_printf(&out, "{result: 0, resultString: %Q, rampUpTime_ms: %d}", "OK", rampUpTime_msec); - } else { - json_printf(&out, "{error: %Q}", "rampUpTime_msec is required"); - } - - mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf); - ri = NULL; - mbuf_free(&fb); - recalcTimings(); - - (void) cb_arg; - (void) fi; - (void) args; -} - -static void flashLightSetRampdownTime(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, 100); - - uint16_t rampDownTime_msec = 0; - - if (json_scanf(args.p, args.len, ri->args_fmt, &rampDownTime_msec) == 1) { - motorRampDownTime_msec = rampDownTime_msec; - printf("FlashLight set motor ramp up time to %d\n", rampDownTime_msec); - json_printf(&out, "{result: 0, resultString: %Q, rampDownTime_ms: %d}", "OK", rampDownTime_msec); - } else { - json_printf(&out, "{error: %Q}", "rampDownTime_msec is required"); - } - - mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf); - ri = NULL; - mbuf_free(&fb); - recalcTimings(); - - (void) cb_arg; - (void) fi; - (void) args; -} - -static void flashLightSetMotorUpdateTime(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, 100); - - uint16_t updateTime = 0; - - if (json_scanf(args.p, args.len, ri->args_fmt, &updateTime) == 1) { - motorUpdateTime_msec = updateTime; - printf("FlashLight set updateTime_msec tp %d\n", updateTime); - json_printf(&out, "{result: 0, resultString: %Q, udateTime_ms: %d}", "OK", updateTime); - } else { - json_printf(&out, "{error: %Q}", "motorUpdateTime_msec is required"); - } - - mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf); - ri = NULL; - mbuf_free(&fb); - recalcTimings(); - - (void) cb_arg; - (void) fi; - (void) args; -} - -static void flashLightGetSettings(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, 1024); - - json_printf(&out, "{pwm: %f, speed: %f, rampupTime_ms: %d, rampdownTime_ms: %d, updateTime_ms: %d, rampupDeltaSpeed: %f, rampdownDeltaSpeed: %f, motorDirection: %d, clientId: %Q, commandTopic: %Q, statusTopic: %Q, mqttConnected: %Q, motorStatus: %d}", - pwm, flashLightSpeed, motorRampUpTime_msec, motorRampDownTime_msec, motorUpdateTime_msec, flashLightRampUp_deltaSpeed, flashLightRampDown_deltaSpeed, - motorDirection, clientId, commandTopic, pubStatusTopic, mqttConnected ? "true" : "false", motorStatus); - - mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf); - ri = NULL; - mbuf_free(&fb); - - (void) cb_arg; - (void) fi; - (void) args; -} - -void net_changed(int ev, void *evd, void *arg) { - if (ev != MGOS_NET_EV_IP_ACQUIRED) return; - // call_peer(); - (void) evd; - (void) arg; -} - -static void mqttCommandHandler(struct mg_connection *c, const char *topic, int topic_len, - const char *msg, int msg_len, void *userdata) { - LOG(LL_INFO, ("Got message on topic %.*s", topic_len, topic)); - - (void) c; - (void) topic; - (void) topic_len; - (void) msg; - (void) msg_len; - (void) userdata; -} - -// void onMqttConnection(struct mg_connection *c, const char *client_id, struct mg_send_mqtt_handshake_opts *opts, void *fn_arg) { -void onMqttConnection(struct mg_connection *c, const char *client_id, struct mg_send_mqtt_handshake_opts *opts, void *fn_arg) { - // add MQTT cmd subscription - LOG(LL_INFO, ("onMqttConnection handler called with clientId=%s", client_id)); - #if 0 - #endif - mgos_mqtt_sub(commandTopic, mqttCommandHandler, NULL); - mqttConnected = true; - - (void) c; - (void) client_id; - (void) opts; - (void) fn_arg; -} - -enum mgos_app_init_result mgos_app_init(void) { - struct mg_rpc *c = mgos_rpc_get_global(); - mg_rpc_add_handler(c, "FlashLight.On", NULL, rpc_flashLightOn, NULL); - mg_rpc_add_handler(c, "FlashLight.Off", NULL, rpc_flashLightOff, NULL); - mg_rpc_add_handler(c, "FlashLight.Speed", "{speed: %d}", rpc_flashLightSetSpeed, NULL); - mg_rpc_add_handler(c, "FlashLight.RampUpTime_msec", "{rampUpTime_ms: %d}", flashLightSetRampupTime, NULL); - mg_rpc_add_handler(c, "FlashLight.RampDownTime_msec", "{rampDownTime_ms: %d}", flashLightSetRampdownTime, NULL); - mg_rpc_add_handler(c, "FlashLight.MotorUpdateTime_msec", "{uptdateTime_ms: %d}", flashLightSetMotorUpdateTime, NULL); - mg_rpc_add_handler(c, "FlashLight.GetSettings", NULL, flashLightGetSettings, NULL); - mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, net_changed, NULL); - - // enable crontab - cron_init(); - - // add MQTT cmd subscription - LOG(LL_INFO, ("Initializing MQTT")); - clientId = mgos_sys_config_get_mqtt_client_id(); - clientId = clientId ? clientId : mgos_sys_config_get_device_id(); - LOG(LL_INFO, ("clientId=%s", clientId)); - LOG(LL_INFO, ("cmdTopic=%s", mgos_sys_config_get_flashLight_mqttCtrlTopic())); - LOG(LL_INFO, ("pubStatusTopic=%s", mgos_sys_config_get_flashLight_mqttStatusTopic())); - c_snprintf(commandTopic, sizeof(commandTopic), mgos_sys_config_get_flashLight_mqttCtrlTopic(), clientId); - c_snprintf(pubStatusTopic, sizeof(pubStatusTopic), mgos_sys_config_get_flashLight_mqttStatusTopic(), clientId); - LOG(LL_INFO, ("cmdTopic=%s", commandTopic)); - LOG(LL_INFO, ("pubStatusTopic=%s", pubStatusTopic)); - mgos_mqtt_set_connect_fn(onMqttConnection, NULL); - - motorFrequency = mgos_sys_config_get_flashLight_motorFrequency(); - LOG(LL_INFO, ("motorFrequency=%d Hz", motorFrequency)); - motorAddress = mgos_sys_config_get_flashLight_address(); - LOG(LL_INFO, ("motorAddress=%d", motorAddress)); - LOG(LL_INFO, ("Initializing motor controller")); - recalcTimings(); - wemos_motor_init(); - LOG(LL_INFO, ("Initializing motor M1")); - wemos_motor_initMotor(M1, motorAddress, motorFrequency); - LOG(LL_INFO, ("Setting up timer")); - motorUpdateTime_msec = mgos_sys_config_get_flashLight_motorUpdateTime(); - mgos_set_timer(motorUpdateTime_msec, MGOS_TIMER_REPEAT, motor_timer_cb, NULL); - LOG(LL_INFO, ("Initialization done")); - return MGOS_APP_INIT_SUCCESS; -} - - ======================================= */ \ No newline at end of file