Browse Source

improv does not open complete menu every time

pull/1027/head
lumapu 2 years ago
parent
commit
efe3bc4d20
  1. 2
      src/app.cpp
  2. 4
      src/app.h
  3. 2
      src/appInterface.h
  4. 1
      src/utils/dbg.cpp
  5. 9
      src/utils/dbg.h
  6. 50
      src/utils/improv.h
  7. 6
      src/wifi/ahoywifi.cpp
  8. 2
      src/wifi/ahoywifi.h

2
src/app.cpp

@ -84,7 +84,7 @@ void app::setup() {
mPubSerial.setup(mConfig, &mSys, &mTimestamp);
mImprov.setup(mConfig->sys.deviceName, mVersion);
mImprov.setup(this, mConfig->sys.deviceName, mVersion);
regularTickers();

4
src/app.h

@ -101,8 +101,8 @@ class app : public IApp, public ah::Scheduler {
mWifi.scanAvailNetworks();
}
void getAvailNetworks(JsonObject obj) {
mWifi.getAvailNetworks(obj);
bool getAvailNetworks(JsonObject obj) {
return mWifi.getAvailNetworks(obj);
}
void setOnUpdate() {

2
src/appInterface.h

@ -25,7 +25,7 @@ class IApp {
virtual const char *getVersion() = 0;
virtual statistics_t *getStatistics() = 0;
virtual void scanAvailNetworks() = 0;
virtual void getAvailNetworks(JsonObject obj) = 0;
virtual bool getAvailNetworks(JsonObject obj) = 0;
virtual uint32_t getUptime() = 0;
virtual uint32_t getTimestamp() = 0;

1
src/utils/dbg.cpp

@ -1,3 +1,4 @@
#include "dbg.h"
DBG_CB mCb = NULL;
bool mDebugEn = true;

9
src/utils/dbg.h

@ -39,6 +39,7 @@
#ifdef ARDUINO
#define DBG_CB std::function<void(String)>
extern DBG_CB mCb;
extern bool mDebugEn;
inline void registerDebugCb(DBG_CB cb) {
mCb = cb;
@ -48,12 +49,16 @@
#define DSERIAL Serial
#endif
inline void setDebugEn(bool en) {
mDebugEn = en;
}
//template <class T>
inline void DBGPRINT(String str, bool ser = true) { if(ser) DSERIAL.print(str); if(NULL != mCb) mCb(str); }
inline void DBGPRINT(String str, bool ser = true) { if(ser && mDebugEn) DSERIAL.print(str); if(NULL != mCb) mCb(str); }
//template <class T>
inline void DBGPRINTLN(String str, bool ser = true) { DBGPRINT(str); DBGPRINT(F("\r\n")); }
inline void DHEX(uint8_t b, bool ser = true) {
if(ser) {
if(ser && mDebugEn) {
if( b<0x10 ) DSERIAL.print(F("0"));
DSERIAL.print(b,HEX);
}

50
src/utils/improv.h

@ -9,6 +9,7 @@
#include <cstring>
#include <functional>
#include "dbg.h"
#include "AsyncJson.h"
// https://www.improv-wifi.com/serial/
// https://github.com/jnthas/improv-wifi-demo/blob/main/src/esp32-wifiimprov/esp32-wifiimprov.ino
@ -16,12 +17,18 @@
// configure ESP through Serial interface
class Improv {
public:
void setup(const char *devName, const char *version) {
void setup(IApp *app, const char *devName, const char *version) {
mApp = app;
mDevName = devName;
mVersion = version;
mScanRunning = false;
}
void tickSerial(void) {
if(mScanRunning)
getNetworks();
if(Serial.available() == 0)
return;
@ -114,7 +121,7 @@ class Improv {
}
void sendDevInfo(void) {
uint8_t buf[100];
uint8_t buf[50];
buf[7] = TYPE_RPC_RESPONSE;
buf[9] = GET_DEVICE_INFO; // repsonse to cmd
uint8_t p = 11;
@ -137,6 +144,37 @@ class Improv {
sendPaket(buf, p);
}
void getNetworks(void) {
if(!mScanRunning)
mApp->scanAvailNetworks();
JsonObject obj;
if(!mApp->getAvailNetworks(obj))
return;
mScanRunning = false;
uint8_t buf[50];
buf[7] = TYPE_RPC_RESPONSE;
buf[9] = GET_WIFI_NETWORKS; // repsonse to cmd
uint8_t p = 11;
JsonArray arr = obj[F("networks")];
for(uint8_t i = 0; i < arr.size(); i++) {
buf[p++] = strlen(arr[i][F("ssid")]);
// ssid
p += char2Improv(arr[i][F("ssid")], &buf[p]);
buf[p++] = String(arr[i][F("rssi")]).length();
// rssi
p += char2Improv(String(arr[i][F("rssi")]).c_str(), &buf[p]);
buf[10] = p - 11; // sub length
buf[8] = p - 9; // paket length
sendPaket(buf, p);
}
}
void setState(uint8_t state) {
uint8_t buf[20];
buf[7] = TYPE_CURRENT_STATE;
@ -162,14 +200,20 @@ class Improv {
void parsePayload(uint8_t type, uint8_t buf[], uint8_t len) {
if(TYPE_RPC == type) {
if(GET_CURRENT_STATE == buf[0])
if(GET_CURRENT_STATE == buf[0]) {
setDebugEn(false);
setState(STATE_AUTHORIZED);
}
else if(GET_DEVICE_INFO == buf[0])
sendDevInfo();
else if(GET_WIFI_NETWORKS == buf[0])
getNetworks();
}
}
IApp *mApp;
const char *mDevName, *mVersion;
bool mScanRunning;
};
#endif /*__IMPROV_H__*/

6
src/wifi/ahoywifi.cpp

@ -274,12 +274,12 @@ void ahoywifi::scanAvailNetworks(void) {
}
//-----------------------------------------------------------------------------
void ahoywifi::getAvailNetworks(JsonObject obj) {
bool ahoywifi::getAvailNetworks(JsonObject obj) {
JsonArray nets = obj.createNestedArray("networks");
int n = WiFi.scanComplete();
if (n < 0)
return;
return false;
if(n > 0) {
int sort[n];
sortRSSI(&sort[0], n);
@ -292,6 +292,8 @@ void ahoywifi::getAvailNetworks(JsonObject obj) {
WiFi.scanDelete();
if(mStaConn == IN_AP_MODE)
WiFi.mode(WIFI_AP);
return true;
}
//-----------------------------------------------------------------------------

2
src/wifi/ahoywifi.h

@ -27,7 +27,7 @@ class ahoywifi {
void tickWifiLoop(void);
bool getNtpTime(void);
void scanAvailNetworks(void);
void getAvailNetworks(JsonObject obj);
bool getAvailNetworks(JsonObject obj);
private:
typedef enum WiFiStatus {

Loading…
Cancel
Save