-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
461 lines (382 loc) · 14 KB
/
common.h
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*!@addtogroup common_includes
* @{
* @defgroup common Main common include
* Commonly used functions used by drivers
* @{
*/
/*
* $Id: common.h 134 2013-03-12 18:15:17Z xander $
*/
/** \file common.h
* \brief Commonly used functions used by drivers.
*
* common.h provides a number of frequently used functions that are useful for writing
* drivers.
* License: You may use this code as you wish, provided you give credit where its due.
*
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER.
*
* Changelog:
* - 0.1: Initial release
* - 0.2: Added version check to issue error when compiling with RobotC < 1.46
* - 0.2: Added __COMMON_H_DEBUG__ to enable/disable sounds when an I2C error occurs
* - 0.2: Removed bool waitForI2CBus(tSensors link, bool silent)
* - 0.3: clearI2CError() added to make writeI2C more robust, I2C bus errors are now handled
* better.
* - 0.4: Added HiTechnic SMUX functions
* - 0.5: Added clip function (Tom Roach)
* - 0.6: clearI2CBus is now conditionally compiled into the FW. Only RobotC < 1.57 needs it.
* - 0.7: ubyteToInt(byte byteVal) modified, works better with 1.57+
* - 0.8: ubyte used for arrays for firmware version 770 and higher<br>
* added support for new colour sensor<br>
* added better handling for when sensor is not configured properly
* - 0.9: added bool HTSMUXsetMode(tSensors link, byte channel, byte mode) prototype<br>
* added int HTSMUXreadAnalogue(tMUXSensor muxsensor)<br>
* added HTSMUXSensorType HTSMUXreadSensorType(tMUXSensor muxsensor)<br>
* added bool HTSMUXreadPort(tMUXSensor muxsensor, tByteArray &result, int numbytes, int offset)<br>
* added bool HTSMUXreadPort(tMUXSensor muxsensor, tByteArray &result, int numbytes)<br>
* added bool HTSMUXsetMode(tMUXSensor muxsensor, byte mode)<br>
* added bool HTSMUXsetAnalogueActive(tMUXSensor muxsensor)<br>
* added bool HTSMUXsetAnalogueInactive(tMUXSensor muxsensor)<br>
* corrected function description for HTSMUXSensorType()
* - 0.10: Removed unnecessary read from HTSMUXsendCommand()
* - 0.11: Added long uByteToLong(byte a1, byte a2, byte a3, byte a4);
* - 0.12: Added HTSMUXreadPowerStatus(tSensors link)<br>
* Added int round(float fl)
* - 0.13: Added motor mux types and data structs
* - 0.14: Added check for digital sensors to prevent conflict with built-in drivers\n
* Changed clearI2CError to take ubyte for address, thanks Aswin
* - 0.15: Removed motor mux and sensor mux functions and types out
* - 0.16: Added max() and min() functions by Mike Henning, Max Bareiss
*
* \author Xander Soldaat (xander_at_botbench.com)
* \date 27 April 2011
* \version 0.16
*/
#pragma systemFile
#ifndef __COMMON_H__
#define __COMMON_H__
#undef __COMMON_H_DEBUG__
//#define __COMMON_H_DEBUG__
/*!< define this as 0 to remove the check */
#ifndef __COMMON_H_SENSOR_CHECK__
#define __COMMON_H_SENSOR_CHECK__ 1
#else
#warn "sensor checking disabled, I hope you know what you are doing!"
#endif
#include "firmwareVersion.h"
#if (kRobotCVersionNumeric < 359)
#error "These drivers are only supported on RobotC version 3.59 or higher"
#endif
#ifndef MAX_ARR_SIZE
/**
* Maximum buffer size for byte_array, can be overridden in your own program.
* It's 17 bytes big because the max I2C buffer size is 16, plus 1 byte to denote
* packet length.
*/
#define MAX_ARR_SIZE 17
#endif
/**
* This define returns the smaller of the two numbers
*/
#define min2(a, b) (a < b ? a : b)
/**
* This define returns the smallest of the three numbers
*/
#define min3(a, b, c) (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c)
/**
* This function returns the bigger of the two numbers
*/
#define max2(a, b) (a > b ? a : b)
/**
* This function returns the biggest of the three numbers
*/
#define max3(a, b, c) (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)
/**
* Returns x if it is between min and max. If outside the range,
* it returns min or max.
*/
#define clip(a, b, c) min2(c, max2(b, a))
/**
* Array of bytes as a struct, this is a work around for RobotC's inability to pass an array to
* a function.
*/
typedef ubyte tByteArray[MAX_ARR_SIZE];
typedef sbyte tsByteArray[MAX_ARR_SIZE];
typedef ubyte tMassiveArray[128]; /*!< 128 byte array for very large blobs of data */
typedef ubyte tHugeByteArray[64]; /*!< 64 byte array for very large blobs of data */
typedef ubyte tBigByteArray[32]; /*!< 32 byte array for large blobs of data */
typedef ubyte tIPaddr[4]; /*!< Struct for holding an IP address */
/**
* Array of ints as a struct, this is a work around for RobotC's inability to pass an array to
* a function.
*/
typedef int tIntArray[MAX_ARR_SIZE];
void clearI2CError(tSensors link, ubyte address);
void clearI2Cbus(tSensors link);
bool waitForI2CBus(tSensors link);
bool writeI2C(tSensors link, tByteArray &request, tByteArray &reply, int replylen);
bool writeI2C(tSensors link, tByteArray &request);
/**
* Clear out the error state on I2C bus by sending a bunch of dummy
* packets.
* @param link the port number
* @param address the I2C address we're sending to
*/
void clearI2CError(tSensors link, ubyte address) {
ubyte error_array[2];
error_array[0] = 1; // Message size
error_array[1] = address; // I2C Address
#ifdef __COMMON_H_DEBUG__
eraseDisplay();
nxtDisplayTextLine(3, "rxmit: %d", ubyteToInt(error_array[1]));
wait1Msec(2000);
#endif // __COMMON_H_DEBUG__
for (int i = 0; i < 5; i++) {
sendI2CMsg(link, &error_array[0], 0);
wait1Msec(5);
}
}
/**
* Wait for the I2C bus to be ready for the next message
* @param link the port number
* @return true if no error occured, false if it did
*/
bool waitForI2CBus(tSensors link)
{
//TI2CStatus i2cstatus;
while (true)
{
//i2cstatus = nI2CStatus[link];
switch (nI2CStatus[link])
//switch(i2cstatus)
{
case NO_ERR:
return true;
case STAT_COMM_PENDING:
break;
case ERR_COMM_CHAN_NOT_READY:
break;
case ERR_COMM_BUS_ERR:
#ifdef __COMMON_H_DEBUG__
PlaySound(soundLowBuzz);
while (bSoundActive) {}
#endif // __COMMON_H_DEBUG__
return false;
}
EndTimeSlice();
}
}
/**
* Write to the I2C bus. This function will clear the bus and wait for it be ready
* before any bytes are sent.
* @param link the port number
* @param request the data to be sent
* @return true if no error occured, false if it did
*/
bool writeI2C(tSensors link, tByteArray &request) {
#if (__COMMON_H_SENSOR_CHECK__ == 1)
//TSensorTypes type = SensorType[link];
switch (SensorType[link])
{
case sensorI2CCustom: break;
case sensorI2CCustom9V: break;
case sensorI2CCustomFast: break;
case sensorI2CCustomFast9V: break;
case sensorI2CCustomFastSkipStates9V: break;
case sensorI2CCustomFastSkipStates: break;
default:
hogCPU();
PlaySound(soundException);
eraseDisplay();
nxtDisplayCenteredTextLine(0, "3rd Party Driver");
nxtDisplayCenteredTextLine(1, "ERROR");
nxtDisplayCenteredTextLine(2, "You have not");
nxtDisplayCenteredTextLine(3, "setup the sensor");
nxtDisplayCenteredTextLine(4, "port correctly. ");
nxtDisplayCenteredTextLine(5, "Please refer to");
nxtDisplayCenteredTextLine(6, "one of the");
nxtDisplayCenteredTextLine(7, "examples.");
wait1Msec(10000);
StopAllTasks();
}
#endif
if (!waitForI2CBus(link)) {
clearI2CError(link, request[1]);
// Let's try the bus again, see if the above packets flushed it out
// clearI2CBus(link);
if (!waitForI2CBus(link))
return false;
}
sendI2CMsg(link, &request[0], 0);
if (!waitForI2CBus(link)) {
clearI2CError(link, request[1]);
sendI2CMsg(link, &request[0], 0);
if (!waitForI2CBus(link))
return false;
}
return true;
}
/**
* Write to the I2C bus. This function will clear the bus and wait for it be ready
* before any bytes are sent.
* @param link the port number
* @param request the data to be sent
* @param reply array to hold received data
* @param replylen the number of bytes (if any) expected in reply to this command
* @return true if no error occured, false if it did
*/
bool writeI2C(tSensors link, tByteArray &request, tByteArray &reply, int replylen) {
// clear the input data buffer
#if (__COMMON_H_SENSOR_CHECK__ == 1)
//TSensorTypes type = SensorType[link];
switch (SensorType[link])
{
case sensorI2CCustom: break;
case sensorI2CCustom9V: break;
case sensorI2CCustomFast: break;
case sensorI2CCustomFast9V: break;
case sensorI2CCustomFastSkipStates9V: break;
case sensorI2CCustomFastSkipStates: break;
default:
hogCPU();
PlaySound(soundException);
eraseDisplay();
nxtDisplayCenteredTextLine(0, "3rd Party Driver");
nxtDisplayCenteredTextLine(1, "ERROR");
nxtDisplayCenteredTextLine(2, "You have not");
nxtDisplayCenteredTextLine(3, "setup the sensor");
nxtDisplayCenteredTextLine(4, "port correctly. ");
nxtDisplayCenteredTextLine(5, "Please refer to");
nxtDisplayCenteredTextLine(6, "one of the");
nxtDisplayCenteredTextLine(7, "examples.");
wait1Msec(10000);
StopAllTasks();
}
#endif
if (!waitForI2CBus(link)) {
clearI2CError(link, request[1]);
// Let's try the bus again, see if the above packets flushed it out
// clearI2CBus(link);
if (!waitForI2CBus(link))
return false;
}
sendI2CMsg(link, &request[0], replylen);
if (!waitForI2CBus(link)) {
clearI2CError(link, request[1]);
sendI2CMsg(link, &request[0], replylen);
if (!waitForI2CBus(link))
return false;
}
// ask for the input to put into the data array
readI2CReply(link, &reply[0], replylen);
return true;
}
/*
bool I2CreadInt(tSensors link, ubyte address, ubyte reg, int &retval, tByteArray request, tByteArray reply, bool msbfirst = true)
{
request[0] = 2; // Message size
request[1] = address; // I2C Address
request[2] = reg;
if (!writeI2C(link, request, reply, 2))
return false;
retval = (msbfirst) ? reply[1] + (reply[0] << 8) : reply[0] + (reply[1] << 8);
return true;
}
bool I2CreadLong(tSensors link, ubyte address, ubyte reg, int &retval, tByteArray request, tByteArray reply, bool msbfirst = true)
{
request[0] = 2; // Message size
request[1] = address; // I2C Address
request[2] = reg;
if (!writeI2C(link, request, reply, 4))
return false;
if (msbfirst)
retval = (reply[0] << 24) + (reply[1] << 16) + (reply[2] << 8) + reply[3];
else
retval = (reply[3] << 24) + (reply[2] << 16) + (reply[1] << 8) + reply[0];
return true;
}
bool I2CreadSByte(tSensors link, ubyte address, ubyte reg, sbyte &retval, tByteArray request, tByteArray reply)
{
request[0] = 2; // Message size
request[1] = address; // I2C Address
request[2] = reg;
if (!writeI2C(link, request, reply, 1))
return false;
retval = (reply[0] >= 128) ? (int)reply[0] - 256 : (int)reply[0];
return true;
}
bool I2CreadUByte(tSensors link, ubyte address, ubyte reg, ubyte &retval, tByteArray request, tByteArray reply)
{
request[0] = 2; // Message size
request[1] = address; // I2C Address
request[2] = reg;
if (!writeI2C(link, request, reply, 1))
return false;
retval = reply[0];
return true;
}
*/
/**
* Create a unique ID (UID) for an NXT. This based on the last 3 bytes
* of the Bluetooth address. The first 3 bytes are manufacturer
* specific and identical for all NXTs and are therefore not used.
* @return a unique ID for the NXT.
*/
long getUID() {
TBTAddress btAddr;
getBTAddress(btAddr);
// Only last 3 bytes are unique in the BT address, the other three are for the
// manufacturer (LEGO): http://www.coffer.com/mac_find/?string=lego
return (long)btAddr[5] + ((long)btAddr[4] << 8) + ((long)btAddr[3] << 16);
}
#define STRTOK_MAX_TOKEN_SIZE 20
#define STRTOK_MAX_BUFFER_SIZE 50
/**
* Tokenise an array of chars, using a seperator
* @param buffer pointer to buffer we're parsing
* @param token pointer to buffer to hold the tokens as we find them
* @param seperator the seperator used between tokens
* @return true if there are still tokens left, false if we're done
*/
bool strtok(char *buffer, char *token, char *seperator)
{
int pos = StringFind(buffer, seperator);
char t_buff[STRTOK_MAX_BUFFER_SIZE];
// Make sure we zero out the buffer and token
memset(token, 0, STRTOK_MAX_TOKEN_SIZE);
memset(&t_buff[0], 0, STRTOK_MAX_BUFFER_SIZE);
// Looks like we found a seperator
if (pos >= 0)
{
// Copy the first token into the token buffer, only if the token is
// not an empty one
if (pos > 0)
memcpy(token, buffer, pos);
// Now copy characters -after- the seperator into the temp buffer
memcpy(&t_buff[0], buffer + (pos + 1), strlen(buffer) - pos);
// Zero out the real buffer
memset(buffer, 0, strlen(buffer) + 1);
// Copy the temp buffer, which now only contains everything after the previous
// token into the buffer for the next round.
memcpy(buffer, &t_buff[0], strlen(&t_buff[0]));
return true;
}
// We found no seperator but the buffer still contains a string
// This can happen when there is no trailing seperator
else if(strlen(buffer) > 0)
{
// Copy the token into the token buffer
memcpy(token, buffer, strlen(buffer));
// Zero out the remainder of the buffer
memset(buffer, 0, strlen(buffer) + 1);
return true;
}
return false;
}
#endif // __COMMON_H__
/*
* $Id: common.h 134 2013-03-12 18:15:17Z xander $
*/
/* @} */
/* @} */