SZSPTP ez430-RF2480 1.0

ZigBee Sensor Network with synchronized time and time-stamped measurements.
but.c
Go to the documentation of this file.
1 /*
2  * but.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 
22 /*
23  * ======== Standard MSP430 includes ========
24  */
25 #include <msp430.h>
26 
27 /*
28  * ======== Includes ========
29  */
30 
31 #include "but.h"
32 #include "nwk.h"
33 
34 /*
35  * ======== Constants ========
36  */
37 
38 /*
39  * ======== Macros ========
40  */
41 
45 #define BUT_SW0BIT BIT2
46 
47 /*
48  * ======== Types ========
49  */
50 
54 typedef enum but_state {
58 } but_state;
59 
60 /*
61  * ======== Global Variables ========
62  */
63 
64 /*
65  * ======== Local Variables ========
66  */
67 
87 UInt32 but_presses;
92 
93 /*
94  * ======== Local Functions ========
95  */
96 
97 /*
98  * ================
99  */
100 
108 void but_init() {
109  but_presses = 0;
110  but_pendingPress = 0;
112  P1IE &= ~BUT_SW0BIT;
113  P1REN |= BUT_SW0BIT;
114  P1OUT |= BUT_SW0BIT;
115  P1IES |= BUT_SW0BIT;
116  P1IFG &= ~BUT_SW0BIT;
117  P1IE |= BUT_SW0BIT;
118 }
119 
132 UInt16 but_proccess() {
133 
134  if (but_presses & 0x80000000) {
135  // count bits set on but_press
136  //eliminate MSB, if there is far 1's, some loops are avoided.
137  but_presses ^= 0x80000000;
138  but_pendingPress = 1;
139  while (but_presses != 0) {
140  while (!(but_presses & 0xFF)) { // eliminate some loops if next 1 is far
141  but_presses >>= 8;
142  }
143  while (!(but_presses & 0x0F)) { // eliminate some loops if next 1 is far
144  but_presses >>= 4;
145  }
146  while (!(but_presses & 0x01)) {
147  but_presses >>= 1;
148  }
150  but_presses >>= 1;
151  }
152  if (but_pendingPress > 3) {
153  // reset device
154  WDTCTL = 0;
155  } else if (but_pendingPress == 2) {
157  } else if (but_pendingPress == 1) {
159  }
160  }
161 
162  switch (but_currentState) {
163  case BUT_S_IDLE:
164  // delay prior presses
165  but_presses <<= 1;
166  // check again in 100ms
167  return 100;
168  case BUT_S_PRESSED:
169  // delay prior presses
170  but_presses <<= 1;
171  // button pressed, register and only enable interrupt after 95ms to avoid bounce
172  but_presses |= 0x01;
174  return 95;
175  case BUT_S_DEBOUNCE:
176  // this happens 95ms after previous call
177  // clear flag, enable interrupt and back to idle
178  P1IFG &= ~BUT_SW0BIT;
179  P1IE |= BUT_SW0BIT;
181  return 5;
182  default:
184  return 1;
185  }
186 }
187 
194  but_pendingPress = 0;
195 }
196 
204 UInt8 but_getPending() {
205  return but_pendingPress;
206 }
207 
216 void BUT_ISR() {
217  if (but_currentState == BUT_S_IDLE) {
219  P1IE &= ~BUT_SW0BIT; // disable interrupt to avoid bounce
220  }
221 }
Network functions interface.
UInt8 but_getPending()
This function get number of button presses on the time window.
Definition: but.c:204
Router.
Definition: nwk.h:224
but_state but_currentState
Current state of the button control.
Definition: but.c:83
UInt32 but_presses
Button presses over time, one bit for each press, shift left over time.
Definition: but.c:87
void BUT_ISR()
Button Interrupt Service Request on port 1.
Definition: but.c:216
void nwk_setDeviceType(nwk_devType newType)
Set device type.
Definition: nwk.c:517
UInt8 but_pendingPress
Number of button presses, it is cleared by but_clearPending.
Definition: but.c:91
but_state
States for the button control.
Definition: but.c:54
Button was pressed.
Definition: but.c:56
Timeout to avoid bounce counting.
Definition: but.c:57
UInt16 but_proccess()
This function processes the tasks of the button control.
Definition: but.c:132
End device.
Definition: nwk.h:225
#define BUT_SW0BIT
Bit on with switch 0 is wired, port 1.2.
Definition: but.c:45
void but_init()
This function initializes the button control variables.
Definition: but.c:108
void but_clearPending()
This function clear pending button presses.
Definition: but.c:193
Waiting for button press.
Definition: but.c:55
Button interface.