Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for cc1101 raw send (#66) + more debug serial cmds #160

Merged
merged 7 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,8 @@ build_flags =
;Radio Frequency (one pin modules) pin setting
-DRF_TX_PINS='{ {"M5 RF433T", GROVE_SDA}, {"Groove W", GROVE_SCL}, {"GROVE Y", GROVE_SDA}}'
-DRF_RX_PINS='{ {"M5 RF433R", GROVE_SCL}, {"Groove W", GROVE_SCL}, {"GROVE Y", GROVE_SDA}}'
-DUSE_CC1101_VIA_SPI
; connection pins using microSD sniffer module https://www.sparkfun.com/products/9419 https://docs.m5stack.com/en/core/Cardputer
-DUSE_CC1101_VIA_SPI
;-DUSE_CC1101_VIA_SPI
-DCC1101_GDO0_PIN=GROVE_SDA
-DCC1101_SS_PIN=GROVE_SCL ; chip select
-DCC1101_MOSI_PIN=14
Expand Down
189 changes: 181 additions & 8 deletions src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//#include <string>
#include "cJSON.h"
#include <inttypes.h> // for PRIu64

#include <Wire.h>

#include "sd_functions.h"
#include "settings.h"
Expand Down Expand Up @@ -47,6 +47,29 @@ void startSerialCommandsHandlerTask() {
}


bool is_free_gpio_pin(int pin_no ){
// check if pin_no is usable for general GPIO
std::vector<int> usable_pins = {GROVE_SDA, GROVE_SCL};

#if defined(STICK_C_PLUS2) || defined(STICK_C_PLUS)
usable_pins.insert(usable_pins.end(), { 25, 26, 32, 33, 0 });
#elif defined(ESP32S3DEVKITC1)
usable_pins.insert(usable_pins.end(), {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // GPIO1 to GPIO25
33, // GPIO33
38, 39, 40, 41, 42, 43, 44, // GPIO38 to GPIO44
47, 48 // GPIO47 to GPIO48
});
#endif

for (int usable_pin : usable_pins)
if (pin_no == usable_pin )
return true;
// else
return false;
}


void SerialPrintHexString(uint64_t val) {
char s[18] = {0};
//snprintf(s, 10, "%x", val);
Expand Down Expand Up @@ -229,18 +252,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 445533 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 Expand Up @@ -424,11 +447,161 @@ bool processSerialCommand(String cmd_str) {
//esp_timer_stop(screensaver_timer);
return true;
}

// gpio cmds https://docs.flipper.net/development/cli/#aqA4b
if(cmd_str.startsWith("gpio mode ")) {
const char* args = cmd_str.c_str() + strlen("gpio mode ");
int pin_number=-1;
int mode=0;
if (sscanf(args, "%d %d", &pin_number, &mode) == 2) {
// check usable pins according to the env
if(mode>=0 && mode<=1 && is_free_gpio_pin(pin_number)) {
pinMode(pin_number, mode);
return true;
}
}
// else
Serial.print("invalid args: ");
Serial.println(args);
return false;
}
if(cmd_str.startsWith("gpio set ")) {
const char* args = cmd_str.c_str() + strlen("gpio set ");
int pin_number=-1;
int value=0;
if (sscanf(args, "%d %d", &pin_number, &value) == 2) {
// check usable pins according to the env
if(value>=0 && value<=1 && is_free_gpio_pin(pin_number)) {
digitalWrite(pin_number, value);
return true;
}
}
// else
Serial.print("invalid args: ");
Serial.println(args);
return false;
}
if(cmd_str.startsWith("gpio read ")) {
const char* args = cmd_str.c_str() + strlen("gpio read ");
int pin_number=-1;
if (sscanf(args, "%d", &pin_number) == 1) {
// check usable pins according to the env
if(is_free_gpio_pin(pin_number)) {
Serial.println(digitalRead(pin_number));
return true;
}
}
// else
Serial.print("invalid args: ");
Serial.println(args);
return false;
}

// TODO: "storage" cmd to manage files https://docs.flipper.net/development/cli/#Xgais

// TODO: "gpio" cmds https://docs.flipper.net/development/cli/#aqA4b

if(cmd_str == "factory_reset") {
// remove config file and recreate
if(SD.exists(CONFIG_FILE)) SD.remove(CONFIG_FILE);
if(LittleFS.exists(CONFIG_FILE)) LittleFS.remove(CONFIG_FILE);
// TODO: need to reset EEPROM too?
getConfigs(); // recreate config file if it does not exists
return true;
}

if(cmd_str == "settings") {
// view current settings
JsonObject setting = settings[0];
serializeJsonPretty(settings, Serial);
Serial.println("");
return true;
}
if(cmd_str == "info device" || cmd_str == "!") {
Serial.print("Bruce v");
Serial.println(BRUCE_VERSION);
// https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ChipID/GetChipID/GetChipID.ino
Serial.printf("Chip is %s (revision v%d)\n", ESP.getChipModel(), ESP.getChipRevision());
Serial.printf("Detected flash size: %d\n", ESP.getFlashChipSize());
//Serial.printf("This chip has %d cores\n", ESP.getChipCores());
//Serial.printf("CPU Freq is %d\n", ESP.getCpuFreqMHz());
// Features: WiFi, BLE, Embedded Flash 8MB (GD)
// Crystal is 40MHz
// MAC: 24:58:7c:5b:24:5c
return true;
}

if(cmd_str == "free") {
// report free memory
Serial.print("Total heap: ");
Serial.println(ESP.getHeapSize());
Serial.print("Free heap: ");
Serial.println(ESP.getFreeHeap());
if(psramFound()) {
Serial.print("Total PSRAM: ");
Serial.println(ESP.getPsramSize());
Serial.print("Free PSRAM: ");
Serial.println(ESP.getFreePsram());
}
return true;
}

if(cmd_str == "i2c") {
// scan for connected i2c modules
// derived from https://learn.adafruit.com/scanning-i2c-addresses/arduino
Wire.begin(GROVE_SDA, GROVE_SCL);
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
} // end for
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
return false;
} else {
Serial.println("done\n");
return true;
}
}

/* WIP
// "storage" cmd to manage files https://docs.flipper.net/development/cli/#Xgais
if(cmd_str.startsWith("storage read ")) {
String txt = "";
String filepath = cmd_str.substring(strlen("storage read "), cmd_str.length());
filepath.trim();
if(!filepath.startsWith("/")) filepath = "/" + filepath; // add "/" if missing
if(SD.exists(filepath)) txt = readSmallFile(SD, filepath);;
if(LittleFS.exists(filepath)) txt = readSmallFile(LittleFS, filepath);
if(txt.length()!=0) {
Serial.println(txt);
return true;
} else return false;
}*/

// TODO: date
// TODO: uptime
// TODO: help

// TODO: more commands https://docs.flipper.net/development/cli#0Z9fs

Serial.println("unsupported serial command: " + cmd_str);
Expand Down
5 changes: 3 additions & 2 deletions src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,12 @@ 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) {
#ifdef USE_CC1101_VIA_SPI
#ifdef USE_CC1101_VIA_SPI
ELECHOUSE_cc1101.Init();
if (ELECHOUSE_cc1101.getCC1101()){
RfModule=1;
EEPROM.write(13, RfModule); //set the byte
Expand Down
Loading
Loading