| 1 | #include <Arduino.h> |
| 2 | #include <SPI.h> |
| 3 | #include <RF24.h> |
| 4 | #include <U8g2lib.h> |
| 5 | #include <WiFi.h> |
| 6 | #include "esp_bt.h" |
| 7 | |
| 8 | // ================= NRF24 ================= |
| 9 | #define CE1_PIN 10 |
| 10 | #define CSN1_PIN 11 |
| 11 | |
| 12 | #define CE2_PIN 12 |
| 13 | #define CSN2_PIN 13 |
| 14 | |
| 15 | #define NRF_SCK 18 |
| 16 | #define NRF_MISO 16 |
| 17 | #define NRF_MOSI 17 |
| 18 | |
| 19 | // ================= OLED ================== |
| 20 | #define OLED_SDA_PIN 8 |
| 21 | #define OLED_SCL_PIN 9 |
| 22 | |
| 23 | // ================= Buttons =============== |
| 24 | #define BTN_UP 4 |
| 25 | |
| 26 | // ========================================= |
| 27 | |
| 28 | // Third radio optional |
| 29 | #define CE3_PIN 46 |
| 30 | #define CSN3_PIN 48 |
| 31 | |
| 32 | SPIClass *NRF_SPI = nullptr; |
| 33 | |
| 34 | // RF24 radios |
| 35 | RF24 RadioA(CE1_PIN, CSN1_PIN); |
| 36 | RF24 RadioB(CE2_PIN, CSN2_PIN); |
| 37 | |
| 38 | // OLED |
| 39 | U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2( |
| 40 | U8G2_R0, |
| 41 | U8X8_PIN_NONE, |
| 42 | OLED_SCL_PIN, |
| 43 | OLED_SDA_PIN |
| 44 | ); |
| 45 | |
| 46 | enum OperationMode { |
| 47 | DEACTIVE_MODE, |
| 48 | BLE_MODULE, |
| 49 | Bluetooth_MODULE |
| 50 | }; |
| 51 | |
| 52 | OperationMode currentMode = DEACTIVE_MODE; |
| 53 | |
| 54 | // BLE advertising channels |
| 55 | const byte ble_channels[] = {2, 26, 80}; |
| 56 | |
| 57 | // Bluetooth hopping channels |
| 58 | //const byte bluetooth_channels[] = { |
| 59 | // 32, 34, 46, 48, 50, 52, |
| 60 | // 0, 1, 2, 4, 6, 8, |
| 61 | // 22, 24, 26, 28, 30, |
| 62 | // 74, 76, 78, 80 |
| 63 | //}; |
| 64 | |
| 65 | const byte bluetooth_channels[] = { |
| 66 | 12, 17, 22, 27, 32, |
| 67 | 37, 42, 47, 52, 57, |
| 68 | 62, 67, 72 |
| 69 | }; |
| 70 | |
| 71 | volatile bool modeChangeRequested = false; |
| 72 | |
| 73 | unsigned long lastButtonPressTime = 0; |
| 74 | const unsigned long debounceDelay = 500; |
| 75 | |
| 76 | // ========================================= |
| 77 | // NRF CONFIG |
| 78 | // ========================================= |
| 79 | |
| 80 | void configureNrf(RF24 &radio) { |
| 81 | |
| 82 | radio.begin(NRF_SPI); |
| 83 | |
| 84 | radio.setAutoAck(false); |
| 85 | radio.stopListening(); |
| 86 | |
| 87 | radio.setRetries(0, 0); |
| 88 | radio.setPALevel(RF24_PA_MAX); |
| 89 | radio.setDataRate(RF24_2MBPS); |
| 90 | |
| 91 | radio.disableCRC(); |
| 92 | } |
| 93 | |
| 94 | // ========================================= |
| 95 | |
| 96 | void IRAM_ATTR handleButtonPress() { |
| 97 | |
| 98 | unsigned long currentTime = millis(); |
| 99 | |
| 100 | if (currentTime - lastButtonPressTime > debounceDelay) { |
| 101 | modeChangeRequested = true; |
| 102 | lastButtonPressTime = currentTime; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // ========================================= |
| 107 | |
| 108 | void configureRadio(RF24 &radio) { |
| 109 | |
| 110 | configureNrf(radio); |
| 111 | |
| 112 | radio.startConstCarrier( |
| 113 | RF24_PA_MAX, |
| 114 | 40 |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | // ========================================= |
| 119 | |
| 120 | void initializeRadios() { |
| 121 | |
| 122 | if (currentMode == DEACTIVE_MODE) { |
| 123 | |
| 124 | RadioA.powerDown(); |
| 125 | RadioB.powerDown(); |
| 126 | |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | configureRadio(RadioA); |
| 131 | configureRadio(RadioB); |
| 132 | } |
| 133 | |
| 134 | // ========================================= |
| 135 | |
| 136 | void updateOLED() { |
| 137 | |
| 138 | u8g2.clearBuffer(); |
| 139 | |
| 140 | u8g2.setFont(u8g2_font_6x10_tr); |
| 141 | |
| 142 | u8g2.setCursor(0, 12); |
| 143 | u8g2.print("Mode:"); |
| 144 | |
| 145 | u8g2.setCursor(50, 12); |
| 146 | |
| 147 | if (currentMode == BLE_MODULE) { |
| 148 | u8g2.print("BLE"); |
| 149 | } |
| 150 | else if (currentMode == Bluetooth_MODULE) { |
| 151 | u8g2.print("BT"); |
| 152 | } |
| 153 | else { |
| 154 | u8g2.print("OFF"); |
| 155 | } |
| 156 | |
| 157 | u8g2.setCursor(0, 30); |
| 158 | u8g2.print("R1:"); |
| 159 | u8g2.print(RadioA.isChipConnected()); |
| 160 | |
| 161 | u8g2.setCursor(0, 45); |
| 162 | u8g2.print("R2:"); |
| 163 | u8g2.print(RadioB.isChipConnected()); |
| 164 | |
| 165 | u8g2.setCursor(0, 60); |
| 166 | u8g2.print("R3:"); |
| 167 | |
| 168 | u8g2.sendBuffer(); |
| 169 | } |
| 170 | |
| 171 | // ========================================= |
| 172 | |
| 173 | void checkModeChange() { |
| 174 | |
| 175 | if (!modeChangeRequested) |
| 176 | return; |
| 177 | |
| 178 | modeChangeRequested = false; |
| 179 | |
| 180 | currentMode = |
| 181 | static_cast<OperationMode>( |
| 182 | (currentMode + 1) % 3 |
| 183 | ); |
| 184 | |
| 185 | initializeRadios(); |
| 186 | |
| 187 | updateOLED(); |
| 188 | } |
| 189 | |
| 190 | // ========================================= |
| 191 | // SETUP |
| 192 | // ========================================= |
| 193 | |
| 194 | void setup() { |
| 195 | |
| 196 | Serial.begin(115200); |
| 197 | |
| 198 | // Disable ESP32 radios |
| 199 | esp_bt_controller_deinit(); |
| 200 | |
| 201 | WiFi.mode(WIFI_OFF); |
| 202 | btStop(); |
| 203 | |
| 204 | // SPI |
| 205 | NRF_SPI = new SPIClass(FSPI); |
| 206 | |
| 207 | NRF_SPI->begin( |
| 208 | NRF_SCK, |
| 209 | NRF_MISO, |
| 210 | NRF_MOSI |
| 211 | ); |
| 212 | |
| 213 | // OLED |
| 214 | u8g2.begin(); |
| 215 | |
| 216 | // Button |
| 217 | pinMode(BTN_UP, INPUT_PULLUP); |
| 218 | |
| 219 | attachInterrupt( |
| 220 | digitalPinToInterrupt(BTN_UP), |
| 221 | handleButtonPress, |
| 222 | FALLING |
| 223 | ); |
| 224 | |
| 225 | initializeRadios(); |
| 226 | |
| 227 | updateOLED(); |
| 228 | |
| 229 | Serial.println("Jammer initialized"); |
| 230 | } |
| 231 | |
| 232 | // ========================================= |
| 233 | // LOOP |
| 234 | // ========================================= |
| 235 | |
| 236 | void loop() { |
| 237 | |
| 238 | checkModeChange(); |
| 239 | |
| 240 | if (currentMode == BLE_MODULE) { |
| 241 | |
| 242 | byte channel = |
| 243 | ble_channels[ |
| 244 | random( |
| 245 | sizeof(ble_channels) |
| 246 | ) |
| 247 | ]; |
| 248 | |
| 249 | RadioA.setChannel(channel); |
| 250 | RadioB.setChannel(channel); |
| 251 | } |
| 252 | |
| 253 | else if (currentMode == Bluetooth_MODULE) { |
| 254 | |
| 255 | byte channel = |
| 256 | bluetooth_channels[ |
| 257 | random( |
| 258 | sizeof(bluetooth_channels) |
| 259 | ) |
| 260 | ]; |
| 261 | |
| 262 | RadioA.setChannel(channel); |
| 263 | RadioB.setChannel(channel); |
| 264 | } |
| 265 | |
| 266 | delay(5); |
| 267 | } |