Skip to content

Commit

Permalink
Several changes.
Browse files Browse the repository at this point in the history
Change all examples to use the built in types. Fix generic types in
JArduino, AbstractJArduino classes.
  • Loading branch information
jsutaria committed Nov 7, 2016
1 parent ed9bab2 commit 4de1402
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ public void tone(Pin pin, short frequency, short duration) throws InvalidPinType
tone(DigitalPin.fromValue(pin.getValue()), frequency, duration);
}

public void tone(DigitalPin pin, Note note, short duration) {
tone(pin, note.getFrequency(), duration);
}

public void tone(Pin pin, Note note, short duration) throws InvalidPinTypeException {
tone(pin, note.getFrequency(), duration);
}

public void noTone(DigitalPin pin) {
// Create message using the factory
FixedSizePacket p = JArduinoProtocol.createNoTone(pin);
Expand Down Expand Up @@ -337,7 +329,8 @@ public byte eeprom_read(short address) {
* Implement this method to handle the incoming message interruptNotification
*/
protected abstract void receiveInterruptNotification(InterruptPin interrupt);

protected abstract void receiveInterruptNotification(Pin interrupt) throws InvalidPinTypeException;

private class JArduinoDriverMessageHandler extends JArduinoMessageHandler implements JArduinoObserver {

@Override
Expand Down Expand Up @@ -413,4 +406,6 @@ public void handleInterruptNotification(InterruptNotificationMsg msg) {
receiveInterruptNotification(msg.getInterrupt());
}
}


}
41 changes: 32 additions & 9 deletions jarduino.core/src/main/java/org/sintef/jarduino/JArduino.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,32 @@ protected void receiveInterruptNotification(InterruptPin interrupt) {
if (interrupt == InterruptPin.PIN_2_INT0) {
interruptRoutineExecutor.submit(new Runnable() {
public void run() {
interrupt0();
try {
interrupt0();
} catch (InvalidPinTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} else if (interrupt == InterruptPin.PIN_3_INT1) {
interruptRoutineExecutor.submit(new Runnable() {
public void run() {
interrupt0();
try {
interrupt0();
} catch (InvalidPinTypeException e) {
e.printStackTrace();
}
}
});
}
}

@Override
protected void receiveInterruptNotification(Pin interrupt) throws InvalidPinTypeException {
if(!interrupt.isInterrupt()) throw new InvalidPinTypeException();
receiveInterruptNotification(InterruptPin.fromValue(interrupt.getValue()));
}

/* ******************************************************
* JAVA Version of common Arduino operations
Expand All @@ -129,15 +143,15 @@ public int map(int value, int l, int h, int nl, int nh) {
* Operation to be implemented by the application
********************************************************/

protected abstract void setup();
protected abstract void setup() throws InvalidPinTypeException;

protected abstract void loop();
protected abstract void loop() throws InvalidPinTypeException;

// Operation to be redefined to handle interrupts
protected void interrupt0() {
protected void interrupt0() throws InvalidPinTypeException {
}

protected void interrupt1() {
protected void interrupt1() throws InvalidPinTypeException {
}

/* ******************************************************
Expand Down Expand Up @@ -166,9 +180,17 @@ public void stopArduino() {
}

public void run() {
setup();
try {
setup();
} catch (InvalidPinTypeException e1) {
e1.printStackTrace();
}
while (!stop) {
loop();
try {
loop();
} catch (InvalidPinTypeException e1) {
e1.printStackTrace();
}
try {
// Just to avoid blocking everything else if the
// application loop does not have delays
Expand All @@ -180,4 +202,5 @@ public void run() {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.sintef.jarduino.DigitalPin;
import org.sintef.jarduino.DigitalState;
import org.sintef.jarduino.InterruptTrigger;
import org.sintef.jarduino.InvalidPinTypeException;
import org.sintef.jarduino.JArduino;
import org.sintef.jarduino.Pin;
import org.sintef.jarduino.PinMode;
import org.sintef.jarduino.InterruptPin;

Expand All @@ -49,8 +51,8 @@
*/
public class GTalkAlert extends JArduino {

private DigitalPin led;
private InterruptPin button;
private Pin led;
private Pin button;
private String gmailOtherUser;
private String gmailUser;
private String gmailPassword;
Expand All @@ -61,7 +63,7 @@ public GTalkAlert(String port) {
super(port);
}

public GTalkAlert(String port, String user, String pwd, String otherUser, DigitalPin led, InterruptPin button) {
public GTalkAlert(String port, String user, String pwd, String otherUser, Pin led, Pin button) throws InvalidPinTypeException {
this(port);

this.led = led;
Expand Down Expand Up @@ -101,9 +103,10 @@ public GTalkAlert(String port, String user, String pwd, String otherUser, Digita
* When button is clicked, sends a message to your friend
*
* @param interrupt
* @throws InvalidPinTypeException
*/
@Override
protected void receiveInterruptNotification(InterruptPin interrupt) {
protected void receiveInterruptNotification(Pin interrupt) throws InvalidPinTypeException {
System.out.println("Interrupt " + interrupt);
super.receiveInterruptNotification(interrupt);
if (interrupt == button) {
Expand All @@ -114,24 +117,24 @@ protected void receiveInterruptNotification(InterruptPin interrupt) {
}
}

public void turnOffLED() {
digitalWrite(led, DigitalState.LOW);
public void turnOffLED() throws InvalidPinTypeException {
digitalWrite(led, LOW);
System.out.println("Hopefully, the LED should now be turned OFF");
}

public void turnOnLED() {
digitalWrite(led, DigitalState.HIGH);
public void turnOnLED() throws InvalidPinTypeException {
digitalWrite(led, HIGH);
System.out.println("Hopefully, the LED should now be turned ON");
}

public void turnOnLEDfor(long delay) {
public void turnOnLEDfor(long delay) throws InvalidPinTypeException {
turnOnLED();
timer.schedule(new Timeout(), delay);
}

@Override
protected void setup() {
pinMode(led, PinMode.OUTPUT);
protected void setup() throws InvalidPinTypeException {
pinMode(led, OUTPUT);
}

@Override
Expand All @@ -142,7 +145,11 @@ private class Timeout extends TimerTask {

@Override
public void run() {
turnOffLED();
try {
turnOffLED();
} catch (InvalidPinTypeException e) {
e.printStackTrace();
}
}
}

Expand All @@ -154,7 +161,11 @@ public void processPacket(Packet p) {
if (p instanceof Message) {
Message msg = (Message) p;
System.out.println(msg.getFrom() + ": " + msg.getBody());
turnOnLEDfor(5000);
try {
turnOnLEDfor(5000);
} catch (InvalidPinTypeException e) {
e.printStackTrace();
}
}
}

Expand All @@ -166,15 +177,16 @@ public void processPacket(Packet p) {
* Arduino board. Disclaimer: We are not responsible for any troubles with
* your google account. The connection to your Google account is entirely
* delegated to the Smack API
* @throws InvalidPinTypeException
*/
public static void main(String[] args) {
public static void main(String[] args) throws InvalidPinTypeException {
String otherName = "your.friend@gmail.com";
String userName = "first.last@gmail.com";
String password = "yourPassword";
String serialPort = "COM4";

DigitalPin led = DigitalPin.PIN_9;
InterruptPin button = InterruptPin.PIN_2_INT0;
Pin led = Pin.PIN_9;
Pin button = Pin.PIN_2;

JArduino arduino = new GTalkAlert(serialPort, userName, password, otherName, led, button);
arduino.runArduinoProcess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.sintef.jarduino.AnalogPin;
import org.sintef.jarduino.InvalidPinTypeException;
import org.sintef.jarduino.JArduino;
import org.sintef.jarduino.Pin;
import org.sintef.jarduino.comm.Serial4JArduino;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
Expand All @@ -24,7 +26,7 @@ public class TemperatureGrapher extends JArduino{
//Change this to false to graph out raw sensor reading also
private boolean onlyDisplayTemperature = true;

private AnalogPin analogPin = AnalogPin.A_0;
private Pin analogPin = pA0;
private short sensorValue;
private long counter;
private final int B=3975;
Expand Down Expand Up @@ -55,7 +57,7 @@ protected void setup() {
}

@Override
protected void loop() {
protected void loop() throws InvalidPinTypeException {
dataset = new XYSeriesCollection();
// read the analog in value:
sensorValue = analogRead(analogPin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import javax.swing.border.EmptyBorder;

import org.sintef.jarduino.DigitalPin;
import org.sintef.jarduino.InvalidPinTypeException;
import org.sintef.jarduino.JArduino;
import org.sintef.jarduino.Pin;
import org.sintef.jarduino.comm.Serial4JArduino;

public class ToneKeyKeyboard extends JArduino {
Expand All @@ -39,7 +41,7 @@ public class ToneKeyKeyboard extends JArduino {
private JPanel contentPane;
private JTextField textField;
//Connect your pieze element to digital pin 8
private DigitalPin pin = DigitalPin.PIN_10;
private Pin pin = p10;
private Timer timer;
private Timeout timeout;
private JFrame frame;
Expand Down Expand Up @@ -104,7 +106,11 @@ private class KeyPressed implements KeyListener {
@Override
public void keyPressed(KeyEvent arg0) {
//release a current playing tone when a key is pressed
noTone(pin);
try {
noTone(pin);
} catch (InvalidPinTypeException e) {
e.printStackTrace();
}
}

@Override
Expand All @@ -119,7 +125,11 @@ public void keyTyped(KeyEvent arg0) {
tone = map(tone, 65, 229, 30, 5000);
System.out.println(tone);
//play the newly acquired tone on the Arduino board
tone(pin, (short) tone, (short) 0);
try {
tone(pin, (short) tone, (short) 0);
} catch (InvalidPinTypeException e) {
e.printStackTrace();
}
//create a new timeout that will end the tone after 1,3 seconds
if (timeout != null) {
timeout.cancel();
Expand All @@ -135,7 +145,11 @@ private class Timeout extends TimerTask {

@Override
public void run() {
noTone(pin);
try {
noTone(pin);
} catch (InvalidPinTypeException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import org.sintef.jarduino.DigitalPin;
import org.sintef.jarduino.DigitalState;
import org.sintef.jarduino.InvalidPinTypeException;
import org.sintef.jarduino.JArduino;
import org.sintef.jarduino.Pin;
import org.sintef.jarduino.PinMode;

import twitter4j.Status;
Expand All @@ -44,7 +46,7 @@
*/
public class Twitter4Arduino extends JArduino{

private DigitalPin led = DigitalPin.PIN_13;
private Pin led = p13;
private Twitter twitter;
private Status last;
private Timer timer;
Expand All @@ -65,19 +67,19 @@ public Twitter4Arduino(String port, String customerKey, String customerSecret,
/*
* Turns off the LED
*/
public void turnOffLED() {
public void turnOffLED() throws InvalidPinTypeException {
digitalWrite(led, DigitalState.LOW);
}

@Override
public void setup() {
public void setup() throws InvalidPinTypeException {
//connect pin 13 to LED
pinMode(led, PinMode.OUTPUT);
turnOffLED(); //turn it of in case it is on after running another application
}

@Override
protected void loop() {
protected void loop() throws InvalidPinTypeException {
List<Status> statuses;
try {
//Get status updates from your tweet feed
Expand All @@ -93,7 +95,7 @@ protected void loop() {
status.getText());
last = status;
//light up the Arduino
digitalWrite(led, DigitalState.HIGH);
digitalWrite(led, HIGH);
timer.schedule(new Timeout(this), 10000);
}
}
Expand Down Expand Up @@ -131,7 +133,11 @@ public Timeout(Twitter4Arduino t4a) {

@Override
public void run() {
t4a.turnOffLED();
try {
t4a.turnOffLED();
} catch (InvalidPinTypeException e) {
e.printStackTrace();
}
}
}
}
Loading

0 comments on commit 4de1402

Please sign in to comment.