Krolyxon
Commit 360d2d2

Branches

Tags

Refs →

orion-rffirmwarewifi_scan.cpp

152 lines · 3.1 KB · C++

1#include <Arduino.h>
2#include <WiFi.h>
3#include "display.h"
4#include "buttons.h"
5
6#define MAX_NETWORKS 30
7
8struct WiFiNet {
9 String ssid;
10 int rssi;
11 int channel;
12 bool encrypted;
13};
14
15static WiFiNet networks[MAX_NETWORKS];
16static int networkCount = 0;
17static int selectedIndex = 0;
18
19// ===== SCAN =====
20void wifi_scan_start()
21{
22 u8g2.clearBuffer();
23 u8g2.drawStr(10, 30, "Scanning WiFi...");
24 u8g2.sendBuffer();
25
26 WiFi.mode(WIFI_STA);
27 WiFi.disconnect();
28
29 delay(100);
30
31 int n = WiFi.scanNetworks();
32
33 networkCount = min(n, MAX_NETWORKS);
34
35 for (int i = 0; i < networkCount; i++)
36 {
37 networks[i].ssid = WiFi.SSID(i);
38 networks[i].rssi = WiFi.RSSI(i);
39 networks[i].channel = WiFi.channel(i);
40 networks[i].encrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN);
41 }
42
43 selectedIndex = 0;
44}
45
46// ===== DRAW =====
47void wifi_scan_draw()
48{
49 u8g2.clearBuffer();
50
51 if (networkCount == 0)
52 {
53 u8g2.drawStr(0, 30, "No networks");
54 u8g2.drawStr(0, 45, "Press BACK");
55 }
56 else
57 {
58 char counter[20];
59 sprintf(counter, "%d/%d", selectedIndex + 1, networkCount);
60 u8g2.setFont(u8g2_font_5x8_tr);
61 u8g2.drawStr(0, 8, counter);
62
63 u8g2.setFont(u8g2_font_6x10_tr);
64
65 for (int i = 0; i < 3; i++)
66 {
67 int idx = selectedIndex + i;
68 if (idx >= networkCount) break;
69
70 int y = 22 + i * 14;
71
72 if (i == 0)
73 {
74 u8g2.drawBox(0, y - 10, 128, 12);
75 u8g2.setDrawColor(0);
76 }
77
78 String text = networks[idx].ssid;
79 if (text.length() > 10)
80 text = text.substring(0, 10) + "..";
81
82 text += " (" + String(networks[idx].rssi) + ")";
83
84 u8g2.drawStr(2, y, text.c_str());
85
86 if (i == 0)
87 u8g2.setDrawColor(1);
88 }
89 }
90
91 u8g2.sendBuffer();
92}
93
94// ===== DETAILS =====
95void wifi_drawDetails()
96{
97 if (networkCount == 0) return;
98
99 WiFiNet &net = networks[selectedIndex];
100
101 u8g2.clearBuffer();
102 u8g2.setFont(u8g2_font_5x8_tr);
103
104 u8g2.drawStr(0, 10, net.ssid.c_str());
105
106 char rssi[20];
107 sprintf(rssi, "RSSI: %d", net.rssi);
108 u8g2.drawStr(0, 20, rssi);
109
110 char ch[20];
111 sprintf(ch, "CH: %d", net.channel);
112 u8g2.drawStr(0, 30, ch);
113
114 u8g2.drawStr(0, 40, net.encrypted ? "Secured" : "Open");
115
116 u8g2.sendBuffer();
117}
118
119// ===== LOOP =====
120void wifi_scan_loop()
121{
122 static uint32_t lastPress = 0;
123
124 if (millis() - lastPress < 150)
125 return;
126
127 if (btnDown() && selectedIndex < networkCount - 1)
128 {
129 selectedIndex++;
130 wifi_scan_draw();
131 lastPress = millis();
132 }
133 else if (btnUp() && selectedIndex > 0)
134 {
135 selectedIndex--;
136 wifi_scan_draw();
137 lastPress = millis();
138 }
139 else if (btnSelect() && networkCount > 0)
140 {
141 wifi_drawDetails();
142 delay(3000);
143 wifi_scan_draw();
144 lastPress = millis();
145 }
146// else if (btnBack())
147// {
148// wifi_scan_start();
149// wifi_scan_draw();
150// lastPress = millis();
151// }
152}