1 /* 2 * Author: Mihai Tudor Panu <mihai.tudor.panu@intel.com> 3 * Copyright (c) 2014 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 #pragma once 25 26 #include <mraa/i2c.hpp> 27 28 #define READ_BUFFER_LENGTH 6 29 30 namespace upm { 31 32 /** 33 * @brief ADXL345 Accelerometer library 34 * @defgroup adxl345 libupm-adxl345 35 * @ingroup seeed i2c accelerometer 36 */ 37 38 /** 39 * @library adxl345 40 * @sensor adxl345 41 * @comname ADXL345 3-Axis Digital Accelerometer 42 * @altname Grove 3-Axis Digital Accelerometer (16g) 43 * @type accelerometer 44 * @man seeed 45 * @con i2c 46 * 47 * @brief API for the ADXL345 3-Axis Digital Accelerometer 48 * 49 * ADXL345 is a 3-axis digital accelerometer. 50 * (http://www.seeedstudio.com/wiki/images/2/2c/ADXL345_datasheet.pdf) 51 * The sensor has configurable resolutions to measure ±2g, ±4g, ±8g, or ±16g. 52 * Note: The Grove* version of the sensor is incompatible with and not detected 53 * on the I2C bus by the Intel(R) Edison using an Arduino* breakout board at 5V 54 * (3V works fine). 55 * 56 * @image html adxl345.jpeg 57 * @snippet adxl345.cxx Interesting 58 */ 59 class Adxl345 { 60 public: 61 /** 62 * Creates an ADXL345 object 63 * 64 * @param bus Number of the used I2C bus 65 */ 66 Adxl345(int bus); 67 68 /** 69 * there is no need for a ADXL345 object destructor 70 * ~Adxl345(); 71 */ 72 73 /** 74 * Returns a pointer to a float[3] that contains acceleration (g) forces 75 * 76 * @return float* to a float[3] 77 */ 78 float* getAcceleration(); 79 80 /** 81 * Returns a pointer to an int[3] that contains the raw register values 82 * for X, Y, and Z 83 * 84 * @return int* to an int[3] 85 */ 86 int16_t* getRawValues(); 87 88 /** 89 * Returns the scale the accelerometer is currently set up to: 2, 4, 8, 90 * or 16 91 * 92 * @return uint with the current scale value 93 */ 94 uint8_t getScale(); 95 96 /** 97 * Updates the acceleration values from the I2C bus 98 * 99 * @return 0 if successful 100 */ 101 mraa::Result update(); 102 private: 103 float m_accel[3]; 104 float m_offsets[3]; 105 int16_t m_rawaccel[3]; 106 uint8_t m_buffer[READ_BUFFER_LENGTH]; 107 mraa::I2c m_i2c; 108 }; 109 110 } 111