Skip to content

Commit

Permalink
Merge pull request #203: Issue_202: Enhance modifier card SleepTimer …
Browse files Browse the repository at this point in the history
…to stop only after the track
  • Loading branch information
boerge1 authored Apr 30, 2024
2 parents 3f21cc9 + 586c90f commit 1b52ec4
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 112 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ Die SD Karte (Ordner mp3 und advert) hat sich gegenüber der Version 3.1.7 geän

# Change Log

## Version 3.1.8 (25.04.2024)
## Version 3.1.8 (26.04.2024)
- [Issue 202](https://github.com/tonuino/TonUINO-TNG/issues/202): Enhance modifier card SleepTimer to stop only after the track finished
- [Issue 200](https://github.com/tonuino/TonUINO-TNG/issues/200): Add possibility to use Pololu-Powerswitch for shutdown
- [Issue 196](https://github.com/tonuino/TonUINO-TNG/issues/196): Enhance Hoerbuch_1 mode to play more tracks
- [Issue 197](https://github.com/tonuino/TonUINO-TNG/issues/197): SPECIAL_START_SHORTCUT: pin A6 cannot be read digital on Nano
Expand Down
2 changes: 1 addition & 1 deletion TonUINO-TNG.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void setup()
LOG(init_log, s_error, F("TonUINO Version 3.1 - refactored by Boerge1\n"));
LOG(init_log, s_error, F("created by Thorsten Voß and licensed under GNU/GPL."));
LOG(init_log, s_error, F("Information and contribution at https://tonuino.de.\n"));
LOG(init_log, s_error, F("V3.1.8 25.04.24\n"));
LOG(init_log, s_error, F("V3.1.8 26.04.24\n"));

Tonuino::getTonuino().setup();
}
Expand Down
Binary file added sd-card/mp3/0938_modifier_sleep_mode.mp3
Binary file not shown.
55 changes: 29 additions & 26 deletions src/modifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace {

Tonuino &tonuino = Tonuino::getTonuino();
Mp3 &mp3 = tonuino.getMp3();

const __FlashStringHelper* str_SleepTimer () { return F("SleepTimer") ; }
const __FlashStringHelper* str_FreezeDance () { return F("FreezeDance") ; }
const __FlashStringHelper* str_KindergardenMode () { return F("Kita") ; }
Expand All @@ -16,17 +19,40 @@ const __FlashStringHelper* str_RepeatSingleModifier() { return F("RepeatSingle")

void SleepTimer::loop() {
if (sleepTimer.isActive() && sleepTimer.isExpired()) {
LOG(modifier_log, s_info, str_SleepTimer(), F(" -> expired"));
if (not stopAfterTrackFinished || stopAfterTrackFinished_active) {
LOG(modifier_log, s_info, str_SleepTimer(), F(" -> SLEEP!"));
if (SM_tonuino::is_in_state<Play>())
SM_tonuino::dispatch(command_e(commandRaw::pause));
//tonuino.resetActiveModifier();
}
else {
stopAfterTrackFinished_active = true;
sleepTimer.start(10 * 60000);
}
}
}
bool SleepTimer::handleNext() {
if (stopAfterTrackFinished_active) {
LOG(modifier_log, s_info, str_SleepTimer(), F(" -> SLEEP!"));
if (SM_tonuino::is_in_state<Play>())
SM_tonuino::dispatch(command_e(commandRaw::pause));
mp3.clearFolderQueue();
stopAfterTrackFinished_active = false;
sleepTimer.stop();
//tonuino.resetActiveModifier();
}
return false;
}

void SleepTimer::init(uint8_t special /* is minutes*/) {
LOG(modifier_log, s_info, str_SleepTimer(), F(" minutes: "), special);
stopAfterTrackFinished_active = false;
if (special > 0x80) {
stopAfterTrackFinished = true;
special -= 0x80;
} else {
stopAfterTrackFinished = false;
}
sleepTimer.start(special * 60000);
//playAdvertisement(advertTracks::t_302_sleep);
}

void FreezeDance::loop() {
Expand Down Expand Up @@ -88,26 +114,3 @@ bool RepeatSingleModifier::handleNext() {
bool RepeatSingleModifier::handlePrevious() {
return handleNext();
}

//bool FeedbackModifier::handleVolumeDown() {
// if (volume > settings.minVolume) {
// playAdvertisement(volume - 1, false);
// } else {
// playAdvertisement(volume, false);
// }
// LOG(modifier_log, s_info, F("FeedbackModifier::handleVolumeDown()!"));
// return false;
//}
//bool FeedbackModifier::handleVolumeUp() {
// if (volume < settings.maxVolume) {
// playAdvertisement(volume + 1, false);
// } else {
// playAdvertisement(volume, false);
// }
// LOG(modifier_log, s_info, F("FeedbackModifier::handleVolumeUp()!"));
// return false;
//}
//bool FeedbackModifier::handleRFID(const folderSettings &/*newCard*/) {
// LOG(modifier_log, s_info, F("FeedbackModifier::handleRFID()"));
// return false;
//}
32 changes: 10 additions & 22 deletions src/modifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct folderSettings;

class Modifier {
public:
Modifier(Tonuino &tonuino, Mp3 &mp3): tonuino(tonuino), mp3(mp3) {}
Modifier() {}
//virtual ~Modifier() {}
virtual void loop () {}
virtual bool handleNext () { return false; }
Expand All @@ -26,26 +26,26 @@ class Modifier {
virtual void init (uint8_t) {}

Modifier& operator=(const Modifier&) = delete;
protected:
Tonuino &tonuino;
Mp3 &mp3;
};

class SleepTimer: public Modifier {
public:
SleepTimer(Tonuino &tonuino, Mp3 &mp3): Modifier(tonuino, mp3) {}
SleepTimer() {}
void loop () final;
bool handleNext () final;

pmode_t getActive () final { return pmode_t::sleep_timer; }
void init(uint8_t) final;

private:
Timer sleepTimer{};
bool stopAfterTrackFinished{};
bool stopAfterTrackFinished_active{};
};

class FreezeDance: public Modifier {
public:
FreezeDance(Tonuino &tonuino, Mp3 &mp3): Modifier(tonuino, mp3) {}
FreezeDance() {}
void loop () final;

pmode_t getActive () final { return pmode_t::freeze_dance; }
Expand All @@ -61,7 +61,7 @@ class FreezeDance: public Modifier {

class Locked: public Modifier {
public:
Locked(Tonuino &tonuino, Mp3 &mp3): Modifier(tonuino, mp3) {}
Locked() {}
bool handleButton(command) final { LOG(modifier_log, s_debug, F("Locked::Button -> LOCKED!")) ; return true; }
bool handleRFID(const folderSettings&)
final { LOG(modifier_log, s_debug, F("Locked::RFID -> LOCKED!")) ; return true; }
Expand All @@ -71,15 +71,15 @@ class Locked: public Modifier {

class ToddlerMode: public Modifier {
public:
ToddlerMode(Tonuino &tonuino, Mp3 &mp3): Modifier(tonuino, mp3) {}
ToddlerMode() {}
bool handleButton(command) final { LOG(modifier_log, s_debug, F("ToddlerMode::Button -> LOCKED!")); return true; }

pmode_t getActive() final { return pmode_t::toddler; }
};

class KindergardenMode: public Modifier {
public:
KindergardenMode(Tonuino &tonuino, Mp3 &mp3): Modifier(tonuino, mp3) {}
KindergardenMode() {}
bool handleNext ( ) final;
bool handleButton(command cmd ) final;
bool handleRFID (const folderSettings &newCard) final;
Expand All @@ -94,22 +94,10 @@ class KindergardenMode: public Modifier {

class RepeatSingleModifier: public Modifier {
public:
RepeatSingleModifier(Tonuino &tonuino, Mp3 &mp3): Modifier(tonuino, mp3) {}
RepeatSingleModifier() {}
bool handleNext () final;
bool handlePrevious() final;
pmode_t getActive () final { return pmode_t::repeat_single; }
};

// An modifier can also do somethings in addition to the modified action
// by returning false (not handled) at the end
// This simple FeedbackModifier will tell the volume before changing it and
// give some feedback once a RFID card is detected.
//class FeedbackModifier: public Modifier {
//public:
// FeedbackModifier(Tonuino &tonuino, Mp3 &mp3, const Settings &settings): Modifier(tonuino, mp3, settings) {}
// bool handleVolumeDown() final;
// bool handleVolumeUp () final;
// bool handleRFID (const folderSettings &newCard) final;
//};

#endif /* SRC_MODIFIER_HPP_ */
1 change: 1 addition & 0 deletions src/mp3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ enum class mp3Tracks: uint16_t {
t_935_yes = 935,
t_936_batch_cards_intro = 936,
t_937_memory_game_cards_intro= 937,
t_938_modifier_sleep_mode = 938,
t_940_shortcut_into = 940,
t_941_pause = 941,
t_942_up = 942,
Expand Down
100 changes: 53 additions & 47 deletions src/state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1624,9 +1624,12 @@ void Admin_ModCard::entry() {

VoiceMenu::entry();

mode = pmode_t::none;
current_subState = start_writeCard;
readyToWrite = false;
current_subState = get_mode;

folder.mode = pmode_t::none;
folder.folder = 0;
folder.special = 0;
folder.special2 = 0;
}

void Admin_ModCard::react(command_e const &cmd_e) {
Expand All @@ -1635,65 +1638,68 @@ void Admin_ModCard::react(command_e const &cmd_e) {
}
const command cmd = commands.getCommand(cmd_e.cmd_raw, state_for_command::admin);

if (not readyToWrite)
if (current_subState != start_writeCard && current_subState != run_writeCard)
VoiceMenu::react(cmd);

if (isAbort(cmd))
return;

if (readyToWrite) {
switch (current_subState) {
case start_writeCard:
folder.folder = 0;
folder.special = 0;
folder.special2 = 0;
folder.mode = mode;
if (mode == pmode_t::sleep_timer)
switch (currentValue) {
case 1:
folder.special = 5;
break;
case 2:
folder.special = 15;
break;
case 3:
folder.special = 30;
break;
case 4:
folder.special = 60;
break;
}
SM_writeCard::folder = folder;
SM_writeCard::start();
current_subState = run_writeCard;
break;
case run_writeCard:
if (handleWriteCard(cmd_e))
return;
break;
default:
break;
}
return;
}
else if (Commands::isSelect(cmd) && (currentValue != 0)) {
if (mode == pmode_t::none) {
mode = static_cast<pmode_t>(currentValue);
if (mode != pmode_t::sleep_timer) {
switch (current_subState) {
case get_mode :
if (Commands::isSelect(cmd) && (currentValue != 0)) {
folder.mode = static_cast<pmode_t>(currentValue);
if (folder.mode != pmode_t::sleep_timer) {
mp3.clearMp3Queue();
readyToWrite = true;
current_subState = start_writeCard;
}
else {
numberOfOptions = 4;
startMessage = mp3Tracks::t_960_timer_intro;
messageOffset = mp3Tracks::t_960_timer_intro;
VoiceMenu::entry();
current_subState = get_sleeptime_timer;
}
}
else {
mp3.clearMp3Queue();
readyToWrite = true;
break;
case get_sleeptime_timer:
if (Commands::isSelect(cmd) && (currentValue != 0)) {
switch (currentValue) {
case 1:
folder.special = 5;
break;
case 2:
folder.special = 15;
break;
case 3:
folder.special = 30;
break;
case 4:
folder.special = 60;
break;
}
numberOfOptions = 2;
startMessage = mp3Tracks::t_938_modifier_sleep_mode;
messageOffset = mp3Tracks::t_933_switch_volume_intro;
VoiceMenu::entry();
current_subState = get_sleeptime_mode;
}
break;
case get_sleeptime_mode :
if (Commands::isSelect(cmd) && (currentValue != 0)) {
if (currentValue == 2)
folder.special += 0x80;
current_subState = start_writeCard;
}
break;
case start_writeCard :
SM_writeCard::folder = folder;
SM_writeCard::start();
current_subState = run_writeCard;
break;
case run_writeCard :
if (handleWriteCard(cmd_e))
return;
break;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,13 @@ class Admin_ModCard: public Amin_BaseWriteCard
void react(command_e const &) final;
private:
enum subState: uint8_t {
get_mode,
get_sleeptime_timer,
get_sleeptime_mode,
start_writeCard,
run_writeCard,
};
pmode_t mode {};
subState current_subState{};
bool readyToWrite {};
};

class Admin_ShortCut: public Admin_BaseSetting
Expand Down
4 changes: 2 additions & 2 deletions src/tonuino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,6 @@ void Tonuino::playTrackNumber () {
// Leider kann das Modul selbst keine Queue abspielen, daher müssen wir selbst die Queue verwalten
void Tonuino::nextTrack(uint8_t tracks, bool fromOnPlayFinished) {
LOG(play_log, s_info, F("nextTrack"));
if (activeModifier->handleNext())
return;
if (fromOnPlayFinished && mp3.isPlayingFolder() && (myFolder.mode == pmode_t::hoerbuch || myFolder.mode == pmode_t::hoerbuch_1)) {
const uint8_t trackToSave = (mp3.getCurrentTrack() < numTracksInFolder) ? mp3.getCurrentTrack()+1 : 1;
settings.writeFolderSettingToFlash(myFolder.folder, trackToSave);
Expand All @@ -276,6 +274,8 @@ void Tonuino::nextTrack(uint8_t tracks, bool fromOnPlayFinished) {
mp3.clearFolderQueue();
}
}
if (activeModifier->handleNext())
return;
mp3.playNext(tracks, fromOnPlayFinished);
if (not fromOnPlayFinished && mp3.isPlayingFolder() && (myFolder.mode == pmode_t::hoerbuch || myFolder.mode == pmode_t::hoerbuch_1)) {
settings.writeFolderSettingToFlash(myFolder.folder, mp3.getCurrentTrack());
Expand Down
15 changes: 7 additions & 8 deletions src/tonuino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,13 @@ class Tonuino {

friend class Base;

Modifier noneModifier {*this, mp3};
SleepTimer sleepTimer {*this, mp3};
FreezeDance freezeDance {*this, mp3};
Locked locked {*this, mp3};
ToddlerMode toddlerMode {*this, mp3};
KindergardenMode kindergardenMode {*this, mp3};
RepeatSingleModifier repeatSingleModifier{*this, mp3};
//FeedbackModifier feedbackModifier {*this, mp3};
Modifier noneModifier {};
SleepTimer sleepTimer {};
FreezeDance freezeDance {};
Locked locked {};
ToddlerMode toddlerMode {};
KindergardenMode kindergardenMode {};
RepeatSingleModifier repeatSingleModifier{};

Modifier* activeModifier {&noneModifier};

Expand Down
Loading

0 comments on commit 1b52ec4

Please sign in to comment.