Skip to content

Commit

Permalink
CLion recommendations part 2
Browse files Browse the repository at this point in the history
- add inline to distance
- add const to functions or local vars
- use vector.empty instead of size()!=0
- use const type &var
- use strtol instead of atoi
- use nullptr instead of NULL
- use (bool3State) type cast for initCheckBox
- use explicit for 1 parameter constructors
- move declarations where vars are used
  • Loading branch information
ewowi committed Nov 24, 2024
1 parent 6cb18dc commit 1d4eb2e
Show file tree
Hide file tree
Showing 29 changed files with 167 additions and 161 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.pio
node_modules
.vscode
.DS_Store
.DS_Store
.idea
2 changes: 1 addition & 1 deletion misc/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@
"fun": 9
},
{
"id": "driverShow",
"id": "showDriver",
"type": "checkbox",
"ro": false,
"value": false,
Expand Down
60 changes: 30 additions & 30 deletions misc/testOutput.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ lib_deps =
build_flags =
-D APP=StarBase
-D PIOENV=$PIOENV
-D VERSION=24110711 ; Date and time (GMT!), update at every commit!!
-D VERSION=24112411 ; Date and time (GMT!), update at every commit!!
-D LFS_THREADSAFE ; enables use of semaphores in LittleFS driver
-D STARBASE_DEVMODE
-mtext-section-literals ;otherwise [UserModLive::setup()]+0xa17): dangerous relocation: l32r: literal target out of range (try using text-section-literals)
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SysModFiles::SysModFiles() :SysModule("Files") {

void SysModFiles::setup() {
SysModule::setup();
Variable parentVar = ui->initSysMod(Variable(), name, 2101);
const Variable parentVar = ui->initSysMod(Variable(), name, 2101);

Variable tableVar = ui->initTable(parentVar, "files", nullptr, false, [this](EventArguments) { switch (eventType) {
case onUI:
Expand Down
14 changes: 7 additions & 7 deletions src/Sys/SysModInstances.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class SysModInstances:public SysModule {
void setup() override {
SysModule::setup();

Variable parentVar = ui->initSysMod(Variable(), name, 3000);
const Variable parentVar = ui->initSysMod(Variable(), name, 3000);

Variable tableVar = ui->initTable(parentVar, "instances", nullptr, true);

Expand Down Expand Up @@ -232,7 +232,7 @@ class SysModInstances:public SysModule {
Variable insVariable = mdl->initVar(tableVar, columnVarID, variable.var["type"], false, [this](Variable insVariable, uint8_t rowNr, uint8_t eventType) {
//extract the variable from insVariable.id()
char pid[32]; strlcpy(pid, insVariable.id() + 3, sizeof(pid)); //+3 : remove ins
char * id = strtok(pid, "_"); if (id != NULL ) {strlcpy(pid, id, sizeof(pid)); id = strtok(NULL, "_");} //split pid and id
char * id = strtok(pid, "_"); if (id != nullptr ) {strlcpy(pid, id, sizeof(pid)); id = strtok(nullptr, "_");} //split pid and id
Variable variable = Variable(mdl->findVar(pid, id));
switch (eventType) { //varEvent
case onSetValue:
Expand Down Expand Up @@ -331,10 +331,10 @@ class SysModInstances:public SysModule {
char * token = strtok(copy, "-"); //before minus

//check if group
if (token != NULL) {
if (token != nullptr) {
// ppf("groupOfName 1:%s 2:%s 3:%s 4:%s 5:%s\n",name, strnstr(name, "-", 32), name, strtok(name, "-"), name?name:"X");
token = strtok(NULL, "-"); //after -
if (token != NULL) {
token = strtok(nullptr, "-"); //after -
if (token != nullptr) {
// ppf("groupOfName g:%s i:%s\n", copy, token);
if (group) strlcpy(group, copy, sizeof(copy)); //group has same size!
return true;
Expand Down Expand Up @@ -778,9 +778,9 @@ class SysModInstances:public SysModule {
char pid[32];
strlcpy(pid, pair.key().c_str(), sizeof(pid));
char * id = strtok(pid, ".");
if (id != NULL ) {
if (id != nullptr ) {
strlcpy(pid, id, sizeof(pid)); //copy the id part
id = strtok(NULL, "."); //the rest after .
id = strtok(nullptr, "."); //the rest after .
}

Variable(mdl->findVar(pid, id)).setValueJV(pair.value());
Expand Down
10 changes: 5 additions & 5 deletions src/Sys/SysModModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ SysModModel::SysModModel() :SysModule("Model") {
void SysModModel::setup() {
SysModule::setup();

Variable parentVar = ui->initSysMod(Variable(), name, 4303);
const Variable parentVar = ui->initSysMod(Variable(), name, 4303);
parentVar.var["s"] = true; //setup

ui->initButton(parentVar, "saveModel", false, [this](EventArguments) { switch (eventType) {
Expand All @@ -524,7 +524,7 @@ void SysModModel::setup() {

#ifdef STARBASE_DEVMODE

ui->initCheckBox(parentVar, "showObsolete", false, false, [this](EventArguments) { switch (eventType) {
ui->initCheckBox(parentVar, "showObsolete", (bool3State)false, false, [this](EventArguments) { switch (eventType) {
case onUI:
variable.setComment("Show in UI (refresh)");
return true;
Expand Down Expand Up @@ -625,7 +625,7 @@ void SysModModel::cleanUpModel(Variable parent, bool oPos, bool ro) {

//recursive call
if (!variable.children().isNull())
cleanUpModel(var, oPos, ro);
cleanUpModel(variable, oPos, ro);
}
}
}
Expand Down Expand Up @@ -689,11 +689,11 @@ Variable SysModModel::initVar(Variable parent, const char * id, const char * typ
var["fun"] = mdl->varEvents.size()-1;
// }

if (varEvent(var, UINT8_MAX, onLoop)) { //test run if it supports loop
if (varEvent(variable, UINT8_MAX, onLoop)) { //test run if it supports loop
//no need to check if already in...
VarLoop loop;
loop.loopFun = varEvent;
loop.var = var;
loop.variable = variable;

ui->loopFunctions.push_back(loop);
var["loopFun"] = ui->loopFunctions.size()-1;
Expand Down
66 changes: 33 additions & 33 deletions src/Sys/SysModModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,56 +39,56 @@ struct Coord3D {
// return x != rhs.x || y != rhs.y || z != rhs.z;
return !(*this==rhs);
}
bool operator==(Coord3D rhs) {
bool operator==(const Coord3D rhs) const {
return x == rhs.x && y == rhs.y && z == rhs.z;
}
bool operator>=(Coord3D rhs) {
bool operator>=(const Coord3D rhs) const {
return x >= rhs.x && y >= rhs.y && z >= rhs.z;
}
bool operator<=(Coord3D rhs) {
bool operator<=(const Coord3D rhs) const {
return x <= rhs.x && y <= rhs.y && z <= rhs.z;
}
bool operator<(Coord3D rhs) {
bool operator<(const Coord3D rhs) const {
return x < rhs.x && y < rhs.y && z < rhs.z;
}
bool operator>=(int rhs) {
bool operator>=(const int rhs) const {
return x >= rhs && y >= rhs && z >= rhs;
}

//assignments
Coord3D operator=(Coord3D rhs) {
Coord3D operator=(const Coord3D rhs) {
// ppf("Coord3D assign %d,%d,%d\n", rhs.x, rhs.y, rhs.z);
x = rhs.x;
y = rhs.y;
z = rhs.z;
return *this;
}
Coord3D operator+=(Coord3D rhs) {
Coord3D operator+=(const Coord3D rhs) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
Coord3D operator-=(Coord3D rhs) {
Coord3D operator-=(const Coord3D rhs) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
Coord3D operator*=(Coord3D rhs) {
Coord3D operator*=(const Coord3D rhs) {
x *= rhs.x;
y *= rhs.y;
z *= rhs.z;
return *this;
}
Coord3D operator/=(Coord3D rhs) {
Coord3D operator/=(const Coord3D rhs) {
if (rhs.x) x /= rhs.x;
if (rhs.y) y /= rhs.y;
if (rhs.z) z /= rhs.z;
return *this;
}
//Minus / delta (abs)
Coord3D operator-(Coord3D rhs) {
Coord3D operator-(const Coord3D rhs) const {
Coord3D result;
// result.x = x > rhs.x? x - rhs.x : rhs.x - x;
// result.y = y > rhs.y? y - rhs.y : rhs.y - y;
Expand All @@ -98,28 +98,28 @@ struct Coord3D {
result.z = z - rhs.z;
return result;
}
Coord3D operator+(Coord3D rhs) {
Coord3D operator+(const Coord3D rhs) const {
return Coord3D{x + rhs.x, y + rhs.y, z + rhs.z};
}
Coord3D operator*(Coord3D rhs) {
Coord3D operator*(const Coord3D rhs) const {
return Coord3D{x * rhs.x, y * rhs.y, z * rhs.z};
}
Coord3D operator/(Coord3D rhs) {
Coord3D operator/(const Coord3D rhs) const {
return Coord3D{x / rhs.x, y / rhs.y, z / rhs.z};
}
Coord3D operator%(Coord3D rhs) {
Coord3D operator%(const Coord3D rhs) const {
return Coord3D{x % rhs.x, y % rhs.y, z % rhs.z};
}
Coord3D minimum(Coord3D rhs) {
Coord3D minimum(const Coord3D rhs) const {
return Coord3D{min(x, rhs.x), min(y, rhs.y), min(z, rhs.z)};
}
Coord3D maximum(Coord3D rhs) {
Coord3D maximum(const Coord3D rhs) const {
return Coord3D{max(x, rhs.x), max(y, rhs.y), max(z, rhs.z)};
}
Coord3D operator*(uint8_t rhs) {
Coord3D operator*(const uint8_t rhs) const {
return Coord3D{x * rhs, y * rhs, z * rhs};
}
Coord3D operator/(uint8_t rhs) {
Coord3D operator/(const uint8_t rhs) const {
return Coord3D{x / rhs, y / rhs, z / rhs};
}
//move the coordinate one step closer to the goal, if difference in coordinates (used in GenFix)
Expand All @@ -129,15 +129,15 @@ struct Coord3D {
if (z != goal.z) z += (z<goal.z)?step:-step;
return *this;
}
unsigned distance(Coord3D rhs) {
unsigned distance(const Coord3D rhs) const {
Coord3D delta = (*this-rhs);
return sqrt((delta.x)*(delta.x) + (delta.y)*(delta.y) + (delta.z)*(delta.z));
}
unsigned distanceSquared(Coord3D rhs) {
unsigned distanceSquared(const Coord3D rhs) const {
Coord3D delta = (*this-rhs);
return (delta.x)*(delta.x) + (delta.y)*(delta.y) + (delta.z)*(delta.z);
}
bool isOutofBounds(Coord3D rhs) {
bool isOutofBounds(const Coord3D rhs) const {
return x < 0 || y < 0 || z < 0 || x >= rhs.x || y >= rhs.y || z >= rhs.z;
}
};
Expand Down Expand Up @@ -209,31 +209,31 @@ class Variable {
JsonObject var;

Variable() {this->var = JsonObject();} //undefined variable
Variable(JsonObject var) {this->var = var;}
explicit Variable(JsonObject var) {this->var = var;}

//core methods
const char *pid() {return var["pid"];}
const char *id() {return var["id"];}
const char *pid() const {return var["pid"];}
const char *id() const {return var["id"];}

JsonVariant value() {return var["value"];}
JsonVariant value(uint8_t rowNr) {return var["value"][rowNr];}
JsonVariant value() const {return var["value"];}
JsonVariant value(uint8_t rowNr) const {return var["value"][rowNr];}

String valueString(uint8_t rowNr = UINT8_MAX);

int order() {return var["o"];}
void order(int value) {var["o"] = value;}
int order() const {return var["o"];}
void order(int value) const {var["o"] = value;}

bool readOnly() {return var["ro"];}
bool readOnly() const {return var["ro"];}
void readOnly(bool value) {var["ro"] = value;}

JsonArray children() {return var["n"];}
JsonArray children() const {return var["n"];}

void defaultOrder(int value) {if (order() > -1000) order(- value); } //set default order (in range >=1000). Don't use auto generated order as order can be changed in the ui (WIP)
void defaultOrder(int value) const {if (order() > -1000) order(- value); } //set default order (in range >=1000). Don't use auto generated order as order can be changed in the ui (WIP)

//recursively remove all value[rowNr] from children of var
void removeValuesForRow(uint8_t rowNr);

JsonArray valArray() {if (var["value"].is<JsonArray>()) return var["value"]; else return JsonArray(); }
JsonArray valArray() const {if (var["value"].is<JsonArray>()) return var["value"]; else return JsonArray(); }

//if variable is a table, loop through its rows
void rows(std::function<void(Variable, uint8_t)> fun = nullptr);
Expand Down
22 changes: 11 additions & 11 deletions src/Sys/SysModNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ SysModNetwork::SysModNetwork() :SysModule("Network") {};
void SysModNetwork::setup() {
SysModule::setup();

Variable parentVar = ui->initSysMod(Variable(), name, 3502);
const Variable parentVar = ui->initSysMod(Variable(), name, 3502);
parentVar.var["s"] = true; //setup

// Variable tableVar = ui->initTable(parentVar, "wfTbl", nullptr, false, [this](EventArguments) { //varEvent ro false: create and delete row possible
// variable.setComment("List of defined and available Wifi APs");
// });

Variable currentVar = ui->initCheckBox(parentVar, "wiFi", true, false, [this](EventArguments) { switch (eventType) {
Variable currentVar = ui->initCheckBox(parentVar, "wiFi", (bool3State)true, false, [this](EventArguments) { switch (eventType) {
case onChange:
if (variable.value().as<bool>())
initWiFiConnection();
Expand Down Expand Up @@ -81,7 +81,7 @@ void SysModNetwork::setup() {

#ifdef STARBASE_ETHERNET

currentVar = ui->initCheckBox(parentVar, "ethernet", false, false, [this](EventArguments) { switch (eventType) {
currentVar = ui->initCheckBox(parentVar, "ethernet", (bool3State)false, false, [this](EventArguments) { switch (eventType) {
case onLoop1s:
//initEthernet not done in onChange as initEthernet needs a bit of a delay
if (!ethActive && variable.getValue().as<bool>())
Expand All @@ -102,31 +102,31 @@ void SysModNetwork::setup() {
mdl->setValueRowNr = rowNr;

if (variable.value() == 0) {//manual
ui->initNumber(variable.var, "address", (uint16_t)0, 0, 255, false, [this](EventArguments) { switch (eventType) {
ui->initNumber(variable, "address", (uint16_t)0, 0, 255, false, [this](EventArguments) { switch (eventType) {
case onChange:
ethActive = false;
return true;
default: return false;
}});
ui->initPin(variable.var, "power", 14, false, [this](EventArguments) { switch (eventType) {
ui->initPin(variable, "power", 14, false, [this](EventArguments) { switch (eventType) {
case onChange:
ethActive = false;
return true;
default: return false;
}});
ui->initPin(variable.var, "mdc", 23, false, [this](EventArguments) { switch (eventType) {
ui->initPin(variable, "mdc", 23, false, [this](EventArguments) { switch (eventType) {
case onChange:
ethActive = false;
return true;
default: return false;
}});
ui->initPin(variable.var, "mdio", 18, false, [this](EventArguments) { switch (eventType) {
ui->initPin(variable, "mdio", 18, false, [this](EventArguments) { switch (eventType) {
case onChange:
ethActive = false;
return true;
default: return false;
}});
ui->initSelect(variable.var, "type", ETH_PHY_LAN8720, false, [this](EventArguments) { switch (eventType) {
ui->initSelect(variable, "type", ETH_PHY_LAN8720, false, [this](EventArguments) { switch (eventType) {
case onUI: {
JsonArray options = variable.setOptions();
options.add("LAN8720");
Expand All @@ -144,7 +144,7 @@ void SysModNetwork::setup() {
return true;
default: return false;
}});
ui->initSelect(variable.var, "clockMode", ETH_CLOCK_GPIO17_OUT, false, [this](EventArguments) { switch (eventType) {
ui->initSelect(variable, "clockMode", ETH_CLOCK_GPIO17_OUT, false, [this](EventArguments) { switch (eventType) {
case onUI: {
JsonArray options = variable.setOptions();
options.add("GPIO0_IN");
Expand Down Expand Up @@ -179,7 +179,7 @@ void SysModNetwork::setup() {

#endif

currentVar = ui->initCheckBox(parentVar, "AP", false, false, [this](EventArguments) { switch (eventType) {
currentVar = ui->initCheckBox(parentVar, "AP", (bool3State)false, false, [this](EventArguments) { switch (eventType) {
case onChange:
if (variable.value().as<bool>())
initAP();
Expand Down Expand Up @@ -315,7 +315,7 @@ void SysModNetwork::initAP() {

const char * apSSID = mdl->getValue("System", "name");
if (WiFi.softAPConfig(IPAddress(4, 3, 2, 1), IPAddress(4, 3, 2, 1), IPAddress(255, 255, 255, 0))
&& WiFi.softAP(apSSID, NULL, apChannel, false)) { //no password!!!
&& WiFi.softAP(apSSID, nullptr, apChannel, false)) { //no password!!!
ppf("AP success %s %s s:%d\n", apSSID, WiFi.softAPIP().toString().c_str(), WiFi.status()); //6 is disconnected
#if defined(STARBASE_LOLIN_WIFI_FIX )
WiFi.setTxPower(WIFI_POWER_8_5dBm );
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModPins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SysModPins::SysModPins() :SysModule("Pins") {

void SysModPins::setup() {
SysModule::setup();
Variable parentVar = ui->initSysMod(Variable(), name, 2202);
const Variable parentVar = ui->initSysMod(Variable(), name, 2202);

//show table of allocated pins
Variable tableVar = ui->initTable(parentVar, "pins", nullptr, true, [](EventArguments) { switch (eventType) {
Expand Down
2 changes: 1 addition & 1 deletion src/Sys/SysModPins.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SysModPins:public SysModule {

void allocatePin(uint8_t pin, const char * owner, const char * details);
void deallocatePin(uint8_t pin = UINT8_MAX, const char * owner = nullptr);
bool isOwner(uint8_t pin, const char * owner) {
bool isOwner(uint8_t pin, const char * owner) const {
return strncmp(pinObjects[pin].owner, owner, sizeof(PinObject::owner)) == 0;
}

Expand Down
Loading

0 comments on commit 1d4eb2e

Please sign in to comment.