Skip to content

Commit

Permalink
Merge pull request #171: Issue 167: Save the last played card in EEPR…
Browse files Browse the repository at this point in the history
…OM and restore it at startup
  • Loading branch information
boerge1 authored Feb 12, 2024
2 parents 403f99e + 41513b9 commit 6ec6a88
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 118 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Die SD Karte (Ordner mp3 und advert) hat sich gegenüber der Version 3.1.5 geän

# Change Log

## Version 3.1.6 (31.01.2024)
## Version 3.1.6 (12.02.2024)
- [Issue 167](https://github.com/tonuino/TonUINO-TNG/issues/167): Save the last played card in EEPROM and restore it at startup
- [Issue 155](https://github.com/tonuino/TonUINO-TNG/issues/155): Implement a Quiz Game

## Version 3.1.5 (30.01.2024)
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.6 31.01.24\n"));
LOG(init_log, s_error, F("V3.1.6 12.02.24\n"));

Tonuino::getTonuino().setup();
}
Expand Down
24 changes: 12 additions & 12 deletions src/chip_card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool Chip_card::auth(MFRC522::PICC_Type piccType) {
return true;
}

Chip_card::readCardEvent Chip_card::readCard(nfcTagObject &nfcTag) {
Chip_card::readCardEvent Chip_card::readCard(folderSettings &nfcTag) {
// Show some details of the PICC (that is: the tag/card)
LOG(card_log, s_debug, F("Card UID: "), dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size));
const MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Expand Down Expand Up @@ -162,21 +162,21 @@ Chip_card::readCardEvent Chip_card::readCard(nfcTagObject &nfcTag) {
}
const uint32_t version = buffer[4];
if ((version != 0) && (version <= cardVersion)) {
nfcTag.nfcFolderSettings.folder = buffer[5];
nfcTag.nfcFolderSettings.mode = static_cast<pmode_t>(buffer[6]);
nfcTag.nfcFolderSettings.special = buffer[7];
nfcTag.nfcFolderSettings.special2 = buffer[8];
nfcTag.folder = buffer[5];
nfcTag.mode = static_cast<pmode_t>(buffer[6]);
nfcTag.special = buffer[7];
nfcTag.special2 = buffer[8];
}
else {
LOG(card_log, s_warning, F("bad ver "), version);
nfcTag.nfcFolderSettings.folder = 0;
nfcTag.nfcFolderSettings.mode = pmode_t::none;
nfcTag.folder = 0;
nfcTag.mode = pmode_t::none;
return readCardEvent::none;
}
return readCardEvent::known;
}

bool Chip_card::writeCard(const nfcTagObject &nfcTag) {
bool Chip_card::writeCard(const folderSettings &nfcTag) {

constexpr byte coockie_4 = (cardCookie & 0x000000ff) >> 0;
constexpr byte coockie_3 = (cardCookie & 0x0000ff00) >> 8;
Expand All @@ -185,10 +185,10 @@ bool Chip_card::writeCard(const nfcTagObject &nfcTag) {
byte buffer[buffferSizeWrite] = {coockie_1, coockie_2, coockie_3, coockie_4, // 0x1337 0xb347 magic cookie to
// identify our nfc tags
cardVersion, // version 1
nfcTag.nfcFolderSettings.folder, // the folder picked by the user
static_cast<uint8_t>(nfcTag.nfcFolderSettings.mode), // the playback mode picked by the user
nfcTag.nfcFolderSettings.special, // track or function for admin cards
nfcTag.nfcFolderSettings.special2,
nfcTag.folder, // the folder picked by the user
static_cast<uint8_t>(nfcTag.mode), // the playback mode picked by the user
nfcTag.special, // track or function for admin cards
nfcTag.special2,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

Expand Down
12 changes: 2 additions & 10 deletions src/chip_card.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ struct folderSettings {
}
};

// this object stores nfc tag data
struct nfcTagObject {
folderSettings nfcFolderSettings;
bool operator==(const nfcTagObject& rhs) const {
return nfcFolderSettings == rhs.nfcFolderSettings;
}
};

enum class cardEvent : uint8_t {
none ,
removed ,
Expand Down Expand Up @@ -93,8 +85,8 @@ class Chip_card {
empty,
};

readCardEvent readCard ( nfcTagObject &nfcTag);
bool writeCard (const nfcTagObject &nfcTag);
readCardEvent readCard ( folderSettings &nfcTag);
bool writeCard (const folderSettings &nfcTag);
void sleepCard ();
void initCard ();
cardEvent getCardEvent ();
Expand Down
8 changes: 7 additions & 1 deletion src/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ inline constexpr uint8_t neoPixelNumber = 24; // Total Number of Pixels
*/
//#define QUIZ_GAME

/* uncomment the below line to store the last played card in EEPROM
* um die letzte Karte im EEPROM zu speichern, in der nächste Zeile den Kommentar entfernen
*/
//#define STORE_LAST_CARD

/* #################################################################################################
* ##### normally, you don't have to edit lines below ############################
* ##### mormalerweise müssen die folgende Zeilen nicht editiert werden ############################
Expand All @@ -138,12 +143,13 @@ inline constexpr int getLevel(levelType t, level l) { return (l == level::inacti
: (t == levelType::activeHigh ? HIGH : LOW); }
// ####### rules for buttons ############################

inline constexpr uint8_t lastSortCut = 24;
#ifdef BUTTONS3X3
#ifdef FIVEBUTTONS
static_assert(false, "The 3x3 Button board doesn't have 5 Buttons");
#endif
inline constexpr uint8_t buttonExtSC_begin = 101;
inline constexpr uint8_t buttonExtSC_buttons = 18;
inline constexpr uint8_t buttonExtSC_buttons = 18; // <= lastSortCut
#endif // BUTTONS3X3

inline constexpr uint32_t buttonLongPress = 1000; // timeout for long press button in ms
Expand Down
8 changes: 4 additions & 4 deletions src/modifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ bool KindergardenMode::handleNext() {
LOG(modifier_log, s_info, str_KindergardenMode(), F(" -> NEXT"));
cardQueued = false;

tonuino.setCard(nextCard);
LOG(modifier_log, s_debug, F("Folder: "), nextCard.nfcFolderSettings.folder, F(" Mode: "), static_cast<uint8_t>(nextCard.nfcFolderSettings.mode));
tonuino.setMyFolder(nextCard, true /*myFolderIsCard*/);
LOG(modifier_log, s_debug, F("Folder: "), nextCard.folder, F(" Mode: "), static_cast<uint8_t>(nextCard.mode));
tonuino.playFolder();
mp3.loop(); // to start the new queue now and not going to Idle
return true;
}
return false;
}
bool KindergardenMode::handleRFID(const nfcTagObject &newCard) {
bool KindergardenMode::handleRFID(const folderSettings &newCard) {
if (!mp3.isPlaying())
return false;

Expand Down Expand Up @@ -107,7 +107,7 @@ bool RepeatSingleModifier::handlePrevious() {
// LOG(modifier_log, s_info, F("FeedbackModifier::handleVolumeUp()!"));
// return false;
//}
//bool FeedbackModifier::handleRFID(const nfcTagObject &/*newCard*/) {
//bool FeedbackModifier::handleRFID(const folderSettings &/*newCard*/) {
// LOG(modifier_log, s_info, F("FeedbackModifier::handleRFID()"));
// return false;
//}
12 changes: 6 additions & 6 deletions src/modifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Tonuino;
class Mp3;
struct nfcTagObject;
struct folderSettings;

class Modifier {
public:
Expand All @@ -20,7 +20,7 @@ class Modifier {
virtual bool handleNext () { return false; }
virtual bool handlePrevious () { return false; }
virtual bool handleButton(command ) { return false; }
virtual bool handleRFID(const nfcTagObject&)
virtual bool handleRFID(const folderSettings&)
{ return false; }
virtual pmode_t getActive () { return pmode_t::none; }
virtual void init (uint8_t) {}
Expand Down Expand Up @@ -63,7 +63,7 @@ class Locked: public Modifier {
public:
Locked(Tonuino &tonuino, Mp3 &mp3): Modifier(tonuino, mp3) {}
bool handleButton(command) final { LOG(modifier_log, s_debug, F("Locked::Button -> LOCKED!")) ; return true; }
bool handleRFID(const nfcTagObject&)
bool handleRFID(const folderSettings&)
final { LOG(modifier_log, s_debug, F("Locked::RFID -> LOCKED!")) ; return true; }

pmode_t getActive() final { return pmode_t::locked; }
Expand All @@ -82,13 +82,13 @@ class KindergardenMode: public Modifier {
KindergardenMode(Tonuino &tonuino, Mp3 &mp3): Modifier(tonuino, mp3) {}
bool handleNext ( ) final;
bool handleButton(command cmd ) final;
bool handleRFID (const nfcTagObject &newCard) final;
bool handleRFID (const folderSettings &newCard) final;

pmode_t getActive ( ) final { return pmode_t::kindergarden; }
void init (uint8_t ) final { cardQueued = false; }

private:
nfcTagObject nextCard{};
folderSettings nextCard{};
bool cardQueued = false;
};

Expand All @@ -109,7 +109,7 @@ class RepeatSingleModifier: public Modifier {
// FeedbackModifier(Tonuino &tonuino, Mp3 &mp3, const Settings &settings): Modifier(tonuino, mp3, settings) {}
// bool handleVolumeDown() final;
// bool handleVolumeUp () final;
// bool handleRFID (const nfcTagObject &newCard) final;
// bool handleRFID (const folderSettings &newCard) final;
//};

#endif /* SRC_MODIFIER_HPP_ */
11 changes: 4 additions & 7 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ constexpr uint16_t endAddress = 256;

#ifdef BUTTONS3X3
constexpr uint16_t maxExtraShortcuts = (endAddress - startAddressExtraShortcuts) / sizeof(folderSettings);
static_assert(buttonExtSC_buttons <= maxExtraShortcuts, "Too many ExtraShortCuts");
static_assert(buttonExtSC_buttons < maxExtraShortcuts, "Too many ExtraShortCuts");
#endif
}

#ifdef BUTTONS3X3
folderSettings Settings::extShortCut{};
#endif

void Settings::clearEEPROM() {
LOG(settings_log, s_info, F("clEEPROM"));
for (uint16_t i = startAddressFolderSettings; i < endAddress; ++i) {
Expand Down Expand Up @@ -121,17 +117,18 @@ void Settings::readExtShortCutFromFlash(uint8_t shortCut, folderSettings&
}


folderSettings& Settings::getShortCut(uint8_t shortCut) {
folderSettings Settings::getShortCut(uint8_t shortCut) {
if (shortCut > 0 && shortCut <= 4)
return shortCuts[shortCut-1];
#ifdef BUTTONS3X3
else if (shortCut >= buttonExtSC_begin && shortCut < buttonExtSC_begin + buttonExtSC_buttons) {
folderSettings extShortCut;
readExtShortCutFromFlash(shortCut-buttonExtSC_begin, extShortCut);
return extShortCut;
}
#endif

return shortCuts[0];
return { 0, pmode_t::none, 0, 0 };
}

void Settings::setShortCut(uint8_t shortCut, const folderSettings& value) {
Expand Down
8 changes: 2 additions & 6 deletions src/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ struct Settings {
void writeExtShortCutToFlash (uint8_t shortCut, const folderSettings& value);
void readExtShortCutFromFlash(uint8_t shortCut, folderSettings& value);

folderSettings& getShortCut(uint8_t shortCut);
void setShortCut(uint8_t shortCut, const folderSettings& value);
folderSettings getShortCut(uint8_t shortCut);
void setShortCut(uint8_t shortCut, const folderSettings& value);

uint32_t cookie;
byte version;
Expand All @@ -44,10 +44,6 @@ struct Settings {
uint8_t adminMenuLocked;
pin_t adminMenuPin;
uint8_t pauseWhenCardRemoved;

#ifdef BUTTONS3X3
static folderSettings extShortCut;
#endif
};

// emulates EEPROM.put() .get() and .update() on LGT8F328P platform
Expand Down
Loading

0 comments on commit 6ec6a88

Please sign in to comment.