Skip to content

Commit

Permalink
read decoded ir signals (pr3y#216), serial fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmaster committed Sep 1, 2024
1 parent c11c591 commit ca5e2c8
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 60 deletions.
36 changes: 36 additions & 0 deletions scripts/bruce_run_from_buffer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# args checking
if [ -z "$1" ] || [ ! -f "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "usage: $(basename $0) INPUT_FILE"
echo
exit 0
fi


INPUTFILE="$1"
INPUTFILEEXT=$( echo -n "$INPUTFILE" | rev | cut -d'.' -f1 | rev )
INPUTFILEEXT=$( echo -n $INPUTFILEEXT | tr '[A-Z]' '[a-z]' ) # force lowercase extension
SERIAL_CMD=""

# try to detect the input file type according on its extension (useful if file is missing or buggy)
case $INPUTFILEEXT in
sub ) SERIAL_CMD="subghz tx_from_buffer" ;;
ir ) SERIAL_CMD="ir tx_from_buffer" ;;
txt ) SERIAL_CMD="badusb run_from_buffer" ;;
js|bjs ) SERIAL_CMD="js run_from_buffer" ;;
esac

if [ -z "$SERIAL_CMD" ]; then
echo "$(basename $0) err: unsupported file format: $INPUTFILEEXT"
exit 1
fi

echo "$SERIAL_CMD" | busybox microcom -s 115200 /dev/ttyACM0 -t 1000

sleep 1

cat "$INPUTFILE" | busybox microcom -s 115200 /dev/ttyACM0 -t 1000

echo "EOF" | busybox microcom -s 115200 /dev/ttyACM0
#echo -e '\x04' | busybox microcom -s 115200 /dev/ttyACM0
4 changes: 2 additions & 2 deletions src/core/mykeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool menuPress(int bot) {
bool checkNextPress(){
#if defined (CARDPUTER)
Keyboard.update();
if(Keyboard.isKeyPressed('.'))
if(Keyboard.isKeyPressed('/') || Keyboard.isKeyPressed('.'))
#elif defined(CORE2) || defined(CORE)
M5.update();
if(M5.BtnC.isPressed())
Expand Down Expand Up @@ -108,7 +108,7 @@ bool checkPrevPress() {
if(axp192.GetBtnPress())
#elif defined(CARDPUTER)
Keyboard.update();
if(Keyboard.isKeyPressed(';'))
if(Keyboard.isKeyPressed(',') || Keyboard.isKeyPressed(';'))
#elif defined(CORE2) || defined(CORE)
M5.update();
if(M5.BtnA.isPressed())
Expand Down
91 changes: 60 additions & 31 deletions src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ bool setupPsramFs() {
return true;
}


String readSmallFileFromSerial() {
String buf = "";
String curr_line = "";
Serial.flush();
while (true) {
if (!Serial.available()) {
delay(500);
Serial.flush();
continue;
}
curr_line = Serial.readStringUntil('\n');
Expand All @@ -67,6 +70,21 @@ String readSmallFileFromSerial() {
}
return buf;
}
/*
String readSmallFileFromSerialAlt() {
String buf = "";
Serial.flush();
while (true) {
if (!Serial.available()) {
delay(500);
Serial.flush();
continue;
}
buf = Serial.readStringUntil(4); // ascii code 4 = EOT (End of Transmit)
return buf;
}
}
* */


/* task to handle serial commands, currently used in headless mode only */
Expand Down Expand Up @@ -134,27 +152,12 @@ void handleSerialCommands() {

String cmd_str;

/*
if (Serial.available() >= MIN_CMD_LEN ) {
size_t len = Serial.available();
char sbuf[len] = {0};
Serial.readBytes(sbuf, len);
Serial.print("received:");
Serial.println(sbuf);
//log_d(sbuf);
cmd_str = String(sbuf);
} else {
//Serial.println("nothing received");
//log_d("nothing received");
if (Serial.available() >= 1) {
cmd_str = Serial.readStringUntil('\n');
} else {
// try again on next iteration
return;
}*/

if (Serial.available() >= 1) {
cmd_str = Serial.readStringUntil('\n');
} else {
// try again on next iteration
return;
}
}

bool r = processSerialCommand(cmd_str);
if(r) setup_gpio(); // temp fix for menu inf. loop
Expand Down Expand Up @@ -199,16 +202,17 @@ bool processSerialCommand(String cmd_str) {
// switch on cmd_str
if(cmd_str.startsWith("ir") ) {

if(cmd_str == "ir rx") { // "ir rx raw"
IrRead i = IrRead(true); // true == headless mode
String r = i.loop_headless(10); // wait for 10 seconds
if(cmd_str.startsWith("ir rx")) {
IrRead i = IrRead(true); // true -> headless mode
String r = "";
if(cmd_str == "ir rx") r = i.loop_headless(10, false); // wait for 10 seconds, false -> try to decode
if(cmd_str == "ir rx raw") r = i.loop_headless(10, true); // true -> raw mode
if(r.length()==0) return false;
// else
Serial.println(r);
return true;
}
//TODO: if(cmd_str == "ir rx" {


if(cmd_str.startsWith("ir tx")) {
// make sure it is initted
gsetIrTxPin(false);
Expand Down Expand Up @@ -241,7 +245,6 @@ bool processSerialCommand(String cmd_str) {
}
//if(cmd_str.startsWith("ir tx sirc")){
//if(cmd_str.startsWith("ir tx samsung")){

//if(cmd_str.startsWith("ir tx raw")){

if(cmd_str.startsWith("ir tx_from_file ")){
Expand All @@ -255,7 +258,20 @@ bool processSerialCommand(String cmd_str) {
// else file not found
return false;
}


if(cmd_str.startsWith("ir tx_from_buffer")){
if(!(setupPsramFs())) return false;
String txt = readSmallFileFromSerial();
String tmpfilepath = "/tmpramfile"; // TODO: random name?
File f = PSRamFS.open(tmpfilepath, FILE_WRITE);
if(!f) return false;
f.write((const uint8_t*) txt.c_str(), txt.length());
f.close();
bool r = txIrFile(&PSRamFS, tmpfilepath);
PSRamFS.remove(tmpfilepath); // TODO: keep cached?
return r;
}

if(cmd_str.startsWith("irsend")) {
// tasmota json command https://tasmota.github.io/docs/Tasmota-IR/#sending-ir-commands
// e.g. IRSend {"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF"}
Expand Down Expand Up @@ -421,8 +437,8 @@ bool processSerialCommand(String cmd_str) {

#if defined(USB_as_HID)
// badusb available
if(cmd_str.startsWith("badusb tx_from_file ")) {
String filepath = cmd_str.substring(strlen("badusb tx_from_file "));
if(cmd_str.startsWith("badusb run_from_file ")) {
String filepath = cmd_str.substring(strlen("badusb run_from_file "));
filepath.trim();
if(filepath.indexOf(".txt") == -1) return false; // invalid filename
if(!filepath.startsWith("/")) filepath = "/" + filepath; // add "/" if missing
Expand All @@ -435,7 +451,7 @@ bool processSerialCommand(String cmd_str) {
key_input(*fs, filepath);
return true;
}
if(cmd_str == "badusb tx_from_buffer") {
if(cmd_str == "badusb run_from_buffer") {
if(!(setupPsramFs())) return false;
String txt = readSmallFileFromSerial();
String tmpfilepath = "/tmpramfile"; // TODO: random name?
Expand Down Expand Up @@ -1001,7 +1017,20 @@ bool processSerialCommand(String cmd_str) {
fileToCopy="";
return false;
}


if(cmd_str.startsWith("js run_from_buffer")){
if(!(setupPsramFs())) return false;
String txt = readSmallFileFromSerial();
String tmpfilepath = "/tmpramfile";
File f = PSRamFS.open(tmpfilepath, FILE_WRITE);
if(!f) return false;
f.write((const uint8_t*) txt.c_str(), txt.length());
f.close();
bool r = run_bjs_script_headless(PSRamFS, tmpfilepath);
PSRamFS.remove(tmpfilepath);
return r;
}

if(cmd_str.startsWith("js ")) {
String filepath = cmd_str.substring(strlen("js "));
filepath.trim();
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ void startup_sound() {
** Where the devices are started and variables set
*********************************************************************/
void setup() {
Serial.setRxBufferSize(SAFE_STACK_BUFFER_SIZE); // Must be invoked before Serial.begin(). Default is 256 chars
Serial.begin(115200);

log_d("Total heap: %d", ESP.getHeapSize());
Expand Down Expand Up @@ -381,7 +382,10 @@ void loop() {
delay(200);
}

#if !defined(LITE_VERSION)
handleSerialCommands();
#endif

#ifdef CARDPUTER
checkShortcutPress(); // shortctus to quickly start apps without navigating the menus
#endif
Expand Down
Loading

0 comments on commit ca5e2c8

Please sign in to comment.