Class library for a LIS3DSH MEMS digital output motion sensor (acceleromoter).
Dependents: stm32f407 mbed_blinky STM32F407
LIS3DSH.h@5:e6a312714223, 2016-08-09 (annotated)
- Committer:
- grantphillips
- Date:
- Tue Aug 09 13:45:18 2016 +0000
- Revision:
- 5:e6a312714223
- Parent:
- 4:a7c327791566
Updated comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
grantphillips | 0:36febce4f85e | 1 | /* LIS3DSH Library v1.0 |
grantphillips | 0:36febce4f85e | 2 | * Copyright (c) 2016 Grant Phillips |
grantphillips | 0:36febce4f85e | 3 | * grant.phillips@nmmu.ac.za |
grantphillips | 0:36febce4f85e | 4 | * |
grantphillips | 0:36febce4f85e | 5 | * |
grantphillips | 0:36febce4f85e | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
grantphillips | 0:36febce4f85e | 7 | * of this software and associated documentation files (the "Software"), to deal |
grantphillips | 0:36febce4f85e | 8 | * in the Software without restriction, including without limitation the rights |
grantphillips | 0:36febce4f85e | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
grantphillips | 0:36febce4f85e | 10 | * copies of the Software, and to permit persons to whom the Software is |
grantphillips | 0:36febce4f85e | 11 | * furnished to do so, subject to the following conditions: |
grantphillips | 0:36febce4f85e | 12 | * |
grantphillips | 0:36febce4f85e | 13 | * The above copyright notice and this permission notice shall be included in |
grantphillips | 0:36febce4f85e | 14 | * all copies or substantial portions of the Software. |
grantphillips | 0:36febce4f85e | 15 | * |
grantphillips | 0:36febce4f85e | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
grantphillips | 0:36febce4f85e | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
grantphillips | 0:36febce4f85e | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
grantphillips | 0:36febce4f85e | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
grantphillips | 0:36febce4f85e | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
grantphillips | 0:36febce4f85e | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
grantphillips | 0:36febce4f85e | 22 | * THE SOFTWARE. |
grantphillips | 0:36febce4f85e | 23 | */ |
grantphillips | 0:36febce4f85e | 24 | |
grantphillips | 0:36febce4f85e | 25 | #ifndef LIS3DSH_H |
grantphillips | 0:36febce4f85e | 26 | #define LIS3DSH_H |
grantphillips | 0:36febce4f85e | 27 | |
grantphillips | 0:36febce4f85e | 28 | #include "mbed.h" |
grantphillips | 0:36febce4f85e | 29 | |
grantphillips | 0:36febce4f85e | 30 | /** Class library for a LIS3DSH MEMS digital output motion sensor (acceleromoter). |
grantphillips | 0:36febce4f85e | 31 | * |
grantphillips | 0:36febce4f85e | 32 | * Example: |
grantphillips | 0:36febce4f85e | 33 | * @code |
grantphillips | 0:36febce4f85e | 34 | * #include "mbed.h" |
grantphillips | 0:36febce4f85e | 35 | * #include "LIS3DSH.h" |
grantphillips | 0:36febce4f85e | 36 | * |
grantphillips | 0:36febce4f85e | 37 | * LIS3DSH acc(PA_7, PA_6, PA_5, PE_3); |
grantphillips | 0:36febce4f85e | 38 | * // mosi, miso, clk , cs |
grantphillips | 0:36febce4f85e | 39 | * |
grantphillips | 0:36febce4f85e | 40 | * int main() { |
grantphillips | 0:36febce4f85e | 41 | * int16_t X, Y, Z; //signed integer variables for raw X,Y,Z values |
grantphillips | 0:36febce4f85e | 42 | * float roll, pitch; //float variables for angles |
grantphillips | 0:36febce4f85e | 43 | * |
grantphillips | 0:36febce4f85e | 44 | * if(acc.Detect() != 1) { |
grantphillips | 0:36febce4f85e | 45 | * printf("LIS3DSH Acceleromoter not detected!\n"); |
grantphillips | 0:36febce4f85e | 46 | * while(1){ }; |
grantphillips | 0:36febce4f85e | 47 | * } |
grantphillips | 0:36febce4f85e | 48 | * |
grantphillips | 0:36febce4f85e | 49 | * while(1) { |
grantphillips | 0:36febce4f85e | 50 | * acc.ReadData(&X, &Y, &Z); //read X, Y, Z values |
grantphillips | 0:36febce4f85e | 51 | * acc.ReadAngles(&roll, &pitch); //read roll and pitch angles |
grantphillips | 1:80b5653015e6 | 52 | * printf("X: %d Y: %d Z: %d\n", X, Y, Z); |
grantphillips | 1:80b5653015e6 | 53 | * printf("Roll: %f Pitch: %f\n", roll, pitch); |
grantphillips | 0:36febce4f85e | 54 | * |
grantphillips | 0:36febce4f85e | 55 | * wait(1.0); //delay before reading next values |
grantphillips | 0:36febce4f85e | 56 | * } |
grantphillips | 5:e6a312714223 | 57 | * } |
grantphillips | 0:36febce4f85e | 58 | * @endcode |
grantphillips | 0:36febce4f85e | 59 | */ |
grantphillips | 0:36febce4f85e | 60 | |
grantphillips | 0:36febce4f85e | 61 | class LIS3DSH { |
grantphillips | 0:36febce4f85e | 62 | public: |
grantphillips | 0:36febce4f85e | 63 | /** Create a LIS3DSH object connected to the specified pins. |
grantphillips | 0:36febce4f85e | 64 | * @param mosi SPI compatible pin used for the LIS3DSH's MOSI pin |
grantphillips | 0:36febce4f85e | 65 | * @param miso SPI compatible pin used for the LIS3DSH's MISO pin |
grantphillips | 0:36febce4f85e | 66 | * @param clk SPI compatible pin used for the LIS3DSH's CLK pin |
grantphillips | 0:36febce4f85e | 67 | * @param cs DigitalOut compatible pin used for the LIS3DSH's CS pin |
grantphillips | 0:36febce4f85e | 68 | */ |
grantphillips | 0:36febce4f85e | 69 | LIS3DSH(PinName mosi, PinName miso, PinName clk, PinName cs); |
grantphillips | 0:36febce4f85e | 70 | |
grantphillips | 0:36febce4f85e | 71 | /** Determines if the LIS3DSH acceleromoter can be detected. |
grantphillips | 0:36febce4f85e | 72 | * @param |
grantphillips | 0:36febce4f85e | 73 | * None |
grantphillips | 0:36febce4f85e | 74 | * @return |
grantphillips | 0:36febce4f85e | 75 | * 1 = detected; 0 = not detected. |
grantphillips | 0:36febce4f85e | 76 | */ |
grantphillips | 0:36febce4f85e | 77 | int Detect(void); |
grantphillips | 0:36febce4f85e | 78 | |
grantphillips | 0:36febce4f85e | 79 | /** Write a byte of data to the LIS3DSH at a selected address. |
grantphillips | 4:a7c327791566 | 80 | * @param addr 8-bit address to where the data should be written |
grantphillips | 4:a7c327791566 | 81 | * @param data 8-bit data byte to write |
grantphillips | 0:36febce4f85e | 82 | * @return |
grantphillips | 0:36febce4f85e | 83 | * None |
grantphillips | 0:36febce4f85e | 84 | */ |
grantphillips | 0:36febce4f85e | 85 | void WriteReg(uint8_t addr, uint8_t data); |
grantphillips | 0:36febce4f85e | 86 | |
grantphillips | 0:36febce4f85e | 87 | /** Read data from the LIS3DSH from a selected address. |
grantphillips | 0:36febce4f85e | 88 | * @param |
grantphillips | 0:36febce4f85e | 89 | * addr 8-bit address from where the data should be read |
grantphillips | 0:36febce4f85e | 90 | * @return |
grantphillips | 0:36febce4f85e | 91 | * Contents of the register as a 8-bit data byte. |
grantphillips | 0:36febce4f85e | 92 | */ |
grantphillips | 0:36febce4f85e | 93 | uint8_t ReadReg(uint8_t addr); |
grantphillips | 0:36febce4f85e | 94 | |
grantphillips | 0:36febce4f85e | 95 | /** Reads the raw X, Y, Z values from the LIS3DSH as signed 16-bit values (int16_t). |
grantphillips | 3:c11d3473c026 | 96 | * @param X Reference to variable for the raw X value |
grantphillips | 3:c11d3473c026 | 97 | * @param Y Reference to variable for the raw Y value |
grantphillips | 3:c11d3473c026 | 98 | * @param Z Reference to variable for the raw Z value |
grantphillips | 0:36febce4f85e | 99 | * @return |
grantphillips | 0:36febce4f85e | 100 | * None |
grantphillips | 0:36febce4f85e | 101 | */ |
grantphillips | 0:36febce4f85e | 102 | void ReadData(int16_t *X, int16_t *Y, int16_t *Z); |
grantphillips | 0:36febce4f85e | 103 | |
grantphillips | 0:36febce4f85e | 104 | /** Reads the roll angle and pitch angle from the LIS3DSH. |
grantphillips | 3:c11d3473c026 | 105 | * @param Roll Reference to variable for the roll angle (0.0 - 359.999999) |
grantphillips | 3:c11d3473c026 | 106 | * @param Pitch Reference to variable for the pitch angle (0.0 - 359.999999) |
grantphillips | 0:36febce4f85e | 107 | * @return |
grantphillips | 0:36febce4f85e | 108 | * None |
grantphillips | 0:36febce4f85e | 109 | */ |
grantphillips | 0:36febce4f85e | 110 | void ReadAngles(float *Roll, float *Pitch); |
grantphillips | 0:36febce4f85e | 111 | |
grantphillips | 0:36febce4f85e | 112 | private: |
grantphillips | 0:36febce4f85e | 113 | SPI _spi; |
grantphillips | 0:36febce4f85e | 114 | DigitalOut _cs; |
grantphillips | 0:36febce4f85e | 115 | float gToDegrees(float V, float H); |
grantphillips | 0:36febce4f85e | 116 | }; |
grantphillips | 0:36febce4f85e | 117 | |
grantphillips | 0:36febce4f85e | 118 | #endif |