Advertisement
Tigerigel

TonuinoProject-Rainer

Nov 21st, 2018
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.68 KB | None | 0 0
  1. // Visual Micro is in vMicro>General>Tutorial Mode
  2. //
  3. /*
  4.     Name:       TonuinoProjekt.ino
  5.     Created:    31.10.2018 11:19:23
  6.     Author:     (c) Thorsten Voß
  7. */
  8.  
  9. // Define User Types below here or use a .h file
  10. //
  11.  
  12. // Define Function Prototypes that use User Types below here or use a .h file
  13. //
  14. #include <stdint.h>
  15. #include <DFMiniMp3.h>
  16. #include <EEPROM.h>
  17. #include <JC_Button.h>
  18. #include <MFRC522.h>
  19. #include <SPI.h>
  20. #include <SoftwareSerial.h>
  21.  
  22. // Define Functions below here or use other .ino or cpp files
  23. //
  24.  
  25. // constants and variables
  26. //
  27. uint8_t maxVolume;                   // maximal volume of DFPlayer Mini
  28. uint8_t minVolume;                   // minimal volume of DFPlayer Mini
  29. uint8_t initVolume;                  // initial volume of DFPlayer Mini
  30. uint8_t playMode;                    // play mode 1=Admin, 2=Config, 3= Play
  31. short cancelFunc;                    // Ask for Cancel
  32. // this object stores nfc tag data
  33. struct nfcTagObject {
  34.     uint32_t cookie;
  35.     uint8_t version;
  36.     uint8_t folder;
  37.     uint8_t mode;
  38.     uint8_t special;
  39. };
  40. struct nfcTagObject myCard;
  41.  
  42. // DFPlayer Mini
  43. SoftwareSerial mySoftwareSerial(2, 3); // RX, TX
  44. uint16_t numTracksInFolder;
  45. uint16_t currentTrack;
  46.  
  47. static void nextTrack(uint16_t track);
  48. int voiceMenu(int numberOfOptions, int startMessage, int messageOffset,
  49.     bool preview = false, int previewFromFolder = 0);
  50. bool knownCard = false;
  51. int voiceMenuSpecial(int firstMsg, int lastMsg, int startMsg);
  52. void playMp3File(short mp3Number);
  53. void adminCard(short adminFunc);
  54. // implement a notification class, its member methods will get called
  55. //
  56. class Mp3Notify {
  57. public:
  58.     static void OnError(uint16_t errorCode) {
  59.         // see DfMp3_Error for code meaning
  60.         Serial.println();
  61.         Serial.print("Com Error ");
  62.         Serial.println(errorCode);
  63.     }
  64.     static void OnPlayFinished(uint16_t track) {
  65.         Serial.print("Track beendet");
  66.         Serial.println(track);
  67.         delay(100);
  68.         nextTrack(track);
  69.     }
  70.     static void OnCardOnline(uint16_t code) {
  71.         Serial.println(F("SD Karte online "));
  72.     }
  73.     static void OnCardInserted(uint16_t code) {
  74.         Serial.println(F("SD Karte bereit "));
  75.     }
  76.     static void OnCardRemoved(uint16_t code) {
  77.         Serial.println(F("SD Karte entfernt "));
  78.     }
  79. };
  80.  
  81. static DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(mySoftwareSerial);
  82. // Leider kann das Modul keine Queue abspielen.
  83. static uint16_t _lastTrackFinished;
  84.  
  85. // MFRC522
  86. //
  87. #define RST_PIN 9                 // Configurable, see typical pin layout above
  88. #define SS_PIN 10                 // Configurable, see typical pin layout above
  89. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522
  90. MFRC522::MIFARE_Key key;
  91. bool successRead;
  92. byte sector = 1;
  93. byte blockAddr = 4;
  94. byte trailerBlock = 7;
  95. MFRC522::StatusCode status;
  96.  
  97. #define buttonPause A0
  98. #define buttonUp A1
  99. #define buttonDown A2
  100. #define busyPin 4
  101.  
  102. #define LONG_PRESS 1000
  103.  
  104. Button pauseButton(buttonPause);
  105. Button upButton(buttonUp);
  106. Button downButton(buttonDown);
  107. bool ignorePauseButton = false;
  108. bool ignoreUpButton = false;
  109. bool ignoreDownButton = false;
  110.  
  111. uint8_t numberOfCards = 0;
  112.  
  113. bool isPlaying() { return !digitalRead(busyPin); }
  114.  
  115. // The setup() function runs once each time the micro-controller starts
  116. //
  117. void setup() {
  118.  
  119.     Serial.begin(115200); // Es gibt ein paar Debug Ausgaben über die serielle
  120.                           // Schnittstelle
  121.     randomSeed(analogRead(A0)); // Zufallsgenerator initialisieren
  122.  
  123.     Serial.println(F("TonUINO Version 2.0a"));
  124.     Serial.println(F("(c) Thorsten Voß"));
  125.  
  126.     // Knöpfe mit PullUp
  127.     pinMode(buttonPause, INPUT_PULLUP);
  128.     pinMode(buttonUp, INPUT_PULLUP);
  129.     pinMode(buttonDown, INPUT_PULLUP);
  130.  
  131.     // Busy Pin
  132.     pinMode(busyPin, INPUT);
  133.    
  134.     // init Volume
  135.     maxVolume = EEPROM.read(101);
  136.     minVolume = EEPROM.read(102);
  137.     initVolume = EEPROM.read(103);
  138.     playMode = EEPROM.read(104);
  139.     Serial.print("Max Volume = ");
  140.     Serial.println(maxVolume);
  141.     Serial.print("Min Volume = ");
  142.     Serial.println(minVolume);
  143.     Serial.print("Init Volume = ");
  144.     Serial.println(initVolume);
  145.     Serial.print("Play Mode = ");
  146.     Serial.println(playMode);
  147.  
  148.     if (maxVolume < 1 || maxVolume > 30) {
  149.         maxVolume = 15;
  150.         EEPROM.write(101, maxVolume);
  151.     }
  152.     if (minVolume < 1 || minVolume > 30) {
  153.         minVolume = 5;
  154.         EEPROM.write(102, minVolume);
  155.     }
  156.     if (initVolume < 1 || initVolume > 30) {
  157.         initVolume = 10;
  158.         EEPROM.write(103, initVolume);
  159.     }
  160.     //playMode = 9;
  161.     if (playMode < 1 || playMode > 3) {
  162.         playMode = 1;
  163.         EEPROM.write(104, playMode);
  164.     }
  165.     // DFPlayer Mini initialisieren
  166.     mp3.begin();
  167.     mp3.setVolume(initVolume);
  168.     //----Set EQ----
  169.     mp3.setEq(DfMp3_Eq_Normal);
  170.     //  mp3.setEq(DfMp3_Eq_Pop);
  171.     //  mp3.setEq(DfMp3_Eq_Rock);
  172.     //  mp3.setEq(DfMp3_Eq_Jazz);
  173.     //  mp3.setEq(DfMp3_Eq_Classic);
  174.     //  mp3.setEq(DfMp3_Eq_Bass);
  175.  
  176.     // NFC Leser initialisieren
  177.     SPI.begin();        // Init SPI bus
  178.     mfrc522.PCD_Init(); // Init MFRC522
  179.     mfrc522
  180.         .PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader
  181.     for (byte i = 0; i < 6; i++) {
  182.         key.keyByte[i] = 0xFF;
  183.     }
  184.  
  185.     // RESET --- ALLE DREI KNÖPFE BEIM STARTEN GEDRÜCKT HALTEN -> alle bekannten
  186.     // Karten werden gelöscht
  187.     if (digitalRead(buttonPause) == LOW && digitalRead(buttonUp) == LOW &&
  188.         digitalRead(buttonDown) == LOW) {
  189.         Serial.println(F("Reset -> EEPROM wird gelöscht"));
  190.         for (int i = 0; i < EEPROM.length(); i++) {
  191.             EEPROM.write(i, 0);
  192.         }
  193.     }
  194.     mp3.playMp3FolderTrack(950);
  195.     delay(3000);
  196.     mp3.playMp3FolderTrack(900);
  197. }
  198.  
  199. // Add the main program code into the continuous loop() function
  200. //
  201. void loop() {
  202.     do {
  203.         mp3.loop();
  204.  
  205.         // Buttons werden nun über JS_Button gehandelt, dadurch kann jede Taste
  206.         // doppelt belegt werden
  207.         pauseButton.read();
  208.         upButton.read();
  209.         downButton.read();
  210.  
  211.         if (pauseButton.wasReleased()) {
  212.             if (ignorePauseButton == false)
  213.                 if (isPlaying())
  214.                     mp3.pause();
  215.                 else
  216.                     mp3.start();
  217.             ignorePauseButton = false;
  218.         }
  219.         else if (pauseButton.pressedFor(LONG_PRESS) &&
  220.             ignorePauseButton == false) {
  221.             if (isPlaying()) {
  222.                 mp3.playAdvertisement(910);
  223.                 delay(2000);
  224.                 mp3.playAdvertisement(myCard.folder);
  225.                 delay(2000);
  226.                 mp3.playAdvertisement(911);
  227.                 delay(2000);
  228.                 mp3.playAdvertisement(currentTrack);
  229.             }
  230.             else if (playMode < 3) {
  231.                 knownCard = false;
  232.                 mp3.playMp3FolderTrack(800);
  233.                 Serial.println(F("Karte resetten..."));
  234.                 resetCard();
  235.                 mfrc522.PICC_HaltA();
  236.                 mfrc522.PCD_StopCrypto1();
  237.             }
  238.             ignorePauseButton = true;
  239.         }
  240.  
  241.         if (upButton.pressedFor(LONG_PRESS)) {
  242.             Serial.println(F("Volume Up"));
  243.             if (mp3.getVolume() < maxVolume) {
  244.                 mp3.increaseVolume();
  245.                 delay(200);
  246.             }
  247.             ignoreUpButton = true;
  248.         }
  249.         else if (upButton.wasReleased()) {
  250.             if (!ignoreUpButton)
  251.                 nextTrack(random(65536));
  252.             else
  253.                 ignoreUpButton = false;
  254.         }
  255.  
  256.         if (downButton.pressedFor(LONG_PRESS)) {
  257.             Serial.println(F("Volume Down"));
  258.             if (mp3.getVolume() > minVolume) {
  259.                 mp3.decreaseVolume();
  260.                 delay(200);
  261.             }
  262.             ignoreDownButton = true;
  263.         }
  264.         else if (downButton.wasReleased()) {
  265.             if (!ignoreDownButton)
  266.                 previousTrack();
  267.             else
  268.                 ignoreDownButton = false;
  269.         }
  270.         // Ende der Buttons
  271.     } while (!mfrc522.PICC_IsNewCardPresent());
  272.  
  273.     // RFID Karte wurde aufgelegt
  274.     //if (playMode == 3)
  275.     //  return;
  276.  
  277.     if (!mfrc522.PICC_ReadCardSerial())
  278.         return;
  279.  
  280.     if (readCard(&myCard) == true) {
  281.         if (myCard.cookie == 322417479 && myCard.folder != 0 && myCard.mode != 0) {
  282.  
  283.             knownCard = true;
  284.             _lastTrackFinished = 0;
  285.             numTracksInFolder = mp3.getFolderTrackCount(myCard.folder);
  286.             Serial.print(numTracksInFolder);
  287.             Serial.print(F(" Dateien in Ordner "));
  288.             Serial.println(myCard.folder);
  289.  
  290.             // Hörspielmodus: eine zufällige Datei aus dem Ordner
  291.             if (myCard.mode == 1) {
  292.                 Serial.println(F("Hörspielmodus -> zufälligen Track wiedergeben"));
  293.                 currentTrack = random(1, numTracksInFolder + 1);
  294.                 Serial.println(currentTrack);
  295.                 mp3.playFolderTrack(myCard.folder, currentTrack);
  296.             }
  297.             // Album Modus: kompletten Ordner spielen
  298.             if (myCard.mode == 2) {
  299.                 Serial.println(F("Album Modus -> kompletten Ordner wiedergeben"));
  300.                 currentTrack = 1;
  301.                 mp3.playFolderTrack(myCard.folder, currentTrack);
  302.             }
  303.             // Party Modus: Ordner in zufälliger Reihenfolge
  304.             if (myCard.mode == 3) {
  305.                 Serial.println(
  306.                     F("Party Modus -> Ordner in zufälliger Reihenfolge wiedergeben"));
  307.                 currentTrack = random(1, numTracksInFolder + 1);
  308.                 mp3.playFolderTrack(myCard.folder, currentTrack);
  309.             }
  310.             // Einzel Modus: eine Datei aus dem Ordner abspielen
  311.             if (myCard.mode == 4) {
  312.                 Serial.println(
  313.                     F("Einzel Modus -> eine Datei aus dem Odrdner abspielen"));
  314.                 currentTrack = myCard.special;
  315.                 mp3.playFolderTrack(myCard.folder, currentTrack);
  316.             }
  317.             // Hörbuch Modus: kompletten Ordner spielen und Fortschritt merken
  318.             if (myCard.mode == 5) {
  319.                 Serial.println(F("Hörbuch Modus -> kompletten Ordner spielen und "
  320.                     "Fortschritt merken"));
  321.                 currentTrack = EEPROM.read(myCard.folder);
  322.                 mp3.playFolderTrack(myCard.folder, currentTrack);
  323.             }
  324.             // Administratorkarte
  325.             if (myCard.mode == 6) {
  326.                 // Administratorfunktion 1
  327.                 if (myCard.special == 1) {
  328.                     adminCard(myCard.special);
  329.                 }
  330.             }
  331.             // Testkarte
  332.             if (myCard.mode == 7) {
  333.                 // Testkartenfunktion
  334.                 if (myCard.special == 1) {
  335.                     testCard();
  336.                 }
  337.             }
  338.         }
  339.  
  340.         // Neue Karte konfigurieren
  341.         else if (playMode < 3) {
  342.             knownCard = false;
  343.             setupCard();
  344.         }
  345.     }
  346.     mfrc522.PICC_HaltA();
  347.     mfrc522.PCD_StopCrypto1();
  348. }
  349.  
  350. // nextTrack Function
  351. //
  352. static void nextTrack(uint16_t track) {
  353.     if (track == _lastTrackFinished) {
  354.         return;
  355.     }
  356.     _lastTrackFinished = track;
  357.  
  358.     if (knownCard == false)
  359.         // Wenn eine neue Karte angelernt wird soll das Ende eines Tracks nicht
  360.         // verarbeitet werden
  361.         return;
  362.  
  363.     if (myCard.mode == 1) {
  364.         Serial.println(F("Hörspielmodus ist aktiv -> keinen neuen Track spielen"));
  365.         //    mp3.sleep(); // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
  366.     }
  367.     if (myCard.mode == 2) {
  368.         if (currentTrack != numTracksInFolder) {
  369.             currentTrack = currentTrack + 1;
  370.             mp3.playFolderTrack(myCard.folder, currentTrack);
  371.             Serial.print(F("Albummodus ist aktiv -> nächster Track: "));
  372.             Serial.print(currentTrack);
  373.         }
  374.         else
  375.             //      mp3.sleep();   // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
  376.         {
  377.         }
  378.     }
  379.     if (myCard.mode == 3) {
  380.         currentTrack = random(1, numTracksInFolder + 1);
  381.         Serial.print(F("Party Modus ist aktiv -> zufälligen Track spielen: "));
  382.         Serial.println(currentTrack);
  383.         mp3.playFolderTrack(myCard.folder, currentTrack);
  384.     }
  385.     if (myCard.mode == 4) {
  386.         Serial.println(F("Einzel Modus aktiv -> Strom sparen"));
  387.         //    mp3.sleep();      // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
  388.     }
  389.     if (myCard.mode == 5) {
  390.         if (currentTrack != numTracksInFolder) {
  391.             currentTrack = currentTrack + 1;
  392.             Serial.print(F("Hörbuch Modus ist aktiv -> nächster Track und "
  393.                 "Fortschritt speichern"));
  394.             Serial.println(currentTrack);
  395.             mp3.playFolderTrack(myCard.folder, currentTrack);
  396.             // Fortschritt im EEPROM abspeichern
  397.             EEPROM.write(myCard.folder, currentTrack);
  398.         }
  399.         else {
  400.             //      mp3.sleep();  // Je nach Modul kommt es nicht mehr zurück aus dem Sleep!
  401.                   // Fortschritt zurück setzen
  402.             EEPROM.write(myCard.folder, 1);
  403.         }
  404.     }
  405. }
  406.  
  407. // previousTrack Function
  408. //
  409. static void previousTrack() {
  410.     if (myCard.mode == 1) {
  411.         Serial.println(F("Hörspielmodus ist aktiv -> Track von vorne spielen"));
  412.         mp3.playFolderTrack(myCard.folder, currentTrack);
  413.     }
  414.     if (myCard.mode == 2) {
  415.         Serial.println(F("Albummodus ist aktiv -> vorheriger Track"));
  416.         if (currentTrack != 1) {
  417.             currentTrack = currentTrack - 1;
  418.         }
  419.         mp3.playFolderTrack(myCard.folder, currentTrack);
  420.     }
  421.     if (myCard.mode == 3) {
  422.         Serial.println(F("Party Modus ist aktiv -> Track von vorne spielen"));
  423.         mp3.playFolderTrack(myCard.folder, currentTrack);
  424.     }
  425.     if (myCard.mode == 4) {
  426.         Serial.println(F("Einzel Modus aktiv -> Track von vorne spielen"));
  427.         mp3.playFolderTrack(myCard.folder, currentTrack);
  428.     }
  429.     if (myCard.mode == 5) {
  430.         Serial.println(F("Hörbuch Modus ist aktiv -> vorheriger Track und "
  431.             "Fortschritt speichern"));
  432.         if (currentTrack != 1) {
  433.             currentTrack = currentTrack - 1;
  434.         }
  435.         mp3.playFolderTrack(myCard.folder, currentTrack);
  436.         // Fortschritt im EEPROM abspeichern
  437.         EEPROM.write(myCard.folder, currentTrack);
  438.     }
  439. }
  440.  
  441. // voiceMenu Function
  442. //
  443. int voiceMenu(int numberOfOptions, int startMessage, int messageOffset,
  444.     bool preview = false, int previewFromFolder = 0) {
  445.     int returnValue = 0;
  446.     if (startMessage != 0)
  447.         mp3.playMp3FolderTrack(startMessage);
  448.     do {
  449.         pauseButton.read();
  450.         upButton.read();
  451.         downButton.read();
  452.         mp3.loop();
  453.         if (pauseButton.wasPressed()) {
  454.             if (returnValue != 0)
  455.                 return returnValue;
  456.             delay(1000);
  457.         }
  458.  
  459.         if (upButton.pressedFor(LONG_PRESS)) {
  460.             returnValue = min(returnValue + 10, numberOfOptions);
  461.             mp3.playMp3FolderTrack(messageOffset + returnValue);
  462.             delay(1000);
  463.             if (preview) {
  464.                 do {
  465.                     delay(10);
  466.                 } while (isPlaying());
  467.                 if (previewFromFolder == 0)
  468.                     mp3.playFolderTrack(returnValue, 1);
  469.                 else
  470.                     mp3.playFolderTrack(previewFromFolder, returnValue);
  471.             }
  472.             ignoreUpButton = true;
  473.         }
  474.         else if (upButton.wasReleased()) {
  475.             if (!ignoreUpButton) {
  476.                 returnValue = min(returnValue + 1, numberOfOptions);
  477.                 mp3.playMp3FolderTrack(messageOffset + returnValue);
  478.                 delay(1000);
  479.                 if (preview) {
  480.                     do {
  481.                         delay(10);
  482.                     } while (isPlaying());
  483.                     if (previewFromFolder == 0)
  484.                         mp3.playFolderTrack(returnValue, 1);
  485.                     else
  486.                         mp3.playFolderTrack(previewFromFolder, returnValue);
  487.                 }
  488.             }
  489.             else
  490.                 ignoreUpButton = false;
  491.         }
  492.  
  493.         if (downButton.pressedFor(LONG_PRESS)) {
  494.             returnValue = max(returnValue - 10, 1);
  495.             mp3.playMp3FolderTrack(messageOffset + returnValue);
  496.             delay(1000);
  497.             if (preview) {
  498.                 do {
  499.                     delay(10);
  500.                 } while (isPlaying());
  501.                 if (previewFromFolder == 0)
  502.                     mp3.playFolderTrack(returnValue, 1);
  503.                 else
  504.                     mp3.playFolderTrack(previewFromFolder, returnValue);
  505.             }
  506.             ignoreDownButton = true;
  507.         }
  508.         else if (downButton.wasReleased()) {
  509.             if (!ignoreDownButton) {
  510.                 returnValue = max(returnValue - 1, 1);
  511.                 mp3.playMp3FolderTrack(messageOffset + returnValue);
  512.                 delay(1000);
  513.                 if (preview) {
  514.                     do {
  515.                         delay(10);
  516.                     } while (isPlaying());
  517.                     if (previewFromFolder == 0)
  518.                         mp3.playFolderTrack(returnValue, 1);
  519.                     else
  520.                         mp3.playFolderTrack(previewFromFolder, returnValue);
  521.                 }
  522.             }
  523.             else
  524.                 ignoreDownButton = false;
  525.         }
  526.     } while (true);
  527. }
  528.  
  529. // voiceMenuSpecial Function - für Lautstärkeänderung zum beliebigen Aufsetzen
  530. //
  531. //int voiceMenu(int numberOfOptions, int startMessage, int messageOffset,
  532. //  bool preview = false, int previewFromFolder = 0)
  533.  
  534. int voiceMenuSpecial(int firstMsg, int lastMsg, int startMsg) {
  535.     int returnValue = 0;
  536.     mp3.playMp3FolderTrack(startMsg);
  537.     returnValue = startMsg;
  538.     do {
  539.         pauseButton.read();
  540.         upButton.read();
  541.         downButton.read();
  542.         mp3.loop();
  543.         if (pauseButton.wasPressed()) {
  544.             return returnValue;
  545.             delay(1000);
  546.         }
  547.  
  548.         if (upButton.wasReleased()) {
  549.             if (!ignoreUpButton) {
  550.                 returnValue = min(returnValue + 1, lastMsg);
  551.                 mp3.playMp3FolderTrack(returnValue);
  552.                 delay(1000);
  553.             }
  554.             else
  555.                 ignoreUpButton = false;
  556.         }
  557.  
  558.          if (downButton.wasReleased()) {
  559.             if (!ignoreDownButton) {
  560.                 returnValue = max(returnValue - 1, firstMsg);
  561.                 mp3.playMp3FolderTrack(returnValue);
  562.                 delay(1000);
  563.             }
  564.             else
  565.                 ignoreDownButton = false;
  566.         }
  567.     } while (true);
  568. }
  569.  
  570. // resetCard Function
  571. //
  572. void resetCard() {
  573.     do {
  574.         pauseButton.read();
  575.         upButton.read();
  576.         downButton.read();
  577.  
  578.         if (upButton.wasReleased() || downButton.wasReleased()) {
  579.             Serial.print(F("Abgebrochen!"));
  580.             mp3.playMp3FolderTrack(802);
  581.             return;
  582.         }
  583.     } while (!mfrc522.PICC_IsNewCardPresent());
  584.  
  585.  
  586.     if (!mfrc522.PICC_ReadCardSerial()) {
  587.         Serial.print(F("Falsche Karte!"));
  588.         return;
  589.     }
  590.  
  591.     Serial.print(F("Karte wird neu Konfiguriert!"));
  592.     setupCard();
  593. }
  594.  
  595. // setupCard Function
  596. //
  597. void setupCard() {
  598.     mp3.pause();
  599.     Serial.print(F("Neue Karte konfigurieren"));
  600.  
  601.     // Abbruch abfragen
  602.     mp3.playMp3FolderTrack(350);
  603.     do {
  604.         pauseButton.read();
  605.         upButton.read();
  606.         downButton.read();
  607.    
  608.         if (upButton.wasReleased() || downButton.wasReleased()) {
  609.             Serial.print(F("Abgebrochen!"));
  610.             mp3.playMp3FolderTrack(802);
  611.             return;
  612.         }
  613.     } while (!pauseButton.wasPressed());
  614.  
  615.     // Ordner abfragen
  616.     myCard.folder = voiceMenu(99, 300, 0, true);
  617.  
  618.     // Wiedergabemodus abfragen
  619.     if (playMode == 1) {
  620.         myCard.mode = voiceMenu(7, 310, 310);
  621.     }
  622.     else {
  623.         myCard.mode = voiceMenu(5, 310, 310);
  624.     }
  625.  
  626.     // Einzelmodus -> Datei abfragen
  627.     if (myCard.mode == 4)
  628.         myCard.special = voiceMenu(mp3.getFolderTrackCount(myCard.folder), 320, 0,
  629.             true, myCard.folder);
  630.  
  631.     // Hörbuchmodus -> Fortschritt im EEPROM auf 1 setzen
  632.     if (myCard.mode == 5)
  633.         EEPROM.write(myCard.folder, 1);
  634.  
  635.     // Admin Funktionen, create Admin Card
  636.     if (myCard.mode == 6) {
  637.         myCard.folder = 1;
  638.         myCard.special = 1;
  639.         Serial.print(F("Administratorkarte erstellt"));
  640.     }
  641.  
  642.     // Test Funktionen, create Test Card
  643.     if (myCard.mode == 7) {
  644.         myCard.folder = 1;
  645.         myCard.special = 1;
  646.         Serial.print(F("Testkarte erstellt"));
  647.     }
  648.  
  649.     // Karte ist konfiguriert -> speichern
  650.     mp3.pause();
  651.     writeCard(myCard);
  652. }
  653.  
  654. // readCard Function
  655. //
  656. bool readCard(struct nfcTagObject *nfcTag) {
  657.     bool returnValue = true;
  658.     // Show some details of the PICC (that is: the tag/card)
  659.     Serial.print(F("Card UID:"));
  660.     dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  661.     Serial.println();
  662.     Serial.print(F("PICC type: "));
  663.     MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  664.     Serial.println(mfrc522.PICC_GetTypeName(piccType));
  665.  
  666.     byte buffer[18];
  667.     byte size = sizeof(buffer);
  668.  
  669.     // Authenticate using key A
  670.     Serial.println(F("Authenticating using key A..."));
  671.     status = (MFRC522::StatusCode)mfrc522.PCD_Authenticate(
  672.         MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  673.     if (status != MFRC522::STATUS_OK) {
  674.         returnValue = false;
  675.         Serial.print(F("PCD_Authenticate() failed: "));
  676.         Serial.println(mfrc522.GetStatusCodeName(status));
  677.         return;
  678.     }
  679.  
  680.     // Show the whole sector as it currently is
  681.     Serial.println(F("Current data in sector:"));
  682.     mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector);
  683.     Serial.println();
  684.  
  685.     // Read data from the block
  686.     Serial.print(F("Reading data from block "));
  687.     Serial.print(blockAddr);
  688.     Serial.println(F(" ..."));
  689.     status = (MFRC522::StatusCode)mfrc522.MIFARE_Read(blockAddr, buffer, &size);
  690.     if (status != MFRC522::STATUS_OK) {
  691.         returnValue = false;
  692.         Serial.print(F("MIFARE_Read() failed: "));
  693.         Serial.println(mfrc522.GetStatusCodeName(status));
  694.     }
  695.     Serial.print(F("Data in block "));
  696.     Serial.print(blockAddr);
  697.     Serial.println(F(":"));
  698.     dump_byte_array(buffer, 16);
  699.     Serial.println();
  700.     Serial.println();
  701.  
  702.     uint32_t tempCookie;
  703.     tempCookie = (uint32_t)buffer[0] << 24;
  704.     tempCookie += (uint32_t)buffer[1] << 16;
  705.     tempCookie += (uint32_t)buffer[2] << 8;
  706.     tempCookie += (uint32_t)buffer[3];
  707.  
  708.     nfcTag->cookie = tempCookie;
  709.     nfcTag->version = buffer[4];
  710.     nfcTag->folder = buffer[5];
  711.     nfcTag->mode = buffer[6];
  712.     nfcTag->special = buffer[7];
  713.  
  714.     return returnValue;
  715. }
  716.  
  717. // writeCard Function
  718. //
  719. void writeCard(struct nfcTagObject nfcTag) {
  720.     MFRC522::PICC_Type mifareType;
  721.     byte buffer[16] = { 0x13, 0x37, 0xb3, 0x47, // 0x1337 0xb347 magic cookie to
  722.                                                // identify our nfc tags
  723.                        0x01,                   // version 1
  724.                        nfcTag.folder,          // the folder picked by the user
  725.                        nfcTag.mode,    // the playback mode picked by the user
  726.                        nfcTag.special, // track or function for admin cards
  727.                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  728.  
  729.     byte size = sizeof(buffer);
  730.  
  731.     mifareType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  732.  
  733.     // Authenticate using key B
  734.     Serial.println(F("Authenticating again using key B..."));
  735.     status = (MFRC522::StatusCode)mfrc522.PCD_Authenticate(
  736.         MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid));
  737.     if (status != MFRC522::STATUS_OK) {
  738.         Serial.print(F("PCD_Authenticate() failed: "));
  739.         Serial.println(mfrc522.GetStatusCodeName(status));
  740.         mp3.playMp3FolderTrack(401);
  741.         return;
  742.     }
  743.  
  744.     // Write data to the block
  745.     Serial.print(F("Writing data into block "));
  746.     Serial.print(blockAddr);
  747.     Serial.println(F(" ..."));
  748.     dump_byte_array(buffer, 16);
  749.     Serial.println();
  750.     status = (MFRC522::StatusCode)mfrc522.MIFARE_Write(blockAddr, buffer, 16);
  751.     if (status != MFRC522::STATUS_OK) {
  752.         Serial.print(F("MIFARE_Write() failed: "));
  753.         Serial.println(mfrc522.GetStatusCodeName(status));
  754.         mp3.playMp3FolderTrack(401);
  755.     }
  756.     else
  757.         mp3.playMp3FolderTrack(400);
  758.     Serial.println();
  759.     delay(100);
  760. }
  761.  
  762. // adminCard Function
  763. //
  764. void adminCard(short adminFunc) {
  765.     int adminSubFunc;
  766.     int adminMode;
  767.     uint8_t oldMaxVolume;
  768.     uint8_t oldMinVolume;
  769.     uint8_t oldInitVolume;
  770.     uint8_t oldPlayMode;
  771.     oldMaxVolume = maxVolume;
  772.     oldMinVolume = minVolume;
  773.     oldInitVolume = initVolume;
  774.     oldPlayMode = playMode;
  775.     mp3.pause();
  776.     Serial.print("Administratorkarte aufgelegt");
  777.     if (adminFunc == 1) {
  778.         adminSubFunc = voiceMenu(5, 500, 500);
  779.         if (adminSubFunc == 1) {
  780.             Serial.print("Max Volume");
  781.             mp3.playMp3FolderTrack(511);
  782.             delay(2000);
  783.             mp3.playMp3FolderTrack(maxVolume);
  784.             delay(1000);
  785.             mp3.playMp3FolderTrack(900);
  786.             maxVolume = voiceMenuSpecial(10, 30, maxVolume);
  787.             minVolume = min(minVolume, maxVolume);
  788.             initVolume = min(initVolume, maxVolume);
  789.         }
  790.         else if (adminSubFunc == 2) {
  791.             Serial.print("Min Volume");
  792.             mp3.playMp3FolderTrack(512);
  793.             delay(2000);
  794.             mp3.playMp3FolderTrack(minVolume);
  795.             delay(1000);
  796.             mp3.playMp3FolderTrack(900);
  797.             minVolume = min(minVolume, maxVolume);
  798.             minVolume = voiceMenuSpecial(5, max(15, maxVolume), minVolume);
  799.             maxVolume = max(maxVolume, minVolume);
  800.             initVolume = max(initVolume, minVolume);
  801.         }
  802.         else if (adminSubFunc == 3) {
  803.             Serial.print("Init Volume");
  804.             mp3.playMp3FolderTrack(513);
  805.             delay(2000);
  806.             mp3.playMp3FolderTrack(initVolume);
  807.             delay(1000);
  808.             mp3.playMp3FolderTrack(900);
  809.             initVolume = min(initVolume, maxVolume);
  810.             initVolume = max(initVolume, minVolume);
  811.             initVolume = voiceMenuSpecial(minVolume, maxVolume, initVolume);
  812.             maxVolume = max(maxVolume, initVolume);
  813.             minVolume = min(minVolume, initVolume);
  814.         }
  815.         else if (adminSubFunc == 4) {
  816.             Serial.print("Modus auswählen");
  817.             adminMode = voiceMenu(5, 520, 520);
  818.             playMode = adminMode;
  819.         }
  820.         else if (adminSubFunc == 5) {
  821.             Serial.print("Administration abgebrochen");
  822.         }
  823.     }
  824.     if (adminSubFunc >= 1 && adminSubFunc <= 3) {
  825.         Serial.print(maxVolume);
  826.         Serial.println(F("=Max"));
  827.         Serial.print(minVolume);
  828.         Serial.println(F("=Min"));
  829.         Serial.print(initVolume);
  830.         Serial.println(F("=Init"));
  831.  
  832.         mp3.playMp3FolderTrack(511);
  833.         delay(2000);
  834.         mp3.playMp3FolderTrack(maxVolume);
  835.         delay(1000);
  836.         mp3.playMp3FolderTrack(900);
  837.         if (oldMaxVolume != maxVolume) {
  838.             EEPROM.write(101, maxVolume);
  839.         }
  840.         delay(1000);
  841.  
  842.         mp3.playMp3FolderTrack(512);
  843.         delay(2000);
  844.         mp3.playMp3FolderTrack(minVolume);
  845.         delay(1000);
  846.         mp3.playMp3FolderTrack(900);
  847.         if (oldMinVolume != minVolume) {
  848.             EEPROM.write(102, minVolume);
  849.         }
  850.         delay(1000);
  851.  
  852.         mp3.playMp3FolderTrack(513);
  853.         delay(2000);
  854.         mp3.playMp3FolderTrack(initVolume);
  855.         delay(1000);
  856.         mp3.playMp3FolderTrack(900);
  857.         if (oldInitVolume != initVolume) {
  858.             EEPROM.write(103, initVolume);
  859.         }
  860.     }
  861.  
  862.     if (adminSubFunc == 4) {
  863.         mp3.playMp3FolderTrack(514);
  864.         delay(2000);
  865.         if (playMode == 1) {
  866.             mp3.playMp3FolderTrack(521);
  867.         }
  868.         else if (playMode == 1) {
  869.             mp3.playMp3FolderTrack(522);
  870.         }
  871.         else if (playMode == 1) {
  872.             mp3.playMp3FolderTrack(523);
  873.         }
  874.         delay(1000);
  875.         mp3.playMp3FolderTrack(900);
  876.         if (oldPlayMode != playMode) {
  877.             EEPROM.write(104, playMode);
  878.         }
  879.     }
  880.  
  881.     mp3.playMp3FolderTrack(599);
  882.     delay(2000);
  883.     mp3.playMp3FolderTrack(900);
  884.     mp3.pause();
  885. }
  886.  
  887. /**
  888.  * Helper routine to dump a byte array as hex values to Serial.
  889.  */
  890. void dump_byte_array(byte *buffer, byte bufferSize) {
  891.     for (byte i = 0; i < bufferSize; i++) {
  892.         Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  893.         Serial.print(buffer[i], HEX);
  894.     }
  895. }
  896.  
  897. // playMp3File - Zahlen zusammensetzen
  898. //
  899. void playMp3File(short mp3Number) {
  900.     //mp3.playAdvertisement(mp3Number);
  901.     mp3.playMp3FolderTrack(mp3Number);
  902.     do {
  903.         delay(100);
  904.     } while (isPlaying());
  905. }
  906.  
  907. // Test Function
  908. //
  909. void testCard() {
  910.     Serial.println(F("Testkarte aufgelegt"));
  911.    
  912.     mp3.pause();
  913.  
  914.     playMp3File(7);
  915.     playMp3File(100);
  916.     playMp3File(7);
  917.     playMp3File(80);
  918.     playMp3File(400);
  919.     playMp3File(400);
  920.     playMp3File(8);
  921.     playMp3File(900);
  922.  
  923.     mp3.pause();
  924.  
  925.     Serial.println(F("Test Ende"));
  926. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement