|
| 1 | +// Processing on Raspberry Pi GPIO Example |
| 2 | +// Author: Tony DiCola |
| 3 | +// |
| 4 | +// Note this code is for Processing and not Arduino |
| 5 | +// |
| 6 | +// See the guide for this sketch at: |
| 7 | +// https://learn.adafruit.com/processing-on-the-raspberry-pi-and-pitft/overview |
| 8 | +// |
| 9 | +// Released under a MIT license: |
| 10 | +// https://opensource.org/licenses/MIT |
| 11 | + |
| 12 | +// Import hardware IO library. |
| 13 | +import processing.io.*; |
| 14 | + |
| 15 | + |
| 16 | +// Pin numbers for the LEDs and button connected to the Pi: |
| 17 | +int redLEDPin = 22; |
| 18 | +int greenLEDPin = 27; |
| 19 | +int buttonPin = 17; |
| 20 | + |
| 21 | +// Variables to hold the width and height of the buttons. |
| 22 | +// This will be set based on the size of the screen. |
| 23 | +int buttonWidth; |
| 24 | +int buttonHeight; |
| 25 | + |
| 26 | +// LED state, on or off (true or false). |
| 27 | +boolean redLED = false; |
| 28 | +boolean greenLED = false; |
| 29 | + |
| 30 | + |
| 31 | +void setup() { |
| 32 | + // Go fullscreen and hide the cursor. |
| 33 | + fullScreen(); |
| 34 | + noCursor(); |
| 35 | + |
| 36 | + // Initialize LEDs as outputs. |
| 37 | + GPIO.pinMode(redLEDPin, GPIO.OUTPUT); |
| 38 | + GPIO.pinMode(greenLEDPin, GPIO.OUTPUT); |
| 39 | + |
| 40 | + // Initialize button as input. |
| 41 | + GPIO.pinMode(buttonPin, GPIO.INPUT); |
| 42 | + |
| 43 | + // Turn the LEDs off. |
| 44 | + GPIO.digitalWrite(redLEDPin, false); |
| 45 | + GPIO.digitalWrite(greenLEDPin, false); |
| 46 | + |
| 47 | + // Compute button width and height based on screen width and height. |
| 48 | + buttonWidth = width/5; |
| 49 | + buttonHeight = height/3; |
| 50 | + |
| 51 | + // Default to drawing black lines around buttons. |
| 52 | + stroke(0, 0, 0); |
| 53 | +} |
| 54 | + |
| 55 | +void draw() { |
| 56 | + // Check button state to see if it's pressed. Because there's a pull-up |
| 57 | + // resistor to 3.3V the button pin will be at high level until the button |
| 58 | + // is pressed and it drops to a low level. |
| 59 | + if (GPIO.digitalRead(buttonPin) == GPIO.LOW) { |
| 60 | + // Blue background when button pressed. |
| 61 | + background(0, 0, 255); |
| 62 | + } |
| 63 | + else { |
| 64 | + // Gray background when button isn't pressed. |
| 65 | + background(100, 100, 100); |
| 66 | + } |
| 67 | + |
| 68 | + // Draw red LED button. |
| 69 | + if (redLED) { |
| 70 | + // Fill button with red when on. |
| 71 | + fill(255, 0, 0); |
| 72 | + } |
| 73 | + else { |
| 74 | + // Otherwise fill with white. |
| 75 | + fill(255, 255, 255); |
| 76 | + } |
| 77 | + rect(buttonWidth, buttonHeight, buttonWidth, buttonHeight); |
| 78 | + |
| 79 | + // Draw green LED button. |
| 80 | + if (greenLED) { |
| 81 | + // Fill button with red when on. |
| 82 | + fill(0, 255, 0); |
| 83 | + } |
| 84 | + else { |
| 85 | + // Otherwise fill with white. |
| 86 | + fill(255, 255, 255); |
| 87 | + } |
| 88 | + rect(3*buttonWidth, buttonHeight, buttonWidth, buttonHeight); |
| 89 | +} |
| 90 | + |
| 91 | +void mousePressed() { |
| 92 | + // Check if red LED button pressed. |
| 93 | + if (overRect(buttonWidth, buttonHeight, buttonWidth, buttonHeight)) { |
| 94 | + // Button pressed, invert the red LED state and turn on/off the LED. |
| 95 | + redLED = !redLED; |
| 96 | + GPIO.digitalWrite(redLEDPin, redLED); |
| 97 | + |
| 98 | + } |
| 99 | + // Check if green LED button pressed. |
| 100 | + if (overRect(3*buttonWidth, buttonHeight, buttonWidth, buttonHeight)) { |
| 101 | + // Button pressed, invert the green LED state and turn on/off the LED. |
| 102 | + greenLED = !greenLED; |
| 103 | + GPIO.digitalWrite(greenLEDPin, greenLED); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +boolean overRect(int x, int y, int width, int height) { |
| 108 | + // Check if the mouse is inside the provided box (defined by x, y position |
| 109 | + // and width, height). |
| 110 | + if (mouseX >= x && mouseX <= x+width && |
| 111 | + mouseY >= y && mouseY <= y+height) { |
| 112 | + return true; |
| 113 | + } else { |
| 114 | + return false; |
| 115 | + } |
| 116 | +} |
0 commit comments