Contiki 2.5
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  * @(#)$Id: leds.c,v 1.7 2009/02/24 21:30:20 adamdunkels Exp $
32  */
33 
34 #include "dev/leds.h"
35 #include "sys/clock.h"
36 #include "sys/energest.h"
37 
38 static unsigned char leds, invert;
39 /*---------------------------------------------------------------------------*/
40 static void
41 show_leds(unsigned char changed)
42 {
43  if(changed & LEDS_GREEN) {
44  /* Green did change */
45  if((invert ^ leds) & LEDS_GREEN) {
46  ENERGEST_ON(ENERGEST_TYPE_LED_GREEN);
47  } else {
48  ENERGEST_OFF(ENERGEST_TYPE_LED_GREEN);
49  }
50  }
51  if(changed & LEDS_YELLOW) {
52  if((invert ^ leds) & LEDS_YELLOW) {
53  ENERGEST_ON(ENERGEST_TYPE_LED_YELLOW);
54  } else {
55  ENERGEST_OFF(ENERGEST_TYPE_LED_YELLOW);
56  }
57  }
58  if(changed & LEDS_RED) {
59  if((invert ^ leds) & LEDS_RED) {
60  ENERGEST_ON(ENERGEST_TYPE_LED_RED);
61  } else {
62  ENERGEST_OFF(ENERGEST_TYPE_LED_RED);
63  }
64  }
65  leds_arch_set(leds ^ invert);
66 }
67 /*---------------------------------------------------------------------------*/
68 void
69 leds_init(void)
70 {
72  leds = invert = 0;
73 }
74 /*---------------------------------------------------------------------------*/
75 void
77 {
78  /* Blink all leds. */
79  unsigned char inv;
80  inv = ~(leds ^ invert);
81  leds_invert(inv);
82 
83  clock_delay(400);
84 
85  leds_invert(inv);
86 }
87 /*---------------------------------------------------------------------------*/
88 unsigned char
89 leds_get(void) {
90  return leds_arch_get();
91 }
92 /*---------------------------------------------------------------------------*/
93 void
94 leds_on(unsigned char ledv)
95 {
96  unsigned char changed;
97  changed = (~leds) & ledv;
98  leds |= ledv;
99  show_leds(changed);
100 }
101 /*---------------------------------------------------------------------------*/
102 void
103 leds_off(unsigned char ledv)
104 {
105  unsigned char changed;
106  changed = leds & ledv;
107  leds &= ~ledv;
108  show_leds(changed);
109 }
110 /*---------------------------------------------------------------------------*/
111 void
112 leds_toggle(unsigned char ledv)
113 {
114  leds_invert(ledv);
115 }
116 /*---------------------------------------------------------------------------*/
117 /* invert the invert register using the leds parameter */
118 void
119 leds_invert(unsigned char ledv) {
120  invert = invert ^ ledv;
121  show_leds(ledv);
122 }
123 /*---------------------------------------------------------------------------*/