Skip to content

Commit

Permalink
Support vars with vectors of strings, use in files, bug fix files shown
Browse files Browse the repository at this point in the history
index.js
- fileEdit: not disabled of readOnly

pio.ini
- ASMParser from 1.1.0 to v2.5

SysModFiles
- remove FileDetails struct and fileList
- replace fileList by vectors (init parameters)
- use initText2 for vector of chars
- set flEdit to readonly (bug fix for too many rows in files)
- filesChanged: repopulate vectors and update model

SysModUI:
- add initTextVector and initVarAndValueVector and initFileEditVector (temp)

SysModule
- add VectorString
  • Loading branch information
ewoudwijma committed Sep 17, 2024
1 parent 95dbbad commit de7eb90
Show file tree
Hide file tree
Showing 8 changed files with 1,191 additions and 1,163 deletions.
2 changes: 1 addition & 1 deletion data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ function createHTML(json, parentNode = null, rowNr = UINT8_MAX) {

varNode = cE("input");
varNode.type = "button";
varNode.disabled = variable.ro;
// varNode.disabled = variable.ro; //fileEdit should always be possible
varNode.value = "🔍"; //initial label, button.value is the label shown on the button
varNode.addEventListener('click', (event) => {
let url = `http://${window.location.hostname}/file/`;
Expand Down
4 changes: 1 addition & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ lib_deps =
build_flags =
-D STARBASE_USERMOD_LIVE
lib_deps =
; https://github.com/hpwit/ASMParser.git#4b2da1a ; mem branche commit of 20240716
; https://github.com/hpwit/ASMParser.git#v2.1.1 ;
https://github.com/hpwit/ASMParser.git#21186c0 ; this is release 1.1.0 of 20240808 but #1.1.0 gives error
https://github.com/hpwit/ASMParser.git#v2.5 ;0.0.1+sha.d1fed6d 20240904

[STARBASE]
build_flags =
Expand Down
60 changes: 29 additions & 31 deletions src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ void SysModFiles::setup() {
ui->setComment(var, "List of files");
return true;
case onAdd:
rowNr = fileList.size();
rowNr = fileNames.size();
web->getResponseObject()["onAdd"]["rowNr"] = rowNr;
//add a row with all defaults
//tbd: File upload does not call onAdd (bug?)
return true;
case onDelete:
if (rowNr != UINT8_MAX && rowNr < fileList.size()) {
const char * fileName = fileList[rowNr].name;
if (rowNr != UINT8_MAX && rowNr < fileNames.size()) {
const char * fileName = fileNames[rowNr].s;
// ppf("fileTbl onDelete %s[%d] = %s %s\n", Variable(var).id(), rowNr, Variable(var).valueString().c_str(), fileName);
this->removeFiles(fileName, false);

Expand All @@ -63,22 +63,14 @@ void SysModFiles::setup() {
default: return false;
}});

ui->initText(tableVar, "flName", nullptr, 32, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue:
for (forUnsigned8 rowNr = 0; rowNr < fileList.size(); rowNr++)
mdl->setValue(var, JsonString(fileList[rowNr].name, JsonString::Copied), rowNr);
return true;
ui->initTextVector(tableVar, "flName", &fileNames, 32, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onUI:
ui->setLabel(var, "Name");
return true;
default: return false;
}});

ui->initNumber(tableVar, "flSize", UINT16_MAX, 0, UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue:
for (forUnsigned8 rowNr = 0; rowNr < fileList.size(); rowNr++)
mdl->setValue(var, fileList[rowNr].size, rowNr);
return true;
ui->initNumber(tableVar, "flSize", &fileSizes, 0, UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onUI:
ui->setLabel(var, "Size (B)");
return true;
Expand All @@ -99,22 +91,15 @@ void SysModFiles::setup() {
// default: return false;
// }});

ui->initNumber(tableVar, "flTime", UINT16_MAX, 0, UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue:
for (forUnsigned8 rowNr = 0; rowNr < fileList.size(); rowNr++)
mdl->setValue(var, fileList[rowNr].time, rowNr);
return true;
ui->initNumber(tableVar, "flTime", &fileTimes, 0, UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onUI:
ui->setLabel(var, "Time");
return true;
default: return false;
}});

ui->initFileEdit(tableVar, "flEdit", nullptr, false, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue:
for (forUnsigned8 rowNr = 0; rowNr < fileList.size(); rowNr++)
mdl->setValue(var, JsonString(fileList[rowNr].name, JsonString::Copied), rowNr);
return true;
//readonly = true, but button must be pressable (done in index.js)
ui->initFileEditVector(tableVar, "flEdit", &fileNames, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onUI:
ui->setLabel(var, "Edit");
return true;
Expand Down Expand Up @@ -149,22 +134,35 @@ void SysModFiles::loop20ms() {
File file = root.openNextFile();

//repopulate file list
fileList.clear();
fileNames.clear();
fileSizes.clear();
fileTimes.clear();
uint8_t rowNr = 0;
while (file) {
FileDetails details;
strcpy(details.name, file.name());
details.size = file.size();
details.time = file.getLastWrite(); // - millis()/1000; if (details.time < 0) details.time = 0;
fileList.push_back(details);

while (rowNr >= fileNames.size()) fileNames.push_back(VectorString()); //create vector space if needed...
strcpy(fileNames[rowNr].s, file.name());
while (rowNr >= fileSizes.size()) fileSizes.push_back(UINT8_MAX); //create vector space if needed...
fileSizes[rowNr] = file.size();
while (rowNr >= fileTimes.size()) fileTimes.push_back(UINT8_MAX); //create vector space if needed...
fileTimes[rowNr] = file.getLastWrite(); // - millis()/1000; if (details.time < 0) details.time = 0;

file.close();
file = root.openNextFile();
rowNr++;
}
root.close();

mdl->setValue("drsize", files->usedBytes());

for (JsonObject childVar: Variable(mdl->findVar("fileTbl")).children())
ui->callVarFun(childVar, UINT8_MAX, onSetValue); //set the value (WIP)
uint8_t rowNrL = 0;
for (VectorString name: fileNames) {
mdl->setValue("flName", JsonString(name.s, JsonString::Copied), rowNrL);
mdl->setValue("flEdit", JsonString(name.s, JsonString::Copied), rowNrL);
rowNrL++;
}
rowNrL = 0; for (uint16_t size: fileSizes) mdl->setValue("flSize", size, rowNrL++);
rowNrL = 0; for (uint16_t time: fileTimes) mdl->setValue("flTime", time, rowNrL++);
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/Sys/SysModFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
#include "SysModule.h"
#include "LittleFS.h"

struct FileDetails {
char name[32];
size_t size;
time_t time;
};

class SysModFiles: public SysModule {

public:

std::vector<FileDetails> fileList;
bool filesChanged = true; //init fileTbl;

std::vector<VectorString> fileNames;
std::vector<uint16_t> fileSizes;
std::vector<uint16_t> fileTimes;

SysModFiles();
void setup();
void loop20ms();
Expand Down
8 changes: 4 additions & 4 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,22 @@ bool SysModModel::callVarOnChange(JsonObject var, unsigned8 rowNr, bool init) {
std::vector<uint8_t> *valuePointer = (std::vector<uint8_t> *)pointer;
while (rowNr >= (*valuePointer).size()) (*valuePointer).push_back(UINT8_MAX); //create vector space if needed...
ppf("%s[%d]:%s (%d - %d - %s)\n", variable.id(), rowNr, variable.valueString().c_str(), pointer, (*valuePointer).size(), var["p"].as<String>().c_str());
(*valuePointer)[rowNr] = value; //value should be an uint16_t
(*valuePointer)[rowNr] = value;
}
else if (var["type"] == "number") {
std::vector<uint16_t> *valuePointer = (std::vector<uint16_t> *)pointer;
while (rowNr >= (*valuePointer).size()) (*valuePointer).push_back(UINT16_MAX); //create vector space if needed...
(*valuePointer)[rowNr] = value; //value should be an uint16_t
(*valuePointer)[rowNr] = value;
}
else if (var["type"] == "coord3D") {
std::vector<Coord3D> *valuePointer = (std::vector<Coord3D> *)pointer;
while (rowNr >= (*valuePointer).size()) (*valuePointer).push_back({-1,-1,-1}); //create vector space if needed...
(*valuePointer)[rowNr] = var["value"][rowNr]; //value should be an uint16_t
(*valuePointer)[rowNr] = value;
}
else
print->printJson("dev callVarOnChange type not supported yet", var);

ppf("callVarOnChange set pointer to vector %s[%d]: v:%s p:%d\n", variable.id(), rowNr, variable.valueString().c_str(), pointer);
ppf("callVarOnChange set pointer to vector %s[%d]: v:%s p:%d\n", variable.id(), rowNr, value.as<String>().c_str(), pointer);
} else
print->printJson("dev value is array but no rowNr\n", var);
} else {
Expand Down
32 changes: 31 additions & 1 deletion src/Sys/SysModUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class SysModUI: public SysModule {
return initVarAndValue<const char *>(parent, id, "text", value, 0, max, readOnly, varFun);
}

//vector of text
JsonObject initTextVector(JsonObject parent, const char * id, std::vector<VectorString> *values, unsigned16 max = 32, bool readOnly = false, VarFun varFun = nullptr) {
return initVarAndValueVector(parent, id, "text", values, 0, max, readOnly, varFun);
}

JsonObject initFileUpload(JsonObject parent, const char * id, const char * value = nullptr, unsigned16 max = 32, bool readOnly = false, VarFun varFun = nullptr) {
return initVarAndValue<const char *>(parent, id, "fileUpload", value, 0, max, readOnly, varFun);
}
Expand Down Expand Up @@ -157,6 +162,11 @@ class SysModUI: public SysModule {
return initVarAndValue<const char *>(parent, id, "fileEdit", value, 0, 0, readOnly, varFun);
}

//vector of fileEdit
JsonObject initFileEditVector(JsonObject parent, const char * id, std::vector<VectorString> *values, bool readOnly = false, VarFun varFun = nullptr) {
return initVarAndValueVector(parent, id, "fileEdit", values, 0, 0, readOnly, varFun);
}

JsonObject initURL(JsonObject parent, const char * id, const char * value = nullptr, bool readOnly = false, VarFun varFun = nullptr) {
return initVarAndValue<const char *>(parent, id, "url", value, 0, 0, readOnly, varFun);
}
Expand Down Expand Up @@ -197,7 +207,7 @@ class SysModUI: public SysModule {

if (initValue(var, min, max, (int)values)) {
uint8_t rowNrL = 0;
for (uint16_t value: *values) { //loop over vector
for (Type value: *values) { //loop over vector
mdl->setValue(var, value, rowNrL); //does onChange if needed, if var in table, update the table row
rowNrL++;
}
Expand All @@ -206,6 +216,26 @@ class SysModUI: public SysModule {
return var;
}

//initVarAndValue using vector of values . WIP!!!
JsonObject initVarAndValueVector(JsonObject parent, const char * id, const char * type, std::vector<VectorString> *values, int min = 0, int max = 255, bool readOnly = true, VarFun varFun = nullptr) {
JsonObject var = initVar(parent, id, type, readOnly, varFun);

if (!var["value"].isNull()) {
print->printJson("Clearing the vector", var);
(*values).clear(); // if values already then rebuild the vector with it
}

if (initValue(var, min, max, (int)values)) {
uint8_t rowNrL = 0;
for (VectorString value: *values) { //loop over vector
mdl->setValue(var, JsonString(value.s, JsonString::Copied), rowNrL); //does onChange if needed, if var in table, update the table row
rowNrL++;
}
}

return var;
}

//adds a variable to the model
JsonObject initVar(JsonObject parent, const char * id, const char * type, bool readOnly = true, VarFun varFun = nullptr);

Expand Down
5 changes: 5 additions & 0 deletions src/SysModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@

#include <vector>

//used for init vars with vectors (vector<char[32]> not allowed)
struct VectorString {
char s[32];
};

class SysModule {

public:
Expand Down
Loading

0 comments on commit de7eb90

Please sign in to comment.