@@ -70,41 +70,118 @@ int scanWifiNetworks() {
7070 return networksCount ;
7171}
7272
73- String selectWifiNetwork (int numNetworks ) {
74- int selectedNetwork = 0 ;
73+ static void drawWifiList (int numNetworks , int topIndex , int selectedIndex , int x , int y0 , int visibleRows , int rowHeight )
74+ {
75+ bool showBar = (numNetworks > visibleRows );
76+ int screenWidth = M5Cardputer .Display .width ();
77+ int listHeight = visibleRows * rowHeight ;
78+ int vbarWidth = showBar ? 8 : 0 ;
79+ int vbarX = screenWidth - vbarWidth ;
80+ int listWidth = screenWidth - vbarWidth - 2 ;
7581
82+ // Clear everything below the header
83+ M5Cardputer .Display .fillRect (0 , y0 - 2 , screenWidth , listHeight + 4 , TFT_BLACK );
84+
85+ // Draw visible rows
86+ M5Cardputer .Display .setTextColor (TFT_LIGHTGRAY , TFT_DARKCYAN );
87+ for (int row = 0 ; row < visibleRows ; row ++ ) {
88+ int ssidIndex = topIndex + row ;
89+ if (ssidIndex >= numNetworks )
90+ break ;
91+
92+ int y = y0 + row * rowHeight ;
93+ String ssid = WiFi .SSID (ssidIndex );
94+ String line ;
95+ if (ssidIndex == selectedIndex )
96+ line = "-> " + ssid ;
97+ else
98+ line = " " + ssid ;
99+
100+ M5Cardputer .Display .drawString (line , x , y );
101+ }
102+
103+ if (!showBar )
104+ return ;
105+
106+ // Overwrite scrollbar on top of the text
107+ M5Cardputer .Display .fillRect (vbarX , y0 - 2 , vbarWidth , listHeight + 4 , TFT_BLACK );
108+ if (numNetworks > visibleRows ) {
109+ int trackX = vbarX + 1 ;
110+ int trackY = y0 ;
111+ int trackWidth = vbarWidth - 2 ;
112+ int trackHeight = listHeight ;
113+
114+ // Scrollbar background
115+ M5Cardputer .Display .fillRect (trackX , trackY , trackWidth , trackHeight , TFT_DARKGREY );
116+ M5Cardputer .Display .drawRect (trackX , trackY , trackWidth , trackHeight , 0x05A3 );
117+
118+ // Scrollbar thumb
119+ int minThumb = 12 ;
120+ int thumbHeigth = (trackHeight * visibleRows ) / numNetworks ;
121+ if (thumbHeigth < minThumb ) thumbHeigth = minThumb ;
122+
123+ // Thumb position maps scroll 'top' into [0 .. trackH - thumbH]
124+ int scrollRange = numNetworks - visibleRows ;
125+ int thumbY = trackY ;
126+ if (scrollRange > 0 )
127+ thumbY = trackY + ((trackHeight - thumbHeigth ) * topIndex ) / scrollRange ;
128+
129+ // Draw scrollbar thumb
130+ M5Cardputer .Display .fillRect (trackX + 1 , thumbY + 1 , trackWidth - 2 , thumbHeigth - 2 , 0x05A3 );
131+ }
132+ }
133+
134+ String selectWifiNetwork (int numNetworks )
135+ {
136+ int visibleRows = 6 ;
137+ int rowHeight = 18 ;
138+ int X = 1 ;
139+ int Y0 = 18 ;
140+ int selectedIndex = 0 ; // Row which is currently selected
141+ int topIndex = 0 ; // The first row on the list
142+ // The visible window is comprised of:
143+ // ===HEADER===
144+ // topIndex |S|
145+ // indexItem1 |C|
146+ // indexItem2 |R|
147+ // indexItem3 |O|
148+ // indexItem4 |L|
149+ // topIndex+visibleRows - 1 |L|
150+
151+ // Select network header
76152 M5Cardputer .Display .fillScreen (TFT_BLACK );
77- M5Cardputer .Display .setTextColor (TFT_DARKCYAN );
153+ M5Cardputer .Display .setTextColor (0x05A3 , TFT_DARKCYAN );
78154 M5Cardputer .Display .setTextSize (1.6 );
79155 M5Cardputer .Display .drawString ("Select Network" , 1 , 1 );
80- M5Cardputer .Display .setTextColor (TFT_LIGHTGRAY );
156+ M5Cardputer .Display .setTextColor (TFT_LIGHTGRAY , TFT_BLACK );
81157
82- while (1 ) {
83- for (int i = 0 ; i < 7 && i < numNetworks ; ++ i ) {
84- String ssid = WiFi .SSID (i );
85- if (i == selectedNetwork ) {
86- M5Cardputer .Display .drawString ("-> " + ssid , 1 , 18 + i * 18 );
87- } else {
88- M5Cardputer .Display .drawString (" " + ssid , 1 , 18 + i * 18 );
89- }
90- }
158+ drawWifiList (numNetworks , topIndex , selectedIndex , X , Y0 , visibleRows , rowHeight );
91159
160+ while (true) {
92161 M5Cardputer .update ();
93- if (M5Cardputer .Keyboard .isChange ()) {
94- if (M5Cardputer .Keyboard .isPressed ()) {
95- M5Cardputer .Speaker .tone (1437 , 20 );
96- M5Cardputer .Display .fillRect (0 , 11 , 20 , 75 , BLACK );
97- Keyboard_Class ::KeysState status = M5Cardputer .Keyboard .keysState ();
162+ if (M5Cardputer .Keyboard .isChange () && M5Cardputer .Keyboard .isPressed ()) {
163+ auto inputKey = M5Cardputer .Keyboard .keysState ();
98164
99- if (M5Cardputer .Keyboard .isKeyPressed (';' ) && selectedNetwork > 0 ) {
100- selectedNetwork -- ;
101- }
102- if (M5Cardputer .Keyboard .isKeyPressed ('.' ) && selectedNetwork < min (4 , numNetworks - 1 )) {
103- selectedNetwork ++ ;
104- }
105- if (status .enter ) {
106- return WiFi .SSID (selectedNetwork );
107- }
165+ // Up (';')
166+ if (M5Cardputer .Keyboard .isKeyPressed (';' ) && selectedIndex > 0 ) {
167+ selectedIndex -- ;
168+ // Means that when I press 'up' the top item isn't the top item anymore, it's second to selectedIndex, so replace it
169+ if (selectedIndex < topIndex )
170+ topIndex = selectedIndex ;
171+ drawWifiList (numNetworks , topIndex , selectedIndex , X , Y0 , visibleRows , rowHeight );
172+ }
173+
174+ // Down ('.')
175+ if (M5Cardputer .Keyboard .isKeyPressed ('.' ) && selectedIndex < (numNetworks - 1 )) {
176+ selectedIndex ++ ;
177+ // When you press 'down' the top item is not seen anymore, so it needs to be updated
178+ if (selectedIndex >= topIndex + visibleRows )
179+ topIndex = selectedIndex - visibleRows + 1 ;
180+ drawWifiList (numNetworks , topIndex , selectedIndex , X , Y0 , visibleRows , rowHeight );
181+ }
182+
183+ if (inputKey .enter ) {
184+ return WiFi .SSID (selectedIndex );
108185 }
109186 }
110187 delay (20 );
0 commit comments