Skip to content

Commit

Permalink
(Serial) Print to UI
Browse files Browse the repository at this point in the history
index.js:
- textarea 10 rows and append instead of replace

SysModFiles
- remove readFile

SysModPrint:
- print: implement No, Serial and UI
- printJson: use print

UserModHA, UserModInstance: use USER_PRINTF
  • Loading branch information
ewoudwijma committed Apr 13, 2024
1 parent 434d9fa commit 1005bf9
Show file tree
Hide file tree
Showing 10 changed files with 1,368 additions and 1,421 deletions.
4 changes: 4 additions & 0 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {
divNode.appendChild(cE("br"));

varNode = cE("textarea");
varNode.rows = 10;
varNode.readOnly = variable.ro;
varNode.addEventListener('dblclick', (event) => {toggleModal(event.target);});
}
Expand Down Expand Up @@ -1014,6 +1015,9 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
}
//You cannot set it to a client side disk file system path, due to security reasons.
}
} else if (node.className == "textarea") {
node.value += commandJson.value;
node.scrollTop = node.scrollHeight;
} else {//inputs and progress type
if (variable.ro && nodeType == "span") { //text and numbers read only
// console.log("changeHTML value span not select", variable, node, commandJson, rowNr);
Expand Down
18 changes: 1 addition & 17 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,4 @@ void SysModFiles::removeFiles(const char * filter, bool reverse) {
}

root.close();
}

bool SysModFiles::readFile(const char * path) {
File f = open(path, "r");
if (f) {

while(f.available()) {
Serial.print((char)f.read());
}
Serial.println();

f.close();
return true;
}
else
return false;
}
}
2 changes: 0 additions & 2 deletions src/Sys/SysModFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class SysModFiles: public SysModule {
//remove files meeting filter condition, if no filter, all, if reverse then all but filter
void removeFiles(const char * filter = nullptr, bool reverse = false);

bool readFile(const char * path);

};

extern SysModFiles *files;
101 changes: 35 additions & 66 deletions src/Sys/SysModPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,92 +62,61 @@ void SysModPrint::setup() {
ui->setLabel(var, "Output");

JsonArray options = ui->setOptions(var);
options.add("No 🚧");
options.add("No");
options.add("Serial");
options.add("UI 🚧");
options.add("UI");

web->clientsToJson(options, true); //ip only
return true;
}
default: return false;
}});

ui->initTextArea(parentVar, "log", "🚧", true, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case f_UIFun:
// ui->setComment(var, "Show the printed log");
return true;
default: return false;
}});
ui->initTextArea(parentVar, "log");
}

void SysModPrint::loop() {
// Module::loop();
if (!setupsDone) setupsDone = true;
}

size_t SysModPrint::print(const char * format, ...) {
void SysModPrint::print(const char * format, ...) {
va_list args;

va_start(args, format);


//tbd: print to UI (crashes because of recursive calls to print in setUIValueV...
// unsigned8 pOut = mdl->getValue("pOut");
// if (pOut == 2) {
// Serial.println(format);
// char value[1024];
// vsnprintf(value, sizeof(value)-1, format, args);
// mdl->setUIValueV("log", "%s", format);
// va_end(args);
// return 1;
// }

Serial.print(strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) == 0?"":"α"); //looptask λ/ asyncTCP task α

for (size_t i = 0; i < strlen(format); i++)
{
if (format[i] == '%')
{
switch (format[i+1])
{
case 's':
Serial.print(va_arg(args, const char *));
break;
case 'u':
Serial.print(va_arg(args, unsigned int));
break;
case 'c':
Serial.print(va_arg(args, int));
break;
case 'd':
Serial.print(va_arg(args, int));
break;
case 'f':
Serial.print(va_arg(args, double));
break;
case '%':
Serial.print("%"); // in case of %%
break;
default:
va_arg(args, int);
// logFile.print(x);
Serial.print(format[i]);
Serial.print(format[i+1]);
}
i++;
}
else
{
Serial.print(format[i]);
// Serial.print(strncmp(pcTaskGetTaskName(NULL), "loopTask", 8) == 0?"":"α"); //looptask λ/ asyncTCP task α

unsigned8 pOut = 1; //default serial
char value[1024];
vsnprintf(value, sizeof(value)-1, format, args);
if (mdls->isConnected) {
if (mdl->model)
pOut = mdl->getValue("pOut");

if (pOut == 1)
Serial.print(value);
else if (pOut == 2) {
JsonObject responseObject = web->getResponseObject();
if (responseObject["log"]["value"].isNull())
responseObject["log"]["value"] = value;
else
responseObject["log"]["value"] = responseObject["log"]["value"].as<String>() + String(value);
// web->addResponse("log", "value", JsonString(value, JsonString::Copied)); //setValue not necessary
// mdl->setUIValueV("log", "%s", value);
}
else if (pOut == 3) {
//tbd
}
}
else
Serial.print(value);

va_end(args);
return 1;
}

size_t SysModPrint::println(const __FlashStringHelper * x) {
return Serial.println(x);
void SysModPrint::println(const __FlashStringHelper * x) {
print("%s\n", x);
}

void SysModPrint::printVar(JsonObject var) {
Expand All @@ -158,11 +127,11 @@ void SysModPrint::printVar(JsonObject var) {
}
}

size_t SysModPrint::printJson(const char * text, JsonVariantConst source) {
print("%s ", text);
size_t size = serializeJson(source, Serial); //for the time being
Serial.println();
return size;
void SysModPrint::printJson(const char * text, JsonVariantConst source) {
char resStr[1024];
serializeJson(source, resStr, sizeof(resStr));

print("%s %s\n", text, resStr);
}

size_t SysModPrint::fFormat(char * buf, size_t size, const char * format, ...) {
Expand Down
6 changes: 3 additions & 3 deletions src/Sys/SysModPrint.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class SysModPrint:public SysModule {
void loop();

//generic print function (based on printf)
size_t print(const char * format, ...);
void print(const char * format, ...);

size_t println(const __FlashStringHelper * x);
void println(const __FlashStringHelper * x);

void printVar(JsonObject var);

size_t printJson(const char * text, JsonVariantConst source);
void printJson(const char * text, JsonVariantConst source);

size_t fFormat(char * buf, size_t size, const char * format, ...);

Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ JsonObject SysModUI::initVar(JsonObject parent, const char * id, const char * ty

//create new var
if (differentParents || var.isNull()) {
USER_PRINTF("initVar create new %s var: %s->%s\n", type, parentId, id);
USER_PRINTF("initVar create new %s var: %s->%s\n", type, parentId?parentId:"", id); //parentId not null otherwise crash
if (parent.isNull()) {
JsonArray vars = mdl->model->as<JsonArray>();
var = vars.add<JsonObject>();
Expand Down
2 changes: 1 addition & 1 deletion src/SysModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#pragma once

#define VERSION 2024040918 //update for each build. Time in GMT !!!
#define VERSION 2024041320 //update for each build. Time in GMT !!!

//conventional (works)
// #define unsigned8 uint8_t
Expand Down
13 changes: 3 additions & 10 deletions src/User/UserModHA.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,19 @@ class UserModHA:public SysModule {
};

void onStateCommand(bool state, HALight* sender) {
Serial.print("State: ");
Serial.println(state);
USER_PRINTF("State: %s\n", state?"true":"false");

sender->setState(state); // report state back to the Home Assistant
}

void onBrightnessCommand(unsigned8 brightness, HALight* sender) {
Serial.print("Brightness: ");
Serial.println(brightness);
USER_PRINTF("Brightness: %s\n", brightness);

sender->setBrightness(brightness); // report brightness back to the Home Assistant
}

void onRGBColorCommand(HALight::RGBColor color, HALight* sender) {
Serial.print("Red: ");
Serial.println(color.red);
Serial.print("Green: ");
Serial.println(color.green);
Serial.print("Blue: ");
Serial.println(color.blue);
USER_PRINTF("Red: %d Green: %d blue: %d\n", color.red, color.green, color.blue);

sender->setRGBColor(color); // report color back to the Home Assistant
}
Expand Down
7 changes: 2 additions & 5 deletions src/User/UserModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,11 @@ class UserModInstances:public SysModule {
int httpResponseCode = http.POST(postMessage);

if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
USER_PRINTF("HTTP Response code: %d %s\n", httpResponseCode, payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
USER_PRINTF("Error code: %d\n", httpResponseCode);
}
// Free resources
http.end();
Expand Down
Loading

0 comments on commit 1005bf9

Please sign in to comment.