From 17cf8413d8eb376bbe1d909a94f779fcd37f22bb Mon Sep 17 00:00:00 2001 From: "Lukas F. Hartmann" Date: Mon, 20 Mar 2023 13:31:35 +0100 Subject: [PATCH] pocket-hid: fix compiler warnings; lower oled brightness --- pocket-reform-keyboard-fw/pocket-hid/flash.sh | 2 +- .../pocket-hid/src/main.c | 77 +++++++++++-------- .../pocket-hid/src/oled.c | 13 +++- .../pocket-hid/src/oled.h | 3 +- .../pocket-hid/src/ws2812.pio.h | 4 +- 5 files changed, 62 insertions(+), 37 deletions(-) diff --git a/pocket-reform-keyboard-fw/pocket-hid/flash.sh b/pocket-reform-keyboard-fw/pocket-hid/flash.sh index 9efed3e..b8188ff 100755 --- a/pocket-reform-keyboard-fw/pocket-hid/flash.sh +++ b/pocket-reform-keyboard-fw/pocket-hid/flash.sh @@ -1,4 +1,4 @@ #!/bin/bash -picotool load build/pocket-hid.uf2 +sudo picotool load build/pocket-hid.uf2 diff --git a/pocket-reform-keyboard-fw/pocket-hid/src/main.c b/pocket-reform-keyboard-fw/pocket-hid/src/main.c index 527cecd..fc8a152 100644 --- a/pocket-reform-keyboard-fw/pocket-hid/src/main.c +++ b/pocket-reform-keyboard-fw/pocket-hid/src/main.c @@ -131,6 +131,7 @@ void on_uart_rx() { } if (c == '\n') { // TODO hack + if (uart_rx_i>6) { gfx_clear(); //gfx_poke_str(0, 3, uart_rx_line); @@ -250,7 +251,7 @@ int main(void) i2c_write_blocking(i2c0, ADDR_SENSOR, buf, 2, false); PIO pio = pio0; - int sm = 0; + uint sm = 0; uint offset = pio_add_program(pio, &ws2812_program); ws2812_program_init(pio, sm, offset, PIN_LEDS, 800000, false); @@ -642,7 +643,7 @@ void led_task(uint32_t color) { uint8_t led_rgb_buf[12*3*6]; -void led_bitmap(uint8_t row, uint8_t* row_rgb) { +void led_bitmap(uint8_t row, const uint8_t* row_rgb) { // row = 5 -> commit if (row > 5) return; @@ -725,11 +726,11 @@ RgbColor HsvToRgb(HsvColor hsv) } region = hsv.h / 43; - remainder = (hsv.h - (region * 43)) * 6; + remainder = (unsigned char)((hsv.h - (region * 43)) * 6); - p = (hsv.v * (255 - hsv.s)) >> 8; - q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8; - t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8; + p = (unsigned char)((hsv.v * (255 - hsv.s)) >> 8); + q = (unsigned char)((hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8); + t = (unsigned char)((hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8); switch (region) { @@ -763,9 +764,9 @@ void led_set(uint32_t rgb) { void led_set_hsv() { HsvColor hsv; RgbColor rgb; - hsv.h = led_hue; + hsv.h = (unsigned char)led_hue; hsv.s = 0xff; - hsv.v = led_brightness; + hsv.v = (unsigned char)led_brightness; rgb = HsvToRgb(hsv); led_set((rgb.r<<16)|(rgb.g<<8)|(rgb.b)); @@ -800,7 +801,7 @@ void led_cycle_hue() { // Invoked when sent REPORT successfully to host // Application can use this to send the next report // Note: For composite reports, report[0] is report ID -void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len) +void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len) { (void) instance; (void) len; @@ -828,16 +829,16 @@ uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_t return 0; } -// hid commands are all 4-letter, so they fit in a 32 bit integer -#define cmd(_s) (*(uint32_t *)(_s)) -#define CMD_TEXT_FRAME cmd("OLED") // fill the screen with a single wall of text -#define CMD_OLED_CLEAR cmd("WCLR") // clear the oled display -#define CMD_OLED_BITMAP cmd("WBIT") // (u16 offset, u8 bytes...) write raw bytes into the oled framebuffer -#define CMD_POWER_OFF cmd("PWR0") // turn off power rails -#define CMD_BACKLIGHT cmd("LITE") // keyboard backlight level -#define CMD_RGB_BACKLIGHT cmd("LRGB") // keyboard backlight rgb -#define CMD_RGB_BITMAP cmd("XRGB") // push rgb backlight bitmap -#define CMD_LOGO cmd("LOGO") // play logo animation +#define CMD_TEXT_FRAME "OLED" // fill the screen with a single wall of text +#define CMD_OLED_CLEAR "WCLR" // clear the oled display +#define CMD_OLED_BITMAP "WBIT" // (u16 offset, u8 bytes...) write raw bytes into the oled framebuffer +#define CMD_POWER_OFF "PWR0" // turn off power rails +#define CMD_BACKLIGHT "LITE" // keyboard backlight level +#define CMD_RGB_BACKLIGHT "LRGB" // keyboard backlight rgb +#define CMD_RGB_BITMAP "XRGB" // push rgb backlight bitmap +#define CMD_LOGO "LOGO" // play logo animation +#define CMD_OLED_BRITE "OBRT" // OLED brightness level +#define CMD_OLED_BRITE2 "OBR2" // OLED brightness level // Invoked when received SET_REPORT control request or // received data on OUT endpoint ( Report ID = 0, Type = 0 ) @@ -846,7 +847,7 @@ void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_ (void) instance; (void) buffer; - char repinfo[64]; + //char repinfo[64]; //sprintf(repinfo, "Rep: %d/%d ", report_type, report_id); //gfx_poke_str(1, 1, repinfo); @@ -868,9 +869,9 @@ void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_ if (report_type == 2) { // Big Reform style if (report_id == 'x') { - const uint32_t command = (buffer[3]<<24u)|(buffer[2]<<16u)|(buffer[1]<<8u)|buffer[0]; + const char* cmd = (const char*)buffer; - if (command == CMD_TEXT_FRAME) { + if (cmd == strnstr(cmd, CMD_TEXT_FRAME, 4)) { gfx_clear(); gfx_on(); @@ -889,31 +890,47 @@ void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_ } gfx_flush(); } - else if (command == CMD_POWER_OFF) { + else if (cmd == strnstr(cmd, CMD_POWER_OFF, 4)) { //reset_menu(); //anim_goodbye(); remote_turn_off_som(); //keyboard_power_off(); //reset_keyboard_state(); } - else if (command == CMD_OLED_CLEAR) { + else if (cmd == strnstr(cmd, CMD_OLED_CLEAR, 4)) { gfx_clear(); gfx_flush(); } - else if (command == CMD_OLED_BITMAP) { - matrix_render_direct((uint8_t*)buffer+4); + else if (cmd == strnstr(cmd, CMD_OLED_BITMAP, 4)) { + matrix_render_direct(&buffer[4]); } - else if (command == CMD_RGB_BITMAP) { + else if (cmd == strnstr(cmd, CMD_RGB_BITMAP, 4)) { // row, data (12 * 3 rgb bytes) - led_bitmap(buffer[4], (uint8_t*)buffer+5); + led_bitmap(buffer[4], &buffer[5]); } - else if (command == CMD_RGB_BACKLIGHT) { + else if (cmd == strnstr(cmd, CMD_RGB_BACKLIGHT, 4)) { uint32_t pixel_grb = (buffer[5]<<16u) | (buffer[6]<<8u) | buffer[4]; led_task(pixel_grb); } - else if (command == CMD_LOGO) { + else if (cmd == strnstr(cmd, CMD_LOGO, 4)) { anim_hello(); } + else if (cmd == strnstr(cmd, CMD_OLED_BRITE, 4)) { + uint8_t val = (uint8_t)atoi((const char*)&buffer[4]); + gfx_poke(0,0,'0'+(val/100)); + gfx_poke(1,0,'0'+((val%100)/10)); + gfx_poke(2,0,'0'+(val%10)); + gfx_flush(); + gfx_contrast(val); + } + else if (cmd == strnstr(cmd, CMD_OLED_BRITE2, 4)) { + uint8_t val = (uint8_t)atoi((const char*)&buffer[4]); + gfx_poke(0,0,'0'+(val/100)); + gfx_poke(1,0,'0'+((val%100)/10)); + gfx_poke(2,0,'0'+(val%10)); + gfx_flush(); + gfx_precharge(val); + } } } diff --git a/pocket-reform-keyboard-fw/pocket-hid/src/oled.c b/pocket-reform-keyboard-fw/pocket-hid/src/oled.c index c59ed2c..e630112 100644 --- a/pocket-reform-keyboard-fw/pocket-hid/src/oled.c +++ b/pocket-reform-keyboard-fw/pocket-hid/src/oled.c @@ -88,7 +88,7 @@ bool gfx_init(bool rotate) { send_cmd2(SetComPins, 0x2); send_cmd2(SetContrast, 0x8f); - send_cmd2(SetPreCharge, 0xf1); + send_cmd2(SetPreCharge, 0x00); send_cmd2(SetVComDetect, 0x40); send_cmd1(DisplayAllOnResume); send_cmd1(NormalDisplay); @@ -144,6 +144,12 @@ done: return; } +void gfx_precharge(uint8_t c) { + send_cmd2(SetPreCharge, c); +done: + return; +} + void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) { *matrix->cursor = c; ++matrix->cursor; @@ -289,14 +295,15 @@ done: } // bitmap[0] needs to be 0x40! -void matrix_render_direct(uint8_t* bitmap) { +void matrix_render_direct(const uint8_t* bitmap) { gfx_on(); // Move to the home position send_cmd3(PageAddr, 0, MatrixRows - 1); send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1); - bitmap[0] = 0x40; + // FIXME + //bitmap[0] = 0x40; i2c_write_blocking_until(i2c0, SSD1306_ADDRESS, bitmap, 1 + MatrixRows * DisplayWidth, false, make_timeout_time_ms(OLED_I2C_TIMEOUT)); done: diff --git a/pocket-reform-keyboard-fw/pocket-hid/src/oled.h b/pocket-reform-keyboard-fw/pocket-hid/src/oled.h index c6ad07a..5170755 100644 --- a/pocket-reform-keyboard-fw/pocket-hid/src/oled.h +++ b/pocket-reform-keyboard-fw/pocket-hid/src/oled.h @@ -84,6 +84,7 @@ void gfx_write(const char *data); void gfx_write_P(const char *data); void gfx_clear_screen(void); void gfx_contrast(uint8_t c); +void gfx_precharge(uint8_t c); void matrix_clear(struct CharacterMatrix *matrix); void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c); @@ -92,7 +93,7 @@ void matrix_write(struct CharacterMatrix *matrix, const char *data); void matrix_write_ln(struct CharacterMatrix *matrix, const char *data); void matrix_write_P(struct CharacterMatrix *matrix, const char *data); void matrix_render(struct CharacterMatrix *matrix); -void matrix_render_direct(uint8_t* bitmap); +void matrix_render_direct(const uint8_t* bitmap); void oled_brightness_inc(void); void oled_brightness_dec(void); diff --git a/pocket-reform-keyboard-fw/pocket-hid/src/ws2812.pio.h b/pocket-reform-keyboard-fw/pocket-hid/src/ws2812.pio.h index 8cbeea6..36361c7 100644 --- a/pocket-reform-keyboard-fw/pocket-hid/src/ws2812.pio.h +++ b/pocket-reform-keyboard-fw/pocket-hid/src/ws2812.pio.h @@ -51,7 +51,7 @@ static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24); sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3; - float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit); + float div = (float)clock_get_hz(clk_sys) / (freq * (float)cycles_per_bit); sm_config_set_clkdiv(&c, div); pio_sm_init(pio, sm, offset, &c); pio_sm_set_enabled(pio, sm, true); @@ -104,7 +104,7 @@ static inline void ws2812_parallel_program_init(PIO pio, uint sm, uint offset, u sm_config_set_set_pins(&c, pin_base, pin_count); sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); int cycles_per_bit = ws2812_parallel_T1 + ws2812_parallel_T2 + ws2812_parallel_T3; - float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit); + float div = (float)clock_get_hz(clk_sys) / (freq * (float)cycles_per_bit); sm_config_set_clkdiv(&c, div); pio_sm_init(pio, sm, offset, &c); pio_sm_set_enabled(pio, sm, true);