Ok guys, here is my current code. It is working more or less the way I wanted it too. I am sure you will see mistakes and improvements This is an eeprom manager for a DIY Guitar effects switcher that myself and a friend develop for free so that poor musicians like us donât have to spend hundreds of $ on overpriced commercial products. And we also have fun doing this of course.
Thank you for your comments and help.
import controlP5.*;
ControlP5 cp5;
DropdownList d1;
DropdownList d2;
PFont font, font1, font2;
import processing.serial.*; //import the Serial library
Serial myPort;
PrintWriter output;
String EepromData[];
String val;
String End;
String portName;
String SaveData;
int b = 0;
int ItemNr = 22;
int StartTimer = 0;
int serialListIndex;
int EEpromSize_iL7 = 4096;
int EEpromSize_iL8 = 16384;
boolean Restoring = false;
boolean WriteFinished = false;
boolean InvalidEeprom = false;
boolean EditType = false;
boolean EditCom = false;
boolean DataCheck = false;
boolean SerialCheck = true;
boolean DumpActive = false;
boolean RestoreCheck = false;
void setup() {
clear();
size(250, 250); // Create Window
cp5 = new ControlP5(this);
font = createFont("Arial Bold", 20);
font1 = createFont("Arial Bold", 10);
font2 = createFont("Arial Bold", 13);
// frameRate(60);
End = "END";
// Add Button
cp5.addButton("Restore") // "Start" is the name of the Button
.setPosition(86, 115) // x and y coordinates of upper left corner of Button
.setSize(80, 20) // (Width, Height)
.setFont(font1)
.setColorBackground(color(216, 216, 216))
.setColorForeground(color(232, 232, 232))
.setColorActive(color(186, 188, 188))
.setColorLabel(color(0, 0, 0))
;
cp5.addButton("Dump") // "Start" is the name of the Button
.setPosition(86, 155) // x and y coordinates of upper left corner of Button
.setSize(80, 20) // (Width, Height)
.setFont(font1)
.setColorBackground(color(216, 216, 216))
.setColorForeground(color(232, 232, 232))
.setColorActive(color(186, 188, 188))
.setColorLabel(color(0, 0, 0))
;
d1 = cp5.addDropdownList("myList-d1")
.setPosition(178, 76)
.setSize(53, 16)
.setBarHeight(16)
.setHeight(120)
.setItemHeight(16)
.setFont(font1)
.setColorBackground(color(216, 216, 216))
.setColorForeground(color(232, 232, 232))
.setColorActive(color(186, 188, 188))
.setColorLabel(color(0, 0, 0))
.setColorValueLabel(color(0, 0, 0))
.setColorCaptionLabel(color(0, 0, 0))
.setOpen(false);
;
d1.getCaptionLabel().set("COM1"); //set PORT before anything is selected
portName = Serial.list()[0]; //0 as default
myPort = new Serial(this, portName, 115200);
myPort.bufferUntil('\n');
// create a DropdownList,
d2 = cp5.addDropdownList("myList-d2")
.setPosition(178, 50)
.setSize(53, 16)
.setOpen(false);
;
customize(d2); // customize the first list
}
void customize(DropdownList d2) {
// a convenience function to customize a DropdownList
d2.setColorBackground(color(216, 216, 216));
d2.setItemHeight(16);
d2.setBarHeight(16);
d2.setHeight(64);
d2.setFont(font1);
d2.setColorForeground(color(232, 232, 232));
d2.setColorActive(color(186, 188, 188));
d2.setColorLabel(color(0, 0, 0));
d2.setColorValueLabel(color(0, 0, 0));
d2.setColorCaptionLabel(color(0, 0, 0));
d2.getCaptionLabel().set("- - - - - -");
}
void draw() {
background(0, 120, 255); // Background Color (r, g, b) or (0 to 255)
textFont(font);
fill(255, 255, 255); // text color (r, g, b)
text("MANAGE EEPROM", 34, 28); // ("text", x coordinate, y coordinate)
textFont(font2);
text("Select iLoopino Version", 18, 62); // ("text", x coordinate, y coordinate)
text("Select Serial COM Port", 18, 88); // ("text", x coordinate, y coordinate)
if (WriteFinished) {
textFont(font);
text("Finished", 83, 210);
if (StartTimer + 5000 < millis()) {
WriteFinished = false;
}
}
if (InvalidEeprom) {
textFont(font);
text("Invalid EEprom Data", 28, 210);
if (StartTimer + 2000 < millis()) {
InvalidEeprom = false;
}
}
if ((!SerialCheck) && (DumpActive) && (SaveData != null)) {
textFont(font);
text("Dumping EEprom", 40, 210);
}
if (d1.isMouseOver()) {
EditCom = true;
d1.clear(); //Delete all the items
for (int i=0; i<Serial.list().length; i++) {
d1.addItem(Serial.list()[i], i); //add the items in the list
}
} else {
EditCom = false;
}
if (d2.isMouseOver()) {
EditType = true;
d2.clear(); //Delete all the items
d2.addItem("iL 7.x", 0);
d2.addItem("iL 8.x", 1);
EditType = true;
} else {
EditType = false;
}
if (RestoreCheck) {
if ((ItemNr < 2) && (!DumpActive)) {
if ((EepromData[0].equals(val) == true) && (DataCheck)) { // Compare two Strings
for (int i = 1; i < EepromData.length; i++) {
// println(EepromData[i]);
myPort.write(EepromData[i]);
myPort.write('\n');
delay(2); //this will be the tempo?
}
WriteFinished();
} else {
StartTimer = millis();
InvalidEeprom = true;
}
DataCheck = false;
RestoreCheck = false;
}
}
}
void serialEvent(Serial myP) {
if (DumpActive) {
SaveData = myP.readString();
SaveData = SaveData.trim();
if ("END".equals(SaveData)) {
SerialCheck = true;
}
if (!SerialCheck) {
output.println(SaveData);
}
// println(SaveData); //read until new input
SaveData = "";
if (SerialCheck) {
output.flush(); // Write the remaining data
output.close(); // Finish the file
myPort.clear(); //delete the port
SerialCheck = false;
DumpActive = false;
WriteFinished();
}
}
}
void controlEvent(ControlEvent theEvent) { //when something in the list is selected
if (EditCom) {
myPort.clear(); //delete the port
myPort.stop(); //stop the port
if (theEvent.isController()) {
portName = Serial.list()[int(theEvent.getController().getValue())]; //port name is set to the selected port in the dropDownMeny
myPort = new Serial(this, portName, 115200); //Create a new connection
myPort.bufferUntil('\n');
delay(200);
}
}
if (EditType) {
ItemNr = int(theEvent.getController().getValue());
if (ItemNr == 0) {
EepromData = loadStrings("data/EEprom_iL7.il7");
val = "EEprom_iL7";
if (EepromData.length == EEpromSize_iL7 + 1) {
DataCheck = true;
}
} else if (ItemNr == 1) {
EepromData = loadStrings("data/EEprom_iL8.il8");
val = "EEPROM_iL8";
if (EepromData.length == EEpromSize_iL8 + 1) {
DataCheck = true;
}
}
}
}
void Dump() {
if (ItemNr == 0) {
output = createWriter("EEprom_iL7.il7");
} else if (ItemNr == 1) {
output = createWriter("EEprom_iL8.il8");
}
DumpActive = true;
SerialCheck = false;
}
void Restore() {
textFont(font);
text("Restoring EEprom", 36, 210);
RestoreCheck = true;
}
void WriteFinished() {
StartTimer = millis();
WriteFinished = true;
}