Contiki-Inga 3.x
leds.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 #include "dev/leds.h"
34 #include "sys/clock.h"
35 #include "sys/energest.h"
36 
37 static unsigned char leds, invert;
38 /*---------------------------------------------------------------------------*/
39 static void
40 show_leds(unsigned char changed)
41 {
42  if(changed & LEDS_GREEN) {
43  /* Green did change */
44  if((invert ^ leds) & LEDS_GREEN) {
45  ENERGEST_ON(ENERGEST_TYPE_LED_GREEN);
46  } else {
47  ENERGEST_OFF(ENERGEST_TYPE_LED_GREEN);
48  }
49  }
50  if(changed & LEDS_YELLOW) {
51  if((invert ^ leds) & LEDS_YELLOW) {
52  ENERGEST_ON(ENERGEST_TYPE_LED_YELLOW);
53  } else {
54  ENERGEST_OFF(ENERGEST_TYPE_LED_YELLOW);
55  }
56  }
57  if(changed & LEDS_RED) {
58  if((invert ^ leds) & LEDS_RED) {
59  ENERGEST_ON(ENERGEST_TYPE_LED_RED);
60  } else {
61  ENERGEST_OFF(ENERGEST_TYPE_LED_RED);
62  }
63  }
64  leds_arch_set(leds ^ invert);
65 }
66 /*---------------------------------------------------------------------------*/
67 void
68 leds_init(void)
69 {
71  leds = invert = 0;
72 }
73 /*---------------------------------------------------------------------------*/
74 void
76 {
77  /* Blink all leds. */
78  unsigned char inv;
79  inv = ~(leds ^ invert);
80  leds_invert(inv);
81 
82  clock_delay(400);
83 
84  leds_invert(inv);
85 }
86 /*---------------------------------------------------------------------------*/
87 unsigned char
88 leds_get(void) {
89  return leds_arch_get();
90 }
91 /*---------------------------------------------------------------------------*/
92 void
93 leds_on(unsigned char ledv)
94 {
95  unsigned char changed;
96  changed = (~leds) & ledv;
97  leds |= ledv;
98  show_leds(changed);
99 }
100 /*---------------------------------------------------------------------------*/
101 void
102 leds_off(unsigned char ledv)
103 {
104  unsigned char changed;
105  changed = leds & ledv;
106  leds &= ~ledv;
107  show_leds(changed);
108 }
109 /*---------------------------------------------------------------------------*/
110 void
111 leds_toggle(unsigned char ledv)
112 {
113  leds_invert(ledv);
114 }
115 /*---------------------------------------------------------------------------*/
116 /* invert the invert register using the leds parameter */
117 void
118 leds_invert(unsigned char ledv) {
119  invert = invert ^ ledv;
120  show_leds(ledv);
121 }
122 /*---------------------------------------------------------------------------*/