136 lines
3.9 KiB
C
136 lines
3.9 KiB
C
#include "QR204.h"
|
|
|
|
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;
|
|
}
|
|
}
|
|
// ***** End of INIT *****
|
|
|
|
// ***** MODE Handling *****
|
|
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);
|
|
}
|
|
|
|
void tp_linefeed(uint8_t numLines) {
|
|
char cmd[3] = { 0x1b, 'd', numLines };
|
|
mgos_uart_write(uart, cmd, 3);
|
|
}
|
|
|
|
void tp_cutpaper(bool fullcut) {
|
|
char cmd[3] = { 0x1d, 'V', fullcut ? '0' : '1' };
|
|
mgos_uart_write(uart, cmd, 3);
|
|
}
|
|
|
|
#define ON true
|
|
#define OFF false
|
|
|
|
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);
|
|
}
|