3 * Extend wiringPi with the MCP4802 SPI Digital to Analog convertor
4 * Copyright (c) 2012-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 ***********************************************************************
26 #include <wiringPiSPI.h>
32 * Write analog value on the given pin
33 *********************************************************************************
36 static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
38 unsigned char spiData [2] ;
39 unsigned char chanBits, dataBits ;
40 int chan = pin - node->pinBase ;
47 chanBits |= ((value >> 4) & 0x0F) ;
48 dataBits = ((value << 4) & 0xF0) ;
50 spiData [0] = chanBits ;
51 spiData [1] = dataBits ;
53 wiringPiSPIDataRW (node->fd, spiData, 2) ;
58 * Create a new wiringPi device node for an mcp4802 on the Pi's
60 *********************************************************************************
63 int mcp4802Setup (const int pinBase, int spiChannel)
65 struct wiringPiNodeStruct *node ;
67 if (wiringPiSPISetup (spiChannel, 1000000) < 0)
70 node = wiringPiNewNode (pinBase, 2) ;
72 node->fd = spiChannel ;
73 node->analogWrite = myAnalogWrite ;