Skip to content

Commit 436aaae

Browse files
author
dave
committed
Nano33BLE tidy up.
1 parent a24d63d commit 436aaae

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

examples/nano33ble/MotionDetection.h

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,40 @@
99
#include <Arduino_LSM9DS1.h>
1010
#include "nano33ble_menu.h"
1111

12-
class MotionDetection : public Executable {
12+
/**
13+
* Here we create a polling event that checks if the acceleration / magnetic data is available, and whenever it is
14+
* it triggers the event. This tidies up the class slightly by avoiding if statements in the exec() methods.
15+
* In short, the timeOfNextCheck method is called every `eventFrequency` micros, and we tell taskManager that the
16+
* event is triggered when both readings are available. At this point `exec()` is called and we take the readings
17+
* and put then into menu items.
18+
*/
19+
class MotionDetection : public BaseEvent {
20+
private:
21+
uint32_t eventFrequency = 1000UL;
1322
public:
1423
void initialise() {
1524
IMU.begin();
1625
auto imuRate = IMU.magneticFieldSampleRate();
17-
taskManager.scheduleFixedRate(max(int(imuRate), 100), this);
26+
eventFrequency = min(int(imuRate), 100) * 1000UL;
1827
}
1928

20-
void exec() {
21-
if(IMU.magneticFieldAvailable()) {
22-
float x, y, z;
23-
IMU.readMagneticField(x, y, z);
24-
menuAccelerometerMagX.setFloatValue(x);
25-
menuAccelerometerMagY.setFloatValue(y);
26-
menuAccelerometerMagZ.setFloatValue(z);
27-
}
28-
29-
if(IMU.accelerationAvailable()) {
30-
float x, y, z;
31-
IMU.readAcceleration(x, y, z);
32-
menuAccelerometerAccelX.setFloatValue(x);
33-
menuAccelerometerAccelY.setFloatValue(y);
34-
menuAccelerometerAccelZ.setFloatValue(z);
35-
}
29+
uint32_t timeOfNextCheck() override {
30+
setTriggered(IMU.magneticFieldAvailable() && IMU.accelerationAvailable());
31+
32+
return eventFrequency;
33+
}
34+
35+
void exec() override {
36+
float x, y, z;
37+
IMU.readMagneticField(x, y, z);
38+
menuAccelerometerMagX.setFloatValue(x);
39+
menuAccelerometerMagY.setFloatValue(y);
40+
menuAccelerometerMagZ.setFloatValue(z);
41+
42+
IMU.readAcceleration(x, y, z);
43+
menuAccelerometerAccelX.setFloatValue(x);
44+
menuAccelerometerAccelY.setFloatValue(y);
45+
menuAccelerometerAccelZ.setFloatValue(z);
3646
}
3747
};
3848

examples/nano33ble/SensorManager.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111
#include <MenuItems.h>
1212
#include "nano33ble_menu.h"
1313

14-
class SensorManager : Executable{
14+
/**
15+
* Here we have a class that extends `Executable`, meaning that the `exec()` method is called every time the event
16+
* is scheduled by task manager. We read the data from the sensors and set them onto menu items.
17+
*/
18+
class SensorManager : public Executable {
1519
private:
1620
bool initialised{};
1721
public:
1822
void initialise() {
1923
initialised = HTS.begin() != 0;
2024
initialised = initialised && BARO.begin();
21-
initialised = initialised && taskManager.scheduleFixedRate(1, this, TIME_SECONDS) != TASKMGR_INVALIDID;
2225
}
2326

24-
void exec() {
27+
void exec() override {
2528
menuTemp.setFromFloatingPointValue(HTS.readTemperature());
2629
menuHumidity.setFromFloatingPointValue(HTS.readHumidity());
2730
menuBPressure.setFromFloatingPointValue(BARO.readPressure());

examples/nano33ble/nano33ble.ino

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,57 @@
1+
/**
2+
* @file nano33ble.ino
3+
* An example of using tcMenu with the Nano33BLE Sense. This example uses some of the sensors on-board the device
4+
* along with an I2C LCD backpack based display and a rotary encoder. Although this is pre-generated for you, you
5+
* can load the example's emf file into TcMenu Designer and take a look around or re-build it.
6+
*
7+
* You can get or adjust the pin configurations by loading the emf file into the designer
8+
*/
9+
110
#include "nano33ble_menu.h"
211
#include "SensorManager.h"
312
#include "MotionDetection.h"
413
#include <AnalogDeviceAbstraction.h>
514

15+
// on the analog menu, we both have an analog input and an analog output (PWM). You can configure those pins here.
616
const int analogInputPin = A0;
717
const int pwmOutputPin = 2;
818

19+
// We work with analog input and output here, so we use an analog device to make it easier. It provides the ability
20+
// to treat analog values as floats between 0..1 on any supported platform.
21+
ArduinoAnalogDevice analogDevice;
22+
23+
// We create a class extending Executable for the temprature, humidity, and pressure sensors that are built in
924
SensorManager sensorManager;
25+
26+
// We create an event class extending BaseEvent to manage the motion detection
1027
MotionDetection motionDetection;
11-
ArduinoAnalogDevice analogDevice;
1228

1329
void setup() {
30+
// First we set up the analog pins
31+
analogDevice.initPin(pwmOutputPin, DIR_OUT);
32+
analogDevice.initPin(analogInputPin, DIR_IN);
33+
34+
// and set up the menu itself, so it starts displaying and accepting input
1435
setupMenu();
36+
37+
// then we initialise our sensor and motion detection and register with task manager.
1538
sensorManager.initialise();
1639
motionDetection.initialise();
17-
analogDevice.initPin(pwmOutputPin, DIR_OUT);
18-
analogDevice.initPin(analogInputPin, DIR_IN);
40+
taskManager.registerEvent(&motionDetection);
41+
taskManager.scheduleFixedRate(1, &sensorManager, TIME_SECONDS);
1942

43+
// lastly we set up something simple to read from analog in
2044
taskManager.scheduleFixedRate(100, [] {
2145
menuAnalogReadingsInA0.setFloatValue(analogDevice.getCurrentFloat(analogInputPin));
2246
});
2347
}
2448

49+
// All TaskManager sketches must call runLoop very often from the loop method, you should not use any delays.
2550
void loop() {
2651
taskManager.runLoop();
27-
2852
}
2953

54+
// And something to change the PWM output when the PWM menu item changes
3055
void CALLBACK_FUNCTION onPWMChanged(int id) {
3156
auto newPwm = menuAnalogReadingsOutputPWM.getCurrentValue() / 100.0F;
3257
analogDevice.setCurrentFloat(pwmOutputPin, newPwm);

0 commit comments

Comments
 (0)