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