Skip to content

Commit

Permalink
Merge pull request #137 from eadmaster/cc1101-support
Browse files Browse the repository at this point in the history
reworked RCSwitch_Read_Raw for CC1101 support, added subghz rx cmd (#66)
  • Loading branch information
pr3y authored Aug 7, 2024
2 parents 49aefa8 + 059c2d6 commit 6849a67
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,12 @@ bool processSerialCommand(String cmd_str) {

if(cmd_str.startsWith("rf") || cmd_str.startsWith("subghz" )) {

gsetRfTxPin(false);
//if(RfTx==0) RfTx=GROVE_SDA; // quick fix
pinMode(RfTx, OUTPUT);
//Serial.println(RfTx);

if(cmd_str.startsWith("subghz rx")) {
float frequency=433.92; // TODO: custom frequency passed as arg. valid ranges for cc1101: 300-348 MHZ, 387-464MHZ and 779-928MHZ.
//String frequency_arg = cmd_str.substring(strlen("subghz rx"), cmd_str.length());
RCSwitch_Read_Raw(frequency);
return true;
}
if(cmd_str.startsWith("subghz tx_from_file")) {
String filepath = cmd_str.substring(strlen("subghz tx_from_file "), cmd_str.length());
filepath.trim();
Expand All @@ -218,7 +219,6 @@ bool processSerialCommand(String cmd_str) {
// else file not found
return false;
}

/* TODO:
if(cmd_str.startsWith("subghz tx")) {
// flipperzero-like cmd https://docs.flipper.net/development/cli/#wLVht
Expand Down
3 changes: 3 additions & 0 deletions src/modules/others/mic.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifdef MIC_SPM1423

#include "mic.h"
#include "core/mykeyboard.h"
#include "core/powerSave.h"
Expand Down Expand Up @@ -317,3 +319,4 @@ void mic_test()
printf("Quit mic test\n");
}

#endif
3 changes: 3 additions & 0 deletions src/modules/others/mic.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifdef MIC_SPM1423

/**
* @file test.h
* @author Forairaaaaa
Expand All @@ -24,3 +26,4 @@ void DisplayMicro();
void new_mic_test();
void new_mic_test_fft();

#endif
75 changes: 55 additions & 20 deletions src/modules/rf/rf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength) {
return bin;
}

void RCSwitch_Read_Raw() {
void RCSwitch_Read_Raw(float frequency) {
RCSwitch rcswitch = RCSwitch();
RfCodes received;

Expand All @@ -344,18 +344,42 @@ void RCSwitch_Read_Raw() {
tft.setTextSize(FP);
tft.println("Waiting for signal.");
char hexString[64];

RestartRec:
pinMode(RfRx, INPUT);
rcswitch.enableReceive(RfRx);
// init receive
#ifdef USE_CC1101_VIA_SPI
// derived from https://github.com/LSatan/SmartRC-CC1101-Driver-Lib/blob/master/examples/Rc-Switch%20examples%20cc1101/ReceiveDemo_Advanced_cc1101/ReceiveDemo_Advanced_cc1101.ino
ELECHOUSE_cc1101.setSpiPin(CC1101_SCK_PIN, CC1101_MISO_PIN, CC1101_MOSI_PIN, CC1101_SS_PIN);
ELECHOUSE_cc1101.setGDO(CC1101_GDO0_PIN, CC1101_GDO2_PIN); //Set Gdo0 (tx) and Gdo2 (rx) for serial transmission function.
if (ELECHOUSE_cc1101.getCC1101()){ // Check the CC1101 Spi connection.
Serial.println("cc1101 Connection OK");
} else {
Serial.println("cc1101 Connection Error");
return;
}
ELECHOUSE_cc1101.Init();
ELECHOUSE_cc1101.setMHZ(frequency);
rcswitch.enableReceive(CC1101_GDO2_PIN);
ELECHOUSE_cc1101.SetRx();
#else
if(frequency!=433.92) {
Serial.println("unsupported frequency");
return;
}
gsetRfRxPin(false);
pinMode(RfRx, INPUT);
rcswitch.enableReceive(RfRx);
#endif

while(!checkEscPress()) {
if(rcswitch.available()) {
long value = rcswitch.getReceivedValue();
Serial.println("Available");
//Serial.println("Available");
if(value) {
Serial.println("has value");
//Serial.println("has value");

unsigned int* raw = rcswitch.getReceivedRawdata();
received.frequency=433920000;
received.frequency=int(frequency*1000000);
received.key=rcswitch.getReceivedValue();
received.protocol="RcSwitch";
received.preset=rcswitch.getReceivedProtocol();
Expand All @@ -367,13 +391,13 @@ void RCSwitch_Read_Raw() {
if(i>0) received.data+=" ";
received.data+=raw[i];
}
Serial.println(received.protocol);
Serial.println(received.data);
//Serial.println(received.protocol);
//Serial.println(received.data);
const char* b = dec2binWzerofill(received.key, received.Bit);
drawMainBorder();
tft.setCursor(10, 28);
tft.setTextSize(FP);
decimalToHexString(received.key,hexString);
decimalToHexString(received.key,hexString); // need to remove the extra padding 0s?
tft.println("Key: " + String(hexString));
tft.setCursor(10, tft.getCursorY());
tft.println("Binary: " + String(b));
Expand All @@ -390,6 +414,23 @@ void RCSwitch_Read_Raw() {
previousMillis = millis();
}
if(received.key>0) {
String subfile_out = "Filetype: Bruce SubGhz RAW File\nVersion 1\n";
subfile_out += "Frequency: " + String(int(frequency*1000000)) + "\n";
if(received.preset=="1") received.preset="FuriHalSubGhzPresetOok270Async";
else if (received.preset=="2") received.preset="FuriHalSubGhzPresetOok650Async";
subfile_out += "Preset: " + String(received.preset) + "\n";
subfile_out += "Protocol: RcSwitch\n";
subfile_out += "Bit: " + String(received.Bit) + "\n";
subfile_out += "Key: " + String(hexString) + "\n";
// subfile_out += "RAW_Data: " + received.data; // not in flipper pattern
subfile_out += "TE: " + String(received.te) + "\n";

#ifndef HAS_SCREEN
// headless mode, just print the file on serial and quit
Serial.println(subfile_out);
return;
#endif

if(checkSelPress()) {
int chosen=0;
options = {
Expand Down Expand Up @@ -422,15 +463,7 @@ void RCSwitch_Read_Raw() {
FS="LittleFS";
}
if(file) {
file.println("Filetype: Bruce SubGhz RAW File\nVersion 1\nFrequency: 433920000");
if(received.preset=="1") received.preset="FuriHalSubGhzPresetOok270Async";
else if (received.preset=="2") received.preset="FuriHalSubGhzPresetOok650Async";
file.println("Preset: " + String(received.preset));
file.println("Protocol: RcSwitch");
file.println("Bit: " + String(received.Bit));
file.println("Key: " + String(hexString));
//file.println("RAW_Data: " + received.data); // not in flipper pattern
file.println("TE: " + String(received.te));
file.println(subfile_out);
displaySuccess(FS + "/bruce_" + String(i) + ".sub");
} else {
Serial.println("Fail saving data to LittleFS");
Expand Down Expand Up @@ -525,7 +558,6 @@ void RCSwitch_RAW_send(int nTransmitterPin, int * ptrtransmittimings, struct Pro
} // end for
}


void sendRfCommand(struct RfCodes rfcode) {
uint32_t frequency = rfcode.frequency;
String protocol = rfcode.protocol;
Expand All @@ -535,6 +567,7 @@ void sendRfCommand(struct RfCodes rfcode) {
byte modulation = 2; // possible values for CC1101: 0 = 2-FSK, 1 =GFSK, 2=ASK, 3 = 4-FSK, 4 = MSK
float deviation = 0;
float rxBW = 0; // Receive bandwidth
float dataRate = 0; // Data Rate
/*
Serial.println("sendRawRfCommand");
Serial.println(data);
Expand Down Expand Up @@ -580,10 +613,12 @@ void sendRfCommand(struct RfCodes rfcode) {
else if(preset == "FuriHalSubGhzPresetMSK99_97KbAsync") {
modulation = 4;
deviation = 47.60742;
dataRate = 99.97;
}
else if(preset == "FuriHalSubGhzPresetGFSK9_99KbAsync") {
modulation = 1;
deviation = 19.042969;
dataRate = 9.996;
}
else if(preset == "1" || preset == "2" || preset == "3" || preset == "4" || preset == "5" || preset == "6" || preset == "7" || preset == "8" || preset == "9" || preset == "10" || preset == "11" || preset == "12"|| preset == "13" || preset == "14") {
rcswitch_protocol_no = preset.toInt();
Expand Down Expand Up @@ -630,10 +665,10 @@ void sendRfCommand(struct RfCodes rfcode) {

ELECHOUSE_cc1101.Init(); // must be set to initialize the cc1101!
ELECHOUSE_cc1101.setMHZ(frequency / 1000000.0); // e.g. 433.92. Here you can set your basic frequency. The lib calculates the frequency automatically (default = 433.92).The cc1101 can: 300-348 MHZ, 387-464MHZ and 779-928MHZ. Read More info from datasheet.
//ELECHOUSE_cc1101.setMHZ(433.92); // e.g. 433.92. Here you can set your basic frequency. The lib calculates the frequency automatically (default = 433.92).The cc1101 can: 300-348 MHZ, 387-464MHZ and 779-928MHZ. Read More info from datasheet.
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);
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();
#endif
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 @@ -18,7 +18,7 @@ void rf_jammerIntermittent();
void rf_jammerFull();
void otherRFcodes();
bool txSubFile(FS *fs, String filepath);
void RCSwitch_Read_Raw();
void RCSwitch_Read_Raw(float frequency=433.92);
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);

0 comments on commit 6849a67

Please sign in to comment.