Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

177 rader
4.6 KiB

  1. /*
  2. * piglow.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://github.com/WiringPi/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 <string.h>
  27. #include <stdlib.h>
  28. #ifndef TRUE
  29. # define TRUE (1==1)
  30. # define FALSE (!TRUE)
  31. #endif
  32. #include <wiringPi.h>
  33. #include <piGlow.h>
  34. static void failUsage (void)
  35. {
  36. fprintf (stderr, "Usage examples:\n") ;
  37. fprintf (stderr, " piglow off # All off\n") ;
  38. fprintf (stderr, " piglow red 50 # Light the 3 red LEDs to 50%%\n") ;
  39. fprintf (stderr, " colours are: red, yellow, orange, green, blue and white\n") ;
  40. fprintf (stderr, " piglow all 75 # Light all to 75%%\n") ;
  41. fprintf (stderr, " piglow leg 0 25 # Light leg 0 to 25%%\n") ;
  42. fprintf (stderr, " piglow ring 3 100 # Light ring 3 to 100%%\n") ;
  43. fprintf (stderr, " piglow led 2 5 100 # Light the single LED on Leg 2, ring 5 to 100%%\n") ;
  44. exit (EXIT_FAILURE) ;
  45. }
  46. static int getPercent (char *typed)
  47. {
  48. int percent ;
  49. percent = atoi (typed) ;
  50. if ((percent < 0) || (percent > 100))
  51. {
  52. fprintf (stderr, "piglow: percent value out of range\n") ;
  53. exit (EXIT_FAILURE) ;
  54. }
  55. return (percent * 255) / 100 ;
  56. }
  57. /*
  58. * main:
  59. * Our little demo prgoram
  60. *********************************************************************************
  61. */
  62. int main (int argc, char *argv [])
  63. {
  64. int percent ;
  65. int ring, leg ;
  66. // Always initialise wiringPi:
  67. // Use the Sys method if you don't need to run as root
  68. wiringPiSetupSys () ;
  69. // Initialise the piGlow devLib
  70. piGlowSetup (FALSE) ;
  71. if (argc == 1)
  72. failUsage () ;
  73. if ((argc == 2) && (strcasecmp (argv [1], "off") == 0))
  74. {
  75. for (leg = 0 ; leg < 3 ; ++leg)
  76. piGlowLeg (leg, 0) ;
  77. return 0 ;
  78. }
  79. if (argc == 3)
  80. {
  81. percent = getPercent (argv [2]) ;
  82. /**/ if (strcasecmp (argv [1], "red") == 0)
  83. piGlowRing (PIGLOW_RED, percent) ;
  84. else if (strcasecmp (argv [1], "yellow") == 0)
  85. piGlowRing (PIGLOW_YELLOW, percent) ;
  86. else if (strcasecmp (argv [1], "orange") == 0)
  87. piGlowRing (PIGLOW_ORANGE, percent) ;
  88. else if (strcasecmp (argv [1], "green") == 0)
  89. piGlowRing (PIGLOW_GREEN, percent) ;
  90. else if (strcasecmp (argv [1], "blue") == 0)
  91. piGlowRing (PIGLOW_BLUE, percent) ;
  92. else if (strcasecmp (argv [1], "white") == 0)
  93. piGlowRing (PIGLOW_WHITE, percent) ;
  94. else if (strcasecmp (argv [1], "all") == 0)
  95. for (ring = 0 ; ring < 6 ; ++ring)
  96. piGlowRing (ring, percent) ;
  97. else
  98. {
  99. fprintf (stderr, "piglow: invalid colour\n") ;
  100. exit (EXIT_FAILURE) ;
  101. }
  102. return 0 ;
  103. }
  104. if (argc == 4)
  105. {
  106. /**/ if (strcasecmp (argv [1], "leg") == 0)
  107. {
  108. leg = atoi (argv [2]) ;
  109. if ((leg < 0) || (leg > 2))
  110. {
  111. fprintf (stderr, "piglow: leg value out of range\n") ;
  112. exit (EXIT_FAILURE) ;
  113. }
  114. percent = getPercent (argv [3]) ;
  115. piGlowLeg (leg, percent) ;
  116. }
  117. else if (strcasecmp (argv [1], "ring") == 0)
  118. {
  119. ring = atoi (argv [2]) ;
  120. if ((ring < 0) || (ring > 5))
  121. {
  122. fprintf (stderr, "piglow: ring value out of range\n") ;
  123. exit (EXIT_FAILURE) ;
  124. }
  125. percent = getPercent (argv [3]) ;
  126. piGlowRing (ring, percent) ;
  127. }
  128. return 0 ;
  129. }
  130. if (argc == 5)
  131. {
  132. if (strcasecmp (argv [1], "led") != 0)
  133. failUsage () ;
  134. leg = atoi (argv [2]) ;
  135. if ((leg < 0) || (leg > 2))
  136. {
  137. fprintf (stderr, "piglow: leg value out of range\n") ;
  138. exit (EXIT_FAILURE) ;
  139. }
  140. ring = atoi (argv [3]) ;
  141. if ((ring < 0) || (ring > 5))
  142. {
  143. fprintf (stderr, "piglow: ring value out of range\n") ;
  144. exit (EXIT_FAILURE) ;
  145. }
  146. percent = getPercent (argv [4]) ;
  147. piGlow1 (leg, ring, percent) ;
  148. return 0 ;
  149. }
  150. failUsage () ;
  151. return 0 ;
  152. }