From b6f2b4864743dbca43cf67bf1143b22e326d4465 Mon Sep 17 00:00:00 2001 From: fillotassi Date: Fri, 3 Aug 2018 20:21:31 +0200 Subject: [PATCH] First effort in creating a rest API --- gadgety_esp8266_embedded_HTML.ino | 140 ++++++++++++++++++++++++++++++ index.html | 37 ++++++++ index_html.h | 54 ++++++++++++ 3 files changed, 231 insertions(+) create mode 100644 gadgety_esp8266_embedded_HTML.ino create mode 100644 index.html create mode 100644 index_html.h diff --git a/gadgety_esp8266_embedded_HTML.ino b/gadgety_esp8266_embedded_HTML.ino new file mode 100644 index 0000000..2291e1e --- /dev/null +++ b/gadgety_esp8266_embedded_HTML.ino @@ -0,0 +1,140 @@ +#include +#include +#include +#include +//#include +#include "index_html.h" +#include + +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 events; + list::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); +// } diff --git a/index.html b/index.html new file mode 100644 index 0000000..774d74d --- /dev/null +++ b/index.html @@ -0,0 +1,37 @@ + + + + + + + Random generator + + + +

Random number generator (hurray!)

+ +

Press to generate new value

+ + + diff --git a/index_html.h b/index_html.h new file mode 100644 index 0000000..3b54262 --- /dev/null +++ b/index_html.h @@ -0,0 +1,54 @@ +unsigned char PROGMEM index_html_gz[] = { + 0x1f, 0x8b, 0x08, 0x08, 0x4a, 0x96, 0x5c, 0x5b, 0x00, 0x03, 0x69, 0x6e, + 0x64, 0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6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, 0x5d, 0x8e, 0x7a, 0x63, 0x2b, 0xd2, 0x2d, 0x4e, 0x5f, 0xc3, + 0x0d, 0xca, 0x3c, 0x18, 0xbb, 0x63, 0x0c, 0x87, 0xd7, 0xe6, 0x72, 0xe8, + 0x03, 0x68, 0x93, 0x35, 0xfc, 0x29, 0xfc, 0x03, 0xa3, 0xe0, 0x3b, 0xbc, + 0x25, 0x04, 0x00, 0x00 +}; +unsigned int index_html_gz_len = 604;