diff --git a/src/main.c b/src/main.c index b29cd30..1053a19 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,7 @@ static char *LOREM_IPSUM = "nec enim.\nNulla efficitur turpis id tempus ultricies.\nPellentesque semper lacinia mi vitae feugiat.\nVestibulum leo nisi, " "pretium quis felis non, mattis varius risus.\n" "Suspendisse porttitor purus lacinia ante euismod, ut eleifend massa fermentum.\nProin pretium elementum massa, at efficitur " - "risus iaculis sed."; + "risus iaculis sed.\n"; static volatile int key_poll_timeout; static int callback_keypoll(void) { @@ -42,6 +42,8 @@ int main(void) { int off_state = 0; uint16_t backlight_save; + term_append(LOREM_IPSUM); + term_append("-----------------------------------------------------------------"); set_statusbar(tick_ctr, shift_state, alpha_state, get_battery_voltage()); set_menubar(); tgrid_display(); @@ -73,13 +75,19 @@ int main(void) { if (kev.key == KEY_ACON && shift_state) { off_state = 1; - // turn backlight off + // save, then turn backlight level off: + // force to display something + dclear(C_WHITE); + dupdate(); + // store the backlight pwm backlight_save = r61524_get(0x5a1) & 0x00ff; - r61524_set(0x5a1, 0); - - // clear screen + // force vram clear dclear(C_BLACK); dupdate(); + dclear(C_BLACK); + dupdate(); + // force backlight pwm to 0 + r61524_set(0x5a1, 0); shift_state = 0; alpha_state = 0; @@ -92,11 +100,9 @@ int main(void) { gint_osmenu(); } - term_scroll_down(); - char buf[128]; sprintf(buf, "tick_ctr=%d key=0x%x mod=%d shift=%d alpha=%d\n", tick_ctr, kev.key, kev.mod, kev.shift, kev.alpha); - tgrid_sets(UNS_TERM_ROWS - 2, 0, C_WHITE, C_BLACK, buf); + term_append(buf); set_statusbar(tick_ctr, shift_state, alpha_state, get_battery_voltage()); set_menubar(); diff --git a/src/term.c b/src/term.c index 1987407..6d82af1 100644 --- a/src/term.c +++ b/src/term.c @@ -23,21 +23,16 @@ static void tgrid_set(int row, int col, int fg, int bg, char c[4]) { tgrid[row][col].bg = bg; } -void tgrid_sets(int row, int col, int fg, int bg, const char *s) { - int _row = row; - int _col = col; - - for (int i = 0; s[i] != '\0';) { - if (_col >= UNS_TERM_COLS) { - _col = 0; - _row++; - } - +static int tgrid_sets(int *row, int *col, int fg, int bg, const char *s) { + int i = 0; + while (s[i] != '\0') { // boundary check - if (_row >= UNS_TERM_ROWS) - return; + if (*row >= UNS_TERM_ROWS) + return i; + if (*col >= UNS_TERM_COLS) + return i; - // detech utf-8 char length + // detect utf-8 char length unsigned char lb = s[i]; int charlen = 0; if ((lb & 0x80) == 0) // lead bit is zero, must be a single ascii @@ -49,28 +44,31 @@ void tgrid_sets(int row, int col, int fg, int bg, const char *s) { else if ((lb & 0xF8) == 0xF0) // 1111 0xxx charlen = 4; - char unichar[4] = ""; - for (int j = 0; j < charlen; j++) + char unichar[4]; + int j; + for (j = 0; j < charlen; j++) unichar[j] = s[i + j]; - - const int char_is_newline = (strncmp(unichar, "\n", 4) == 0); - - if (char_is_newline) { - for (; _col < UNS_TERM_COLS; _col++) - tgrid_set(_row, _col, fg, bg, " \0\0\0"); - - _col = 0; - _row++; - } else { - tgrid_set(_row, _col, fg, bg, unichar); - - _col++; - } + for (; j < 4; j++) + unichar[j] = '\0'; i += charlen; + + const int char_is_newline = (strncmp(unichar, "\n", 4) == 0); + if (char_is_newline) { + // clear line end + for (; *col < UNS_TERM_COLS; *col = *col + 1) + tgrid_set(*row, *col, C_WHITE, C_BLACK, " \0\0"); + } else { + tgrid_set(*row, *col, fg, bg, unichar); + *col = *col + 1; + } } + + return i; } +int term_writeat(int row, int col, int fg, int bg, const char *s) { return tgrid_sets(&row, &col, fg, bg, s); } + void tgrid_display(void) { dclear(C_BLACK); dfont(&uf5x7); @@ -102,7 +100,58 @@ void tgrid_display(void) { } void term_scroll_down(void) { - for (int i = 0; i < UNS_TERM_ROWS - 1; i++) { + // clear last line + for (int i = 0; i < UNS_TERM_COLS; i++) { + tgrid[UNS_TERM_ROWS - 1][i].chr[0] = ' '; + tgrid[UNS_TERM_ROWS - 1][i].chr[1] = '\0'; + tgrid[UNS_TERM_ROWS - 1][i].chr[2] = '\0'; + tgrid[UNS_TERM_ROWS - 1][i].chr[3] = '\0'; + tgrid[UNS_TERM_ROWS - 1][i].fg = C_WHITE; + tgrid[UNS_TERM_ROWS - 1][i].bg = C_BLACK; + } + + for (int i = 1; i < UNS_TERM_ROWS - 1; i++) { memcpy(tgrid[i], tgrid[i + 1], UNS_TERM_COLS * sizeof(struct tcell)); } +} + +void term_append(const char *str) { + static int row = 1; + static int col = 0; + + int i = 0; + + while (str[i] != '\0') { + // handle cursor overflow (column) + if (col >= UNS_TERM_COLS) { + col = 0; + row++; + } + + // handle cursor overflow (row) + if (row >= UNS_TERM_ROWS - 1) { + term_scroll_down(); + row = UNS_TERM_ROWS - 2; + } + + i += tgrid_sets(&row, &col, C_WHITE, C_BLACK, str + i); + } +} + +int term_consume_stdio(void) { + int i = 0; + + char chunk[128 + 1]; + while (1) { + int n = read((int)stdout, (void *)chunk, sizeof(chunk) - 1); + + if (n < 0) + break; + + chunk[n] = '\0'; + term_append(chunk); + i += n; + } + + return i; } \ No newline at end of file diff --git a/src/term.h b/src/term.h index 1bfa588..25aa32b 100644 --- a/src/term.h +++ b/src/term.h @@ -4,9 +4,12 @@ #define UNS_TERM_ROWS 22 #define UNS_TERM_COLS 65 -void tgrid_sets(int row, int col, int fg, int bg, const char *s); +// dclear(C_BLACK), display text grid, then dupdate() void tgrid_display(void); +int term_writeat(int row, int col, int fg, int bg, const char *s); void term_scroll_down(void); +void term_append(const char *str); +int term_consume_stdio(void); #endif // #ifndef UNS_TERM_H \ No newline at end of file diff --git a/src/ui.c b/src/ui.c index b9063a4..a19c3f2 100644 --- a/src/ui.c +++ b/src/ui.c @@ -43,11 +43,11 @@ void set_menubar(void) { center_str(10, buf, fnkeys[i]); buf[10] = '\0'; - tgrid_sets(UNS_TERM_ROWS - 1, col, C_WHITE, C_BLUE, buf); + term_writeat(UNS_TERM_ROWS - 1, col, C_WHITE, C_BLUE, buf); // print spacers if (i < 6 - 1) - tgrid_sets(UNS_TERM_ROWS - 1, col + 10, C_WHITE, C_BLACK, " "); + term_writeat(UNS_TERM_ROWS - 1, col + 10, C_WHITE, C_BLACK, " "); col += 10 + 1; }