11/* *
2- * An example sketch for the Arduino Uno R4 Wifi board that includes support for a TFT screen and keypad input. It
2+ * An example sketch for the Arduino Uno R4 Wifi board that includes support for a TFT screen and keypad input. It
33 * also has remote connectivity in the form of the inbuilt Wi-Fi module. The sketch controls the LED Matrix on the R4
44 * WiFi version somewhat akin to a disco.
55 *
1010 * and for the keypad
1111 * UP - 5, DOWN - 4, LEFT - 6, RIGHT - 7
1212 */
13- #include " r4UnoButtonsTft_menu.h"
13+
14+ #include " UnoR4Disco_menu.h"
1415#include " xbmpImages.h"
1516#include " IoAbstraction.h"
1617#include < graphics/TcThemeBuilder.h>
1718#include < stockIcons/wifiAndConnectionIcons16x12.h>
18- #include < tcMenuVersion.h>
1919#include < WiFi.h>
2020#include " DiscoTime.h"
2121
@@ -26,7 +26,7 @@ TitleWidget connectedWidget(iconsConnection, 2, 16, 12);
2626
2727DiscoTime discoTime;
2828
29- // At the moment the Wi-Fi on this board has an issue with TagVal .
29+ // At the moment the Wi-Fi on this board has a latency issue with non-blocking IO .
3030// We do not recommend using the Wi-Fi yet.
3131#define R4_START_WIFI false
3232
@@ -46,36 +46,63 @@ void tryAndStartWifi() {
4646#endif
4747}
4848
49+
50+ void buildMenu (TcMenuBuilder& builder) {
51+ builder .subMenu (MENU_DISCO_ID , " Disco" , NoMenuFlags, nullptr )
52+ .analogBuilder (MENU_DISCO_SPEED_ID , " Speed" , DONT_SAVE , NoMenuFlags, 2 , nullptr )
53+ .offset (0 ).divisor (1 ).step (1 ).maxValue (10 ).unit (" " ).endItem ()
54+ .actionItem (MENU_ZOOM_TEXT_START_ZOOM_ID , " Start Disco" , NoMenuFlags, onStartDisco)
55+ .endSub ()
56+ .subMenu (MENU_SCROLL_TEXT_ID , " Scroll Text" , NoMenuFlags, nullptr )
57+ .textItem (MENU_SCROLL_TEXT_TEXT_ID , " Text" , DONT_SAVE , 16 , NoMenuFlags, " TcMenu" , nullptr )
58+ .actionItem (MENU_SCROLL_TEXT_START_SCROLL_ID , " Start Scroll" , NoMenuFlags, onStartScroll)
59+ .endSub ()
60+ .subMenu (MENU_SHOW_XBMP_ID , " Show Xbmp" , NoMenuFlags, nullptr )
61+ .scrollChoiceBuilder (MENU_SHOW_XBMP_XBMP_ID , " Xbmp" , DONT_SAVE , NoMenuFlags, 0 , nullptr ).ofCustomRtFunction (fnShowXbmpXbmpRtCall, 0 ).endItem ()
62+ .actionItem (MENU_SHOW_XBMP_SHOW_IMAGE_ID , " Show Image" , NoMenuFlags, onShowXbmp)
63+ .endSub ()
64+ .subMenu (MENU_ANALOG_ID , " Analog" , NoMenuFlags, nullptr )
65+ .analogBuilder (MENU_ANALOG_A0_D_A_C_ID , " A0 DAC" , DONT_SAVE , NoMenuFlags, 0 , onAnalogDacChange)
66+ .offset (0 ).divisor (1 ).step (1 ).maxValue (100 ).unit (" %" ).endItem ()
67+ .floatItem (MENU_ANALOG_A1_VALUE_ID , " A1 Value" , DONT_SAVE , 1 , NoMenuFlags, 0.0 , nullptr )
68+ .endSub ()
69+ .subMenu (MENU_WI_FI_ID , " WiFi" , NoMenuFlags, nullptr )
70+ .ipAddressItem (MENU_WI_FI_I_P_ADDRESS_ID , " IP Address" , DONT_SAVE , NoMenuFlags, IpAddressStorage (127 , 0 , 0 , 1 ), nullptr )
71+ .boolItem (MENU_WI_FI_CONNECTED_ID , " Connected" , DONT_SAVE , NAMING_YES_NO , NoMenuFlags, false , nullptr )
72+ .endSub ();
73+ }
74+
75+
76+
4977void setup () {
78+ // start the serial port so that we can log
5079 Serial.begin (115200 );
5180
81+ // set up the DAC and analog input.
5282 internalAnalogDevice ().initPin (DAC , DIR_OUT );
5383 internalAnalogDevice ().initPin (A1 , DIR_IN );
5484 internalAnalogDevice ().setCurrentValue (DAC , 0 );
5585
5686 setupMenu ();
5787
58- menuShowXbmpXbmp .setNumberOfRows (2 );
88+ getMenuXbmp () .setNumberOfRows (2 );
5989
60- taskManager.schedule (repeatMillis ( 1500 ), [] {
61- menuAnalogA1Value .setFloatValue (internalAnalogDevice ().getCurrentFloat (A1 ) * analogReference ());
90+ taskManager.schedule (repeatSeconds ( 1 ), [] {
91+ getMenuA1Value () .setFloatValue (internalAnalogDevice ().getCurrentFloat (A1 ) * analogReference ());
6292 if (!wifiFailed) {
6393 wifiWidget.setCurrentState (fromWiFiRSSITo4StateIndicator (WiFi.RSSI ()));
6494 }
6595 });
6696
97+ // here we set up the Wi-Fi and prepare a title widget that represents the Wi-Fi status.
6798 if (WiFi.status () == WL_NO_MODULE ) {
6899 serlogF (SER_ERROR , " WiFi module failure" );
69100 wifiFailed = true ;
70101 wifiWidget.setCurrentState (0 );
71- menuWiFiConnected .setBoolean (false );
102+ getMenuConnected () .setBoolean (false );
72103 }
73104
74105 if (!wifiFailed) {
75- String fv = WiFi.firmwareVersion ();
76- if (fv < WIFI_FIRMWARE_LATEST_VERSION ) {
77- serlogF (SER_WARNING , " Please upgrade the firmware" );
78- }
79106 taskManager.schedule (onceMillis (100 ), tryAndStartWifi);
80107 }
81108
@@ -96,42 +123,49 @@ void setup() {
96123
97124void loop () {
98125 taskManager.runLoop ();
126+
99127}
100128
101- // In this case we show the image descriptions for the Xbitmap menu. We only need override the value callback. For each
102- // time the scroll choice needs the value for an index, it calls back here.
103- //
104129// This callback needs to be implemented by you, see the below docs:
105130// 1. List Docs - https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/menu-item-types/list-menu-item/
106131// 2. ScrollChoice Docs - https://www.thecoderscorner.com/products/arduino-libraries/tc-menu/menu-item-types/scrollchoice-menu-item/
107132int CALLBACK_FUNCTION fnShowXbmpXbmpRtCall (RuntimeMenuItem* item, uint8_t row, RenderFnMode mode, char * buffer, int bufferSize) {
108133 switch (mode) {
109- case RENDERFN_VALUE :
110- strncpy (buffer, imageWithDescription[row].getName (), bufferSize);
111- return true ;
112- default :
113- return defaultRtListCallback (item, row, mode, buffer, bufferSize);
134+ case RENDERFN_VALUE :
135+ strncpy (buffer, imageWithDescription[row].getName (), bufferSize);
136+ return true ;
137+ default :
138+ return defaultRtListCallback (item, row, mode, buffer, bufferSize);
114139 }
115140}
116141
117- void CALLBACK_FUNCTION onAnalogDacChange (int id) {
118- internalAnalogDevice ().setCurrentFloat (DAC , menuAnalogA0DAC.getCurrentValue () / 100 .0F );
142+
143+ void CALLBACK_FUNCTION onStartDisco (int id) {
144+ // called when the start disco action is clicked. We start the disco!
145+ discoTime.start (getMenuSpeed ().getCurrentValue () * 20 );
119146}
120147
148+
121149void CALLBACK_FUNCTION onShowXbmp (int id) {
150+ // called whenever the start xbitmap is called, it shows how to convert
151+ // an XBitmap onto the LEDs.
122152 GFXcanvas1 canvas (12 , 8 );
123- auto imgIdx = menuShowXbmpXbmp .getCurrentValue ();
153+ auto imgIdx = getMenuXbmp () .getCurrentValue ();
124154 if (imgIdx < 0 || imgIdx >= NUMBER_OF_XBMPS ) return ;
125155
126156 discoTime.picture (imageWithDescription[imgIdx].getData ());
127157}
128158
129- void CALLBACK_FUNCTION onStartDisco (int id) {
130- discoTime.start (menuDiscoSpeed.getCurrentValue () * 20 );
159+
160+ void CALLBACK_FUNCTION onAnalogDacChange (int id) {
161+ // here we take the current value of the DAC menu item and put it onto the dac.
162+ internalAnalogDevice ().setCurrentFloat (DAC , getMenuA0DAC ().getCurrentValue () / 100 .0F );
131163}
132164
165+
133166void CALLBACK_FUNCTION onStartScroll (int id) {
167+ // here we start scrolling through text a word at a time when the start scroll item is clicked
134168 char sz[20 ];
135- menuScrollTextText .copyValue (sz, sizeof sz);
169+ getMenuScrollText () .copyValue (sz, sizeof sz);
136170 discoTime.text (sz);
137171}
0 commit comments