sn3218.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * sn3218.c:
  3. * Extend wiringPi with the SN3218 I2C LEd Driver
  4. * Copyright (c) 2012-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 <wiringPi.h>
  25. #include <wiringPiI2C.h>
  26. #include "sn3218.h"
  27. /*
  28. * myAnalogWrite:
  29. * Write analog value on the given pin
  30. *********************************************************************************
  31. */
  32. static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
  33. {
  34. int fd = node->fd ;
  35. int chan = 0x01 + (pin - node->pinBase) ;
  36. wiringPiI2CWriteReg8 (fd, chan, value & 0xFF) ; // Value
  37. wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update
  38. }
  39. /*
  40. * sn3218Setup:
  41. * Create a new wiringPi device node for an sn3218 on the Pi's
  42. * SPI interface.
  43. *********************************************************************************
  44. */
  45. int sn3218Setup (const int pinBase)
  46. {
  47. int fd ;
  48. struct wiringPiNodeStruct *node ;
  49. if ((fd = wiringPiI2CSetup (0x54)) < 0)
  50. return FALSE ;
  51. // Setup the chip - initialise all 18 LEDs to off
  52. //wiringPiI2CWriteReg8 (fd, 0x17, 0) ; // Reset
  53. wiringPiI2CWriteReg8 (fd, 0x00, 1) ; // Not Shutdown
  54. wiringPiI2CWriteReg8 (fd, 0x13, 0x3F) ; // Enable LEDs 0- 5
  55. wiringPiI2CWriteReg8 (fd, 0x14, 0x3F) ; // Enable LEDs 6-11
  56. wiringPiI2CWriteReg8 (fd, 0x15, 0x3F) ; // Enable LEDs 12-17
  57. wiringPiI2CWriteReg8 (fd, 0x16, 0x00) ; // Update
  58. node = wiringPiNewNode (pinBase, 18) ;
  59. node->fd = fd ;
  60. node->analogWrite = myAnalogWrite ;
  61. return TRUE ;
  62. }