pseudoPins.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * pseudoPins.c:
  3. * Extend wiringPi with a number of pseudo pins which can be
  4. * digitally or analog written/read.
  5. *
  6. * Note:
  7. * Just one set of pseudo pins can exist per Raspberry Pi.
  8. * These pins are shared between all programs running on
  9. * that Raspberry Pi. The values are also persistant as
  10. * they live in shared RAM. This gives you a means for
  11. * temporary variable storing/sharing between programs,
  12. * or for other cunning things I've not thought of yet..
  13. *
  14. * Copyright (c) 2012-2016 Gordon Henderson
  15. ***********************************************************************
  16. * This file is part of wiringPi:
  17. * https://projects.drogon.net/raspberry-pi/wiringpi/
  18. *
  19. * wiringPi is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Lesser General Public License as
  21. * published by the Free Software Foundation, either version 3 of the
  22. * License, or (at your option) any later version.
  23. *
  24. * wiringPi is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with wiringPi.
  31. * If not, see <http://www.gnu.org/licenses/>.
  32. ***********************************************************************
  33. */
  34. #define SHARED_NAME "wiringPiPseudoPins"
  35. #define PSEUDO_PINS 64
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/mman.h>
  39. #include <sys/stat.h>
  40. #include <fcntl.h>
  41. #include <wiringPi.h>
  42. #include "pseudoPins.h"
  43. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  44. {
  45. int *ptr = (int *)node->data0 ;
  46. int myPin = pin - node->pinBase ;
  47. return *(ptr + myPin) ;
  48. }
  49. static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
  50. {
  51. int *ptr = (int *)node->data0 ;
  52. int myPin = pin - node->pinBase ;
  53. *(ptr + myPin) = value ;
  54. }
  55. /*
  56. * pseudoPinsSetup:
  57. * Create a new wiringPi device node for the pseudoPins driver
  58. *********************************************************************************
  59. */
  60. int pseudoPinsSetup (const int pinBase)
  61. {
  62. struct wiringPiNodeStruct *node ;
  63. void *ptr ;
  64. node = wiringPiNewNode (pinBase, PSEUDO_PINS) ;
  65. node->fd = shm_open (SHARED_NAME, O_CREAT | O_RDWR, 0666) ;
  66. if (node->fd < 0)
  67. return FALSE ;
  68. if (ftruncate (node->fd, PSEUDO_PINS * sizeof (int)) < 0)
  69. return FALSE ;
  70. ptr = mmap (NULL, PSEUDO_PINS * sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, node->fd, 0) ;
  71. node->data0 = (unsigned int)ptr ;
  72. node->analogRead = myAnalogRead ;
  73. node->analogWrite = myAnalogWrite ;
  74. return TRUE ;
  75. }