Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

110 linhas
3.1 KiB

  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://github.com/WiringPi/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 <stdio.h>
  37. #include <unistd.h>
  38. #include <sys/types.h>
  39. #include <sys/mman.h>
  40. #include <fcntl.h>
  41. #include <stdint.h>
  42. #include <sys/stat.h>
  43. #include <fcntl.h>
  44. #include <wiringPi.h>
  45. #include "pseudoPins.h"
  46. static int myAnalogRead(struct wiringPiNodeStruct *node, int pin)
  47. {
  48. int *ptr = (int *)(intptr_t)node->data0; // Cast to intptr_t to handle pointer-to-integer conversion
  49. int myPin = pin - node->pinBase;
  50. return *(ptr + myPin);
  51. }
  52. static void myAnalogWrite(struct wiringPiNodeStruct *node, int pin, int value)
  53. {
  54. int *ptr = (int *)(intptr_t)node->data0;
  55. int myPin = pin - node->pinBase;
  56. *(ptr + myPin) = value;
  57. }
  58. /*
  59. * pseudoPinsSetup:
  60. * Create a new wiringPi device node for the pseudoPins driver
  61. *********************************************************************************
  62. */
  63. int pseudoPinsSetup(const int pinBase)
  64. {
  65. struct wiringPiNodeStruct *node;
  66. void *ptr;
  67. node = wiringPiNewNode(pinBase, PSEUDO_PINS);
  68. if (node == NULL) {
  69. fprintf(stderr, "Error creating new wiringPi node");
  70. return FALSE;
  71. }
  72. node->fd = shm_open(SHARED_NAME, O_CREAT | O_RDWR, 0666);
  73. if (node->fd < 0) {
  74. perror("Error opening shared memory");
  75. return FALSE;
  76. }
  77. if (ftruncate(node->fd, PSEUDO_PINS * sizeof(int)) < 0) {
  78. perror("Error resizing shared memory");
  79. return FALSE;
  80. }
  81. ptr = mmap(NULL, PSEUDO_PINS * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, node->fd, 0);
  82. if (ptr == MAP_FAILED) {
  83. perror("Error mapping shared memory");
  84. return FALSE;
  85. }
  86. node->data0 = (unsigned int)(uintptr_t)ptr;
  87. node->analogRead = myAnalogRead;
  88. node->analogWrite = myAnalogWrite;
  89. return TRUE;
  90. }