00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #pragma systemFile
00060
00061 #ifndef __COMMON_H__
00062 #define __COMMON_H__
00063
00064 #undef __COMMON_H_DEBUG__
00065
00066
00067
00068 #ifndef __COMMON_H_SENSOR_CHECK__
00069 #define __COMMON_H_SENSOR_CHECK__ 1
00070 #else
00071 #warn "sensor checking disabled, I hope you know what you are doing!"
00072 #endif
00073
00074 #include "firmwareVersion.h"
00075 #if (kFirmwareVersion < 785)
00076 #error "These drivers are only supported on RobotC version 2.0 or higher"
00077 #endif
00078
00079 #ifndef MAX_ARR_SIZE
00080
00081
00082
00083
00084
00085 #define MAX_ARR_SIZE 17
00086 #endif
00087
00088
00089
00090
00091
00092 #define min2(a, b) (a < b ? a : b)
00093
00094
00095
00096
00097
00098 #define min3(a, b, c) (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c)
00099
00100
00101
00102
00103 #define max2(a, b) (a > b ? a : b)
00104
00105
00106
00107
00108
00109 #define max3(a, b, c) (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)
00110
00111
00112
00113
00114
00115
00116 #define clip(a, b, c) min2(c, max2(b, a))
00117
00118
00119
00120
00121
00122 #define round(x) (int)(x + 0.5)
00123
00124
00125
00126
00127
00128
00129 typedef ubyte tByteArray[MAX_ARR_SIZE];
00130 typedef sbyte tsByteArray[MAX_ARR_SIZE];
00131
00132
00133
00134
00135
00136 typedef int tIntArray[MAX_ARR_SIZE];
00137
00138 void clearI2CError(tSensors link, ubyte address);
00139 bool waitForI2CBus(tSensors link);
00140 bool writeI2C(tSensors link, tByteArray &data, int replylen);
00141 bool readI2C(tSensors link, tByteArray &data, int replylen);
00142
00143
00144
00145
00146
00147
00148
00149
00150 void clearI2CError(tSensors link, ubyte address) {
00151 ubyte error_array[2];
00152 error_array[0] = 1;
00153 error_array[1] = address;
00154
00155 #ifdef __COMMON_H_DEBUG__
00156 eraseDisplay();
00157 nxtDisplayTextLine(3, "rxmit: %d", ubyteToInt(error_array[1]));
00158 wait1Msec(2000);
00159 #endif // __COMMON_H_DEBUG__
00160
00161 for (int i = 0; i < 5; i++) {
00162 sendI2CMsg(link, error_array[0], 0);
00163 wait1Msec(5);
00164 }
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 bool waitForI2CBus(tSensors link)
00174 {
00175
00176 while (true)
00177 {
00178
00179 switch (nI2CStatus[link])
00180
00181 {
00182 case NO_ERR:
00183 return true;
00184
00185 case STAT_COMM_PENDING:
00186 break;
00187
00188 case ERR_COMM_CHAN_NOT_READY:
00189 break;
00190
00191 case ERR_COMM_BUS_ERR:
00192 #ifdef __COMMON_H_DEBUG__
00193 PlaySound(soundLowBuzz);
00194 while (bSoundActive) {}
00195 #endif // __COMMON_H_DEBUG__
00196 return false;
00197 }
00198 }
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 bool writeI2C(tSensors link, tByteArray &data, int replylen) {
00211
00212 #if (__COMMON_H_SENSOR_CHECK__ == 1)
00213 TSensorTypes type = SensorType[link];
00214 if ((type != sensorI2CCustom) &&
00215 (type != sensorI2CCustom9V) &&
00216 (type != sensorI2CCustomFast) &&
00217 (type != sensorI2CCustomFast9V) &&
00218 (type != sensorI2CCustomFastSkipStates9V) &&
00219 (type != sensorI2CCustomFastSkipStates)) {
00220 hogCPU();
00221 PlaySound(soundException);
00222 eraseDisplay();
00223 nxtDisplayCenteredTextLine(0, "3rd Party Driver");
00224 nxtDisplayCenteredTextLine(1, "ERROR");
00225 nxtDisplayCenteredTextLine(2, "You have not");
00226 nxtDisplayCenteredTextLine(3, "setup the sensor");
00227 nxtDisplayCenteredTextLine(4, "port correctly. ");
00228 nxtDisplayCenteredTextLine(5, "Please refer to");
00229 nxtDisplayCenteredTextLine(6, "one of the");
00230 nxtDisplayCenteredTextLine(7, "examples.");
00231 wait1Msec(10000);
00232 StopAllTasks();
00233 }
00234 #endif
00235
00236 if (!waitForI2CBus(link)) {
00237 clearI2CError(link, data[1]);
00238
00239
00240
00241 if (!waitForI2CBus(link))
00242 return false;
00243 }
00244
00245 sendI2CMsg(link, data[0], replylen);
00246
00247 if (!waitForI2CBus(link)) {
00248 clearI2CError(link, data[1]);
00249 sendI2CMsg(link, data[0], replylen);
00250 if (!waitForI2CBus(link))
00251 return false;
00252 }
00253 return true;
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 bool readI2C(tSensors link, tByteArray &data, int replylen) {
00266
00267 #if (__COMMON_H_SENSOR_CHECK__ == 1)
00268 TSensorTypes type = SensorType[link];
00269 if ((type != sensorI2CCustom) &&
00270 (type != sensorI2CCustom9V) &&
00271 (type != sensorI2CCustomFast) &&
00272 (type != sensorI2CCustomFast9V) &&
00273 (type != sensorI2CCustomFastSkipStates9V) &&
00274 (type != sensorI2CCustomFastSkipStates)) {
00275 hogCPU();
00276 PlaySound(soundException);
00277 eraseDisplay();
00278 nxtDisplayCenteredTextLine(0, "3rd Party Driver");
00279 nxtDisplayCenteredTextLine(1, "ERROR");
00280 nxtDisplayCenteredTextLine(2, "You have not");
00281 nxtDisplayCenteredTextLine(3, "setup the sensor");
00282 nxtDisplayCenteredTextLine(4, "port correctly. ");
00283 nxtDisplayCenteredTextLine(5, "Please refer to");
00284 nxtDisplayCenteredTextLine(6, "one of the");
00285 nxtDisplayCenteredTextLine(7, "examples.");
00286 wait1Msec(10000);
00287 StopAllTasks();
00288 }
00289 #endif
00290
00291
00292 memset(data, 0, sizeof(tByteArray));
00293
00294
00295 if (!waitForI2CBus(link))
00296 return false;
00297
00298
00299 readI2CReply(link, data[0], replylen);
00300
00301 return true;
00302 }
00303
00304 #endif // __COMMON_H__
00305
00306
00307
00308
00309
00310