first commit

This commit is contained in:
pandacraft 2025-03-21 16:04:17 +01:00
commit a5a0434432
1126 changed files with 439481 additions and 0 deletions

View file

@ -0,0 +1,125 @@
package grovepi.buttonrotarydemo;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove
* Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import grovepi.observer.ButtonInvoker;
import grovepi.observer.ButtonPressDistinguisher;
import grovepi.observer.DigitalInputReader;
import grovepi.observer.RotaryInvoker;
import grovepi.observer.RotaryAngleDeterminer;
import grovepi.observer.AnalogInputReader;
/**
* ButtonRotaryDemo
* This GrovePi sensor demonstration is part of a coursework project
* for CS505 Design Patterns, at Central Connecticut State University,
* Fall 2017, with Dr. Chad Williams.
*
* The main method creates a BottonInvoker object and begins listening for button events:
* singlePress, doublePress, and longPress. Each of these events will invoke methods
* implemented in SampleButtonInvoker. Write your own class that implements ButtonInvoker interface.
* (SampleButtonInvoker is quite boring)
*
* @see SampleButtonInvoker
* @see grovepi.observer.ButtonInvoker
*
* The main method also creates a RotaryInvoker object and begins reading the
* angle of the rotary sensor. Each time the angle is read, the method
* invokeWithDegrees(degrees) of SampleRotaryInvoker is executed. Write your own class that
* implements RotaryInvoker interface. (SampleRotaryInvoker is even more boring)
*
* @see SampleRotaryInvoker
* @see grovepi.observer.RotaryInvoker
*
* @author James Luczynski
* @author Jeff Blankenship
*/
public class ButtonRotaryDemo{
/**
* @param args the command line arguments
* @throws java.lang.InterruptedException
* @throws java.lang.Exception
*/
public static void main(String[] args){
//button can be connected to D2-D8.
int buttonPin = 6;
//rotary can be connected to A0, A1, A2
int rotaryPin = 2;
//instantiate a ButtonInvoker
SampleButtonInvoker invoker = new SampleButtonInvoker();
initButton(invoker, buttonPin);
//instantiate a RotaryInvoker
SampleRotaryInvoker rotaryInvoker = new SampleRotaryInvoker();
initRotary(rotaryInvoker,rotaryPin);
}
/**
* Initializes DigitalInputReader and ButtonPressDistinguisher objects.
* Button presses will invoke the methods defined in the ButtonInvoker parameter.
* @param invoker any object of a class that implements ButtonInvoker interface
* @param pin the GrovePi port number the button sensor is plugged into
*/
public static void initButton(ButtonInvoker invoker, int pin) {
try {
DigitalInputReader buttonReader = new DigitalInputReader(pin);
ButtonPressDistinguisher distinguisher = new ButtonPressDistinguisher(invoker);
buttonReader.addObserver(distinguisher);
buttonReader.startReading();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Initializes AnalogInputReader and RotaryAngleDeterminer objects.
* Rotations of the rotary sensor will invoke the method defined in the RotaryInvoker parameter.
* @param invoker any object of a class that implements RotaryInvoker interface
* @param pin the GrovePi port number the rotary sensor is plugged into
*/
public static void initRotary(RotaryInvoker invoker, int pin){
try {
AnalogInputReader rotaryReader = new AnalogInputReader(pin);
RotaryAngleDeterminer determiner = new RotaryAngleDeterminer(invoker);
rotaryReader.addObserver(determiner);
rotaryReader.startReading();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,75 @@
package grovepi.buttonrotarydemo;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import grovepi.observer.ButtonInvoker;
/**
* ButtonInvoker Sample.
* This GrovePi sensor demonstration is part of a coursework project
* for CS505 Design Patterns, at Central Connecticut State University,
* Fall 2017, with Dr. Chad Williams.
*
* ButtonInvokerdemo realizes the ButtonInvoker interface and provides
* a sample implementation for each of its methods.
*
* You can write your own class that implements ButtonInvoker interface (this one is very boring)
* to determine what happens for each different press
*
* @see grovepi.observer.ButtonInvoker
* @author James Luczynski
* @author Jeff Blankenship
*/
public class SampleButtonInvoker implements ButtonInvoker{
@Override
public void singlePress(){
//singlePress code
System.out.println("Single Press");
}
@Override
public void doublePress(){
//doublePress code
System.out.println("Double Press");
}
@Override
public void longPress(){
//longPress code
System.out.println("Long Press");
}
}

View file

@ -0,0 +1,74 @@
package grovepi.buttonrotarydemo;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import grovepi.observer.RotaryInvoker;
import java.text.DecimalFormat;
/**
* This GrovePi sensor demonstration is part of a coursework project
* for CS505 Design Patterns, at Central Connecticut State University,
* Fall 2017, with Dr. Chad Williams.
*
* This class provides a sample implementation of the RotaryInvoker interface.
*
* You can write your own class that implements RotaryInvoker interface (this one is very boring)
* to determine what happens on based on the angle of the rotary sensor.
*
* @see grovepi.observer.ButtonInvoker
* @author James Luczynski
* @author Jeff Blankenship
*/
public class SampleRotaryInvoker implements RotaryInvoker{
/**
* Angle from the previous reading of the rotary sensor.
* NOTE: the rotary sensor has only 300 degrees of rotation.
*/
private double degrees;
/**
* A somewhat arbitrary number. If the difference in degrees from the previous
* reading and the most recent reading is less than the tolerance, don't do anything.
*/
private double tolerance = 4;
/**
* Prints current angle of rotary sensor if different from last reading
* @param degrees most recent reading of rotary sensor angle
*/
public void invokeWithDegrees(double degrees){
if (Math.abs(this.degrees - degrees) > tolerance)
{
this.degrees = degrees;
DecimalFormat df = new DecimalFormat("###.#");
String output = df.format(this.degrees);
System.out.println("Rotary sensor position (degrees): " + output);
}
}
}

View file

@ -0,0 +1,89 @@
package grovepi.observer;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import com.dexterind.grovepi.sensors.base.AnalogSensor;
import java.io.IOException;
/**
* An instance of this class reads sensor data from a GrovePi analog sensor and
* updates an observer with this data.
*
* @author James Luczynski
* @author Jeff Blankenship
*/
public class AnalogInputReader extends InputSensorReader implements Runnable{
private AnalogSensor sensor;
/**
* Constructor
* @param pin the GrovePi port number the sensor is plugged into
* @throws IOException
* @throws InterruptedException
* @throws Exception
*/
public AnalogInputReader(int pin) throws InterruptedException, Exception{
sensor = new AnalogSensor(pin, 4);
readDelay = 250;
}
/**
* Constructor
* @param pin the GrovePi port number the sensor is plugged into
* @param bufferLength length of byte[] to be read from the sensor
* @throws InterruptedException
* @throws Exception
*/
public AnalogInputReader(int pin, int bufferLength) throws InterruptedException, Exception{
sensor = new AnalogSensor(pin, bufferLength);
readDelay = 250;
}
/**
* Reads the values of the sensor, and notifies this instance's observers
* of these new values
*/
public void run() {
try {
byte[] newState = sensor.readBytes();
state = newState;
notifyObservers(state);
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Begins reading sensor values
*/
public void startReading(){
super.startReading(this);
}
}

View file

@ -0,0 +1,51 @@
package grovepi.observer;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Declares the methods that are called when ButtonPressDistinguisher identifies
* a single, double, and long press respectively. Write your own class that implements
* this interface to determine what specific behavior should occur for each specific
* button press.
* @see ButtonPressDistinguisher
* @see SamplebuttonInvoker
*
* @author James Luczynski
* @author Jeff Blankenship
*/
public interface ButtonInvoker {
public abstract void singlePress();
public abstract void doublePress();
public abstract void longPress();
}

View file

@ -0,0 +1,138 @@
package grovepi.observer;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* An instance of this class recieves updates containing sensor events, analyzes
* the timing of these events and invokes the appropriate method on this instance's
* ButtonInvoker object. The ButtonInvoker object can be changed at runtime
* to get different behaviour triggered by button presses.
*
* @see ButtonInvoker
* @author James Luczynski
* @author Jeff Blankenship
*/
public class ButtonPressDistinguisher implements InputSensorObserver, Runnable{
private boolean buttonDown = false;
private boolean waitingForSecondPress = false;
private long lastPress;
private long lastRelease;
private Thread thread = new Thread(this);
private int longPressTimeInterval = 1000;
private int doublePressTimeInterval = 1000;
private ButtonInvoker invoker;
/**
* Constructor
* @see ButtonInvoker
* @param invoker
*/
public ButtonPressDistinguisher(ButtonInvoker invoker){
this.invoker = invoker;
}
/**
* Changes this instance's buttonInvoker object to the one in the argument provided.
* @param invoker
*/
public void setInvoker(ButtonInvoker invoker){
this.invoker = invoker;
}
/**
* Analyzes the timing of button events and determines if a single, double or long press
* has occurred. It then invokes the associated method on this instance's ButtonInvoker object
* @param b the byte[] read from the button sensor
*/
public void update(byte[] b){
long timeOfAction = System.currentTimeMillis();
buttonDown = b[0] == 1 ? true : false;
if (buttonDown) { //if this update results from button being pressed down.
lastPress = timeOfAction;
} else{ //button was just released
lastRelease = timeOfAction;
if (lastRelease - lastPress > longPressTimeInterval){
invoker.longPress();
}
else{
if (thread.isAlive()){ //if thread still waiting, it's a double press
invoker.doublePress();
waitingForSecondPress = false;
}else{ //this press is not second press of a double press, must wait
waitingForSecondPress = true;
thread = new Thread(this);
thread.start();
}
}
}
}
public void run(){
try{
Thread.sleep(doublePressTimeInterval); //after "enough" time has elapsed,
if(waitingForSecondPress){ //and still waiting for second press, then it's a single press
invoker.singlePress();
waitingForSecondPress = false;
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
/**
* Changes the time interval that two button presses must occur within, in
* in order to be considered a double press
* @param interval the new double press interval in milliseconds
*/
public void setDoublePressInterval(int interval){
doublePressTimeInterval = interval;
}
/**
* @return the current double press time interval in milliseconds
*/
public int getDoublePressInterval(){
return doublePressTimeInterval;
}
/**
* Changes the length of time (in milliseconds) that the button must be
* continuously pressed down for in order for that press to be considered a long press
* @param interval the new long press interval in milliseconds
*/
public void setLongPressInterval(int interval){
longPressTimeInterval = interval;
}
/**
* @return the current long press time interval in milliseconds.
*/
public int getLongPressInterval(){
return longPressTimeInterval;
}
}

View file

@ -0,0 +1,79 @@
package grovepi.observer;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import com.dexterind.grovepi.sensors.base.DigitalSensor;
import java.io.IOException;
/**
* An instance of this class reads sensor data from a GrovePi digital sensor and
* updates an observer with this data.
*
* @author James Luczynski
* @author Jeff Blankenship
*/
public class DigitalInputReader extends InputSensorReader implements Runnable{
private DigitalSensor sensor;
/**
* Constructor
* @param pin the GrovePi port number the sensor is plugged into
* @throws IOException
* @throws InterruptedException
* @throws Exception
*/
public DigitalInputReader(int pin) throws IOException, InterruptedException, Exception{
this.pin = pin;
sensor = new DigitalSensor(pin);
readDelay = 125;
}
/**
* Reads the values of the sensor, and updates this object's observers
* if the values have changed from the last reading.
*/
public void run() {
try {
byte[] newState = sensor.readBytes();
if (!equals(state, newState)){
state = newState;
notifyObservers(state);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* Begins reading sensor values.
*/
public void startReading(){
super.startReading(this);
}
}

View file

@ -0,0 +1,50 @@
package grovepi.observer;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Provides the interface that all concrete InputSensorObservers must implement.
* InputSensorObserver objects will receive updates from InputSensorReader objects.
* Concrete subclasses should process and analyze the data from these updates.
*
* @author James Luczynski
* @author Jeff Blankenship
*/
public interface InputSensorObserver {
/**
* Concrete implementations of this method should include a way to process
* the data in the byte[]
* @param b the byte[] read from a sensor
*/
public abstract void update(byte[] b);
}

View file

@ -0,0 +1,133 @@
package grovepi.observer;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
*
*
* @author James Luczynski
* @author Jeff Blankenship
* @author Chad Williams
*/
public abstract class InputSensorReader {
/**
* Time in milliseconds between consecutive readings of sensor values
* Each subclass reinitialize this differently in their constructors
*/
protected int readDelay = 100;
/**
* The pin number on the GrovePi that this object reads from
*/
protected int pin;
/**
* The list of observers that will be updated with the read values
*/
protected ArrayList<InputSensorObserver> observers = new ArrayList();
/**
* Responsible for making intermittent readings of sensor values
*/
private ScheduledExecutorService exec;
/**
* contains the most recent values read from the sensor
*/
protected byte[] state = {0};
/**
* Adds an InputSensorObserver object to the list of observers
* @param observer the InputSensorObserver object to be added
*/
public void addObserver(InputSensorObserver observer){
observers.add(observer);
}
/**
* Removes an InputSensorObserver object to the list of observers
* @param observer the InputSensorObserver object to be removed
*/
public void removeObserver(InputSensorObserver observer){
observers.remove(observer);
}
public ArrayList getObservers(){
return observers;
}
/**
* Begins reading the values of the sensor.
* @param r
*/
protected void startReading(Runnable r){
exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(r, 0, readDelay, TimeUnit.MILLISECONDS);
}
/**
* Stops the intermittent readings of the sensor.
*/
public void stopReading(){
if (exec != null)
exec.shutdown();
}
/**
* Changes the time interval between consecutive reads to the sensor
* @param delay the new time interval in milliseconds
*/
public void setDelay(int delay){
readDelay = delay;
}
/**
* Notifies all observers of this object of the newly read values from the sensor
* @param b
*/
public void notifyObservers(byte[] b){
for (InputSensorObserver obs : observers)
obs.update(b);
}
/**
* @param b1
* @param b2
* @return true if both byte[] are equal, false otherwise
*/
protected static boolean equals(byte[] b1, byte[] b2){
if (b1.length != b2.length)
return false;
else{
for (int i = 0; i < b1.length; i++)
if (b1[i] != b2[i])
return false;
}
return true;
}
}

View file

@ -0,0 +1,114 @@
package grovepi.observer;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* RotaryAngleDeterminer.
* This GrovePi sensor demonstration is part of a coursework project
* for CS505 Design Patterns, at Central Connecticut State University,
* Fall 2017, with Dr. Chad Williams.
*
* @author James Luczynski
* @author Jeff Blankenship
* @author Chad Williams
*/
public class RotaryAngleDeterminer implements InputSensorObserver{
/**
* Reference voltage of ADC is 5v
*/
private static final double ADC_REF = 5;
/**
* Vcc of the grove interface is 5 volts
*/
private static final double GROVE_VCC = 5;
/**
* Grove rotary sensor has only 300 degrees of rotation
*/
private static final double FULL_ANGLE = 300;
private RotaryInvoker invoker;
/**
* Constructor
* @see RotaryInvoker
* @param invoker
*/
public RotaryAngleDeterminer(RotaryInvoker invoker){
this.invoker = invoker;
}
/**
* Changes this instance's rotaryInvoker object to the one in the argument provided.
* @param invoker
*/
public void setInvoker(RotaryInvoker invoker){
this.invoker = invoker;
}
/**
* Computes the angle in degrees from the parameter b,
* Invokes this instance's RotaryInvoker invokeWithDegrees(double) method
* @see RotaryInvoker
* @param b
*/
public void update(byte[] b) {
double degrees = getDegrees(b);
if (inRange(degrees))
invoker.invokeWithDegrees(degrees);
}
/**
* Computes the angle in degrees from the byte[] b
* @param b byte[] read from the rotary sensor
* @return angle in degrees
*/
private double getDegrees(byte[] b){
int[] v = unsign(b);
double sensorValue = (v[1]*256) + v[2];
double voltage = sensorValue * ADC_REF / 1023;
double degrees = voltage * FULL_ANGLE / GROVE_VCC;
return degrees;
}
private int[] unsign(byte[] b) {
int[] v = new int[b.length];
for (int i = 0; i < b.length; i++)
v[i] = unsign(b[i]);
return v;
}
private int unsign(byte b) {
return b & 0xFF;
}
private boolean inRange(double degrees){
return (0 <= degrees && degrees <= FULL_ANGLE);
}
}

View file

@ -0,0 +1,49 @@
package grovepi.observer;
/*
* **********************************************************************
* PROJECT : GrovePi Java Library
*
* This file is part of the GrovePi Java Library project. More information about
* this project can be found here: https://github.com/DexterInd/GrovePi
* **********************************************************************
*
* ## License
*
* The MIT License (MIT)
* GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Declares the methods that are called when RotaryAngleDeterminer receives
* an update containing the current angle of the rotary sensor.
* You can write your own class that implements
* this interface to determine what specific behavior should occur on these updates
*
* @see RotaryAngleDeterminer
* @see SampleRotaryInvoker
*
* @author James Luczynski
* @author Jeff Blankenship
* @author Chad Williams
*/
public interface RotaryInvoker {
public abstract void invokeWithDegrees(double degrees);
}