Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed bugs, but still can't test with EEG
  • Loading branch information
amd11037 committed Apr 4, 2015
1 parent daf15bf commit 16b32f5
Showing 1 changed file with 50 additions and 75 deletions.
125 changes: 50 additions & 75 deletions LampDemo.java
Expand Up @@ -8,13 +8,13 @@ import org.json.*;
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));

Expand All @@ -24,48 +24,57 @@ public class LampDemo {
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;
}
}
}
Expand All @@ -75,57 +84,23 @@ public class LampDemo {
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();
}
}

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;
}
}
}

0 comments on commit 16b32f5

Please sign in to comment.