gadgetyWebServer/gadgety_esp8266_embedded_HTML.ino

141 lines
3 KiB
Arduino
Raw Normal View History

2018-08-03 20:21:31 +02:00
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include <FS.h>
#include "index_html.h"
#include <list>
using namespace std;
#define TIMESTEPS 80000 // 1 millisecond
#define LINES_NUM 8
typedef unsigned int line_time_t;
enum {
EVENT_NOTEON,
EVENT_NOTEOFF,
EVENT_JUMP,
EVENT_BARRIER,
EVENT_ENVELOPE // Todo
};
typedef struct {
unsigned type;
line_time_t time;
unsigned tone;
line_time_t new_cursor;
unsigned lines_mask;
} event_t;
typedef struct {
line_time_t cursor;
// ADSR? Other line/synth parameters?
list<event_t> events;
list<event_t>::iterator next_event;
} line_t;
// Todo: jsonify() and unjsonify()
line_t lines[LINES_NUM];
void sequencer_init(void)
{
unsigned i;
for(i = 0; i < LINES_NUM; ++i) {
lines[i].cursor = 0;
lines[i].next_event = lines[i].events.begin();
}
pinMode(2, OUTPUT);
}
// Call every TIMESTEPS seconds
void sequencer_main_loop(void)
{
unsigned i;
// Restart main loop timer
timer0_write(ESP.getCycleCount() + TIMESTEPS);
for(i = 0; i < LINES_NUM; ++i) {
while(lines[i].next_event != lines[i].events.end() && lines[i].next_event->time == lines[i].cursor) {
// Peform event: lines[i].next_event
// ...
++lines[i].next_event;
}
if(lines[i].next_event != lines[i].events.end())
++lines[i].cursor;
}
digitalWrite(2, !digitalRead(2));
}
const byte DNS_PORT = 53;
ESP8266WebServer server(80); //Server on port 80
IPAddress myIP = WiFi.softAPIP(); //Get IP address
DNSServer dnsServer;
void redirect_index()
{
server.sendHeader("Location", "/");
server.send(301);
}
void serve_index()
{
server.sendHeader("Content-Encoding", "gzip");
server.send_P(200, "text/html", (const char*)index_html_gz, index_html_gz_len);
}
void serve_random()
{
server.send(200, "application/json", "{ \"val\": " + String(rand()) + " }");
}
void setup()
{
// Start sequencer
sequencer_init();
noInterrupts();
timer0_isr_init();
timer0_attachInterrupt(sequencer_main_loop);
timer0_write(ESP.getCycleCount() + TIMESTEPS);
interrupts();
// SPIFFS.begin();
Serial.begin(115200);
WiFi.mode(WIFI_AP); //Only Access point
WiFi.softAP("Gadgety", "gadgety123"); //Start HOTspot removing password will disable security
// server.on("/", handleRoot); //Which routine to handle at root location
server.onNotFound([]() {
// handleRoot();
});
server.begin(); //Start server
//server.serveStatic("/", SPIFFS, "/index.html");
server.onNotFound(redirect_index);
server.on("/index.html", serve_index);
server.on("/", serve_index);
server.on("/random.ws", serve_random);
dnsServer.start(DNS_PORT, "www.gadgety.com", myIP);
}
void loop() {
dnsServer.processNextRequest();
server.handleClient(); //Handle client requests
if (server.arg(1)=="1") {
Serial.print("Acceso");
} else if (server.arg(1)=="0") {
Serial.print("Spento");
}
}
// void handleRoot() {
// server.send (200, "text/html", index.html);
// }