Working version including remote access

This commit is contained in:
Dirk Jahnke 2018-04-06 18:23:50 +02:00
parent 8ab81f735e
commit fb759c481d
4 changed files with 137 additions and 61 deletions

47
mos.yml
View File

@ -29,45 +29,22 @@ config_schema:
# - ["my_app.bool_value", "b", false, {title: "Some boolean value"}]
# - ["my_app.string_value", "s", "", {title: "Some string value"}]
# - ["my_app.int_value", "i", 123, {title: "Some integer value"}]
- ["i2c.enable", true]
- ["i2c.freq", 400]
- ["i2c.sda_gpio", 4] # D2
- ["i2c.scl_gpio", 5] # D1
- ["flashLight", "o", {title: "Flash light / alarm light settings"}]
- ["flashLight.address", "i", 0x30, {title: "i2c address of motor controller TB6612 (e.g. WEMOS)"}]
- ["flashLight.motorFrequency", "i", 500, {title: "Frequency of PWM in Hz"}]
- ["flashLight.motorUpdateTime", "i", 50, {title: "Time between motor updates in msec"}]
- ["flashLight.mqttCtrlTopic", "s", "flashLight/%s/ctrl", {title: "MQTT channel to subscribe to receive commands; %s is replaced by clientId"}]
- ["flashLight.mqttStatusTopic", "s", "flashLight/%s/status", {title: "MQTT channel to publish to send status change infos; %s is replaced by clientId"}]
- ["mqtt.enable", true]
- ["mqtt.server", "mqtt.pmpark.de:1883"]
- ["mqtt.user", "default"]
- ["mqtt.pass", "12345678"]
- ["mqtt.will_message", "offline"]
- ["mqtt.will_topic", "flashLight/"]
# - ["i2c.enable", true]
# - ["i2c.freq", 400]
# - ["i2c.sda_gpio", 4] # D2
# - ["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"}]
# - ["mqtt.enable", true]
# - ["mqtt.server", "mqtt.pmpark.de:1883"]
# - ["mqtt.user", "default"]
# - ["mqtt.pass", "12345678"]
# - ["mqtt.will_message", "offline"]
# - ["mqtt.will_topic", "flashLight/"]
- ["sntp.enable", true]
- ["sntp.server", "time.google.com"]
# These settings get compiled into the C structure, and can be accessed
# from the C code this way:
#
# printf("Hello from %s!\n", mgos_sys_config_get_device_id());
#
# Settings are cool: can be modified remotely without full firmware upgrade!
#
# To see all available compiled settings, buid the firmware and open
# build/gen/mgos_config.h file.
#
# Also, in this config_schema section, you can override existing
# settings that has been created by other libraries. For example, debug log
# level is 2 by default. For this firmware we can override it to 3:
#
# config_schema:
# - ["debug.level", 3]
# List of libraries used by this app, in order of initialisation
libs:
- origin: https://github.com/mongoose-os-libs/ca-bundle

View File

@ -4,12 +4,14 @@ static bool initialized = false;
static uint8_t uart = 0;
static const char initCmd[] = { 0x1b, '@' };
static char configuredEscapeCharacter = 0;
// ***** INIT *****
void tp_init(uint8_t uartNo) {
if (!initialized) {
uart = uartNo;
mgos_uart_write(uart, initCmd, sizeof(initCmd));
configuredEscapeCharacter = (char) mgos_sys_config_get_tp_escapeCharacter();
initialized = true;
}
}
@ -21,15 +23,26 @@ static uint8_t currentMode = 0;
void tp_set_mode(uint8_t addModes) {
currentMode |= addModes;
char setModeCmd[3] = {0x1b, '!', currentMode};
LOG(LL_INFO, ("Change mode to %x, tried to set modes %x", currentMode, addModes));
mgos_uart_write(uart, setModeCmd, 3);
}
void tp_reset_mode(uint8_t removeModes) {
currentMode &= ~removeModes;
char setModeCmd[3] = {0x1b, '!', currentMode};
LOG(LL_INFO, ("Change mode to %x, tried to remove modes %x", currentMode, removeModes));
mgos_uart_write(uart, setModeCmd, 3);
}
void tp_toggle_mode(uint8_t toggleModes) {
currentMode ^= toggleModes;
char setModeCmd[3] = {0x1b, '!', currentMode};
LOG(LL_INFO, ("Change mode to %x, tried to toggle modes %x", currentMode, toggleModes));
mgos_uart_write(uart, setModeCmd, 3);
}
// ***** End of MODE Handling *****
void tp_reverse_feed(uint8_t numLines) {
char cmd[3] = { 0x1b, 'e', numLines };
mgos_uart_write(uart, cmd, 3);
@ -45,28 +58,78 @@ void tp_cutpaper(bool fullcut) {
mgos_uart_write(uart, cmd, 3);
}
// ***** End of MODE Handling *****
#define ON true
#define OFF false
/*
PRINT #1, CHR$(&H1B);"@"; 'Initializes the printer (ESC @)
PRINT #1, CHR$(&H1B);"a";CHR$(1);'Specifies a centered printing position (ESC a)
PRINT #1, CHR$(&H1B);"!";CHR$(0); 'Specifies font A (ESC !)
PRINT #1, "January 14, 2002 15:00";
PRINT #1, CHR$(&H1B);"d";CHR$(3); 'Prints and 3 line feeding (ESC d)
PRINT #1, CHR$(&H1B);"a";CHR$(0); 'Selects the left print position (ESC a)
PRINT #1, CHR$(&H1B);"!";CHR$(1); 'Selects font B
PRINT #1, "TM-U210B"
PRINT #1, "TM-U210D"
PRINT #1, "PS-170"
PRINT #1, CHR$(&HA);
$20.00";CHR$(&HA); $21.00";CHR$(&HA); $17.00";CHR$(&HA);
'Line feeding (LF)
PRINT #1, CHR$(&H1B);"!";CHR$(17); 'Selects double-height mode
PRINT #1, "TOTAL $58.00"; CHR$(&HA);
PRINT #1, CHR$(&H1B);"!";CHR$(0); 'Cancels double-height mode
PRINT #1, "------------------------------";CHR$(&HA); PRINT #1, "PAID $60.00";CHR$(&HA); PRINT #1, "CHANGE $ 2.00";CHR$(&HA);
PRINT #1, CHR$(&H1D);"V";CHR$(66);CHR$(0); 'Feeds paper & cut
Drawer Kick (ESC p)
PRINT #1, CHR$(&H1B); CHR$(&H70); CHR$(&H0); CHR$(60); CHR$(120);
static bool escaped = false;
static bool start_of_line = true;
*/
void tp_print_text(char *text) {
char *currentChar = text;
char previousChar = 0;
char *endChar = &text[strlen(text)];
bool ignoreAfterSpecialChar = false;
start_of_line = true;
escaped = false;
for (currentChar = text; currentChar <= endChar; previousChar = *currentChar, currentChar++) {
if (*currentChar == configuredEscapeCharacter) {
*currentChar = '\\';
}
switch (*currentChar) {
case 0: // end of string, do not print; flush print buffers by sending new line
tp_reset_mode(TP_MODE_ALL);
mgos_uart_write(uart, "\n", 1);
LOG(LL_INFO, ("0-Byte reached, ending processing of text"));
start_of_line = true;
break;
case '\n': // end of line (any character can be used): stop heading mode if necessary
case '\r':
tp_reset_mode(TP_MODE_DOUBLEHEIGHT);
start_of_line = true;
break;
case '#': // heading, any level: use double height print
if (escaped) {
mgos_uart_write(uart, "#", 1);
escaped = false;
} else if (start_of_line) tp_set_mode(TP_MODE_DOUBLEHEIGHT);
start_of_line = false;
break;
case ' ': // white space characters
case '\t':
ignoreAfterSpecialChar = (previousChar == '#') || (previousChar == ' ') || (previousChar == '\t') || escaped;
if (!ignoreAfterSpecialChar) {
mgos_uart_write(uart, " ", 1);
escaped = false;
}
break;
case '_': // Underline?
if (escaped) {
mgos_uart_write(uart, "_", 1);
escaped = false;
} else tp_toggle_mode(TP_MODE_UNDERLINED);
if (previousChar == '_') mgos_uart_write(uart, "_", 1); // __ prints a single _
start_of_line = false;
break;
case '*': // Emphasize?
if (escaped) {
mgos_uart_write(uart, "*", 1);
escaped = false;
} else tp_toggle_mode(TP_MODE_EMPHASIZED);
if (previousChar == '*') mgos_uart_write(uart, "*", 1); // ** prints a single *
start_of_line = false;
break;
case '\\': // Escape character -- do not print, except it was escaped
if (escaped) {
mgos_uart_write(uart, &configuredEscapeCharacter, 1);
escaped = false;
} else escaped = true; // do not print yet
break;
default:
mgos_uart_write(uart, currentChar, 1);
start_of_line = false;
break;
}
}
tp_reset_mode(TP_MODE_ALL);
}

View File

@ -8,7 +8,7 @@
#define TP_MODE_ALTERNATEFONT 0x01
#define TP_MODE_EMPFHASIZED 0x08
#define TP_MODE_EMPHASIZED 0x08
#define TP_MODE_DOUBLEHEIGHT 0x10
#define TP_MODE_DOUBLEWIDTH 0x20
#define TP_MODE_UNDERLINED 0x80
@ -21,5 +21,6 @@ extern void tp_set_mode(uint8_t addModes);
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

View File

@ -64,7 +64,11 @@ static void timer_cb(void *arg) {
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, "xyz\n");
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;
@ -89,6 +93,33 @@ static void printSystemInfo() {
#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);
@ -105,11 +136,15 @@ enum mgos_app_init_result mgos_app_init(void) {
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 */);
mgos_uart_set_rx_enabled(UART_NO, false);
// 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");