Skip to content

Commit

Permalink
added support for more IR protocols, IR RAW mode fixes (pr3y#216), ad…
Browse files Browse the repository at this point in the history
…ded dialogChoice() js function
  • Loading branch information
eadmaster committed Sep 23, 2024
1 parent c541c45 commit 03497d0
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 78 deletions.
6 changes: 6 additions & 0 deletions src/core/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void displayError(String txt) {
return;
#endif
displayRedStripe(txt);
while(!checkAnyKeyPress()) delay(100);
}

void displayWarning(String txt) {
Expand All @@ -101,22 +102,27 @@ void displayWarning(String txt) {
return;
#endif
displayRedStripe(txt, TFT_BLACK,TFT_YELLOW);
while(!checkAnyKeyPress()) delay(100);
}

void displayInfo(String txt) {
#ifndef HAS_SCREEN
Serial.println("INFO: " + txt);
return;
#endif
// todo: add newlines to txt if too long
displayRedStripe(txt, TFT_WHITE, TFT_BLUE);
while(!checkAnyKeyPress()) delay(100);
}

void displaySuccess(String txt) {
#ifndef HAS_SCREEN
Serial.println("SUCCESS: " + txt);
return;
#endif
// todo: add newlines to txt if too long
displayRedStripe(txt, TFT_WHITE, TFT_DARKGREEN);
while(!checkAnyKeyPress()) delay(100);
}

void setPadCursor(int16_t padx, int16_t pady) {
Expand Down
12 changes: 5 additions & 7 deletions src/core/sd_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ bool copyToFs(FS from, FS to, String path) {
int prog=0;

if(&to==&LittleFS && (LittleFS.totalBytes() - LittleFS.usedBytes()) < tot) {
Serial.println("Not enaugh space on LittleFS for this file");
displayError("Not enaugh space");
delay(3000);
displayError("Not enought space");
return false;
}
//tft.drawRect(5,HEIGHT-12, (WIDTH-10), 9, FGCOLOR);
Expand Down Expand Up @@ -303,11 +301,11 @@ String readSmallFile(FS &fs, String filepath) {
if (!file) return "";

size_t fileSize = file.size();
if(fileSize > SAFE_STACK_BUFFER_SIZE) {
if(fileSize > SAFE_STACK_BUFFER_SIZE || fileSize > ESP.getFreeHeap()) {
displayError("File is too big");
Serial.println("File is too big");
return "";
}
// TODO: if(psramFound()) -> use PSRAM instead

fileContent = file.readString();

Expand Down Expand Up @@ -616,6 +614,7 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
if(filepath.endsWith(".bjs") || filepath.endsWith(".js")) options.insert(options.begin(), {"JS Script Run", [&]() {
delay(200);
run_bjs_script_headless(fs, filepath);
exit=true;
}});
#if defined(USB_as_HID)
if(filepath.endsWith(".txt")) {
Expand All @@ -626,7 +625,7 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
}});
options.push_back({"USB HID Type", [&]() {
String t = readSmallFile(fs, filepath);
displayInfo("Typing");
displayRedStripe("Typing");
key_input_from_string(t);
}});
}
Expand Down Expand Up @@ -875,7 +874,6 @@ void viewFile(FS fs, String filepath) {
bool checkLittleFsSize() {
if((LittleFS.totalBytes() - LittleFS.usedBytes()) < 4096) {
displayError("LittleFS is Full");
delay(2000);
return false;
} else return true;
}
Expand Down
21 changes: 11 additions & 10 deletions src/core/serialcmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,6 @@ bool processSerialCommand(String cmd_str) {
// TODO: rewrite using ArduinoJson parser?
// TODO: decode "data" into "address, command" and use existing "send*Command" funcs

//IRsend irsend(IrTx); //inverted = false
//Serial.println(IrTx);
IRsend irsend(IrTx,true); // Set the GPIO to be used to sending the message.
//IRsend irsend(IrTx); //inverted = false
irsend.begin();
cJSON *root = cJSON_Parse(cmd_str.c_str() + 6);
if (root == NULL) {
Serial.println("This is NOT json format");
Expand All @@ -304,7 +299,12 @@ bool processSerialCommand(String cmd_str) {
cJSON * dataItem = cJSON_GetObjectItem(root, "data");
cJSON * bitsItem = cJSON_GetObjectItem(root,"bits");

if(protocolItem && cJSON_IsString(protocolItem)) protocolStr = protocolItem->valuestring;
if(protocolItem && cJSON_IsString(protocolItem)) {
protocolStr = protocolItem->valuestring;
} else {
Serial.println("missing or invalid protocol to send");
return false;
}
if(bitsItem && cJSON_IsNumber(bitsItem)) bits = bitsItem->valueint;
if(dataItem && cJSON_IsString(dataItem)) {
dataStr = dataItem->valuestring;
Expand All @@ -320,14 +320,15 @@ bool processSerialCommand(String cmd_str) {
//Serial.println(protocolItem->valuestring);

cJSON_Delete(root);

if(protocolStr == "nec"){
/*if(protocolStr == "nec"){
// sendNEC(uint64_t data, uint16_t nbits, uint16_t repeat)
irsend.sendNEC(data, bits, 10);
return true;
}
// TODO: more protocols
return false;
*/

return sendDecodedCommand(protocolStr, dataStr, String(bits));
}

// turn off the led
Expand Down
64 changes: 57 additions & 7 deletions src/modules/bjs_interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ static duk_ret_t native_getBattery(duk_context *ctx) {
return 1;
}

/* 2FIX: not working
// terminate the script
static duk_ret_t native_exit(duk_context *ctx) {
duk_error(ctx, DUK_ERR_ERROR, "Script exited");
interpreter_start=false;
return 0;
}

*/

static duk_ret_t native_getBoard(duk_context *ctx) {
String board = "Undefined";
Expand Down Expand Up @@ -644,14 +645,12 @@ static duk_ret_t native_subghzSetFrequency(duk_context *ctx) {
static duk_ret_t native_dialogMessage(duk_context *ctx) {
// usage: dialogMessage(msg : string)
displayInfo(String(duk_to_string(ctx, 0)));
while(!checkAnyKeyPress()) delay(100);
return 0;
}

static duk_ret_t native_dialogError(duk_context *ctx) {
// usage: dialogError(msg : string)
displayError(String(duk_to_string(ctx, 0)));
while(!checkAnyKeyPress()) delay(100);
return 0;
}

Expand All @@ -675,6 +674,56 @@ static duk_ret_t native_dialogPickFile(duk_context *ctx) {
return 1;
}

static duk_ret_t native_dialogChoice(duk_context *ctx) {
// usage: dialogChoice(choices : string[])
// returns: string (val1, 2, ...), or empty string if cancelled
const char* r = "";

if (duk_is_array(ctx, 0)) {
options = {
{"Cancel", [&]() { r = ""; }},
};

// Get the length of the array
duk_uint_t len = duk_get_length(ctx, 0);
for (duk_uint_t i = 0; i < len; i++) {
// Get each element in the array
duk_get_prop_index(ctx, 0, i);

// Ensure it's a string
if (!duk_is_string(ctx, -1)) {
duk_pop(ctx);
duk_error(ctx, DUK_ERR_TYPE_ERROR, "Choice array elements must be strings.");
}

// Get the string
const char *choiceKey = duk_get_string(ctx, -1);
duk_pop(ctx);
i++;
duk_get_prop_index(ctx, 0, i);

// Ensure it's a string
if (!duk_is_string(ctx, -1)) {
duk_pop(ctx);
duk_error(ctx, DUK_ERR_TYPE_ERROR, "Choice array elements must be strings.");
}

// Get the string
const char *choiceValue = duk_get_string(ctx, -1);
duk_pop(ctx);

// add to the choices list
options.push_back({choiceKey, [&]() { r = choiceValue; }});
} // end for

delay(200);
loopOptions(options);
}

duk_push_string(ctx, r);
return 1;
}

static duk_ret_t native_dialogViewFile(duk_context *ctx) {
// usage: dialogViewFile(path : string)
// returns: nothing
Expand Down Expand Up @@ -800,8 +849,8 @@ bool interpreter() {
duk_put_global_string(ctx, "digitalWrite");
duk_push_c_function(ctx, native_pinMode, 2);
duk_put_global_string(ctx, "pinMode");
duk_push_c_function(ctx, native_exit, 0);
duk_put_global_string(ctx, "exit");
//duk_push_c_function(ctx, native_exit, 0);
//duk_put_global_string(ctx, "exit");

// Get Informations from the board
duk_push_c_function(ctx, native_getBattery, 0);
Expand Down Expand Up @@ -917,10 +966,11 @@ bool interpreter() {
duk_put_global_string(ctx, "dialogMessage");
duk_push_c_function(ctx, native_dialogError, 1);
duk_put_global_string(ctx, "dialogError");
// TODO: dialogYesNo()
duk_push_c_function(ctx, native_dialogPickFile, 1);
duk_put_global_string(ctx, "dialogPickFile");
// TODO: dialogChoice(choices: array)
// TODO: dialogYesNo()
duk_push_c_function(ctx, native_dialogChoice, 1);
duk_put_global_string(ctx, "dialogChoice");
duk_push_c_function(ctx, native_dialogViewFile, 1);
duk_put_global_string(ctx, "dialogViewFile");
duk_push_c_function(ctx, native_keyboard, 3);
Expand Down
Loading

0 comments on commit 03497d0

Please sign in to comment.