-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathSA56004X.device.nut
137 lines (117 loc) · 3.01 KB
/
SA56004X.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
// Temperature Sensor Class for SA56004X
class SA56004X
{
i2cPort = null;
i2cAddress = null;
conversionRate = 0x04;
constructor(port, address)
{
if(port == I2C_12)
{
// Configure I2C bus on pins 1 & 2
hardware.configure(I2C_12);
i2cPort = hardware.i2c12;
}
else if(port == I2C_89)
{
// Configure I2C bus on pins 8 & 9
hardware.configure(I2C_89);
i2cPort = hardware.i2c89;
}
else
{
server.log("Invalid I2C port specified.");
}
i2cAddress = address << 1;
// Configure device for single shot, no alarms
write(0x09, 0xD5);
// Set default conversion rate (1Hz)
setRate(conversionRate);
}
// Read a byte
function read(register)
{
local data = i2cPort.read(i2cAddress, format("%c", register), 1);
if(data == null)
{
server.log("I2C Read Failure");
return -1;
}
return data[0];
}
// Write a byte
function write(register, data)
{
i2cPort.write(i2cAddress, format("%c%c", register, data));
}
// Set continuous conversion rate, 0 = 0.06Hz, 4 = 1Hz, 9 = 32Hz
function setRate(rate)
{
if(rate >= 0 && rate <= 9)
{
write(0x0a, rate);
conversionRate = rate;
}
else
{
write(0x0a, 0x04);
conversionRate = 0x04;
server.log("Invalid conversion rate, using default 1Hz");
}
}
// Stop continuous conversion
function stop()
{
write(0x09, 0xD5);
}
// Start conversion, continuous or single shot
function start(continuous)
{
if(continuous == true)
{
write(0x09, 0x55);
}
else
{
write(0x0f, 0x00);
}
}
// Check if conversion is completed
function isReady()
{
return (read(0x02) & 0x80)?false:true;
}
// Retrieve temperature (from local sensor) in deg C
function getTemperature()
{
// Get 11-bit signed temperature value in 0.125C steps
local temp = (read(0x00) << 3) | (read(0x22) >> 5);
if(temp & 0x400)
{
// Negative two's complement value
return -((~temp & 0x7FF) + 1) / 8.0;
}
else
{
// Positive value
return temp / 8.0;
}
}
}
// Instantiate the sensor
local sensor = TemperatureSensor(I2C_89, 0x4c);
// Capture and log a temperature reading every 5s
function capture()
{
// Set timer for the next capture
imp.wakeup(30.0, capture);
// Start a single shot conversion
sensor.start(false);
// Wait for conversion to complete
while(!sensor.isReady()) imp.sleep(0.05);
// Output the temperature
local temp = sensor.getTemperature();
server.log(format("%3.1fC", temp));
}
// Start capturing temperature
capture();