3 * Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip
4 * The chip has 1 8-bit DAC and 4 x 8-bit ADCs
5 * Copyright (c) 2013 Gordon Henderson
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
10 * wiringPi is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
15 * wiringPi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with wiringPi.
22 * If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************
29 #include "wiringPiI2C.h"
36 *********************************************************************************
39 static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
43 b [1] = value & 0xFF ;
44 write (node->fd, b, 2) ;
50 *********************************************************************************
53 static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
57 wiringPiI2CWrite (node->fd, 0x40 | ((pin - node->pinBase) & 3)) ;
59 x = wiringPiI2CRead (node->fd) ; // Throw away the first read
60 x = wiringPiI2CRead (node->fd) ;
68 * Create a new instance of a PCF8591 I2C GPIO interface. We know it
69 * has 4 pins, (4 analog inputs and 1 analog output which we'll shadow
70 * input 0) so all we need to know here is the I2C address and the
71 * user-defined pin base.
72 *********************************************************************************
75 int pcf8591Setup (const int pinBase, const int i2cAddress)
78 struct wiringPiNodeStruct *node ;
80 if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
83 node = wiringPiNewNode (pinBase, 4) ;
86 node->analogRead = myAnalogRead ;
87 node->analogWrite = myAnalogWrite ;