Advertisement
gyhn

Untitled

Nov 22nd, 2019
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.43 KB | None | 0 0
  1. /**
  2.  * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are met:
  6.  *
  7.  * Redistributions of source code must retain the above copyright
  8.  * notice, this list of conditions and the following disclaimer.
  9.  *
  10.  * Redistributions in binary form must reproduce the above copyright
  11.  * notice, this list of conditions and the following disclaimer in the
  12.  * documentation and/or other materials provided with the distribution.
  13.  *
  14.  * Neither the name of the copyright holder nor the names of the
  15.  * contributors may be used to endorse or promote products derived from
  16.  * this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22.  * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  23.  * OR CONTRIBUTORS BE LIABLE FOR ANY
  24.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25.  * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31.  * ANY WAY OUT OF THE USE OF THIS
  32.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  33.  *
  34.  * The information provided is believed to be accurate and reliable.
  35.  * The copyright holder assumes no responsibility
  36.  * for the consequences of use
  37.  * of such information nor for any infringement of patents or
  38.  * other rights of third parties which may result from its use.
  39.  * No license is granted by implication or otherwise under any patent or
  40.  * patent rights of the copyright holder.
  41.  *
  42.  * @file    bsec.h
  43.  * @date    25 July 2019
  44.  * @version 1.2.1474
  45.  *
  46.  */
  47.  
  48. #ifndef BSEC_CLASS_H
  49. #define BSEC_CLASS_H
  50.  
  51. /* Includes */
  52. #include "Arduino.h"
  53. #include "Wire.h"
  54. #include "SPI.h"
  55. #include "inc/bsec_datatypes.h"
  56. #include "inc/bsec_interface.h"
  57. #include "bme680/bme680.h"
  58.  
  59. /* BSEC class definition */
  60. class Bsec
  61. {
  62. public:
  63.     /* Public variables */
  64.     bsec_version_t version; // Stores the version of the BSEC algorithm
  65.     int64_t nextCall;       // Stores the time when the algorithm has to be called next in ms
  66.     int8_t bme680Status;    // Placeholder for the BME680 driver's error codes
  67.     bsec_library_return_t status;
  68.     float iaq, rawTemperature, pressure, rawHumidity, gasResistance, stabStatus, runInStatus, temperature, humidity,
  69.         staticIaq, co2Equivalent, breathVocEquivalent, compGasValue, gasPercentage;
  70.     uint8_t iaqAccuracy, staticIaqAccuracy, co2Accuracy, breathVocAccuracy, compGasAccuracy, gasPercentageAcccuracy;
  71.     int64_t outputTimestamp; // Timestamp in ms of the output
  72.     static TwoWire *wireObj;
  73.     static SPIClass *spiObj;
  74.  
  75.     /* Public APIs */
  76.     /**
  77.      * @brief Constructor
  78.      */
  79.     Bsec();
  80.  
  81.     /**
  82.      * @brief Function to initialize the BSEC library and the BME680 sensor
  83.      * @param devId     : Device identifier parameter for the read/write interface functions
  84.      * @param intf      : Physical communication interface
  85.      * @param read      : Pointer to the read function
  86.      * @param write     : Pointer to the write function
  87.      * @param idleTask  : Pointer to the idling task
  88.      */
  89.     void begin(uint8_t devId, enum bme680_intf intf, bme680_com_fptr_t read, bme680_com_fptr_t write, bme680_delay_fptr_t idleTask);
  90.  
  91.     /**
  92.      * @brief Function to initialize the BSEC library and the BME680 sensor
  93.      * @param i2cAddr   : I2C address
  94.      * @param i2c       : Pointer to the TwoWire object
  95.      * @param idleTask  : Task to be called when idling
  96.      */
  97.     void begin(uint8_t i2cAddr, TwoWire &i2c, bme680_delay_fptr_t idleTask = delay_ms);
  98.  
  99.     /**
  100.      * @brief Function to initialize the BSEC library and the BME680 sensor
  101.      * @param chipSelect    : SPI chip select
  102.      * @param spi           : Pointer to the SPIClass object
  103.      * @param idleTask  : Task to be called when idling
  104.      */
  105.     void begin(uint8_t chipSelect, SPIClass &spi, bme680_delay_fptr_t idleTask = delay_ms);
  106.  
  107.     /**
  108.      * @brief Function that sets the desired sensors and the sample rates
  109.      * @param sensorList    : The list of output sensors
  110.      * @param nSensors      : Number of outputs requested
  111.      * @param sampleRate    : The sample rate of requested sensors
  112.      */
  113.     void updateSubscription(bsec_virtual_sensor_t sensorList[], uint8_t nSensors, float sampleRate = BSEC_SAMPLE_RATE_ULP);
  114.  
  115.     /**
  116.      * @brief Callback from the user to trigger reading of data from the BME680, process and store outputs
  117.      * @return true if there are new outputs. false otherwise
  118.      */
  119.     // bool run(void);
  120.     bool run(long time_trigger);
  121.  
  122.     /**
  123.      * @brief Function to get the state of the algorithm to save to non-volatile memory
  124.      * @param state         : Pointer to a memory location that contains the state
  125.      */
  126.     void getState(uint8_t *state);
  127.  
  128.     /**
  129.      * @brief Function to set the state of the algorithm from non-volatile memory
  130.      * @param state         : Pointer to a memory location that contains the state
  131.      */
  132.     void setState(uint8_t *state);
  133.  
  134.     /**
  135.      * @brief Function to set the configuration of the algorithm from memory
  136.      * @param state         : Pointer to a memory location that contains the configuration
  137.      */
  138.     void setConfig(const uint8_t *config);
  139.  
  140.     /**
  141.      * @brief Function to set the temperature offset
  142.      * @param tempOffset    : Temperature offset in degree Celsius
  143.      */
  144.     void setTemperatureOffset(float tempOffset)
  145.     {
  146.         _tempOffset = tempOffset;
  147.     }
  148.  
  149.     /**
  150.      * @brief Function to calculate an int64_t timestamp in milliseconds
  151.      */
  152.     // int64_t getTimeMs(void);
  153.     int64_t getTimeMs(long time_trigger);
  154.  
  155.     /**
  156.     * @brief Task that delays for a ms period of time
  157.     * @param period : Period of time in ms
  158.     */
  159.     static void delay_ms(uint32_t period);
  160.  
  161.     /**
  162.     * @brief Callback function for reading registers over I2C
  163.     * @param devId      : Library agnostic parameter to identify the device to communicate with
  164.     * @param regAddr    : Register address
  165.     * @param regData    : Pointer to the array containing the data to be read
  166.     * @param length : Length of the array of data
  167.     * @return   Zero for success, non-zero otherwise
  168.     */
  169.     static int8_t i2cRead(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length);
  170.  
  171.     /**
  172.     * @brief Callback function for writing registers over I2C
  173.     * @param devId      : Library agnostic parameter to identify the device to communicate with
  174.     * @param regAddr    : Register address
  175.     * @param regData    : Pointer to the array containing the data to be written
  176.     * @param length : Length of the array of data
  177.     * @return   Zero for success, non-zero otherwise
  178.     */
  179.     static int8_t i2cWrite(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length);
  180.  
  181.     /**
  182.     * @brief Callback function for reading and writing registers over SPI
  183.     * @param devId      : Library agnostic parameter to identify the device to communicate with
  184.     * @param regAddr    : Register address
  185.     * @param regData    : Pointer to the array containing the data to be read or written
  186.     * @param length : Length of the array of data
  187.     * @return   Zero for success, non-zero otherwise
  188.     */
  189.     static int8_t spiTransfer(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length);
  190.  
  191. private:
  192.     /* Private variables */
  193.     struct bme680_dev _bme680;
  194.     struct bme680_field_data _data;
  195.     float _tempOffset;
  196.     // Global variables to help create a millisecond timestamp that doesn't overflow every 51 days.
  197.     // If it overflows, it will have a negative value. Something that should never happen.
  198.     uint32_t millisOverflowCounter;
  199.     uint32_t lastTime;
  200.  
  201.     /* Private APIs */
  202.     /**
  203.      * @brief Get the version of the BSEC library
  204.      */
  205.     void getVersion(void);
  206.  
  207.     /**
  208.      * @brief Read data from the BME680 and process it
  209.      * @param currTimeNs: Current time in ns
  210.      * @param bme680Settings: BME680 sensor's settings
  211.      * @return true if there are new outputs. false otherwise
  212.      */
  213.     bool readProcessData(int64_t currTimeNs, bsec_bme_settings_t bme680Settings);
  214.  
  215.     /**
  216.      * @brief Set the BME680 sensor's configuration
  217.      * @param bme680Settings: Settings to configure the BME680
  218.      * @return BME680 return code. BME680_OK for success, failure otherwise
  219.      */
  220.     int8_t setBme680Config(bsec_bme_settings_t bme680Settings);
  221.  
  222.     /**
  223.      * @brief Common code for the begin function
  224.      */
  225.     void beginCommon(void);
  226.  
  227.     /**
  228.      * @brief Function to zero the outputs
  229.      */
  230.     void zeroOutputs(void);
  231. };
  232.  
  233. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement