Jonathan Tousignant
/
Projet_S5
Projet S5
accelerometer.cpp@1:feae56685ab1, 2014-03-27 (annotated)
- Committer:
- joGenie
- Date:
- Thu Mar 27 15:36:48 2014 +0000
- Revision:
- 1:feae56685ab1
- Parent:
- 0:d80295a0bcc2
Projet S5;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joGenie | 0:d80295a0bcc2 | 1 | #include "accelerometer.h" |
joGenie | 0:d80295a0bcc2 | 2 | |
joGenie | 0:d80295a0bcc2 | 3 | Accelerometer::Accelerometer() : |
joGenie | 0:d80295a0bcc2 | 4 | i2c(p9, p10) |
joGenie | 0:d80295a0bcc2 | 5 | { |
joGenie | 0:d80295a0bcc2 | 6 | initAccel(); |
joGenie | 0:d80295a0bcc2 | 7 | } |
joGenie | 0:d80295a0bcc2 | 8 | |
joGenie | 0:d80295a0bcc2 | 9 | Accelerometer::~Accelerometer(){} |
joGenie | 0:d80295a0bcc2 | 10 | |
joGenie | 0:d80295a0bcc2 | 11 | void Accelerometer::writeToRegister(int address, int startingRegister, int data2Write) |
joGenie | 0:d80295a0bcc2 | 12 | { |
joGenie | 0:d80295a0bcc2 | 13 | char data[2]; |
joGenie | 0:d80295a0bcc2 | 14 | data[0] = startingRegister; |
joGenie | 0:d80295a0bcc2 | 15 | data[1] = data2Write; |
joGenie | 0:d80295a0bcc2 | 16 | |
joGenie | 0:d80295a0bcc2 | 17 | i2c.write(address, data, 2); |
joGenie | 0:d80295a0bcc2 | 18 | } |
joGenie | 0:d80295a0bcc2 | 19 | |
joGenie | 0:d80295a0bcc2 | 20 | int Accelerometer::readRegister(int address, int startingRegister) |
joGenie | 0:d80295a0bcc2 | 21 | { |
joGenie | 0:d80295a0bcc2 | 22 | char content[1]; |
joGenie | 0:d80295a0bcc2 | 23 | char reg[1]; |
joGenie | 0:d80295a0bcc2 | 24 | reg[0] = startingRegister; |
joGenie | 0:d80295a0bcc2 | 25 | |
joGenie | 0:d80295a0bcc2 | 26 | i2c.write(address, reg, 1, true); |
joGenie | 0:d80295a0bcc2 | 27 | i2c.read(address, content, 1); |
joGenie | 0:d80295a0bcc2 | 28 | |
joGenie | 0:d80295a0bcc2 | 29 | return content[0]; |
joGenie | 0:d80295a0bcc2 | 30 | } |
joGenie | 0:d80295a0bcc2 | 31 | |
joGenie | 0:d80295a0bcc2 | 32 | char Accelerometer::i2c_read_reg(char address) //fonction qui lit les valeurs des registres |
joGenie | 0:d80295a0bcc2 | 33 | { |
joGenie | 0:d80295a0bcc2 | 34 | // Read from selected register adress |
joGenie | 0:d80295a0bcc2 | 35 | i2c.start(); |
joGenie | 0:d80295a0bcc2 | 36 | i2c.write(MMA8452_WRITE_ADDRESS); |
joGenie | 0:d80295a0bcc2 | 37 | i2c.write(address); |
joGenie | 0:d80295a0bcc2 | 38 | i2c.start(); |
joGenie | 0:d80295a0bcc2 | 39 | i2c.write(MMA8452_READ_ADDRESS); |
joGenie | 0:d80295a0bcc2 | 40 | char data = i2c.read(0); |
joGenie | 0:d80295a0bcc2 | 41 | i2c.stop(); |
joGenie | 0:d80295a0bcc2 | 42 | |
joGenie | 0:d80295a0bcc2 | 43 | // return the data readed |
joGenie | 0:d80295a0bcc2 | 44 | return data; |
joGenie | 0:d80295a0bcc2 | 45 | } |
joGenie | 0:d80295a0bcc2 | 46 | |
joGenie | 0:d80295a0bcc2 | 47 | bool Accelerometer::initAccel() //fonction qui initialise l'Accelerometer avec i2c |
joGenie | 0:d80295a0bcc2 | 48 | { |
joGenie | 0:d80295a0bcc2 | 49 | // Start I2C communication |
joGenie | 0:d80295a0bcc2 | 50 | char data = i2c_read_reg(WHO_AM_I); |
joGenie | 0:d80295a0bcc2 | 51 | if(data != 0x2A) |
joGenie | 0:d80295a0bcc2 | 52 | { |
joGenie | 0:d80295a0bcc2 | 53 | return false; |
joGenie | 0:d80295a0bcc2 | 54 | } |
joGenie | 0:d80295a0bcc2 | 55 | |
joGenie | 0:d80295a0bcc2 | 56 | // Put accelerometer in active mode at 6.25Hz |
joGenie | 0:d80295a0bcc2 | 57 | writeToRegister(MMA8452_WRITE_ADDRESS, CTRL_REG1, 0x31); |
joGenie | 0:d80295a0bcc2 | 58 | |
joGenie | 0:d80295a0bcc2 | 59 | // Put accelerometer in standby mode at 6.25Hz |
joGenie | 0:d80295a0bcc2 | 60 | writeToRegister(MMA8452_WRITE_ADDRESS, CTRL_REG1, 0x30); |
joGenie | 0:d80295a0bcc2 | 61 | |
joGenie | 0:d80295a0bcc2 | 62 | // Set accelerometer in 4g range |
joGenie | 0:d80295a0bcc2 | 63 | writeToRegister(MMA8452_WRITE_ADDRESS, XYZ_DATA_CFG, 0x1); |
joGenie | 0:d80295a0bcc2 | 64 | |
joGenie | 0:d80295a0bcc2 | 65 | // Put accelerometer in active mode at 6.25Hz |
joGenie | 0:d80295a0bcc2 | 66 | writeToRegister(MMA8452_WRITE_ADDRESS, CTRL_REG1, 0x31); |
joGenie | 0:d80295a0bcc2 | 67 | |
joGenie | 0:d80295a0bcc2 | 68 | return true; |
joGenie | 0:d80295a0bcc2 | 69 | } |
joGenie | 0:d80295a0bcc2 | 70 | |
joGenie | 0:d80295a0bcc2 | 71 | unsigned short* Accelerometer::getAccelValue() |
joGenie | 0:d80295a0bcc2 | 72 | { |
joGenie | 0:d80295a0bcc2 | 73 | unsigned short* values = new unsigned short[3]; |
joGenie | 0:d80295a0bcc2 | 74 | |
joGenie | 0:d80295a0bcc2 | 75 | values[0] = i2c_read_reg(OUT_X_MSB); |
joGenie | 0:d80295a0bcc2 | 76 | values[1] = i2c_read_reg(OUT_Y_MSB); |
joGenie | 0:d80295a0bcc2 | 77 | values[2] = i2c_read_reg(OUT_Z_MSB); |
joGenie | 0:d80295a0bcc2 | 78 | |
joGenie | 0:d80295a0bcc2 | 79 | return values; |
joGenie | 0:d80295a0bcc2 | 80 | } |
joGenie | 0:d80295a0bcc2 | 81 | |
joGenie | 0:d80295a0bcc2 | 82 | extern void *Accelerometer_C_new() |
joGenie | 0:d80295a0bcc2 | 83 | { |
joGenie | 0:d80295a0bcc2 | 84 | return new Accelerometer(); |
joGenie | 0:d80295a0bcc2 | 85 | } |
joGenie | 0:d80295a0bcc2 | 86 | |
joGenie | 0:d80295a0bcc2 | 87 | extern void Accelerometer_C_delete(void *accelerometer) |
joGenie | 0:d80295a0bcc2 | 88 | { |
joGenie | 0:d80295a0bcc2 | 89 | Accelerometer *acc = (Accelerometer*)accelerometer; |
joGenie | 0:d80295a0bcc2 | 90 | delete acc; |
joGenie | 0:d80295a0bcc2 | 91 | } |
joGenie | 0:d80295a0bcc2 | 92 | |
joGenie | 0:d80295a0bcc2 | 93 | extern unsigned short* Accelerometer_C_getAccelValue(void *accelerometer) |
joGenie | 0:d80295a0bcc2 | 94 | { |
joGenie | 0:d80295a0bcc2 | 95 | Accelerometer *acc = (Accelerometer*)accelerometer; |
joGenie | 0:d80295a0bcc2 | 96 | return acc->getAccelValue(); |
joGenie | 0:d80295a0bcc2 | 97 | } |
joGenie | 0:d80295a0bcc2 | 98 |