Priyanka Pashte
/
ESD_Project_USBMouse
USBMouse
Accelerometer/Accelerometer.cpp@0:e062501cfe81, 2015-12-13 (annotated)
- Committer:
- priyankapashte
- Date:
- Sun Dec 13 10:06:24 2015 +0000
- Revision:
- 0:e062501cfe81
USBDevice library modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
priyankapashte | 0:e062501cfe81 | 1 | #include "Accelerometer.h" |
priyankapashte | 0:e062501cfe81 | 2 | |
priyankapashte | 0:e062501cfe81 | 3 | |
priyankapashte | 0:e062501cfe81 | 4 | #define REG_CTRL_REG_1 0x2A |
priyankapashte | 0:e062501cfe81 | 5 | #define REG_OUT_X_MSB 0x01 |
priyankapashte | 0:e062501cfe81 | 6 | #define REG_OUT_Y_MSB 0x03 |
priyankapashte | 0:e062501cfe81 | 7 | #define REG_OUT_Z_MSB 0x05 |
priyankapashte | 0:e062501cfe81 | 8 | |
priyankapashte | 0:e062501cfe81 | 9 | Accelerometer::Accelerometer(PinName sda, PinName scl, int address) : i2c(sda, scl), addr(address) { |
priyankapashte | 0:e062501cfe81 | 10 | // activate the peripheral |
priyankapashte | 0:e062501cfe81 | 11 | uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; |
priyankapashte | 0:e062501cfe81 | 12 | writeRegs(data, 2); |
priyankapashte | 0:e062501cfe81 | 13 | } |
priyankapashte | 0:e062501cfe81 | 14 | |
priyankapashte | 0:e062501cfe81 | 15 | Accelerometer::~Accelerometer() { } |
priyankapashte | 0:e062501cfe81 | 16 | |
priyankapashte | 0:e062501cfe81 | 17 | |
priyankapashte | 0:e062501cfe81 | 18 | float Accelerometer::Acc_X() { |
priyankapashte | 0:e062501cfe81 | 19 | //divide by 4096 b/c MMA output is 4096 counts per g so this f outputs accelorometer value formatted to g (gravity) |
priyankapashte | 0:e062501cfe81 | 20 | return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); |
priyankapashte | 0:e062501cfe81 | 21 | } |
priyankapashte | 0:e062501cfe81 | 22 | |
priyankapashte | 0:e062501cfe81 | 23 | float Accelerometer::Acc_Y() { |
priyankapashte | 0:e062501cfe81 | 24 | return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); |
priyankapashte | 0:e062501cfe81 | 25 | } |
priyankapashte | 0:e062501cfe81 | 26 | |
priyankapashte | 0:e062501cfe81 | 27 | float Accelerometer::Acc_Z() { |
priyankapashte | 0:e062501cfe81 | 28 | return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); |
priyankapashte | 0:e062501cfe81 | 29 | } |
priyankapashte | 0:e062501cfe81 | 30 | |
priyankapashte | 0:e062501cfe81 | 31 | |
priyankapashte | 0:e062501cfe81 | 32 | |
priyankapashte | 0:e062501cfe81 | 33 | int16_t Accelerometer::getAccAxis(uint8_t addr) { |
priyankapashte | 0:e062501cfe81 | 34 | int16_t acc; |
priyankapashte | 0:e062501cfe81 | 35 | uint8_t result[2]; |
priyankapashte | 0:e062501cfe81 | 36 | readRegs(addr, result, 2); |
priyankapashte | 0:e062501cfe81 | 37 | |
priyankapashte | 0:e062501cfe81 | 38 | acc = (result[0] << 6) | (result[1] >> 2); |
priyankapashte | 0:e062501cfe81 | 39 | if (acc > 16383/2) |
priyankapashte | 0:e062501cfe81 | 40 | acc -= 16383; |
priyankapashte | 0:e062501cfe81 | 41 | |
priyankapashte | 0:e062501cfe81 | 42 | return acc; |
priyankapashte | 0:e062501cfe81 | 43 | } |
priyankapashte | 0:e062501cfe81 | 44 | |
priyankapashte | 0:e062501cfe81 | 45 | void Accelerometer::readRegs(int addr, uint8_t * data, int len) { |
priyankapashte | 0:e062501cfe81 | 46 | char t[1] = {addr}; |
priyankapashte | 0:e062501cfe81 | 47 | i2c.write(addr, t, 1, true); |
priyankapashte | 0:e062501cfe81 | 48 | i2c.read(addr, (char *)data, len); |
priyankapashte | 0:e062501cfe81 | 49 | } |
priyankapashte | 0:e062501cfe81 | 50 | |
priyankapashte | 0:e062501cfe81 | 51 | |
priyankapashte | 0:e062501cfe81 | 52 | |
priyankapashte | 0:e062501cfe81 | 53 | void Accelerometer::writeRegs(uint8_t * data, int len) { |
priyankapashte | 0:e062501cfe81 | 54 | i2c.write(addr, (char *)data, len); |
priyankapashte | 0:e062501cfe81 | 55 | } |