-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSA-lib.nxc
290 lines (267 loc) · 5.8 KB
/
LSA-lib.nxc
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
/************************************************************************/
/* */
/* Program Name: LSA-lib.nxc */
/* ============================= */
/* */
/* Copyright (c) 2008 by mindsensors.com */
/* Email: info (<at>) mindsensors (<dot>) com */
/* */
/* 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; version 3 of the License. */
/* Read the license at: http://www.gnu.org/licenses/gpl.txt */
/* */
/************************************************************************/
/*
* When Who Comments
* 08/28/12 Deepak Patil Initial authoring based on LL-lib.nxc
*/
string LSA_format_hex ( byte loop )
{
int j;
string s;
int b = 0xF0;
int a = 0x0F;
j = (loop&b)>>4;
s = "";
switch ( j ) {
case 1:
s += "1";
break;
case 2:
s += "2";
break;
case 3:
s += "3";
break;
case 4:
s += "4";
break;
case 5:
s += "5";
break;
case 6:
s += "6";
break;
case 7:
s += "7";
break;
case 8:
s += "8";
break;
case 9:
s += "9";
break;
case 10:
s += "A";
break;
case 11:
s += "B";
break;
case 12:
s += "C";
break;
case 13:
s += "D";
break;
case 14:
s += "E";
break;
case 15:
s += "F";
break;
case 16:
s += "0";
break;
case 0:
s += "0";
break;
}
j = loop&a;
switch ( j ) {
case 1:
s += "1";
break;
case 2:
s += "2";
break;
case 3:
s += "3";
break;
case 4:
s += "4";
break;
case 5:
s += "5";
break;
case 6:
s += "6";
break;
case 7:
s += "7";
break;
case 8:
s += "8";
break;
case 9:
s += "9";
break;
case 10:
s += "A";
break;
case 11:
s += "B";
break;
case 12:
s += "C";
break;
case 13:
s += "D";
break;
case 14:
s += "E";
break;
case 15:
s += "F";
break;
case 16:
s += "0";
break;
case 0:
s += "0";
break;
}
return (s);
}
int LSA_weighted_average (int i)
{
int s, j, c;
int b = 0x80; s = 0;
c = 0;
for ( j = 0; j < 8; j++) {
if ( i&b ) {
s += (j+1) * 10;
c++;
}
b = b>>1;
}
return (s/c);
}
string LSA_format_bin ( int i ) {
string s;
int j;
int b = 0x80;
s = "";
for ( j = 0; j < 8; j++) {
if ( i&b ) {
s += NumToStr(1);
} else {
s += NumToStr(0);
}
b = b>>1;
}
return (s);
}
int LSA_SendCommand(byte port, byte addr, byte cmd)
{
byte cmdBuf[];
byte nByteReady = 0;
ArrayBuild(cmdBuf, addr, 0x41, cmd);
I2CWrite(port, 1, cmdBuf);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING);
int status = I2CCheckStatus(port);
while (status > NO_ERR)
status = I2CCheckStatus(port);
return status;
}
/**
* The LightSensorAray sensor can be put to sleep, so that
* it's not consuming power when it is not required.
*
* It wakes up on it's own when any i2c communication happens.
* Or you can specifically wake it up by 'P' command.
*/
int LSA_Sleep(byte port, byte addr)
{
return LSA_SendCommand(port, addr, 'D');
}
/**
* Read any byte from LSA
*/
byte LSA_Read (byte port, byte i2cAddr, int reg_to_read)
{
byte message[];
unsigned byte buf[20];
int count, l;
byte nByteReady = 0;
ArrayBuild(message, i2cAddr, reg_to_read);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING);
count = 1;
if(I2CBytes(port, message, count, buf)) {
return buf[0];
}
return 0;
}
/**
* The LightSensorAray sensor can be put to sleep, so that
* it's not consuming power when it is not required.
*
* It wakes up on it's own when any i2c communication happens.
* Or you can specifically wake it up by 'P' command.
*/
int LSA_WakeUp(byte port, byte addr)
{
return LSA_Read(port, addr, 0x42);
}
int LSA_ReadRaw_Calibrated (byte port, byte i2cAddr, byte &returnValue[])
{
byte message[];
unsigned byte buf[8];
int count, l;
byte nByteReady = 0;
// read calibrated readings
ArrayBuild(message, i2cAddr, 0x42);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING);
count = 8;
if(I2CBytes(port, message, count, returnValue)) {
return 1;
}
return 0;
}
// following function is supported in firmware version 1.15 and later.
int LSA_ReadRaw_Uncalibrated (byte port, byte i2cAddr, int &returnValue[])
{
byte message[];
unsigned byte buf[16];
int count, i;
byte nByteReady = 0;
// read uncalibrated readings
ArrayBuild(message, i2cAddr, 0x6A);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING);
count = 8; // read 8 bytes at once.
if(I2CBytes(port, message, count, buf)) {
for ( i=0; i < 4; i++) {
returnValue[i] = buf[i*2] + (buf[(i*2)+1]<<8);
}
}
ArrayBuild(message, i2cAddr, 0x72);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING);
count = 8; // read 8 bytes at once.
if(I2CBytes(port, message, count, buf)) {
for ( i=0; i < 4; i++) {
returnValue[i+4] = buf[i*2] + (buf[(i*2)+1]<<8);
}
return 1;
}
return 0;
}
int LSA_ReadInteger(byte port, byte addr, byte reg ) {
unsigned int result = 0;
byte outBuf[], cmdBuf[], count;
count = 2;
ArrayBuild(cmdBuf, addr, reg);
if (I2CBytes(port, cmdBuf, count, outBuf)) {
result = outBuf[1]*256 + outBuf[0];
}
return result;
}