001package com.pi4j.wiringpi; 002 003/* 004 * #%L 005 * ********************************************************************** 006 * ORGANIZATION : Pi4J 007 * PROJECT : Pi4J :: Java Library (Core) 008 * FILENAME : Gertboard.java 009 * 010 * This file is part of the Pi4J project. More information about 011 * this project can be found here: http://www.pi4j.com/ 012 * ********************************************************************** 013 * %% 014 * Copyright (C) 2012 - 2013 Pi4J 015 * %% 016 * Licensed under the Apache License, Version 2.0 (the "License"); 017 * you may not use this file except in compliance with the License. 018 * You may obtain a copy of the License at 019 * 020 * http://www.apache.org/licenses/LICENSE-2.0 021 * 022 * Unless required by applicable law or agreed to in writing, software 023 * distributed under the License is distributed on an "AS IS" BASIS, 024 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 025 * See the License for the specific language governing permissions and 026 * limitations under the License. 027 * #L% 028 */ 029 030 031import com.pi4j.util.NativeLibraryLoader; 032 033/** 034 * <p> 035 * The Gertboard has an on-board Digital to Analog (DAC) converter and an Analog to Digital (ADC) 036 * converters. These are connected via the SPI bus back to the Raspberry Pi host. 037 * </p> 038 * 039 * <p> 040 * Each of the DAC and ADC chips has 2 channels. 041 * </p> 042 * 043 * <p> 044 * The DAC has a resolution of 8 bits and produces an output voltage between 0 and 2.047 volts, the 045 * ADC has a resolution of 10 bits and can take an input voltage between 0 and 3.3 volts. 046 * </p> 047 * 048 * <p> 049 * Part of the wiringPi library includes code to setup and drive these chips in an easy to use 050 * manner. 051 * </p> 052 * 053 * <p> 054 * To use in a program, first you need to make sure that the 5 SPI jumpers are present on the 055 * Gertboard (there are 7 in total, 5 for the SPI, 2 to connect the serial to the ATmega), the 056 * photo below shows all 7 jumpers in-place. 057 * </p> 058 * 059 * <p> 060 * Before using the Pi4J library, you need to ensure that the Java VM in configured with access to 061 * the following system libraries: 062 * <ul> 063 * <li>pi4j</li> 064 * <li>wiringPi</li> 065 * </ul> 066 * <blockquote> This library depends on the wiringPi native system library.</br> (developed by 067 * Gordon Henderson @ <a href="https://projects.drogon.net/">https://projects.drogon.net/</a>) 068 * </blockquote> 069 * </p> 070 * 071 * @see <a href="http://www.pi4j.com/">http://www.pi4j.com/</a> 072 * @see <a 073 * href="https://projects.drogon.net/raspberry-pi/gertboard/analog-inout/">https://projects.drogon.net/raspberry-pi/gertboard/analog-inout/</a> 074 * @author Robert Savage (<a 075 * href="http://www.savagehomeautomation.com">http://www.savagehomeautomation.com</a>) 076 */ 077public class Gertboard { 078 079 public static final int SPI_ADC_SPEED = 1000000; 080 public static final int SPI_DAC_SPEED = 1000000; 081 public static final int SPI_A2D = 0; 082 public static final int SPI_D2A = 1; 083 084 // private constructor 085 private Gertboard() { 086 // forbid object construction 087 } 088 089 static { 090 // Load the platform library 091 NativeLibraryLoader.load("pi4j", "libpi4j.so"); 092 } 093 094 /** 095 * <p> This outputs the supplied value (0-255) to the given channel (0 or 1). The output voltage is: 096 * 097 * <pre> 098 * vOut = value / 255 * 2.047 099 * </pre> 100 * 101 * or to find the value for a given voltage: 102 * 103 * <pre> 104 * value = vOut / 2.047 * 255 105 * </pre> 106 * </p> 107 * @see <a 108 * href="https://projects.drogon.net/raspberry-pi/gertboard/analog-inout/">https://projects.drogon.net/raspberry-pi/gertboard/analog-inout/</a> 109 * @param chan Analog channel to write to (0 or 1). 110 * @param value The output value (0-255) supplied to the given channel (0 or 1). 111 */ 112 public static native void gertboardAnalogWrite(int chan, int value); 113 114 /** 115 * <p> This returns a value from 0 to 1023 representing the value on the supplied channel (0 or 1). 116 * To convert this to a voltage, use the following formula: 117 * 118 * <pre> 119 * vIn = value * 3.3 / 1023 120 * </pre> 121 * </p> 122 * 123 * @see <a 124 * href="https://projects.drogon.net/raspberry-pi/gertboard/analog-inout/">https://projects.drogon.net/raspberry-pi/gertboard/analog-inout/</a> 125 * @param chan Analog channel to read from (0 or 1). 126 * @return This returns a value from 0 to 1023 representing the value on the supplied channel (0 127 * or 1). 128 */ 129 public static native int gertboardAnalogRead(int chan); 130 131 /** 132 * <p> This must be called to initialize the SPI bus to communicate with the Gertboards ADC and DAC 133 * chips. If the return value is < 0 then an error occurred and errno will be set appropriately. 134 * </p> 135 * 136 * @see <a 137 * href="https://projects.drogon.net/raspberry-pi/gertboard/analog-inout/">https://projects.drogon.net/raspberry-pi/gertboard/analog-inout/</a> 138 * @return If the return value is < 0 then an error occurred and errno will be set 139 * appropriately. If the return value is '0' or greater than the call was successful. 140 */ 141 public static native int gertboardSPISetup(); 142}