Skip to content

Commit

Permalink
added support for cc1101 raw send (pr3y#66), cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmaster committed Aug 10, 2024
1 parent 7edef22 commit 6bf4592
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,18 @@ bool processSerialCommand(String cmd_str) {
// flipperzero-like cmd https://docs.flipper.net/development/cli/#wLVht
// e.g. subghz tx 0000000000200001 868250000 403 10 // https://forum.flipper.net/t/friedland-libra-48249sl-wireless-doorbell-request/4528/20
// {hex_key} {frequency} {te} {count}
// subghz tx 000000000044553C 433920000 174 10
// subghz tx 0000000000445533 433920000 174 10
const char* args = cmd_str.c_str() + strlen("subghz tx");
uint64_t key=0;
unsigned long frequency=433920000;
unsigned int te=0;
unsigned int count=10;
if(strlen(args)<=1) return false;
if(sscanf(args, " %llx %lu %u %u", &key, &frequency, &te, &count)<=0) return false; // missing 1 req arg
unsigned int bits=24; // TODO: compute from key
if(!initRfModule("tx", float(frequency/1000000.0))) return false; // check valid frequency and init the rf module
unsigned int bits=64; // TODO: compute from key
//RCSwitch_send( hexStringToDecimal(txt.c_str()) , bits, pulse, protocol, repeat);
RCSwitch_send( key, bits, te, 1, count );
deinitRfModule();
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void setRFModuleMenu() {
*/
};
delay(200);
loopOptions(options); // TODO: pre-select current value of RfModule
loopOptions(options, idx); // 2fix: idx highlight not working?
delay(200);
EEPROM.begin(EEPROMSIZE); // open eeprom
if(result == 1) {
Expand Down
116 changes: 78 additions & 38 deletions src/modules/rf/rf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void rf_spectrum() { //@IncursioHack - https://github.com/IncursioHack ----thank
tft.fillScreen(TFT_BLACK);
tft.setTextSize(1);
tft.println("");
tft.println(" RF433 - Spectrum");
tft.println(" RF - Spectrum");
pinMode(RfRx, INPUT);
initRMT();

Expand Down Expand Up @@ -128,15 +128,25 @@ void rf_spectrum() { //@IncursioHack - https://github.com/IncursioHack ----thank


void rf_jammerFull() { //@IncursioHack - https://github.com/IncursioHack - thanks @EversonPereira - rfcardputer
pinMode(RfTx, OUTPUT);
// init rf module
int nTransmitterPin = RfTx;
if(!initRfModule("tx")) return;
if(RfModule == 1) { // CC1101 in use
#ifdef USE_CC1101_VIA_SPI
nTransmitterPin = CC1101_GDO0_PIN;
#else
return;
#endif
}

tft.fillScreen(TFT_BLACK);
tft.println("");
tft.println(" RF433 - Jammer Full");
tft.println(" RF - Jammer Full");
tft.println("");
tft.println("");
tft.setTextSize(2);
sendRF = true;
digitalWrite(RfTx, HIGH); // Turn on Jammer
digitalWrite(nTransmitterPin, HIGH); // Turn on Jammer
int tmr0=millis(); // control total jammer time;
tft.println("Sending... Press ESC to stop.");
while (sendRF) {
Expand All @@ -146,15 +156,24 @@ void rf_jammerFull() { //@IncursioHack - https://github.com/IncursioHack - than
break;
}
}
digitalWrite(RfTx, LOW); // Turn Jammer OFF
deinitRfModule(); // Turn Jammer OFF
}


void rf_jammerIntermittent() { //@IncursioHack - https://github.com/IncursioHack - thanks @EversonPereira - rfcardputer
pinMode(RfTx, OUTPUT);
int nTransmitterPin = RfTx;
if(!initRfModule("tx")) return;
if(RfModule == 1) { // CC1101 in use
#ifdef USE_CC1101_VIA_SPI
nTransmitterPin = CC1101_GDO0_PIN;
#else
return;
#endif
}

tft.fillScreen(TFT_BLACK);
tft.println("");
tft.println(" RF433 - Jammer Intermittent");
tft.println(" RF - Jammer Intermittent");
tft.println("");
tft.println("");
tft.setTextSize(2);
Expand All @@ -170,13 +189,13 @@ void rf_jammerIntermittent() { //@IncursioHack - https://github.com/IncursioHack
returnToMenu=true;
break;
}
digitalWrite(RfTx, HIGH); // Ativa o pino
digitalWrite(nTransmitterPin, HIGH); // Ativa o pino
// keeps the pin active for a while and increase increase
for (int widthsize = 1; widthsize <= (1 + sequence); widthsize++) {
delayMicroseconds(50);
}

digitalWrite(RfTx, LOW); // Desativa o pino
digitalWrite(nTransmitterPin, LOW); // Desativa o pino
// keeps the pin inactive for the same time as before
for (int widthsize = 1; widthsize <= (1 + sequence); widthsize++) {
delayMicroseconds(50);
Expand All @@ -185,7 +204,7 @@ void rf_jammerIntermittent() { //@IncursioHack - https://github.com/IncursioHack
}
}

digitalWrite(RfTx, LOW); // Deactivate pin
deinitRfModule();
}


Expand All @@ -197,10 +216,7 @@ void RCSwitch_send(uint64_t data, unsigned int bits, int pulse, int protocol, in

if(RfModule==1) {
#ifdef USE_CC1101_VIA_SPI
pinMode(CC1101_GDO0_PIN, OUTPUT);
mySwitch.enableTransmit(CC1101_GDO0_PIN);
ELECHOUSE_cc1101.setPA(12); // set TxPower. The following settings are possible depending on the frequency band. (-30 -20 -15 -10 -6 0 5 7 10 11 12) Default is max!
ELECHOUSE_cc1101.SetTx();
#else
Serial.println("USE_CC1101_VIA_SPI not defined");
return; // not enabled for this board
Expand All @@ -220,7 +236,7 @@ void RCSwitch_send(uint64_t data, unsigned int bits, int pulse, int protocol, in
Serial.println(pulse);
Serial.println(protocol);
Serial.println(repeat);
* */
*/

mySwitch.disableTransmit();

Expand Down Expand Up @@ -390,6 +406,9 @@ void deinitRfModule() {

bool initRfModule(String mode, float frequency) {

// use default frequency if no one is passed
if(!frequency) frequency = RfFreq;

if(RfModule == 1) { // CC1101 in use
#ifdef USE_CC1101_VIA_SPI
if (ELECHOUSE_cc1101.getCC1101()){ // Check the CC1101 Spi connection.
Expand All @@ -404,22 +423,24 @@ bool initRfModule(String mode, float frequency) {
// make sure it is in idle state when changing frequency and other parameters
// "If any frequency programming register is altered when the frequency synthesizer is running, the synthesizer may give an undesired response. Hence, the frequency programming should only be updated when the radio is in the IDLE state." https://github.com/LSatan/SmartRC-CC1101-Driver-Lib/issues/65
ELECHOUSE_cc1101.setSidle();

// use default frequency if no one is passed
if(!frequency) frequency = RfFreq;


if(!(frequency>=300 && frequency<=928)) // TODO: check all supported subranges: 300-348 MHZ, 387-464MHZ and 779-928MHZ.
return false;
// else
ELECHOUSE_cc1101.setMHZ(frequency);

/* MEMO: cannot change other params after this is executed -> moved in the caller func
if(mode=="tx")
/* MEMO: cannot change other params after this is executed */
if(mode=="tx") {
pinMode(CC1101_GDO0_PIN, OUTPUT);
ELECHOUSE_cc1101.setPA(12); // set TxPower. The following settings are possible depending
ELECHOUSE_cc1101.SetTx();
else
}
else if(mode=="rx") {
pinMode(CC1101_GDO0_PIN, INPUT);
ELECHOUSE_cc1101.SetRx();
*/
}
// else if mode is unspecified wont start TX/RX mode here -> done by the caller

#else
// TODO: PCA9554-based implmentation
return false;
Expand All @@ -437,7 +458,8 @@ bool initRfModule(String mode, float frequency) {
//if(RfTx==0) RfTx=GROVE_SDA; // quick fix
pinMode(RfTx, OUTPUT);
digitalWrite(RfTx, LED_OFF);
} else {
}
else if(mode=="rx") {
// Rx Mode
gsetRfRxPin(false);
//if(RfRx==0) RfRx=GROVE_SCL; // quick fix
Expand Down Expand Up @@ -469,10 +491,8 @@ bool RCSwitch_Read_Raw(float frequency) {
#ifdef CC1101_GDO2_PIN
rcswitch.enableReceive(CC1101_GDO2_PIN);
#else
pinMode(CC1101_GDO0_PIN, INPUT);
rcswitch.enableReceive(CC1101_GDO0_PIN);
#endif
ELECHOUSE_cc1101.SetRx();
#else
return false;
#endif
Expand Down Expand Up @@ -601,9 +621,16 @@ bool RCSwitch_Read_Raw(float frequency) {


// ported from https://github.com/sui77/rc-switch/blob/3a536a172ab752f3c7a58d831c5075ca24fd920b/RCSwitch.cpp
void RCSwitch_RAW_Bit_send(int nTransmitterPin, RfCodes data) {
if (nTransmitterPin == -1)
return;
void RCSwitch_RAW_Bit_send(RfCodes data) {
int nTransmitterPin = RfTx;
if(RfModule==1) {
#ifdef USE_CC1101_VIA_SPI
nTransmitterPin = CC1101_GDO0_PIN;
#else
return;
#endif
}

if (data.data == "")
return;
bool currentlogiclevel = false;
Expand All @@ -626,19 +653,27 @@ void RCSwitch_RAW_Bit_send(int nTransmitterPin, RfCodes data) {
digitalWrite(nTransmitterPin, currentlogiclevel ? HIGH : LOW);
delayMicroseconds(data.te);

Serial.print(currentBit);
Serial.print("=");
Serial.println(currentlogiclevel);
//Serial.print(currentBit);
//Serial.print("=");
//Serial.println(currentlogiclevel);

currentBit--;
}
digitalWrite(nTransmitterPin, LOW);
}
}
void RCSwitch_RAW_send(int nTransmitterPin, int * ptrtransmittimings) {
if (nTransmitterPin == -1)
return;


void RCSwitch_RAW_send(int * ptrtransmittimings) {
int nTransmitterPin = RfTx;
if(RfModule==1) {
#ifdef USE_CC1101_VIA_SPI
nTransmitterPin = CC1101_GDO0_PIN;
#else
return;
#endif
}

if (!ptrtransmittimings)
return;

Expand Down Expand Up @@ -744,15 +779,19 @@ void sendRfCommand(struct RfCodes rfcode) {
}

// init transmitter
if(!initRfModule("tx", frequency/1000000.0)) return;
if(!initRfModule("", frequency/1000000.0)) return;
if(RfModule == 1) { // CC1101 in use
#ifdef USE_CC1101_VIA_SPI
// derived from https://github.com/LSatan/SmartRC-CC1101-Driver-Lib/blob/master/examples/Rc-Switch%20examples%20cc1101/SendDemo_cc1101/SendDemo_cc1101.ino
ELECHOUSE_cc1101.setModulation(modulation);
if(deviation) ELECHOUSE_cc1101.setDeviation(deviation);
if(rxBW) ELECHOUSE_cc1101.setRxBW(rxBW); // Set the Receive Bandwidth in kHz. Value from 58.03 to 812.50. Default is 812.50 kHz.
if(dataRate) ELECHOUSE_cc1101.setDRate(dataRate);
pinMode(CC1101_GDO0_PIN, OUTPUT);
ELECHOUSE_cc1101.setPA(12); // set TxPower. The following settings are possible depending on the frequency band. (-30 -20 -15 -10 -6 0 5 7 10 11 12) Default is max!
ELECHOUSE_cc1101.SetTx();
#else
Serial.println("USE_CC1101_VIA_SPI not defined");
return;
#endif
} else {
Expand All @@ -762,6 +801,7 @@ void sendRfCommand(struct RfCodes rfcode) {
Serial.println(modulation);
return;
}
initRfModule("tx", frequency/1000000.0);
}

if(protocol == "RAW") {
Expand Down Expand Up @@ -792,14 +832,14 @@ void sendRfCommand(struct RfCodes rfcode) {

// send rf command
displayRedStripe("Sending..",TFT_WHITE,FGCOLOR);
RCSwitch_RAW_send(RfTx, transmittimings);
RCSwitch_RAW_send(transmittimings);
free(transmittimings);
}
else if (protocol == "BinRAW") {
rfcode.data = hexStrToBinStr(rfcode.data); // transform from "00 01 02 ... FF" into "00000000 00000001 00000010 .... 11111111"
Serial.println(rfcode.data);
//Serial.println(rfcode.data);
rfcode.data.trim();
RCSwitch_RAW_Bit_send(RfTx,rfcode);
RCSwitch_RAW_Bit_send(rfcode);
}

else if(protocol == "RcSwitch") {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/rf/rf.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ bool RCSwitch_Read_Raw(float frequency=0);
void RCSwitch_send(uint64_t data, unsigned int bits, int pulse=0, int protocol=1, int repeat=10);
void addToRecentCodes(struct RfCodes rfcode);
void sendRfCommand(struct RfCodes rfcode);
bool initRfModule(String mode, float frequency=0);
bool initRfModule(String mode="", float frequency=0);
void initCC1101once();
void deinitRfModule();

0 comments on commit 6bf4592

Please sign in to comment.