Yet another "unexpected token: void" question
in
Programming Questions
•
4 months ago
Hello all. I'm new to this forum, but I've been using Processing for a couple of months now. This is my first project, and it involves a lot of different things, but basically it takes data from the serial port(s) and plots it on the screen. Unfortunately I've stumbled upon an unexpected token: void error which is driving me mad. I've checked semi-colons and curly braces until my eyes bleed, and I can't see the error. Which indicates that my code is probably quite badly written... Speaking of which, any tips for cleaning it up and making it neater to work with are welcome. The comments are mostly from older versions and have suffered a fair bit of splicing, I'm afraid... The error points me to line 112 (the definition of the function "shell") but as I say I can't see anything missing prior to that which could be causing this. Any help is appreciated.
- /*
- Yes, this code is awful. Do not read this code if inefficiency,
- redundancy, inconsistency, redundancy and inefficiency offend you.
- It's a work in progress, poorly documented.
- */
- int boxw = 100;
- int boxh = 100;
- int textx;
- int texty;
- String display0 = "0.0993";
- String display1 = "1.297";
- int focus = 0;
- int posx;
- int posy;
- float displayOut0 = 0.0993;
- float displayOut1 = 1.297;
- import javax.swing.JFrame;
- PFrame f;
- secondApplet s;
- /*-------------------------------------------------------------------------------*/
- int i = 75;
- int x = 0;
- int screens = 0;
- int serialIndex;
- /*Initialise integers.
- i = frame counter to check when to clear and move to a new
- screen.
- x = point counter used as x-co-ordinate when multiplied by
- time step
- screens = number of screens used so far. Used to determine
- maximum value of x for current screen.
- serialIndex = position of first USB entry in serial port
- list.*/
- float updateTime;
- float y1;
- float y2;
- String y3;
- float output1;
- float output2;
- float output3;
- /*Initialise floats.
- updateTime = timestep per serial communication,provided by
- the serial device as 4th element of each data list.
- y1 = the raw data taken from the first element of each data
- list.
- y2 = the raw data taken from the second element of each data
- list.
- y3 = the raw data taken from the third element of each data
- list.
- output1 = y1 adjusted to represent a co-ordinate relative to
- the axes on the canvas.
- output2 = y2 adjusted to represent a co-ordinate relative to
- the axes on the canvas.
- output3 = y3 adjusted to represent a co-ordinate relative to
- the axes on the canvas.*/
- String general = "/home/half/ABonding/Sketches/SDP4/Logger/";
- String[] val;
- String unique;
- /*Initialise strings.
- general = directory in which to save data. This is a subdir
- of the application itself.
- val = string array derived from the data list provided at
- each serial event.*/
- PrintWriter output;
- /*Initialise PrintWriter.
- output = datastream used to write arrays to a file.*/
- boolean saveOver = false;
- boolean exitOver = false;
- boolean autoOver;
- boolean autoExport = false;
- /*Initialise booleans.
- saveOver = variable to test for mouseover "save" button.
- exitOver = variable to test for mouseover "exit" button.*/
- import processing.serial.*;
- Serial myPort;
- /*Import serial library and start "myPort" serial variable.*/
- import java.net.*;
- import java.io.*;
- import java.util.*;
- /*Import generic java libs "just in case" and for bash interface.*/
- /*-------------------------------------------------------------------------------*/
- String[] runTC = {
- "/bin/bash",
- "-c",
- "ipython -noconfirm_exit ~/ABonding/wrapper.py"
- };
- String pipeOut;
- void shell(String[] params) {
- try {
- String line;
- OutputStream stdin = null;
- Process p = Runtime.getRuntime().exec(params);
- BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
- while ( (line = input.readLine ()) != null) {
- pipeOut = line;
- }
- input.close();
- }
- catch (Exception err) {
- err.printStackTrace();
- }
- }
- String repString = "";
- void checkUser() {
- String returnedValues;
- String[] cmd = {
- "whoami"
- };
- try {
- Process p = Runtime.getRuntime().exec(cmd);
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
- while ( (returnedValues = stdInput.readLine ()) != null) {
- String[] folders = split(general, "/");
- folders[2] = returnedValues;
- for (int i = 0; i < folders.length; i++) {
- repString += folders[i] + "/";
- }
- repString = repString.substring(0, repString.length()-1);
- }
- }
- catch (Exception err) {
- err.printStackTrace();
- }
- general = repString;
- }
- void drawAxes() {
- textSize(24);
- text("Bonding Monitor", 400, 50); //Set up text parameters
- textSize(16);
- initButtons();
- grid();
- line(75, 90, 75, 602);
- line(75, 602, 775, 602); //Draw axes.
- text("Time (s)", 415, 635); //Label x-axis.
- pushMatrix();
- translate(25, 356);
- rotate(radians(270));
- text("Signal (bits)", 0, 0);
- popMatrix(); //Label y-axis.
- for (int k = 602; k >= 90; k -= 32) {
- text((602 - k)*2, 50, k); //Set yaxis ticks.
- }
- line(750, 15, 760, 15);
- text("Current", 695, 20); //Generate legend.
- text("Voltage", 695, 50);
- text("Temperature", 695, 80);
- stroke(0, 0, 255);
- line(750, 75, 760, 75);
- stroke(255, 0, 0);
- line(750, 45, 760, 45);
- text("I: ", 525, 30);
- text("V: ", 525, 50);
- text("T: ", 525, 70);
- //Previous line generates value boxes for instant measurement.
- stroke(0);
- fill(255);
- }
- void grid() {
- stroke(192); //Set stroke to light grey.
- for (int i = 90; i < 571; i += 32) {
- line(75, i, 775, i); //Generate y-axis (horizontal) grid.
- }
- for (int i = 75; i < 776; i += 100) {
- line(i, 90, i, 602); //Generate x-axis (vertical) grid.
- }
- stroke(0); //Reset stroke for next function to use.
- }
- void saveGraph() {
- String[] datetime= new String[6];
- datetime[0] = str(year());
- datetime[1] = str(month());
- datetime[2] = str(day());
- datetime[3] = str(hour());
- datetime[4] = str(minute());
- datetime[5] = str(second());
- for (int i=0; i<datetime.length; i++) {
- if (datetime[i].length() == 1) {
- datetime[i] = "0" + datetime[i];
- }
- }
- unique = general + datetime[0] + "-" + datetime[1] + "-" + datetime[2] + "/" + datetime[3] + ":" + datetime[4] + ":" + datetime[5] + ".png";
- save(unique);
- println("Graph output to " + unique);
- }
- /*saveGraph() generates a unique path for graphic (within Logger/)
- and puts it there, confirming completion.*/
- void exportData() {
- if (autoExport == true) {
- String s_x = "" + x*updateTime/1000;
- String s_y1 = "" + y1;
- String s_y2 = "" + y2;
- String s_y3 = "" + y3;
- String sep = "\t";
- String outString = s_x + sep + s_y1 + sep + s_y2 + sep + s_y3;
- output.println(outString);
- }
- }
- /*exportData() generates a unique path for the datafile (same dir
- as saveGraph(), usually, uses output to write xData and the yDatas
- to it, then closes, confirming.*/
- void switchAuto() {
- if (autoExport == false) {
- fill(45);
- rect(133, 58, 8, 8);
- autoExport = true;
- fill(0);
- println("Autosave on");
- }
- else {
- fill(255);
- rect(133, 58, 8, 8);
- autoExport = false;
- fill(0);
- println("Autosave off");
- }
- }
- void initButtons() {
- fill(200); //Set fill to light grey, then draw buttons.
- rect(20, 15, 100, 25);
- rect(20, 50, 100, 25);
- fill(0); //Reset fill to write text, then write text.
- text("Snapshot", 70, 33);
- text("Exit", 70, 68);
- text("Autoexport data", 217, 68);
- if (autoExport == true) {
- fill(45);
- rect(132, 57, 10, 10);
- }
- else {
- fill(255);
- rect(132, 57, 10, 10);
- }
- stroke(0);
- fill(0);
- }
- boolean overSave() {
- if (mouseX >= 20 && mouseX <= 120 && //Test whether mouse hovering
- mouseY >= 15 && mouseY <= 40) { //over "Save" button.
- return true;
- }
- else {
- return false;
- }
- }
- boolean overExit() {
- if (mouseX >= 20 && mouseX <= 120 && //Test whether mouse hovering
- mouseY >= 50 && mouseY <= 75) { //over "Exit" button.
- return true;
- }
- else {
- return false;
- }
- }
- boolean overAuto() {
- if (mouseX >= 132 && mouseX <= 142 && //Test whether mouse hovering
- mouseY >= 57 && mouseY <= 67) { //over button.
- return true;
- }
- else {
- return false;
- }
- }
- void update() { //Update the "____Over" bools to current
- if (overSave()) { //button state.
- saveOver = true;
- }
- else if (overExit()) {
- exitOver = true;
- }
- else if (overAuto()) {
- autoOver = true;
- }
- else {
- autoOver = saveOver = exitOver = false;
- }
- }
- void mousePressed() {
- if (saveOver) {
- saveGraph(); //On a mouse event over the "Save"
- } //button, call saveGraph().
- if (autoOver) {
- switchAuto();
- }
- if (exitOver) {
- output.flush();
- output.close();
- println("Data output to " + unique);
- File file = new File(unique);
- if (file.length() == 0) {
- println("File empty!");
- if (file.delete()) {
- println("Cleaned up.");
- }
- else {
- println("Could not delete file.");
- }
- }
- exit();
- }
- }
- void reset(int q) {
- background(255);
- drawAxes(); //Clear the screen and re-draw axes.
- }
- /*-------------------------------------------------------------------------------*/
- void setup() {
- checkUser();
- PFont fnt;
- fnt = createFont("Arial", 16, true); //Set up text
- textFont(fnt);
- textAlign(CENTER);
- stroke(0); //Set stroke, fill, just in case.
- fill(0);
- size(800, 640);
- background(255); //Default window size 800x640, white bg.
- drawAxes(); //Does what it says on the tin.
- String portName = "/dev/ttyUSB0";
- myPort = new Serial(this, portName, 115200);
- myPort.bufferUntil('\n'); //These two lines set up the serial port
- println(portName); //to communicate at 115200 baud with the
- //usual linebreak char and confirm chosen port.
- String[] datetime= new String[6];
- datetime[0] = str(year());
- datetime[1] = str(month());
- datetime[2] = str(day());
- datetime[3] = str(hour());
- datetime[4] = str(minute());
- datetime[5] = str(second());
- for (int i=0; i<datetime.length; i++) {
- if (datetime[i].length() == 1) {
- datetime[i] = "0" + datetime[i];
- }
- }
- unique = general + datetime[0] + "-" + datetime[1] + "-" + datetime[2] + "/" + datetime[3] + ":" + datetime[4] + ":" + datetime[5] + ".csv";
- output = createWriter(unique);
- PFrame f = new PFrame();
- s.textAlign(CENTER);
- s.size(boxw, boxh);
- s.background(255);
- s.noFill();
- }
- void keyReleased() {
- fill(0);
- if (key >= '0' && key <= '9' || key == '.') {
- posx = boxw/2;
- if (focus == 0) {
- s.fill(255);
- s.rect(1, 20, 99, 19);
- s.fill(0);
- posy = 20 + boxh/8 + 4;
- if (display0.length() < 9) {
- display0 += str(key);
- displayOut0 = parseFloat(display0);
- s.text(display0, posx, posy);
- }
- }
- else if (focus == 1) {
- s.fill(255);
- s.rect(1, 50, 99, 19);
- s.fill(0);
- posy = 10 + boxh/2 + 4;
- if (display1.length() < 9) {
- display1 += str(key);
- displayOut1 = parseFloat(display1);
- s.text(display1, posx, posy);
- }
- }
- }
- else if (keyCode == 9) {
- if (focus < 1) {
- focus += 1;
- }
- else {
- focus = 0;
- }
- if (focus == 0) {
- s.noFill();
- s.stroke(255, 0, 0);
- s.rect(1, 20, 99, 19);
- s.stroke(0);
- }
- if (focus == 1) {
- s.noFill();
- s.stroke(255, 0, 0);
- s.rect(1, 50, 99, 19);
- s.stroke(0);
- }
- }
- else if (keyCode == 8) {
- if (focus == 0) {
- posy = 20 + boxh/8 + 4;
- s.fill(255);
- s.rect(1, 20, 99, 19);
- s.fill(0);
- if (display0.length() > 0) {
- display0 = display0.substring(0, display0.length()-1);
- displayOut0 = parseFloat(display0);
- }
- else {
- displayOut0 = 0;
- }
- s.text(display0, posx, posy);
- }
- if (focus == 1) {
- posy = 10 + boxh/2 + 4;
- s.fill(255);
- s.rect(1, 50, 99, 19);
- s.fill(0);
- if (display1.length() > 0) {
- display1 = display1.substring(0, display1.length()-1);
- displayOut1 = parseFloat(display1);
- }
- else {
- displayOut1 = 0;
- }
- s.text(display1, posx, posy);
- }
- }
- println(str(displayOut0) + ", " + str(displayOut1);
- s.noFill();
- }
- void draw() {
- s.stroke(0);
- s.text("Calibration", boxw/2, 10);
- s.fill(0);
- s.noFill();
- if (focus == 0) {
- s.stroke(255, 0, 0);
- }
- s.rect(1, 20, 99, 19);
- s.stroke(0);
- if (focus == 1) {
- s.stroke(255, 0, 0);
- }
- s.rect(1, 50, 99, 19);
- s.stroke(0);
- update(); //each loop. Namely: update the buttons to appropriate
- stroke(0); //state.
- if (saveOver) {
- fill(230);
- text("Snapshot", 70, 33);
- }
- else if (exitOver) {
- fill(230);
- text("Exit", 70, 68);
- }
- else {
- initButtons();
- }
- }
- void serialEvent(Serial myPort) {
- val = split(myPort.readString(), ","); //Read and process datalist.
- fill(0);
- if (i == 75) {
- for (int n = 75; n < 776; n += 100) {
- updateTime = float(val[2]);
- text(str(updateTime*(screens*700 + n - 75)/1000), n, 620);
- } //The above updates the x-axis ticks with the correct timing.
- }
- shell(runTC);
- y1 = float(val[0]);
- y2 = float(val[1]);
- y3 = pipeOut;
- fill(255);
- stroke(255);
- rect(540, 10, 70, 70);
- stroke(0);
- fill(0);
- text("I: ", 525, 30);
- text("V: ", 525, 50);
- text("T: ", 525, 70);
- if (i < 776) {
- output1 = 602 - y1/2;
- if (output1 < 90) { //Limiters clip to max & min of axis.
- output1 = 90;
- }
- if (output1 > 602) {
- output1 = 602;
- }
- ellipse(i, output1, 1, 1); //Each ellipse is one data point
- output2 = 602 - y2/2; //of the appropriate colour.
- if (output2 < 90) {
- output2 = 90;
- }
- if (output2 > 602) {
- output2 = 602;
- }
- stroke(255, 0, 0);
- ellipse(i, output2, 1, 1);
- output3 = 602 - (1024*float(y3)/600)/2;
- if (output3 < 90) {
- output3 = 90;
- }
- if (output3 > 602) {
- output3 = 602;
- }
- stroke(0, 0, 255);
- ellipse(i, output3, 1, 1);
- x += 1;
- i += 1;
- stroke(0);
- fill(0);
- }
- else {
- screens += 1;
- reset(screens); //Wipe the screen and reset iteration counter.
- i = 75;
- }
- y1 = float(val[0])*displayOut0;
- y2 = float(val[1])*displayOut1;
- text(nf(y1, 4, 2), 575, 30);
- text(nf(y2, 4, 2), 575, 50);
- text(y3, 575, 70);
- print("Current: " + str(y1) + "\n" + "Voltage: " + str(y2) + "\n" + "Temperature: " + y3 + "\n\n");
- exportData(); //and export data to a .csv in the dir.
- }
- public class PFrame extends JFrame {
- public PFrame() {
- setBounds(100, 100, boxw + 4, boxh + 31);
- s = new secondApplet();
- add(s);
- s.init();
- show();
- }
- }
- public class secondApplet extends PApplet {
- public void setup() {
- // size(400, 300);
- // noLoop();
- }
- public void draw() {
- }
- }
1