Contiki-NG
Loading...
Searching...
No Matches
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/*---------------------------------------------------------------------------*/
50void
51leds_init(void)
52{
53 leds_arch_init();
54}
55/*---------------------------------------------------------------------------*/
56void
57leds_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/*---------------------------------------------------------------------------*/
69unsigned char
70leds_get(void) {
71 return leds_arch_get();
72}
73/*---------------------------------------------------------------------------*/
74void
76{
77 leds_arch_set(ledv);
78}
79/*---------------------------------------------------------------------------*/
80void
82{
83 leds_arch_set(leds_arch_get() | ledv);
84}
85/*---------------------------------------------------------------------------*/
86void
88{
89 leds_arch_set(leds_arch_get() & ~ledv);
90}
91/*---------------------------------------------------------------------------*/
92void
94{
95 leds_arch_set(leds_arch_get() ^ ledv);
96}
97/*---------------------------------------------------------------------------*/
98#else /* LEDS_LEGACY_API */
99/*---------------------------------------------------------------------------*/
100#if LEDS_COUNT
101extern const leds_t leds_arch_leds[];
102#else
103static const leds_t *leds_arch_leds = NULL;
104#endif
105/*---------------------------------------------------------------------------*/
106#if GPIO_HAL_PORT_PIN_NUMBERING
107#define LED_PORT(led) (led).port
108#else
109#define LED_PORT(led) GPIO_HAL_NULL_PORT
110#endif
111/*---------------------------------------------------------------------------*/
112void
114{
115 leds_num_t led;
116
117 for(led = 0; led < LEDS_COUNT; led++) {
118 gpio_hal_arch_pin_set_output(LED_PORT(leds_arch_leds[led]),
119 leds_arch_leds[led].pin);
120 }
122}
123/*---------------------------------------------------------------------------*/
124void
126{
127 if(led >= LEDS_COUNT) {
128 return;
129 }
130
131 if(leds_arch_leds[led].negative_logic) {
132 gpio_hal_arch_clear_pin(LED_PORT(leds_arch_leds[led]),
133 leds_arch_leds[led].pin);
134 } else {
135 gpio_hal_arch_set_pin(LED_PORT(leds_arch_leds[led]),
136 leds_arch_leds[led].pin);
137 }
138}
139/*---------------------------------------------------------------------------*/
140void
142{
143 if(led >= LEDS_COUNT) {
144 return;
145 }
146
147 if(leds_arch_leds[led].negative_logic) {
148 gpio_hal_arch_set_pin(LED_PORT(leds_arch_leds[led]),
149 leds_arch_leds[led].pin);
150 } else {
151 gpio_hal_arch_clear_pin(LED_PORT(leds_arch_leds[led]),
152 leds_arch_leds[led].pin);
153 }
154}
155/*---------------------------------------------------------------------------*/
156void
158{
159 if(led >= LEDS_COUNT) {
160 return;
161 }
162
163 gpio_hal_arch_toggle_pin(LED_PORT(leds_arch_leds[led]),
164 leds_arch_leds[led].pin);
165}
166/*---------------------------------------------------------------------------*/
167void
169{
170 leds_num_t led;
171
172 for(led = 0; led < LEDS_COUNT; led++) {
173 if((1 << led) & leds) {
174 if(leds_arch_leds[led].negative_logic) {
175 gpio_hal_arch_clear_pin(LED_PORT(leds_arch_leds[led]),
176 leds_arch_leds[led].pin);
177 } else {
178 gpio_hal_arch_set_pin(LED_PORT(leds_arch_leds[led]),
179 leds_arch_leds[led].pin);
180 }
181 }
182 }
183}
184/*---------------------------------------------------------------------------*/
185void
187{
188 leds_num_t led;
189
190 for(led = 0; led < LEDS_COUNT; led++) {
191 if((1 << led) & leds) {
192 if(leds_arch_leds[led].negative_logic) {
193 gpio_hal_arch_set_pin(LED_PORT(leds_arch_leds[led]),
194 leds_arch_leds[led].pin);
195 } else {
196 gpio_hal_arch_clear_pin(LED_PORT(leds_arch_leds[led]),
197 leds_arch_leds[led].pin);
198 }
199 }
200 }
201}
202/*---------------------------------------------------------------------------*/
203void
205{
206 leds_num_t led;
207
208 for(led = 0; led < LEDS_COUNT; led++) {
209 if((1 << led) & leds) {
210 gpio_hal_arch_toggle_pin(LED_PORT(leds_arch_leds[led]),
211 leds_arch_leds[led].pin);
212 }
213 }
214}
215/*---------------------------------------------------------------------------*/
216void
218{
220 leds_on(leds);
221}
222/*---------------------------------------------------------------------------*/
225{
226 leds_mask_t rv = 0;
227 leds_num_t led;
228 uint8_t pin_state;
229
230 for(led = 0; led < LEDS_COUNT; led++) {
231 pin_state = gpio_hal_arch_read_pin(LED_PORT(leds_arch_leds[led]),
232 leds_arch_leds[led].pin);
233
234 if((leds_arch_leds[led].negative_logic == false && pin_state == 1) ||
235 (leds_arch_leds[led].negative_logic == true && pin_state == 0)) {
236 rv |= 1 << led;
237 }
238 }
239
240 return rv;
241}
242/*---------------------------------------------------------------------------*/
243#endif /* LEDS_LEGACY_API */
244/*---------------------------------------------------------------------------*/
245/**
246 * @}
247 */
Header file for the GPIO HAL.
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition clock.c:164
#define LEDS_ALL
The OR mask representation of all device LEDs.
Definition leds.h:195
uint8_t leds_mask_t
An OR mask datatype to represents multiple LEDs.
Definition leds.h:164
uint8_t leds_num_t
The LED number.
Definition leds.h:159
#define LEDS_COUNT
The number of LEDs present on a device.
Definition leds.h:189
leds_mask_t leds_get()
Get the status of LEDs.
Definition leds.c:224
void leds_single_on(leds_num_t led)
Turn a single LED on.
Definition leds.c:125
void leds_single_toggle(leds_num_t led)
Toggle a single LED.
Definition leds.c:157
void leds_on(leds_mask_t leds)
Turn on multiple LEDs.
Definition leds.c:168
void leds_single_off(leds_num_t led)
Turn a single LED off.
Definition leds.c:141
void leds_off(leds_mask_t leds)
Turn off multiple LEDs.
Definition leds.c:186
void leds_toggle(leds_mask_t leds)
Toggle multiple LEDs.
Definition leds.c:204
void leds_init()
Initialise the LED HAL.
Definition leds.c:113
void leds_set(leds_mask_t leds)
Set all LEDs to a specific state.
Definition leds.c:217
Header file for the LED HAL.
A LED logical representation.
Definition leds.h:219