diff --git a/src/buttons.c b/src/buttons.c index 712789a..d8de504 100644 --- a/src/buttons.c +++ b/src/buttons.c @@ -24,12 +24,12 @@ static void button_timer_callback(struct btstack_timer_source* ts) { if (gpio_get(BUTTON_PIN_POWER)) { if (bt_connected) { printf("=> Ignoring input, bluetooth connected.\n"); - } else if (current_power == 0) { - printf("= Resuming: %i%% -> %i%%\n", current_power, resume_power); + } else if (target_power == 0) { + printf("= Resuming: %i%% -> %i%%\n", target_power, resume_power); state_set_power(resume_power); } else { - printf("= Suspending: %i%% -> %i%%\n", current_power, resume_power); - resume_power = current_power; + printf("= Suspending: %i%% -> %i%%\n", target_power, resume_power); + resume_power = target_power; state_set_power(0); } } @@ -37,22 +37,22 @@ static void button_timer_callback(struct btstack_timer_source* ts) { if (gpio_get(BUTTON_PIN_PLUS)) { if (bt_connected) { printf("+> Ignoring input, bluetooth connected.\n"); - } else if (current_power <= (100 - BUTTON_STEP)) { - printf("+ Increasing power: %i%% -> %i%%\n", current_power, current_power + BUTTON_STEP); - state_set_power(current_power + BUTTON_STEP); + } else if (target_power <= (100 - BUTTON_STEP)) { + printf("+ Increasing power: %i%% -> %i%%\n", target_power, target_power + BUTTON_STEP); + state_set_power(target_power + BUTTON_STEP); } else { - printf("+ Increasing power: %i%% -> %i%%\n", current_power, 100); + printf("+ Increasing power: %i%% -> %i%%\n", target_power, 100); state_set_power(100); } } if (gpio_get(BUTTON_PIN_MINUS)) { if (bt_connected) { printf("-> Ignoring input, bluetooth connected.\n"); - } else if (current_power >= BUTTON_STEP) { - printf("- Reducing power: %i%% -> %i%%\n", current_power, current_power - BUTTON_STEP); - state_set_power(current_power - BUTTON_STEP); + } else if (target_power >= BUTTON_STEP) { + printf("- Reducing power: %i%% -> %i%%\n", target_power, target_power - BUTTON_STEP); + state_set_power(target_power - BUTTON_STEP); } else { - printf("- Reducing power: %i%% -> %i%%\n", current_power, 0); + printf("- Reducing power: %i%% -> %i%%\n", target_power, 0); state_set_power(0); } } diff --git a/src/main.c b/src/main.c index 2bde2a3..b26c853 100644 --- a/src/main.c +++ b/src/main.c @@ -30,7 +30,9 @@ static void heartbeat_handler(struct btstack_timer_source* ts) { int main() { stdio_init_all(); stdio_rtt_init(); - printf("Starting...\n"); + + printf("Underclocking CPU...\n"); + //set_sys_clock_khz(18000, true); if (cyw43_arch_init()) { printf("CYW43 init failed\n"); @@ -62,9 +64,6 @@ int main() { // turn on bluetooth! hci_power_control(HCI_POWER_ON); - printf("Underclocking CPU...\n"); - set_sys_clock_khz(18000, true); - btstack_run_loop_execute(); return 0; } diff --git a/src/state.c b/src/state.c index ef97dc6..236427e 100644 --- a/src/state.c +++ b/src/state.c @@ -1,4 +1,5 @@ #include "state.h" +#include "btstack.h" #include "hardware/pwm.h" #include "pico/stdlib.h" @@ -8,6 +9,30 @@ static int pwm_slice = 0; uint8_t current_power = 0; +uint8_t target_power = 0; + +#define RAMP_PERIOD_MS 100 +#define RAMP_STEP 50 + +// Timer and callback for ramping power. Jumping ~50% power or more in one go kills the +// chip with electrical ripples, so be a bit gentle about it. +static btstack_timer_source_t ramp_timer; +static void ramp_timer_callback(struct btstack_timer_source* ts) { + if (current_power != target_power) { + if (current_power < target_power - RAMP_STEP) { + current_power += RAMP_STEP; + } else if (current_power > target_power + RAMP_STEP) { + current_power -= RAMP_STEP; + } else { + current_power = target_power; + } + pwm_set_chan_level(pwm_slice, PWM_CHAN_A, current_power); + pwm_set_chan_level(pwm_slice, PWM_CHAN_B, current_power); + } + + btstack_run_loop_set_timer(ts, RAMP_PERIOD_MS); + btstack_run_loop_add_timer(ts); +} void state_init() { pwm_slice = pwm_gpio_to_slice_num(PWM_PIN_0); @@ -18,13 +43,14 @@ void state_init() { pwm_set_wrap(pwm_slice, 99); state_set_power(0); pwm_set_enabled(pwm_slice, true); + + ramp_timer.process = ramp_timer_callback; + ramp_timer.process(&ramp_timer); } void state_set_power(uint8_t power) { if (power > 100) { power = 100; } - current_power = power; - pwm_set_chan_level(pwm_slice, PWM_CHAN_A, power); - pwm_set_chan_level(pwm_slice, PWM_CHAN_B, power); + target_power = power; } diff --git a/src/state.h b/src/state.h index 7dbe9b8..d34ae92 100644 --- a/src/state.h +++ b/src/state.h @@ -5,11 +5,13 @@ // Current power level, 0-100. extern uint8_t current_power; +// Target power level (that we're ramping to), 0-100. +extern uint8_t target_power; // Initialize wand state. void state_init(); -// Set the wand's power, 0-100. +// Set the wand's target power, 0-100. void state_set_power(uint8_t power); #endif