From ff801b378372ae30a8e402c4404ca8aff2b70018 Mon Sep 17 00:00:00 2001 From: Archie Hilton Date: Tue, 2 Jul 2024 21:26:26 +0100 Subject: [PATCH] Add rotary encoder control --- .ccls | 2 -- .clangd | 5 +--- src/CMakeLists.txt | 3 ++- src/main.cc | 58 ++++++++++++++++++++++++++++++++++------------ 4 files changed, 46 insertions(+), 22 deletions(-) delete mode 100644 .ccls diff --git a/.ccls b/.ccls deleted file mode 100644 index 117fa70..0000000 --- a/.ccls +++ /dev/null @@ -1,2 +0,0 @@ -%compile_commands.json ---sysroot=/usr/arm-none-eabi/ diff --git a/.clangd b/.clangd index bf0f570..77d1a1d 100755 --- a/.clangd +++ b/.clangd @@ -1,5 +1,2 @@ CompileFlags: - #Add: [--query-driver=/usr/bin/arm-none-eabi-g++] - Compiler: /usr/bin/arm-none-eabi-g++ - - + Compiler: /usr/toolchains/arm-none-eabi/13/bin/arm-none-eabi-g++ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d78d2dc..c77a991 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,7 +2,8 @@ add_executable(main main.cc ) -target_link_libraries(main pico_stdlib hardware_i2c hardware_uart) +target_link_libraries(main pico_stdlib hardware_i2c hardware_uart hardware_irq + hardware_gpio) pico_enable_stdio_uart(main 0) pico_enable_stdio_usb(main 1) diff --git a/src/main.cc b/src/main.cc index 8bb977f..8ecad84 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,11 +2,14 @@ #include "hardware/gpio.h" #include "hardware/i2c.h" +#include "pico/time.h" +#include "pico/types.h" + #include #include -#include -#include +#include +#include #define ADDR 0x3c @@ -45,6 +48,30 @@ static uint32_t charset[127] = { 0x7c90904c, 0x90b0d090, 0x81816618, 0x00ff0000, 0x81816618, 0x10081008, INVALID_CH, }; +static uint8_t encoder_pos = 0; + +void irq_callback(uint gpio, uint32_t events) { + static absolute_time_t time_last = get_absolute_time(); + static uint8_t prev = 0; + if (gpio == 19) { + absolute_time_t time_now = get_absolute_time(); + if (time_now - time_last < 1000) return; + + time_last = time_now; + uint8_t twenty = gpio_get(20); + if (twenty != prev) { + if (twenty == 0) { + if (events & GPIO_IRQ_EDGE_RISE) encoder_pos++; + else encoder_pos--; + } else { + if (events & GPIO_IRQ_EDGE_RISE) encoder_pos--; + else encoder_pos++; + } + prev = twenty; + } + } +} + int main(void) { stdio_init_all(); @@ -53,6 +80,12 @@ int main(void) { gpio_init(25); gpio_set_dir(25, GPIO_OUT); + gpio_init(19); + gpio_init(20); + gpio_set_dir(19, GPIO_IN); + gpio_set_dir(20, GPIO_IN); + + gpio_set_irq_enabled_with_callback(19, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, &irq_callback); i2c_init(i2c0, 400000); gpio_init(0); @@ -65,15 +98,12 @@ int main(void) { busy_wait_ms(100); disp.fill(0); - disp.puts(0, 0, "Hello, World!"); - disp.flush(); while (true) { - /* - gpio_put(25, false); - busy_wait_ms(failure ? 50 : 500); - gpio_put(25, true); - busy_wait_ms(failure ? 50 : 500); - */ + disp.fill(0); + disp.put_char(4, 0, (encoder_pos & 0xF) >= 0xA ? (encoder_pos&0xf) - 0xA + 'A':(encoder_pos&0xf)+'0'); + disp.put_char(0, 0, ((encoder_pos>>4) & 0xF) >= 0xA? ((encoder_pos>>4) & 0xf) - 0xA + 'A':((encoder_pos>>4)&0xf)+'0'); + disp.flush(); + busy_wait_ms(50); } return 0; @@ -84,9 +114,9 @@ static void write_cmd(uint8_t cmd) { if (i2c_write_timeout_us(i2c0, ADDR, buf, 2, false, 1000000) < 0) { failure = true; - printf("TX fail\n"); + std::printf("TX fail\n"); } else { - printf("TX succ\n"); + std::printf("TX succ\n"); } } @@ -168,9 +198,7 @@ void ssd1306::flush() { } void ssd1306::put_char(uint8_t row, uint8_t col, char c) { - if (row % 4 != 0) return; - - memcpy(&m_display_buffer[1 + (row + (128*col))], &charset[c-1], 4); + memcpy(m_display_buffer+(1+(row + (128*col))), &charset[c-1], 4); } void ssd1306::puts(uint8_t row, uint8_t col, const char *str) {