Compare commits

...

4 commits

Author SHA1 Message Date
embr ff1e83657d clock down to 64khz 2023-05-01 22:09:13 +02:00
embr 67bb342cae bodge around power fuckery (and un-downclock for now) 2023-05-01 21:41:53 +02:00
embr 3c06c31114 underclock CPU to 18MHz 2023-04-26 00:44:42 +02:00
embr 8025f0d380 clang-format 2023-04-26 00:44:29 +02:00
4 changed files with 53 additions and 21 deletions

View file

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

View file

@ -1,11 +1,11 @@
#include "btstack.h"
#include "buttons.h"
#include "hci_dump.h"
#include "hci_dump_segger_rtt_stdout.h"
#include "pico/btstack_cyw43.h"
#include "pico/cyw43_arch.h"
#include "pico/stdlib.h"
#include "state.h"
#include "buttons.h"
#include "stdio_rtt.h"
#include "vibe_bt.h"
#include <stdio.h>
@ -31,8 +31,12 @@ int main() {
stdio_init_all();
stdio_rtt_init();
sleep_ms(1000);
printf("Underclocking CPU...\n");
set_sys_clock_khz(64000, true);
if (cyw43_arch_init()) {
printf("CYW43 init failed");
printf("CYW43 init failed\n");
return -1;
}

View file

@ -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);
if (power > 100) {
power = 100;
}
target_power = power;
}

View file

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