Add JSON parser, vue.js SPA and pulse generator

This commit is contained in:
fillotassi 2018-09-09 23:55:47 +02:00
parent b6f2b48647
commit 2686fb98d1
3 changed files with 4430 additions and 37 deletions

8
gadgety.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef _GADGETY_H
#define _GADGETY_H
#include <ESP8266WebServer.h>
void gadgety_static_init_server(ESP8266WebServer* server);
#endif // _GADGETY_H

View file

@ -1,15 +1,18 @@
//#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <DNSServer.h> #include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
//#include <FS.h> #include "gadgety.h"
#include "index_html.h" #include <ArduinoJson.h>
#include <list> #include <list>
using namespace std; using namespace std;
#define TIMESTEPS 80000 // 1 millisecond #define TIMESTEPS 80000 // 1 millisecond
#define LINES_NUM 8 #define LINES_NUM 4
const unsigned LINE_PIN_MAP[] = { 2, 12, 13, 14 };
typedef unsigned int line_time_t; typedef unsigned int line_time_t;
@ -36,7 +39,6 @@ typedef struct {
list<event_t>::iterator next_event; list<event_t>::iterator next_event;
} line_t; } line_t;
// Todo: jsonify() and unjsonify()
line_t lines[LINES_NUM]; line_t lines[LINES_NUM];
void sequencer_init(void) void sequencer_init(void)
@ -46,8 +48,8 @@ unsigned i;
for(i = 0; i < LINES_NUM; ++i) { for(i = 0; i < LINES_NUM; ++i) {
lines[i].cursor = 0; lines[i].cursor = 0;
lines[i].next_event = lines[i].events.begin(); lines[i].next_event = lines[i].events.begin();
pinMode(LINE_PIN_MAP[i], OUTPUT);
} }
pinMode(2, OUTPUT);
} }
// Call every TIMESTEPS seconds // Call every TIMESTEPS seconds
@ -61,15 +63,22 @@ unsigned i;
for(i = 0; i < LINES_NUM; ++i) { for(i = 0; i < LINES_NUM; ++i) {
while(lines[i].next_event != lines[i].events.end() && lines[i].next_event->time == lines[i].cursor) { while(lines[i].next_event != lines[i].events.end() && lines[i].next_event->time == lines[i].cursor) {
// Peform event: lines[i].next_event // Peform event: lines[i].next_event
// ... switch(lines[i].next_event->type) {
case EVENT_NOTEON:
digitalWrite(LINE_PIN_MAP[i], 1);
break;
case EVENT_NOTEOFF:
digitalWrite(LINE_PIN_MAP[i], 0);
break;
// Todo: tone, jump, barrier, envelope
}
++lines[i].next_event; ++lines[i].next_event;
} }
if(lines[i].next_event != lines[i].events.end()) if(lines[i].next_event != lines[i].events.end())
++lines[i].cursor; ++lines[i].cursor;
} }
digitalWrite(2, !digitalRead(2));
} }
const byte DNS_PORT = 53; const byte DNS_PORT = 53;
@ -77,21 +86,43 @@ ESP8266WebServer server(80); //Server on port 80
IPAddress myIP = WiFi.softAPIP(); //Get IP address IPAddress myIP = WiFi.softAPIP(); //Get IP address
DNSServer dnsServer; DNSServer dnsServer;
void redirect_index() void serve_play(void)
{ {
server.sendHeader("Location", "/"); unsigned lineno, t;
server.send(301); event_t event;
} JsonArray::iterator it;
const char *p;
void serve_index() if(server.args() != 1)
{ return;
server.sendHeader("Content-Encoding", "gzip");
server.send_P(200, "text/html", (const char*)index_html_gz, index_html_gz_len);
}
void serve_random() StaticJsonBuffer<512> jsonbuf;
{ JsonObject &model = jsonbuf.parseObject(server.arg(0));
server.send(200, "application/json", "{ \"val\": " + String(rand()) + " }"); JsonArray &model_lines = model["lines"];
unsigned millis_per_slot = model["millis_per_slot"];
//
for(lineno = 0; lineno < LINES_NUM; ++lineno) {
lines[lineno].cursor = 0;
lines[lineno].events.clear();
}
for(it = model_lines.begin(), lineno = 0; it != model_lines.end() && lineno < LINES_NUM; ++it, ++lineno) {
for(p = (const char*)(*it), t = 0; *p; ++p, ++t) {
if(*p != '0') {
event.type = EVENT_NOTEON;
event.time = t * millis_per_slot;
lines[lineno].events.push_back(event);
event.type = EVENT_NOTEOFF;
event.time += millis_per_slot / 2;
lines[lineno].events.push_back(event);
}
}
}
for(lineno = 0; lineno < LINES_NUM; ++lineno)
lines[lineno].next_event = lines[lineno].events.begin();
} }
void setup() void setup()
@ -104,25 +135,19 @@ void setup()
timer0_attachInterrupt(sequencer_main_loop); timer0_attachInterrupt(sequencer_main_loop);
timer0_write(ESP.getCycleCount() + TIMESTEPS); timer0_write(ESP.getCycleCount() + TIMESTEPS);
interrupts(); interrupts();
// SPIFFS.begin(); // SPIFFS.begin();
Serial.begin(115200); Serial.begin(115200);
WiFi.mode(WIFI_AP); //Only Access point WiFi.mode(WIFI_AP); //Only Access point
WiFi.softAP("Gadgety", "gadgety123"); //Start HOTspot removing password will disable security 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.begin();
server.on("/index.html", serve_index); gadgety_static_init_server(&server);
server.on("/", serve_index); server.on("/play.ws", serve_play);
server.on("/random.ws", serve_random);
dnsServer.start(DNS_PORT, "www.gadgety.com", myIP); dnsServer.start(DNS_PORT, "gadgety.com", myIP);
} }
void loop() { void loop() {
dnsServer.processNextRequest(); dnsServer.processNextRequest();
server.handleClient(); //Handle client requests server.handleClient(); //Handle client requests
@ -131,8 +156,8 @@ void loop() {
} else if (server.arg(1)=="0") { } else if (server.arg(1)=="0") {
Serial.print("Spento"); Serial.print("Spento");
} }
} }
// void handleRoot() { // void handleRoot() {

4360
gadgety_static.cpp Normal file

File diff suppressed because it is too large Load diff