From 111319e22dbce44a338dffa35a0491a8562cc9c9 Mon Sep 17 00:00:00 2001 From: cuu Date: Sun, 27 Apr 2025 15:32:55 +0800 Subject: [PATCH] add set kbd backlight via i2c example in picocalc_helloworld --- Code/picocalc_helloworld/i2ckbd/i2ckbd.c | 28 ++++++++++++++++++++++++ Code/picocalc_helloworld/i2ckbd/i2ckbd.h | 3 +++ Code/picocalc_helloworld/main.c | 17 ++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/Code/picocalc_helloworld/i2ckbd/i2ckbd.c b/Code/picocalc_helloworld/i2ckbd/i2ckbd.c index 78d0367..51bfb32 100644 --- a/Code/picocalc_helloworld/i2ckbd/i2ckbd.c +++ b/Code/picocalc_helloworld/i2ckbd/i2ckbd.c @@ -77,6 +77,34 @@ int read_battery() { return -1; } + if (buff != 0) { + return buff; + } + return -1; +} + +int set_kbd_backlight(uint8_t val){ + int retval; + uint16_t buff = 0; + unsigned char msg[2]; + msg[0] = 0x0A; + msg[1] = val; + bitSet(msg[0],7); + + if (i2c_inited == 0) return -1; + + retval = i2c_write_timeout_us(I2C_KBD_MOD, I2C_KBD_ADDR, msg, 2, false, 500000); + if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) { + printf("read_battery i2c write error\n"); + return -1; + } + sleep_ms(16); + retval = i2c_read_timeout_us(I2C_KBD_MOD, I2C_KBD_ADDR, (unsigned char *) &buff, 2, false, 500000); + if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) { + printf("read_battery i2c read error read\n"); + return -1; + } + if (buff != 0) { return buff; } diff --git a/Code/picocalc_helloworld/i2ckbd/i2ckbd.h b/Code/picocalc_helloworld/i2ckbd/i2ckbd.h index 0a162b0..b39d7c1 100644 --- a/Code/picocalc_helloworld/i2ckbd/i2ckbd.h +++ b/Code/picocalc_helloworld/i2ckbd/i2ckbd.h @@ -16,7 +16,10 @@ void init_i2c_kbd(); int read_i2c_kbd(); int read_battery(); +int set_kbd_backlight(uint8_t); #define bitRead(value, bit) (((value) >> (bit)) & 0x01) #define bitClear(value, bit) ((value) &= ~(1 << (bit))) +#define bitSet(value, bit) ((value) |= (1 << (bit))) + #endif \ No newline at end of file diff --git a/Code/picocalc_helloworld/main.c b/Code/picocalc_helloworld/main.c index f2d2a76..5c48d17 100644 --- a/Code/picocalc_helloworld/main.c +++ b/Code/picocalc_helloworld/main.c @@ -220,6 +220,16 @@ void test_battery(){ lcd_print_string(buf); } +void test_kbd_backlight(uint8_t val) { + char buf[64]; + int kbd_backlight = set_kbd_backlight(val); + kbd_backlight = kbd_backlight>>8; + + sprintf(buf, "kbd backlight %d\n", kbd_backlight); + printf("%s", buf); + lcd_print_string(buf); +} + int main() { set_sys_clock_khz(133000, true); stdio_init_all(); @@ -252,6 +262,13 @@ int main() { sleep_ms(2000); test_battery(); + test_kbd_backlight(50); + sleep_ms(1000); + test_kbd_backlight(100); + sleep_ms(1000); + test_kbd_backlight(255); + sleep_ms(1000); + test_kbd_backlight(0); while (1) { int c = lcd_getc(0); if(c != -1 && c > 0) {