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
2529SensorManager sensorManager;
2630
2731// We create an event class extending BaseEvent to manage the motion detection
2832MotionDetection 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
3259void 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.
88113void loop () {
89114 taskManager.runLoop ();
115+
90116}
91117
118+
92119void 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