3 * Extend wiringPi with the DRC control protocll to Arduino
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 ***********************************************************************
31 #include "wiringSerial.h"
43 * Change the pin mode on the remote DRC device
44 *********************************************************************************
47 static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
49 /**/ if (mode == OUTPUT)
50 serialPutchar (node->fd, 'o') ; // Input
51 else if (mode == PWM_OUTPUT)
52 serialPutchar (node->fd, 'p') ; // PWM
54 serialPutchar (node->fd, 'i') ; // Default to input
56 serialPutchar (node->fd, pin - node->pinBase) ;
62 * ATmegas only have pull-up's on of off. No pull-downs.
63 *********************************************************************************
66 static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
69 // Force pin into input mode
71 serialPutchar (node->fd, 'i' ) ;
72 serialPutchar (node->fd, pin) ;
74 /**/ if (mode == PUD_UP)
76 serialPutchar (node->fd, '1') ;
77 serialPutchar (node->fd, pin - node->pinBase) ;
79 else if (mode == PUD_OFF)
81 serialPutchar (node->fd, '0') ;
82 serialPutchar (node->fd, pin - node->pinBase) ;
89 *********************************************************************************
92 static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
94 serialPutchar (node->fd, value == 0 ? '0' : '1') ;
95 serialPutchar (node->fd, pin - node->pinBase) ;
101 *********************************************************************************
104 static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value)
106 serialPutchar (node->fd, 'v') ;
107 serialPutchar (node->fd, pin - node->pinBase) ;
108 serialPutchar (node->fd, value & 0xFF) ;
114 *********************************************************************************
117 static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
121 serialPutchar (node->fd, 'a') ;
122 serialPutchar (node->fd, pin - node->pinBase) ;
123 vHi = serialGetchar (node->fd) ;
124 vLo = serialGetchar (node->fd) ;
126 return (vHi << 8) | vLo ;
132 *********************************************************************************
135 static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
137 serialPutchar (node->fd, 'r') ; // Send read command
138 serialPutchar (node->fd, pin - node->pinBase) ;
139 return (serialGetchar (node->fd) == '0') ? 0 : 1 ;
145 * Create a new instance of an DRC GPIO interface.
146 * Could be a variable nunber of pins here - we might not know in advance
147 * if it's an ATmega with 14 pins, or something with less or more!
148 *********************************************************************************
151 int drcSetup (const int pinBase, const int numPins, const char *device)
156 struct wiringPiNodeStruct *node ;
158 if ((fd = serialOpen (device, 115200)) < 0)
159 return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ;
161 delay (10) ; // May need longer if it's an Uno that reboots on the open...
163 // Flush any pending input
165 while (serialDataAvail (fd))
166 (void)serialGetchar (fd) ;
169 for (tries = 1 ; tries < 5 ; ++tries)
171 serialPutchar (fd, '@') ;
172 then = time (NULL) + 2 ;
173 while (time (NULL) < then)
174 if (serialDataAvail (fd))
176 if (serialGetchar (fd) == '@')
189 return wiringPiFailure (WPI_FATAL, "Unable to communidate with DRC device") ;
192 node = wiringPiNewNode (pinBase, numPins) ;
195 node->pinMode = myPinMode ;
196 node->pullUpDnControl = myPullUpDnControl ;
197 node->analogRead = myAnalogRead ;
198 node->digitalRead = myDigitalRead ;
199 node->digitalWrite = myDigitalWrite ;
200 node->pwmWrite = myPwmWrite ;