3 * Extend wiringPi with the PCF8574 I2C GPIO expander chip
4 * Copyright (c) 2013 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with wiringPi.
21 * If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
29 #include "wiringPiI2C.h"
36 * The PCF8574 is an odd chip - the pins are effectively bi-directional,
37 * however the pins should be drven high when used as an input pin...
38 * So, we're effectively copying digitalWrite...
39 *********************************************************************************
42 static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
46 bit = 1 << ((pin - node->pinBase) & 7) ;
50 old &= (~bit) ; // Write bit to 0
52 old |= bit ; // Write bit to 1
54 wiringPiI2CWrite (node->fd, old) ;
62 *********************************************************************************
65 static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
69 bit = 1 << ((pin - node->pinBase) & 7) ;
77 wiringPiI2CWrite (node->fd, old) ;
84 *********************************************************************************
87 static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
91 mask = 1 << ((pin - node->pinBase) & 7) ;
92 value = wiringPiI2CRead (node->fd) ;
94 if ((value & mask) == 0)
103 * Create a new instance of a PCF8574 I2C GPIO interface. We know it
104 * has 8 pins, so all we need to know here is the I2C address and the
105 * user-defined pin base.
106 *********************************************************************************
109 int pcf8574Setup (const int pinBase, const int i2cAddress)
112 struct wiringPiNodeStruct *node ;
114 if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
117 node = wiringPiNewNode (pinBase, 8) ;
120 node->pinMode = myPinMode ;
121 node->digitalRead = myDigitalRead ;
122 node->digitalWrite = myDigitalWrite ;
123 node->data2 = wiringPiI2CRead (fd) ;