This commit is contained in:
embr 2023-04-25 16:36:13 +02:00
parent 792d00bc05
commit 34d94d3ee5
6 changed files with 83 additions and 0 deletions

View file

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

63
src/buttons.c Normal file
View file

@ -0,0 +1,63 @@
#include "buttons.h"
#include "btstack.h"
#include "hardware/gpio.h"
#include "pico/stdlib.h"
#include "state.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 (current_power == 0) {
printf("= Resuming: %i%% -> %i%%\n", current_power, resume_power);
state_set_power(resume_power);
} else {
printf("= Suspending: %i%% -> %i%%\n", current_power, resume_power);
resume_power = current_power;
state_set_power(0);
}
}
if (gpio_get(BUTTON_PIN_PLUS)) {
state_set_power(current_power + BUTTON_STEP);
}
if (gpio_get(BUTTON_PIN_MINUS)) {
if (current_power < BUTTON_STEP) {
state_set_power(0);
} else {
state_set_power(current_power - BUTTON_STEP);
}
}
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

@ -5,6 +5,7 @@
#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>
@ -42,6 +43,9 @@ int main() {
// set initial state
state_init();
// hook up buttons
buttons_init();
att_server_init(profile_data, bt_att_read_callback, bt_att_write_callback);
hci_event_callback_registration.callback = &bt_packet_handler;

View file

@ -7,6 +7,8 @@
static int pwm_slice = 0;
uint8_t current_power = 0;
void state_init() {
pwm_slice = pwm_gpio_to_slice_num(PWM_PIN_0);
@ -19,6 +21,10 @@ void state_init() {
}
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);
}

View file

@ -3,6 +3,9 @@
#include <stdint.h>
// Current power level, 0-100.
extern uint8_t current_power;
// Initialize wand state.
void state_init();