Skip to content

Commit

Permalink
Merge pull request #166 from and3rson/wifiSetTxPowerMenu
Browse files Browse the repository at this point in the history
WiFi tx power change option in settings, autoload from nvs
  • Loading branch information
black-ghost-off authored Dec 28, 2024
2 parents 78efd09 + f36c7f8 commit b07727b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
51 changes: 51 additions & 0 deletions firmware/keira/src/apps/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
#include "icons/music.h"
#include "fmanager.h"

#include <WiFi.h> // for setWiFiTxPower
#include <Preferences.h>

LauncherApp::LauncherApp() : App("Menu") {
networkService = static_cast<NetworkService*>(ServiceManager::getInstance()->getService<NetworkService>("network"));
}
Expand Down Expand Up @@ -133,6 +136,7 @@ void LauncherApp::run() {
}
),
ITEM::MENU("Мережі WiFi", [this]() { this->wifiManager(); }),
ITEM::MENU("Потужність WiFi", [this]() { this->setWiFiTxPower(); }),
ITEM::MENU("Звук", [this]() { this->runApp<SoundConfigApp>(); }),
ITEM::MENU("Про систему", [this]() { this->about(); }),
ITEM::MENU("Інфо про пристрій", [this]() { this->info(); }),
Expand Down Expand Up @@ -211,7 +215,54 @@ template <typename T, typename... Args>
void LauncherApp::runApp(Args&&... args) {
AppManager::getInstance()->runApp(new T(std::forward<Args>(args)...));
}
void LauncherApp::setWiFiTxPower() {
// String names[16];
// int partitionCount = lilka::sys.get_partition_labels(names);

String names[] = {
"19.5 dBm", "19 dBm", "18.5 dBm", "17 dBm", "15 dBm", "13 dBm", "11 dBm", "8.5 dBm", "7 dBm", "2 dBm", "-1 dBm"
};
wifi_power_t values[] = {
WIFI_POWER_19_5dBm,
WIFI_POWER_19dBm,
WIFI_POWER_18_5dBm,
WIFI_POWER_17dBm,
WIFI_POWER_15dBm,
WIFI_POWER_13dBm,
WIFI_POWER_11dBm,
WIFI_POWER_8_5dBm,
WIFI_POWER_7dBm,
WIFI_POWER_5dBm,
WIFI_POWER_2dBm,
WIFI_POWER_MINUS_1dBm
};
lilka::Menu wifiSetTxMenu;
wifiSetTxMenu.setTitle("Оберіть потужність");
wifiSetTxMenu.addActivationButton(lilka::Button::B); // Exit
// Add names
for (auto i = 0; i < sizeof(names) / sizeof(names[0]); i++)
wifiSetTxMenu.addItem(names[i]);
// Perform draw
while (!wifiSetTxMenu.isFinished()) {
wifiSetTxMenu.update();
wifiSetTxMenu.draw(canvas);
queueDraw();
}
auto button = wifiSetTxMenu.getButton();

if (button == lilka::Button::B) return;

auto index = wifiSetTxMenu.getCursor();

// Set power immediately
WiFi.setTxPower(values[index]);

// Save value to NVS
Preferences prefs;
prefs.begin(WIFI_KEIRA_NAMESPACE, false);
prefs.putInt("txPower", static_cast<int>(values[index]));
prefs.end();
}
void LauncherApp::wifiToggle() {
networkService->setEnabled(!networkService->getEnabled());
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/keira/src/apps/launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class LauncherApp : public App {
void alert(String title, String message);
template <typename T, typename... Args>
void runApp(Args&&... args);

void setWiFiTxPower();
void wifiToggle();
void wifiManager();
void about();
Expand Down
15 changes: 9 additions & 6 deletions firmware/keira/src/services/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ NetworkService::NetworkService() :

void NetworkService::run() {
Preferences prefs;
prefs.begin("keira", true);
prefs.begin(WIFI_KEIRA_NAMESPACE, true);
bool enabled = prefs.isKey("enabled") ? prefs.getBool("enabled") : false;
wifi_power_t txPower =
prefs.isKey("txPower") ? static_cast<wifi_power_t>(prefs.getInt("txPower")) : WIFI_POWER_19_5dBm;
prefs.end();

WiFi.setTxPower(txPower);
WiFi.onEvent([this](WiFiEvent_t event, WiFiEventInfo_t info) {
switch (event) {
case ARDUINO_EVENT_WIFI_STA_START: {
Expand All @@ -45,7 +48,7 @@ void NetworkService::run() {
setNetworkState(NETWORK_STATE_ONLINE);
Preferences prefs;
String connectedSSID = String(info.wifi_sta_connected.ssid, info.wifi_sta_connected.ssid_len);
prefs.begin("keira", false);
prefs.begin(WIFI_KEIRA_NAMESPACE, false);
if (!prefs.isKey("last_ssid") || !String(prefs.getString("last_ssid")).equals(connectedSSID)) {
// Set current SSID as last connected
prefs.putString("last_ssid", String(connectedSSID));
Expand All @@ -54,7 +57,7 @@ void NetworkService::run() {
prefs.end();
String ssidHash = hash(connectedSSID);
String savedPassword = getPassword(connectedSSID);
prefs.begin("keira", false);
prefs.begin(WIFI_KEIRA_NAMESPACE, false);
if (savedPassword != lastPassword) {
// Save password for the connected network
prefs.putString(String(ssidHash + "_pw").c_str(), lastPassword);
Expand Down Expand Up @@ -155,7 +158,7 @@ void NetworkService::autoConnect() {

// Check if there is a known network to connect to
Preferences prefs;
prefs.begin("keira", true);
prefs.begin(WIFI_KEIRA_NAMESPACE, true);
if (!prefs.isKey("last_ssid")) {
lilka::serial_log("NetworkService: no last SSID found, skipping auto connection");
} else {
Expand Down Expand Up @@ -206,7 +209,7 @@ bool NetworkService::getEnabled() {

void NetworkService::setEnabled(bool enabled) {
Preferences prefs;
prefs.begin("keira", false);
prefs.begin(WIFI_KEIRA_NAMESPACE, false);
prefs.putBool("enabled", enabled);
prefs.end();

Expand All @@ -224,7 +227,7 @@ void NetworkService::setEnabled(bool enabled) {

String NetworkService::getPassword(String ssid) {
Preferences prefs;
prefs.begin("keira", true);
prefs.begin(WIFI_KEIRA_NAMESPACE, true);
String ssidHash = hash(ssid);
String result;
if (!prefs.isKey(String(ssidHash + "_pw").c_str())) {
Expand Down
2 changes: 2 additions & 0 deletions firmware/keira/src/services/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <WiFi.h>
#include "service.h"

#define WIFI_KEIRA_NAMESPACE "kwifi"

enum NetworkState {
NETWORK_STATE_DISABLED,
NETWORK_STATE_OFFLINE,
Expand Down

0 comments on commit b07727b

Please sign in to comment.