Skip to content

Commit e301dc0

Browse files
committed
Major example improvement for Nano33
1 parent 326ea48 commit e301dc0

9 files changed

Lines changed: 212 additions & 173 deletions

File tree

examples/arduino32/nano33ble/MotionDetection.h renamed to examples/arduino32/Nano33BLEOLED/MotionDetection.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <TaskManager.h>
55
#include <Arduino_LSM9DS1.h>
6-
#include "nano33ble_menu.h"
6+
#include "Nano33BLEOLED_menu.h"
77

88
/**
99
* Here we create a polling event that checks if the acceleration / magnetic data is available, and whenever it is
@@ -31,14 +31,14 @@ class MotionDetection : public BaseEvent {
3131
void exec() override {
3232
float x, y, z;
3333
IMU.readMagneticField(x, y, z);
34-
menuAccelerometerMagX.setFloatValue(x);
35-
menuAccelerometerMagY.setFloatValue(y);
36-
menuAccelerometerMagZ.setFloatValue(z);
34+
getMenuMagX().setFloatValue(x);
35+
getMenuMagY().setFloatValue(y);
36+
getMenuMagZ().setFloatValue(z);
3737

3838
IMU.readAcceleration(x, y, z);
39-
menuAccelerometerAccelX.setFloatValue(x);
40-
menuAccelerometerAccelY.setFloatValue(y);
41-
menuAccelerometerAccelZ.setFloatValue(z);
39+
getMenuAccelX().setFloatValue(x);
40+
getMenuAccelY().setFloatValue(y);
41+
getMenuAccelZ().setFloatValue(z);
4242
}
4343
};
4444

examples/arduino32/nano33ble/nano33ble.emf renamed to examples/arduino32/Nano33BLEOLED/Nano33BLEOLED.emf

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"unitName": "C",
1515
"step": 1,
1616
"name": "Temp",
17+
"variableName": "",
1718
"id": 1,
1819
"eepromAddress": -1,
1920
"functionName": "",
@@ -34,6 +35,7 @@
3435
"unitName": "%",
3536
"step": 1,
3637
"name": "Humidity",
38+
"variableName": "",
3739
"id": 3,
3840
"eepromAddress": -1,
3941
"functionName": "",
@@ -54,6 +56,7 @@
5456
"unitName": "KPa",
5557
"step": 1,
5658
"name": "B. Pressure",
59+
"variableName": "",
5760
"id": 2,
5861
"eepromAddress": -1,
5962
"functionName": "",
@@ -69,6 +72,7 @@
6972
"item": {
7073
"secured": false,
7174
"name": "Accelerometer",
75+
"variableName": "",
7276
"id": 4,
7377
"eepromAddress": -1,
7478
"functionName": "",
@@ -83,10 +87,12 @@
8387
"type": "floatItem",
8488
"defaultValue": "0.0",
8589
"item": {
86-
"numDecimalPlaces": 2,
90+
"numDecimalPlaces": 1,
8791
"name": "MagX",
92+
"variableName": "",
8893
"id": 5,
8994
"eepromAddress": -1,
95+
"functionName": "",
9096
"readOnly": false,
9197
"localOnly": false,
9298
"visible": true,
@@ -98,10 +104,12 @@
98104
"type": "floatItem",
99105
"defaultValue": "0.0",
100106
"item": {
101-
"numDecimalPlaces": 2,
107+
"numDecimalPlaces": 1,
102108
"name": "MagY",
109+
"variableName": "",
103110
"id": 6,
104111
"eepromAddress": -1,
112+
"functionName": "",
105113
"readOnly": false,
106114
"localOnly": false,
107115
"visible": true,
@@ -113,10 +121,12 @@
113121
"type": "floatItem",
114122
"defaultValue": "0.0",
115123
"item": {
116-
"numDecimalPlaces": 2,
124+
"numDecimalPlaces": 1,
117125
"name": "MagZ",
126+
"variableName": "",
118127
"id": 7,
119128
"eepromAddress": -1,
129+
"functionName": "",
120130
"readOnly": false,
121131
"localOnly": false,
122132
"visible": true,
@@ -128,10 +138,12 @@
128138
"type": "floatItem",
129139
"defaultValue": "0.0",
130140
"item": {
131-
"numDecimalPlaces": 2,
141+
"numDecimalPlaces": 1,
132142
"name": "AccelX",
143+
"variableName": "",
133144
"id": 8,
134145
"eepromAddress": -1,
146+
"functionName": "",
135147
"readOnly": false,
136148
"localOnly": false,
137149
"visible": true,
@@ -143,10 +155,12 @@
143155
"type": "floatItem",
144156
"defaultValue": "0.0",
145157
"item": {
146-
"numDecimalPlaces": 2,
158+
"numDecimalPlaces": 1,
147159
"name": "AccelY",
160+
"variableName": "",
148161
"id": 9,
149162
"eepromAddress": -1,
163+
"functionName": "",
150164
"readOnly": false,
151165
"localOnly": false,
152166
"visible": true,
@@ -158,8 +172,9 @@
158172
"type": "floatItem",
159173
"defaultValue": "0.0",
160174
"item": {
161-
"numDecimalPlaces": 2,
175+
"numDecimalPlaces": 1,
162176
"name": "AccelZ",
177+
"variableName": "",
163178
"id": 10,
164179
"eepromAddress": -1,
165180
"functionName": "",
@@ -175,8 +190,10 @@
175190
"item": {
176191
"secured": false,
177192
"name": "Analog Readings",
193+
"variableName": "",
178194
"id": 11,
179195
"eepromAddress": -1,
196+
"functionName": "",
180197
"readOnly": false,
181198
"localOnly": false,
182199
"visible": true,
@@ -188,8 +205,9 @@
188205
"type": "floatItem",
189206
"defaultValue": "0.0",
190207
"item": {
191-
"numDecimalPlaces": 2,
208+
"numDecimalPlaces": 1,
192209
"name": "In A0",
210+
"variableName": "",
193211
"id": 12,
194212
"eepromAddress": -1,
195213
"functionName": "",
@@ -210,6 +228,7 @@
210228
"unitName": "%",
211229
"step": 1,
212230
"name": "Output PWM",
231+
"variableName": "",
213232
"id": 13,
214233
"eepromAddress": -1,
215234
"functionName": "onPWMChanged",
@@ -229,7 +248,7 @@
229248
],
230249
"lastThemeUuid": "396ED4DF-AD7B-4951-A848-A9E5838A549B",
231250
"applicationUUID": "e95fcf8a-8a03-4043-9313-01fd8b8e8707",
232-
"applicationName": "Nano 33 BLE Sense",
251+
"applicationName": "Nano 33 BLE OLED",
233252
"lastProperties": [
234253
{
235254
"name": "DISPLAY_VARIABLE",
@@ -364,19 +383,19 @@
364383
],
365384
"namingRecursive": true,
366385
"useCppMain": false,
367-
"saveLocation": "ALL_TO_CURRENT",
386+
"saveLocation": "ONE_SINGLE_FILE_MENU_MAIN",
387+
"useDynamicMenus": true,
368388
"usingSizedEEPROMStorage": false,
389+
"eepromSaveMode": "DYNAMIC_WRITE_BY_ID",
369390
"eepromDefinition": "",
370391
"authenticatorDefinition": "",
371392
"projectIoExpanders": [
372-
"customIO:io23017",
373-
"deviceIO:"
393+
"deviceIO:",
394+
"customIO:io23017"
374395
],
375396
"menuInMenuCollection": {
376397
"menuDefinitions": []
377-
},
378-
"packageNamespace": "",
379-
"appIsModular": false
398+
}
380399
},
381400
"stringLists": []
382401
}
Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,82 @@
11
/**
2-
* @file nano33ble.ino
2+
* @file Nano33BLEOLED.ino
33
* An example of using tcMenu with the Nano33BLE Sense. This example uses some of the sensors on-board the device
44
* along with an I2C LCD backpack based display and a rotary encoder. Although this is pre-generated for you, you
55
* can load the example's emf file into TcMenu Designer and take a look around or re-build it.
66
*
77
* You can get or adjust the pin configurations by loading the emf file into the designer
8-
*
9-
* Getting started: https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/tcmenu-overview-quick-start/
8+
* Web designer: https://designer.thecoderscorner.com
109
*/
1110

12-
#include "nano33ble_menu.h"
11+
// include the menu project file, it contains all the glue code to get things working
12+
#include "Nano33BLEOLED_menu.h"
13+
// and this example uses the inbuilt sensors on the board, there are two classes that access them.
1314
#include "SensorManager.h"
1415
#include "MotionDetection.h"
15-
#include <AnalogDeviceAbstraction.h>
16-
#include <tcMenuVersion.h>
16+
// and the BLE library
1717
#include <ArduinoBLE.h>
18-
#include <stockIcons/wifiAndConnectionIcons16x12.h>
18+
// and stock icons that include things like Wi-Fi and connectivity icons.
19+
#include "stockIcons/wifiAndConnectionIcons16x12.h"
20+
21+
// on the analog menu, we both have an analog input and an analog output (PWM). Change as appropriate.
22+
constexpr int analogInputPin = A0;
23+
constexpr int pwmOutputPin = 2;
1924

20-
// on the analog menu, we both have an analog input and an analog output (PWM). You can configure those pins here.
21-
const int analogInputPin = A0;
22-
const int pwmOutputPin = 2;
25+
// Title widgets
26+
TitleWidget bleRssiWidget(iconsWifi, 5, 16, 12, nullptr);
2327

24-
// We create a class extending Executable for the temprature, humidity, and pressure sensors that are built in
28+
// We create a class extending Executable for the temperature, humidity, and pressure sensors that are built in
2529
SensorManager sensorManager;
2630

2731
// We create an event class extending BaseEvent to manage the motion detection
2832
MotionDetection motionDetection;
2933

30-
TitleWidget bleRssiWidget(iconsWifi, 5, 16, 12, nullptr);
34+
// This is the menu structure, you can adjust this as needed yourself, no need to round-trip to designer,
35+
// unless of course you prefer editing menus there.
36+
void buildMenu(TcMenuBuilder& builder) {
37+
builder.usingDynamicEEPROMStorage()
38+
.analogBuilder(MENU_TEMP_ID, "Temp", DONT_SAVE, MenuFlags().readOnly(), 0, nullptr)
39+
.offset(0).divisor(10).step(1).maxValue(2000).unit("C").endItem()
40+
.analogBuilder(MENU_HUMIDITY_ID, "Humidity", DONT_SAVE, MenuFlags().readOnly(), 0, nullptr)
41+
.offset(0).divisor(10).step(1).maxValue(1000).unit("%").endItem()
42+
.analogBuilder(MENU_B_PRESSURE_ID, "B. Pressure", DONT_SAVE, MenuFlags().readOnly(), 0, nullptr)
43+
.offset(0).divisor(10).step(1).maxValue(32000).unit("KPa").endItem()
44+
.subMenu(MENU_ACCELEROMETER_ID, "Accelerometer", NoMenuFlags, nullptr)
45+
.floatItem(MENU_MAG_X_ID, "MagX", DONT_SAVE, 1, NoMenuFlags, 0.0, nullptr)
46+
.floatItem(MENU_MAG_Y_ID, "MagY", DONT_SAVE, 1, NoMenuFlags, 0.0, nullptr)
47+
.floatItem(MENU_MAG_Z_ID, "MagZ", DONT_SAVE, 1, NoMenuFlags, 0.0, nullptr)
48+
.floatItem(MENU_ACCEL_X_ID, "AccelX", DONT_SAVE, 1, NoMenuFlags, 0.0, nullptr)
49+
.floatItem(MENU_ACCEL_Y_ID, "AccelY", DONT_SAVE, 1, NoMenuFlags, 0.0, nullptr)
50+
.floatItem(MENU_ACCEL_Z_ID, "AccelZ", DONT_SAVE, 1, NoMenuFlags, 0.0, nullptr)
51+
.endSub()
52+
.subMenu(MENU_ANALOG_READINGS_ID, "Analog Readings", NoMenuFlags, nullptr)
53+
.floatItem(MENU_IN_A0_ID, "In A0", DONT_SAVE, 1, NoMenuFlags, 0.0, nullptr)
54+
.analogBuilder(MENU_OUTPUT_P_W_M_ID, "Output PWM", DONT_SAVE, NoMenuFlags, 0, onPWMChanged)
55+
.offset(0).divisor(0).step(1).maxValue(100).unit("%").endItem()
56+
.endSub();
57+
}
3158

3259
void setup() {
3360
// start up serial and wait for it to actually begin, needed on this board.
3461
Serial.begin(115200);
35-
while(!Serial);
36-
37-
serEnableLevel(SER_NETWORK_DEBUG, true);
62+
while(!Serial && millis() < 10000) {}
3863

64+
// start up wire as well.
3965
Wire.begin();
4066
Wire.setClock(400000);
4167

42-
// First we set up the analog pins
68+
setupMenu();
69+
70+
// First we set up the analog pins, we configure a PWM output pin onto which should be connected
71+
// an LED, and we take readings from the analogInputPin. These are configured further up.
4372
internalAnalogDevice().initPin(pwmOutputPin, DIR_OUT);
4473
internalAnalogDevice().initPin(analogInputPin, DIR_IN);
4574

4675
// Here we tell the encoder not to wrap (we don't technically need to do this as false is the default.
4776
// Wrap means go from maxValue back to 0, or from 0 back to maxValue. On is true, Off (default) is false.
4877
menuMgr.setUseWrapAroundEncoder(false);
4978
// We can also define overrides for a particular menu item
50-
menuMgr.addEncoderWrapOverride(menuAnalogReadingsOutputPWM, true);
79+
menuMgr.addEncoderWrapOverride(getMenuOutputPWM(), true);
5180

5281
// add a title widget that represents the ble signal strength / connection
5382
// and create a task that updates its status each second.
@@ -60,9 +89,6 @@ void setup() {
6089
else bleRssiWidget.setCurrentState(4);
6190
});
6291

63-
// and set up the menu itself, so it starts displaying and accepting input
64-
setupMenu();
65-
6692
// then we initialise our sensor and motion detection and register with task manager.
6793
sensorManager.initialise();
6894
motionDetection.initialise();
@@ -80,18 +106,19 @@ void setup() {
80106

81107
// lastly we set up something simple to read from analog in
82108
taskManager.scheduleFixedRate(100, [] {
83-
menuAnalogReadingsInA0.setFloatValue(internalAnalogDevice().getCurrentFloat(analogInputPin));
109+
getMenuInA0().setFloatValue(internalAnalogDevice().getCurrentFloat(analogInputPin));
84110
});
85111
}
86112

87-
// All TaskManager sketches must call runLoop very often from the loop method, you should not use any delays.
88113
void loop() {
89114
taskManager.runLoop();
115+
90116
}
91117

118+
92119
void CALLBACK_FUNCTION onPWMChanged(int id) {
93120
// here we are notified of changes in the PWM menu item and we convert that change to a value between 0 and 1.
94-
auto newPwm = menuAnalogReadingsOutputPWM.getCurrentValue() / 100.0F;
121+
auto newPwm = getMenuOutputPWM().getCurrentValue() / 100.0F;
95122
// then we can apply that to the output pin, analogDevice does all the conversion work for us.
96123
internalAnalogDevice().setCurrentFloat(pwmOutputPin, newPwm);
97124
}

0 commit comments

Comments
 (0)