From 16b32f572beb8a585b385bb760fbcca2de26afbe Mon Sep 17 00:00:00 2001 From: Ashley Date: Sat, 4 Apr 2015 03:31:01 -0400 Subject: [PATCH] Fixed bugs, but still can't test with EEG --- LampDemo.java | 125 ++++++++++++++++++++------------------------------ 1 file changed, 50 insertions(+), 75 deletions(-) diff --git a/LampDemo.java b/LampDemo.java index e9033d1..c0f538e 100644 --- a/LampDemo.java +++ b/LampDemo.java @@ -8,13 +8,13 @@ import arduino.Arduino; public class LampDemo { + private static boolean LAMP_ON = false; public static void main(String[] args) throws UnknownHostException, IOException, JSONException { - Arduino ard = new Arduino("/dev/tty0"); - boolean LAMP_ON = false; - + double threshold = 0.5; + Arduino ard = new Arduino("/dev/cu.usbserial-AD01VFL0"); //connect the Arduino ard.connect(); - String param; + String params; String JSONResponse; BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); @@ -24,48 +24,57 @@ public static void main(String[] args) throws UnknownHostException, IOException, t.start(); //connect to the EPOC server socket - Socket clientSocket = new Socket("localhost", 3333); + Socket clientSocket = new Socket("localhost", 2222); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); //get user param and use that to turn on the lamp and send to server - System.out.println("Enter EEG event to control lamp: "); + System.out.println("Enter 2 EEG events to control lamp (separated by commas): "); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - param = inFromUser.readLine(); - outToServer.writeBytes(param + '\n'); + params = inFromUser.readLine(); + outToServer.writeBytes(params + '\n'); + String[] tokens = params.split(", "); - //fix this to terminate nicely - while(ard.isConnected()) { + //fix this to terminate nicely, current setup causes stale lockfile + if(ard.isConnected()) { while ((JSONResponse = inFromServer.readLine()) != null) { JSONObject obj = new JSONObject(JSONResponse); //System.out.println(obj); //debug - if (API_Main.getAffectivMap().containsKey(param)){ - JSONArray affectiv = obj.getJSONObject("EmoStateData").getJSONArray("Affectiv"); - for (int i = 0; i < affectiv.length(); i++) { - double param_val = affectiv.getJSONObject(i).getDouble(param); - if (param_val > 0.5) { - //turn on lamp - lampSwitch(ard, LAMP_ON); + for (String token : tokens) { + //for expressiv and affectiv events, which are contained in JSONArrays + if (API_Main.getAffectivMap().containsKey(token) || API_Main.getExpressivMap().containsKey(token)){ + JSONArray array = (API_Main.getAffectivMap().containsKey(token)) ? + obj.getJSONObject("EmoStateData").getJSONArray("Affectiv") : + obj.getJSONObject("EmoStateData").getJSONArray("Expressiv"); + for (int i = 0; i < array.length(); i++) { + double param_val = array.getJSONObject(i).getDouble(token); + if (param_val > threshold && token == tokens[0]) { + //turn on lamp + lampSwitch(ard, LAMP_ON); + } + else if (param_val > threshold && token == tokens[1]) { + //turn off lamp + lampSwitch(ard, LAMP_ON); + } } } - } - else if (API_Main.getExpressivMap().containsKey(param)){ - JSONArray expressiv = obj.getJSONObject("EmoStateData").getJSONArray("Expressiv"); - for (int i = 0; i < expressiv.length(); i++) { - double param_val = expressiv.getJSONObject(i).getDouble(param); - if (param_val > 0.5) { - //turn on lamp - lampSwitch(ard, LAMP_ON); + //for cognitiv events, which are contained in a JSONObject + else if (API_Main.getCogntivMap().containsKey(token)) { + String cog_action = obj.getJSONObject("EmoStateData").getString("Cognitiv"); + if (cog_action.equals(token)) { + double param_val = obj.getJSONObject("EmoStateData").getDouble("Cognitiv"); + if (param_val > threshold && token == tokens[0]) { + //turn on lamp + lampSwitch(ard, LAMP_ON); + } + else if (param_val > threshold && token == tokens[1]) { + //turn off lamp + lampSwitch(ard, LAMP_ON); + } } } - } - else { - String cog_action = obj.getJSONObject("EmoStateData").getString("Cognitiv"); - if (cog_action.equals(param)) { - double param_val = obj.getJSONObject("EmoStateData").getDouble("Cognitiv"); - if (param_val > 0.5) { - //turn on lamp - lampSwitch(ard, LAMP_ON); - } + else { + System.out.println("Received bad input, enter a valid EEG event."); + continue; } } } @@ -75,8 +84,7 @@ else if (API_Main.getExpressivMap().containsKey(param)){ inFromUser.close(); inFromServer.close(); outToServer.close(); - ard.disconnect(); //makes no sense here - + ard.disconnect(); //makes no sense to include this right now } catch (Exception e) { System.out.println("Could not start EPOC data server socket."); e.printStackTrace(); @@ -84,48 +92,15 @@ else if (API_Main.getExpressivMap().containsKey(param)){ } public static void lampSwitch(Arduino ard, boolean lampON) { - if (lampON) - ard.digitalWrite(13, true); - else + if (lampON) { + System.out.println("Shutting off lamp..."); ard.digitalWrite(13, false); - } - - private static void testAnalogWrite(Arduino ard) { - for(int n=0; n<255; n++) { - System.out.println(n); - ard.analogWrite(3, n); - sleep(10); - } - } - - private static void testAnalogRead(Arduino ard) { - for(int n=0; n<100; n++) { - System.out.println(ard.analogRead(5)); - sleep(100); + LAMP_ON = !LAMP_ON; } - } - - private static void testDigitalRead(Arduino ard) { - ard.pinMode(10, 'I'); - System.out.println(ard.digitalRead(10)); - } - - private static void testDigitalWrite(Arduino ard) { - ard.pinMode(13, 'O'); - for(int n=0; n<5; n++) { + else { + System.out.println("Turning on lamp..."); ard.digitalWrite(13, true); - sleep(500); - ard.digitalWrite(13, false); - sleep(500); - } - } - - private static void sleep(long delay) { - try { - Thread.sleep(delay); - } - catch(InterruptedException e) { - e.printStackTrace(); + LAMP_ON = !LAMP_ON; } } }