SZSPTP ez430-RF2480 1.0

ZigBee Sensor Network with synchronized time and time-stamped measurements.
led.c
Go to the documentation of this file.
1 /*
2  * led.c
3  *
4  * Date: 19/06/2014
5  * Author: Fernando Biazi Nascimento
6  * Copyright © 2014 Fernando Biazi Nascimento. All rights reserved.
7  *
8  * License of use and copy on file license.txt
9  *
10  */
11 
20 /*
21  * ======== Standard MSP430 includes ========
22  */
23 #include <msp430.h>
24 
25 /*
26  * ======== Includes ========
27  */
28 
29 #include "led.h"
30 #include "im.h"
31 
32 /*
33  * ======== Constants ========
34  */
35 
39 #define LED_REDCTRLMASK 0x0F
40 
43 #define LED_GREENCTRLMASK 0xF0
44 
45 /*
46  * ======== Macros ========
47  */
48 
52 #define LED_REDGET P1OUT & BIT0
53 
56 #define LED_REDSET P1OUT |= BIT0
57 
60 #define LED_REDCLR P1OUT &= ~BIT0
61 
64 #define LED_GREENGET P1OUT & BIT1
65 
68 #define LED_GREENSET P1OUT |= BIT1
69 
72 #define LED_GREENCLR P1OUT &= ~BIT1
73 
74 /*
75  * ======== Types ========
76  */
77 
83 typedef enum led_state {
84  LED_S_IDLE, LED_S_RUNNING
85 } led_state;
86 
87 /*
88  * ======== Global Variables ========
89  */
90 
91 /*
92  * ======== Local Variables ========
93  */
94 
112 
120 UInt16 led_redCfg;
121 
126 
127 /*
128  * ======== Local Functions ========
129  */
130 
131 /*
132  * ================
133  */
134 
141 void led_init() {
142  led_control = 0;
143  led_redCfg = 0x1F40 | 0x0005; // 500ms delay, blink 5 times
144  led_currentState = LED_S_IDLE;
145 }
146 
158 UInt16 led_proccess() {
159  UInt8 n; // number of times red led must blink
160  switch (led_currentState) {
161  case LED_S_IDLE:
162  led_currentState = LED_S_RUNNING;
163  return 10;
164  case LED_S_RUNNING:
165  n = led_redCfg & 0x000F;
166  if (n == 0) { // Don't blink, just wait 200ms, because configuration may change.
167  LED_REDCLR;
168  return 200;
169  } else if ((led_control & LED_REDCTRLMASK) < n) { // Must blink n times.
170  if (LED_REDGET) { // Led is on, add one to count (control) and wait for next time.
171  if (led_redCfg >> 4) // if led off delay is zero, just don't count
172  led_control++; // and the LED will blink indefinitely.
173  LED_REDCLR;
174  return 199;
175  } else { // Led is off, turn it on and wait 5ms to turn off.
176  LED_REDSET;
177  return 1;
178  }
179  } else { // Have blinked n times, clear count (control) and wait the delay to blink again.
180  led_control &= ~LED_REDCTRLMASK;
181  return (led_redCfg >> 4);
182  }
183  default:
184  led_currentState = LED_S_IDLE;
185  return 1;
186  }
187 }
188 
201 void led_redConfigure(UInt8 n, UInt16 offDelay) {
202  led_redCfg = (n & 0x0F) | ((offDelay & 0x0FFF) << 4);
203 }
204 
212 void led_greenUp() {
214  if ((led_control & LED_GREENCTRLMASK) != 0xF0)
215  led_control += 0x10;
216  LED_GREENSET;
218 }
219 
230  if ((led_control & LED_GREENCTRLMASK) != 0x00)
231  led_control -= 0x10;
232  if ((led_control & LED_GREENCTRLMASK) == 0x00)
233  LED_GREENCLR;
235 }
#define LED_REDSET
Turn red LED on.
Definition: led.c:56
Interruption management (enable/disable) interface.
#define LED_GREENCTRLMASK
Mask to get bit's of green LED control.
Definition: led.c:43
#define LED_REDGET
Read red LED status, 0x01: on; 0x00: off.
Definition: led.c:52
#define LED_REDCTRLMASK
Mask to get bit's of red led control.
Definition: led.c:39
UInt16 led_proccess()
This function processes the tasks of the LED, it controls the red LED.
Definition: led.c:158
void im_disable_interrupt()
Disable interrupt.
Definition: im.c:162
led_state
Led state.
Definition: led.c:83
led_state led_currentState
Current state of the LEDs control.
Definition: led.c:125
void im_enable_interrupt()
Enable interrupt.
Definition: im.c:222
void led_greenDown()
Green led down.
Definition: led.c:228
#define LED_GREENCLR
Turn green LED off.
Definition: led.c:72
void led_redConfigure(UInt8 n, UInt16 offDelay)
Configures red led.
Definition: led.c:201
void led_init()
LED Initialization.
Definition: led.c:141
void led_greenUp()
Green led up.
Definition: led.c:212
#define LED_GREENSET
Turn green LED on.
Definition: led.c:68
#define LED_REDCLR
Turn red LED off.
Definition: led.c:60
Led interface.
UInt16 led_redCfg
Configuration of the red LED.
Definition: led.c:120
UInt8 led_control
led_control has different uses for red and green LEDs.
Definition: led.c:111