altos/drivers/ao_led.c: Declare LED port as void * to allow any SoC to use this code
[fw/altos] / src / drivers / ao_led.c
1 /*
2  * Copyright © 2018 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20
21 static const struct {
22         void            *port;
23         uint16_t        pin;
24 } ao_leds[] = {
25 #ifdef LED_0_PORT
26     [0] { LED_0_PORT, LED_0_PIN },
27 #endif
28 #ifdef LED_1_PORT
29     [1] { LED_1_PORT, LED_1_PIN },
30 #endif
31 #ifdef LED_2_PORT
32     [2] { LED_2_PORT, LED_2_PIN },
33 #endif
34 #ifdef LED_3_PORT
35     [3] { LED_3_PORT, LED_3_PIN },
36 #endif
37 #ifdef LED_4_PORT
38     [4] { LED_4_PORT, LED_4_PIN },
39 #endif
40 #ifdef LED_5_PORT
41     [5] { LED_5_PORT, LED_5_PIN },
42 #endif
43 #ifdef LED_6_PORT
44     [6] { LED_6_PORT, LED_6_PIN },
45 #endif
46 #ifdef LED_7_PORT
47     [7] { LED_7_PORT, LED_7_PIN },
48 #endif
49 #ifdef LED_8_PORT
50     [8] { LED_8_PORT, LED_8_PIN },
51 #endif
52 #ifdef LED_9_PORT
53     [9] { LED_9_PORT, LED_9_PIN },
54 #endif
55 #ifdef LED_10_PORT
56     [10] { LED_10_PORT, LED_10_PIN },
57 #endif
58 #ifdef LED_11_PORT
59     [11] { LED_11_PORT, LED_11_PIN },
60 #endif
61 #ifdef LED_12_PORT
62     [12] { LED_12_PORT, LED_12_PIN },
63 #endif
64 #ifdef LED_13_PORT
65     [13] { LED_13_PORT, LED_13_PIN },
66 #endif
67 #ifdef LED_14_PORT
68     [14] { LED_14_PORT, LED_14_PIN },
69 #endif
70 #ifdef LED_15_PORT
71     [15] { LED_15_PORT, LED_15_PIN },
72 #endif
73 #ifdef LED_16_PORT
74     [16] { LED_16_PORT, LED_16_PIN },
75 #endif
76 #ifdef LED_17_PORT
77     [17] { LED_17_PORT, LED_17_PIN },
78 #endif
79 #ifdef LED_18_PORT
80     [18] { LED_18_PORT, LED_18_PIN },
81 #endif
82 #ifdef LED_19_PORT
83     [19] { LED_19_PORT, LED_19_PIN },
84 #endif
85 #ifdef LED_20_PORT
86     [20] { LED_20_PORT, LED_20_PIN },
87 #endif
88 #ifdef LED_21_PORT
89     [21] { LED_21_PORT, LED_21_PIN },
90 #endif
91 #ifdef LED_22_PORT
92     [22] { LED_22_PORT, LED_22_PIN },
93 #endif
94 #ifdef LED_23_PORT
95     [23] { LED_23_PORT, LED_23_PIN },
96 #endif
97 #ifdef LED_24_PORT
98     [24] { LED_24_PORT, LED_24_PIN },
99 #endif
100 #ifdef LED_25_PORT
101     [25] { LED_25_PORT, LED_25_PIN },
102 #endif
103 #ifdef LED_26_PORT
104     [26] { LED_26_PORT, LED_26_PIN },
105 #endif
106 #ifdef LED_27_PORT
107     [27] { LED_27_PORT, LED_27_PIN },
108 #endif
109 #ifdef LED_28_PORT
110     [28] { LED_28_PORT, LED_28_PIN },
111 #endif
112 #ifdef LED_29_PORT
113     [29] { LED_29_PORT, LED_29_PIN },
114 #endif
115 #ifdef LED_30_PORT
116     [30] { LED_30_PORT, LED_30_PIN },
117 #endif
118 #ifdef LED_31_PORT
119     [31] { LED_31_PORT, LED_31_PIN },
120 #endif
121 };
122 #define N_LED   (sizeof (ao_leds)/sizeof(ao_leds[0]))
123
124 void
125 ao_led_on(AO_LED_TYPE colors)
126 {
127         AO_LED_TYPE i;
128         for (i = 0; i < N_LED; i++)
129                 if (colors & (1 << i))
130                         ao_gpio_set(ao_leds[i].port, ao_leds[i].pin, 1);
131 }
132
133 void
134 ao_led_off(AO_LED_TYPE colors)
135 {
136         AO_LED_TYPE i;
137         for (i = 0; i < N_LED; i++)
138                 if (colors & (1 << i))
139                         ao_gpio_set(ao_leds[i].port, ao_leds[i].pin, 0);
140 }
141
142 void
143 ao_led_set(AO_LED_TYPE colors)
144 {
145         AO_LED_TYPE i;
146         for (i = 0; i < N_LED; i++)
147                 ao_gpio_set(ao_leds[i].port, ao_leds[i].pin, (colors >> i) & 1);
148 }
149
150 void
151 ao_led_toggle(AO_LED_TYPE colors)
152 {
153         AO_LED_TYPE i;
154         for (i = 0; i < N_LED; i++)
155                 if (colors & (1 << i))
156                         ao_gpio_set(ao_leds[i].port, ao_leds[i].pin, ~ao_gpio_get(ao_leds[i].port, ao_leds[i].pin));
157 }
158
159 void
160 ao_led_for(AO_LED_TYPE colors, AO_TICK_TYPE ticks) 
161 {
162         ao_led_on(colors);
163         ao_delay(ticks);
164         ao_led_off(colors);
165 }
166
167 void
168 ao_led_init(void)
169 {
170         AO_LED_TYPE     bit;
171
172         for (bit = 0; bit < N_LED; bit++)
173                 ao_enable_output(ao_leds[bit].port, ao_leds[bit].pin, 0);
174 }