From 17ecbd8cd0c6b0501bc2ce3f28ff51f24cbff277 Mon Sep 17 00:00:00 2001 From: whowechina Date: Sat, 5 Oct 2024 14:05:58 +0800 Subject: [PATCH] Multiple spin works --- firmware/src/CMakeLists.txt | 2 +- firmware/src/board_defs.h | 3 +- firmware/src/commands.c | 4 ++- firmware/src/main.c | 36 ++++---------------- firmware/src/spin.c | 67 +++++++++++++++++++++++++++++++++++++ firmware/src/spin.h | 18 ++++++++++ firmware/src/tmag5273.c | 5 +-- 7 files changed, 99 insertions(+), 36 deletions(-) create mode 100644 firmware/src/spin.c create mode 100644 firmware/src/spin.h diff --git a/firmware/src/CMakeLists.txt b/firmware/src/CMakeLists.txt index 451e283..f7b072c 100644 --- a/firmware/src/CMakeLists.txt +++ b/firmware/src/CMakeLists.txt @@ -1,7 +1,7 @@ function(make_firmware board board_def) pico_sdk_init() add_executable(${board} - main.c light.c button.c save.c config.c commands.c cli.c + main.c light.c button.c spin.c save.c config.c commands.c cli.c tmag5273.c hid.c usb_descriptors.c) target_compile_definitions(${board} PUBLIC ${board_def}) pico_enable_stdio_usb(${board} 1) diff --git a/firmware/src/board_defs.h b/firmware/src/board_defs.h index fcf7fce..2d8a27e 100644 --- a/firmware/src/board_defs.h +++ b/firmware/src/board_defs.h @@ -14,7 +14,8 @@ #define BUS_I2C_SCL 1 #define BUS_I2C_FREQ 400*1000 -#define BUTTON_DEF { 12, 11, 10, 5, 4, 3, 13, 2 } +#define SPIN_DEF { 13, 12, 11, 10, 9 } +#define BUTTON_DEF { 16, 17, 18, 19, 20, 21, 22 } #else diff --git a/firmware/src/commands.c b/firmware/src/commands.c index c1f6ea1..9dc62bd 100644 --- a/firmware/src/commands.c +++ b/firmware/src/commands.c @@ -134,7 +134,7 @@ static void handle_i2c(int argc, char *argv[]) return; } - const char *choices[] = {"set", "read"}; + const char *choices[] = {"set", "read", "change"}; int match = cli_match_prefix(choices, count_of(choices), argv[0]); if (match < 0) { return; @@ -148,6 +148,8 @@ static void handle_i2c(int argc, char *argv[]) uint16_t angle = tmag5273_read_angle(); printf("angle: %d (%04x)\n", angle, angle); + } else if (match == 2) { + tmag5273_change_addr(0x12); } } diff --git a/firmware/src/main.c b/firmware/src/main.c index 5c8aeef..8aca678 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -32,8 +32,7 @@ #include "light.h" #include "button.h" - -#include "tmag5273.h" +#include "spin.h" static void run_lights() { @@ -55,14 +54,10 @@ static void core1_loop() static void read_spin() { - uint16_t angle = tmag5273_read_angle(); - float deg = angle / 16.0f; - printf("%7.2f:", deg); - - angle /= 160; - for (int i = 0; i < 36; i++) { - char c = (i == angle) ? '*' : ' '; - printf("%c", c); + for (int i = 0; i < spin_num(); i++) { + uint16_t angle = spin_read(i); + float deg = angle / 16.0f; + printf(" %d:%7.2f", i, deg); } printf("\n"); } @@ -78,9 +73,9 @@ static void core0_loop() cli_fps_count(0); button_update(); + spin_update(); hid_update(); - read_spin(); sleep_us(900); @@ -127,24 +122,7 @@ void init() light_init(); button_init(); - - i2c_init(BUS_I2C, BUS_I2C_FREQ); - gpio_init(BUS_I2C_SDA); - gpio_init(BUS_I2C_SCL); - gpio_set_function(BUS_I2C_SDA, GPIO_FUNC_I2C); - gpio_set_function(BUS_I2C_SCL, GPIO_FUNC_I2C); - gpio_pull_up(BUS_I2C_SDA); - gpio_pull_up(BUS_I2C_SCL); - - gpio_init(21); - gpio_set_dir(21, GPIO_OUT); - gpio_put(21, 1); - - sleep_ms(1); - - tmag5273_init(1, BUS_I2C); - tmag5273_use(1); - tmag5273_init_sensor(); + spin_init(); cli_init("musec_pico>", "\n << Musec Pico Controller >>\n" " https://github.com/whowechina\n\n"); diff --git a/firmware/src/spin.c b/firmware/src/spin.c new file mode 100644 index 0000000..db8fd46 --- /dev/null +++ b/firmware/src/spin.c @@ -0,0 +1,67 @@ +/* + * The Spinning of Buttons + * WHowe + * + */ + +#include "spin.h" + +#include +#include + +#include "hardware/gpio.h" +#include "hardware/timer.h" +#include "hardware/pwm.h" + +#include "config.h" +#include "board_defs.h" + +#include "tmag5273.h" + +static const uint8_t spin_enablers[] = SPIN_DEF; + +#define SPIN_NUM (count_of(spin_enablers)) + +void spin_init() +{ + gpio_init(BUS_I2C_SDA); + gpio_set_function(BUS_I2C_SDA, GPIO_FUNC_I2C); + gpio_pull_up(BUS_I2C_SDA); + gpio_init(BUS_I2C_SCL); + gpio_set_function(BUS_I2C_SCL, GPIO_FUNC_I2C); + gpio_pull_up(BUS_I2C_SCL); + i2c_init(BUS_I2C, BUS_I2C_FREQ); + + for (int i = 0; i < SPIN_NUM; i++) { + uint8_t ce = spin_enablers[i]; + gpio_init(ce); + gpio_set_dir(ce, GPIO_OUT); + gpio_put(ce, 1); + sleep_us(1000); + tmag5273_init(i, BUS_I2C); + tmag5273_init_sensor(); + } +} + +uint8_t spin_num() +{ + return SPIN_NUM; +} + +static uint16_t spin_reading[SPIN_NUM]; + +void spin_update() +{ + for (int i = 0; i < SPIN_NUM; i++) { + tmag5273_use(i); + spin_reading[i] = tmag5273_read_angle(); + } +} + +uint16_t spin_read(uint8_t index) +{ + if (index >= SPIN_NUM) { + return 0; + } + return spin_reading[index]; +} diff --git a/firmware/src/spin.h b/firmware/src/spin.h new file mode 100644 index 0000000..1b0f296 --- /dev/null +++ b/firmware/src/spin.h @@ -0,0 +1,18 @@ +/* + * The Spinning of Buttons + * WHowe + */ + +#ifndef SPIN_H +#define SPIN_H + +#include +#include +#include "hardware/flash.h" + +void spin_init(); +uint8_t spin_num(); +void spin_update(); +uint16_t spin_read(uint8_t index); + +#endif diff --git a/firmware/src/tmag5273.c b/firmware/src/tmag5273.c index 0b39b61..d473236 100644 --- a/firmware/src/tmag5273.c +++ b/firmware/src/tmag5273.c @@ -61,9 +61,7 @@ void tmag5273_init(unsigned instance, i2c_inst_t *i2c_port) current_instance = instance; INSTANCE.port = i2c_port; INSTANCE.addr = TMAG5273_DEF_ADDR; - if (instance > 0) { - tmag5273_change_addr(TMAG5273_DEF_ADDR + instance); - } + tmag5273_change_addr(TMAG5273_DEF_ADDR + 1 + instance); } } @@ -112,6 +110,5 @@ uint16_t tmag5273_read_angle() if (buf[0] & 0x01) { INSTANCE.cache = (buf[1] << 8 | buf[2]) & 0x1fff; } - printf("%4d(%d) ", INSTANCE.addr, current_instance); return INSTANCE.cache; }