-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightTempC.nc
executable file
·172 lines (147 loc) · 3.36 KB
/
LightTempC.nc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<stdio.h>
#include "LightTemp.h"
#define LIGHTFREQ 1000
#define TEMPFREQ 2000
#define LIGHTLIMIT 30
#define TEMPLIMIT 85
#define RADIOFREQ 4000
module LightTempC @safe()
{
uses interface Timer<TMilli> as Timer0;
uses interface Timer<TMilli> as Timer1;
uses interface Leds;
uses interface Boot;
uses interface Read<uint16_t> as Temp;
uses interface Read<uint16_t> as Light;
uses interface Receive;
uses interface AMSend;
uses interface Packet;
uses interface SplitControl as RadioControl;
}
implementation
{
uint16_t RADFREQ = 0;
message_t packet;
bool lock = FALSE;
uint16_t lux;
event void Boot.booted()
{
call RadioControl.start();
}
event void RadioControl.startDone(error_t err) {
if (err == SUCCESS) {
call Timer0.startPeriodic(LIGHTFREQ);
call Timer1.startPeriodic(TEMPFREQ);
}
}
event void RadioControl.stopDone(error_t err) {
}
event void Timer0.fired()
{
call Light.read();
}
event void Timer1.fired()
{
call Temp.read();
}
event void Light.readDone(error_t result, uint16_t data)
{
lux = 2.5 * 6250.0 * (data/4096.0);
printf("\nLuminosity is: %d",lux);
RADFREQ += LIGHTFREQ;
if (result == SUCCESS)
{
/*if (lux < LIGHTLIMIT)
{
call Leds.led2On();
}
else
{
call Leds.led2Off();
}
*/
}
}
event void Temp.readDone(error_t result, uint16_t data)
{
radio_sense_msg_t* rsm;
uint16_t celsius = -39.6 + (0.01 * data);
uint16_t farenheit = (((9.0 * celsius)/5)+32);
RADFREQ += TEMPFREQ;
printf("\nTemperature is: %d", farenheit);
if (result == SUCCESS)
{
/*
if (farenheit > TEMPLIMIT)
{
call Leds.led1On();
}
else
{
call Leds.led1Off();
}
*/
/*
if (RADFREQ == RADIOFREQ)
{
if (lock) return;
else
{
rsm = (radio_sense_msg_t*)call Packet.getPayload(&packet, sizeof(radio_sense_msg_t));
if (rsm == NULL) {
return;
}
rsm->error = result;
if ((farenheit >= TEMPLIMIT)&&(lux >= LIGHTLIMIT))
rsm->data = 3;
else if ((farenheit < TEMPLIMIT)&&(lux >= LIGHTLIMIT))
rsm->data = 1;
if ((farenheit >= TEMPLIMIT)&&(lux < LIGHTLIMIT))
rsm->data = 2;
else
rsm->data = 3;
if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_sense_msg_t)) == SUCCESS)
{
lock = TRUE;
}
}
}
*/
}
}
event void AMSend.sendDone(message_t* bufPtr, error_t error) {
if (&packet == bufPtr) {
lock = FALSE;
}
}
event message_t* Receive.receive(message_t* bufPtr,
void* payload, uint8_t len)
{
uint16_t luxv, tempv;
if (len != sizeof(radio_sense_msg_t)) {
return bufPtr;
}
else
{
radio_sense_msg_t* rsm = (radio_sense_msg_t*)payload;
luxv = rsm->data % 2;
tempv = rsm->data / 2;
printf("\nLuminosity is: %d", lux);
if (luxv <= 0)
{
call Leds.led2On();
}
else{
call Leds.led2Off();
}
if (tempv <= 0)
{
call Leds.led1On();
}
else{
call Leds.led1Off();
}
}
return bufPtr;
}
}