drcSerial.c 5.0 KB

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