forked from electricimp/reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhih6131.device.nut
241 lines (187 loc) · 6.89 KB
/
hih6131.device.nut
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
Copyright (C) 2013 Electric Imp, Inc
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// **********************************************************************************************************************************
class sensor {
i2c = null;
pin_en_l = null;
pin_drain = null;
addr = null;
ready = false;
name = "sensor";
static sensorlist = {};
constructor(_i2c=null, _pin_en_l=null, _pin_drain=null, _addr=null) {
i2c = _i2c;
pin_en_l = _pin_en_l;
pin_drain = _pin_drain;
addr = _addr;
::last_activity <- time();
if (i2c) i2c.configure(CLOCK_SPEED_400_KHZ);
if (pin_en_l) pin_en_l.configure(DIGITAL_OUT);
if (pin_drain) pin_drain.configure(DIGITAL_OUT);
// Test the sensor and if its alive then setup a handler to execute all functions of the class
if (test()) {
sensorlist[name] <- this;
agent.on(name, agent_event.bindenv(this));
}
}
function enable() {
if (pin_en_l) pin_en_l.write(0);
if (pin_drain) pin_drain.write(1);
imp.sleep(0.001);
}
function disable() {
if (pin_en_l) pin_en_l.write(1);
if (pin_drain) pin_drain.write(0);
}
function test() {
if (i2c == null) {
ready = false;
} else {
enable();
local t = i2c.read(addr, "", 1);
ready = (t != null);
disable();
}
return ready;
}
function get_nv(key) {
if (("nv" in getroottable()) && (key in ::nv)) {
return ::nv[key];
} else {
return null;
}
}
function set_nv(key, value) {
if (!("nv" in getroottable())) ::nv <- {};
::nv[key] <- value;
}
function dump_nv(root = null) {
if ("nv" in getroottable()) {
if (root == null) root = ::nv;
foreach (k,v in root) {
if (typeof v == "array" || typeof v == "table") {
log("NV: " + k + " => " + v)
dump_nv(v);
} else {
log("NV: " + k + " => " + v)
}
}
} else {
log("NV: Not defined");
}
}
function get_wake_reason() {
switch (hardware.wakereason()) {
case WAKEREASON_POWER_ON: return "power on";
case WAKEREASON_TIMER: return "timer";
case WAKEREASON_SW_RESET: return "software reset";
case WAKEREASON_PIN1: return "pin1 interrupt";
case WAKEREASON_NEW_SQUIRREL: return "new squirrel";
default: return "unknown";
}
}
function get_bootreason() {
// log("GET bootreason: " + get_nv("reason"));
return get_nv("reason");
}
function set_bootreason(_reason = null) {
set_nv("reason", _reason);
// log("SET bootreason to " + _reason);
}
function agent_event(data) {
last_activity = time();
if (data.method in this && typeof this[data.method] == "function") {
// Formulate the function and the callback
local method = this[data.method];
local params = [this];
local callback = remote_response(name, data.method).bindenv(this);
if ("params" in data) {
if (typeof data.params == "array") {
params.extend(data.params);
} else {
params.push(data.params);
}
}
params.push(callback);
// Execute the function call with the parameters and callbacks
try {
method.acall(params);
} catch (e) {
log(format("Exception while executing '%s.%s': %s", name, data.method, e))
}
}
}
function reset() {
if (i2c) {
i2c.write(0x00,format("%c",RESET_VAL));
imp.sleep(0.01);
}
}
function sleep(dur = 600, delay = 0, callback = null) {
switch (hardware.wakereason()) {
case WAKEREASON_POWER_ON:
case WAKEREASON_NEW_SQUIRREL:
delay = delay >= 10 ? delay : 10;
break;
}
server.log("Sleeping in " + delay + " for " + dur + ". Last wake reason: " + get_wake_reason());
imp.wakeup(delay, function() {
imp.onidle(function() {
// Clearing the interrupt pins like this is a bit hacky but it gets the job done.
// If squirrel had a destructor() function, I would prefer to do it there.
if (i2c) i2c.read(addr, lis3dh.INT1_SRC, 1);
if (i2c) i2c.read(addr, lis3dh.TAP_SRC, 1);
server.expectonlinein(dur);
imp.deepsleepfor(dur);
}.bindenv(this))
}.bindenv(this))
}
function remote_response(dev, method) {
return function(data = null) {
agent.send(dev + "." + method, data);
}
}
}
// **********************************************************************************************************************************
class hih6131 extends sensor {
static WAIT = 80; // milliseconds
pin_en_l = null;
pin_drain = null;
name = "thermistor";
constructor(_i2c, _pin_en_l = null, _pin_drain = null, _addr = 0x4E){
base.constructor(_i2c, _pin_en_l, _pin_drain, _addr);
}
function convert(th) {
local t = ((((th[2] << 6 ) | (th[3] >> 2)) * 165) / 16383.0) - 40;
local h = ((((th[0] & 0x3F) << 8 ) | (th[1] )) / 163.83 );
//Round to 2 decimal places
t = (t*100).tointeger() / 100.0;
h = (h*100).tointeger() / 100.0;
return { temperature = t, humidity = h};
}
function read(callback = null) {
if (!ready) return null;
enable();
i2c.write(addr, "");
// Do a non-blocking read
imp.wakeup(WAIT/1000.0, function() {
local th = i2c.read(addr, "", 4);
disable();
if (th == null) {
callback(null);
} else {
callback(convert(th));
}
}.bindenv(this));
}
}