Contiki-NG
leds.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * Copyright (c) 2018, George Oikonomou - http://www.spd.gr
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*---------------------------------------------------------------------------*/
33 /**
34  * \addtogroup leds
35  * @{
36  *
37  * \file
38  * Implementation of the platform-independent aspects of the LED HAL
39  */
40 /*---------------------------------------------------------------------------*/
41 #include "contiki.h"
42 #include "dev/gpio-hal.h"
43 #include "dev/leds.h"
44 
45 #include <stdint.h>
46 #include <stdbool.h>
47 /*---------------------------------------------------------------------------*/
48 #if LEDS_LEGACY_API
49 /*---------------------------------------------------------------------------*/
50 void
51 leds_init(void)
52 {
53  leds_arch_init();
54 }
55 /*---------------------------------------------------------------------------*/
56 void
57 leds_blink(void)
58 {
59  /* Blink all leds that were initially off. */
60  unsigned char blink;
61  blink = ~leds_arch_get();
62  leds_toggle(blink);
63 
64  clock_delay(400);
65 
66  leds_toggle(blink);
67 }
68 /*---------------------------------------------------------------------------*/
69 unsigned char
70 leds_get(void) {
71  return leds_arch_get();
72 }
73 /*---------------------------------------------------------------------------*/
74 void
76 {
77  leds_arch_set(ledv);
78 }
79 /*---------------------------------------------------------------------------*/
80 void
81 leds_on(leds_mask_t ledv)
82 {
83  leds_arch_set(leds_arch_get() | ledv);
84 }
85 /*---------------------------------------------------------------------------*/
86 void
88 {
89  leds_arch_set(leds_arch_get() & ~ledv);
90 }
91 /*---------------------------------------------------------------------------*/
92 void
94 {
95  leds_arch_set(leds_arch_get() ^ ledv);
96 }
97 /*---------------------------------------------------------------------------*/
98 #else /* LEDS_LEGACY_API */
99 /*---------------------------------------------------------------------------*/
100 #if LEDS_COUNT
101 extern const leds_t leds_arch_leds[];
102 #else
103 static const leds_t *leds_arch_leds = NULL;
104 #endif
105 /*---------------------------------------------------------------------------*/
106 void
108 {
109  leds_num_t led;
110 
111  for(led = 0; led < LEDS_COUNT; led++) {
112  gpio_hal_arch_pin_set_output(leds_arch_leds[led].pin);
113  }
115 }
116 /*---------------------------------------------------------------------------*/
117 void
119 {
120  if(led >= LEDS_COUNT) {
121  return;
122  }
123 
124  if(leds_arch_leds[led].negative_logic) {
125  gpio_hal_arch_clear_pin(leds_arch_leds[led].pin);
126  } else {
127  gpio_hal_arch_set_pin(leds_arch_leds[led].pin);
128  }
129 }
130 /*---------------------------------------------------------------------------*/
131 void
133 {
134  if(led >= LEDS_COUNT) {
135  return;
136  }
137 
138  if(leds_arch_leds[led].negative_logic) {
139  gpio_hal_arch_set_pin(leds_arch_leds[led].pin);
140  } else {
141  gpio_hal_arch_clear_pin(leds_arch_leds[led].pin);
142  }
143 }
144 /*---------------------------------------------------------------------------*/
145 void
147 {
148  if(led >= LEDS_COUNT) {
149  return;
150  }
151 
152  gpio_hal_arch_toggle_pin(leds_arch_leds[led].pin);
153 }
154 /*---------------------------------------------------------------------------*/
155 void
157 {
158  leds_num_t led;
159 
160  for(led = 0; led < LEDS_COUNT; led++) {
161  if((1 << led) & leds) {
162  if(leds_arch_leds[led].negative_logic) {
163  gpio_hal_arch_clear_pin(leds_arch_leds[led].pin);
164  } else {
165  gpio_hal_arch_set_pin(leds_arch_leds[led].pin);
166  }
167  }
168  }
169 }
170 /*---------------------------------------------------------------------------*/
171 void
173 {
174  leds_num_t led;
175 
176  for(led = 0; led < LEDS_COUNT; led++) {
177  if((1 << led) & leds) {
178  if(leds_arch_leds[led].negative_logic) {
179  gpio_hal_arch_set_pin(leds_arch_leds[led].pin);
180  } else {
181  gpio_hal_arch_clear_pin(leds_arch_leds[led].pin);
182  }
183  }
184  }
185 }
186 /*---------------------------------------------------------------------------*/
187 void
189 {
190  leds_num_t led;
191 
192  for(led = 0; led < LEDS_COUNT; led++) {
193  if((1 << led) & leds) {
194  gpio_hal_arch_toggle_pin(leds_arch_leds[led].pin);
195  }
196  }
197 }
198 /*---------------------------------------------------------------------------*/
199 void
201 {
203  leds_on(leds);
204 }
205 /*---------------------------------------------------------------------------*/
208 {
209  leds_mask_t rv = 0;
210  leds_num_t led;
211  uint8_t pin_state;
212 
213  for(led = 0; led < LEDS_COUNT; led++) {
214  pin_state = gpio_hal_arch_read_pin(leds_arch_leds[led].pin);
215 
216  if((leds_arch_leds[led].negative_logic == false && pin_state == 1) ||
217  (leds_arch_leds[led].negative_logic == true && pin_state == 0)) {
218  rv |= 1 << led;
219  }
220  }
221 
222  return rv;
223 }
224 /*---------------------------------------------------------------------------*/
225 #endif /* LEDS_LEGACY_API */
226 /*---------------------------------------------------------------------------*/
227 /**
228  * @}
229  */
void leds_single_toggle(leds_num_t led)
Toggle a single LED.
Definition: leds.c:146
void leds_on(leds_mask_t leds)
Turn on multiple LEDs.
Definition: leds.c:156
void leds_init()
Initialise the LED HAL.
Definition: leds.c:107
void leds_single_off(leds_num_t led)
Turn a single LED off.
Definition: leds.c:132
#define LEDS_ALL
The OR mask representation of all device LEDs.
Definition: leds.h:195
leds_mask_t leds_get()
Get the status of LEDs.
Definition: leds.c:207
#define LEDS_COUNT
The number of LEDs present on a device.
Definition: leds.h:189
void leds_single_on(leds_num_t led)
Turn a single LED on.
Definition: leds.c:118
void gpio_hal_arch_toggle_pin(gpio_hal_pin_t pin)
Toggle a GPIO pin.
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:164
A LED logical representation.
Definition: leds.h:213
void leds_off(leds_mask_t leds)
Turn off multiple LEDs.
Definition: leds.c:172
void leds_toggle(leds_mask_t leds)
Toggle multiple LEDs.
Definition: leds.c:188
Header file for the GPIO HAL.
Header file for the LED HAL.
uint8_t leds_mask_t
An OR mask datatype to represents multiple LEDs.
Definition: leds.h:164
void leds_set(leds_mask_t leds)
Set all LEDs to a specific state.
Definition: leds.c:200
uint8_t leds_num_t
The LED number.
Definition: leds.h:159