Last active
October 7, 2016 18:05
-
-
Save LarsBergqvist/07683e694a7b3bb302531b043eb095a4 to your computer and use it in GitHub Desktop.
A Processing sketch that uses G4P's GImageToggleButton for switch an Arduino's digital output pins on and off
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import g4p_controls.*; | |
//IArduinoIntegration arduino = new ArduinoFakeIntegration(); | |
IArduinoIntegration arduino = new ArduinoIntegration(this); | |
Digital_GPIO_Pin[] pins = { new Digital_GPIO_Pin("D8",8,0), | |
new Digital_GPIO_Pin("D9",9,1), | |
new Digital_GPIO_Pin("D10",10,0), | |
new Digital_GPIO_Pin("D11",11,0) | |
}; | |
public void setup() { | |
size(400, 200); | |
int buttonArea = width/pins.length; | |
int numDrawnButtons = 0; | |
for (Digital_GPIO_Pin pin : pins ) { | |
pin.button = new GImageToggleButton(this, numDrawnButtons*buttonArea+10, 10); | |
pin.button.setState(pin.state); | |
pin.label = new GLabel(this, numDrawnButtons*buttonArea+10, 10 + 40, 50, 50); | |
arduino.setupDigitalPinAsOutput(pin.pinNumber); | |
arduino.setState(pin.pinNumber,pin.state); | |
numDrawnButtons++; | |
} | |
} | |
public void draw() { | |
background(150, 200, 150); | |
for (Digital_GPIO_Pin pin : pins ) { | |
pin.label.setText(pin.name + " : " + pin.getStateAsText()); | |
arduino.setState(pin.pinNumber,pin.state); | |
} | |
} | |
// Event handler for image toggle buttons | |
public void handleToggleButtonEvents(GImageToggleButton button, GEvent event) { | |
for (Digital_GPIO_Pin pin : pins ) { | |
if (pin.button == button) { | |
pin.state = button.getState(); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment