Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __MSAC_H__
00013 #define __MSAC_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #pragma systemFile
00037
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041
00042 #define MSAC_I2C_ADDR 0x02
00043
00044 #define MSAC_CMD 0x41
00045
00046 #define MSAC_X_TILT 0x42
00047 #define MSAC_Y_TILT 0x43
00048 #define MSAC_Z_TILT 0x44
00049
00050 #define MSAC_X_ACCEL 0x45
00051 #define MSAC_Y_ACCEL 0x47
00052 #define MSAC_Z_ACCEL 0x49
00053
00054 #define MSAC_RANGE_2_5 1
00055 #define MSAC_RANGE_3_3 2
00056 #define MSAC_RANGE_6_7 3
00057 #define MSAC_RANGE_10 4
00058
00059 bool MSACreadTilt(tSensors link, int &x_tilt, int &y_tilt, int &z_tilt);
00060 bool MSACreadAccel(tSensors link, int &x_accel, int &y_accel, int &z_accel);
00061 bool MSACsendCmd(tSensors link, byte command);
00062 bool MSACsetRange(tSensors link, int range);
00063
00064 tByteArray MSAC_I2CRequest;
00065 tByteArray MSAC_I2CReply;
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 bool MSACreadTilt(tSensors link, int &x_tilt, int &y_tilt, int &z_tilt) {
00077 memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00078
00079 MSAC_I2CRequest[0] = 2;
00080 MSAC_I2CRequest[1] = MSAC_I2C_ADDR;
00081 MSAC_I2CRequest[2] = MSAC_X_TILT;
00082
00083 if (!writeI2C(link, MSAC_I2CRequest, 3))
00084 return false;
00085
00086 if (!readI2C(link, MSAC_I2CReply, 3))
00087 return false;
00088
00089 x_tilt = MSAC_I2CReply[0] - 128;
00090 y_tilt = MSAC_I2CReply[1] - 128;
00091 z_tilt = MSAC_I2CReply[2] - 128;
00092
00093 return true;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 bool MSACreadAccel(tSensors link, int &x_accel, int &y_accel, int &z_accel) {
00106 memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00107
00108 MSAC_I2CRequest[0] = 2;
00109 MSAC_I2CRequest[1] = MSAC_I2C_ADDR;
00110 MSAC_I2CRequest[2] = MSAC_X_ACCEL;
00111
00112 if (!writeI2C(link, MSAC_I2CRequest, 6))
00113 return false;
00114
00115 if (!readI2C(link, MSAC_I2CReply, 6))
00116 return false;
00117
00118
00119 x_accel = MSAC_I2CReply[0] + (MSAC_I2CReply[1] << 8);
00120 y_accel = MSAC_I2CReply[2] + (MSAC_I2CReply[3] << 8);
00121 z_accel = MSAC_I2CReply[4] + (MSAC_I2CReply[5] << 8);
00122 return true;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132 bool MSACsendCmd(tSensors link, byte command) {
00133 memset(MSAC_I2CRequest, 0, sizeof(tByteArray));
00134
00135 MSAC_I2CRequest[0] = 3;
00136 MSAC_I2CRequest[1] = MSAC_I2C_ADDR;
00137 MSAC_I2CRequest[2] = MSAC_CMD;
00138 MSAC_I2CRequest[3] = command;
00139
00140 return writeI2C(link, MSAC_I2CRequest, 0);
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150 bool MSACsetRange(tSensors link, int range) {
00151 byte command = 0;
00152
00153 switch (range) {
00154 case 1: command = '1'; break;
00155 case 2: command = '2'; break;
00156 case 3: command = '3'; break;
00157 case 4: command = '4'; break;
00158 default: return false; break;
00159 }
00160 return MSACsendCmd(link, command);
00161 }
00162
00163 #endif //__MSAC_H__
00164
00165
00166
00167
00168
00169