Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 155 additions & 59 deletions Buttons.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
/*
* Buttons.c
*
* Created on: 6 mars 2016
* author: Sebastien CAPOU (neskweek@gmail.com)
* Created on: 21 Octber 2016
* author: Sebastien CAPOU (neskweek@gmail.com) and Andras Kun (kun.andras@yahoo.de)
* Source : https://github.com/neskweek/LightSaberOS
*/
#include <DFPlayer.h>

#include "Buttons.h"
#include "Config.h"
#include "SoundFont.h"

extern DFPlayer dfplayer;

extern SoundFont soundFont;
extern bool actionMode;
extern bool configMode;
enum SaberStateEnum {S_STANDBY, S_SABERON, S_CONFIG, S_SLEEP, S_JUKEBOX};
enum ActionModeSubStatesEnum {AS_HUM, AS_IGNITION, AS_RETRACTION, AS_BLADELOCKUP, AS_BLASTERDEFLECTMOTION, AS_BLASTERDEFLECTPRESS, AS_CLASH, AS_SWING, AS_SPIN, AS_FORCE};
extern SaberStateEnum SaberState;
extern SaberStateEnum PrevSaberState;
extern ActionModeSubStatesEnum ActionModeSubStates;
//extern bool actionMode;
//extern bool configMode;
extern unsigned long sndSuppress;
extern bool hum_playing;
extern int8_t modification;
extern bool play;
extern bool blasterBlocks;
extern bool lockup;
#ifdef JUKEBOX
extern bool jukebox_play;
extern uint8_t jb_track;
#endif
//extern bool blasterBlocks;
//extern bool lockup;
extern int8_t blink;
extern bool changeMenu;
extern uint8_t menu;
extern bool enterMenu;

#if defined LEDSTRINGS
//extern uint8_t ledPins[] = { LEDSTRING1, LEDSTRING2, LEDSTRING3, LEDSTRING4,
//LEDSTRING5, LEDSTRING6 };
extern uint8_t blasterPin;
#endif
extern uint8_t blaster;
extern void HumRelaunch();
extern void SinglePlay_Sound(uint8_t track);
extern void LoopPlay_Sound(uint8_t track);
extern void Pause_Sound();
extern void Resume_Sound();
extern void Set_Loop_Playback();
// ====================================================================================
// === BUTTONS CALLBACK FUNCTIONS ===
// ====================================================================================
Expand All @@ -32,33 +53,49 @@ void mainClick() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Main button click."));
#endif
if (actionMode) {
if (SaberState==S_SABERON) {
if (soundFont.getForce()) {

ActionModeSubStates=AS_FORCE;
// Some Soundfont may not have Force sounds
if (millis() - sndSuppress >= 30) {
dfplayer.playPhysicalTrack(soundFont.getForce());
SinglePlay_Sound(soundFont.getForce());
sndSuppress = millis();
}
}
} else if (configMode) {
} else if (SaberState==S_CONFIG) {
//Button "+"
modification = 1;
play = true;
} else if (!configMode && !actionMode) {
} else if (SaberState==S_STANDBY) {
// LightSaber poweron
actionMode = true;
;
SaberState=S_SABERON;
PrevSaberState=S_STANDBY;
ActionModeSubStates=AS_IGNITION;
//actionMode = true;
}
#ifdef JUKEBOX
else if (SaberState==S_JUKEBOX) {
#if defined LS_BUTTON_DEBUG
Serial.print(F("Next JukeBox sound file "));Serial.print(jb_track);
#endif // jump to next song and start playing it
if (jb_track==NR_CONFIGFOLDERFILES+NR_JUKEBOXSONGS) {
jb_track=NR_CONFIGFOLDERFILES+1; // fold back to first song in the dir designated for music playback
}
else {
jb_track++;
}
SinglePlay_Sound(jb_track);
}
#endif
} // mainClick

void mainDoubleClick() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Main button double click."));
#endif
if (actionMode) {
if (SaberState==S_SABERON) {
//ACTION TO DEFINE
} else if (configMode) {
} else if (SaberState==S_CONFIG) {
// Trigger needs to be hardened with some sort of double click combinaison

//RESET CONFIG
Expand All @@ -69,7 +106,7 @@ void mainDoubleClick() {
// // }
// }

} else if (!configMode && !actionMode) {
} else if (SaberState==S_STANDBY) {
//ACTION TO DEFINE
}
} // mainDoubleClick
Expand All @@ -78,21 +115,27 @@ void mainLongPressStart() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Main button longPress start"));
#endif
if (actionMode) {
if (SaberState==S_SABERON) {
// LightSaber shutdown
actionMode = false;
} else if (configMode) {
ActionModeSubStates=AS_RETRACTION;
SaberState=S_STANDBY;
PrevSaberState=S_SABERON;
//actionMode = false;
} else if (SaberState==S_CONFIG) {
// Change Menu
changeMenu = true;
enterMenu = true;
menu++;
#if defined LUXEON
if (menu==4){menu=7;}
if (menu==5){menu=0;} // 3 menu items
#endif
#if defined LEDSTRINGS
if (menu==2){menu=4;}
if (menu==2){menu=0;} // 2 menu items
#endif
#if defined NEOPIXEL
if (menu==5){menu=0;} // 4 menu items
#endif
} else if (!configMode && !actionMode) {
} else if (SaberState==S_STANDBY) {
/*
* ACTION TO DEFINE
*/
Expand All @@ -103,16 +146,16 @@ void mainLongPress() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Main button longPress..."));
#endif
if (actionMode) {
if (SaberState==S_SABERON) {
/*
* ACTION TO DEFINE
*/

} else if (configMode) {
} else if (SaberState==S_CONFIG) {
/*
* ACTION TO DEFINE
*/
} else if (!configMode && !actionMode) {
} else if (SaberState==S_STANDBY) {
/*
* ACTION TO DEFINE
*/
Expand All @@ -123,7 +166,7 @@ void mainLongPressStop() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Main button longPress stop"));
#endif
if (!configMode && !actionMode) {
if (SaberState==S_STANDBY) {
/*
* ACTION TO DEFINE
*/
Expand All @@ -134,63 +177,117 @@ void lockupClick() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Lockup button click."));
#endif
if (actionMode) {
if (SaberState==S_SABERON) {
// Blaster

blasterBlocks = !blasterBlocks;

} else if (configMode) {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Start button activated blaster bolt deflect"));
#endif
ActionModeSubStates=AS_BLASTERDEFLECTPRESS;
} else if (SaberState==S_CONFIG) {
// Button "-"
modification = -1;
play = true;
} else if (!configMode && !actionMode) {
} else if (SaberState==S_STANDBY) {
/*
* ACTION TO DEFINE
*/
}
#ifdef JUKEBOX
else if (SaberState==S_JUKEBOX) {
if (jukebox_play) {
// pause the song
#if defined LS_BUTTON_DEBUG
Serial.println(F("Pause Song"));
#endif
jukebox_play=false;
Pause_Sound();
} else {
// resume playing the song
#if defined LS_BUTTON_DEBUG
Serial.println(F("Resume Song"));
#endif
jukebox_play=true;
Resume_Sound();
}

}
#endif

} // lockupClick

void lockupDoubleClick() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Lockup button double click."));
#endif
if (actionMode) {
//ACTION TO DEFINE
} else if (configMode) {
//ACTION TO DEFINE
} else if (!configMode && !actionMode) {
if (SaberState==S_SABERON) {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Start motion triggered blaster bolt deflect"));
#endif
if (ActionModeSubStates!=AS_BLASTERDEFLECTMOTION) { // start motion triggered blaster deflect
ActionModeSubStates=AS_BLASTERDEFLECTMOTION;
#if defined LS_BUTTON_DEBUG
Serial.println(F("Start motion triggered blaster bolt deflect"));
#endif
}
else { // stop motion triggered blaster deflect
#if defined LS_BUTTON_DEBUG
Serial.println(F("End motion triggered blaster bolt deflect"));
#endif
HumRelaunch();
ActionModeSubStates=AS_HUM;
}
} else if (SaberState==S_CONFIG) {
//ACTION TO DEFINE
}
#ifdef JUKEBOX
else if (SaberState==S_STANDBY) {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Enter JukeBox"));
#endif
SaberState=S_JUKEBOX;
PrevSaberState=S_STANDBY;
//ACTION TO DEFINE
} else if (SaberState==S_JUKEBOX) {
//Entering JukeBox mode (MP3 player)
SaberState=S_STANDBY;
PrevSaberState=S_JUKEBOX;
// stop/pause track being played
Pause_Sound();
}
#endif
} // lockupDoubleClick

void lockupLongPressStart() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Lockup button longPress start"));
#endif
if (actionMode) {
if (SaberState==S_SABERON) {
//Lockup Start
lockup = true;
ActionModeSubStates=AS_BLADELOCKUP;
blink=0;
// Serial.println(soundFont.getLockup());
if (soundFont.getLockup()) {
dfplayer.playPhysicalTrack(soundFont.getLockup());
SinglePlay_Sound(soundFont.getLockup());
sndSuppress = millis();
while (millis() - sndSuppress < 50) {
}
dfplayer.setSingleLoop(true);
Set_Loop_Playback();
sndSuppress = millis();
while (millis() - sndSuppress < 50) {
}
}
} else if (configMode) {
} else if (SaberState==S_CONFIG) {
//Leaving Config Mode
changeMenu = false;
// repeat = true;
configMode = false;
SaberState=S_STANDBY;
PrevSaberState=S_CONFIG;
//configMode = false;

} else if (!configMode && !actionMode) {
} else if (SaberState==S_STANDBY) {
//Entering Config Mode
configMode = true;
SaberState=S_CONFIG;
PrevSaberState=S_STANDBY;
//configMode = true;

}
} // lockupLongPressStart
Expand All @@ -199,15 +296,17 @@ void lockupLongPress() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Lockup button longPress..."));
#endif
if (actionMode) {
if (SaberState==S_SABERON) {
/*
* ACTION TO DEFINE
*/
} else if (configMode) {
ActionModeSubStates=AS_BLADELOCKUP; // needed, otherwise the FSM will change to AS_HUM and the lockup will end prematurely when the hum is relaunched
sndSuppress = millis(); // trick the hum relaunch by starting the stopper all over again otherwise the hum relaunch will interrupt the lockup
} else if (SaberState==S_CONFIG) {
/*
* ACTION TO DEFINE
*/
} else if (!configMode && !actionMode) {
} else if (SaberState==S_STANDBY) {
/*
* ACTION TO DEFINE
*/
Expand All @@ -218,13 +317,10 @@ void lockupLongPressStop() {
#if defined LS_BUTTON_DEBUG
Serial.println(F("Lockup button longPress stop"));
#endif
if (actionMode) {
//Lockup Stop
lockup = false;
dfplayer.playPhysicalTrack(soundFont.getHum());
delay(70);
dfplayer.setSingleLoop(true);
sndSuppress = millis();
if (SaberState==S_SABERON) {
HumRelaunch();
ActionModeSubStates=AS_HUM;
}
} // lockupLongPressStop


12 changes: 7 additions & 5 deletions Buttons.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Buttons.h
*
* Created on: 6 mars 2016
* author: Sebastien CAPOU (neskweek@gmail.com)
* Created on: 21 Octber 2016
* author: Sebastien CAPOU (neskweek@gmail.com) and Andras Kun (kun.andras@yahoo.de)
* Source : https://github.com/neskweek/LightSaberOS
*/
#include <Arduino.h>
Expand All @@ -13,9 +13,9 @@
/*
* BUTTONS PARAMETERS
************************************/
#define CLICK 5 // ms you need to press a button to be a click
#define PRESS_ACTION 200 // ms you need to press a button to be a long press, in action mode
#define PRESS_CONFIG 400 // ms you need to press a button to be a long press, in config mode
#define CLICK 200 //5 // ms you need to press a button to be a click
#define PRESS_ACTION 600 //200 // ms you need to press a button to be a long press, in action mode
#define PRESS_CONFIG 600 //400 // ms you need to press a button to be a long press, in config mode
/************************************/


Expand All @@ -38,3 +38,5 @@ void lockupLongPress();
void lockupLongPressStop();

#endif /* BUTTONS_H_ */


Loading