Added NVS storage for last channel used in SpanPoint

Applicable only for remote devices (i.e. not a Hub device).  Last channel stored is used as initial channel upon reboot, provided that this channel is allowed by the channel mask (if not, the first allowed channel is selected instead).
This commit is contained in:
Gregg 2022-12-27 09:55:30 -06:00
parent 3fae9c24a9
commit eecce34848
2 changed files with 18 additions and 2 deletions

View File

@ -2247,6 +2247,16 @@ void SpanPoint::init(const char *password){
statusQueue = xQueueCreate(1,sizeof(esp_now_send_status_t)); // create statusQueue even if not needed statusQueue = xQueueCreate(1,sizeof(esp_now_send_status_t)); // create statusQueue even if not needed
setChannelMask(channelMask); // default channel mask at start-up uses channels 1-13 setChannelMask(channelMask); // default channel mask at start-up uses channels 1-13
uint8_t channel;
if(!isHub){ // this is not a hub
nvs_flash_init(); // initialize NVS
nvs_open("POINT",NVS_READWRITE,&pointNVS); // open SpanPoint data namespace in NVS
if(!nvs_get_u8(pointNVS,"CHANNEL",&channel)){ // if channel found in NVS...
if(channelMask & (1<<channel)) // ... and if channel is allowed by channel mask
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); // set the WiFi channel
}
}
initialized=true; initialized=true;
} }
@ -2287,8 +2297,11 @@ uint8_t SpanPoint::nextChannel(){
channel=(channel<13)?channel+1:1; // advance to next channel channel=(channel<13)?channel+1:1; // advance to next channel
} while(!(channelMask & (1<<channel))); // until we find next valid one } while(!(channelMask & (1<<channel))); // until we find next valid one
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); // set the WiFi channel esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); // set the WiFi channel
nvs_set_u8(pointNVS,"CHANNEL",channel);
nvs_commit(pointNVS);
return(channel); return(channel);
} }
@ -2364,6 +2377,8 @@ boolean SpanPoint::isHub=false;
vector<SpanPoint *> SpanPoint::SpanPoints; vector<SpanPoint *> SpanPoint::SpanPoints;
uint16_t SpanPoint::channelMask=0x3FFE; uint16_t SpanPoint::channelMask=0x3FFE;
QueueHandle_t SpanPoint::statusQueue; QueueHandle_t SpanPoint::statusQueue;
nvs_handle SpanPoint::pointNVS;
/////////////////////////////// ///////////////////////////////
// MISC // // MISC //

View File

@ -799,8 +799,9 @@ class SpanPoint {
static boolean initialized; static boolean initialized;
static boolean isHub; static boolean isHub;
static vector<SpanPoint *> SpanPoints; static vector<SpanPoint *> SpanPoints;
static uint16_t channelMask; // channel mask static uint16_t channelMask; // channel mask (only used for remote devices)
static QueueHandle_t statusQueue; // queue for communication between SpanPoint::dataSend and SpanPoint::send static QueueHandle_t statusQueue; // queue for communication between SpanPoint::dataSend and SpanPoint::send
static nvs_handle pointNVS; // NVS storage for channel number (only used for remote devices)
static void dataReceived(const uint8_t *mac, const uint8_t *incomingData, int len); static void dataReceived(const uint8_t *mac, const uint8_t *incomingData, int len);
static void init(const char *password="HomeSpan"); static void init(const char *password="HomeSpan");