Krolyxon
Commit ed0847b

Branches

Tags

Refs →

orion-rfsrcwifiwifi_scan.cpp

139 lines · 3.1 KB · C++

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