Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue when missing motion sensor was crashing instead of panicking. #360

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 27 additions & 159 deletions inc/MicroBitAccelerometer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,84 +27,53 @@ DEALINGS IN THE SOFTWARE.

#include "CodalConfig.h"
#include "MicroBitCompat.h"
#include "codal-core/inc/core/CodalComponent.h"
#include "codal-core/inc/driver-models/Accelerometer.h"
#include "codal-core/inc/driver-models/Compass.h"
#include "codal-core/inc/driver-models/I2C.h"
#include "codal-core/inc/driver-models/Pin.h"
#include "codal-core/inc/types/CoordinateSystem.h"
#include "MicroBitI2C.h"


/**
* Class definition for MicroBitAccelerometer.
*/
class MicroBitAccelerometer : public Accelerometer
namespace codal
{
public:
/**
* Class definition for MicroBitAccelerometer.
*/
class MicroBitAccelerometer : public Accelerometer
{
public:

static Accelerometer* driver; // The instance of an Accelerometer driver.
static NRF52Pin irq1; // IRQ pin for detected acceleromters to use
static CoordinateSpace coordinateSpace; // Default coordinate space

/**
* Constructor.
* Create a software abstraction of an accelerometer.
* Create an instance of an accelerometer that detects which on-board
* sensor is present and initialises the respective driver.
*
* @param coordinateSpace the orientation of the sensor. Defaults to: SIMPLE_CARTESIAN
* @param coordinateSpace the orientation of the sensor.
* @param id the unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER
*
*/
MicroBitAccelerometer(MicroBitI2C &i2c, uint16_t id = MICROBIT_ID_ACCELEROMETER);
MicroBitAccelerometer(MicroBitI2C &i2c, CoordinateSpace &coordinateSpace, uint16_t id = MICROBIT_ID_ACCELEROMETER);

/**
* Device autodetection. Scans the given I2C bus for supported accelerometer devices.
* if found, constructs an appropriate driver and returns it.
*
* @param i2c the bus to scan.
*
*/
static Accelerometer& autoDetect(MicroBitI2C &i2c);

/**
* Attempts to set the sample rate of the accelerometer to the specified value (in ms).
*
* @param period the requested time between samples, in milliseconds.
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
* Constructor for an empty driver.
*
* @note The requested rate may not be possible on the hardware. In this case, the
* nearest lower rate is chosen.
* This constructor should only be called when the on-board sensor is not
* detected, and we need an instance that's able to panic on first usage.
*
* @note This method should be overriden (if supported) by specific accelerometer device drivers.
*/
int setPeriod(int period);

/**
* Reads the currently configured sample rate of the accelerometer.
* @param coordinateSpace the orientation of the sensor.
* @param id the unique EventModel id of this component. Defaults to: MICROBIT_ID_ACCELEROMETER
*
* @return The time between samples, in milliseconds.
*/
int getPeriod();
MicroBitAccelerometer(CoordinateSpace &coordinateSpace, uint16_t id = MICROBIT_ID_ACCELEROMETER);

/**
* Attempts to set the sample range of the accelerometer to the specified value (in g).
*
* @param range The requested sample range of samples, in g.
*
* @return MICROBIT_OK on success, MICROBIT_I2C_ERROR is the request fails.
* Device autodetection. Scans the given I2C bus for supported accelerometer devices.
* if found, constructs an appropriate driver and returns it.
*
* @note The requested range may not be possible on the hardware. In this case, the
* nearest lower range is chosen.
* @param i2c the bus to scan.
*
* @note This method should be overriden (if supported) by specific accelerometer device drivers.
*/
int setRange(int range);

/**
* Reads the currently configured sample range of the accelerometer.
*
* @return The sample range, in g.
*/
int getRange();
static Accelerometer& autoDetect(MicroBitI2C &i2c);

/**
* Configures the accelerometer for G range and sample rate defined
Expand All @@ -113,125 +82,24 @@ class MicroBitAccelerometer : public Accelerometer
* 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.
*/
int configure();
virtual int configure() override;

/**
* 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
* 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.
*
* @note This method should be overidden by the hardware driver to implement the requested
* changes in hardware.
*/
int requestUpdate();

/**
* Reads the last accelerometer value stored, and provides it in the coordinate system requested.
*
* @param coordinateSpace The coordinate system to use.
* @return The force measured in each axis, in milli-g.
*/
Sample3D getSample(CoordinateSystem coordinateSystem);

/**
* Reads the last accelerometer value stored, and in the coordinate system defined in the constructor.
* @return The force measured in each axis, in milli-g.
*/
Sample3D getSample();

/**
* reads the value of the x axis from the latest update retrieved from the accelerometer,
* using the default coordinate system as specified in the constructor.
*
* @return the force measured in the x axis, in milli-g.
*/
int getX();

/**
* reads the value of the y axis from the latest update retrieved from the accelerometer,
* using the default coordinate system as specified in the constructor.
*
* @return the force measured in the y axis, in milli-g.
*/
int getY();

/**
* reads the value of the z axis from the latest update retrieved from the accelerometer,
* using the default coordinate system as specified in the constructor.
*
* @return the force measured in the z axis, in milli-g.
*/
int getZ();

/**
* Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.
*
* @return The pitch of the device, in degrees.
*
* @code
* accelerometer.getPitch();
* @endcode
*/
int getPitch();

/**
* Provides a rotation compensated pitch of the device, based on the latest update retrieved from the accelerometer.
*
* @return The pitch of the device, in radians.
*
* @code
* accelerometer.getPitchRadians();
* @endcode
*/
float getPitchRadians();

/**
* Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.
*
* @return The roll of the device, in degrees.
*
* @code
* accelerometer.getRoll();
* @endcode
*/
int getRoll();

/**
* Provides a rotation compensated roll of the device, based on the latest update retrieved from the accelerometer.
*
* @return The roll of the device, in radians.
*
* @code
* accelerometer.getRollRadians();
* @endcode
*/
float getRollRadians();

/**
* Retrieves the last recorded gesture.
*
* @return The last gesture that was detected.
*
* Example:
* @code
*
* if (accelerometer.getGesture() == SHAKE)
* display.scroll("SHAKE!");
* @endcode
*/
uint16_t getGesture();
virtual int requestUpdate() override;

/**
* Destructor.
*/
~MicroBitAccelerometer();
};
};
}

#endif
Loading
Loading