Add rotary encoder control

This commit is contained in:
Archie Hilton 2024-07-02 21:26:26 +01:00
parent 41a9699f48
commit ff801b3783
4 changed files with 46 additions and 22 deletions

2
.ccls
View File

@ -1,2 +0,0 @@
%compile_commands.json
--sysroot=/usr/arm-none-eabi/

View File

@ -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++

View File

@ -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)

View File

@ -2,11 +2,14 @@
#include "hardware/gpio.h"
#include "hardware/i2c.h"
#include "pico/time.h"
#include "pico/types.h"
#include <pico/stdlib.h>
#include <pico/stdio.h>
#include <stdio.h>
#include <string.h>
#include <cstdio>
#include <cstring>
#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) {