Skip to content

Commit

Permalink
done pref, enow
Browse files Browse the repository at this point in the history
  • Loading branch information
plaber committed Apr 29, 2023
1 parent 7b1836d commit abf87f4
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 45 deletions.
1 change: 1 addition & 0 deletions conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct config
bool skwf;
bool bt;
bool enow;
bool enowone;
uint8_t pins[6];
uint8_t pinb;
uint8_t pinp;
Expand Down
3 changes: 2 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ char exjpg[5] = ".jpg";
char extxt[5] = ".txt";

struct config conf = {
"v0.14e",
"v0.15",
"LedHDxx",
{}, //macs
0, //macson
Expand All @@ -37,6 +37,7 @@ struct config conf = {
false, //skwf
false, //bluetooth
false, //enow
false, //enowone
{22,25,26,27,0,0}, //pins
BTN_PIN,
PWR_PIN,
Expand Down
2 changes: 1 addition & 1 deletion sub_btn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void check_off()
{
char ans[8];
sprintf(ans, "iamon%02d", enow_getorder());
enow_send(String(ans));
enow_send(ans);
}
if (offm != millis())
{
Expand Down
50 changes: 15 additions & 35 deletions sub_enow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,9 @@ static uint16_t macmask;

void enow_begin()
{
WiFi.persistent(false);
WiFi.mode(WIFI_AP);
WiFi.softAP("ESPNOW", nullptr, WIFI_CHANNEL);
WiFi.softAPdisconnect(false);

bool conENow = WifiEspNow.begin();//Broadcast
if (!conENow)
{
Serial.println("Enow failed");
}
else
{
WifiEspNow.onReceive(enow_receive, nullptr);//Broadcast
Serial.println("Enow begin");
}
bool conENow = WifiEspNowBroadcast.begin("ESPNOW", WIFI_CHANNEL);
if (!conENow) Serial.println("WifiEspNowBroadcast.begin() failed");
else WifiEspNowBroadcast.onReceive(enow_receive, nullptr);
macmask = 1;
for (int i = 0; i <= conf.macslen; i++) macmask |= 1 << i;
int me = enow_getorder();
Expand All @@ -32,12 +20,12 @@ void enow_begin()

void enow_end()
{
WifiEspNow.onReceive(nullptr, nullptr);//Broadcast
WifiEspNowBroadcast.onReceive(nullptr, nullptr);
}

void enow_poll()
{
//WifiEspNow.loop();//Broadcast
WifiEspNowBroadcast.loop();
}

void enow_receive(const uint8_t mac[6], const uint8_t * buf, size_t count, void * cbarg)
Expand All @@ -60,7 +48,7 @@ void enow_receive(const uint8_t mac[6], const uint8_t * buf, size_t count, void
for (int i = 0; i < count; ++i) buft[i] = buf[i]; //Serial.print(static_cast<char>(buf[i]));
buft[count] = 0;
String q = String(buft);
Serial.println(String(millis()) + ": " + q);
Serial.println(q);
if (q.startsWith("enow"))
{
int dei = q.indexOf("=");
Expand Down Expand Up @@ -94,25 +82,20 @@ void enow_receive(const uint8_t mac[6], const uint8_t * buf, size_t count, void
Serial.print(conf.macson, BIN); Serial.print(" "); Serial.println(macmask, BIN);
if (conf.macson == macmask)
{
//WifiEspNowBroadcast.send(reinterpret_cast < const uint8_t * > ("enow_go=1"), 9);
//WifiEspNowBroadcast.send(reinterpret_cast < const uint8_t * > ("enow_go=1"), 9);
for (int i = 0; i < conf.macslen; i++) {WifiEspNow.send(conf.macs[i], reinterpret_cast <const uint8_t * > ("enow_go=1"), 9); delay(5);}
for (int i = 0; i < conf.macslen; i++) {WifiEspNow.send(conf.macs[i], reinterpret_cast <const uint8_t * > ("enow_go=1"), 9); delay(5);}
WifiEspNowBroadcast.send(reinterpret_cast < const uint8_t * > ("enow_go=1"), 9);
WifiEspNowBroadcast.send(reinterpret_cast < const uint8_t * > ("enow_go=1"), 9);
state.go = true;
}
}
}

void enow_send(String txt)
void enow_send(char txt[])
{
char msg[60];
int len = txt.length();
txt.toCharArray(msg, 60);
snprintf(msg, sizeof(msg), "%s", txt);
//WifiEspNowBroadcast.send(reinterpret_cast < const uint8_t * > (msg), len);
for (int i = 0; i < conf.macslen; i++) {WifiEspNow.send(conf.macs[i], reinterpret_cast <const uint8_t * > (msg), len); delay(5);}
Serial.printf("Sending message (%d): %s\n", len, msg);
/*
int len = snprintf(msg, sizeof(msg), "%s", txt);
WifiEspNowBroadcast.send(reinterpret_cast < const uint8_t * > (msg), len);
Serial.println("Sending message");
Serial.println(msg);
Serial.print("Recipients:");
const int MAX_PEERS = 20;
WifiEspNowPeerInfo peers[MAX_PEERS];
Expand All @@ -123,7 +106,6 @@ void enow_send(String txt)
Serial.write(' ');
}
Serial.println();
*/
}

int enow_getorder()
Expand Down Expand Up @@ -172,15 +154,13 @@ void enow_wakeup()
Serial.print("adding peer ");
for (int m = 0; m < 6; m++) Serial.printf("%02X", conf.macs[i][m]);
Serial.println();
WifiEspNow.addPeer(conf.macs[i]);
WifiEspNow.addPeer(conf.macs[i], 3);
}
}
int ord = enow_getorder();
if (ord == conf.macslen + 1)
{
//WifiEspNowBroadcast.send(reinterpret_cast<const uint8_t*>("go"), 2);
for (int i = 0; i < conf.macslen; i++) WifiEspNow.send(conf.macs[i], reinterpret_cast <const uint8_t * > ("go"), 2);

WifiEspNowBroadcast.send(reinterpret_cast<const uint8_t*>("go"), 2);
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion sub_enow.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void enow_begin();
void enow_end();
void enow_poll();
void enow_receive(const uint8_t mac[6], const uint8_t * buf, size_t count, void * cbarg);
void enow_send(String txt);
void enow_send(char txt[]);
int enow_getorder();
void enow_wakeup2();
void enow_wakeup();
Expand Down
16 changes: 14 additions & 2 deletions sub_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ static const char content_files1[] PROGMEM = R"=====(
<meta charset='UTF-8'>
<style>
td {padding: 0 10px;}
input {margin:0 5px 5px 0;}
</style>
</head>
<body>
Expand Down Expand Up @@ -715,7 +716,7 @@ static const char content_prog[] PROGMEM = R"=====(
<head>
<title>poihd prog</title><meta charset='UTF-8'>
<script src='prog.js'></script>
<style>img,input {margin-right: 5px;}</style>
<style>img,input,select {margin:0 5px 5px 0;}</style>
</head>
<body>
<a href='/'>main</a>   
Expand All @@ -733,7 +734,7 @@ prog <input type='button' value='load' onClick='lp()'><input type='button' value
pics <input type='button' value='clear' onClick='if(confirm("del all?")) ws.clearRegions()'><label><input type='checkbox' id='delrc'>del by click</label>
<div style='width:100%;overflow:scroll;'><table id='pcs'></table></div><br>
<select id='progs' onchange='loadf(this.value)'></select>
<select id='progs' onchange='loadf(this.value)'></select><input type='button' value='del' onclick='delp()'>
<br>
<form method='POST'>
name <input name='progn' id='progn' maxlength=25 onkeyup='this.value = this.value.replace(/[^A-Za-z0-9_.]/g, "")'><br>
Expand Down Expand Up @@ -848,6 +849,17 @@ async function savep(){
loads(document.getElementById('progn').value);
alert(t);
}
function delp(){
var a = document.getElementById('progs');
var v = a.value, i = a.selectedIndex;
if (i == -1) return;
var t = a.options[i].text;
var q = confirm('delete '+t+' ?');
if(q) fetch('/del?f='+encodeURI('/prog_'+v+'.txt'))
.then((response) => {return response.text();})
.then((data) => {alert(data);if(data.indexOf('deleted')!=-1){a.remove(i); a.onchange();}
})
}
)=====";

void handleProg()
Expand Down
32 changes: 31 additions & 1 deletion sub_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@ void json_save()
preferences.putBool( "skwf", conf.skwf);
preferences.putBool( "blth", conf.bt);
preferences.putBool( "enow", conf.enow);
preferences.putBool( "enow1", conf.enowone);
preferences.putInt( "whdr", state.whdr);
preferences.putUShort("bpms", state.bpm);
preferences.putString("prog", state.progname);
if (state.progname != "no_prog")
{
preferences.putString("prog", state.progname);
}
else
{
preferences.remove("prog");
}
if (ssid[1] != "spiffs" && pass[1] != "spiffs")
{
preferences.putString("ssd1", ssid[1]);
Expand Down Expand Up @@ -112,6 +120,7 @@ void json_load()
conf.skwf = preferences.getBool( "skwf", false);
conf.bt = preferences.getBool( "blth", false);
conf.enow = preferences.getBool( "enow", false);
conf.enowone = preferences.getBool( "enow1", false);
state.whdr = preferences.getInt( "whdr", 3);
state.bpm = preferences.getUShort("bpms", 4000);
state.progname = preferences.getString("prog", "no_prog");
Expand All @@ -120,6 +129,27 @@ void json_load()
ssid[2] = preferences.getString("ssd2", "spiffs");
pass[2] = preferences.getString("pss2", "spiffs");
preferences.end();
if (conf.enowone)
{
preferences.begin("conf", false);
preferences.putBool( "skwf", false);
preferences.putBool( "enow", false);
preferences.putBool( "enow1", false);
preferences.end();
}
}

void json_del()
{
Preferences preferences;
preferences.begin("conf", false);
conf.macslen = 0;
memset(conf.macs, 0, 96);
state.progname = "no_prog";
preferences.putUChar( "mlen", conf.macslen);
preferences.putBytes( "mmac", conf.macs, 96);
preferences.remove("prog");
preferences.end();
}

void json_clear()
Expand Down
5 changes: 3 additions & 2 deletions sub_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#include <Preferences.h>
#include <nvs_flash.h>

void json_save();
void json_load();
String str_encode(String in);
void mac_decode(String in, uint8_t *ans);
void json_save();
void json_load();
void json_del();

#endif
11 changes: 9 additions & 2 deletions sub_udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ String get_answ(String san, String sav)
conf.cont = savi;
return "cont set ok " + sav;
}
if (san == "delconf")
{
json_del();
conf.macslen = 0;
return F("conf deleted");
}
if (san == "drip")
{
IPAddress myip = WiFi.localIP();
Expand All @@ -137,6 +143,7 @@ String get_answ(String san, String sav)
conf.bt = false;
conf.skwf = true;
conf.enow = true;
conf.enowone = true;
json_save();
return "enow on";
}
Expand Down Expand Up @@ -220,8 +227,8 @@ String get_answ(String san, String sav)
if (san == "macor")
{
int px = enow_getorder();
led_clear();
for (int i = 0; i < px; i++) led_setpx(i, green);
for (int i = 0; i < 22; i++) led_setpxall(i, black);
for (int i = 0; i < px; i++) led_setpxall(i, green);
led_show();
return "mac order";
}
Expand Down

0 comments on commit abf87f4

Please sign in to comment.