1
0
mirror of https://github.com/whowechina/iidx_pico.git synced 2024-11-12 01:10:50 +01:00

Analog and I2C mode change at runtime

This commit is contained in:
whowechina 2023-04-16 16:20:09 +08:00
parent 9902bb8e26
commit 1cf7f4092b
5 changed files with 77 additions and 25 deletions

View File

@ -18,11 +18,10 @@
#define TT_RGB_PIN 28
#define TT_RGB_ORDER GRB
/* If you define both Analog and I2C, it will use analog */
#define TT_AS5600_ANALOG 27
//#define TT_AS5600_SCL 27
//#define TT_AS5600_SDA 26
//#define TT_AS5600_I2C i2c1
#define TT_AS5600_ANALOG 26
#define TT_AS5600_SCL 27
#define TT_AS5600_SDA 26
#define TT_AS5600_I2C i2c1
#else

View File

@ -23,6 +23,7 @@ static iidx_cfg_t default_cfg = {
.brightness = 5,
.reversed = false,
},
.tt_sensor_analog = false,
.tt_sensor_reversed = false,
.effects = {
.play_vol = 255,
@ -34,7 +35,12 @@ static iidx_cfg_t default_cfg = {
static void config_loaded()
{
/* configuration validation */
if ((iidx_cfg->tt_led.start > 8) ||
(iidx_cfg->tt_led.start + iidx_cfg->tt_led.num > 128)) {
iidx_cfg->tt_led.start = 0;
iidx_cfg->tt_led.num = 24;
config_changed();
}
}
void config_changed()

View File

@ -26,6 +26,7 @@ typedef struct __attribute ((packed)) {
uint8_t brightness;
bool reversed;
} tt_led;
bool tt_sensor_analog;
bool tt_sensor_reversed;
struct {
uint8_t play_vol;

View File

@ -77,6 +77,7 @@ static struct {
#define PRESSED_ALL(k) ((input.keys & (k)) == (k))
#define PRESSED_ANY(k) (input.keys & (k))
#define JUST_PRESSED(k) (input.just_pressed & (k))
#define JUST_RELEASED(k) (input.just_released & (k))
typedef void (*mode_func)();
@ -135,6 +136,8 @@ static struct {
bool adjust_led_start;
int16_t start_angle;
uint8_t counter;
bool e4_press_valid;
uint64_t e4_timeout;
} tt_ctx;
void mode_tt_enter()
@ -150,9 +153,14 @@ static void mode_tt_key_change()
} else if (JUST_PRESSED(E_EFFECT)) {
tt_ctx.adjust_led_start = false;
tt_ctx.start_angle = input.angle;
} else if (JUST_PRESSED(E_VEFX)) {
iidx_cfg->tt_led.reversed = !iidx_cfg->tt_led.reversed;
} else if (JUST_PRESSED(E_4)) {
tt_ctx.e4_press_valid = true;
tt_ctx.e4_timeout = setup_tick_ms + 3000;
}
if (JUST_RELEASED(E_VEFX)) {
iidx_cfg->tt_led.reversed = !iidx_cfg->tt_led.reversed;
} else if (JUST_RELEASED(E_4) && tt_ctx.e4_press_valid) {
iidx_cfg->tt_sensor_reversed = !iidx_cfg->tt_sensor_reversed;
}
@ -213,11 +221,20 @@ void mode_tt_loop()
setup_led_button[LED_E_EFFECT] = tt_rgb32(0, 128, 0, false) & mask;
}
uint32_t cyan = button_rgb32(0, 90, 90, false);
uint32_t yellow = button_rgb32(90, 90, 0, false);
uint32_t red = button_rgb32(99, 0, 0, false);
uint32_t green = button_rgb32(0, 99, 0, false);
uint32_t cyan = button_rgb32(0, 99, 99, false);
uint32_t yellow = button_rgb32(99, 99, 0, false);
setup_led_button[LED_E_VEFX] = iidx_cfg->tt_led.reversed ? cyan : yellow;
setup_led_button[LED_E_4] = iidx_cfg->tt_sensor_reversed ? cyan : yellow;
setup_led_button[LED_E_4] = iidx_cfg->tt_sensor_analog ?
(iidx_cfg->tt_sensor_reversed ? red : green) :
(iidx_cfg->tt_sensor_reversed ? yellow : cyan);
if (PRESSED_ANY(E_4) && tt_ctx.e4_press_valid && (setup_tick_ms > tt_ctx.e4_timeout)) {
iidx_cfg->tt_sensor_analog = !iidx_cfg->tt_sensor_analog;
tt_ctx.e4_press_valid = false;
}
}
static struct {
@ -275,6 +292,7 @@ bool setup_run(uint16_t keys, uint16_t angle)
if (input.just_pressed) {
printf("+ %04x\n", input.just_pressed);
printf("S:%u L:%u\n", iidx_cfg->tt_led.start, iidx_cfg->tt_led.num);
}
if (input.just_released) {
printf("- %04x\n", input.just_released);

View File

@ -20,24 +20,42 @@
static uint16_t angle = 0;
void turntable_init()
static bool running_analog = false;
static void init_i2c()
{
#ifdef TT_AS5600_ANALOG
adc_init();
adc_gpio_init(TT_AS5600_ANALOG);
adc_select_input(TT_AS5600_ANALOG - 26);
#else
i2c_init(TT_AS5600_I2C, 333 * 1000);
gpio_set_function(TT_AS5600_SCL, GPIO_FUNC_I2C);
gpio_set_function(TT_AS5600_SDA, GPIO_FUNC_I2C);
gpio_pull_up(TT_AS5600_SCL);
gpio_pull_up(TT_AS5600_SDA);
#endif
}
#ifdef TT_AS5600_ANALOG
static void init_analog()
{
adc_init();
adc_gpio_init(TT_AS5600_ANALOG);
adc_select_input(TT_AS5600_ANALOG - 26);
}
uint32_t max_adc = 3500;
static void follow_mode_change()
{
if (running_analog != iidx_cfg->tt_sensor_analog) {
turntable_init();
}
}
void turntable_init()
{
running_analog = iidx_cfg->tt_sensor_analog;
if (running_analog) {
init_analog();
} else {
init_i2c();
}
}
uint32_t max_adc = 3650;
static inline void adjust_max(uint32_t value)
{
if (value > max_adc) {
@ -78,11 +96,9 @@ static uint16_t read_average(uint16_t size)
return (all / size) % max_adc;
}
#endif
void turntable_update()
static void update_analog()
{
#ifdef TT_AS5600_ANALOG
const uint16_t deadzone = 24;
static uint16_t sample = 0;
int new_value = read_average(200);
@ -92,7 +108,10 @@ void turntable_update()
}
sample = new_value;
angle = (sample * 4095) / max_adc;
#else
}
static void update_i2c()
{
const uint8_t as5600_addr = 0x36;
uint8_t buf[2] = {0x0c, 0x00};
i2c_write_blocking_until(TT_AS5600_I2C, as5600_addr, buf, 1, true,
@ -101,7 +120,16 @@ void turntable_update()
make_timeout_time_ms(1));
angle = ((uint16_t)buf[0] & 0x0f) << 8 | buf[1];
#endif
}
void turntable_update()
{
follow_mode_change();
if (running_analog) {
update_analog();
} else {
update_i2c();
}
}
uint16_t turntable_read()