Compare commits

..

No commits in common. "589ca48d6420a5e423fbe4a887d12d2369aa3599" and "ff1e83657d477b3e9f0a7964133f38b2976893b4" have entirely different histories.

6 changed files with 105 additions and 13 deletions

View file

@ -19,6 +19,7 @@ add_executable(vibe-check
src/main.c src/main.c
src/state.c src/state.c
src/vibe_bt.c src/vibe_bt.c
src/buttons.c
) )
target_link_libraries(vibe-check target_link_libraries(vibe-check

78
src/buttons.c Normal file
View file

@ -0,0 +1,78 @@
#include "buttons.h"
#include "btstack.h"
#include "hardware/gpio.h"
#include "pico/stdlib.h"
#include "state.h"
#include "vibe_bt.h"
#include <stdio.h>
#define POLL_PERIOD_MS 100
#define BUTTON_PIN_POWER 20
#define BUTTON_PIN_PLUS 19
#define BUTTON_PIN_MINUS 18
#define BUTTON_STEP 25
static btstack_timer_source_t button_timer;
// What power do we return to when the power button is pressed while off?
static uint8_t resume_power = 50;
static void button_timer_callback(struct btstack_timer_source* ts) {
// TODO: Debounce this.
if (gpio_get(BUTTON_PIN_POWER)) {
if (bt_connected) {
printf("=> Ignoring input, bluetooth connected.\n");
} 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", target_power, resume_power);
resume_power = target_power;
state_set_power(0);
}
}
if (gpio_get(BUTTON_PIN_PLUS)) {
if (bt_connected) {
printf("+> Ignoring input, bluetooth connected.\n");
} 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", target_power, 100);
state_set_power(100);
}
}
if (gpio_get(BUTTON_PIN_MINUS)) {
if (bt_connected) {
printf("-> Ignoring input, bluetooth connected.\n");
} 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", target_power, 0);
state_set_power(0);
}
}
btstack_run_loop_set_timer(ts, POLL_PERIOD_MS);
btstack_run_loop_add_timer(ts);
}
void buttons_init() {
gpio_set_dir(BUTTON_PIN_POWER, GPIO_IN);
gpio_set_dir(BUTTON_PIN_PLUS, GPIO_IN);
gpio_set_dir(BUTTON_PIN_MINUS, GPIO_IN);
button_timer.process = button_timer_callback;
button_timer.process(&button_timer);
// Why do these not trigger??
// printf("Setting interrupts for buttons...\n");
// gpio_set_irq_callback(button_callback);
// gpio_set_irq_enabled(BUTTON_PIN_POWER, BUTTON_EVENTS, true);
// gpio_set_irq_enabled(BUTTON_PIN_PLUS, BUTTON_EVENTS, true);
// gpio_set_irq_enabled(BUTTON_PIN_MINUS, BUTTON_EVENTS, true);
}

6
src/buttons.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef BUTTONS_H
#define BUTTONS_H
void buttons_init();
#endif

View file

@ -1,4 +1,5 @@
#include "btstack.h" #include "btstack.h"
#include "buttons.h"
#include "hci_dump.h" #include "hci_dump.h"
#include "hci_dump_segger_rtt_stdout.h" #include "hci_dump_segger_rtt_stdout.h"
#include "pico/btstack_cyw43.h" #include "pico/btstack_cyw43.h"
@ -46,6 +47,9 @@ int main() {
// set initial state // set initial state
state_init(); state_init();
// hook up buttons
buttons_init();
att_server_init(profile_data, bt_att_read_callback, bt_att_write_callback); att_server_init(profile_data, bt_att_read_callback, bt_att_write_callback);
hci_event_callback_registration.callback = &bt_packet_handler; hci_event_callback_registration.callback = &bt_packet_handler;

View file

@ -8,28 +8,26 @@
static int pwm_slice = 0; static int pwm_slice = 0;
// Note: These are all in % of a %, eg. 100% = 10000. uint8_t current_power = 0;
uint16_t current_power = 0; uint8_t target_power = 0;
uint16_t target_power = 0;
#define RAMP_PERIOD_MS 1 #define RAMP_PERIOD_MS 100
#define RAMP_STEP_UP 200 #define RAMP_STEP 50
#define RAMP_STEP_DOWN 50
// Timer and callback for ramping power. Jumping ~50% power or more in one go kills the // 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. // chip with electrical ripples, so be a bit gentle about it.
static btstack_timer_source_t ramp_timer; static btstack_timer_source_t ramp_timer;
static void ramp_timer_callback(struct btstack_timer_source* ts) { static void ramp_timer_callback(struct btstack_timer_source* ts) {
if (current_power != target_power) { if (current_power != target_power) {
if (current_power < target_power - RAMP_STEP_UP) { if (current_power < target_power - RAMP_STEP) {
current_power += RAMP_STEP_UP; current_power += RAMP_STEP;
} else if (current_power > target_power + RAMP_STEP_DOWN) { } else if (current_power > target_power + RAMP_STEP) {
current_power -= RAMP_STEP_DOWN; current_power -= RAMP_STEP;
} else { } else {
current_power = target_power; current_power = target_power;
} }
pwm_set_chan_level(pwm_slice, PWM_CHAN_A, current_power / 100); pwm_set_chan_level(pwm_slice, PWM_CHAN_A, current_power);
pwm_set_chan_level(pwm_slice, PWM_CHAN_B, current_power / 100); pwm_set_chan_level(pwm_slice, PWM_CHAN_B, current_power);
} }
btstack_run_loop_set_timer(ts, RAMP_PERIOD_MS); btstack_run_loop_set_timer(ts, RAMP_PERIOD_MS);
@ -54,5 +52,5 @@ void state_set_power(uint8_t power) {
if (power > 100) { if (power > 100) {
power = 100; power = 100;
} }
target_power = power * 100; target_power = power;
} }

View file

@ -3,6 +3,11 @@
#include <stdint.h> #include <stdint.h>
// 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. // Initialize wand state.
void state_init(); void state_init();