okLed.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * okLed.c:
  3. * Make the OK LED on the Pi Pulsate...
  4. *
  5. * Originally posted to the Raspberry Pi forums:
  6. * http://www.raspberrypi.org/phpBB3/viewtopic.php?p=162581#p162581
  7. *
  8. * Compile this and store it somewhere, then kick it off at boot time
  9. * e.g. by putting it in /etc/rc.local and running it in the
  10. * background &
  11. *
  12. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  13. ***********************************************************************
  14. * This file is part of wiringPi:
  15. * https://projects.drogon.net/raspberry-pi/wiringpi/
  16. *
  17. * wiringPi is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Lesser General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * wiringPi is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Lesser General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Lesser General Public License
  28. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  29. ***********************************************************************
  30. */
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #include <string.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <math.h>
  37. #include <wiringPi.h>
  38. #include <softPwm.h>
  39. #define OK_LED 16
  40. int main ()
  41. {
  42. int fd, i ;
  43. if ((fd = open ("/sys/class/leds/led0/trigger", O_RDWR)) < 0)
  44. {
  45. fprintf (stderr, "Unable to change LED trigger: %s\n", strerror (errno)) ;
  46. return 1 ;
  47. }
  48. write (fd, "none\n", 5) ;
  49. close (fd) ;
  50. if (wiringPiSetupGpio () < 0)
  51. {
  52. fprintf (stderr, "Unable to setup GPIO: %s\n", strerror (errno)) ;
  53. return 1 ;
  54. }
  55. softPwmCreate (OK_LED, 0, 100) ;
  56. for (;;)
  57. {
  58. for (i = 0 ; i <= 100 ; ++i)
  59. {
  60. softPwmWrite (OK_LED, i) ;
  61. delay (10) ;
  62. }
  63. delay (50) ;
  64. for (i = 100 ; i >= 0 ; --i)
  65. {
  66. softPwmWrite (OK_LED, i) ;
  67. delay (10) ;
  68. }
  69. delay (10) ;
  70. }
  71. return 0 ;
  72. }