| 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 |
#include "Communication.h" |
| 45 |
#include "Command.h" |
| 46 |
#include "ADXL345.h" |
| 47 |
#include "Console.h" |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
const char* cmdList[] = { |
| 55 |
"help?", |
| 56 |
"communication=", |
| 57 |
"communication?", |
| 58 |
"acceleration?", |
| 59 |
"accelerationX?", |
| 60 |
"accelerationY?", |
| 61 |
"accelerationZ?", |
| 62 |
"interrupts?"}; |
| 63 |
|
| 64 |
|
| 65 |
const char* cmdDescription[] = { |
| 66 |
" - Displays all available commands.", |
| 67 |
" - Selects the communication interface. Accepted values:\r\n\ |
| 68 |
\t0 - SPI. \r\n\ |
| 69 |
\t1 - I2C.", |
| 70 |
" - Displays the selected communication interface.", |
| 71 |
" - Displays the acceleration on XYZ axis.", |
| 72 |
" - Displays the acceleration on X axis.", |
| 73 |
" - Displays the acceleration on Y axis.", |
| 74 |
" - Displays the acceleration on Z axis.", |
| 75 |
" - Displays the state of the interrupts."}; |
| 76 |
|
| 77 |
|
| 78 |
const char* cmdExample[] = { |
| 79 |
"", |
| 80 |
"To select the I2C communication port, type communication=1", |
| 81 |
"", |
| 82 |
"", |
| 83 |
"", |
| 84 |
"", |
| 85 |
"", |
| 86 |
""}; |
| 87 |
|
| 88 |
const char cmdNo = (sizeof(cmdList) / sizeof(const char*)); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
cmdFunction cmdFunctions[8] = {GetHelp, SetCommunication, |
| 95 |
GetCommunication, GetAcceleration, |
| 96 |
GetAccelerationX, GetAccelerationY, |
| 97 |
GetAccelerationZ, GetInterrupts}; |
| 98 |
|
| 99 |
float x = 0; |
| 100 |
float y = 0; |
| 101 |
float z = 0; |
| 102 |
unsigned char intSource = 0; |
| 103 |
unsigned char communicationPort = 0; |
| 104 |
unsigned char adxlInitializedOk = 0; |
| 105 |
unsigned short interruptsOccured = 0; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
void GetHelp(double* param, char paramNo) |
| 113 |
{ |
| 114 |
char displayCmd; |
| 115 |
|
| 116 |
CONSOLE_Print("Available commands:\r\n"); |
| 117 |
for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 118 |
{ |
| 119 |
CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd], |
| 120 |
(char*)cmdDescription[displayCmd]); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
char DoDeviceInit(void) |
| 134 |
{ |
| 135 |
char receivedChar = 0; |
| 136 |
char charNumber = 0; |
| 137 |
double commProtocol = 0; |
| 138 |
char command[10]; |
| 139 |
|
| 140 |
CONSOLE_Print("\nPlease select the communication interface. Accepted values:\r\n\ |
| 141 |
\t0 - SPI. \r\n\ |
| 142 |
\t1 - I2C.\r\n"); |
| 143 |
CONSOLE_Print("%s",cmdList[1]); |
| 144 |
|
| 145 |
|
| 146 |
while((receivedChar != '\n') && (receivedChar != '\r')) |
| 147 |
{ |
| 148 |
UART_ReadChar(&receivedChar); |
| 149 |
command[charNumber++] = receivedChar; |
| 150 |
} |
| 151 |
commProtocol = (double)(command[0] - 0x30); |
| 152 |
|
| 153 |
|
| 154 |
if(ADXL345_Init(commProtocol) == 0) |
| 155 |
{ |
| 156 |
SetCommunication(&commProtocol, 1); |
| 157 |
|
| 158 |
GetHelp(NULL, 0); |
| 159 |
return SUCCESS; |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
CONSOLE_Print("ADXL345 Error\r\n"); |
| 164 |
return ERROR; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
void SetCommunication(double* param, char paramNo) |
| 177 |
{ |
| 178 |
if(paramNo >= 1) |
| 179 |
{ |
| 180 |
if(param[0] == 0) |
| 181 |
{ |
| 182 |
communicationPort = ADXL345_SPI_COMM; |
| 183 |
} |
| 184 |
else if(param[0] == 1) |
| 185 |
{ |
| 186 |
communicationPort = ADXL345_I2C_COMM; |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
if(param[0] < 0) |
| 191 |
{ |
| 192 |
communicationPort = ADXL345_SPI_COMM; |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
communicationPort = ADXL345_I2C_COMM; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
CONSOLE_Print("%s",(char*)cmdList[1]); |
| 203 |
if(communicationPort == ADXL345_I2C_COMM) |
| 204 |
{ |
| 205 |
CONSOLE_Print("I2C\r\n"); |
| 206 |
} |
| 207 |
else |
| 208 |
{ |
| 209 |
CONSOLE_Print("SPI\r\n"); |
| 210 |
} |
| 211 |
|
| 212 |
if(ADXL345_Init(communicationPort) == 0) |
| 213 |
{ |
| 214 |
CONSOLE_Print("ADXL345 OK\r\n"); |
| 215 |
adxlInitializedOk = 1; |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
CONSOLE_Print("ADXL345 Error\r\n"); |
| 220 |
adxlInitializedOk = 0; |
| 221 |
} |
| 222 |
|
| 223 |
ADXL345_SetTapDetection(ADXL345_SINGLE_TAP | |
| 224 |
ADXL345_DOUBLE_TAP, |
| 225 |
ADXL345_TAP_X_EN, |
| 226 |
0x64, |
| 227 |
0x20, |
| 228 |
0x40, |
| 229 |
0x10, |
| 230 |
0x00); |
| 231 |
ADXL345_SetFreeFallDetection(0x01, |
| 232 |
0x05, |
| 233 |
0x14, |
| 234 |
0x00); |
| 235 |
ADXL345_SetPowerMode(0x1); |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
|
| 240 |
CONSOLE_Print("Invalid parameter!\r\n"); |
| 241 |
CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]); |
| 242 |
CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
void GetCommunication(double* param, char paramNo) |
| 252 |
{ |
| 253 |
if(adxlInitializedOk) |
| 254 |
{ |
| 255 |
|
| 256 |
CONSOLE_Print("%s",cmdList[1]); |
| 257 |
if(communicationPort == ADXL345_I2C_COMM) |
| 258 |
{ |
| 259 |
CONSOLE_Print("I2C\r\n"); |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
CONSOLE_Print("SPI\r\n"); |
| 264 |
} |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
CONSOLE_Print("ADXL345 was not succesfully initialized. \ |
| 269 |
Select the ADXL345 comm. port using the 'communication=' command.\r\n "); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
void GetAcceleration(double* param, char paramNo) |
| 279 |
{ |
| 280 |
unsigned short samplesNumber = 0; |
| 281 |
|
| 282 |
if(adxlInitializedOk) |
| 283 |
{ |
| 284 |
|
| 285 |
samplesNumber = 30; |
| 286 |
while(samplesNumber) |
| 287 |
{ |
| 288 |
|
| 289 |
ADXL345_GetGxyz(&x,&y,&z); |
| 290 |
|
| 291 |
CONSOLE_Print("x axis: %.3f\r\n",x); |
| 292 |
CONSOLE_Print("y axis: %.3f\r\n",y); |
| 293 |
CONSOLE_Print("z axis: %.3f\r\n",z); |
| 294 |
TIME_DelayMs(200); |
| 295 |
samplesNumber--; |
| 296 |
} |
| 297 |
} |
| 298 |
else |
| 299 |
{ |
| 300 |
CONSOLE_Print("ADXL345 was not succesfully initialized. \ |
| 301 |
Select the ADXL345 comm. port using the 'communication=' command.\r\n"); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
void GetAccelerationX(double* param, char paramNo) |
| 311 |
{ |
| 312 |
unsigned short samplesNumber = 0; |
| 313 |
|
| 314 |
if(adxlInitializedOk) |
| 315 |
{ |
| 316 |
|
| 317 |
samplesNumber = 30; |
| 318 |
while(samplesNumber) |
| 319 |
{ |
| 320 |
|
| 321 |
ADXL345_GetGxyz(&x,&y,&z); |
| 322 |
|
| 323 |
CONSOLE_Print("x axis: %.3f\r\n",x); |
| 324 |
TIME_DelayMs(200); |
| 325 |
samplesNumber--; |
| 326 |
} |
| 327 |
} |
| 328 |
else |
| 329 |
{ |
| 330 |
CONSOLE_Print("ADXL345 was not succesfully initialized. \ |
| 331 |
Select the ADXL345 comm. port using the 'communication=' command.\r\n"); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
void GetAccelerationY(double* param, char paramNo) |
| 341 |
{ |
| 342 |
unsigned short samplesNumber = 0; |
| 343 |
|
| 344 |
if(adxlInitializedOk) |
| 345 |
{ |
| 346 |
|
| 347 |
samplesNumber = 30; |
| 348 |
while(samplesNumber) |
| 349 |
{ |
| 350 |
|
| 351 |
ADXL345_GetGxyz(&x,&y,&z); |
| 352 |
|
| 353 |
CONSOLE_Print("y axis: %.3f\r\n",y); |
| 354 |
TIME_DelayMs(200); |
| 355 |
samplesNumber--; |
| 356 |
} |
| 357 |
} |
| 358 |
else |
| 359 |
{ |
| 360 |
CONSOLE_Print("ADXL345 was not succesfully initialized. \ |
| 361 |
Select the ADXL345 comm. port using the 'communication=' command.\r\n"); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
void GetAccelerationZ(double* param, char paramNo) |
| 371 |
{ |
| 372 |
unsigned short samplesNumber = 0; |
| 373 |
|
| 374 |
if(adxlInitializedOk) |
| 375 |
{ |
| 376 |
|
| 377 |
samplesNumber = 30; |
| 378 |
while(samplesNumber) |
| 379 |
{ |
| 380 |
|
| 381 |
ADXL345_GetGxyz(&x,&y,&z); |
| 382 |
|
| 383 |
CONSOLE_Print("z axis: %.3f\r\n",z); |
| 384 |
TIME_DelayMs(200); |
| 385 |
samplesNumber--; |
| 386 |
} |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
CONSOLE_Print("ADXL345 was not succesfully initialized. \ |
| 391 |
Select the ADXL345 comm. port using the 'communication=' command.\r\n"); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
void GetInterrupts(double* param, char paramNo) |
| 401 |
{ |
| 402 |
if(adxlInitializedOk) |
| 403 |
{ |
| 404 |
interruptsOccured = 0; |
| 405 |
|
| 406 |
intSource = ADXL345_GetRegisterValue(ADXL345_INT_SOURCE); |
| 407 |
|
| 408 |
if((intSource & ADXL345_SINGLE_TAP) != 0) |
| 409 |
{ |
| 410 |
CONSOLE_Print("Single Tap.\n"); |
| 411 |
interruptsOccured = 1; |
| 412 |
} |
| 413 |
if((intSource & ADXL345_DOUBLE_TAP) != 0) |
| 414 |
{ |
| 415 |
CONSOLE_Print("Double Tap.\n"); |
| 416 |
interruptsOccured = 1; |
| 417 |
} |
| 418 |
if((intSource & ADXL345_FREE_FALL) != 0) |
| 419 |
{ |
| 420 |
CONSOLE_Print("Free Fall.\n"); |
| 421 |
interruptsOccured = 1; |
| 422 |
} |
| 423 |
if(interruptsOccured == 0) |
| 424 |
{ |
| 425 |
|
| 426 |
CONSOLE_Print("None.\n"); |
| 427 |
} |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
CONSOLE_Print("ADXL345 was not succesfully initialized. \ |
| 432 |
Select the ADXL345 comm. port using the 'communication=' command.\r\n"); |
| 433 |
} |
| 434 |
|
| 435 |
} |