You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

drc.c 5.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * drc.c:
  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/
  8. *
  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.
  13. *
  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.
  18. *
  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. ***********************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <time.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include "wiringPi.h"
  29. #include "wiringSerial.h"
  30. #include "drc.h"
  31. #ifndef TRUE
  32. # define TRUE (1==1)
  33. # define FALSE (1==2)
  34. #endif
  35. /*
  36. * myPinMode:
  37. * Change the pin mode on the remote DRC device
  38. *********************************************************************************
  39. */
  40. static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
  41. {
  42. /**/ if (mode == OUTPUT)
  43. serialPutchar (node->fd, 'o') ; // Input
  44. else if (mode == PWM_OUTPUT)
  45. serialPutchar (node->fd, 'p') ; // PWM
  46. else
  47. serialPutchar (node->fd, 'i') ; // Default to input
  48. serialPutchar (node->fd, pin - node->pinBase) ;
  49. }
  50. /*
  51. * myPullUpDnControl:
  52. * ATmegas only have pull-up's on of off. No pull-downs.
  53. *********************************************************************************
  54. */
  55. static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
  56. {
  57. // Force pin into input mode
  58. serialPutchar (node->fd, 'i' ) ;
  59. serialPutchar (node->fd, pin) ;
  60. /**/ if (mode == PUD_UP)
  61. {
  62. serialPutchar (node->fd, '1') ;
  63. serialPutchar (node->fd, pin - node->pinBase) ;
  64. }
  65. else if (mode == PUD_OFF)
  66. {
  67. serialPutchar (node->fd, '0') ;
  68. serialPutchar (node->fd, pin - node->pinBase) ;
  69. }
  70. }
  71. /*
  72. * myDigitalWrite:
  73. *********************************************************************************
  74. */
  75. static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
  76. {
  77. serialPutchar (node->fd, value == 0 ? '0' : '1') ;
  78. serialPutchar (node->fd, pin - node->pinBase) ;
  79. }
  80. /*
  81. * myPwmWrite:
  82. *********************************************************************************
  83. */
  84. static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value)
  85. {
  86. serialPutchar (node->fd, 'v') ;
  87. serialPutchar (node->fd, pin - node->pinBase) ;
  88. serialPutchar (node->fd, value & 0xFF) ;
  89. }
  90. /*
  91. * myAnalogRead:
  92. *********************************************************************************
  93. */
  94. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  95. {
  96. int vHi, vLo ;
  97. serialPutchar (node->fd, 'a') ;
  98. serialPutchar (node->fd, pin - node->pinBase) ;
  99. vHi = serialGetchar (node->fd) ;
  100. vLo = serialGetchar (node->fd) ;
  101. return (vHi << 8) | vLo ;
  102. }
  103. /*
  104. * myDigitalRead:
  105. *********************************************************************************
  106. */
  107. static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
  108. {
  109. serialPutchar (node->fd, 'r') ; // Send read command
  110. serialPutchar (node->fd, pin - node->pinBase) ;
  111. return (serialGetchar (node->fd) == '0') ? 0 : 1 ;
  112. }
  113. /*
  114. * drcSetup:
  115. * Create a new instance of an DRC GPIO interface.
  116. * Could be a variable nunber of pins here - we might not know in advance
  117. * if it's an ATmega with 14 pins, or something with less or more!
  118. *********************************************************************************
  119. */
  120. int drcSetup (const int pinBase, const int numPins, const char *device)
  121. {
  122. int fd ;
  123. int ok, tries ;
  124. time_t then ;
  125. struct wiringPiNodeStruct *node ;
  126. if ((fd = serialOpen (device, 115200)) < 0)
  127. return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ;
  128. delay (10) ; // May need longer if it's an Uno that reboots on the open...
  129. // Flush any pending input
  130. while (serialDataAvail (fd))
  131. (void)serialGetchar (fd) ;
  132. ok = FALSE ;
  133. for (tries = 1 ; tries < 5 ; ++tries)
  134. {
  135. serialPutchar (fd, '@') ;
  136. then = time (NULL) + 2 ;
  137. while (time (NULL) < then)
  138. if (serialDataAvail (fd))
  139. {
  140. if (serialGetchar (fd) == '@')
  141. {
  142. ok = TRUE ;
  143. break ;
  144. }
  145. }
  146. if (ok)
  147. break ;
  148. }
  149. if (!ok)
  150. {
  151. serialClose (fd) ;
  152. return wiringPiFailure (WPI_FATAL, "Unable to communidate with DRC device") ;
  153. }
  154. node = wiringPiNewNode (pinBase, numPins) ;
  155. node->fd = fd ;
  156. node->pinMode = myPinMode ;
  157. node->pullUpDnControl = myPullUpDnControl ;
  158. node->analogRead = myAnalogRead ;
  159. node->digitalRead = myDigitalRead ;
  160. node->digitalWrite = myDigitalWrite ;
  161. node->pwmWrite = myPwmWrite ;
  162. return 0 ;
  163. }