From e9396f3cd316b066d68af0f29334863e982dd744 Mon Sep 17 00:00:00 2001 From: "Lukas F. Hartmann" Date: Fri, 26 Aug 2022 16:37:47 +0200 Subject: [PATCH] HID: fix more c typing problems --- .../pocket-hid/src/main.c | 2 +- .../pocket-hid/src/oled.c | 44 +++++++++++-------- .../pocket-hid/src/oled.h | 3 +- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/pocket-reform-keyboard-fw/pocket-hid/src/main.c b/pocket-reform-keyboard-fw/pocket-hid/src/main.c index 9a72d7e..d8c269f 100644 --- a/pocket-reform-keyboard-fw/pocket-hid/src/main.c +++ b/pocket-reform-keyboard-fw/pocket-hid/src/main.c @@ -206,7 +206,7 @@ int main(void) }*/ gfx_init(false); - gfx_poke_str(0, 0, "Hello Pocket!"); + gfx_poke_cstr(0, 0, "Hello Pocket!"); gfx_flush(); while (1) { diff --git a/pocket-reform-keyboard-fw/pocket-hid/src/oled.c b/pocket-reform-keyboard-fw/pocket-hid/src/oled.c index 8d00ba2..d7c033b 100644 --- a/pocket-reform-keyboard-fw/pocket-hid/src/oled.c +++ b/pocket-reform-keyboard-fw/pocket-hid/src/oled.c @@ -12,13 +12,13 @@ #include #include "font.c" -int oledbrt = 0; +uint8_t oledbrt = 0; struct CharacterMatrix display; // Write command sequence. // Returns true on success. static inline bool _send_cmd1(uint8_t cmd) { - char buf[] = {0x00, cmd}; + uint8_t buf[] = {0x00, cmd}; i2c_write_blocking(i2c0, SSD1306_ADDRESS, buf, 2, false); return true; } @@ -52,7 +52,7 @@ static void clear_display(void) { send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1); send_cmd3(ColumnAddr, 0, DisplayWidth - 1); - char buf[1 + MatrixRows * DisplayWidth]; + uint8_t buf[1 + MatrixRows * DisplayWidth]; buf[0] = 0x40; for (int i=0; icursor - &matrix->display[0][0]) % MatrixCols; + int cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols; while (cursor_col++ < MatrixCols) { matrix_write_char_inner(matrix, ' '); @@ -180,18 +180,23 @@ void gfx_poke(uint8_t x, uint8_t y, uint8_t c) { } void gfx_poke_str(uint8_t x, uint8_t y, char* str) { - int len = strlen(str); + int len = (int)strlen(str); + if (len<1) return; if (len>21) len = 21; // clip - if (y<0 || y>3) return; + if (y>3) return; for (int xx=x; xx=0 && xx<21) { + if (xx<21) { display.display[y][xx] = (uint8_t)str[xx-x]; } } } +void gfx_poke_cstr(uint8_t x, uint8_t y, const char* str) { + gfx_poke_str(x, y, strdup(str)); +} + void gfx_write_char(uint8_t c) { matrix_write_char(&display, c); } @@ -240,16 +245,16 @@ void gfx_clear_screen(void) { } void gfx_clear_invert(void) { - for (int y=0;y<4;y++) { - for (int x=0;x<21;x++) { + for (int y=0; y<4; y++) { + for (int x=0; x<21; x++) { display.invert[y][x] = 0; } } } void gfx_invert_row(uint8_t y) { - if (y<0 || y>3) return; - for (int x=0;x<21;x++) { + if (y>3) return; + for (int x=0; x<21; x++) { display.invert[y][x] = 1; } } @@ -261,7 +266,7 @@ void matrix_render(struct CharacterMatrix *matrix) { send_cmd3(PageAddr, 0, MatrixRows - 1); send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1); - char buf[1 + MatrixRows * DisplayWidth]; + uint8_t buf[1 + MatrixRows * DisplayWidth]; buf[0] = 0x40; int i = 1; @@ -310,7 +315,10 @@ void oled_brightness_inc(void) { } void oled_brightness_dec(void) { - oledbrt-=10; - if (oledbrt<0) oledbrt = 0; + if (oledbrt<10) { + oledbrt = 0; + } else { + oledbrt-=10; + } gfx_contrast(oledbrt); } diff --git a/pocket-reform-keyboard-fw/pocket-hid/src/oled.h b/pocket-reform-keyboard-fw/pocket-hid/src/oled.h index a733574..c6ad07a 100644 --- a/pocket-reform-keyboard-fw/pocket-hid/src/oled.h +++ b/pocket-reform-keyboard-fw/pocket-hid/src/oled.h @@ -70,6 +70,7 @@ struct CharacterMatrix { void gfx_poke(uint8_t x, uint8_t y, uint8_t c); void gfx_poke_str(uint8_t x, uint8_t y, char* str); +void gfx_poke_cstr(uint8_t x, uint8_t y, const char* str); void gfx_clear_invert(void); void gfx_invert_row(uint8_t y); bool gfx_init(bool rotate); @@ -82,7 +83,7 @@ void gfx_write_char(uint8_t c); void gfx_write(const char *data); void gfx_write_P(const char *data); void gfx_clear_screen(void); -void gfx_contrast(int c); +void gfx_contrast(uint8_t c); void matrix_clear(struct CharacterMatrix *matrix); void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c);