Con7968 let's get physical - io programming using pi4 j

  • Published on

  • View

  • Download




<ul><li> I/O Programming with Java on the Raspberry Pi with Pi4J JavaOne 2013 - CON7968 Robert Savage, Software Architect, AMX LLC. Lets Get Physical: </li> <li> Additional Resources Blog Articles on Project: Source Code For Demos </li> <li> Disclaimer! UAYOR! / YMMV! (use at your own risk / your mileage may vary) The projects and sample code provided in this demonstration are intended as examples and may not cover all practical use cases, local building codes, fire and/or security regulations depending on your implementation and location. THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. DISCLAIMER: Views expressed are solely the personal views and opinions of the presenter and do not represent the views of Oracle Corp., AMX, LLC or any other company. </li> <li> Agenda / Topics Raspberry Pi Overview Pi4J Overview Basic Getting Started Examples Simple GPIO Example/Demo GPIO Triggers Example/Demo RS232 Serial Communication Example / Demo Components Expansion Putting It All Together Roadmap Q&amp;A </li> <li> Raspberry Pi Single Board Computer Linux Operating system ARM Architecture Low Cost Low Power </li> <li> Java on the Raspberry Pi OpenJDK Installation: Oracle Hotspot JVM for ARM/Linux (preferred) JDK 7 Installation: JDK 8 Installation: Soft-float ABI Hard-float ABI </li> <li> What is Pi4J? Open Source Project Low Level I/O Object-oriented API Event Based Raspberry Pi Platform (Linux/ARM) Java / C (JNI + Native) </li> <li> Pi4J : Supported I/O GPIO General Purpose I/O UART Serial / RS232 SPI Serial Peripheral Interface I2C Inter-Integrated Circuit PWM Pulse-Width-Modulation </li> <li> Pi4J : GPIO Demo Basic program using a simple momentary button (input) and LED (output). GOAL: Illuminate LED when the button is pressed. Deactivate LED when button is released. </li> <li> GPIODemo:WiringDiagram </li> <li> GPIODemo:ProgramLogic </li> <li> // create GPIO controller final GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; input pin final GpioPinDigitalInput buttonPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP); // led; output pin final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW); // create event listener for button input pin buttonPin.addListener(new GpioPinListenerDigital() { @Override public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { if(event.getState().isHigh()){ // turn off LED pin ledPin.setState(PinState.LOW); } else{ // turn on LED pin ledPin.setState(PinState.HIGH); }} }); GPIODemo:SampleCode Full source available at: </li> <li> GPIODemo:InAction Pi4J : GPIO Demo Demo Time! Im not here for pretty pictures .. Lets see some *working* code! </li> <li> Pi4J : GPIO Trigger Demo Pi4J provides simple automation triggers for common GPIO event interactions. ( == less code) GOAL: Toggle LED state when the button is pressed. </li> <li> GPIOTriggerDemo:WiringDiagram </li> <li> GPIOTriggerDemo:ProgramLogic </li> <li> // create GPIO controller final GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; activates when button is pushed final GpioPinDigitalInput buttonPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP); // led; illuminates when GPIO is HI final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW); // create button event listener buttonPin.addTrigger(new GpioToggleStateTrigger(PinState.LOW, ledPin)); GPIOTriggerDemo:SampleCode Full source available at: </li> <li> GPIOTriggerDemo:InAction Pi4J : GPIO Trigger Demo Demo Time! This isnt rocket science! </li> <li> Pi4J : RS232 Demo Basic program using RS232 serial communication. GOAL: Display Hello World on LED message reader. </li> <li> RS232Demo:WiringDiagram </li> <li> RS232Demo:ProgramLogic </li> <li> RS232Demo:SampleCode // create an instance of the serial communications class final Serial serial = SerialFactory.createInstance(); // open the default serial port provided on the P1 header // (this is where our LED reader is connected), 2400); // 2400 BAUD, N, 8, 1 // create and register the serial data listener serial.addListener(new SerialDataListener() { @Override public void dataReceived(SerialDataEvent event) { // print out the data received to the console System.out.println(event.getData()); } }); // send "Hello World" message to sign serial.writeln("Hello World! -- "); Thread.sleep(500); serial.writeln(""); Full source available at: </li> <li> RS232Demo:InAction Pi4J : RS232 Demo Demo Time! </li> <li> Pi4J : Component API The component APIs provides an abstraction layer from the hardware I/O layer. This allows hardware design/circuitry to change with *less* impact to your implementation code. For example, a RELAY could be controlled from GPIO, RS232, SPI, or I2C. You program defines the RELAY impl up front based on the hardware interface, but the rest of your program logic works against the RELAY component interface and not the direct hardware /communication IO interfaces. </li> <li> Pi4J : Component API Keypad Light / LED Dimmable Light LCD Power Controller Relay Momentary Switch Toggle Switch Analog Sensor Distance Sensor Motion Sensor Temperature Sensor </li> <li> ComponentDemo:WiringDiagram </li> <li> ComponentDemo:ProgramLogic </li> <li> ComponentDemo:SampleCode // create GPIO controller final GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; activates when button is pushed final MomentarySwitch momentarySwitch = new GpioMomentarySwitchComponent( gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP), PinState.HIGH, // "OFF" PIN STATE PinState.LOW); // "ON" PIN STATE // led; illuminates when momentary switch is pushed final LED led = new GpioLEDComponent( gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW)); // create momentary switch event listener momentarySwitch.addListener(new SwitchListener() { @Override public void onStateChange(SwitchStateChangeEvent event) { if(event.getNewState() == SwitchState.ON){ led.on(); // turn ON LED } else{; // turn OFF LED } } }); Full source available at: </li> <li> ComponentDemo:InAction Pi4J : Component Demo Demo Time! </li> <li> Pi4J : I/O Expansion GPIO General Purpose I/O PWM Pulse-Width Modulation ADC Analog-to-Digital Converter DAC Digital-to-Analog Converter </li> <li> Pi4J : Putting It All Together Time to jump in to the deep end of the pool. </li> <li> Pi4J : Access Control Demo RFID Access Keypad Access Override (Exit) Switch Doorbell Door Sensors Keypad Tamper Switch Electromechanical Lock (Solenoid) Security Alarm Notifications Access Log Remote Access </li> <li> AccessControlDemo:WiringDiagram </li> <li> AccessControlDemo:InAction Pi4J : Access Control Demo Code &amp; Demo Time! Full source available at: </li> <li> Pi4J : Roadmap Version 1.0 Release Q1 2014 Additional Component Interfaces + Impls Platform Abstraction Testing Harness Library Refactoring 1-Wire / Infrared / Bluetooth / USB HID / RTC Web Service/Remote API </li> <li> Questions? </li> <li> Contact / Links / Shameless Plugs Website Source Forum!forum/pi4j Blog Twitter @pi4j | @savageautomate Email </li> </ul>