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