parent
2cff906f01
commit
ff29f2133e
@ -0,0 +1,61 @@ |
||||
/*
|
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2017 Lancaster University. |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
/**
|
||||
* This file contains functions used to maintain compatability and portability. |
||||
* It also contains constants that are used elsewhere in the DAL. |
||||
*/ |
||||
|
||||
#ifndef MICROBIT_UTIL_H |
||||
#define MICROBIT_UTIL_H |
||||
|
||||
#include "MicroBitConfig.h" |
||||
|
||||
#define CREATE_KEY_VALUE_TABLE(NAME, PAIRS) const KeyValueTable NAME { PAIRS, sizeof(PAIRS) / sizeof(KeyValueTableEntry) }; |
||||
|
||||
/**
|
||||
* Provides a simple key/value pair lookup table with range lookup support. |
||||
* Normally stored in FLASH to reduce RAM usage. Keys should be pre-sorted |
||||
* in ascending order. |
||||
*/ |
||||
|
||||
struct KeyValueTableEntry |
||||
{ |
||||
const uint32_t key; |
||||
const uint32_t value; |
||||
}; |
||||
|
||||
struct KeyValueTable |
||||
{ |
||||
const KeyValueTableEntry *data; |
||||
const int length; |
||||
|
||||
KeyValueTableEntry* find(const uint32_t key) const; |
||||
uint32_t get(const uint32_t key) const; |
||||
uint32_t getKey(const uint32_t key) const; |
||||
bool hasKey(const uint32_t key) const; |
||||
}; |
||||
|
||||
|
||||
#endif |
@ -0,0 +1,240 @@ |
||||
/*
|
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2017 Lancaster University. |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#ifndef FXOS8700_H |
||||
#define FXOS8700_H |
||||
|
||||
#include "MicroBitConfig.h" |
||||
#include "MicroBitComponent.h" |
||||
#include "MicroBitPin.h" |
||||
#include "MicroBitI2C.h" |
||||
#include "MicroBitAccelerometer.h" |
||||
#include "MicroBitCompass.h" |
||||
#include "CoordinateSystem.h" |
||||
#include "MicroBitUtil.h" |
||||
|
||||
/**
|
||||
* I2C constants |
||||
*/ |
||||
#define FXOS8700_DEFAULT_ADDR 0x3C |
||||
|
||||
/**
|
||||
* FXOS8700 Register map |
||||
*/ |
||||
#define FXOS8700_STATUS_REG 0x00 |
||||
#define FXOS8700_OUT_X_MSB 0x01 |
||||
#define FXOS8700_OUT_X_LSB 0x02 |
||||
#define FXOS8700_OUT_Y_MSB 0x03 |
||||
#define FXOS8700_OUT_Y_LSB 0x04 |
||||
#define FXOS8700_OUT_Z_MSB 0x05 |
||||
#define FXOS8700_OUT_Z_LSB 0x06 |
||||
#define FXOS8700_F_SETUP 0x09 |
||||
#define FXOS8700_TRIG_CFG 0x0A |
||||
#define FXOS8700_SYSMOD 0x0B |
||||
#define FXOS8700_INT_SOURCE 0x0C |
||||
#define FXOS8700_WHO_AM_I 0x0D |
||||
#define FXOS8700_XYZ_DATA_CFG 0x0E |
||||
#define FXOS8700_HP_FILTER_CUTOFF 0x0F |
||||
#define FXOS8700_PL_STATUS 0x10 |
||||
#define FXOS8700_PL_CFG 0x11 |
||||
#define FXOS8700_PL_COUNT 0x12 |
||||
#define FXOS8700_PL_BF_ZCOMP 0x13 |
||||
#define FXOS8700_PL_THS_REG 0x14 |
||||
#define FXOS8700_A_FFMT_CFG 0x15 |
||||
#define FXOS8700_A_FFMT_SRC 0x16 |
||||
#define FXOS8700_A_FFMT_THS 0x17 |
||||
#define FXOS8700_A_FFMT_COUNT 0x18 |
||||
#define FXOS8700_TRANSIENT_CFG 0x1D |
||||
#define FXOS8700_TRANSIENT_SRC 0x1E |
||||
#define FXOS8700_TRANSIENT_THS 0x1F |
||||
#define FXOS8700_TRANSIENT_COUNT 0x20 |
||||
#define FXOS8700_PULSE_CFG 0x21 |
||||
#define FXOS8700_PULSE_SRC 0x22 |
||||
#define FXOS8700_PULSE_THSX 0x23 |
||||
#define FXOS8700_PULSE_THSY 0x24 |
||||
#define FXOS8700_PULSE_THSZ 0x25 |
||||
#define FXOS8700_PULSE_TMLT 0x26 |
||||
#define FXOS8700_PULSE_LTCY 0x27 |
||||
#define FXOS8700_PULSE_WIND 0x28 |
||||
#define FXOS8700_ASLP_COUNT 0x29 |
||||
#define FXOS8700_CTRL_REG1 0x2A |
||||
#define FXOS8700_CTRL_REG2 0x2B |
||||
#define FXOS8700_CTRL_REG3 0x2C |
||||
#define FXOS8700_CTRL_REG4 0x2D |
||||
#define FXOS8700_CTRL_REG5 0x2E |
||||
#define FXOS8700_OFF_X 0x2F |
||||
#define FXOS8700_OFF_Y 0x30 |
||||
#define FXOS8700_OFF_Z 0x31 |
||||
#define FXOS8700_M_DR_STATUS 0x32 |
||||
#define FXOS8700_M_OUT_X_MSB 0x33 |
||||
#define FXOS8700_M_OUT_X_LSB 0x34 |
||||
#define FXOS8700_M_OUT_Y_MSB 0x35 |
||||
#define FXOS8700_M_OUT_Y_LSB 0x36 |
||||
#define FXOS8700_M_OUT_Z_MSB 0x37 |
||||
#define FXOS8700_M_OUT_Z_LSB 0x38 |
||||
#define FXOS8700_CMP_X_MSB 0x39 |
||||
#define FXOS8700_CMP_X_LSB 0x3A |
||||
#define FXOS8700_CMP_Y_MSB 0x3B |
||||
#define FXOS8700_CMP_Y_LSB 0x3C |
||||
#define FXOS8700_CMP_Z_MSB 0x3D |
||||
#define FXOS8700_CMP_Z_LSB 0x3E |
||||
#define FXOS8700_M_OFF_X_MSB 0x3F |
||||
#define FXOS8700_M_OFF_X_LSB 0x40 |
||||
#define FXOS8700_M_OFF_Y_MSB 0x41 |
||||
#define FXOS8700_M_OFF_Y_LSB 0x42 |
||||
#define FXOS8700_M_OFF_Z_MSB 0x43 |
||||
#define FXOS8700_M_OFF_Z_LSB 0x44 |
||||
#define FXOS8700_MAX_X_MSB 0x45 |
||||
#define FXOS8700_MAX_X_LSB 0x46 |
||||
#define FXOS8700_MAX_Y_MSB 0x47 |
||||
#define FXOS8700_MAX_Y_LSB 0x48 |
||||
#define FXOS8700_MAX_Z_MSB 0x49 |
||||
#define FXOS8700_MAX_Z_LSB 0x4A |
||||
#define FXOS8700_MIN_X_MSB 0x4B |
||||
#define FXOS8700_MIN_X_LSB 0x4C |
||||
#define FXOS8700_MIN_Y_MSB 0x4D |
||||
#define FXOS8700_MIN_Y_LSB 0x4E |
||||
#define FXOS8700_MIN_Z_MSB 0x4F |
||||
#define FXOS8700_MIN_Z_LSB 0x50 |
||||
#define FXOS8700_TEMP 0x51 |
||||
#define FXOS8700_M_THS_CFG 0x52 |
||||
#define FXOS8700_M_THS_SRC 0x53 |
||||
#define FXOS8700_M_THS_X_MSB 0x54 |
||||
#define FXOS8700_M_THS_X_LSB 0x55 |
||||
#define FXOS8700_M_THS_Y_MSB 0x56 |
||||
#define FXOS8700_M_THS_Y_LSB 0x57 |
||||
#define FXOS8700_M_THS_Z_MSB 0x58 |
||||
#define FXOS8700_M_THS_Z_LSB 0x59 |
||||
#define FXOS8700_M_THS_COUNT 0x5A |
||||
#define FXOS8700_M_CTRL_REG1 0x5B |
||||
#define FXOS8700_M_CTRL_REG2 0x5C |
||||
#define FXOS8700_M_CTRL_REG3 0x5D |
||||
#define FXOS8700_M_INT_SRC 0x5E |
||||
#define FXOS8700_A_VECM_CFG 0x5F |
||||
#define FXOS8700_A_VECM_THS_MSB 0x60 |
||||
#define FXOS8700_A_VECM_THS_LSB 0x61 |
||||
#define FXOS8700_A_VECM_CNT 0x62 |
||||
#define FXOS8700_A_VECM_INITX_MSB 0x63 |
||||
#define FXOS8700_A_VECM_INITX_LSB 0x64 |
||||
#define FXOS8700_A_VECM_INITY_MSB 0x65 |
||||
#define FXOS8700_A_VECM_INITY_LSB 0x66 |
||||
#define FXOS8700_A_VECM_INITZ_MSB 0x67 |
||||
#define FXOS8700_A_VECM_INITZ_LSB 0x68 |
||||
#define FXOS8700_M_VECM_CFG 0x69 |
||||
#define FXOS8700_M_VECM_THS_MSB 0x6A |
||||
#define FXOS8700_M_VECM_THS_LSB 0x6B |
||||
#define FXOS8700_M_VECM_CNT 0x6C |
||||
#define FXOS8700_M_VECM_INITX_MSB 0x6D |
||||
#define FXOS8700_M_VECM_INITX_LSB 0x6E |
||||
#define FXOS8700_M_VECM_INITY_MSB 0x6F |
||||
#define FXOS8700_M_VECM_INITY_LSB 0x70 |
||||
#define FXOS8700_M_VECM_INITZ_MSB 0x71 |
||||
#define FXOS8700_M_VECM_INITZ_LSB 0x72 |
||||
#define FXOS8700_A_FFMT_THS_X_MSB 0x73 |
||||
#define FXOS8700_A_FFMT_THS_X_LSB 0x74 |
||||
#define FXOS8700_A_FFMT_THS_Y_MSB 0x75 |
||||
#define FXOS8700_A_FFMT_THS_Y_LSB 0x76 |
||||
#define FXOS8700_A_FFMT_THS_Z_MSB 0x77 |
||||
#define FXOS8700_A_FFMT_THS_Z_LSB 0x78 |
||||
|
||||
|
||||
/**
|
||||
* FXOS8700 constants |
||||
*/ |
||||
#define FXOS8700_WHOAMI_VAL 0xC7 |
||||
|
||||
/**
|
||||
* Class definition for an FXSO8700 hybrid Accelerometer/Magnetometer |
||||
*/ |
||||
class FXOS8700 : public MicroBitAccelerometer, public MicroBitCompass |
||||
{ |
||||
MicroBitI2C& i2c; // The I2C interface to use.
|
||||
MicroBitPin &int1; // Data ready interrupt.
|
||||
uint16_t address; // I2C address of this accelerometer.
|
||||
|
||||
public: |
||||
|
||||
/**
|
||||
* Constructor. |
||||
* Create a software abstraction of an accelerometer. |
||||
* |
||||
* @param _i2c an instance of I2C used to communicate with the onboard accelerometer. |
||||
* @param _int1 a pin connected to the INT1 interrupt source of the sensor. |
||||
* @param address the default I2C address of the accelerometer. Defaults to: FXOS8700_DEFAULT_ADDR. |
||||
* |
||||
*/ |
||||
FXOS8700(MicroBitI2C &_i2c, MicroBitPin &_int1, CoordinateSpace &coordinateSpace, uint16_t address = FXOS8700_DEFAULT_ADDR, uint16_t aid = MICROBIT_ID_ACCELEROMETER, uint16_t cid = MICROBIT_ID_COMPASS); |
||||
|
||||
/**
|
||||
* Configures the accelerometer for G range and sample rate defined |
||||
* in this object. The nearest values are chosen to those defined |
||||
* that are supported by the hardware. The instance variables are then |
||||
* updated to reflect reality. |
||||
* |
||||
* @return DEVICE_OK on success, DEVICE_I2C_ERROR if the accelerometer could not be configured. |
||||
*/ |
||||
int configure(); |
||||
|
||||
/**
|
||||
* Reads the acceleration data from the accelerometer, and stores it in our buffer. |
||||
* This only happens if the accelerometer indicates that it has new data via int1. |
||||
* |
||||
* On first use, this member function will attempt to add this component to the |
||||
* list of fiber components in order to constantly update the values stored |
||||
* by this object. |
||||
* |
||||
* This technique is called lazy instantiation, and it means that we do not |
||||
* obtain the overhead from non-chalantly adding this component to fiber components. |
||||
* |
||||
* @return DEVICE_OK on success, DEVICE_I2C_ERROR if the read request fails. |
||||
*/ |
||||
virtual int requestUpdate(); |
||||
|
||||
/**
|
||||
* Attempts to read the 8 bit ID from the accelerometer, this can be used for |
||||
* validation purposes. |
||||
* |
||||
* @return the 8 bit ID returned by the accelerometer, or DEVICE_I2C_ERROR if the request fails. |
||||
* |
||||
* @code |
||||
* accelerometer.whoAmI(); |
||||
* @endcode |
||||
*/ |
||||
int whoAmI(); |
||||
|
||||
/**
|
||||
* A periodic callback invoked by the fiber scheduler idle thread. |
||||
* |
||||
* Internally calls updateSample(). |
||||
*/ |
||||
virtual void idleCallback(); |
||||
|
||||
/**
|
||||
* Destructor. |
||||
*/ |
||||
~FXOS8700(); |
||||
|
||||
}; |
||||
|
||||
#endif |
@ -0,0 +1,152 @@ |
||||
/*
|
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2017 Lancaster University. |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#ifndef LSM303_A_H |
||||
#define LSM303_A_H |
||||
|
||||
#include "MicroBitConfig.h" |
||||
#include "MicroBitComponent.h" |
||||
#include "CoordinateSystem.h" |
||||
#include "MicroBitAccelerometer.h" |
||||
#include "MicroBitI2C.h" |
||||
#include "MicroBitUtil.h" |
||||
|
||||
/**
|
||||
* I2C constants |
||||
*/ |
||||
#define LSM303_A_DEFAULT_ADDR 0x32 |
||||
|
||||
/**
|
||||
* LSM303 Register map (partial) |
||||
*/ |
||||
|
||||
#define LSM303_STATUS_REG_AUX_A 0x07 |
||||
#define LSM303_OUT_TEMP_L_A 0x0C |
||||
#define LSM303_OUT_TEMP_H_A 0x0D |
||||
#define LSM303_INT_COUNTER_REG_A 0x0E |
||||
#define LSM303_WHO_AM_I_A 0x0F |
||||
#define LSM303_TEMP_CFG_REG_A 0x1F |
||||
#define LSM303_CTRL_REG1_A 0x20 |
||||
#define LSM303_CTRL_REG2_A 0x21 |
||||
#define LSM303_CTRL_REG3_A 0x22 |
||||
#define LSM303_CTRL_REG4_A 0x23 |
||||
#define LSM303_CTRL_REG5_A 0x24 |
||||
#define LSM303_CTRL_REG6_A 0x25 |
||||
#define LSM303_DATACAPTURE_A 0x26 |
||||
#define LSM303_STATUS_REG_A 0x27 |
||||
#define LSM303_OUT_X_L_A 0x28 |
||||
#define LSM303_OUT_X_H_A 0x29 |
||||
#define LSM303_OUT_Y_L_A 0x2A |
||||
#define LSM303_OUT_Y_H_A 0x2B |
||||
#define LSM303_OUT_Z_L_A 0x2C |
||||
#define LSM303_OUT_Z_H_A 0x2D |
||||
#define LSM303_FIFO_CTRL_REG_A 0x2E |
||||
#define LSM303_FIFO_SRC_REG_A 0x2F |
||||
#define LSM303_INT1_CFG_A 0x30 |
||||
#define LSM303_INT1_SRC_A 0x31 |
||||
#define LSM303_INT1_THS_A 0x32 |
||||
#define LSM303_INT1_DURATION_A 0x33 |
||||
#define LSM303_INT2_CFG_A 0x34 |
||||
#define LSM303_INT2_SRC_A 0x35 |
||||
#define LSM303_INT2_THS_A 0x36 |
||||
#define LSM303_INT2_DURATION_A 0x37 |
||||
#define LSM303_CLICK_CFG_A 0x38 |
||||
#define LSM303_CLICK_SRC_A 0x39 |
||||
#define LSM303_CLICK_THS_A 0x3A |
||||
#define LSM303_TIME_LIMIT_A 0x3B |
||||
#define LSM303_TIME_LATENCY_A 0x3C |
||||
#define LSM303_TIME_WINDOW_A 0x3D |
||||
#define LSM303_ACT_THS_A 0x3E |
||||
#define LSM303_ACT_DUR_A 0x3F |
||||
|
||||
/**
|
||||
* LSM303_A constants |
||||
*/ |
||||
#define LSM303_A_WHOAMI_VAL 0x33 |
||||
|
||||
/**
|
||||
* Class definition for LSM303Accelerometer. |
||||
* This class provides a simple wrapper between the hybrid FXOS8700 accelerometer and higher level accelerometer funcitonality. |
||||
*/ |
||||
class LSM303Accelerometer : public MicroBitAccelerometer |
||||
{ |
||||
MicroBitI2C& i2c; // The I2C interface to use.
|
||||
MicroBitPin& int1; // Data ready interrupt.
|
||||
uint16_t address; // I2C address of this accelerometer.
|
||||
|
||||
public: |
||||
|
||||
/**
|
||||
* Constructor. |
||||
* Create a software abstraction of an accelerometer. |
||||
* |
||||
* @param coordinateSpace The orientation of the sensor. Defaults to: SIMPLE_CARTESIAN |
||||
* @param id The unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER |
||||
* |
||||
*/ |
||||
LSM303Accelerometer(MicroBitI2C& _i2c, MicroBitPin &_int1, CoordinateSpace &coordinateSpace, uint16_t address = LSM303_A_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_ACCELEROMETER); |
||||
|
||||
/**
|
||||
* Configures the accelerometer for G range and sample rate defined |
||||
* in this object. The nearest values are chosen to those defined |
||||
* that are supported by the hardware. The instance variables are then |
||||
* updated to reflect reality. |
||||
* |
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured. |
||||
* |
||||
* @note This method should be overidden by the hardware driver to implement the requested |
||||
* changes in hardware. |
||||
*/ |
||||
virtual int configure(); |
||||
|
||||
/**
|
||||
* Poll to see if new data is available from the hardware. If so, update it. |
||||
* n.b. it is not necessary to explicitly call this funciton to update data |
||||
* (it normally happens in the background when the scheduler is idle), but a check is performed |
||||
* if the user explicitly requests up to date data. |
||||
* |
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the update fails. |
||||
* |
||||
* @note This method should be overidden by the hardware driver to implement the requested |
||||
* changes in hardware. |
||||
*/ |
||||
virtual int requestUpdate(); |
||||
|
||||
/**
|
||||
* A periodic callback invoked by the fiber scheduler idle thread. |
||||
* |
||||
* Internally calls updateSample(). |
||||
*/ |
||||
virtual void idleCallback(); |
||||
|
||||
|
||||
/**
|
||||
* Destructor. |
||||
*/ |
||||
~LSM303Accelerometer(); |
||||
|
||||
}; |
||||
|
||||
#endif |
||||
|
@ -0,0 +1,139 @@ |
||||
/*
|
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2017 Lancaster University. |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#ifndef LSM303_M_H |
||||
#define LSM303_M_H |
||||
|
||||
#include "MicroBitConfig.h" |
||||
#include "MicroBitComponent.h" |
||||
#include "CoordinateSystem.h" |
||||
#include "MicroBitCompass.h" |
||||
#include "MicroBitI2C.h" |
||||
#include "MicroBitUtil.h" |
||||
|
||||
/**
|
||||
* Term to convert sample data into SI units.
|
||||
* TODO: Write this, if necessary |
||||
*/ |
||||
#define LSM303_M_NORMALIZE_SAMPLE(x) ((int)x) |
||||
|
||||
/**
|
||||
* LSM303_M MAGIC ID value |
||||
* Returned from the MAG_WHO_AM_I register for ID purposes. |
||||
*/ |
||||
#define LSM303_M_WHOAMI_VAL 0x20 |
||||
|
||||
/**
|
||||
* I2C constants |
||||
*/ |
||||
#define LSM303_M_DEFAULT_ADDR 0x3C |
||||
|
||||
/**
|
||||
* LSM303_M Register map |
||||
*/ |
||||
#define LSM303_OFFSET_X_REG_L_M 0x45 |
||||
#define LSM303_OFFSET_X_REG_H_M 0x46 |
||||
#define LSM303_OFFSET_Y_REG_L_M 0x47 |
||||
#define LSM303_OFFSET_Y_REG_H_M 0x48 |
||||
#define LSM303_OFFSET_Z_REG_L_M 0x49 |
||||
#define LSM303_OFFSET_Z_REG_H_M 0x4A |
||||
#define LSM303_WHO_AM_I_M 0x4F |
||||
#define LSM303_CFG_REG_A_M 0x60 |
||||
#define LSM303_CFG_REG_B_M 0x61 |
||||
#define LSM303_CFG_REG_C_M 0x62 |
||||
#define LSM303_INT_CRTL_REG_M 0x63 |
||||
#define LSM303_INT_SOURCE_REG_M 0x64 |
||||
#define LSM303_INT_THS_L_REG_M 0x65 |
||||
#define LSM303_INT_THS_H_REG_M 0x66 |
||||
#define LSM303_STATUS_REG_M 0x67 |
||||
#define LSM303_OUTX_L_REG_M 0x68 |
||||
#define LSM303_OUTX_H_REG_M 0x69 |
||||
#define LSM303_OUTY_L_REG_M 0x6A |
||||
#define LSM303_OUTY_H_REG_M 0x6B |
||||
#define LSM303_OUTZ_L_REG_M 0x6C |
||||
#define LSM303_OUTZ_H_REG_M 0x6D |
||||
|
||||
|
||||
|
||||
/**
|
||||
* Class definition for LSM303_M. |
||||
* |
||||
* This class provides the low level driver implementation for the LSM303_M Magnetometer |
||||
* |
||||
*/ |
||||
class LSM303Magnetometer : public MicroBitCompass |
||||
{ |
||||
MicroBitI2C& i2c; // The I2C interface to use.
|
||||
MicroBitPin& int1; // Data ready interrupt.
|
||||
uint16_t address; // I2C address of this compass.
|
||||
|
||||
public: |
||||
|
||||
/**
|
||||
* Constructor. |
||||
* Create a software abstraction of an compass. |
||||
* |
||||
* @param coordinateSpace The orientation of the sensor. Defaults to: SIMPLE_CARTESIAN |
||||
* @param id The unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER |
||||
* |
||||
*/ |
||||
LSM303Magnetometer(MicroBitI2C& _i2c, MicroBitPin &_int1, CoordinateSpace &coordinateSpace, uint16_t address = LSM303_M_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_COMPASS); |
||||
|
||||
/**
|
||||
* Configures the compass for the sample rate defined in this object.
|
||||
* The nearest values are chosen to those defined |
||||
* that are supported by the hardware. The instance variables are then |
||||
* updated to reflect reality. |
||||
* |
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the compass could not be configured. |
||||
*/ |
||||
virtual int configure(); |
||||
|
||||
/**
|
||||
* Poll to see if new data is available from the hardware. If so, update it. |
||||
* n.b. it is not necessary to explicitly call this function to update data |
||||
* (it normally happens in the background when the scheduler is idle), but a check is performed |
||||
* if the user explicitly requests up to date data. |
||||
* |
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the update fails. |
||||
*/ |
||||
virtual int requestUpdate(); |
||||
|
||||
/**
|
||||
* A periodic callback invoked by the fiber scheduler idle thread. |
||||
* |
||||
* Internally calls updateSample(). |
||||
*/ |
||||
virtual void idleCallback(); |
||||
|
||||
|
||||
/**
|
||||
* Destructor. |
||||
*/ |
||||
~LSM303Magnetometer(); |
||||
|
||||
}; |
||||
|
||||
#endif |
||||
|
@ -0,0 +1,134 @@ |
||||
/*
|
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2017 Lancaster University. |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#ifndef MAG3110_H |
||||
#define MAG3110_H |
||||
|
||||
#include "MicroBitConfig.h" |
||||
#include "MicroBitComponent.h" |
||||
#include "CoordinateSystem.h" |
||||
#include "MicroBitCompass.h" |
||||
#include "MicroBitI2C.h" |
||||
#include "MicroBitUtil.h" |
||||
|
||||
/**
|
||||
* Term to convert sample data into SI units |
||||
*/ |
||||
#define MAG3110_NORMALIZE_SAMPLE(x) (100 * (int)x) |
||||
|
||||
/**
|
||||
* MAG3110 MAGIC ID value |
||||
* Returned from the MAG_WHO_AM_I register for ID purposes. |
||||
*/ |
||||
#define MAG3110_WHOAMI_VAL 0xC4 |
||||
|
||||
/**
|
||||
* I2C constants |
||||
*/ |
||||
#define MAG3110_DEFAULT_ADDR 0x1D |
||||
|
||||
/**
|
||||
* MAG3110 Register map |
||||
*/ |
||||
#define MAG_DR_STATUS 0x00 |
||||
#define MAG_OUT_X_MSB 0x01 |
||||
#define MAG_OUT_X_LSB 0x02 |
||||
#define MAG_OUT_Y_MSB 0x03 |
||||
#define MAG_OUT_Y_LSB 0x04 |
||||
#define MAG_OUT_Z_MSB 0x05 |
||||
#define MAG_OUT_Z_LSB 0x06 |
||||
#define MAG_WHOAMI 0x07 |
||||
#define MAG_SYSMOD 0x08 |
||||
#define MAG_OFF_X_MSB 0x09 |
||||
#define MAG_OFF_X_LSB 0x0A |
||||
#define MAG_OFF_Y_MSB 0x0B |
||||
#define MAG_OFF_Y_LSB 0x0C |
||||
#define MAG_OFF_Z_MSB 0x0D |
||||
#define MAG_OFF_Z_LSB 0x0E |
||||
#define MAG_DIE_TEMP 0x0F |
||||
#define MAG_CTRL_REG1 0x10 |
||||
#define MAG_CTRL_REG2 0x11 |
||||
|
||||
|
||||
/**
|
||||
* Class definition for MAG3110. |
||||
* |
||||
* This class provides the low level driver implementation for the MAG3110 Magnetometer |
||||
* |
||||
*/ |
||||
class MAG3110 : public MicroBitCompass |
||||
{ |
||||
MicroBitI2C& i2c; // The I2C interface to use.
|
||||
MicroBitPin& int1; // Data ready interrupt.
|
||||
uint16_t address; // I2C address of this compass.
|
||||
|
||||
public: |
||||
|
||||
/**
|
||||
* Constructor. |
||||
* Create a software abstraction of an compass. |
||||
* |
||||
* @param coordinateSpace The orientation of the sensor. Defaults to: SIMPLE_CARTESIAN |
||||
* @param id The unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER |
||||
* |
||||
*/ |
||||
MAG3110(MicroBitI2C& _i2c, MicroBitPin &_int1, CoordinateSpace &coordinateSpace, uint16_t address = MAG3110_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_COMPASS); |
||||
|
||||
/**
|
||||
* Configures the compass for the sample rate defined in this object.
|
||||
* The nearest values are chosen to those defined |
||||
* that are supported by the hardware. The instance variables are then |
||||
* updated to reflect reality. |
||||
* |
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the compass could not be configured. |
||||
*/ |
||||
virtual int configure(); |
||||
|
||||
/**
|
||||
* Poll to see if new data is available from the hardware. If so, update it. |
||||
* n.b. it is not necessary to explicitly call this function to update data |
||||
* (it normally happens in the background when the scheduler is idle), but a check is performed |
||||
* if the user explicitly requests up to date data. |
||||
* |
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the update fails. |
||||
*/ |
||||
virtual int requestUpdate(); |
||||
|
||||
/**
|
||||
* A periodic callback invoked by the fiber scheduler idle thread. |
||||
* |
||||
* Internally calls updateSample(). |
||||
*/ |
||||
virtual void idleCallback(); |
||||
|
||||
|
||||
/**
|
||||
* Destructor. |
||||
*/ |
||||
~MAG3110(); |
||||
|
||||
}; |
||||
|
||||
#endif |
||||
|
@ -0,0 +1,128 @@ |
||||
/*
|
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2017 Lancaster University. |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#ifndef MMA8653_H |
||||
#define MMA8653_H |
||||
|
||||
#include "MicroBitConfig.h" |
||||
#include "MicroBitComponent.h" |
||||
#include "CoordinateSystem.h" |
||||
#include "MicroBitAccelerometer.h" |
||||
#include "MicroBitI2C.h" |
||||
#include "MicroBitUtil.h" |
||||
|
||||
/**
|
||||
* Relevant pin assignments |
||||
*/ |
||||
#define MICROBIT_PIN_ACCEL_DATA_READY P0_28 |
||||
|
||||
/**
|
||||
* I2C constants |
||||
*/ |
||||
#define MMA8653_DEFAULT_ADDR 0x3A |
||||
|
||||
/**
|
||||
* MMA8653 Register map (partial) |
||||
*/ |
||||
#define MMA8653_STATUS 0x00 |
||||
#define MMA8653_OUT_X_MSB 0x01 |
||||
#define MMA8653_WHOAMI 0x0D |
||||
#define MMA8653_XYZ_DATA_CFG 0x0E |
||||
#define MMA8653_CTRL_REG1 0x2A |
||||
#define MMA8653_CTRL_REG2 0x2B |
||||
#define MMA8653_CTRL_REG3 0x2C |
||||
#define MMA8653_CTRL_REG4 0x2D |
||||
#define MMA8653_CTRL_REG5 0x2E |
||||
|
||||
|
||||
/**
|
||||
* MMA8653 constants |
||||
*/ |
||||
#define MMA8653_WHOAMI_VAL 0x5A |
||||
|
||||
/**
|
||||
* Class definition for MMA8653. |
||||
* This class provides a simple wrapper between the hybrid FXOS8700 accelerometer and higher level accelerometer funcitonality. |
||||
*/ |
||||
class MMA8653 : public MicroBitAccelerometer |
||||
{ |
||||
MicroBitI2C& i2c; // The I2C interface to use.
|
||||
MicroBitPin& int1; // Data ready interrupt.
|
||||
uint16_t address; // I2C address of this accelerometer.
|
||||
|
||||
public: |
||||
|
||||
/**
|
||||
* Constructor. |
||||
* Create a software abstraction of an accelerometer. |
||||
* |
||||
* @param coordinateSpace The orientation of the sensor. Defaults to: SIMPLE_CARTESIAN |
||||
* @param id The unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER |
||||
* |
||||
*/ |
||||
MMA8653(MicroBitI2C& _i2c, MicroBitPin &_int1, CoordinateSpace &coordinateSpace, uint16_t address = MMA8653_DEFAULT_ADDR, uint16_t id = MICROBIT_ID_ACCELEROMETER); |
||||
|
||||
/**
|
||||
* Configures the accelerometer for G range and sample rate defined |
||||
* in this object. The nearest values are chosen to those defined |
||||
* that are supported by the hardware. The instance variables are then |
||||
* updated to reflect reality. |
||||
* |
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the accelerometer could not be configured. |
||||
* |
||||
* @note This method should be overidden by the hardware driver to implement the requested |
||||
* changes in hardware. |
||||
*/ |
||||
virtual int configure(); |
||||
|
||||
/**
|
||||
* Poll to see if new data is available from the hardware. If so, update it. |
||||
* n.b. it is not necessary to explicitly call this funciton to update data |
||||
* (it normally happens in the background when the scheduler is idle), but a check is performed |
||||
* if the user explicitly requests up to date data. |
||||
* |
||||
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the update fails. |
||||
* |
||||
* @note This method should be overidden by the hardware driver to implement the requested |
||||
* changes in hardware. |
||||
*/ |
||||
virtual int requestUpdate(); |
||||
|
||||
/**
|
||||
* A periodic callback invoked by the fiber scheduler idle thread. |
||||
* |
||||
* Internally calls updateSample(). |
||||
*/ |
||||
virtual void idleCallback(); |
||||
|
||||
|
||||
/**
|
||||
* Destructor. |
||||
*/ |
||||
~MMA8653(); |
||||
|
||||
}; |
||||
|
||||
#endif |
||||
|