2016-07-09 17:16:15 -07:00
|
|
|
import processing.core.*;
|
2016-07-10 06:24:02 -07:00
|
|
|
import static processing.core.PApplet.*;
|
2016-07-09 17:16:15 -07:00
|
|
|
import cc.arduino.*;
|
|
|
|
|
|
|
|
class LEDControl {
|
|
|
|
final int comRate = 57600;
|
|
|
|
|
|
|
|
Arduino arduino;
|
|
|
|
boolean[] state = {false, false, false, false};
|
|
|
|
|
|
|
|
LEDControl(PApplet applet, int whichArduino) {
|
|
|
|
// Arduino - initialize correct arduino
|
|
|
|
String[] arduinos = Arduino.list();
|
2016-07-10 00:36:44 -07:00
|
|
|
if (arduinos.length == 0) {
|
2016-07-10 06:24:02 -07:00
|
|
|
println("There are no arduinos available for use.");
|
2016-07-10 00:36:44 -07:00
|
|
|
applet.exit();
|
|
|
|
} else {
|
2016-07-10 06:24:02 -07:00
|
|
|
println("Available arduinos:");
|
2016-07-10 00:36:44 -07:00
|
|
|
for( String ard : arduinos) {
|
2016-07-10 06:24:02 -07:00
|
|
|
println(ard);
|
2016-07-10 00:36:44 -07:00
|
|
|
}
|
2016-07-10 06:24:02 -07:00
|
|
|
print("Using arduino ");
|
|
|
|
println(arduinos[whichArduino]);
|
2016-07-10 00:36:44 -07:00
|
|
|
|
2016-07-10 00:21:47 -07:00
|
|
|
// this.arduino = new Arduino(applet, arduinos[whichArduino], comRate);
|
|
|
|
//
|
|
|
|
// for (Direction dir : Direction.values()) {
|
|
|
|
// this.arduino.pinMode(dir.pin, Arduino.OUTPUT);
|
|
|
|
// }
|
2016-07-10 00:36:44 -07:00
|
|
|
}
|
2016-07-09 17:16:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean isActive(Direction dir) {
|
|
|
|
return this.state[dir.index];
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:53:20 -07:00
|
|
|
void turnOff(Direction dir) {
|
2016-07-09 17:16:15 -07:00
|
|
|
this.arduino.digitalWrite(dir.pin, Arduino.LOW);
|
|
|
|
this.state[dir.index] = false;
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:53:20 -07:00
|
|
|
void turnOn(Direction dir) {
|
2016-07-09 17:16:15 -07:00
|
|
|
this.arduino.digitalWrite(dir.pin, Arduino.HIGH);
|
|
|
|
this.state[dir.index] = true;
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:53:20 -07:00
|
|
|
void toggle(Direction dir) {
|
2016-07-09 17:16:15 -07:00
|
|
|
if (isActive(dir)) {
|
|
|
|
turnOff(dir);
|
|
|
|
} else {
|
|
|
|
turnOn(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:53:20 -07:00
|
|
|
void allOn() {
|
2016-07-09 17:51:24 -07:00
|
|
|
for (Direction dir : Direction.values()) {
|
|
|
|
turnOn(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:53:20 -07:00
|
|
|
void allOff() {
|
2016-07-09 17:51:24 -07:00
|
|
|
for (Direction dir : Direction.values()) {
|
|
|
|
turnOff(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 07:53:20 -07:00
|
|
|
void keyPressed(char key) {
|
|
|
|
switch (key) {
|
|
|
|
case 'd':
|
|
|
|
turnOn(Direction.RIGHT);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
turnOn(Direction.UP);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
turnOn(Direction.DOWN);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
turnOn(Direction.LEFT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void keyReleased(char key) {
|
|
|
|
switch (key) {
|
|
|
|
case 'd':
|
|
|
|
turnOff(Direction.RIGHT);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
turnOff(Direction.UP);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
turnOff(Direction.DOWN);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
turnOff(Direction.LEFT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 17:51:24 -07:00
|
|
|
|
2016-07-09 17:16:15 -07:00
|
|
|
enum Direction {
|
|
|
|
LEFT (0, 10),
|
|
|
|
RIGHT (1, 5),
|
|
|
|
UP (2, 6),
|
|
|
|
DOWN (3, 9);
|
|
|
|
|
|
|
|
final int index, pin;
|
|
|
|
Direction(int index, int pin) {
|
|
|
|
this.index = index;
|
|
|
|
this.pin = pin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|