brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 5 | /*----------------------------------------------------------------------------*/ |
| 6 | |
| 7 | #ifndef __ADXL345_SPI_h__ |
| 8 | #define __ADXL345_SPI_h__ |
| 9 | |
| 10 | #include "SensorBase.h" |
| 11 | |
| 12 | class DigitalInput; |
| 13 | class DigitalOutput; |
| 14 | class SPI; |
| 15 | |
| 16 | /** |
| 17 | * ADXL345 Accelerometer on SPI. |
| 18 | * |
| 19 | * This class alows access to an Analog Devices ADXL345 3-axis accelerometer via SPI. |
| 20 | * This class assumes the sensor is wired in 4-wire SPI mode. |
| 21 | */ |
| 22 | class ADXL345_SPI : public SensorBase |
| 23 | { |
| 24 | protected: |
Brian Silverman | 6874947 | 2014-01-05 14:50:00 -0800 | [diff] [blame^] | 25 | static const uint8_t kPowerCtlRegister = 0x2D; |
| 26 | static const uint8_t kDataFormatRegister = 0x31; |
| 27 | static const uint8_t kDataRegister = 0x32; |
| 28 | static constexpr double kGsPerLSB = 0.00390625; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 29 | enum SPIAddressFields {kAddress_Read=0x80, kAddress_MultiByte=0x40}; |
| 30 | enum PowerCtlFields {kPowerCtl_Link=0x20, kPowerCtl_AutoSleep=0x10, kPowerCtl_Measure=0x08, kPowerCtl_Sleep=0x04}; |
| 31 | enum DataFormatFields {kDataFormat_SelfTest=0x80, kDataFormat_SPI=0x40, kDataFormat_IntInvert=0x20, |
| 32 | kDataFormat_FullRes=0x08, kDataFormat_Justify=0x04}; |
| 33 | |
| 34 | public: |
| 35 | enum DataFormat_Range {kRange_2G=0x00, kRange_4G=0x01, kRange_8G=0x02, kRange_16G=0x03}; |
| 36 | enum Axes {kAxis_X=0x00, kAxis_Y=0x02, kAxis_Z=0x04}; |
| 37 | struct AllAxes |
| 38 | { |
| 39 | double XAxis; |
| 40 | double YAxis; |
| 41 | double ZAxis; |
| 42 | }; |
| 43 | |
| 44 | public: |
| 45 | ADXL345_SPI(DigitalOutput &clk, DigitalOutput &mosi, DigitalInput &miso, |
| 46 | DigitalOutput &cs, DataFormat_Range range=kRange_2G); |
| 47 | ADXL345_SPI(DigitalOutput *clk, DigitalOutput *mosi, DigitalInput *miso, |
| 48 | DigitalOutput *cs, DataFormat_Range range=kRange_2G); |
Brian Silverman | 6874947 | 2014-01-05 14:50:00 -0800 | [diff] [blame^] | 49 | ADXL345_SPI(uint8_t moduleNumber, uint32_t clk, uint32_t mosi, uint32_t miso, uint32_t cs, |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 50 | DataFormat_Range range=kRange_2G); |
| 51 | virtual ~ADXL345_SPI(); |
| 52 | virtual double GetAcceleration(Axes axis); |
| 53 | virtual AllAxes GetAccelerations(); |
| 54 | |
| 55 | protected: |
| 56 | void Init(DigitalOutput *clk, DigitalOutput *mosi, DigitalInput *miso, |
| 57 | DigitalOutput *cs, DataFormat_Range range); |
| 58 | |
| 59 | DigitalOutput *m_clk; |
| 60 | DigitalOutput *m_mosi; |
| 61 | DigitalInput *m_miso; |
| 62 | DigitalOutput *m_cs; |
| 63 | SPI* m_spi; |
| 64 | }; |
| 65 | |
| 66 | #endif |
| 67 | |