forked from bitbank2/sense_hat_unchained
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensehat.c
395 lines (359 loc) · 9.99 KB
/
sensehat.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// Sense Hat Unchained
// A simple set of C functions to initialize and
// read data from the sensors on the Sense Hat
//
// These functions are not meant to be comprehensive
// but merely to be a simple example of using
// standard Linux file system drivers to talk to
// I2C devices.
//
// The Sense Hat consists of the following:
// 8x8 LED array mapped to a microcontroller at 0x46
// 5-way joystick mapped to the same microcontroller
// HTS221 humidity/temp sensor at 0x5F
// LPS25H pressure/temp sensor at 0x5C
// LSM9DS1 accel/gyro/mag mapped to 0x1C (mag) 0x6A (accel)
//
// Written by Larry Bank - 11/10/2017
// Copyright (c) 2017 BitBank Software, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
//
// The LED Array is handled by a dedicated microcontroller
// It must be updated in a single shot by writing
// 192 bytes starting at register 0
// The memory is laid out in each row like this:
// RRRRRRRRGGGGGGGGBBBBBBBB
// Each byte can have 64 unique levels (0-63)
//
static unsigned char LEDArray[192];
// I2C file handles
static int file_led = -1; // LED array
static int file_hum = -1; // humidity/temp sensor
static int file_pres = -1; // pressure sensor
static int file_acc = -1; // accelerometer/gyro
static int file_mag = -1; // magnetometer
static int i2cRead(int iHandle, unsigned char ucAddr, unsigned char *buf, int iLen);
static int i2cWrite(int iHandle, unsigned char ucAddr, unsigned char *buf, int iLen);
// humidity/temp calibration values
static int H0_rH_x2, H1_rH_x2, T0_degC_x8;
static int T1_degC_x8, H0_T0_OUT;
static int H1_T0_OUT, T0_OUT, T1_OUT;
//
// Opens file system handles to the I2C devices
//
int shInit(int iChannel)
{
unsigned char ucTemp[32];
char filename[32];
sprintf(filename, "/dev/i2c-%d", iChannel);
if ((file_led = open(filename, O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open the i2c bus; need to run as sudo?\n");
return -1;
}
/*
if (ioctl(file_led, I2C_SLAVE, 0x46) < 0)
{
fprintf(stderr, "Failed to acquire bus for LED matrix\n");
goto badexit;
}
*/
file_acc = open(filename, O_RDWR);
if (ioctl(file_acc, I2C_SLAVE, 0x6a) < 0)
{
fprintf(stderr, "Failed to acquire bus for accelerometer\n");
goto badexit;
}
file_mag = open(filename, O_RDWR);
if (ioctl(file_mag, I2C_SLAVE, 0x1c) < 0)
{
fprintf(stderr, "Failed to acquire bus for magnetometer\n");
goto badexit;
}
// Fill the LED with black
/*
memset(LEDArray, 0, sizeof(LEDArray));
i2cWrite(file_led, 0, LEDArray, sizeof(LEDArray));
*/
file_hum = open(filename, O_RDWR);
if (ioctl(file_hum, I2C_SLAVE, 0x5f) < 0)
{
fprintf(stderr, "Failed to acquire bus for Humidity sensor\n");
goto badexit;
}
file_pres = open(filename, O_RDWR);
if (ioctl(file_pres, I2C_SLAVE, 0x5C) < 0)
{
fprintf(stderr, "Failed to aquire bus for Pressure sensor\n");
goto badexit;
}
// Prepare humidity sensor
i2cRead(file_hum, 0x10, ucTemp, 1); // AV_CONF
ucTemp[0] &= 0xc0;
ucTemp[0] |= 0x1b; // avgt=16, avgh=32
i2cWrite(file_hum, 0x10, ucTemp, 1);
i2cRead(file_hum, 0x20+0x80, ucTemp, 3); // get CTRL_REG 1-3
ucTemp[0] &= 0x78; // keep reserved bits
ucTemp[0] |= 0x81; // turn on + 1Hz sample rate
ucTemp[1] &= 0x7c; // turn off heater + boot + one shot
i2cWrite(file_hum, 0x20+0x80, ucTemp, 3); // turn on + set sample rate
// Get the H/T calibration values
i2cRead(file_hum, 0x30+0x80, ucTemp, 16);
H0_rH_x2 = ucTemp[0];
H1_rH_x2 = ucTemp[1];
T0_degC_x8 = ucTemp[2];
T1_degC_x8 = ucTemp[3];
T0_degC_x8 |= ((ucTemp[5] & 0x3) << 8); // 2 msb bits
T1_degC_x8 |= ((ucTemp[5] & 0xc) << 6);
H0_T0_OUT = ucTemp[6] | (ucTemp[7] << 8);
H1_T0_OUT = ucTemp[10] | (ucTemp[11] << 8);
T0_OUT = ucTemp[12] | (ucTemp[13] << 8);
T1_OUT = ucTemp[14] | (ucTemp[15] << 8);
if (H0_T0_OUT > 32767) H0_T0_OUT -= 65536; // signed
if (H1_T0_OUT > 32767) H1_T0_OUT -= 65536;
if (T0_OUT > 32767) T0_OUT -= 65536;
if (T1_OUT > 32767) T1_OUT -= 65536;
// prepare pressure sensor
ucTemp[0] = 0x90; // turn on and set 1Hz update
i2cWrite(file_pres, 0x20, ucTemp, 1);
// Init magnetometer
ucTemp[0] = 0x48; // output data rate/power mode
ucTemp[1] = 0x00; // default scale
ucTemp[2] = 0x00; // continuous conversion
ucTemp[3] = 0x08; // high performance mode
i2cWrite(file_mag, 0x20+0x80, ucTemp, 4);
// Init accelerometer/gyroscope
ucTemp[0] = 0x60; // 119hz accel
i2cWrite(file_acc, 0x20, ucTemp, 1);
ucTemp[0] = 0x38; // enable gyro on all axes
i2cWrite(file_acc, 0x1e, ucTemp, 1);
ucTemp[0] = 0x28; // data rate + full scale + bw selection
// bits: ODR_G2 | ODR_G1 | ODR_G0 | FS_G1 | FS_G0 | 0 | BW_G1 | BW_G0
// 0x28 = 14.9hz, 500dps
i2cWrite(file_acc, 0x10, ucTemp, 1); // gyro ctrl_reg1
return 1;
// problems opening the I2C handles/addresses
badexit:
/*
if (file_led != -1)
{
close(file_led);
file_led = -1;
}
*/
if (file_hum != -1)
{
close(file_hum);
file_hum = -1;
}
if (file_pres != -1)
{
close(file_pres);
file_pres = -1;
}
if (file_acc != -1)
{
close(file_acc);
file_acc = -1;
}
if (file_mag != -1)
{
close(file_mag);
file_mag = -1;
}
return 0;
} /* shInit() */
//
// Set a single pixel on the 8x8 LED Array
//
int shSetPixel(int x, int y, uint16_t color, int bUpdate)
{
int i;
if (x >= 0 && x < 8 && y >= 0 && y < 8 && file_led >= 0)
{
i = (y*24)+x; // offset into array
LEDArray[i] = (uint8_t)((color >> 10) & 0x3e); // Red
LEDArray[i+8] = (uint8_t)((color >> 5) & 0x3f); // Green
LEDArray[i+16] = (uint8_t)((color << 1) & 0x3e); // Blue
if (bUpdate)
{
i2cWrite(file_led, 0, LEDArray, 192); // have to send the whole array at once
}
return 1;
}
return 0;
} /* shSetPixel() */
int shGetAccel(int *Ax, int *Ay, int *Az)
{
unsigned char ucTemp[8];
int rc;
rc = i2cRead(file_acc, 0x28+0x80, ucTemp, 6);
if (rc == 6)
{
int x, y, z;
x = ucTemp[0] + (ucTemp[1] << 8);
y = ucTemp[2] + (ucTemp[3] << 8);
z = ucTemp[4] + (ucTemp[5] << 8);
// fix the signed values
if (x > 32767) x -= 65536;
if (y > 32767) y -= 65536;
if (z > 32767) z -= 65536;
*Ax = x; *Ay = y; *Az = z;
return 1;
}
return 0;
} /* shGetAccel() */
int shGetGyro(int *Gx, int *Gy, int *Gz)
{
unsigned char ucTemp[8];
int rc;
rc = i2cRead(file_acc, 0x18+0x80, ucTemp, 6);
if (rc == 6)
{
*Gx = ucTemp[0] + (ucTemp[1] << 8);
*Gy = ucTemp[2] + (ucTemp[3] << 8);
*Gz = ucTemp[4] + (ucTemp[5] << 8);
return 1;
}
return 0;
} /* shGetGyro() */
int shGetMagneto(int *Mx, int *My, int *Mz)
{
unsigned char ucTemp[8];
int rc;
rc = i2cRead(file_mag, 0x28+0x80, ucTemp, 6);
if (rc == 6)
{
int x, y, z;
x = ucTemp[0] + (ucTemp[1] << 8);
y = ucTemp[2] + (ucTemp[3] << 8);
z = ucTemp[4] + (ucTemp[5] << 8);
// fix signed values
if (x > 32767) x -= 65536;
if (y > 32767) y -= 65536;
if (z > 32767) z -= 65536;
*Mx = z; *My = y; *Mz = z;
return 1;
}
return 0;
} /* shGetMagneto() */
//
// Returns the air pressure in hPa and temp in C * 10 (18.1 = 181)
//
// 1 = pressure successfully read
// 0 = failed to read pressure
//
int shGetPressure(int *Pressure, int *Temp)
{
unsigned char ucTemp[8];
int rc, P, T;
if (file_pres != -1 && Pressure != NULL)
{
rc = i2cRead(file_pres, 0x28+0x80, ucTemp, 5);
if (rc == 5)
{
P = ucTemp[0] + (ucTemp[1]<<8) + (ucTemp[2]<<16);
*Pressure = P / 4096; //hPa
T = ucTemp[3] + (ucTemp[4] << 8);
if (T > 32767) T -= 65536; // twos compliment
T = 425 + (T / 48); // 42.5 + T value/480
*Temp = T;
}
return 1;
}
return 0;
} /* shGetPressure() */
int shGetTempHumid(int *Temp, int *Humid)
{
unsigned char ucTemp[4];
int rc;
int H_T_out, T_out, T0_degC, T1_degC;
int H0_rh, H1_rh;
int tmp;
rc = i2cRead(file_hum, 0x28+0x80, ucTemp, 4);
if (rc == 4)
{
H_T_out = ucTemp[0] + (ucTemp[1] << 8);
T_out = ucTemp[2] + (ucTemp[3] << 8);
if (H_T_out > 32767) H_T_out -=65536;
if (T_out > 32767) T_out -= 65536;
T0_degC = T0_degC_x8 / 8;
T1_degC = T1_degC_x8 / 8;
H0_rh = H0_rH_x2 / 2;
H1_rh = H1_rH_x2 / 2;
tmp = (H_T_out - H0_T0_OUT) * (H1_rh - H0_rh)*10;
*Humid = tmp / (H1_T0_OUT - H0_T0_OUT) + H0_rh*10;
tmp = (T_out - T0_OUT) * (T1_degC - T0_degC)*10;
*Temp = tmp / (T1_OUT - T0_OUT) + T0_degC*10;
return 1;
}
return 0; // not ready
} /* shGetTempHumid() */
void shShutdown(void)
{
// Blank the LED array
memset(LEDArray, 0, sizeof(LEDArray));
i2cWrite(file_led, 0, LEDArray, 192);
// Close all I2C file handles
if (file_led != -1) close(file_led);
if (file_hum != -1) close(file_hum);
if (file_pres != -1) close(file_pres);
if (file_acc != -1) close(file_acc);
if (file_mag != -1) close(file_mag);
file_led = file_hum = file_pres = file_acc = file_mag = -1;
} /* shShutdown() */
static int i2cRead(int iHandle, unsigned char ucAddr, unsigned char *buf, int iLen)
{
int rc;
rc = write(iHandle, &ucAddr, 1);
if (rc == 1)
{
rc = read(iHandle, buf, iLen);
}
return rc;
} /* i2cRead() */
int i2cWrite(int iHandle, unsigned char ucAddr, unsigned char *buf, int iLen)
{
unsigned char ucTemp[512];
int rc;
if (iLen > 511 || iLen < 1 || buf == NULL)
return -1; // invalid write
ucTemp[0] = ucAddr; // send the register number first
memcpy(&ucTemp[1], buf, iLen); // followed by the data
rc = write(iHandle, ucTemp, iLen+1);
return rc-1;
} /* i2cWrite() */
unsigned char shReadJoystick(void)
{
unsigned char ucBuf[2];
int rc;
if (file_led != -1)
{
rc = i2cRead(file_led, 0xf2, ucBuf, 1);
if (rc == 1)
return ucBuf[0];
}
return 0;
} /* shReadJoystick() */