00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __MSDIST_H__
00013 #define __MSDIST_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #pragma systemFile
00038
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042
00043 #define MSDIST_I2C_ADDR 0x02
00044
00045 #define MSDIST_CMD 0x41
00046
00047 #define MSDIST_DIST 0x42
00048 #define MSDIST_VOLT 0x44
00049 #define MSDIST_MOD_TYPE 0x50
00050 #define MSDIST_MINDIST 0x52
00051 #define MSDIST_MAXDIST 0x54
00052
00053 #define MSDIST_GP2D12 0x31
00054 #define MSDIST_GP2D120 0x32
00055 #define MSDIST_GP2YA21 0x33
00056 #define MSDIST_GP2YA02 0x34
00057 #define MSDIST_CUSTOM 0x35
00058
00059 int MSDISTreadDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00060 int MSDISTreadVoltage(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00061 int MSDISTreadMinDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00062 int MSDISTreadMaxDist(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00063 int MSDISTreadModuleType(tSensors link, ubyte address = MSDIST_I2C_ADDR);
00064 bool MSDISTsendCmd(tSensors link, byte command, ubyte address = MSDIST_I2C_ADDR);
00065
00066 tByteArray MSDIST_I2CRequest;
00067 tByteArray MSDIST_I2CReply;
00068
00069 bool MSDISTcalibrated[] = {false, false, false, false};
00070
00071
00072
00073
00074
00075
00076
00077 int MSDISTreadDist(tSensors link, ubyte address) {
00078
00079
00080 if (!MSDISTcalibrated[link]) {
00081 if (!MSDISTsendCmd(link, MSDISTreadModuleType(link),address))
00082
00083 return -1;
00084 else
00085 MSDISTcalibrated[link] = true;
00086 }
00087
00088 memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00089
00090 MSDIST_I2CRequest[0] = 2;
00091 MSDIST_I2CRequest[1] = address;
00092 MSDIST_I2CRequest[2] = MSDIST_DIST;
00093
00094 if (!writeI2C(link, MSDIST_I2CRequest, 2))
00095 return -1;
00096
00097 if (!readI2C(link, MSDIST_I2CReply, 2))
00098 return -1;
00099
00100 return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110 int MSDISTreadVoltage(tSensors link, ubyte address) {
00111 memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00112
00113 MSDIST_I2CRequest[0] = 2;
00114 MSDIST_I2CRequest[1] = address;
00115 MSDIST_I2CRequest[2] = MSDIST_VOLT;
00116
00117 if (!writeI2C(link, MSDIST_I2CRequest, 2))
00118 return -1;
00119
00120 if (!readI2C(link, MSDIST_I2CReply, 2))
00121 return -1;
00122
00123
00124 return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134 int MSDISTreadMinDist(tSensors link, ubyte address) {
00135 memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00136
00137 MSDIST_I2CRequest[0] = 2;
00138 MSDIST_I2CRequest[1] = address;
00139 MSDIST_I2CRequest[2] = MSDIST_MINDIST;
00140
00141 if (!writeI2C(link, MSDIST_I2CRequest, 2))
00142 return -1;
00143
00144 if (!readI2C(link, MSDIST_I2CReply, 2))
00145 return -1;
00146
00147
00148 return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158 int MSDISTreadMaxDist(tSensors link, ubyte address) {
00159 memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00160
00161 MSDIST_I2CRequest[0] = 2;
00162 MSDIST_I2CRequest[1] = address;
00163 MSDIST_I2CRequest[2] = MSDIST_MAXDIST;
00164
00165 if (!writeI2C(link, MSDIST_I2CRequest, 2))
00166 return -1;
00167
00168 if (!readI2C(link, MSDIST_I2CReply, 2))
00169 return -1;
00170
00171
00172 return (0x00FF & MSDIST_I2CReply[0]) + ((0x00FF & MSDIST_I2CReply[1]) <<8);
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182 int MSDISTreadModuleType(tSensors link, ubyte address) {
00183 memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00184
00185 MSDIST_I2CRequest[0] = 2;
00186 MSDIST_I2CRequest[1] = address;
00187 MSDIST_I2CRequest[2] = MSDIST_MOD_TYPE;
00188
00189 if (!writeI2C(link, MSDIST_I2CRequest, 1))
00190 return -1;
00191
00192 if (!readI2C(link, MSDIST_I2CReply, 1))
00193 return -1;
00194
00195 return 0x00FF & MSDIST_I2CReply[0];
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 bool MSDISTsendCmd(tSensors link, byte command, ubyte address) {
00207 memset(MSDIST_I2CRequest, 0, sizeof(tByteArray));
00208
00209 MSDIST_I2CRequest[0] = 3;
00210 MSDIST_I2CRequest[1] = address;
00211 MSDIST_I2CRequest[2] = MSDIST_CMD;
00212 MSDIST_I2CRequest[3] = command;
00213
00214 return writeI2C(link, MSDIST_I2CRequest, 0);
00215 }
00216
00217
00218 #endif //__MSDIST_H__
00219
00220
00221
00222
00223
00224