001/* 002 * $Rev: $: Revision of last commit 003 * $Author: $: Author of last commit 004 * $Date: $: Date of last commit 005 * $URL: $ 006 * 007 * Written by Tom Gutwin 008 * Copyright (C) 2015 Tom B. Gutwin, North Vancouver BC Canada 009 * 010 * This program is free software; you can redistribute it and/or modify 011 * it under the terms of the GNU General Public License as published by 012 * the Free Software Foundation; either version 3of the License, or 013 * (at your option) any later version. 014 * 015 * This program is distributed in the hope that it will be useful, 016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 018 * GNU General Public License for more details. 019 * 020 * You should have received a copy of the GNU General Public License 021 * along with this program; If not, see <http://www.gnu.org/licenses/>. 022 */ 023 024package ca.bc.webarts.raspberry; 025 026import com.pi4j.io.gpio.GpioController; 027import com.pi4j.io.gpio.GpioFactory; 028import com.pi4j.io.gpio.GpioPinDigitalOutput; 029import com.pi4j.io.gpio.PinState; 030import com.pi4j.io.gpio.RaspiPin; 031 032import java.io.BufferedReader; 033import java.io.InputStreamReader; 034 035/** A simple example calss to control 3 GPIO pins that are connected to a RGB LED. **/ 036public class RGBLed 037{ 038 private static final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 039 040 public static String userInput(String prompt) 041 { 042 String retString = ""; 043 System.err.print(prompt); 044 try 045 { 046 retString = stdin.readLine(); 047 } 048 catch(Exception e) 049 { 050 System.out.println(e); 051 String s; 052 try 053 { 054 s = userInput("<Oooch/>"); 055 } 056 catch(Exception exception) 057 { 058 exception.printStackTrace(); 059 } 060 } 061 return retString; 062 } 063 064 public static void main(String[] args) 065 throws InterruptedException 066 { 067 System.out.println("GPIO Control - pin 22, 23 & 24 ... started."); 068 069 // create gpio controller 070 final GpioController gpio = GpioFactory.getInstance(); 071 072 final GpioPinDigitalOutput redPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_23, "red", PinState.LOW); 073 final GpioPinDigitalOutput greenPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_24, "green", PinState.LOW); 074 final GpioPinDigitalOutput bluePin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, "blue", PinState.LOW); 075 076 /* 077 * yellow = R+G 078 * cyan = G+B 079 * magenta = R+B 080 * white = R+G+B 081 */ 082 083 boolean go = true; 084 while (go) 085 { 086 String s = userInput("R, G, B, or QUIT > "); 087 if ("R".equals(s.toUpperCase())) 088 redPin.toggle(); 089 else if ("G".equals(s.toUpperCase())) 090 greenPin.toggle(); 091 else if ("B".equals(s.toUpperCase())) 092 bluePin.toggle(); 093 else if ("QUIT".equals(s.toUpperCase()) || "Q".equals(s.toUpperCase())) 094 go = false; 095 else 096 System.out.println("Unknown command [" + s + "]"); 097 } 098 // Switch them off 099 redPin.low(); 100 greenPin.low(); 101 bluePin.low(); 102 103 redPin.toggle(); //R 104 sleep(500); 105 redPin.toggle(); //G 106 greenPin.toggle(); 107 sleep(500); 108 greenPin.toggle();//B 109 bluePin.toggle(); 110 sleep(500); 111 bluePin.toggle(); 112 113 redPin.toggle();// RG = Y 114 greenPin.toggle(); 115 sleep(500); 116 redPin.toggle();//GB 117 //greenPin.toggle(); 118 bluePin.toggle(); 119 sleep(500); 120 redPin.toggle();//RGB 121 sleep(500); 122 greenPin.toggle();//RB 123 sleep(500); 124 bluePin.toggle(); 125 redPin.toggle(); 126 sleep(500); 127 redPin.high(); 128 greenPin.high(); 129 bluePin.high(); 130 sleep(500); 131 redPin.low(); 132 greenPin.low(); 133 bluePin.low(); 134 sleep(500); 135 redPin.high(); 136 greenPin.high(); 137 bluePin.high(); 138 sleep(500); 139 140 141 // Switch them off 142 redPin.low(); 143 greenPin.low(); 144 bluePin.low(); 145 // stop all GPIO activity/threads by shutting down the GPIO controller 146 // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks) 147 gpio.shutdown(); 148 } 149 150 151 /** 152 * A method to simply abstract the Try/Catch required to put the current 153 * thread to sleep for the specified time in ms. 154 * 155 * @param waitTime the sleep time in milli seconds (ms). 156 * @return boolean value specifying if the sleep completed (true) or was interupted (false). 157 */ 158 public static boolean sleep(long waitTime) 159 { 160 boolean retVal = true; 161 if (waitTime<0) 162 retVal=false; 163 else 164 { 165 /* 166 * BLOCK for the spec'd time 167 */ 168 try 169 { 170 Thread.sleep(waitTime); 171 } 172 catch (InterruptedException iex) 173 { 174 retVal = false; 175 } 176 } 177 return retVal; 178 } 179 180}