1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-22 23:56:13 +01:00

Wifi connect and waveform

This commit is contained in:
Harald Barth 2021-10-05 21:53:02 +02:00
parent 75dffd9dfa
commit d174c05127
2 changed files with 124 additions and 1 deletions

View File

@ -179,7 +179,7 @@ void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
hw_timer_t *timer = NULL;
timer = timerBegin(0, 2, true); // prescaler can be 2 to 65536 so choose 2
timerAttachInterrupt(timer, interruptHandler, true);
timerAlarmWrite(timer, CLOCK_CYCLES / 2, true); // divide by prescaler
timerAlarmWrite(timer, CLOCK_CYCLES / 6, true); // divide by prescaler*3 (Clockbase is 80Mhz and not F_CPU 240Mhz)
timerAlarmEnable(timer);
}

View File

@ -25,15 +25,138 @@
#include "RingStream.h"
#include "CommandDistributor.h"
static std::vector<WiFiClient> clients; // a list to hold all clients
static WiFiServer *server = NULL;
static RingStream *outboundRing = new RingStream(2048);
bool WifiESP::setup(const char *SSid,
const char *password,
const char *hostname,
int port,
const byte channel) {
bool havePassword = true;
bool haveSSID = true;
bool wifiUp = false;
const char *yourNetwork = "Your network ";
if (strncmp(yourNetwork, SSid, 13) == 0 || strncmp("", SSid, 13) == 0)
haveSSID = false;
if (strncmp(yourNetwork, password, 13) == 0 || strncmp("", password, 13) == 0)
havePassword = false;
if (haveSSID && havePassword) {
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(true);
WiFi.begin(SSid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
DIAG(F("Wifi STA IP %s"),WiFi.localIP().toString().c_str());
wifiUp = true;
}
}
if (!haveSSID) {
// prepare all strings
String strSSID("DCC_");
String strPass("PASS_");
String strMac = WiFi.macAddress();
strMac.remove(0,9);
strMac.replace(":","");
strMac.replace(":","");
strSSID.concat(strMac);
strPass.concat(strMac);
WiFi.mode(WIFI_AP);
if (WiFi.softAP(strSSID.c_str(),
havePassword ? password : strPass.c_str(),
channel, false, 8)) {
DIAG(F("Wifi AP SSID %s PASS %s"),strSSID.c_str(),havePassword ? password : strPass.c_str());
DIAG(F("Wifi AP IP %s"),WiFi.softAPIP().toString().c_str());
wifiUp = true;
}
}
if (!wifiUp) {
DIAG(F("Wifi all fail"));
// no idea to go on
return false;
}
server = new WiFiServer(port); // start listening on tcp port
server->begin();
DIAG(F("Server up port %d"),port);
return true;
return false;
}
void WifiESP::loop() {
int clientId; //tmp loop var
if (WiFi.status() == WL_CONNECTED /* || what for AP? */) {
if (server->hasClient()) {
// loop over all clients and remove inactive
for (clientId=0; clientId<clients.size(); clientId++){
// check if client is there and alive
if(!clients[clientId].connected()) {
clients[clientId].stop();
clients.erase(clients.begin()+clientId);
}
}
WiFiClient client;
while (client = server->available()) {
clients.push_back(client);
DIAG(F("New client %s"), client.remoteIP().toString().c_str());
}
}
// loop over all connected clients
for (clientId=0; clientId<clients.size(); clientId++){
if(clients[clientId].connected()) {
int len;
if ((len = clients[clientId].available()) > 0) {
// read data from client
byte cmd[len+1];
for(int i=0; i<len; i++) {
cmd[i]=clients[clientId].read();
}
cmd[len]=0;
outboundRing->mark(clientId);
CommandDistributor::parse(clientId,cmd,outboundRing);
outboundRing->commit();
}
}
} // all clients
// something to write out?
clientId=outboundRing->peek();
if (clientId >= 0) {
if ((unsigned int)clientId > clients.size()) {
// something is wrong with the ringbuffer position
outboundRing->info();
} else {
// we have data to send in outboundRing
if(clients[clientId].connected()) {
outboundRing->read(); // read over peek()
int count=outboundRing->count();
{
char buffer[count+1];
for(int i=0;i<count;i++) {
int c = outboundRing->read();
if (c >= 0)
buffer[i] = (char)c;
else {
DIAG(F("Ringread fail at %d"),i);
break;
}
}
buffer[count]=0;
clients[clientId].write(buffer,count);
}
}
}
}
} //connected
}
#endif //ESP32