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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * pcf8591.c:
  3. * Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip
  4. * The chip has 1 8-bit DAC and 4 x 8-bit ADCs
  5. * Copyright (c) 2013 Gordon Henderson
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://projects.drogon.net/raspberry-pi/wiringpi/
  9. *
  10. * wiringPi is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * wiringPi is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with wiringPi.
  22. * If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <unistd.h>
  26. #include "wiringPi.h"
  27. #include "wiringPiI2C.h"
  28. #include "pcf8591.h"
  29. /*
  30. * myAnalogWrite:
  31. *********************************************************************************
  32. */
  33. static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
  34. {
  35. unsigned char b [2] ;
  36. b [0] = 0x40 ;
  37. b [1] = value & 0xFF ;
  38. write (node->fd, b, 2) ;
  39. }
  40. /*
  41. * myAnalogRead:
  42. *********************************************************************************
  43. */
  44. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  45. {
  46. int x ;
  47. wiringPiI2CWrite (node->fd, 0x40 | ((pin - node->pinBase) & 3)) ;
  48. x = wiringPiI2CRead (node->fd) ; // Throw away the first read
  49. x = wiringPiI2CRead (node->fd) ;
  50. return x ;
  51. }
  52. /*
  53. * pcf8591Setup:
  54. * Create a new instance of a PCF8591 I2C GPIO interface. We know it
  55. * has 4 pins, (4 analog inputs and 1 analog output which we'll shadow
  56. * input 0) so all we need to know here is the I2C address and the
  57. * user-defined pin base.
  58. *********************************************************************************
  59. */
  60. int pcf8591Setup (const int pinBase, const int i2cAddress)
  61. {
  62. int fd ;
  63. struct wiringPiNodeStruct *node ;
  64. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  65. return fd ;
  66. node = wiringPiNewNode (pinBase, 4) ;
  67. node->fd = fd ;
  68. node->analogRead = myAnalogRead ;
  69. node->analogWrite = myAnalogWrite ;
  70. return 0 ;
  71. }