diff --git a/src/rf.cpp b/src/rf.cpp index abeba7b2..366df9ae 100644 --- a/src/rf.cpp +++ b/src/rf.cpp @@ -155,14 +155,40 @@ struct RfCodes { String protocol = ""; String preset = ""; String data = ""; + int te = 0; String filepath = ""; }; #include +void RCSwitch_send(uint64_t data, unsigned int bits, int pulse, int protocol, int repeat) +{ + RCSwitch mySwitch = RCSwitch(); + mySwitch.enableTransmit(RfTx); + mySwitch.setProtocol(protocol); + if (pulse) { mySwitch.setPulseLength(pulse); } + mySwitch.setPulseLength(pulse); + mySwitch.setRepeatTransmit(repeat); + mySwitch.send(data, bits); +} + +struct HighLow { + uint8_t high; // 1 + uint8_t low; //31 +}; + +struct Protocol { + uint16_t pulseLength; // base pulse length in microseconds, e.g. 350 + HighLow syncFactor; + HighLow zero; + HighLow one; + bool invertedSignal; +}; + // ported from https://github.com/sui77/rc-switch/blob/3a536a172ab752f3c7a58d831c5075ca24fd920b/RCSwitch.cpp -void RCSwitch_RAW_send(int nTransmitterPin, int * ptrtransmittimings, int nRepeatTransmit=1) { + +void RCSwitch_RAW_send(int nTransmitterPin, int * ptrtransmittimings, struct Protocol protocol) { if (nTransmitterPin == -1) return; @@ -170,19 +196,39 @@ void RCSwitch_RAW_send(int nTransmitterPin, int * ptrtransmittimings, int nRepea return; bool currentlogiclevel = true; + int nRepeatTransmit = 1; + //HighLow pulses ; for (int nRepeat = 0; nRepeat < nRepeatTransmit; nRepeat++) { unsigned int currenttiming = 0; - while( ptrtransmittimings[currenttiming] && currenttiming < RCSWITCH_MAX_CHANGES ) { + while( ptrtransmittimings[currenttiming] ) { // && currenttiming < RCSWITCH_MAX_CHANGES if(ptrtransmittimings[currenttiming] >= 0) { currentlogiclevel = true; + //pulses = protocol.one; } else { // negative value currentlogiclevel = false; ptrtransmittimings[currenttiming] = (-1) * ptrtransmittimings[currenttiming]; // invert sign + //pulses = protocol.zero; } + digitalWrite(nTransmitterPin, currentlogiclevel ? HIGH : LOW); delayMicroseconds( ptrtransmittimings[currenttiming] ); + + /* + uint8_t firstLogicLevel = (protocol.invertedSignal) ? LOW : HIGH; + uint8_t secondLogicLevel = (protocol.invertedSignal) ? HIGH : LOW; + + digitalWrite(nTransmitterPin, firstLogicLevel); + delayMicroseconds( protocol.pulseLength * pulses.high); + digitalWrite(nTransmitterPin, secondLogicLevel); + delayMicroseconds( protocol.pulseLength * pulses.low); + * */ + + Serial.print(ptrtransmittimings[currenttiming]); + Serial.print("="); + Serial.println(currentlogiclevel); + currenttiming++; } digitalWrite(nTransmitterPin, LOW); @@ -190,7 +236,7 @@ void RCSwitch_RAW_send(int nTransmitterPin, int * ptrtransmittimings, int nRepea } -void sendRawRfCommand(struct RfCodes rfcode) { +void sendRfCommand(struct RfCodes rfcode) { uint32_t frequency = rfcode.frequency; String protocol = rfcode.protocol; String preset = rfcode.preset; @@ -210,8 +256,19 @@ void sendRawRfCommand(struct RfCodes rfcode) { // MEMO: frequency is fixed with some transmitters https://github.com/sui77/rc-switch/issues/256 // TODO: add frequency switching via CC1101 https://github.com/LSatan/SmartRC-CC1101-Driver-Lib - if(! preset.startsWith("FuriHalSubGhzPresetOok")) { - // only ASK/OOK is supported + // Radio preset name (configures modulation, bandwidth, filters, etc.). + struct Protocol rcswitch_protocol; + int rcswitch_protocol_no = 1; + if(preset == "FuriHalSubGhzPresetOok270Async") { + rcswitch_protocol_no = 1; + // pulseLength , syncFactor , zero , one, invertedSignal + rcswitch_protocol = { 350, { 1, 31 }, { 1, 3 }, { 3, 1 }, false }; + } + else if(preset == "FuriHalSubGhzPresetOok650Async") { + rcswitch_protocol_no = 2; + rcswitch_protocol = { 650, { 1, 10 }, { 1, 2 }, { 2, 1 }, false }; + } + else { Serial.print("unsupported preset: "); Serial.println(preset); return; @@ -227,59 +284,62 @@ void sendRawRfCommand(struct RfCodes rfcode) { FuriHalSubGhzPresetCustom, //Custom Preset */ - if(protocol != "RAW") { - Serial.print("unsupported protocol: "); - Serial.println(protocol); - return; - } - + // init output pin digitalWrite(RfTx, LED_OFF); - if(RfTx==0) RfTx=GROVE_SDA; // quick fix pinMode(RfTx, OUTPUT); - //Serial.println(RfTx); - //RCSwitch mySwitch = RCSwitch(); - //mySwitch.enableTransmit(RfTx); - - //mySwitch.setProtocol(1); - //mySwitch.setPulseLength(pulse); - //mySwitch.setRepeatTransmit(10); - - // alloc buffer for transmittimings - int* transmittimings = (int *) calloc(sizeof(int), data.length()); // should be smaller the data.length() - size_t transmittimings_idx = 0; - - // split data into words, convert to int, and store them in transmittimings - String curr_word = ""; - int curr_val = 0; - for(int i=0; i // for PRIu64 -#include + #include #include #include "sd_functions.h" #include "settings.h" #include "display.h" +#include "rf.h" void SerialPrintHexString(uint64_t val) { @@ -142,10 +143,7 @@ void handleSerialCommands() { if(RfTx==0) RfTx=GROVE_SDA; // quick fix pinMode(RfTx, OUTPUT); //Serial.println(RfTx); - - RCSwitch mySwitch = RCSwitch(); - mySwitch.enableTransmit(RfTx); - + /* WIP: if(cmd_str.startsWith("subghz tx")) { // flipperzero-like cmd https://docs.flipper.net/development/cli/#wLVht @@ -182,6 +180,7 @@ void handleSerialCommands() { dataStr = dataItem->valuestring; } else { Serial.println("missing or invalid data to send"); + cJSON_Delete(root); return; } //String dataStr = cmd_str.substring(36, 36+8); @@ -190,12 +189,7 @@ void handleSerialCommands() { //SerialPrintHexString(data); //Serial.println(bits); - mySwitch.setProtocol(protocol); - if (pulse) { mySwitch.setPulseLength(pulse); } - mySwitch.setPulseLength(pulse); - mySwitch.setRepeatTransmit(repeat); - - mySwitch.send(data, bits); + RCSwitch_send(data, bits, pulse, protocol, repeat); cJSON_Delete(root); return; @@ -203,6 +197,7 @@ void handleSerialCommands() { } // endof rf if(cmd_str.startsWith("music_player " ) || cmd_str.startsWith("ttf" ) ) { + // TODO: move in audio.cpp module AudioOutputI2S *audioout = new AudioOutputI2S(); // https://github.com/earlephilhower/ESP8266Audio/blob/master/src/AudioOutputI2S.cpp#L32 #ifdef CARDPUTER audioout->SetPinout(41, 43, 42); @@ -312,6 +307,12 @@ void handleSerialCommands() { } return; } + + if(cmd_str == "clock" ) { + esp_timer_stop(screensaver_timer); // disable screensaver while the clock is running + runClockLoop(); + return; + } Serial.println("unsupported serial command: " + cmd_str);