Compare commits

..

3 commits

Author SHA1 Message Date
embr 5dc32c9136 ignore buttons if bluetooth is connected 2023-04-25 16:45:23 +02:00
embr 34d94d3ee5 buttons 2023-04-25 16:36:13 +02:00
embr 792d00bc05 ayy we can remote control an LED 2023-04-25 14:47:56 +02:00
6 changed files with 117 additions and 5 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
@ -26,6 +27,7 @@ target_link_libraries(vibe-check
pico_cyw43_arch_none
pico_btstack_ble
pico_btstack_cyw43
hardware_pwm
)
pico_btstack_make_gatt_header(vibe-check PRIVATE "${CMAKE_SOURCE_DIR}/src/vibe.gatt")

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 (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)) {
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 {
printf("+ Increasing power: %i%% -> %i%%\n", current_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 {
printf("- Reducing power: %i%% -> %i%%\n", current_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

@ -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>
@ -41,7 +42,9 @@ int main() {
// set initial state
state_init();
state_set_power(0);
// hook up buttons
buttons_init();
att_server_init(profile_data, bt_att_read_callback, bt_att_write_callback);

View file

@ -1,10 +1,30 @@
#include "state.h"
#include "hardware/pwm.h"
#include "pico/stdlib.h"
#define PWM_PIN_0 16
#define PWM_PIN_1 17
static int pwm_slice = 0;
uint8_t current_power = 0;
void state_init() {
// TODO: Assign GPIO pins to PWM.
pwm_slice = pwm_gpio_to_slice_num(PWM_PIN_0);
gpio_set_function(PWM_PIN_0, GPIO_FUNC_PWM);
gpio_set_function(PWM_PIN_1, GPIO_FUNC_PWM);
pwm_set_wrap(pwm_slice, 99);
state_set_power(0);
pwm_set_enabled(pwm_slice, true);
}
void state_set_power(uint8_t power) {
// TODO: Set 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,10 +3,13 @@
#include <stdint.h>
// Initializes wand state.
// Current power level, 0-100.
extern uint8_t current_power;
// Initialize wand state.
void state_init();
// Sets the wand's power, 0-100.
// Set the wand's power, 0-100.
void state_set_power(uint8_t power);
#endif