Jumperless/JumperlessNano/src/FileParsing.cpp

1085 lines
26 KiB
C++
Raw Normal View History

2023-05-31 16:00:30 -07:00
#include "FileParsing.h"
#include <Arduino.h>
#include "LittleFS.h"
#include "MatrixStateRP2040.h"
#include "SafeString.h"
2023-06-13 20:57:04 -07:00
#include "ArduinoJson.h"
2023-05-31 16:00:30 -07:00
#include "NetManager.h"
2023-06-13 20:57:04 -07:00
#include "JumperlessDefinesRP2040.h"
#include "LEDs.h"
2023-06-24 12:56:08 -07:00
#include <EEPROM.h>
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
bool debugFP = EEPROM.read(DEBUG_FILEPARSINGADDRESS);
bool debugFPtime = EEPROM.read(TIME_FILEPARSINGADDRESS);
2023-05-31 16:00:30 -07:00
createSafeString(nodeFileString, 1200);
2023-06-13 20:57:04 -07:00
createSafeString(nodeString, 1200);
createSafeString(specialFunctionsString, 800);
2023-06-24 12:56:08 -07:00
char inputBuffer[8000] = {0};
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
ArduinoJson::StaticJsonDocument<8000> wokwiJson;
2023-06-13 20:57:04 -07:00
String connectionsW[MAX_BRIDGES][5];
2023-05-31 16:00:30 -07:00
File nodeFile;
2023-06-13 20:57:04 -07:00
File wokwiFile;
2023-05-31 16:00:30 -07:00
unsigned long timeToFP = 0;
2023-06-13 20:57:04 -07:00
int numConnsJson = 0;
2023-12-15 19:24:36 -08:00
void savePreformattedNodeFile(int source)
2023-06-24 12:56:08 -07:00
{
LittleFS.remove("nodeFile.txt");
nodeFile = LittleFS.open("nodeFile.txt", "w+");
2023-12-16 12:11:15 -08:00
if (debugFP)
{
2023-12-16 12:11:15 -08:00
Serial.print("source = ");
Serial.println(source);
}
2023-12-16 12:11:15 -08:00
if (source == 0)
{
2023-12-16 12:11:15 -08:00
while (Serial.available() == 0)
{
}
while (Serial.available() > 0)
{
nodeFile.write(Serial.read());
delay(1);
}
}
2023-12-16 12:11:15 -08:00
if (source == 1)
{
while (Serial1.available() == 0)
{
}
delayMicroseconds(900);
// Serial.println("waiting for Arduino to send file");
while (Serial1.available() > 0)
{
nodeFile.write(Serial1.read());
delayMicroseconds(900);
// Serial.println(Serial1.available());
}
2023-06-24 12:56:08 -07:00
2023-12-16 12:11:15 -08:00
while (Serial1.available() > 0)
{
Serial1.read();
delay(1);
}
}
2023-12-15 19:24:36 -08:00
nodeFile.close();
2023-06-24 12:56:08 -07:00
}
2023-06-13 20:57:04 -07:00
void parseWokwiFileToNodeFile(void)
{
2023-06-24 12:56:08 -07:00
// delay(3000);
2023-06-13 20:57:04 -07:00
LittleFS.begin();
timeToFP = millis();
wokwiFile = LittleFS.open("wokwi.txt", "w+");
if (!wokwiFile)
{
if (debugFP)
Serial.println("Failed to open wokwi.txt");
return;
}
else
{
if (debugFP)
2023-06-24 12:56:08 -07:00
{
Serial.println("\n\ropened wokwi.txt\n\r");
}
else
{
// Serial.println("\n\r");
}
2023-06-13 20:57:04 -07:00
}
2023-06-24 12:56:08 -07:00
Serial.println("paste Wokwi diagram.json here\n\r");
2023-06-13 20:57:04 -07:00
while (Serial.available() == 0)
{
}
int numCharsRead = 0;
2023-06-24 12:56:08 -07:00
char firstChar = Serial.read();
if (firstChar != '{') // in case you just paste a wokwi file in from the menu, the opening brace will have already been read
{
inputBuffer[numCharsRead] = '{';
numCharsRead++;
}
else
{
inputBuffer[numCharsRead] = firstChar;
numCharsRead++;
}
/*
Serial.println(firstChar);
Serial.println(firstChar);
Serial.println(firstChar);
Serial.println(firstChar);
Serial.println(firstChar);
Serial.print(firstChar);
*/
delay(1);
2023-06-13 20:57:04 -07:00
while (Serial.available() > 0)
{
char c = Serial.read();
inputBuffer[numCharsRead] = c;
numCharsRead++;
2023-06-24 12:56:08 -07:00
delayMicroseconds(1000);
2023-06-13 20:57:04 -07:00
}
createSafeStringFromCharArray(wokwiFileString, inputBuffer);
2023-06-24 12:56:08 -07:00
delay(10);
2023-06-13 20:57:04 -07:00
wokwiFile.write(inputBuffer, numCharsRead);
delay(10);
wokwiFile.seek(0);
2023-06-24 12:56:08 -07:00
if (debugFP)
Serial.println("\n\n\rwokwiFile\n\n\r");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
/* for (int i = 0; i < numCharsRead; i++)
{
Serial.print((char)wokwiFile.read());
}*/
if (debugFP)
2023-06-13 20:57:04 -07:00
{
2023-06-24 12:56:08 -07:00
Serial.print(wokwiFileString);
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.println("\n\n\rnumCharsRead = ");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.print(numCharsRead);
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.println("\n\n\r");
}
2023-06-13 20:57:04 -07:00
wokwiFile.close();
2023-06-24 12:56:08 -07:00
deserializeJson(wokwiJson, inputBuffer);
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
if (debugFP)
{
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.println("\n\n\rwokwiJson\n\n\r");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.println("\n\n\rconnectionsW\n\n\r");
2023-06-13 20:57:04 -07:00
}
2023-06-24 12:56:08 -07:00
numConnsJson = wokwiJson["connections"].size();
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
copyArray(wokwiJson["connections"], connectionsW);
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
// deserializeJson(connectionsW, Serial);
if (debugFP)
{
Serial.println(wokwiJson["connections"].size());
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
for (int i = 0; i < MAX_BRIDGES; i++)
{
// Serial.println(wokwiJson["connections"].size());
if (connectionsW[i][0] == "")
{
break;
}
Serial.print(connectionsW[i][0]);
Serial.print(", \t ");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.print(connectionsW[i][1]);
Serial.print(", \t ");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.print(connectionsW[i][2]);
Serial.print(", \t ");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.print(connectionsW[i][3]);
Serial.print(", \t ");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.print(connectionsW[i][4]);
Serial.print(", \t ");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.println();
}
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.println("\n\n\rRedefining\n\n\r");
}
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
changeWokwiDefinesToJumperless();
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
writeToNodeFile();
// while(1);
openNodeFile();
2023-06-13 20:57:04 -07:00
}
void changeWokwiDefinesToJumperless(void)
{
2023-06-24 12:56:08 -07:00
String connString1 = " ";
String connString2 = " ";
String connStringColor = " ";
String bb = "bb1:";
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
int nodeNumber;
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
for (int i = 0; i < numConnsJson; i++)
2023-06-13 20:57:04 -07:00
{
2023-06-24 12:56:08 -07:00
if (debugFP)
{
Serial.println(' ');
2023-06-24 12:56:08 -07:00
}
for (int j = 0; j < 2; j++)
{
nodeNumber = -1;
connString1 = connectionsW[i][j];
if (debugFP)
{
Serial.print(connString1);
Serial.print(" \t\t ");
}
if (connString1.startsWith("bb1:") || connString1.startsWith("bb2:"))
{
// Serial.print("bb1 or bb2 ");
int periodIndex = connString1.indexOf(".");
connString1 = connString1.substring(4, periodIndex);
if (connString1.endsWith("b"))
{
nodeNumber = 30;
// Serial.println("bottom");
connString1.substring(0, connString1.length() - 1);
nodeNumber += connString1.toInt();
}
else if (connString1.endsWith("t"))
{
nodeNumber = 0;
// Serial.println("top");
connString1.substring(0, connString1.length() - 1);
nodeNumber += connString1.toInt();
}
else if (connString1.endsWith("n"))
{
nodeNumber = GND;
}
else if (connString1.endsWith("p"))
{
nodeNumber = SUPPLY_5V;
}
}
else if (connString1.startsWith("nano:"))
{
// Serial.print("nano\t");
int periodIndex = connString1.indexOf(".");
connString1 = connString1.substring(5, periodIndex);
nodeNumber = NANO_D0;
if (isDigit(connString1[connString1.length() - 1]))
{
nodeNumber += connString1.toInt();
}
else if (connString1.equals("5V"))
{
nodeNumber = SUPPLY_5V;
}
else if (connString1.equalsIgnoreCase("AREF"))
{
nodeNumber = NANO_AREF;
}
else if (connString1.equalsIgnoreCase("GND"))
{
nodeNumber = GND;
}
else if (connString1.equalsIgnoreCase("RESET"))
{
nodeNumber = NANO_RESET;
}
else if (connString1.equalsIgnoreCase("3.3V"))
{
nodeNumber = SUPPLY_3V3;
}
else if (connString1.startsWith("A"))
{
nodeNumber = NANO_A0;
nodeNumber += connString1.toInt();
}
}
else if (connString1.startsWith("vcc1:"))
{
// Serial.print("vcc1\t");
nodeNumber = SUPPLY_5V;
}
else if (connString1.startsWith("vcc2:"))
{
// Serial.print("vcc2\t");
nodeNumber = SUPPLY_3V3;
}
else if (connString1.startsWith("gnd1:"))
{
// Serial.print("gnd1\t");
nodeNumber = GND;
}
else if (connString1.startsWith("gnd2:"))
{
// Serial.print("gnd2\t");
nodeNumber = GND;
}
else if (connString1.startsWith("gnd3:"))
{
nodeNumber = GND;
}
else if (connString1.startsWith("pot1:"))
{
nodeNumber = DAC0_5V;
}
else
{
connectionsW[i][j] = -1;
}
// nodeNumber += connString1.toInt();
connectionsW[i][j] = nodeNumber;
if (debugFP)
{
Serial.print(connectionsW[i][j]);
Serial.print(" \t ");
}
}
2023-06-13 20:57:04 -07:00
}
}
2023-06-24 12:56:08 -07:00
void clearNodeFile(void)
{
LittleFS.remove("nodeFile.txt");
2023-06-13 20:57:04 -07:00
}
void writeToNodeFile(void)
{
LittleFS.remove("nodeFile.txt");
delay(10);
nodeFile = LittleFS.open("nodeFile.txt", "w+");
if (!nodeFile)
{
if (debugFP)
Serial.println("Failed to open nodeFile");
return;
}
else
{
if (debugFP)
Serial.println("\n\rrecreated nodeFile.txt\n\n\rloading bridges from wokwi.txt\n\r");
}
2023-06-24 12:56:08 -07:00
nodeFile.print("{\n\r");
2023-06-13 20:57:04 -07:00
for (int i = 0; i < numConnsJson; i++)
{
if (connectionsW[i][0] == "-1" && connectionsW[i][1] != "-1")
{
lightUpNode(connectionsW[i][0].toInt());
continue;
}
if (connectionsW[i][1] == "-1" && connectionsW[i][0] != "-1")
{
lightUpNode(connectionsW[i][1].toInt());
continue;
}
2023-06-24 12:56:08 -07:00
if (connectionsW[i][0] == connectionsW[i][1])
2023-06-13 20:57:04 -07:00
{
lightUpNode(connectionsW[i][0].toInt());
continue;
}
nodeFile.print(connectionsW[i][0]);
nodeFile.print("-");
nodeFile.print(connectionsW[i][1]);
nodeFile.print(",\n\r");
}
2023-06-24 12:56:08 -07:00
nodeFile.print("}\n\r");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
if (debugFP)
{
Serial.println("wrote to nodeFile.txt");
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
Serial.println("nodeFile.txt contents:");
nodeFile.seek(0);
2023-06-13 20:57:04 -07:00
2023-06-24 12:56:08 -07:00
while (nodeFile.available())
{
Serial.write(nodeFile.read());
}
Serial.println("\n\r");
}
2023-06-13 20:57:04 -07:00
nodeFile.close();
}
2023-05-31 16:00:30 -07:00
void openNodeFile()
{
timeToFP = millis();
2023-06-24 12:56:08 -07:00
2023-05-31 16:00:30 -07:00
nodeFile = LittleFS.open("nodeFile.txt", "r");
if (!nodeFile)
{
2023-06-13 20:57:04 -07:00
if (debugFP)
Serial.println("Failed to open nodeFile");
2023-05-31 16:00:30 -07:00
return;
}
else
{
2023-06-13 20:57:04 -07:00
if (debugFP)
Serial.println("\n\ropened nodeFile.txt\n\n\rloading bridges from file\n\r");
2023-05-31 16:00:30 -07:00
}
nodeFileString.read(nodeFile);
nodeFile.close();
splitStringToFields();
// parseStringToBridges();
}
void splitStringToFields()
{
int openBraceIndex = 0;
int closeBraceIndex = 0;
2023-06-13 20:57:04 -07:00
if (debugFP)
Serial.println("\n\rraw input file\n\r");
if (debugFP)
Serial.println(nodeFileString);
if (debugFP)
Serial.println("\n\rsplitting and cleaning up string\n\r");
if (debugFP)
Serial.println("_");
2023-12-16 12:11:15 -08:00
2023-05-31 16:00:30 -07:00
openBraceIndex = nodeFileString.indexOf("{");
closeBraceIndex = nodeFileString.indexOf("}");
2023-12-16 12:11:15 -08:00
int fIndex = nodeFileString.indexOf("f");
int foundOpenBrace = -1;
int foundCloseBrace = -1;
int foundF = -1;
if (openBraceIndex != -1)
{
foundOpenBrace = 1;
}
if (closeBraceIndex != -1)
{
foundCloseBrace = 1;
}
if (fIndex != -1)
{
foundF = 1;
}
// Serial.println(openBraceIndex);
// Serial.println(closeBraceIndex);
// Serial.println(fIndex);
if (foundF == 1)
{
nodeFileString.substring(nodeFileString, fIndex + 1, nodeFileString.length());
}
if (foundOpenBrace == 1 && foundCloseBrace == 1)
{
nodeFileString.substring(specialFunctionsString, openBraceIndex + 1, closeBraceIndex);
}
else
{
nodeFileString.substring(specialFunctionsString, 0, nodeFileString.length());
}
2023-05-31 16:00:30 -07:00
specialFunctionsString.trim();
2023-06-13 20:57:04 -07:00
if (debugFP)
Serial.println(specialFunctionsString);
if (debugFP)
Serial.println("^\n\r");
/*
nodeFileString.remove(0, closeBraceIndex + 1);
nodeFileString.trim();
openBraceIndex = nodeFileString.indexOf("{");
closeBraceIndex = nodeFileString.indexOf("}");
//nodeFileString.substring(specialFunctionsString, openBraceIndex + 1, closeBraceIndex);
specialFunctionsString.trim();
if(debugFP)Serial.println("_");
if(debugFP)Serial.println(specialFunctionsString);
if(debugFP)Serial.println("^\n\r");
*/
2023-05-31 16:00:30 -07:00
replaceSFNamesWithDefinedInts();
}
void replaceSFNamesWithDefinedInts(void)
{
2023-06-13 20:57:04 -07:00
if (debugFP)
Serial.println("replacing special function names with defined ints\n\r");
2023-06-24 12:56:08 -07:00
2023-05-31 16:00:30 -07:00
specialFunctionsString.replace("GND", "100");
specialFunctionsString.replace("SUPPLY_5V", "105");
specialFunctionsString.replace("SUPPLY_3V3", "103");
specialFunctionsString.replace("DAC0_5V", "106");
specialFunctionsString.replace("DAC1_8V", "107");
specialFunctionsString.replace("I_N", "109");
specialFunctionsString.replace("I_P", "108");
specialFunctionsString.replace("EMPTY_NET", "127");
2023-06-13 20:57:04 -07:00
specialFunctionsString.replace("ADC0_5V", "110");
specialFunctionsString.replace("ADC1_5V", "111");
specialFunctionsString.replace("ADC2_5V", "112");
specialFunctionsString.replace("ADC3_8V", "113");
2023-05-31 16:00:30 -07:00
2023-06-13 20:57:04 -07:00
// if(debugFP)Serial.println(specialFunctionsString);
// if(debugFP)Serial.println("\n\n\r");
2023-05-31 16:00:30 -07:00
2023-06-13 20:57:04 -07:00
replaceNanoNamesWithDefinedInts();
2023-05-31 16:00:30 -07:00
}
2023-06-13 20:57:04 -07:00
void replaceNanoNamesWithDefinedInts(void) // for dome reason Arduino's String wasn't replacing like 1 or 2 of the names, so I'm using SafeString now and it works
2023-05-31 16:00:30 -07:00
{
2023-06-13 20:57:04 -07:00
if (debugFP)
Serial.println("replacing special function names with defined ints\n\r");
2023-06-24 12:56:08 -07:00
2023-05-31 16:00:30 -07:00
char nanoName[5];
itoa(NANO_D10, nanoName, 10);
specialFunctionsString.replace("D10", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D11, nanoName, 10);
specialFunctionsString.replace("D11", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D12, nanoName, 10);
specialFunctionsString.replace("D12", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D13, nanoName, 10);
specialFunctionsString.replace("D13", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D0, nanoName, 10);
specialFunctionsString.replace("D0", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D1, nanoName, 10);
specialFunctionsString.replace("D1", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D2, nanoName, 10);
specialFunctionsString.replace("D2", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D3, nanoName, 10);
specialFunctionsString.replace("D3", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D4, nanoName, 10);
specialFunctionsString.replace("D4", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D5, nanoName, 10);
specialFunctionsString.replace("D5", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D6, nanoName, 10);
specialFunctionsString.replace("D6", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D7, nanoName, 10);
specialFunctionsString.replace("D7", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D8, nanoName, 10);
specialFunctionsString.replace("D8", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_D9, nanoName, 10);
specialFunctionsString.replace("D9", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_RESET, nanoName, 10);
specialFunctionsString.replace("RESET", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_AREF, nanoName, 10);
specialFunctionsString.replace("AREF", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_A0, nanoName, 10);
specialFunctionsString.replace("A0", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_A1, nanoName, 10);
specialFunctionsString.replace("A1", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_A2, nanoName, 10);
specialFunctionsString.replace("A2", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_A3, nanoName, 10);
specialFunctionsString.replace("A3", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_A4, nanoName, 10);
specialFunctionsString.replace("A4", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_A5, nanoName, 10);
specialFunctionsString.replace("A5", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_A6, nanoName, 10);
specialFunctionsString.replace("A6", nanoName);
2023-06-13 20:57:04 -07:00
2023-05-31 16:00:30 -07:00
itoa(NANO_A7, nanoName, 10);
specialFunctionsString.replace("A7", nanoName);
2023-06-13 20:57:04 -07:00
// if(debugFP)Serial.println(bridgeString);
if (debugFP)
Serial.println(specialFunctionsString);
if (debugFP)
Serial.println("\n\n\r");
2023-05-31 16:00:30 -07:00
2023-06-13 20:57:04 -07:00
parseStringToBridges();
2023-05-31 16:00:30 -07:00
}
void parseStringToBridges(void)
{
2023-06-13 20:57:04 -07:00
// int bridgeStringLength = bridgeString.length() - 1;
2023-05-31 16:00:30 -07:00
int specialFunctionsStringLength = specialFunctionsString.length() - 1;
int readLength = 0;
2023-12-16 12:11:15 -08:00
2023-05-31 16:00:30 -07:00
newBridgeLength = 0;
newBridgeIndex = 0;
2023-06-13 20:57:04 -07:00
if (debugFP)
2023-12-16 12:11:15 -08:00
{
2023-06-13 20:57:04 -07:00
Serial.println("parsing bridges into array\n\r");
2023-12-16 12:11:15 -08:00
}
int stringIndex = 0;
char delimitersCh[] = ",- \n\r";
createSafeString(buffer, 10);
createSafeStringFromCharArray(delimiters, delimitersCh);
int doneReading = 0;
2023-05-31 16:00:30 -07:00
2023-12-16 12:11:15 -08:00
for (int i = 0; i <= specialFunctionsStringLength; i++)
2023-05-31 16:00:30 -07:00
{
2023-12-16 12:11:15 -08:00
stringIndex = specialFunctionsString.stoken(buffer, stringIndex, delimiters);
buffer.toInt(path[newBridgeIndex].node1);
2023-06-13 20:57:04 -07:00
2023-12-16 12:11:15 -08:00
if (debugFP)
{
Serial.print("node1 = ");
Serial.println(path[newBridgeIndex].node1);
}
stringIndex = specialFunctionsString.stoken(buffer, stringIndex, delimiters);
buffer.toInt(path[newBridgeIndex].node2);
if (debugFP)
{
Serial.print("node2 = ");
Serial.println(path[newBridgeIndex].node2);
}
2023-05-31 16:00:30 -07:00
2023-12-16 12:11:15 -08:00
readLength = stringIndex;
2023-05-31 16:00:30 -07:00
2023-06-13 20:57:04 -07:00
2023-12-16 12:11:15 -08:00
if (readLength == -1)
{
doneReading = 1;
break;
}
2023-05-31 16:00:30 -07:00
newBridgeLength++;
newBridgeIndex++;
2023-12-16 12:11:15 -08:00
if (debugFP)
2023-06-13 20:57:04 -07:00
{
2023-12-16 12:11:15 -08:00
Serial.print("readLength = ");
Serial.println(readLength);
Serial.print("specialFunctionsString.length() = ");
Serial.println(specialFunctionsString.length());
}
2023-05-31 16:00:30 -07:00
2023-12-16 12:11:15 -08:00
if (debugFP)
Serial.print(newBridgeIndex);
if (debugFP)
Serial.print("-");
if (debugFP)
Serial.println(newBridge[newBridgeIndex][1]);
2023-05-31 16:00:30 -07:00
2023-12-16 12:11:15 -08:00
}
2023-05-31 16:00:30 -07:00
newBridgeIndex = 0;
2023-06-13 20:57:04 -07:00
if (debugFP)
for (int i = 0; i < newBridgeLength; i++)
{
Serial.print("[");
Serial.print(path[newBridgeIndex].node1);
Serial.print("-");
Serial.print(path[newBridgeIndex].node2);
Serial.print("],");
newBridgeIndex++;
}
if (debugFP)
Serial.print("\n\rbridge pairs = ");
if (debugFP)
Serial.println(newBridgeLength);
2023-05-31 16:00:30 -07:00
nodeFileString.clear();
// if(debugFP)Serial.println(nodeFileString);
timeToFP = millis() - timeToFP;
2023-06-24 12:56:08 -07:00
if (debugFPtime)
2023-06-13 20:57:04 -07:00
Serial.print("\n\rtook ");
2023-05-31 16:00:30 -07:00
2023-06-13 20:57:04 -07:00
if (debugFPtime)
Serial.print(timeToFP);
if (debugFPtime)
Serial.println("ms to open and parse file\n\r");
2023-05-31 16:00:30 -07:00
}
2023-06-24 12:56:08 -07:00
2023-12-16 12:11:15 -08:00
int lenHelper(int x)
{
if (x >= 1000000000)
return 10;
if (x >= 100000000)
return 9;
if (x >= 10000000)
return 8;
if (x >= 1000000)
return 7;
if (x >= 100000)
return 6;
if (x >= 10000)
return 5;
if (x >= 1000)
return 4;
if (x >= 100)
return 3;
if (x >= 10)
return 2;
return 1;
}
int printLen(int x)
{
return x < 0 ? lenHelper(-x) + 1 : lenHelper(x);
}
2023-06-24 12:56:08 -07:00
void debugFlagInit(void)
{
2023-06-26 12:23:28 -07:00
if (EEPROM.read(FIRSTSTARTUPADDRESS) == 255)
{
EEPROM.write(FIRSTSTARTUPADDRESS, 0);
EEPROM.write(DEBUG_FILEPARSINGADDRESS, 0);
EEPROM.write(TIME_FILEPARSINGADDRESS, 0);
EEPROM.write(DEBUG_NETMANAGERADDRESS, 0);
EEPROM.write(TIME_NETMANAGERADDRESS, 0);
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSADDRESS, 0);
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSALTADDRESS, 0);
EEPROM.write(DEBUG_LEDSADDRESS, 0);
EEPROM.write(LEDBRIGHTNESSADDRESS, DEFAULTBRIGHTNESS);
EEPROM.write(RAILBRIGHTNESSADDRESS, DEFAULTRAILBRIGHTNESS);
EEPROM.write(SPECIALBRIGHTNESSADDRESS, DEFAULTSPECIALNETBRIGHTNESS);
2023-12-16 12:11:15 -08:00
EEPROM.commit();
delay(5);
}
#ifdef EEPROMSTUFF
debugFP = EEPROM.read(DEBUG_FILEPARSINGADDRESS);
debugFPtime = EEPROM.read(TIME_FILEPARSINGADDRESS);
2023-06-24 12:56:08 -07:00
debugNM = EEPROM.read(DEBUG_NETMANAGERADDRESS);
debugNMtime = EEPROM.read(TIME_NETMANAGERADDRESS);
2023-06-26 12:23:28 -07:00
debugNTCC = EEPROM.read(DEBUG_NETTOCHIPCONNECTIONSADDRESS);
debugNTCC2 = EEPROM.read(DEBUG_NETTOCHIPCONNECTIONSALTADDRESS);
2023-06-26 12:23:28 -07:00
LEDbrightnessRail = EEPROM.read(RAILBRIGHTNESSADDRESS);
LEDbrightness = EEPROM.read(LEDBRIGHTNESSADDRESS);
LEDbrightnessSpecial = EEPROM.read(SPECIALBRIGHTNESSADDRESS);
2023-06-24 12:56:08 -07:00
debugLEDs = EEPROM.read(DEBUG_LEDSADDRESS);
2023-06-24 12:56:08 -07:00
#else
2023-06-26 12:23:28 -07:00
debugFP = 1;
debugFPtime = 1;
debugNM = 1;
debugNMtime = 1;
2023-06-24 12:56:08 -07:00
2023-06-26 12:23:28 -07:00
debugNTCC = 1;
debugNTCC2 = 1;
2023-06-24 12:56:08 -07:00
// debugLEDs = 1;
2023-06-26 12:23:28 -07:00
#endif
2023-06-24 12:56:08 -07:00
if (debugFP != 0 && debugFP != 1)
2023-08-12 15:12:32 -07:00
EEPROM.write(DEBUG_FILEPARSINGADDRESS, 0);
2023-07-05 13:02:54 -07:00
if (debugFPtime != 0 && debugFPtime != 1)
2023-08-12 15:12:32 -07:00
EEPROM.write(TIME_FILEPARSINGADDRESS, 0);
2023-07-05 13:02:54 -07:00
if (debugNM != 0 && debugNM != 1)
2023-08-12 15:12:32 -07:00
EEPROM.write(DEBUG_NETMANAGERADDRESS, 0);
2023-07-05 13:02:54 -07:00
if (debugNMtime != 0 && debugNMtime != 1)
2023-08-12 15:12:32 -07:00
EEPROM.write(TIME_NETMANAGERADDRESS, 0);
2023-07-05 13:02:54 -07:00
if (debugNTCC != 0 && debugNTCC != 1)
2023-08-12 15:12:32 -07:00
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSADDRESS, 0);
2023-07-05 13:02:54 -07:00
if (debugNTCC2 != 0 && debugNTCC2 != 1)
2023-08-12 15:12:32 -07:00
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSALTADDRESS, 0);
2023-07-05 13:02:54 -07:00
if (debugLEDs != 0 && debugLEDs != 1)
2023-08-12 15:12:32 -07:00
EEPROM.write(DEBUG_LEDSADDRESS, 0);
2023-07-05 13:02:54 -07:00
if (LEDbrightnessRail < 0 || LEDbrightnessRail > 200)
{
EEPROM.write(RAILBRIGHTNESSADDRESS, DEFAULTRAILBRIGHTNESS);
2023-07-05 13:02:54 -07:00
LEDbrightnessRail = DEFAULTRAILBRIGHTNESS;
}
if (LEDbrightness < 0 || LEDbrightness > 200)
{
EEPROM.write(LEDBRIGHTNESSADDRESS, DEFAULTBRIGHTNESS);
LEDbrightness = DEFAULTBRIGHTNESS;
}
2023-07-05 13:02:54 -07:00
if (LEDbrightnessSpecial < 0 || LEDbrightnessSpecial > 200)
{
EEPROM.write(SPECIALBRIGHTNESSADDRESS, DEFAULTSPECIALNETBRIGHTNESS);
LEDbrightnessSpecial = DEFAULTSPECIALNETBRIGHTNESS;
}
2023-07-05 13:02:54 -07:00
EEPROM.commit();
delay(5);
2023-06-24 12:56:08 -07:00
}
void debugFlagSet(int flag)
{
int flagStatus;
switch (flag)
{
case 1:
{
flagStatus = EEPROM.read(DEBUG_FILEPARSINGADDRESS);
if (flagStatus == 0)
2023-06-24 12:56:08 -07:00
{
EEPROM.write(DEBUG_FILEPARSINGADDRESS, 1);
2023-06-24 12:56:08 -07:00
debugFP = true;
2023-06-24 12:56:08 -07:00
}
else
{
EEPROM.write(DEBUG_FILEPARSINGADDRESS, 0);
2023-06-24 12:56:08 -07:00
debugFP = false;
2023-06-24 12:56:08 -07:00
}
break;
}
case 2:
{
flagStatus = EEPROM.read(TIME_FILEPARSINGADDRESS);
if (flagStatus == 0)
2023-06-24 12:56:08 -07:00
{
EEPROM.write(TIME_FILEPARSINGADDRESS, 1);
2023-06-24 12:56:08 -07:00
debugFPtime = true;
2023-06-24 12:56:08 -07:00
}
else
{
EEPROM.write(TIME_FILEPARSINGADDRESS, 0);
2023-06-24 12:56:08 -07:00
debugFPtime = false;
2023-06-24 12:56:08 -07:00
}
break;
}
case 3:
{
flagStatus = EEPROM.read(DEBUG_NETMANAGERADDRESS);
if (flagStatus == 0)
2023-06-24 12:56:08 -07:00
{
EEPROM.write(DEBUG_NETMANAGERADDRESS, 1);
2023-06-24 12:56:08 -07:00
debugNM = true;
2023-06-24 12:56:08 -07:00
}
else
{
EEPROM.write(DEBUG_NETMANAGERADDRESS, 0);
2023-06-24 12:56:08 -07:00
debugNM = false;
2023-06-24 12:56:08 -07:00
}
break;
}
case 4:
{
flagStatus = EEPROM.read(TIME_NETMANAGERADDRESS);
if (flagStatus == 0)
2023-06-24 12:56:08 -07:00
{
EEPROM.write(TIME_NETMANAGERADDRESS, 1);
2023-06-24 12:56:08 -07:00
debugNMtime = true;
2023-06-24 12:56:08 -07:00
}
else
{
EEPROM.write(TIME_NETMANAGERADDRESS, 0);
2023-06-24 12:56:08 -07:00
debugNMtime = false;
2023-06-24 12:56:08 -07:00
}
break;
}
case 5:
{
flagStatus = EEPROM.read(DEBUG_NETTOCHIPCONNECTIONSADDRESS);
if (flagStatus == 0)
2023-06-24 12:56:08 -07:00
{
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSADDRESS, 1);
2023-06-24 12:56:08 -07:00
debugNTCC = true;
2023-06-24 12:56:08 -07:00
}
else
{
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSADDRESS, 0);
2023-06-24 12:56:08 -07:00
debugNTCC = false;
2023-06-24 12:56:08 -07:00
}
break;
}
case 6:
{
flagStatus = EEPROM.read(DEBUG_NETTOCHIPCONNECTIONSALTADDRESS);
if (flagStatus == 0)
2023-06-24 12:56:08 -07:00
{
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSALTADDRESS, 1);
2023-06-24 12:56:08 -07:00
debugNTCC2 = true;
2023-06-24 12:56:08 -07:00
}
else
{
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSALTADDRESS, 0);
2023-06-24 12:56:08 -07:00
debugNTCC2 = false;
2023-06-24 12:56:08 -07:00
}
break;
}
case 7:
{
flagStatus = EEPROM.read(DEBUG_LEDSADDRESS);
if (flagStatus == 0)
2023-06-24 12:56:08 -07:00
{
EEPROM.write(DEBUG_LEDSADDRESS, 1);
2023-06-24 12:56:08 -07:00
debugLEDs = true;
2023-06-24 12:56:08 -07:00
}
else
{
EEPROM.write(DEBUG_LEDSADDRESS, 0);
2023-06-24 12:56:08 -07:00
debugLEDs = false;
2023-06-24 12:56:08 -07:00
}
break;
}
case 0:
{
EEPROM.write(DEBUG_FILEPARSINGADDRESS, 0);
EEPROM.write(TIME_FILEPARSINGADDRESS, 0);
EEPROM.write(DEBUG_NETMANAGERADDRESS, 0);
EEPROM.write(TIME_NETMANAGERADDRESS, 0);
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSADDRESS, 0);
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSALTADDRESS, 0);
EEPROM.write(DEBUG_LEDSADDRESS, 0);
debugFP = false;
debugFPtime = false;
debugNM = false;
debugNMtime = false;
debugNTCC = false;
debugNTCC2 = false;
debugLEDs = false;
break;
}
case 9:
{
EEPROM.write(DEBUG_FILEPARSINGADDRESS, 1);
EEPROM.write(TIME_FILEPARSINGADDRESS, 1);
EEPROM.write(DEBUG_NETMANAGERADDRESS, 1);
EEPROM.write(TIME_NETMANAGERADDRESS, 1);
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSADDRESS, 1);
EEPROM.write(DEBUG_NETTOCHIPCONNECTIONSALTADDRESS, 1);
EEPROM.write(DEBUG_LEDSADDRESS, 1);
debugFP = true;
debugFPtime = true;
debugNM = true;
debugNMtime = true;
debugNTCC = true;
debugNTCC2 = true;
debugLEDs = true;
break;
}
}
2023-07-05 13:02:54 -07:00
delay(4);
2023-06-24 12:56:08 -07:00
EEPROM.commit();
2023-07-05 13:02:54 -07:00
delay(8);
return;
2023-06-24 12:56:08 -07:00
}
void runCommandAfterReset(char command)
{
if (EEPROM.read(CLEARBEFORECOMMANDADDRESS) == 1)
{
return;
}
else
{
EEPROM.write(CLEARBEFORECOMMANDADDRESS, 1);
EEPROM.write(LASTCOMMANDADDRESS, command);
EEPROM.commit();
digitalWrite(RESETPIN, HIGH);
delay(1);
digitalWrite(RESETPIN, LOW);
AIRCR_Register = 0x5FA0004; // hard reset
}
}
char lastCommandRead(void)
{
Serial.print("last command: ");
Serial.println((char)EEPROM.read(LASTCOMMANDADDRESS));
return EEPROM.read(LASTCOMMANDADDRESS);
}
void lastCommandWrite(char lastCommand)
{
EEPROM.write(LASTCOMMANDADDRESS, lastCommand);
}