Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
Accelerometer.hpp@21:a111be2582be, 2017-01-18 (annotated)
- Committer:
- dupm2216
- Date:
- Wed Jan 18 02:38:05 2017 +0000
- Revision:
- 21:a111be2582be
- Parent:
- 19:f5aa0ce5546b
Add code header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dupm2216 | 21:a111be2582be | 1 | ///////////////////////////////////////////////////////////// |
dupm2216 | 21:a111be2582be | 2 | // APP 1: Systèmes à microprocesseurs // |
dupm2216 | 21:a111be2582be | 3 | // // |
dupm2216 | 21:a111be2582be | 4 | // Université de Sherbrooke // |
dupm2216 | 21:a111be2582be | 5 | // Génie informatique // |
dupm2216 | 21:a111be2582be | 6 | // Session 5, Hiver 2017 // |
dupm2216 | 21:a111be2582be | 7 | // // |
dupm2216 | 21:a111be2582be | 8 | // Date: 17 janvier 2017 // |
dupm2216 | 21:a111be2582be | 9 | // // |
dupm2216 | 21:a111be2582be | 10 | // Auteurs: Maxime Dupuis, dupm2216 // |
dupm2216 | 21:a111be2582be | 11 | // Bruno Allaire-Lemay, allb2701 // |
dupm2216 | 21:a111be2582be | 12 | ///////////////////////////////////////////////////////////// |
dupm2216 | 21:a111be2582be | 13 | |
dupm2216 | 3:1a9d0f0a50bf | 14 | #ifndef ACCELEROMETER_HPP |
dupm2216 | 3:1a9d0f0a50bf | 15 | #define ACCELEROMETER_HPP |
dupm2216 | 1:7becb0e903e3 | 16 | |
dupm2216 | 1:7becb0e903e3 | 17 | #include "mbed.h" |
dupm2216 | 19:f5aa0ce5546b | 18 | #include "Utility.hpp" |
dupm2216 | 1:7becb0e903e3 | 19 | |
dupm2216 | 7:1e00dfecc92d | 20 | namespace accelerometer |
dupm2216 | 1:7becb0e903e3 | 21 | { |
dupm2216 | 7:1e00dfecc92d | 22 | const int NUMBER_OF_DATA_REGISTERS = 6; |
dupm2216 | 7:1e00dfecc92d | 23 | const char OUT_X_MSB_REGISTER = 0x01; |
dupm2216 | 7:1e00dfecc92d | 24 | const char OUT_X_LSB_REGISTER = 0x02; |
dupm2216 | 7:1e00dfecc92d | 25 | const char OUT_Y_MSB_REGISTER = 0x03; |
dupm2216 | 7:1e00dfecc92d | 26 | const char OUT_Y_LSB_REGISTER = 0x04; |
dupm2216 | 7:1e00dfecc92d | 27 | const char OUT_Z_MSB_REGISTER = 0x05; |
dupm2216 | 7:1e00dfecc92d | 28 | const char OUT_Z_LSB_REGISTER = 0x06; |
dupm2216 | 7:1e00dfecc92d | 29 | |
dupm2216 | 7:1e00dfecc92d | 30 | const char WHO_AM_I_REGISTER = 0x0D; |
dupm2216 | 7:1e00dfecc92d | 31 | const char CTRL_REG1_REGISTER_ADDRESS = 0x2A; |
dupm2216 | 7:1e00dfecc92d | 32 | |
dupm2216 | 7:1e00dfecc92d | 33 | const int I2C_ACCELEROMETER_ADDRESS = 0x1D; |
dupm2216 | 7:1e00dfecc92d | 34 | |
dupm2216 | 7:1e00dfecc92d | 35 | enum Axis |
dupm2216 | 7:1e00dfecc92d | 36 | { |
dupm2216 | 7:1e00dfecc92d | 37 | AXIS_X, |
dupm2216 | 7:1e00dfecc92d | 38 | AXIS_Y, |
dupm2216 | 7:1e00dfecc92d | 39 | AXIS_Z, |
dupm2216 | 7:1e00dfecc92d | 40 | AXIS_INVALID |
dupm2216 | 7:1e00dfecc92d | 41 | }; |
dupm2216 | 7:1e00dfecc92d | 42 | |
dupm2216 | 7:1e00dfecc92d | 43 | int raw_axis_data_to_int(signed char raw_axis_data); |
dupm2216 | 7:1e00dfecc92d | 44 | char get_axis_register(Axis axis); |
dupm2216 | 7:1e00dfecc92d | 45 | double g_force_from_int_axis_data(const int axis_data); |
dupm2216 | 7:1e00dfecc92d | 46 | double angle_from_int_axis_data(const int axis_data); |
dupm2216 | 7:1e00dfecc92d | 47 | |
dupm2216 | 7:1e00dfecc92d | 48 | class Accelerometer |
dupm2216 | 7:1e00dfecc92d | 49 | { |
dupm2216 | 7:1e00dfecc92d | 50 | public: |
dupm2216 | 7:1e00dfecc92d | 51 | Accelerometer( |
dupm2216 | 7:1e00dfecc92d | 52 | PinName sda_pin, |
dupm2216 | 19:f5aa0ce5546b | 53 | PinName scl_pin, |
dupm2216 | 19:f5aa0ce5546b | 54 | const int filter_size, |
dupm2216 | 7:1e00dfecc92d | 55 | const int slave_address = I2C_ACCELEROMETER_ADDRESS |
dupm2216 | 7:1e00dfecc92d | 56 | ); |
dupm2216 | 7:1e00dfecc92d | 57 | |
dupm2216 | 7:1e00dfecc92d | 58 | void write_register(const char register_address, const char new_value); |
dupm2216 | 7:1e00dfecc92d | 59 | char read_register(const char register_address); |
dupm2216 | 7:1e00dfecc92d | 60 | |
dupm2216 | 7:1e00dfecc92d | 61 | //axis_data must be an array of length 6 |
dupm2216 | 7:1e00dfecc92d | 62 | void read_all_axis(signed char* axis_data); |
dupm2216 | 7:1e00dfecc92d | 63 | void print_all_axis_data(); |
dupm2216 | 7:1e00dfecc92d | 64 | |
dupm2216 | 7:1e00dfecc92d | 65 | void set_standby(); |
dupm2216 | 7:1e00dfecc92d | 66 | void set_active(); |
dupm2216 | 7:1e00dfecc92d | 67 | void init(); |
dupm2216 | 7:1e00dfecc92d | 68 | |
dupm2216 | 7:1e00dfecc92d | 69 | //Read data from the 8 most significant bits (MSB) of a given axis |
dupm2216 | 7:1e00dfecc92d | 70 | int read_axis_data_8_bits(Axis axis); |
dupm2216 | 7:1e00dfecc92d | 71 | |
dupm2216 | 7:1e00dfecc92d | 72 | double get_angle_from_horizontal(); |
dupm2216 | 1:7becb0e903e3 | 73 | |
dupm2216 | 7:1e00dfecc92d | 74 | private: |
dupm2216 | 7:1e00dfecc92d | 75 | I2C device; |
dupm2216 | 19:f5aa0ce5546b | 76 | utility::MovingAverageFilter filter; |
dupm2216 | 7:1e00dfecc92d | 77 | const int slave_address; |
dupm2216 | 19:f5aa0ce5546b | 78 | |
dupm2216 | 7:1e00dfecc92d | 79 | }; |
dupm2216 | 9:12519f9dd3cd | 80 | } |
dupm2216 | 3:1a9d0f0a50bf | 81 | #endif |