Commit 1118818c authored by Dorian Zedler's avatar Dorian Zedler

bug fixes

parent 9b0530ee
......@@ -30,6 +30,7 @@
// --- general conf ---
// cycling speed between different timers (in ms)
#define cyclingSpeed 2000
#define maxTimers 2
// refresh interval (in ms)
#define refreshInterval 300
......@@ -67,7 +68,7 @@ struct WiFiConfigData
// --- helper vars ---
SpeedTimerDataT timerData;
SpeedTimerDataT timerData[maxTimers];
double lastRefresh = 0;
int i = 0;
int latestCycleTimerId = 0;
......@@ -160,7 +161,7 @@ void loop() {
handleSerial();
}
if(!client.connected()){
if (!client.connected()) {
break;
}
......@@ -178,7 +179,10 @@ void loop() {
//Serial.println(response);
// process the response
if (response.startsWith("<message>") && response.endsWith("</message>")) {
response.replace("\n","");
response.replace("\r","");
if (response.startsWith("<message>") && response.endsWith("</message>") && response.indexOf("</message><message>") < 0) {
// if we recieved a complete (not corrupted) message -> process it
response.replace("</message>", "");
response.replace("<message>", "");
......@@ -186,6 +190,20 @@ void loop() {
// process the recieved data
refreshTimerVals(response);
}
else if (response.startsWith("<message>") && response.endsWith("</message>") && response.indexOf("</message><message>") > 0){
//Serial.println("split response: " + response);
response = response.substring(9, response.length() - 10);
String firstMessage = response.substring(0, response.lastIndexOf("</message>"));
String secondMessage = response.substring(response.lastIndexOf("<message>") + 9, response.length());
//Serial.println("| first message: " + firstMessage);
//Serial.println("| second message: " + secondMessage);
// process the recieved data
refreshTimerVals(firstMessage);
refreshTimerVals(secondMessage);
}
else {
Serial.println("invalid response: " + response);
}
......@@ -213,7 +231,7 @@ bool initSession() {
// wait for it to answer
unsigned long timeout = millis();
while (client.available() == 0) {
printLoadingScreen();
if (millis() - timeout > 5000) {
......@@ -256,7 +274,7 @@ bool initSession() {
bool refreshTimerVals(String timerJSON) {
//Serial.println("starting to process JSON: " + timerJSON);
const size_t capacity = JSON_ARRAY_SIZE(2) + 2 * ( JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) ) + 50;
const size_t capacity = JSON_ARRAY_SIZE(maxTimers) + maxTimers * ( JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) ) + 50;
DynamicJsonDocument doc(capacity);
// try to parse JSON
......@@ -265,116 +283,120 @@ bool refreshTimerVals(String timerJSON) {
if (parseError == DeserializationError::Ok && doc["header"] == 9001) {
// if the request was successfully parsed and successfull -> process it
// the id of the timer to be shown on the display
int showTimer = -1;
// get all timers
JsonArray timers = doc["data"];
// if there is one or more timer in stopped state, this variable will be set to true
bool cycleStoppedTimers = false;
for (int i = 0; i < timers.size(); i++) {
// extract the required data from the timer and write it to the local storage
JsonObject data = doc["data"][i];
int cT = data["currTime"];
timerData[i].currTime = cT;
int rT = data["reactTime"];
timerData[i].reactTime = rT;
int s = data["state"];
timerData[i].state = (timerState)s;
timerData[i].startTime = millis() - timerData[i].currTime;
}
doc.clear();
return true;
}
else {
doc.clear();
return false;
}
}
void updateDisplay() {
// the id of the timer to be shown on the display
int showTimer = -1;
// if there is one or more timer in stopped state, this variable will be set to true
bool cycleStoppedTimers = false;
// should there be a cycle now?
bool cycleNow = millis() - timeOfLastCycle >= cyclingSpeed;
// should there be a cycle now?
bool cycleNow = millis() - timeOfLastCycle >= cyclingSpeed;
// the id of the first timer which is not stopped or disabled
int firstRunningTimer = -1;
// the id of the first timer which is not stopped or disabled
int firstRunningTimer = -1;
// the ids of all timers in WON, LOST, FAILED or CACELLED state
int * stoppedTimers = NULL;
stoppedTimers = new int[timers.size()];
int stoppedTimersCount = 0;
// the ids of all timers in WON, LOST, FAILED or CACELLED state
int * stoppedTimers = NULL;
stoppedTimers = new int[maxTimers];
int stoppedTimersCount = 0;
for (int i = 0; i < timers.size(); i++) {
if (timers.getElement(i)["state"].as<int>() >= WON && timers.getElement(i)["state"].as<int>() < DISABLED) {
// this timer is stopped, but not disabled
for (int i = 0; i < maxTimers; i++) {
if (timerData[i].state >= WON && timerData[i].state < DISABLED) {
// this timer is stopped, but not disabled
// there is already at least one stopped timer -> the display should cycle between all stopped timers
cycleStoppedTimers = true;
// there is already at least one stopped timer -> the display should cycle between all stopped timers
cycleStoppedTimers = true;
// add the id of this timer to the stopped timers list
stoppedTimers[stoppedTimersCount] = i;
stoppedTimersCount ++;
// add the id of this timer to the stopped timers list
stoppedTimers[stoppedTimersCount] = i;
stoppedTimersCount ++;
}
else if (timers.getElement(i)["state"].as<int>() <= RUNNING && firstRunningTimer == -1) {
// this timer is not staopped or deactivated
firstRunningTimer = i;
}
}
else if (timerData[i].state <= RUNNING && firstRunningTimer == -1) {
// this timer is not staopped or deactivated
firstRunningTimer = i;
}
}
if (!cycleStoppedTimers) {
// if there is no timer in stopped styte yet, all displays would show the same
// -> simply use the first not-deactivated timer
showTimer = firstRunningTimer;
if (!cycleStoppedTimers) {
// if there is no timer in stopped styte yet, all displays would show the same
// -> simply use the first not-deactivated timer
showTimer = firstRunningTimer;
}
else {
if (!cycleNow) {
// if we are not currently cycling -> use the last shown timer
showTimer = latestCycleTimerId;
}
else {
if (!cycleNow) {
// if we are not currently cycling -> use the last shown timer
showTimer = latestCycleTimerId;
}
else {
// else -> show the next timer
int newTimerIndex = 0;
// find the last shown timer in the stopped timers array
// if it is the last one or not found -> timer with index 0 will be shown
for (int i = 0; i < stoppedTimersCount; i++) {
if (stoppedTimers[i] == latestCycleTimerId && i < stoppedTimersCount - 1) {
// if the last shown timer is not the last on in the array
// -> increase the index for the timer to show by one
newTimerIndex = i + 1;
}
// else -> show the next timer
int newTimerIndex = 0;
// find the last shown timer in the stopped timers array
// if it is the last one or not found -> timer with index 0 will be shown
for (int i = 0; i < stoppedTimersCount; i++) {
if (stoppedTimers[i] == latestCycleTimerId && i < stoppedTimersCount - 1) {
// if the last shown timer is not the last on in the array
// -> increase the index for the timer to show by one
newTimerIndex = i + 1;
}
// set timer to show
showTimer = stoppedTimers[newTimerIndex];
// update last shown timer
latestCycleTimerId = showTimer;
// update last cycle
timeOfLastCycle = millis();
}
// set timer to show
showTimer = stoppedTimers[newTimerIndex];
// update last shown timer
latestCycleTimerId = showTimer;
// update last cycle
timeOfLastCycle = millis();
}
}
// delete the dynamic array
delete [] stoppedTimers;
stoppedTimers = NULL;
// delete the dynamic array
delete [] stoppedTimers;
stoppedTimers = NULL;
//Serial.println("showing timer " + String(showTimer) + " currently cycling: " + String(cycleStoppedTimers));
//Serial.println("showing timer " + String(showTimer) + " currently cycling: " + String(cycleStoppedTimers));
// extract the required data from the timer and write it to the local storage
JsonObject data = doc["data"][showTimer];
int cT = data["currTime"];
timerData.currTime = cT;
int rT = data["reactTime"];
timerData.reactTime = rT;
int s = data["state"];
timerData.state = (timerState)s;
timerData.startTime = millis() - timerData.currTime;
doc.clear();
return true;
}
else {
doc.clear();
return false;
}
}
void updateDisplay() {
matrix.setTextColor(getTimerTextColor());
drawTimerText();
// update text color
matrix.setTextColor(getTimerTextColor(timerData[showTimer]));
drawTimerText(timerData[showTimer]);
}
// = drawing functions =
void drawTimerText() {
void drawTimerText(SpeedTimerDataT timer) {
String text = "";
switch (timerData.state) {
switch (timer.state) {
case IDLE: {
// draw 00,000
drawTimeToDisplay(0, 3);
drawTimeToDisplay(0, 3, getTimerTextColor(timer));
return;
}
......@@ -388,21 +410,21 @@ void drawTimerText() {
case RUNNING: {
// calculate the current time dynamically to make the timer run smoothly
double currTime = (millis() - timerData.startTime);
double currTime = (millis() - timer.startTime);
// draw the time with one decimal place because the last two are unreadable anyway
drawTimeToDisplay(currTime, 1);
drawTimeToDisplay(currTime, 1, getTimerTextColor(timer));
return;
}
case WON: {
// draw the full time (three decimal places)
drawTimeToDisplay(timerData.currTime, 3);
drawTimeToDisplay(timer.currTime, 3, getTimerTextColor(timer));
return;
}
case LOST: {
// draw the full time (three decimal places)
drawTimeToDisplay(timerData.currTime, 3);
drawTimeToDisplay(timer.currTime, 3, getTimerTextColor(timer));
return;
}
......@@ -422,7 +444,7 @@ void drawTimerText() {
printDisplay(text, true);
}
void drawTimeToDisplay(int timeMs, int decimalPlaces) {
void drawTimeToDisplay(int timeMs, int decimalPlaces, uint16_t color) {
// correct invalid decimal place input
if (decimalPlaces > 3 || decimalPlaces < 0) {
......@@ -461,16 +483,16 @@ void drawTimeToDisplay(int timeMs, int decimalPlaces) {
matrix.print(mSecondsText);
// draw comma
matrix.drawPixel(6 * secondsText.length() - 1 + leftMargin, 8, getTimerTextColor());
matrix.drawPixel(6 * secondsText.length() - 2 + leftMargin, 9, getTimerTextColor());
matrix.drawPixel(6 * secondsText.length() - 1 + leftMargin, 8, color);
matrix.drawPixel(6 * secondsText.length() - 2 + leftMargin, 9, color);
// apply
matrix.show();
}
uint16_t getTimerTextColor() {
uint16_t getTimerTextColor(SpeedTimerDataT timer) {
uint16_t color;
switch (timerData.state) {
switch (timer.state) {
case IDLE:
color = matrix.Color(0, 0, 120);
break;
......@@ -560,6 +582,8 @@ void printLoadingScreen() {
matrix.show();
}
// = Serial functions =
void handleSerial() {
while (Serial.available()) {
digitalWrite(LED_BUILTIN, HIGH);
......@@ -640,7 +664,7 @@ bool loadWiFiConf(WiFiConfigData * targetConf)
void writeWiFiConf(WiFiConfigData * srcConf, char WiFiSSID[50], char WiFiPWD[50])
{
byte len;
byte len;
strncpy( srcConf->WiFiSSID, WiFiSSID, sizeof(srcConf->WiFiSSID) );
len = strlen(srcConf->WiFiSSID);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment