Skip to content

Commit

Permalink
Removed wifi & ntp-time from hal; upgraded lua bindings accordingly a…
Browse files Browse the repository at this point in the history
…nd migrated all apps (more or less)
  • Loading branch information
simonmicro committed May 31, 2021
1 parent 73b1246 commit 270134d
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 55 deletions.
5 changes: 0 additions & 5 deletions include/osw_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ class OswHal {
uint8_t getActivityMode(void);

// Time

void updateTimeViaNTP(long gmtOffset_sec, int daylightOffset_sec, uint32_t timeout_sec);
void setUTCTime(long);
uint32_t getUTCTime(void);
void getUTCTime(uint32_t* hour, uint32_t* minute, uint32_t* second);
Expand All @@ -145,9 +143,6 @@ class OswHal {
void getDate(uint32_t* day, uint32_t* month, uint32_t* year);
void getWeekdayString(int firstNChars, string* output);

// RF
MiniWifi* getWiFi(void);

// Destructor
~OswHal(){};

Expand Down
2 changes: 2 additions & 0 deletions include/services/OswServiceTaskWiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class OswServiceTaskWiFi : public OswServiceTask {
bool isConnected();
IPAddress getIP(); /// Either get ip of this ap client it connected and enabled or station if enabled
void queueTimeUpdateViaNTP();
int32_t getSignalStrength();
uint8_t getSignalQuality();

//WiFi (client)
bool isWiFiEnabled();
Expand Down
17 changes: 12 additions & 5 deletions src/apps/_experiments/dnatilt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
#include <osw_app.h>
#include <osw_hal.h>

#include <services/OswServiceTasks.h>
#include <services/OswServiceTaskWiFi.h>

#define HTTP_GET_BUF_LEN 10
char httpGetBuffer[HTTP_GET_BUF_LEN];

void OswAppDNATilt::setup(OswHal* hal) { //
hal->getWiFi()->setDebugStream(&Serial);
void OswAppDNATilt::setup(OswHal* hal) {

}

void OswAppDNATilt::loop(OswHal* hal) {
hal->getWiFi()->checkWifi();

static long lastDraw = 0;

if (millis() - lastDraw > 250 /* 4fps redraw */) {
Expand All @@ -31,7 +32,7 @@ void OswAppDNATilt::loop(OswHal* hal) {
} else if (hal->getAccelerationX() < -250) {
hal->getCanvas()->print("DNA Lamp --");
} else {
if (hal->getWiFi()->isConnected()) {
if (OswServiceAllTasks::wifi.isConnected()) {
hal->getCanvas()->print("DNA Lamp");
} else {
hal->getCanvas()->print("No WiFi");
Expand All @@ -41,9 +42,15 @@ void OswAppDNATilt::loop(OswHal* hal) {
hal->requestFlush();
}

/*
THIS IS NOW MANAGED BY THE WIFI SERVICE.
The get() method is still a NON-STATIC method of MiniWiFi and therfore currently unusable.
Then you need this code working, please contact the author of the MiniWiFi lib or extend it youself.
if (hal->getAccelerationX() > 250) {
hal->getWiFi()->get("http://192.168.1.54/api/ladder/hue/decr", httpGetBuffer, HTTP_GET_BUF_LEN);
} else if (hal->getAccelerationX() < -250) {
hal->getWiFi()->get("http://192.168.1.54/api/ladder/hue/incr", httpGetBuffer, HTTP_GET_BUF_LEN);
}
*/
}
21 changes: 12 additions & 9 deletions src/apps/tools/time_config.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "./apps/tools/time_config.h"

#include <config.h>
Expand All @@ -9,9 +8,11 @@
#include <time.h>
#include <osw_ui.h>

#include <services/OswServiceTasks.h>
#include <services/OswServiceTaskWiFi.h>

void OswAppTimeConfig::setup(OswHal* hal) {
hal->getWiFi()->setDebugStream(&Serial);

}

void OswAppTimeConfig::enterManualMode(OswHal* hal) {
Expand Down Expand Up @@ -148,7 +149,7 @@ void OswAppTimeConfig::loop(OswHal* hal) {
hal->gfx()->setTextSize(2);

OswUI::getInstance()->setTextCursor(BUTTON_3);
if (hal->getWiFi()->isConnected()) {
if (OswServiceAllTasks::wifi.isConnected()) {
hal->gfx()->print(LANG_DISCONNECT);
} else {
hal->gfx()->print(LANG_CONNECT);
Expand All @@ -157,19 +158,21 @@ void OswAppTimeConfig::loop(OswHal* hal) {
}

if (hal->btnHasGoneDown(BUTTON_3)) {
if (hal->getWiFi()->isConnected()) {
hal->getWiFi()->disableWiFi();
if (OswServiceAllTasks::wifi.isConnected()) {
OswServiceAllTasks::wifi.disconnectWiFi();
OswServiceAllTasks::wifi.disableWiFi();
} else {
hal->getWiFi()->joinWifi();
OswServiceAllTasks::wifi.enableWiFi();
OswServiceAllTasks::wifi.connectWiFi();
}
}

if (hal->getWiFi()->isConnected()) {
if (OswServiceAllTasks::wifi.isConnected()) {
OswUI::getInstance()->setTextCursor(BUTTON_2);
hal->gfx()->print(LANG_TFW_UPDATE);
if (hal->btnHasGoneDown(BUTTON_2)) {
if (hal->getWiFi()->isConnected()) {
hal->updateTimeViaNTP(OswConfigAllKeys::timeZone.get() * 3600, OswConfigAllKeys::daylightOffset.get() * 3600, 5 /*seconds*/);
if (OswServiceAllTasks::wifi.isConnected()) {
OswServiceAllTasks::wifi.queueTimeUpdateViaNTP();
}
}
} else {
Expand Down
24 changes: 1 addition & 23 deletions src/hal/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,4 @@ void OswHal::getWeekdayString(int firstNChars, string *output) {
}

*output = value.substr(0, firstNChars);
}

void OswHal::updateTimeViaNTP(long gmtOffset_sec, int daylightOffset_sec, uint32_t timeout_sec) {
long start = millis();
if (getWiFi()->isConnected()) {
// this configures the timezone and sets the esps time to UTC
configTime(gmtOffset_sec + 3600, daylightOffset_sec, "pool.ntp.org", "time.nist.gov");

Serial.println("Waiting for time");

while (!time(nullptr) && millis() - start < timeout_sec * 1000) {
Serial.print(".");
delay(1000);
}

// sometimes time(nullptr) returns seconds since boot
// so check the request was resolved
if (time(nullptr) > 1600000000) {
Serial.println(time(nullptr));
setUTCTime(time(nullptr));
}
}
}
}
10 changes: 0 additions & 10 deletions src/hal/wifi.cpp

This file was deleted.

9 changes: 6 additions & 3 deletions src/overlays/overlays.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <services/OswServiceTasks.h>
#include <services/OswServiceTaskWiFi.h>

#include "./overlays/overlays.h"

#include "osw_pins.h"
Expand Down Expand Up @@ -28,8 +31,8 @@ void drawBattery(OswHal* hal, uint16_t x, uint16_t y) {
}

void drawWiFi(OswHal* hal, uint16_t x, uint16_t y) {
if (hal->getWiFi()->isEnabled()) {
for (uint8_t i = 0; i < hal->getWiFi()->getSignalQuality() / 20; i++) {
if (OswServiceAllTasks::wifi.isWiFiEnabled()) {
for (uint8_t i = 0; i < OswServiceAllTasks::wifi.getSignalQuality() / 20; i++) {
hal->getCanvas()->getGraphics2D()->fillFrame(x + 3 * i, y + 12 - i * 2, 2, i * 2,
OswUI::getInstance()->getForegroundColor()); // outer frame
}
Expand All @@ -39,7 +42,7 @@ void drawWiFi(OswHal* hal, uint16_t x, uint16_t y) {
void drawOverlays(OswHal* hal) {
if (hal->isCharging()) {
drawUsbConnected(hal, 120 - 16, 6); // width is 31
} else if (!hal->getWiFi()->isConnected()) {
} else if (!OswServiceAllTasks::wifi.isConnected()) {
drawBattery(hal, 120 - 15, 6);
}
drawWiFi(hal, 138, 6);
Expand Down
14 changes: 14 additions & 0 deletions src/services/OswServiceTaskWiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,18 @@ void OswServiceTaskWiFi::updateWiFiConfig() {
Serial.println(String(__FILE__) + ": Off");
#endif
}
}

int32_t OswServiceTaskWiFi::getSignalStrength() { return WiFi.RSSI(); }
uint8_t OswServiceTaskWiFi::getSignalQuality() {
int32_t rssi = getSignalStrength();
uint8_t quality = 0;
if (rssi < -100) {
quality = 0;
} else if (rssi > -50) {
quality = 100;
} else {
quality = 2 * (rssi + 100);
}
return quality;
}

0 comments on commit 270134d

Please sign in to comment.