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 <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include <FS.h>
#include "index_html.h"
#include "gadgety.h"
#include <ArduinoJson.h>
#include <list>
using namespace std;
#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;
@ -36,7 +39,6 @@ typedef struct {
list<event_t>::iterator next_event;
} line_t;
// Todo: jsonify() and unjsonify()
line_t lines[LINES_NUM];
void sequencer_init(void)
@ -46,8 +48,8 @@ unsigned i;
for(i = 0; i < LINES_NUM; ++i) {
lines[i].cursor = 0;
lines[i].next_event = lines[i].events.begin();
pinMode(LINE_PIN_MAP[i], OUTPUT);
}
pinMode(2, OUTPUT);
}
// Call every TIMESTEPS seconds
@ -61,15 +63,22 @@ unsigned 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) {
// 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;
}
if(lines[i].next_event != lines[i].events.end())
++lines[i].cursor;
}
digitalWrite(2, !digitalRead(2));
}
const byte DNS_PORT = 53;
@ -77,21 +86,43 @@ ESP8266WebServer server(80); //Server on port 80
IPAddress myIP = WiFi.softAPIP(); //Get IP address
DNSServer dnsServer;
void redirect_index()
void serve_play(void)
{
server.sendHeader("Location", "/");
server.send(301);
unsigned lineno, t;
event_t event;
JsonArray::iterator it;
const char *p;
if(server.args() != 1)
return;
StaticJsonBuffer<512> jsonbuf;
JsonObject &model = jsonbuf.parseObject(server.arg(0));
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();
}
void serve_index()
{
server.sendHeader("Content-Encoding", "gzip");
server.send_P(200, "text/html", (const char*)index_html_gz, index_html_gz_len);
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);
}
}
}
void serve_random()
{
server.send(200, "application/json", "{ \"val\": " + String(rand()) + " }");
for(lineno = 0; lineno < LINES_NUM; ++lineno)
lines[lineno].next_event = lines[lineno].events.begin();
}
void setup()
@ -109,20 +140,14 @@ void setup()
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);
server.begin();
gadgety_static_init_server(&server);
server.on("/play.ws", serve_play);
dnsServer.start(DNS_PORT, "www.gadgety.com", myIP);
dnsServer.start(DNS_PORT, "gadgety.com", myIP);
}
void loop() {
dnsServer.processNextRequest();
server.handleClient(); //Handle client requests

4360
gadgety_static.cpp Normal file

File diff suppressed because it is too large Load diff