piGlow1.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * piGlow1.c:
  3. * Very simple demonstration of the PiGlow board.
  4. * This uses the piGlow devLib.
  5. *
  6. * Copyright (c) 2013 Gordon Henderson.
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <poll.h>
  28. #include <wiringPi.h>
  29. #include <piGlow.h>
  30. #define PIGLOW_BASE 533
  31. #ifndef TRUE
  32. # define TRUE (1==1)
  33. # define FALSE (!TRUE)
  34. #endif
  35. /*
  36. * keypressed: clearKeypressed:
  37. * Simple but effective ways to tell if the enter key has been pressed
  38. *********************************************************************************
  39. */
  40. static int keypressed (void)
  41. {
  42. struct pollfd polls ;
  43. polls.fd = fileno (stdin) ;
  44. polls.events = POLLIN ;
  45. return poll (&polls, 1, 0) != 0 ;
  46. }
  47. static void clearKeypressed (void)
  48. {
  49. while (keypressed ())
  50. (void)getchar () ;
  51. }
  52. /*
  53. * pulseLed:
  54. * Pulses the LED at position leg, ring from off to a max. value,
  55. * then off again
  56. *********************************************************************************
  57. */
  58. static void pulseLed (int leg, int ring)
  59. {
  60. int i ;
  61. for (i = 0 ; i < 140 ; ++i)
  62. {
  63. piGlow1 (leg, ring, i) ;
  64. delay (1) ;
  65. }
  66. delay (10) ;
  67. for (i = 140 ; i >= 0 ; --i)
  68. {
  69. piGlow1 (leg, ring, i) ;
  70. delay (1) ;
  71. }
  72. }
  73. /*
  74. * pulseLeg:
  75. * Same as above, but a whole leg at a time
  76. *********************************************************************************
  77. */
  78. static void pulseLeg (int leg)
  79. {
  80. int i ;
  81. for (i = 0 ; i < 140 ; ++i)
  82. {
  83. piGlowLeg (leg, i) ; delay (1) ;
  84. }
  85. delay (10) ;
  86. for (i = 140 ; i >= 0 ; --i)
  87. {
  88. piGlowLeg (leg, i) ; delay (1) ;
  89. }
  90. }
  91. /*
  92. * pulse Ring:
  93. * Same as above, but a whole ring at a time
  94. *********************************************************************************
  95. */
  96. static void pulseRing (int ring)
  97. {
  98. int i ;
  99. for (i = 0 ; i < 140 ; ++i)
  100. {
  101. piGlowRing (ring, i) ; delay (1) ;
  102. }
  103. delay (10) ;
  104. for (i = 140 ; i >= 0 ; --i)
  105. {
  106. piGlowRing (ring, i) ; delay (1) ;
  107. }
  108. }
  109. #define LEG_STEPS 3
  110. static int legSequence [] =
  111. {
  112. 4, 12, 99,
  113. 99, 4, 12,
  114. 12, 99, 4,
  115. } ;
  116. #define RING_STEPS 16
  117. static int ringSequence [] =
  118. {
  119. 0, 0, 0, 0, 0, 64,
  120. 0, 0, 0, 0, 64, 64,
  121. 0, 0, 0, 64, 64, 0,
  122. 0, 0, 64, 64, 0, 0,
  123. 0, 64, 64, 0, 0, 0,
  124. 64, 64, 0, 0, 0, 0,
  125. 64, 0, 0, 0, 0, 0,
  126. 0, 0, 0, 0, 0, 0,
  127. 64, 0, 0, 0, 0, 0,
  128. 64, 64, 0, 0, 0, 0,
  129. 0, 64, 64, 0, 0, 0,
  130. 0, 0, 64, 64, 0, 0,
  131. 0, 0, 0, 64, 64, 0,
  132. 0, 0, 0, 0, 64, 64,
  133. 0, 0, 0, 0, 0, 64,
  134. 0, 0, 0, 0, 0, 0,
  135. } ;
  136. /*
  137. * main:
  138. * Our little demo prgoram
  139. *********************************************************************************
  140. */
  141. int main (void)
  142. {
  143. int i ;
  144. int step, ring, leg ;
  145. // Always initialise wiringPi:
  146. // Use the Sys method if you don't need to run as root
  147. wiringPiSetupSys () ;
  148. // Initialise the piGlow devLib with our chosen pin base
  149. piGlowSetup (1) ;
  150. // LEDs, one at a time
  151. printf ("LEDs, one at a time\n") ;
  152. for (; !keypressed () ;)
  153. for (leg = 0 ; leg < 3 ; ++leg)
  154. {
  155. for (ring = 0 ; ring < 6 ; ++ring)
  156. {
  157. pulseLed (leg, ring) ;
  158. if (keypressed ())
  159. break ;
  160. }
  161. if (keypressed ())
  162. break ;
  163. }
  164. clearKeypressed () ;
  165. // Rings, one at a time
  166. printf ("Rings, one at a time\n") ;
  167. for (; !keypressed () ;)
  168. for (ring = 0 ; ring < 6 ; ++ring)
  169. {
  170. pulseRing (ring) ;
  171. if (keypressed ())
  172. break ;
  173. }
  174. clearKeypressed () ;
  175. // Legs, one at a time
  176. printf ("Legs, one at a time\n") ;
  177. for (; !keypressed () ;)
  178. for (leg = 0 ; leg < 3 ; ++leg)
  179. {
  180. pulseLeg (leg) ;
  181. if (keypressed ())
  182. break ;
  183. }
  184. clearKeypressed () ;
  185. delay (1000) ;
  186. // Sequence - alternating rings, legs and random
  187. printf ("Sequence now\n") ;
  188. for (; !keypressed () ;)
  189. {
  190. for (i = 0 ; i < 20 ; ++i)
  191. for (step = 0 ; step < LEG_STEPS ; ++step)
  192. {
  193. for (leg = 0 ; leg < 3 ; ++leg)
  194. piGlowLeg (leg, legSequence [step * 3 + leg]) ;
  195. delay (80) ;
  196. }
  197. for (i = 0 ; i < 10 ; ++i)
  198. for (step = 0 ; step < RING_STEPS ; ++step)
  199. {
  200. for (ring = 0 ; ring < 6 ; ++ring)
  201. piGlowRing (ring, ringSequence [step * 6 + ring]) ;
  202. delay (80) ;
  203. }
  204. for (i = 0 ; i < 1000 ; ++i)
  205. {
  206. leg = random () % 3 ;
  207. ring = random () % 6 ;
  208. piGlow1 (leg, ring, random () % 256) ;
  209. delay (5) ;
  210. piGlow1 (leg, ring, 0) ;
  211. }
  212. }
  213. return 0 ;
  214. }