Skip to content

Commit

Permalink
NRF24 last build
Browse files Browse the repository at this point in the history
Changed from RPD watching (too noisy, not reliable) to data availability on channel.

Added disclaimer message on Information
  • Loading branch information
bmorcelli committed Oct 3, 2024
1 parent 422559a commit 3f66059
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
4 changes: 3 additions & 1 deletion src/core/menu_items/NRF24.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#include "NRF24.h"
#include "core/display.h"
#include "modules/NRF24/nrf_common.h"
#include "modules/NRF24/nrf_jammer.h"
#include "modules/NRF24/nrf_spectrum.h"

void NRF24Menu::optionsMenu() {
options.clear();
options.push_back({"Jammer 2.4G", [=]() { nrf_jammer(); }});
options.push_back({"Information", [=]() { nrf_info(); }});
#if defined(STICK_C_PLUS) || defined(STICK_C_PLUS2)
options.push_back({"Spectrum", [=]() { nrf_spectrum(&CC_NRF_SPI); }});
#elif defined(CARDPUTER) || defined(ESP32S3DEVKITC1)
options.push_back({"Spectrum", [=]() { nrf_spectrum(&sdcardSPI); }});
#else
options.push_back({"Spectrum", [=]() { nrf_spectrum(&SPI); }});
#endif
options.push_back({"Jammer 2.4G", [=]() { nrf_jammer(); }});

options.push_back({"Main Menu", [=]() { backToMenu(); }});
delay(200);
Expand Down
22 changes: 22 additions & 0 deletions src/modules/NRF24/nrf_common.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
#include "nrf_common.h"
#include "../../core/mykeyboard.h"

RF24 NRFradio(NRF24_CE_PIN, NRF24_SS_PIN);

void nrf_info() {
tft.fillScreen(BGCOLOR);
tft.setTextSize(FM);
tft.setTextColor(TFT_RED, BGCOLOR);
tft.drawCentreString("_Disclaimer_",WIDTH/2,10,1);
tft.setTextColor(TFT_WHITE, BGCOLOR);
tft.setTextSize(FP);
tft.setCursor(15,33);
tft.println("These functions were made to be used in a controlled environment for STUDY only.");
tft.println("\nDO NOT use these functions to harm people or companies, you can go to jail!");
tft.setTextColor(FGCOLOR, BGCOLOR);
tft.println("\nThis device is VERY sensible to noise, so long wires or passing near VCC line can make things go wrong.");
delay(1000);
while(!checkAnyKeyPress());
}

bool nrf_start() {
#if defined(USE_NRF24_VIA_SPI)
pinMode(NRF24_SS_PIN, OUTPUT);
digitalWrite(NRF24_SS_PIN, HIGH);
pinMode(NRF24_CE_PIN, OUTPUT);
digitalWrite(NRF24_CE_PIN, LOW);

#if defined(STICK_C_PLUS) || defined(STICK_C_PLUS2)
CC_NRF_SPI.begin(NRF24_SCK_PIN,NRF24_MISO_PIN,NRF24_MOSI_PIN,NRF24_SS_PIN);
#elif defined(CARDPUTER) || defined(ESP32S3DEVKITC1)
Expand Down
4 changes: 3 additions & 1 deletion src/modules/NRF24/nrf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@

extern RF24 NRFradio;

bool nrf_start();
bool nrf_start();

void nrf_info();
51 changes: 12 additions & 39 deletions src/modules/NRF24/nrf_spectrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
#include "../../core/mykeyboard.h"


// nRF24L01P Registers
#define _NRF24_CONFIG 0x00
#define _NRF24_EN_AA 0x01
#define _NRF24_RF_CH 0x05
#define _NRF24_RF_SETUP 0x06
#define _NRF24_RPD 0x09
#define CHANNELS 80
#define RGB565(r, g, b) ((((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)))
uint8_t channel[CHANNELS];
Expand All @@ -30,51 +24,29 @@ inline void setRegister(SPIClass &SSPI, byte r, byte v) {
digitalWrite(NRF24_SS_PIN, HIGH);
}

inline void powerUp(SPIClass &SSPI) {
byte v = getRegister(SSPI,_NRF24_CONFIG) | 0x02;
delayMicroseconds(20);
setRegister(SSPI,_NRF24_CONFIG, v);
delayMicroseconds(130);
}

inline void powerDown(SPIClass &SSPI) {
byte v = getRegister(SSPI,_NRF24_CONFIG) & ~0x02;
delayMicroseconds(20);
setRegister(SSPI,_NRF24_CONFIG, v);
}

inline void enable_nrf() {
digitalWrite(NRF24_CE_PIN, HIGH);
}

inline void disable_nrf() {
digitalWrite(NRF24_CE_PIN, LOW);
}

inline void setRX(SPIClass &SSPI) {
byte v = getRegister(SSPI,_NRF24_CONFIG) | 0x01;
delayMicroseconds(20);
setRegister(SSPI,_NRF24_CONFIG, v);
enable_nrf();
delayMicroseconds(100);
setRegister(SSPI,0x00, getRegister(SSPI,0x00) & ~0x02);
}

// Scanning Channels
#define _BW WIDTH/CHANNELS
String scanChannels(SPIClass* SSPI, bool web) {
String result="{";
disable_nrf();
digitalWrite(NRF24_CE_PIN, LOW);
for (int i = 0; i < CHANNELS; i++) {
setRegister(*SSPI,_NRF24_RF_CH,i); //(126 * i) / CHANNELS);
setRX(*SSPI);
delayMicroseconds(80);
disable_nrf();
int rpd = getRegister(*SSPI,_NRF24_RPD) * 200;
NRFradio.setChannel(i);
NRFradio.startListening();
delayMicroseconds(128);
NRFradio.stopListening();
int rpd=0;
if(NRFradio.testCarrier()) rpd=200;
channel[i] = (channel[i] * 3 + rpd) / 4;

int level = (channel[i] > 125) ? 125 : channel[i]; // Clamp values

tft.drawFastVLine(i*_BW, 0, 125, (i % 8) ? TFT_BLACK : RGB565(25, 25, 25));
tft.drawFastVLine(i*_BW, HEIGHT-(10+level), level, (i % 2 == 0) ? FGCOLOR : TFT_DARKGREY); // Use green for even indices
tft.drawFastVLine(i*_BW, 0, HEIGHT-(9+level), (i % 8) ? TFT_BLACK : RGB565(25, 25, 25));
tft.drawFastVLine(i*_BW, 0, rpd ? 2 : 0, TFT_DARKGREY);
if(web) {
if(i>0) result+=",";
Expand All @@ -92,6 +64,7 @@ void nrf_spectrum(SPIClass* SSPI) {
tft.drawString("2.40Ghz",0,HEIGHT-LH);
tft.drawCentreString("2.44Ghz", WIDTH/2,HEIGHT-LH,1);
tft.drawRightString("2.48Ghz",WIDTH,HEIGHT-LH,1);
memset(channel,0,CHANNELS);

if(nrf_start()) {
NRFradio.setAutoAck(false);
Expand All @@ -103,7 +76,7 @@ void nrf_spectrum(SPIClass* SSPI) {
}
NRFradio.setDataRate(RF24_1MBPS);

while(!checkNextPress()) {
while(!checkEscPress()) {
scanChannels(SSPI);
}
NRFradio.stopListening();
Expand Down

0 comments on commit 3f66059

Please sign in to comment.