Darley Gonzalez
/
I2C_Slave
Programa
MMA8451Q.h@0:782b008e3b3a, 2021-03-02 (annotated)
- Committer:
- darley_gonzalez
- Date:
- Tue Mar 02 22:37:23 2021 +0000
- Revision:
- 0:782b008e3b3a
I2c Slave
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darley_gonzalez | 0:782b008e3b3a | 1 | #ifndef MMA8451Q_H |
darley_gonzalez | 0:782b008e3b3a | 2 | #define MMA8451Q_H |
darley_gonzalez | 0:782b008e3b3a | 3 | |
darley_gonzalez | 0:782b008e3b3a | 4 | #include "mbed.h" |
darley_gonzalez | 0:782b008e3b3a | 5 | |
darley_gonzalez | 0:782b008e3b3a | 6 | /** |
darley_gonzalez | 0:782b008e3b3a | 7 | * MMA8451Q accelerometer example |
darley_gonzalez | 0:782b008e3b3a | 8 | * |
darley_gonzalez | 0:782b008e3b3a | 9 | * @code |
darley_gonzalez | 0:782b008e3b3a | 10 | * #include "mbed.h" |
darley_gonzalez | 0:782b008e3b3a | 11 | * #include "MMA8451Q.h" |
darley_gonzalez | 0:782b008e3b3a | 12 | * |
darley_gonzalez | 0:782b008e3b3a | 13 | * #define MMA8451_I2C_ADDRESS (0x1d<<1) |
darley_gonzalez | 0:782b008e3b3a | 14 | * |
darley_gonzalez | 0:782b008e3b3a | 15 | * int main(void) { |
darley_gonzalez | 0:782b008e3b3a | 16 | * |
darley_gonzalez | 0:782b008e3b3a | 17 | * MMA8451Q acc(P_E25, P_E24, MMA8451_I2C_ADDRESS); |
darley_gonzalez | 0:782b008e3b3a | 18 | * PwmOut rled(LED_RED); |
darley_gonzalez | 0:782b008e3b3a | 19 | * PwmOut gled(LED_GREEN); |
darley_gonzalez | 0:782b008e3b3a | 20 | * PwmOut bled(LED_BLUE); |
darley_gonzalez | 0:782b008e3b3a | 21 | * |
darley_gonzalez | 0:782b008e3b3a | 22 | * while (true) { |
darley_gonzalez | 0:782b008e3b3a | 23 | * rled = 1.0 - abs(acc.getAccX()); |
darley_gonzalez | 0:782b008e3b3a | 24 | * gled = 1.0 - abs(acc.getAccY()); |
darley_gonzalez | 0:782b008e3b3a | 25 | * bled = 1.0 - abs(acc.getAccZ()); |
darley_gonzalez | 0:782b008e3b3a | 26 | * wait(0.1); |
darley_gonzalez | 0:782b008e3b3a | 27 | * } |
darley_gonzalez | 0:782b008e3b3a | 28 | * } |
darley_gonzalez | 0:782b008e3b3a | 29 | * @endcode |
darley_gonzalez | 0:782b008e3b3a | 30 | */ |
darley_gonzalez | 0:782b008e3b3a | 31 | class MMA8451Q |
darley_gonzalez | 0:782b008e3b3a | 32 | { |
darley_gonzalez | 0:782b008e3b3a | 33 | public: |
darley_gonzalez | 0:782b008e3b3a | 34 | /** |
darley_gonzalez | 0:782b008e3b3a | 35 | * MMA8451Q constructor |
darley_gonzalez | 0:782b008e3b3a | 36 | * |
darley_gonzalez | 0:782b008e3b3a | 37 | * @param sda SDA pin |
darley_gonzalez | 0:782b008e3b3a | 38 | * @param sdl SCL pin |
darley_gonzalez | 0:782b008e3b3a | 39 | * @param addr addr of the I2C peripheral |
darley_gonzalez | 0:782b008e3b3a | 40 | */ |
darley_gonzalez | 0:782b008e3b3a | 41 | MMA8451Q(PinName sda, PinName scl, int addr); |
darley_gonzalez | 0:782b008e3b3a | 42 | |
darley_gonzalez | 0:782b008e3b3a | 43 | /** |
darley_gonzalez | 0:782b008e3b3a | 44 | * MMA8451Q destructor |
darley_gonzalez | 0:782b008e3b3a | 45 | */ |
darley_gonzalez | 0:782b008e3b3a | 46 | ~MMA8451Q(); |
darley_gonzalez | 0:782b008e3b3a | 47 | |
darley_gonzalez | 0:782b008e3b3a | 48 | /** |
darley_gonzalez | 0:782b008e3b3a | 49 | * Get the value of the WHO_AM_I register |
darley_gonzalez | 0:782b008e3b3a | 50 | * |
darley_gonzalez | 0:782b008e3b3a | 51 | * @returns WHO_AM_I value |
darley_gonzalez | 0:782b008e3b3a | 52 | */ |
darley_gonzalez | 0:782b008e3b3a | 53 | uint8_t getWhoAmI(); |
darley_gonzalez | 0:782b008e3b3a | 54 | |
darley_gonzalez | 0:782b008e3b3a | 55 | /** |
darley_gonzalez | 0:782b008e3b3a | 56 | * Get X axis acceleration |
darley_gonzalez | 0:782b008e3b3a | 57 | * |
darley_gonzalez | 0:782b008e3b3a | 58 | * @returns X axis acceleration |
darley_gonzalez | 0:782b008e3b3a | 59 | */ |
darley_gonzalez | 0:782b008e3b3a | 60 | float getAccX(); |
darley_gonzalez | 0:782b008e3b3a | 61 | |
darley_gonzalez | 0:782b008e3b3a | 62 | /** |
darley_gonzalez | 0:782b008e3b3a | 63 | * Get Y axis acceleration |
darley_gonzalez | 0:782b008e3b3a | 64 | * |
darley_gonzalez | 0:782b008e3b3a | 65 | * @returns Y axis acceleration |
darley_gonzalez | 0:782b008e3b3a | 66 | */ |
darley_gonzalez | 0:782b008e3b3a | 67 | float getAccY(); |
darley_gonzalez | 0:782b008e3b3a | 68 | |
darley_gonzalez | 0:782b008e3b3a | 69 | /** |
darley_gonzalez | 0:782b008e3b3a | 70 | * Get Z axis acceleration |
darley_gonzalez | 0:782b008e3b3a | 71 | * |
darley_gonzalez | 0:782b008e3b3a | 72 | * @returns Z axis acceleration |
darley_gonzalez | 0:782b008e3b3a | 73 | */ |
darley_gonzalez | 0:782b008e3b3a | 74 | float getAccZ(); |
darley_gonzalez | 0:782b008e3b3a | 75 | |
darley_gonzalez | 0:782b008e3b3a | 76 | /** |
darley_gonzalez | 0:782b008e3b3a | 77 | * Get XYZ axis acceleration |
darley_gonzalez | 0:782b008e3b3a | 78 | * |
darley_gonzalez | 0:782b008e3b3a | 79 | * @param res array where acceleration data will be stored |
darley_gonzalez | 0:782b008e3b3a | 80 | */ |
darley_gonzalez | 0:782b008e3b3a | 81 | void getAccAllAxis(float * res); |
darley_gonzalez | 0:782b008e3b3a | 82 | |
darley_gonzalez | 0:782b008e3b3a | 83 | private: |
darley_gonzalez | 0:782b008e3b3a | 84 | I2C m_i2c; |
darley_gonzalez | 0:782b008e3b3a | 85 | int m_addr; |
darley_gonzalez | 0:782b008e3b3a | 86 | void readRegs(int addr, uint8_t * data, int len); |
darley_gonzalez | 0:782b008e3b3a | 87 | void writeRegs(uint8_t * data, int len); |
darley_gonzalez | 0:782b008e3b3a | 88 | int16_t getAccAxis(uint8_t addr); |
darley_gonzalez | 0:782b008e3b3a | 89 | |
darley_gonzalez | 0:782b008e3b3a | 90 | }; |
darley_gonzalez | 0:782b008e3b3a | 91 | |
darley_gonzalez | 0:782b008e3b3a | 92 | #endif |