Skip to content

Commit

Permalink
Implement mute button and hold-to-bootloader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Mar 22, 2024
1 parent 492de18 commit 6273952
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pico_sdk_init()
# Include Pimoroni libraries
include(drivers/rgbled/rgbled)
include(drivers/encoder/encoder)
include(drivers/button/button)

# Add your source files
add_executable(${NAME})
Expand All @@ -42,10 +43,11 @@ add_compile_definitions(${NAME}
PICO_AUDIO_I2S_MONO_INPUT=0
PICO_AUDIO_I2S_DATA_PIN=14
PICO_AUDIO_I2S_CLOCK_PIN_BASE=15
DEBUG_BOOTLOADER_SHORTCUT=1
)

target_link_libraries(${NAME} PUBLIC
pico_stdlib pico_audio_i2s pico_unique_id pico_multicore rgbled encoder tinyusb_device tinyusb_board
pico_stdlib pico_audio_i2s pico_unique_id pico_multicore rgbled encoder button tinyusb_device tinyusb_board
)

# create map/bin/hex file etc.
Expand Down
33 changes: 33 additions & 0 deletions src/board.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#include "encoder.hpp"
#include "rgbled.hpp"
#include "button.hpp"
#include "board_config.h"

#include "pico/bootrom.h"
#include "hardware/structs/rosc.h"
#include "hardware/watchdog.h"
#include "hardware/sync.h"

using namespace encoder;
using namespace pimoroni;

Encoder volume_control(pio1, 0, {ENC_A, ENC_B});
RGBLED rgbled(LED_R, LED_G, LED_B);
Button button(BUTTON, pimoroni::ACTIVE_LOW, 0);

bool pressed = false;
absolute_time_t pressed_time;

extern "C" {
void system_init() {
Expand All @@ -24,6 +34,29 @@ extern "C" {
return volume_control.delta();
}

bool get_mute_button_pressed() {
return button.read();
}

void handle_mute_button_held() {
#ifdef DEBUG_BOOTLOADER_SHORTCUT
bool current = button.raw();
if(current && pressed) {
if (absolute_time_diff_us(pressed_time, get_absolute_time()) >= 2000000ul) {
sleep_ms(500);
save_and_disable_interrupts();
rosc_hw->ctrl = ROSC_CTRL_ENABLE_VALUE_ENABLE << ROSC_CTRL_ENABLE_LSB;
reset_usb_boot(0, 0);
}
} else if (current) {
pressed_time = get_absolute_time();
pressed = true;
} else {
pressed = false;
}
#endif
}

void system_led(uint8_t r, uint8_t g, uint8_t b) {
rgbled.set_rgb(r, g, b);
}
Expand Down
4 changes: 3 additions & 1 deletion src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

void system_init();
int32_t get_volume_delta();
void system_led(uint8_t r, uint8_t g, uint8_t b);
void system_led(uint8_t r, uint8_t g, uint8_t b);
bool get_mute_button_pressed();
void handle_mute_button_held();
8 changes: 8 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,14 @@ void audio_task(void)
if (board_millis() - start_ms >= volume_interval_ms)
{
int32_t volume_delta = get_volume_delta();

// Long press triggers reset to bootloader
handle_mute_button_held();

if(get_mute_button_pressed()) {
mute[0] = !mute[0];
mute[1] = !mute[1];
}
/*if(volume_delta > 0) {
system_led(0, 255, 0);
} else if (volume_delta < 0) {
Expand Down

0 comments on commit 6273952

Please sign in to comment.