thanks, i have a working sketch currently for the stm32, can you suggest any improvements? i basically stole functions from the player.h file as well as the led strip library of the prop board
#include <Arduino.h>
#include "Player.h"
#include "Led.h"
#include <LedStrip.h>
LedStrip myLedStrip;
// Initialize PlayersPool instance
PlayersPool players = PlayersPool::getInstance();
Player* channelPlayers[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
#define DEBOUNCE_DELAY 50 // in milliseconds
bool lastChannelStates[6] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; // assume all pins are HIGH initially
unsigned long lastDebounceTimes[6] = {0, 0, 0, 0, 0, 0};
BoardLed led1(LED1);
BoardLed led2(LED2);
// Additional variables for bank switching
bool bankSwitchMode = false;
unsigned long bankSwitchStartTime = 0;
const unsigned long BANK_SWITCH_DURATION = 3000; // 3 seconds
const unsigned long LONG_PRESS_DURATION = 2000; // 2 seconds
unsigned long channel6PressStartTime = 0;
bool channel6Pressed = false;
char currentBank = '1'; // Default bank
bool checkingLongPress = false; // New flag to track if we're checking for a long press on CHANNEL6
void debugMsg(const char* type, const char* format, int pin_num) {
char msg[100];
sprintf(msg, format, pin_num);
Serial.print(type);
Serial.print(": ");
Serial.println(msg);
}
void releaseIfStopped(Player*& player) {
if (player && player->getStatus() == playerStopped) {
players.release(player);
player = NULL;
}
}
static void pollAudioActivityLED() {
static bool playing = false;
led2.setOn();
if (Audio.isPlaying() && !playing) {
playing = true;
led1.stopBlink();
led1.blink(250, 125);
} else if (!Audio.isPlaying() && playing) {
playing = false;
led1.stopBlink();
led1.blink(1000, 500);
}
}
void setup() {
Serial.begin(9600);
Audio.begin(44100);
led1.initialize();
led2.initialize();
pinMode(CHANNEL1, INPUT);
pinMode(CHANNEL2, INPUT);
pinMode(CHANNEL3, INPUT);
pinMode(CHANNEL4, INPUT);
pinMode(CHANNEL5, INPUT);
pinMode(CHANNEL6, INPUT);
led2.setOn();
led1.blink(1000, 500);
myLedStrip.begin(6, WS2812B);
players.initialize();
// Set the 1st LED with white color
myLedStrip.set(1, 255, 255, 255);
// Set the 2nd LED with red color
myLedStrip.set(2, 255, 0, 0);
// Set the 3rd LED with green color
myLedStrip.set(3, 0, 255, 0);
// Set the 4th LED with blue color
myLedStrip.set(4, 0, 0, 255);
// Update all the LEDs
myLedStrip.update();
// Delay a second
delay(1000);
// Sets all the LEDs with some color
// Note the first parameter: when zero
// all the LEDs are set with the same color
myLedStrip.set(0, 111, 55, 255);
// Update all the LEDs
myLedStrip.update();
// Delay half a second
delay(500);
}
void loop() {
pollAudioActivityLED();
bool currentChannel6State = digitalRead(CHANNEL6);
debugMsg("DebugInfo", "Current Channel 6 State:", currentChannel6State);
if (currentChannel6State == LOW && !channel6Pressed) {
channel6PressStartTime = millis();
channel6Pressed = true;
checkingLongPress = true;
debugMsg("DebugInfo", "Channel 6 Press Detected", 0);
} else if (currentChannel6State == HIGH && channel6Pressed) {
channel6Pressed = false;
channel6PressStartTime = 0;
checkingLongPress = false;
debugMsg("DebugInfo", "Channel 6 Release Detected", 0);
}
if (channel6Pressed && (millis() - channel6PressStartTime > LONG_PRESS_DURATION)) {
led1.blink(500, 500);
led2.blink(500, 500);
bankSwitchMode = true;
bankSwitchStartTime = millis();
channel6Pressed = false;
}
if (bankSwitchMode && (millis() - bankSwitchStartTime > BANK_SWITCH_DURATION)) {
bankSwitchMode = false;
led1.stopBlink();
led2.stopBlink();
}
if (bankSwitchMode) {
for (int i = 0; i < 5; i++) {
int pin = CHANNEL1 + i;
if (digitalRead(pin) == LOW) {
currentBank = '1' + i;
bankSwitchMode = false;
led1.stopBlink();
led2.stopBlink();
break;
}
}
} else {
for (int i = 0; i < 6; i++) {
int pin = CHANNEL1 + i;
bool currentReading = digitalRead(pin);
if (checkingLongPress && i == 5) continue;
if (currentReading != lastChannelStates[i]) {
lastDebounceTimes[i] = millis();
}
if ((millis() - lastDebounceTimes[i]) > DEBOUNCE_DELAY) {
if (currentReading == LOW && !channelPlayers[i]) {
channelPlayers[i] = players.acquire();
if (channelPlayers[i]) {
char filename[] = "\\0X\\Y.wav";
filename[2] = currentBank;
filename[4] = '1' + i;
if (!channelPlayers[i]->play(filename, PlayModeNormal)) {
debugMsg("DebugError", "Pin %i - error playing", pin);
players.release(channelPlayers[i]);
} else {
debugMsg("DebugInfo", "Pin %i playing", pin);
}
}
}
releaseIfStopped(channelPlayers[i]);
}
lastChannelStates[i] = currentReading;
}
}
players.poll();
delay(100);
}