153 lines
4.1 KiB
C
153 lines
4.1 KiB
C
#include <stdio.h>
|
|
|
|
#include "mgos.h"
|
|
#include "mgos_app.h"
|
|
#include "mgos_gpio.h"
|
|
#include "mgos_system.h"
|
|
#include "mgos_timers.h"
|
|
#include "mgos_uart.h"
|
|
#include "mgos_rpc.h"
|
|
#include "mgos_sys_config.h"
|
|
#include "mgos_mqtt.h"
|
|
#include "mgos_net.h"
|
|
#include "mgos_crontab.h"
|
|
#include "common/mbuf.h"
|
|
#include "common/platform.h"
|
|
#include "common/cs_dbg.h"
|
|
#include "common/json_utils.h"
|
|
#include "common/mg_str.h"
|
|
#include "common/str_util.h"
|
|
#if CS_PLATFORM == CS_P_ESP32
|
|
#include <esp_system.h>
|
|
#endif
|
|
|
|
#include "QR204.h"
|
|
|
|
static bool doPrintInTimer = false;
|
|
|
|
#if CS_PLATFORM == CS_P_ESP32
|
|
#define UART_NO 1
|
|
int esp32_uart_rx_fifo_len(int uart_no);
|
|
extern uint8_t temprature_sens_read();
|
|
extern uint32_t hall_sens_read();
|
|
static int tempOffset = 17;
|
|
#elif CS_PLATFORM == CS_P_ESP8266
|
|
#define UART_NO 1
|
|
#else
|
|
#error Unsupported platform
|
|
#endif
|
|
|
|
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");
|
|
|
|
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
|
|
}
|
|
|
|
(void) arg;
|
|
}
|
|
|
|
static void printSystemInfo() {
|
|
#if CS_PLATFORM == CS_P_ESP32
|
|
esp_chip_info_t ci;
|
|
esp_chip_info(&ci);
|
|
tp_print(UART_NO,
|
|
"ESP32 mod=%d, cores=%d, rev=%d\n",
|
|
ci.model, ci.cores, ci.revision);
|
|
#elif CS_PLATFORM == CS_P_ESP8266
|
|
tp_print(UART_NO,
|
|
"ESP8266: cpu=%d MHz\n",
|
|
(int) mgos_get_cpu_freq() / 1000000);
|
|
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
|
|
}
|
|
|
|
// RPC Interfaces
|
|
static void rpc_tpPrint(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);
|
|
char *text = NULL;
|
|
|
|
mbuf_init(&fb, 100);
|
|
|
|
if (json_scanf(args.p, args.len, ri->args_fmt, &text) == 1) {
|
|
LOG(LL_INFO, ("TP print text: %s\n", text));
|
|
json_printf(&out, "{result: 0, resultString: %Q}", "OK");
|
|
tp_print_text(text);
|
|
} else {
|
|
json_printf(&out, "{error: %Q}", "text is required");
|
|
}
|
|
|
|
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
|
ri = NULL;
|
|
mbuf_free(&fb);
|
|
|
|
(void) cb_arg;
|
|
(void) fi;
|
|
(void) args;
|
|
}
|
|
|
|
|
|
enum mgos_app_init_result mgos_app_init(void) {
|
|
struct mgos_uart_config ucfg;
|
|
mgos_uart_config_set_defaults(UART_NO, &ucfg);
|
|
/*
|
|
* At this point it is possible to adjust baud rate, pins and other settings.
|
|
* 115200 8-N-1 is the default mode, but we set it anyway
|
|
*/
|
|
ucfg.baud_rate = 9600;
|
|
ucfg.num_data_bits = 8;
|
|
ucfg.parity = MGOS_UART_PARITY_NONE;
|
|
ucfg.stop_bits = MGOS_UART_STOP_BITS_1;
|
|
if (!mgos_uart_configure(UART_NO, &ucfg)) {
|
|
LOG(LL_ERROR,("ERROR: Cannot configure uart %d", UART_NO));
|
|
return MGOS_APP_INIT_ERROR;
|
|
}
|
|
|
|
mgos_uart_set_rx_enabled(UART_NO, false);
|
|
printf("Initial printer operation");
|
|
tp_init(UART_NO);
|
|
|
|
mgos_set_timer(60000 /* ms */, true /* repeat */, timer_cb, NULL /* arg */);
|
|
|
|
// Initialize RPC interfaces
|
|
struct mg_rpc *c = mgos_rpc_get_global();
|
|
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");
|
|
*/
|
|
if (mgos_sys_config_get_tp_printSystemInfoOnStartup()) {
|
|
printSystemInfo();
|
|
}
|
|
|
|
return MGOS_APP_INIT_SUCCESS;
|
|
}
|
|
|