Multiple spin works

This commit is contained in:
whowechina
2024-10-05 14:05:58 +08:00
parent a7615d2d21
commit 17ecbd8cd0
7 changed files with 99 additions and 36 deletions
+1 -1
View File
@@ -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)
+2 -1
View File
@@ -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
+3 -1
View File
@@ -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);
}
}
+7 -29
View File
@@ -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");
+67
View File
@@ -0,0 +1,67 @@
/*
* The Spinning of Buttons
* WHowe <github.com/whowechina>
*
*/
#include "spin.h"
#include <stdint.h>
#include <stdbool.h>
#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];
}
+18
View File
@@ -0,0 +1,18 @@
/*
* The Spinning of Buttons
* WHowe <github.com/whowechina>
*/
#ifndef SPIN_H
#define SPIN_H
#include <stdint.h>
#include <stdbool.h>
#include "hardware/flash.h"
void spin_init();
uint8_t spin_num();
void spin_update();
uint16_t spin_read(uint8_t index);
#endif
+1 -4
View File
@@ -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;
}