├── .gitignore ├── library.properties ├── .github ├── workflows │ └── githubci.yml ├── PULL_REQUEST_TEMPLATE.md └── ISSUE_TEMPLATE.md ├── LICENSE ├── README.md ├── examples └── sensorapi │ └── sensorapi.ino ├── Adafruit_FXAS21002C.h └── Adafruit_FXAS21002C.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | *.smod 19 | 20 | # Compiled Static libraries 21 | *.lai 22 | *.la 23 | *.a 24 | *.lib 25 | 26 | # Executables 27 | *.exe 28 | *.out 29 | *.app 30 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit FXAS21002C 2 | version=2.2.3 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Unified sensor driver for the FXAS210002C Gyroscope 6 | paragraph=Unified sensor driver for the FXAS21002C Gyroscope 7 | category=Sensors 8 | url=https://github.com/adafruit/Adafruit_FXAS21002C 9 | architectures=* 10 | depends=Adafruit Unified Sensor, Adafruit BusIO 11 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py main_platforms 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit FXAS21002C Gyroscope Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Adafruit Industries 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Adafruit_FXAS21002C [![Build Status](https://github.com/adafruit/Adafruit_FXAS21002C/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_FXAS21002C/actions) 2 | ================ 3 | 4 | Driver for the [Adafruit FXAS21001C Gyroscope Breakout](https://www.adafruit.com/product/3463) 5 | 6 | ## Sensor Characteristics 7 | 8 | - 2-3.6V Supply 9 | - ±250/500/1000/2000°/s configurable range 10 | - Output Data Rates (ODR) from 12.5 to 800 Hz 11 | - 16-bit digital output resolution 12 | - 192 bytes FIFO buffer (32 X/Y/Z samples) 13 | 14 | ## Documentation/Links 15 | 16 | The Doxygen documentation is automatically generated from the source files 17 | in this repository, and documents the API exposed by this driver. For 18 | practical details on how to connect and use this sensor, consult the Learning 19 | Guide. 20 | 21 | - [Adafruit Learning Guide](https://learn.adafruit.com/nxp-precision-9dof-breakout/overview) 22 | (datasheet, schematics, installation details, etc.) 23 | - [API Documentation](https://adafruit.github.io/Adafruit_FXAS21002C/) (automatically generated via doxygen from source) 24 | - [Adafruit Precision NXP 9-DoF Breakout Board](https://www.adafruit.com/product/3463) 25 | 26 | ## License 27 | 28 | This open source code is licensed under the MIT license (see [LICENSE](LICENSE) 29 | for details). 30 | 31 | Adafruit invests time and resources providing this open source code, please 32 | support Adafruit and open-source hardware by purchasing products from 33 | [Adafruit](https://www.adafruit.com)! 34 | -------------------------------------------------------------------------------- /examples/sensorapi/sensorapi.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* Assign a unique ID to this sensor at the same time */ 6 | Adafruit_FXAS21002C gyro = Adafruit_FXAS21002C(0x0021002C); 7 | 8 | void displaySensorDetails(void) { 9 | sensor_t sensor; 10 | gyro.getSensor(&sensor); 11 | Serial.println("------------------------------------"); 12 | Serial.print("Sensor: "); 13 | Serial.println(sensor.name); 14 | Serial.print("Driver Ver: "); 15 | Serial.println(sensor.version); 16 | Serial.print("Unique ID: 0x"); 17 | Serial.println(sensor.sensor_id, HEX); 18 | Serial.print("Max Value: "); 19 | Serial.print(sensor.max_value); 20 | Serial.println(" rad/s"); 21 | Serial.print("Min Value: "); 22 | Serial.print(sensor.min_value); 23 | Serial.println(" rad/s"); 24 | Serial.print("Resolution: "); 25 | Serial.print(sensor.resolution); 26 | Serial.println(" rad/s"); 27 | Serial.println("------------------------------------"); 28 | Serial.println(""); 29 | delay(500); 30 | } 31 | 32 | void setup(void) { 33 | Serial.begin(9600); 34 | 35 | /* Wait for the Serial Monitor */ 36 | while (!Serial) { 37 | delay(1); 38 | } 39 | 40 | Serial.println("Gyroscope Test"); 41 | Serial.println(""); 42 | 43 | /* Initialise the sensor */ 44 | if (!gyro.begin()) { 45 | /* There was a problem detecting the FXAS21002C ... check your connections 46 | */ 47 | Serial.println("Ooops, no FXAS21002C detected ... Check your wiring!"); 48 | while (1) 49 | ; 50 | } 51 | 52 | /* Set gyro range. (optional, default is 250 dps) */ 53 | // gyro.setRange(GYRO_RANGE_2000DPS); 54 | 55 | /* Display some basic information on this sensor */ 56 | displaySensorDetails(); 57 | } 58 | 59 | void loop(void) { 60 | /* Get a new sensor event */ 61 | sensors_event_t event; 62 | gyro.getEvent(&event); 63 | 64 | /* Display the results (speed is measured in rad/s) */ 65 | Serial.print("X: "); 66 | Serial.print(event.gyro.x); 67 | Serial.print(" "); 68 | Serial.print("Y: "); 69 | Serial.print(event.gyro.y); 70 | Serial.print(" "); 71 | Serial.print("Z: "); 72 | Serial.print(event.gyro.z); 73 | Serial.print(" "); 74 | Serial.println("rad/s "); 75 | delay(500); 76 | } 77 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /Adafruit_FXAS21002C.h: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_FXAS21002C.h 3 | * 4 | * This is part of Adafruit's FXAS21002C driver for the Arduino platform. It 5 | * is designed specifically to work with the Adafruit FXAS21002C breakout: 6 | * https://www.adafruit.com/products/3463 7 | * 8 | * These sensors use I2C to communicate, 2 pins (SCL+SDA) are required 9 | * to interface with the breakout. 10 | * 11 | * Adafruit invests time and resources providing this open source code, 12 | * please support Adafruit and open-source hardware by purchasing 13 | * products from Adafruit! 14 | * 15 | * Written by Kevin "KTOWN" Townsend for Adafruit Industries. 16 | * 17 | * MIT license, all text here must be included in any redistribution. 18 | * 19 | */ 20 | #ifndef __FXAS21002C_H__ 21 | #define __FXAS21002C_H__ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | /*========================================================================= 30 | I2C ADDRESS/BITS AND SETTINGS 31 | -----------------------------------------------------------------------*/ 32 | /** 7-bit address for this sensor */ 33 | // #define FXAS21002C_ADDRESS (0x21) // 0100001 34 | /** Device ID for this sensor (used as a sanity check during init) */ 35 | #define FXAS21002C_ID (0xD7) // 1101 0111 36 | /** Gyroscope sensitivity at 250dps */ 37 | #define GYRO_SENSITIVITY_250DPS (0.0078125F) // Table 35 of datasheet 38 | /** Gyroscope sensitivity at 500dps */ 39 | #define GYRO_SENSITIVITY_500DPS (0.015625F) 40 | /** Gyroscope sensitivity at 1000dps */ 41 | #define GYRO_SENSITIVITY_1000DPS (0.03125F) 42 | /** Gyroscope sensitivity at 2000dps */ 43 | #define GYRO_SENSITIVITY_2000DPS (0.0625F) 44 | /*=========================================================================*/ 45 | 46 | /*! 47 | Define valid gyroscope output data rate values(ODR) 48 | */ 49 | #define GYRO_ODR_800HZ (800.0f) /**< 800Hz */ 50 | #define GYRO_ODR_400HZ (400.0f) /**< 400Hz */ 51 | #define GYRO_ODR_200HZ (200.0f) /**< 200Hz */ 52 | #define GYRO_ODR_100HZ (100.0f) /**< 100Hz */ 53 | #define GYRO_ODR_50HZ (50.0f) /**< 50Hz */ 54 | #define GYRO_ODR_25HZ (25.0f) /**< 25Hz */ 55 | #define GYRO_ODR_12_5HZ (12.5f) /**< 12.5Hz*/ 56 | 57 | /*========================================================================= 58 | REGISTERS 59 | -----------------------------------------------------------------------*/ 60 | /*! 61 | Raw register addresses used to communicate with the sensor. 62 | */ 63 | typedef enum { 64 | GYRO_REGISTER_STATUS = 0x00, /**< 0x00 */ 65 | GYRO_REGISTER_OUT_X_MSB = 0x01, /**< 0x01 */ 66 | GYRO_REGISTER_OUT_X_LSB = 0x02, /**< 0x02 */ 67 | GYRO_REGISTER_OUT_Y_MSB = 0x03, /**< 0x03 */ 68 | GYRO_REGISTER_OUT_Y_LSB = 0x04, /**< 0x04 */ 69 | GYRO_REGISTER_OUT_Z_MSB = 0x05, /**< 0x05 */ 70 | GYRO_REGISTER_OUT_Z_LSB = 0x06, /**< 0x06 */ 71 | GYRO_REGISTER_WHO_AM_I = 72 | 0x0C, /**< 0x0C (default value = 0b11010111, read only) */ 73 | GYRO_REGISTER_CTRL_REG0 = 74 | 0x0D, /**< 0x0D (default value = 0b00000000, read/write) */ 75 | GYRO_REGISTER_CTRL_REG1 = 76 | 0x13, /**< 0x13 (default value = 0b00000000, read/write) */ 77 | GYRO_REGISTER_CTRL_REG2 = 78 | 0x14, /**< 0x14 (default value = 0b00000000, read/write) */ 79 | } gyroRegisters_t; 80 | /*=========================================================================*/ 81 | 82 | /*========================================================================= 83 | OPTIONAL SPEED SETTINGS 84 | -----------------------------------------------------------------------*/ 85 | /*! 86 | Enum to define valid gyroscope range values 87 | */ 88 | typedef enum { 89 | GYRO_RANGE_250DPS = 250, /**< 250dps */ 90 | GYRO_RANGE_500DPS = 500, /**< 500dps */ 91 | GYRO_RANGE_1000DPS = 1000, /**< 1000dps */ 92 | GYRO_RANGE_2000DPS = 2000 /**< 2000dps */ 93 | } gyroRange_t; 94 | /*=========================================================================*/ 95 | 96 | /*========================================================================= 97 | RAW GYROSCOPE DATA TYPE 98 | -----------------------------------------------------------------------*/ 99 | /*! 100 | Struct to store a single raw (integer-based) gyroscope vector 101 | */ 102 | typedef struct gyroRawData_s { 103 | int16_t x; /**< Raw int16_t value for the x axis */ 104 | int16_t y; /**< Raw int16_t value for the y axis */ 105 | int16_t z; /**< Raw int16_t value for the z axis */ 106 | } gyroRawData_t; 107 | /*=========================================================================*/ 108 | 109 | /**************************************************************************/ 110 | /*! 111 | @brief Unified sensor driver for the Adafruit FXAS21002C breakout. 112 | */ 113 | /**************************************************************************/ 114 | class Adafruit_FXAS21002C : public Adafruit_Sensor { 115 | public: 116 | Adafruit_FXAS21002C(int32_t sensorID = -1); 117 | ~Adafruit_FXAS21002C(); 118 | bool begin(uint8_t addr = 0x21, TwoWire *wire = &Wire); 119 | bool getEvent(sensors_event_t *event); 120 | void getSensor(sensor_t *sensor); 121 | void standby(boolean standby); 122 | 123 | void setRange(gyroRange_t range); 124 | void setODR(float ODR); 125 | gyroRange_t getRange(); 126 | float getODR(); 127 | gyroRawData_t raw; ///< Raw gyroscope values from last sensor read 128 | 129 | protected: 130 | Adafruit_I2CDevice *i2c_dev = NULL; ///< Pointer to I2C bus interface 131 | 132 | private: 133 | bool initialize(); 134 | gyroRange_t _range; 135 | float _ODR; 136 | int32_t _sensorID; 137 | }; 138 | 139 | #endif 140 | -------------------------------------------------------------------------------- /Adafruit_FXAS21002C.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_FXAS21002C.cpp 3 | * 4 | * @mainpage Adafruit FXAS21002C gyroscope sensor driver 5 | * 6 | * @section intro_sec Introduction 7 | * 8 | * This is the documentation for Adafruit's FXAS21002C driver for the 9 | * Arduino platform. It is designed specifically to work with the 10 | * Adafruit FXAS21002C breakout: https://www.adafruit.com/products/3463 11 | * 12 | * These sensors use I2C to communicate, 2 pins (SCL+SDA) are required 13 | * to interface with the breakout. 14 | * 15 | * Adafruit invests time and resources providing this open source code, 16 | * please support Adafruit and open-source hardware by purchasing 17 | * products from Adafruit! 18 | * 19 | * @section dependencies Dependencies 20 | * 21 | * This library depends on Adafruit_Sensor being 23 | * present on your system. Please make sure you have installed the latest 24 | * version before using this library. 25 | * 26 | * @section author Author 27 | * 28 | * Written by Kevin "KTOWN" Townsend for Adafruit Industries. 29 | * 30 | * @section license License 31 | * 32 | * MIT license, all text here must be included in any redistribution. 33 | * 34 | */ 35 | #include "Adafruit_FXAS21002C.h" 36 | #include 37 | 38 | /*************************************************************************** 39 | PRIVATE FUNCTIONS 40 | ***************************************************************************/ 41 | 42 | /**************************************************************************/ 43 | /*! 44 | @brief Initializes the hardware to a default state. 45 | 46 | @return True if the device was successfully initialized, otherwise false. 47 | */ 48 | /**************************************************************************/ 49 | bool Adafruit_FXAS21002C::initialize() { 50 | Adafruit_BusIO_Register CTRL_REG0(i2c_dev, GYRO_REGISTER_CTRL_REG0); 51 | Adafruit_BusIO_Register CTRL_REG1(i2c_dev, GYRO_REGISTER_CTRL_REG1); 52 | 53 | /* Set the range the an appropriate value */ 54 | _range = GYRO_RANGE_250DPS; 55 | 56 | /* Clear the raw sensor data */ 57 | raw.x = 0; 58 | raw.y = 0; 59 | raw.z = 0; 60 | 61 | /* Reset then switch to active mode with 100Hz output */ 62 | CTRL_REG1.write(0x00); // Standby 63 | CTRL_REG1.write(1 << 6); // Reset 64 | CTRL_REG0.write(0x03); // Set full scale range to +-250 dps 65 | _ODR = GYRO_ODR_100HZ; // Update global ODR variable 66 | CTRL_REG1.write(0x0E); // Active 67 | delay(100); // 60ms + 1/ODR 68 | 69 | return true; 70 | } 71 | 72 | /*************************************************************************** 73 | CONSTRUCTOR 74 | ***************************************************************************/ 75 | 76 | /**************************************************************************/ 77 | /*! 78 | @brief Instantiates a new Adafruit_FXAS21002C class, including assigning 79 | a unique ID to the gyroscope for logging purposes. 80 | 81 | @param sensorID The unique ID to associate with the gyroscope. 82 | */ 83 | /**************************************************************************/ 84 | Adafruit_FXAS21002C::Adafruit_FXAS21002C(int32_t sensorID) { 85 | _sensorID = sensorID; 86 | } 87 | 88 | /*************************************************************************** 89 | DESTRUCTOR 90 | ***************************************************************************/ 91 | 92 | Adafruit_FXAS21002C::~Adafruit_FXAS21002C() { 93 | if (i2c_dev) 94 | delete i2c_dev; 95 | } 96 | 97 | /*************************************************************************** 98 | PUBLIC FUNCTIONS 99 | ***************************************************************************/ 100 | 101 | /**************************************************************************/ 102 | /*! 103 | @brief Setup the HW 104 | 105 | @param addr The I2C address of the sensor. 106 | @param wire Pointer to Wire instance 107 | 108 | @return True if the device was successfully initialized, otherwise false. 109 | */ 110 | /**************************************************************************/ 111 | bool Adafruit_FXAS21002C::begin(uint8_t addr, TwoWire *wire) { 112 | 113 | if (i2c_dev) 114 | delete i2c_dev; 115 | i2c_dev = new Adafruit_I2CDevice(addr, wire); 116 | if (!i2c_dev->begin()) 117 | return false; 118 | 119 | Adafruit_BusIO_Register WHO_AM_I(i2c_dev, GYRO_REGISTER_WHO_AM_I); 120 | if (WHO_AM_I.read() != FXAS21002C_ID) 121 | return false; 122 | 123 | return initialize(); 124 | } 125 | 126 | /**************************************************************************/ 127 | /*! 128 | @brief Gets the most recent sensor event 129 | 130 | @param[out] event 131 | A reference to the sensors_event_t instances where the 132 | accelerometer data should be written. 133 | 134 | @return True if the event was successfully read, otherwise false. 135 | */ 136 | /**************************************************************************/ 137 | bool Adafruit_FXAS21002C::getEvent(sensors_event_t *event) { 138 | // bool readingValid = false; 139 | 140 | /* Clear the event */ 141 | memset(event, 0, sizeof(sensors_event_t)); 142 | 143 | /* Clear the raw data placeholder */ 144 | raw.x = 0; 145 | raw.y = 0; 146 | raw.z = 0; 147 | 148 | event->version = sizeof(sensors_event_t); 149 | event->sensor_id = _sensorID; 150 | event->type = SENSOR_TYPE_GYROSCOPE; 151 | event->timestamp = millis(); 152 | 153 | /* Read 7 bytes from the sensor */ 154 | uint8_t buffer[7] = {0}; 155 | buffer[0] = GYRO_REGISTER_STATUS; 156 | i2c_dev->write_then_read(buffer, 1, buffer, 7); 157 | 158 | /* Shift values to create properly formed integer */ 159 | event->gyro.x = (int16_t)((buffer[1] << 8) | buffer[2]); 160 | event->gyro.y = (int16_t)((buffer[3] << 8) | buffer[4]); 161 | event->gyro.z = (int16_t)((buffer[5] << 8) | buffer[6]); 162 | 163 | /* Assign raw values in case someone needs them */ 164 | raw.x = event->gyro.x; 165 | raw.y = event->gyro.y; 166 | raw.z = event->gyro.z; 167 | 168 | /* Compensate values depending on the resolution */ 169 | switch (_range) { 170 | case GYRO_RANGE_250DPS: 171 | event->gyro.x *= GYRO_SENSITIVITY_250DPS; 172 | event->gyro.y *= GYRO_SENSITIVITY_250DPS; 173 | event->gyro.z *= GYRO_SENSITIVITY_250DPS; 174 | break; 175 | case GYRO_RANGE_500DPS: 176 | event->gyro.x *= GYRO_SENSITIVITY_500DPS; 177 | event->gyro.y *= GYRO_SENSITIVITY_500DPS; 178 | event->gyro.z *= GYRO_SENSITIVITY_500DPS; 179 | break; 180 | case GYRO_RANGE_1000DPS: 181 | event->gyro.x *= GYRO_SENSITIVITY_1000DPS; 182 | event->gyro.y *= GYRO_SENSITIVITY_1000DPS; 183 | event->gyro.z *= GYRO_SENSITIVITY_1000DPS; 184 | break; 185 | case GYRO_RANGE_2000DPS: 186 | event->gyro.x *= GYRO_SENSITIVITY_2000DPS; 187 | event->gyro.y *= GYRO_SENSITIVITY_2000DPS; 188 | event->gyro.z *= GYRO_SENSITIVITY_2000DPS; 189 | break; 190 | } 191 | 192 | /* Convert values to rad/s */ 193 | event->gyro.x *= SENSORS_DPS_TO_RADS; 194 | event->gyro.y *= SENSORS_DPS_TO_RADS; 195 | event->gyro.z *= SENSORS_DPS_TO_RADS; 196 | 197 | return true; 198 | } 199 | 200 | /**************************************************************************/ 201 | /*! 202 | @brief Gets the sensor_t data 203 | 204 | @param[out] sensor 205 | A reference to the sensor_t instances where the 206 | gyroscope sensor info should be written. 207 | */ 208 | /**************************************************************************/ 209 | void Adafruit_FXAS21002C::getSensor(sensor_t *sensor) { 210 | /* Clear the sensor_t object */ 211 | memset(sensor, 0, sizeof(sensor_t)); 212 | 213 | /* Insert the sensor name in the fixed length char array */ 214 | strncpy(sensor->name, "FXAS21002C", sizeof(sensor->name) - 1); 215 | sensor->name[sizeof(sensor->name) - 1] = 0; 216 | sensor->version = 1; 217 | sensor->sensor_id = _sensorID; 218 | sensor->type = SENSOR_TYPE_GYROSCOPE; 219 | sensor->min_delay = 0; 220 | sensor->max_value = (float)this->_range * SENSORS_DPS_TO_RADS; 221 | sensor->min_value = ((float)this->_range * -1.0) * SENSORS_DPS_TO_RADS; 222 | sensor->resolution = 0.0F; // TBD 223 | } 224 | 225 | /**************************************************************************/ 226 | /*! 227 | @brief Set the gyroscope full scale range. 228 | @param range gyroscope full scale range 229 | */ 230 | /**************************************************************************/ 231 | void Adafruit_FXAS21002C::setRange(gyroRange_t range) { 232 | Adafruit_BusIO_Register CTRL_REG0(i2c_dev, GYRO_REGISTER_CTRL_REG0); 233 | Adafruit_BusIO_RegisterBits range_bits(&CTRL_REG0, 2, 0); 234 | 235 | standby(true); 236 | 237 | /* write FS[1:0] bits (bits controlling the full scale range) with correct 238 | * values according to page 40 of the datasheet */ 239 | switch (range) { 240 | case GYRO_RANGE_250DPS: 241 | range_bits.write(0b11); 242 | break; 243 | case GYRO_RANGE_500DPS: 244 | range_bits.write(0b10); 245 | break; 246 | case GYRO_RANGE_1000DPS: 247 | range_bits.write(0b01); 248 | break; 249 | case GYRO_RANGE_2000DPS: 250 | range_bits.write(0b00); 251 | break; 252 | } 253 | 254 | standby(false); 255 | 256 | _range = range; 257 | } 258 | 259 | /**************************************************************************/ 260 | /*! 261 | @brief Get the gyroscope full scale range. 262 | @return gyroscope full scale range 263 | */ 264 | /**************************************************************************/ 265 | gyroRange_t Adafruit_FXAS21002C::getRange() { return _range; } 266 | 267 | /**************************************************************************/ 268 | /*! 269 | @brief Puts device into/out of standby mode 270 | @param standby Whether we want to go into standby! 271 | */ 272 | /**************************************************************************/ 273 | void Adafruit_FXAS21002C::standby(boolean standby) { 274 | Adafruit_BusIO_Register CTRL_REG1(i2c_dev, GYRO_REGISTER_CTRL_REG1); 275 | Adafruit_BusIO_RegisterBits active_bit(&CTRL_REG1, 2, 0); 276 | 277 | if (standby) { 278 | active_bit.write(0x00); 279 | delay(100); 280 | } else { 281 | active_bit.write(0x03); 282 | } 283 | } 284 | 285 | /**************************************************************************/ 286 | /*! 287 | @brief Configures the device with certain output data rate(ODR) 288 | Supports ODRs: 800.0Hz, 400.0Hz, 200.0Hz, 289 | 100.0Hz, 50.0Hz, 25.0Hz, 12.5Hz 290 | @param ODR : the output data rate to be set to the gyroscope 291 | */ 292 | /**************************************************************************/ 293 | void Adafruit_FXAS21002C::setODR(float ODR) { 294 | Adafruit_BusIO_Register CTRL_REG1(i2c_dev, GYRO_REGISTER_CTRL_REG1); 295 | Adafruit_BusIO_RegisterBits datarate_bits(&CTRL_REG1, 3, 2); 296 | 297 | /* CTRL_REG1 should only be set in Standby or Ready mode. First enter Standby 298 | * mode */ 299 | standby(true); 300 | /* _ODR is only updated if the input ODR is one of the valid ODRs */ 301 | if (ODR == GYRO_ODR_800HZ) { 302 | datarate_bits.write(0b000); 303 | } else if (ODR == GYRO_ODR_400HZ) { 304 | datarate_bits.write(0b001); 305 | } else if (ODR == GYRO_ODR_200HZ) { 306 | datarate_bits.write(0b010); 307 | } else if (ODR == GYRO_ODR_100HZ) { 308 | datarate_bits.write(0b011); 309 | } else if (ODR == GYRO_ODR_50HZ) { 310 | datarate_bits.write(0b100); 311 | } else if (ODR == GYRO_ODR_25HZ) { 312 | datarate_bits.write(0b101); 313 | } else if (ODR == GYRO_ODR_12_5HZ) { 314 | datarate_bits.write(0b110); 315 | } 316 | // update internal _ODR variable. Note that this update happens regardless of 317 | // the validity of ODR 318 | _ODR = ODR; 319 | standby(false); 320 | } 321 | 322 | /**************************************************************************/ 323 | /*! 324 | @brief Obtain the current output data rate(ODR) from the gyroscope's 325 | register 326 | @return The Output Data Rate(ODR) in Hz 327 | */ 328 | /**************************************************************************/ 329 | float Adafruit_FXAS21002C::getODR() { return _ODR; } 330 | --------------------------------------------------------------------------------