|
| 1 | +#include <M5Stack.h> |
| 2 | +#include <M5ez.h> |
| 3 | +#include <ezTime.h> |
| 4 | +#include <Wire.h> |
| 5 | + |
| 6 | +#include "MFRC522_I2C.h" |
| 7 | +#include "images.h" |
| 8 | + |
| 9 | +#define MAIN_DECLARED |
| 10 | + |
| 11 | +// 0x28 is i2c address on SDA. Check your address with i2cscanner if not match. |
| 12 | +MFRC522 mfrc522(0x28); // Create MFRC522 instance. |
| 13 | + |
| 14 | +void setup() { |
| 15 | +#include <themes/default.h> |
| 16 | +#include <themes/dark.h> |
| 17 | + |
| 18 | + // RFID init |
| 19 | + Serial.begin(115200); // Initialize serial communications with the PC |
| 20 | + Wire.begin(); // Initialize I2C |
| 21 | + mfrc522.PCD_Init(); // Init MFRC522 |
| 22 | + |
| 23 | + M5.Speaker.setVolume(1); |
| 24 | + M5.Speaker.update(); |
| 25 | + |
| 26 | + ezt::setDebug(INFO); |
| 27 | + ez.begin(); |
| 28 | +} |
| 29 | + |
| 30 | +void loop() { |
| 31 | + ezMenu mainmenu("Spawnrider M5H@ck"); |
| 32 | + mainmenu.txtSmall(); |
| 33 | + mainmenu.addItem("RFID Tools", menu_rfid); |
| 34 | + mainmenu.addItem("WIFI Tools", menu_not_implemented); |
| 35 | + mainmenu.addItem("Built-in wifi & other settings", ez.settings.menu); |
| 36 | + mainmenu.addItem("M5H@ck settings", menu_m5hack); |
| 37 | + mainmenu.upOnFirst("last|up"); |
| 38 | + mainmenu.downOnLast("first|down"); |
| 39 | + mainmenu.run(); |
| 40 | +} |
| 41 | + |
| 42 | +void menu_rfid() { |
| 43 | + ezMenu rfidmenu("RFID Tools"); |
| 44 | + rfidmenu.txtSmall(); |
| 45 | + rfidmenu.addItem("Get UID", rfid_tools); |
| 46 | + rfidmenu.addItem("Reader infos", show_reader_infos); |
| 47 | + rfidmenu.addItem("Back"); |
| 48 | + rfidmenu.upOnFirst("last|up"); |
| 49 | + rfidmenu.downOnLast("first|down"); |
| 50 | + rfidmenu.run(); |
| 51 | +} |
| 52 | + |
| 53 | +void rfid_tools() { |
| 54 | + ez.screen.clear(); |
| 55 | + |
| 56 | + char cardUid[15]; |
| 57 | + int i = 0; |
| 58 | + do { |
| 59 | + if(i > 10) return; |
| 60 | + M5.Lcd.println("Put the NFC Card on the reader..."); |
| 61 | + // Look for new cards, and select one if present |
| 62 | + delay(500); |
| 63 | + i++; |
| 64 | + } while (! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial()); |
| 65 | + |
| 66 | + // Now a card is selected. The UID and SAK is in mfrc522.uid. |
| 67 | + //M5.Speaker.beep(); |
| 68 | + |
| 69 | + // Dump UID |
| 70 | + Serial.print(F("Card UID:")); |
| 71 | + for (byte i = 0; i < mfrc522.uid.size; i++) { |
| 72 | + Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); |
| 73 | + Serial.print(mfrc522.uid.uidByte[i], HEX); |
| 74 | + sprintf(&cardUid[i * 2], "%02X", mfrc522.uid.uidByte[i]); |
| 75 | + //M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); |
| 76 | + //M5.Lcd.print(mfrc522.uid.uidByte[i], HEX); |
| 77 | + } |
| 78 | + Serial.println(); |
| 79 | + |
| 80 | + ez.msgBox("Card UID", cardUid); |
| 81 | +} |
| 82 | + |
| 83 | +void show_reader_infos() { |
| 84 | + // Get the MFRC522 software version |
| 85 | + byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg); |
| 86 | + Serial.print(F("MFRC522 Software Version: 0x")); |
| 87 | + Serial.print(v, HEX); |
| 88 | + if (v == 0x91) { |
| 89 | + Serial.print(F(" = v1.0")); |
| 90 | + ez.msgBox("MFRC522 Software Version", "Version v1.0"); |
| 91 | + } else if (v == 0x92){ |
| 92 | + Serial.print(F(" = v2.0")); |
| 93 | + ez.msgBox("MFRC522 Software Version", "Version v2.0"); |
| 94 | + } else { |
| 95 | + Serial.print(F(" (unknown)")); |
| 96 | + ez.msgBox("MFRC522 Software Version", "Unknown version"); |
| 97 | + } |
| 98 | + Serial.println(""); |
| 99 | + // When 0x00 or 0xFF is returned, communication probably failed |
| 100 | + if ((v == 0x00) || (v == 0xFF)) { |
| 101 | + Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?")); |
| 102 | + ez.msgBox("MFRC522 Software Version", "Communication failure, is the MFRC522 properly connected?"); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +void menu_not_implemented() { |
| 107 | + ez.msgBox("Not implemented", "To be implemented"); |
| 108 | +} |
| 109 | + |
| 110 | +void menu_m5hack() { |
| 111 | + ezMenu systemenu("Spawnrider M5H@ck"); |
| 112 | + systemenu.txtSmall(); |
| 113 | + systemenu.addItem("System Information", sysInfo); |
| 114 | + systemenu.addItem("WiFi Settings", ez.wifi.menu); |
| 115 | + systemenu.addItem("About M5H@ck", aboutM5Hack); |
| 116 | + systemenu.addItem("Updates via https", mainmenu_ota); |
| 117 | + systemenu.addItem("Power Off", powerOff); |
| 118 | + systemenu.addItem("Back"); |
| 119 | + systemenu.upOnFirst("last|up"); |
| 120 | + systemenu.downOnLast("first|down"); |
| 121 | + systemenu.run(); |
| 122 | +} |
| 123 | + |
| 124 | +void mainmenu_ota() { |
| 125 | + if (ez.msgBox("Get OTA_https", "This will replace the current program with a new version from GitHub.", "Cancel#OK#") == "OK") { |
| 126 | + ezProgressBar progress_bar("OTA update in progress", "Downloading ...", "Abort"); |
| 127 | +#include "raw_githubusercontent_com.h" // the root certificate is now in const char * root_cert |
| 128 | + ESP.restart(); |
| 129 | + if (ez.wifi.update("https://raw.githubusercontent.com/ropg/M5ez/master/compiled_binaries/OTA_https.bin", root_cert, &progress_bar)) { |
| 130 | + ez.msgBox("Over The Air updater", "OTA download successful. Reboot to new firmware", "Reboot"); |
| 131 | + ESP.restart(); |
| 132 | + } else { |
| 133 | + ez.msgBox("OTA error", ez.wifi.updateError(), "OK"); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +void powerOff() { |
| 139 | + m5.powerOFF(); |
| 140 | +} |
| 141 | + |
| 142 | +void aboutM5Hack() { |
| 143 | + ez.msgBox("About M5H@ck", "M5H@ck was written by | Yohann Ciurlik | | https://spawnrider.net"); |
| 144 | +} |
0 commit comments