Clean up
This commit is contained in:
parent
8483963b92
commit
6328cd8145
17
src/display.cpp
Normal file
17
src/display.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "display.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
Display display(32, 8);
|
||||||
|
|
||||||
|
void setBrightness(int brightness) {
|
||||||
|
brightness = max(1, min(brightness, 255));
|
||||||
|
if (display.getBrightness() == brightness) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
config.brightness = brightness;
|
||||||
|
display.setBrightness(brightness);
|
||||||
|
config_set_dirty();
|
||||||
|
|
||||||
|
Serial.printf("Setting brightness to %5.1f%%\n", brightness / 2.55);
|
||||||
|
}
|
||||||
10
src/display.h
Normal file
10
src/display.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef MEDIATABLE_DISPLAY_H
|
||||||
|
#define MEDIATABLE_DISPLAY_H
|
||||||
|
|
||||||
|
#include "display/Display.h"
|
||||||
|
|
||||||
|
extern Display display;
|
||||||
|
|
||||||
|
void setBrightness(int brightness);
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
bool SYMBOLS[SYMBOL_COUNT][DISPLAY_CHAR_WIDTH * DISPLAY_CHAR_HEIGHT] = {
|
bool SYMBOLS[SYMBOL_COUNT][DISPLAY_CHAR_WIDTH * DISPLAY_CHAR_HEIGHT] = {
|
||||||
{
|
{
|
||||||
X, X, X,
|
X, X, X,
|
||||||
X, _, X,
|
X, _, X,
|
||||||
X, _, X,
|
X, _, X,
|
||||||
X, _, X,
|
X, _, X,
|
||||||
X, X, X,
|
X, X, X,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
_, _, X,
|
_, _, X,
|
||||||
_, X, X,
|
_, X, X,
|
||||||
|
|||||||
@ -120,11 +120,11 @@ public:
|
|||||||
leds.show();
|
leds.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setBrightness(int brightness) {
|
void setBrightness(uint8_t brightness) {
|
||||||
leds.setBrightness(max(0, min(brightness, 255)));
|
leds.setBrightness(brightness);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getBrightness() {
|
uint8_t getBrightness() {
|
||||||
return leds.getBrightness();
|
return leds.getBrightness();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
341
src/main.cpp
341
src/main.cpp
@ -1,347 +1,34 @@
|
|||||||
#include <WiFi.h>
|
#include "serial.h"
|
||||||
|
|
||||||
#include <ArduinoOTA.h>
|
|
||||||
|
|
||||||
#include "mode/Mode.h"
|
|
||||||
#include "display/Display.h"
|
|
||||||
#include <WebServer.h>
|
|
||||||
#include <esp_sntp.h>
|
|
||||||
#include <EEPROM.h>
|
|
||||||
|
|
||||||
#include "mode/GameOfLife/GameOfLife.h"
|
|
||||||
#include "mode/Pong/Pong.h"
|
|
||||||
#include "mode/Clock/Clock.h"
|
|
||||||
#include "mode/SpaceInvaders/SpaceInvaders.h"
|
|
||||||
#include "mode/NewYear/NewYear.h"
|
|
||||||
#include "mode/Border/Border.h"
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "wifi.h"
|
||||||
|
|
||||||
WebServer server(80);
|
#include "server.h"
|
||||||
|
|
||||||
Display display(32, 8);
|
#include "mode.h"
|
||||||
|
#include "display.h"
|
||||||
ModeId currentModeId = NONE;
|
|
||||||
|
|
||||||
microseconds_t lastMicros = 0;
|
|
||||||
|
|
||||||
ModeBase *mode = nullptr;
|
|
||||||
|
|
||||||
bool connected = false;
|
|
||||||
|
|
||||||
void mode_check();
|
|
||||||
|
|
||||||
void mode_step();
|
|
||||||
|
|
||||||
void unloadOldMode();
|
|
||||||
|
|
||||||
void loadNewMode();
|
|
||||||
|
|
||||||
void setBrightness(int brightness);
|
|
||||||
|
|
||||||
void setSpeed(double speed);
|
|
||||||
|
|
||||||
void serial_read();
|
|
||||||
|
|
||||||
void wifi_loop();
|
|
||||||
|
|
||||||
void ntp_setup();
|
|
||||||
|
|
||||||
uint32_t ip2int(const IPAddress &ip);
|
|
||||||
|
|
||||||
void setMode(ModeId value);
|
|
||||||
|
|
||||||
void web_index() {
|
|
||||||
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
|
||||||
server.send(200, "text/html", "");
|
|
||||||
server.sendContent("<a href='/mode?mode=0'>NONE</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=1'>BORDER</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=2'>CLOCK</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=3'>GAME_OF_LIFE_BLACK_WHITE</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=4'>GAME_OF_LIFE_GRAYSCALE</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=5'>GAME_OF_LIFE_COLOR_FADE</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=6'>GAME_OF_LIFE_RANDOM_COLOR</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=7'>PONG</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=8'>SPACE_INVADERS</a><br>");
|
|
||||||
server.sendContent("<a href='/mode?mode=9'>NEW_YEAR</a><br>");
|
|
||||||
server.sendContent("Helligkeit: <a href='/brighter'>+</a> / <a href='/darker'>-</a><br>");
|
|
||||||
server.sendContent("Geschwindigkeit: <a href='/faster'>+</a> / <a href='/slower'>-</a><br>");
|
|
||||||
server.client().flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
void web_setMode() {
|
|
||||||
if (!server.hasArg("mode")) {
|
|
||||||
server.send(400, "text/plain", "Missing 'mode'");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
double value = strtod(server.arg("mode").c_str(), nullptr);
|
|
||||||
if (isnan(value)) {
|
|
||||||
server.send(400, "text/plain", "'mode' not a number");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setMode((ModeId) value);
|
|
||||||
server.sendHeader("location", "/");
|
|
||||||
server.send(301, "text/plain", "ok");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setMode(ModeId value) {
|
|
||||||
if (config.mode == value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
config.mode = value;
|
|
||||||
config_set_dirty();
|
|
||||||
}
|
|
||||||
|
|
||||||
void web_brighter() {
|
|
||||||
setBrightness(display.getBrightness() + 10);
|
|
||||||
server.sendHeader("location", "/");
|
|
||||||
server.send(301, "text/plain", "ok");
|
|
||||||
}
|
|
||||||
|
|
||||||
void web_darker() {
|
|
||||||
setBrightness(max(1, display.getBrightness() - 10));
|
|
||||||
server.sendHeader("location", "/");
|
|
||||||
server.send(301, "text/plain", "ok");
|
|
||||||
}
|
|
||||||
|
|
||||||
void web_faster() {
|
|
||||||
setSpeed(config.speed * 1.1);
|
|
||||||
server.sendHeader("location", "/");
|
|
||||||
server.send(301, "text/plain", "ok");
|
|
||||||
}
|
|
||||||
|
|
||||||
void web_slower() {
|
|
||||||
setSpeed(config.speed / 1.1);
|
|
||||||
server.sendHeader("location", "/");
|
|
||||||
server.send(301, "text/plain", "ok");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.begin(115200);
|
|
||||||
Serial.println("\n\n\nStartup!");
|
|
||||||
|
|
||||||
WiFi.begin("HappyNet", "1Grausame!Sackratte7");
|
|
||||||
yield();
|
|
||||||
|
|
||||||
ArduinoOTA.onStart([]() {
|
|
||||||
Serial.print("\n\nOTA Update: ");
|
|
||||||
display.clear();
|
|
||||||
display.loop();
|
|
||||||
});
|
|
||||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
|
||||||
double ratio = (double) progress / (double) total;
|
|
||||||
Serial.printf("\rOTA Update: %3.0f%%", ratio * 100);
|
|
||||||
|
|
||||||
auto index = (uint16_t) round(ratio * (double) display.pixelCount);
|
|
||||||
auto color = (uint8_t) round(ratio * 255.0);
|
|
||||||
display.setIndex(index, 255 - color, color, 0);
|
|
||||||
display.loop();
|
|
||||||
});
|
|
||||||
ArduinoOTA.onEnd([]() {
|
|
||||||
Serial.println("\nOTA Success!\n");
|
|
||||||
display.clear();
|
|
||||||
display.loop();
|
|
||||||
});
|
|
||||||
ArduinoOTA.onError([](int error) {
|
|
||||||
Serial.println("\nOTA Failure!\n");
|
|
||||||
display.clear();
|
|
||||||
display.loop();
|
|
||||||
});
|
|
||||||
ArduinoOTA.begin();
|
|
||||||
yield();
|
|
||||||
|
|
||||||
|
serial_setup();
|
||||||
config_setup();
|
config_setup();
|
||||||
|
wifi_setup();
|
||||||
|
|
||||||
|
server_setup();
|
||||||
|
|
||||||
display.setup();
|
display.setup();
|
||||||
yield();
|
|
||||||
display.setBrightness(config.brightness);
|
display.setBrightness(config.brightness);
|
||||||
|
|
||||||
server.on("", web_index);
|
|
||||||
server.on("/", web_index);
|
|
||||||
server.on("/mode", web_setMode);
|
|
||||||
server.on("/mode/", web_setMode);
|
|
||||||
server.on("/brighter", web_brighter);
|
|
||||||
server.on("/brighter/", web_brighter);
|
|
||||||
server.on("/darker", web_darker);
|
|
||||||
server.on("/darker/", web_darker);
|
|
||||||
server.on("/faster", web_faster);
|
|
||||||
server.on("/faster/", web_faster);
|
|
||||||
server.on("/slower", web_slower);
|
|
||||||
server.on("/slower/", web_slower);
|
|
||||||
server.begin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
yield();
|
serial_loop();
|
||||||
ArduinoOTA.handle();
|
|
||||||
serial_read();
|
|
||||||
server.handleClient();
|
|
||||||
config_loop();
|
config_loop();
|
||||||
wifi_loop();
|
wifi_loop();
|
||||||
mode_check();
|
|
||||||
|
server_loop();
|
||||||
|
|
||||||
|
mode_loop();
|
||||||
display.loop();
|
display.loop();
|
||||||
mode_step();
|
|
||||||
}
|
|
||||||
|
|
||||||
void timeSyncCallback(struct timeval *tv) {
|
|
||||||
Serial.printf("timeSyncCallback: %ld\n", tv->tv_sec);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wifi_loop() {
|
|
||||||
bool hasIp = (uint32_t) WiFi.localIP() != 0;
|
|
||||||
if (!connected) {
|
|
||||||
if (hasIp) {
|
|
||||||
connected = true;
|
|
||||||
Serial.printf("WiFi connected: %s\n", WiFi.localIP().toString().c_str());
|
|
||||||
ntp_setup();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!hasIp) {
|
|
||||||
connected = false;
|
|
||||||
Serial.println("WiFi disconnected!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t ip2int(const IPAddress &ip) {
|
|
||||||
return ((ip[0] * 256 + ip[1]) * 256 + ip[2]) * 256 + ip[3];
|
|
||||||
}
|
|
||||||
|
|
||||||
void ntp_setup() {
|
|
||||||
char calculatedGateway[16] = {0};
|
|
||||||
uint32_t local = ip2int(WiFi.localIP());
|
|
||||||
uint32_t netmask = ip2int(WiFi.subnetMask());
|
|
||||||
uint32_t gateway = local & netmask + 1;
|
|
||||||
snprintf(
|
|
||||||
calculatedGateway,
|
|
||||||
sizeof(calculatedGateway),
|
|
||||||
"%u.%u.%u.%u",
|
|
||||||
(gateway >> 24) & 0xFF,
|
|
||||||
(gateway >> 16) & 0xFF,
|
|
||||||
(gateway >> 8) & 0xFF,
|
|
||||||
(gateway >> 0) & 0xFF
|
|
||||||
);
|
|
||||||
sntp_set_time_sync_notification_cb(timeSyncCallback);
|
|
||||||
Serial.printf("configTime(%s / %s / %s)\n", WiFi.gatewayIP().toString().c_str(), calculatedGateway, "pool.ntp.org");
|
|
||||||
configTime(3600, 3600, WiFi.gatewayIP().toString().c_str(), calculatedGateway, "pool.ntp.org");
|
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
void serial_read() {
|
|
||||||
if (Serial.available()) {
|
|
||||||
int input = Serial.read();
|
|
||||||
switch (input) {
|
|
||||||
case '0':
|
|
||||||
case '1':
|
|
||||||
case '2':
|
|
||||||
case '3':
|
|
||||||
case '4':
|
|
||||||
case '5':
|
|
||||||
case '6':
|
|
||||||
case '7':
|
|
||||||
case '8':
|
|
||||||
case '9':
|
|
||||||
setMode((ModeId) (input - '0'));
|
|
||||||
break;
|
|
||||||
case '+':
|
|
||||||
setBrightness(display.getBrightness() + 10);
|
|
||||||
break;
|
|
||||||
case '-':
|
|
||||||
setBrightness(max(1, display.getBrightness() - 10));
|
|
||||||
break;
|
|
||||||
case ',':
|
|
||||||
setSpeed(config.speed / 1.1);
|
|
||||||
break;
|
|
||||||
case '.':
|
|
||||||
setSpeed(config.speed * 1.1);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Serial.printf("Unknown command: %c\n", input);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setBrightness(int brightness) {
|
|
||||||
brightness = max(0, min(brightness, 255));
|
|
||||||
if (display.getBrightness() == brightness) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
config.brightness = brightness;
|
|
||||||
display.setBrightness(brightness);
|
|
||||||
config_set_dirty();
|
|
||||||
Serial.printf("Setting brightness to %5.1f%%\n", brightness / 2.55);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setSpeed(double speed) {
|
|
||||||
speed = min(max(0.01, speed), 10000.0);
|
|
||||||
if (config.speed == speed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
config.speed = speed;
|
|
||||||
config_set_dirty();
|
|
||||||
Serial.printf("Setting speed to %6.2fx\n", config.speed);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mode_check() {
|
|
||||||
if (currentModeId != config.mode) {
|
|
||||||
unloadOldMode();
|
|
||||||
loadNewMode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void unloadOldMode() {
|
|
||||||
if (mode != nullptr) {
|
|
||||||
delete mode;
|
|
||||||
mode = nullptr;
|
|
||||||
}
|
|
||||||
display.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loadNewMode() {
|
|
||||||
currentModeId = config.mode;
|
|
||||||
lastMicros = 0;
|
|
||||||
switch (currentModeId) {
|
|
||||||
case BORDER:
|
|
||||||
mode = new Border(&display);
|
|
||||||
break;
|
|
||||||
case CLOCK:
|
|
||||||
mode = new Clock(&display);
|
|
||||||
break;
|
|
||||||
case GAME_OF_LIFE_BLACK_WHITE:
|
|
||||||
mode = new GameOfLife(&display, BLACK_WHITE);
|
|
||||||
break;
|
|
||||||
case GAME_OF_LIFE_GRAYSCALE:
|
|
||||||
mode = new GameOfLife(&display, GRAYSCALE);
|
|
||||||
break;
|
|
||||||
case GAME_OF_LIFE_COLOR_FADE:
|
|
||||||
mode = new GameOfLife(&display, COLOR_FADE);
|
|
||||||
break;
|
|
||||||
case GAME_OF_LIFE_RANDOM_COLOR:
|
|
||||||
mode = new GameOfLife(&display, RANDOM_COLOR);
|
|
||||||
break;
|
|
||||||
case PONG:
|
|
||||||
mode = new Pong(&display);
|
|
||||||
break;
|
|
||||||
case SPACE_INVADERS:
|
|
||||||
mode = new SpaceInvaders(&display);
|
|
||||||
break;
|
|
||||||
case NEW_YEAR:
|
|
||||||
mode = new NewYear(&display);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Serial.print("No mode loaded.\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Serial.printf("Mode: %s\n", mode == nullptr ? "None" : mode->getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
void mode_step() {
|
|
||||||
if (mode == nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
auto currentMicros = (int64_t) micros();
|
|
||||||
microseconds_t dt = (microseconds_t) min(1000000.0, max(1.0, (double) (currentMicros - lastMicros) * config.speed));
|
|
||||||
lastMicros = currentMicros;
|
|
||||||
mode->step(dt);
|
|
||||||
}
|
|
||||||
|
|||||||
103
src/mode.cpp
Normal file
103
src/mode.cpp
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
#include "mode.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "mode/Border/Border.h"
|
||||||
|
#include "mode/Clock/Clock.h"
|
||||||
|
#include "mode/GameOfLife/GameOfLife.h"
|
||||||
|
#include "mode/Pong/Pong.h"
|
||||||
|
#include "mode/SpaceInvaders/SpaceInvaders.h"
|
||||||
|
#include "mode/NewYear/NewYear.h"
|
||||||
|
#include "display.h"
|
||||||
|
|
||||||
|
ModeId currentModeId = NONE;
|
||||||
|
|
||||||
|
microseconds_t lastMicros = 0;
|
||||||
|
|
||||||
|
ModeBase *mode = nullptr;
|
||||||
|
|
||||||
|
void unloadOldMode();
|
||||||
|
|
||||||
|
void loadNewMode();
|
||||||
|
|
||||||
|
void mode_step();
|
||||||
|
|
||||||
|
void mode_loop() {
|
||||||
|
if (currentModeId != config.mode) {
|
||||||
|
unloadOldMode();
|
||||||
|
loadNewMode();
|
||||||
|
}
|
||||||
|
mode_step();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setMode(ModeId value) {
|
||||||
|
if (config.mode == value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
config.mode = value;
|
||||||
|
config_set_dirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setSpeed(double speed) {
|
||||||
|
speed = min(max(0.01, speed), 10000.0);
|
||||||
|
if (config.speed == speed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
config.speed = speed;
|
||||||
|
config_set_dirty();
|
||||||
|
Serial.printf("Setting speed to %6.2fx\n", config.speed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unloadOldMode() {
|
||||||
|
if (mode != nullptr) {
|
||||||
|
delete mode;
|
||||||
|
mode = nullptr;
|
||||||
|
}
|
||||||
|
display.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadNewMode() {
|
||||||
|
currentModeId = config.mode;
|
||||||
|
lastMicros = 0;
|
||||||
|
switch (currentModeId) {
|
||||||
|
case BORDER:
|
||||||
|
mode = new Border(&display);
|
||||||
|
break;
|
||||||
|
case CLOCK:
|
||||||
|
mode = new Clock(&display);
|
||||||
|
break;
|
||||||
|
case GAME_OF_LIFE_BLACK_WHITE:
|
||||||
|
mode = new GameOfLife(&display, BLACK_WHITE);
|
||||||
|
break;
|
||||||
|
case GAME_OF_LIFE_GRAYSCALE:
|
||||||
|
mode = new GameOfLife(&display, GRAYSCALE);
|
||||||
|
break;
|
||||||
|
case GAME_OF_LIFE_COLOR_FADE:
|
||||||
|
mode = new GameOfLife(&display, COLOR_FADE);
|
||||||
|
break;
|
||||||
|
case GAME_OF_LIFE_RANDOM_COLOR:
|
||||||
|
mode = new GameOfLife(&display, RANDOM_COLOR);
|
||||||
|
break;
|
||||||
|
case PONG:
|
||||||
|
mode = new Pong(&display);
|
||||||
|
break;
|
||||||
|
case SPACE_INVADERS:
|
||||||
|
mode = new SpaceInvaders(&display);
|
||||||
|
break;
|
||||||
|
case NEW_YEAR:
|
||||||
|
mode = new NewYear(&display);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Serial.print("No mode loaded.\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Serial.printf("Mode: %s\n", mode == nullptr ? "None" : mode->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
void mode_step() {
|
||||||
|
if (mode == nullptr) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto currentMicros = (int64_t) micros();
|
||||||
|
microseconds_t dt = (microseconds_t) min(1000000.0, max(1.0, (double) (currentMicros - lastMicros) * config.speed));
|
||||||
|
lastMicros = currentMicros;
|
||||||
|
mode->step(dt);
|
||||||
|
}
|
||||||
12
src/mode.h
Normal file
12
src/mode.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef MEDIATABLE_MODE_H
|
||||||
|
#define MEDIATABLE_MODE_H
|
||||||
|
|
||||||
|
#include "mode/Mode.h"
|
||||||
|
|
||||||
|
void mode_loop();
|
||||||
|
|
||||||
|
void setMode(ModeId value);
|
||||||
|
|
||||||
|
void setSpeed(double speed);
|
||||||
|
|
||||||
|
#endif
|
||||||
45
src/serial.cpp
Normal file
45
src/serial.cpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include "serial.h"
|
||||||
|
#include "display.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "mode.h"
|
||||||
|
|
||||||
|
void serial_setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.println("\n\n\nStartup!");
|
||||||
|
}
|
||||||
|
|
||||||
|
void serial_loop() {
|
||||||
|
if (Serial.available()) {
|
||||||
|
int input = Serial.read();
|
||||||
|
switch (input) {
|
||||||
|
case '0':
|
||||||
|
case '1':
|
||||||
|
case '2':
|
||||||
|
case '3':
|
||||||
|
case '4':
|
||||||
|
case '5':
|
||||||
|
case '6':
|
||||||
|
case '7':
|
||||||
|
case '8':
|
||||||
|
case '9':
|
||||||
|
setMode((ModeId)(input - '0'));
|
||||||
|
break;
|
||||||
|
case '+':
|
||||||
|
setBrightness(display.getBrightness() + 10);
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
setBrightness(display.getBrightness() - 10);
|
||||||
|
break;
|
||||||
|
case ',':
|
||||||
|
setSpeed(config.speed / 1.1);
|
||||||
|
break;
|
||||||
|
case '.':
|
||||||
|
setSpeed(config.speed * 1.1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Serial.printf("Unknown command: %c\n", input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/serial.h
Normal file
8
src/serial.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef MEDIATABLE_SERIAL_H
|
||||||
|
#define MEDIATABLE_SERIAL_H
|
||||||
|
|
||||||
|
void serial_loop();
|
||||||
|
|
||||||
|
void serial_setup();
|
||||||
|
|
||||||
|
#endif
|
||||||
98
src/server.cpp
Normal file
98
src/server.cpp
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
#include <WebServer.h>
|
||||||
|
#include <cmath>
|
||||||
|
#include "server.h"
|
||||||
|
#include "mode/Mode.h"
|
||||||
|
#include "mode.h"
|
||||||
|
#include "display.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
WebServer server(80);
|
||||||
|
|
||||||
|
void web_index();
|
||||||
|
|
||||||
|
void web_setMode();
|
||||||
|
|
||||||
|
void web_brighter();
|
||||||
|
|
||||||
|
void web_darker();
|
||||||
|
|
||||||
|
void web_faster();
|
||||||
|
|
||||||
|
void web_slower();
|
||||||
|
|
||||||
|
void server_setup() {
|
||||||
|
server.on("", web_index);
|
||||||
|
server.on("/", web_index);
|
||||||
|
server.on("/mode", web_setMode);
|
||||||
|
server.on("/mode/", web_setMode);
|
||||||
|
server.on("/brighter", web_brighter);
|
||||||
|
server.on("/brighter/", web_brighter);
|
||||||
|
server.on("/darker", web_darker);
|
||||||
|
server.on("/darker/", web_darker);
|
||||||
|
server.on("/faster", web_faster);
|
||||||
|
server.on("/faster/", web_faster);
|
||||||
|
server.on("/slower", web_slower);
|
||||||
|
server.on("/slower/", web_slower);
|
||||||
|
server.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void server_loop() {
|
||||||
|
server.handleClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
void web_index() {
|
||||||
|
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
||||||
|
server.send(200, "text/html", "");
|
||||||
|
server.sendContent("<a href='/mode?mode=0'>NONE</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=1'>BORDER</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=2'>CLOCK</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=3'>GAME_OF_LIFE_BLACK_WHITE</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=4'>GAME_OF_LIFE_GRAYSCALE</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=5'>GAME_OF_LIFE_COLOR_FADE</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=6'>GAME_OF_LIFE_RANDOM_COLOR</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=7'>PONG</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=8'>SPACE_INVADERS</a><br>");
|
||||||
|
server.sendContent("<a href='/mode?mode=9'>NEW_YEAR</a><br>");
|
||||||
|
server.sendContent("Helligkeit: <a href='/brighter'>+</a> / <a href='/darker'>-</a><br>");
|
||||||
|
server.sendContent("Geschwindigkeit: <a href='/faster'>+</a> / <a href='/slower'>-</a><br>");
|
||||||
|
server.client().flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void web_setMode() {
|
||||||
|
if (!server.hasArg("mode")) {
|
||||||
|
server.send(400, "text/plain", "Missing 'mode'");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
double value = strtod(server.arg("mode").c_str(), nullptr);
|
||||||
|
if (isnan(value)) {
|
||||||
|
server.send(400, "text/plain", "'mode' not a number");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setMode((ModeId) value);
|
||||||
|
server.sendHeader("location", "/");
|
||||||
|
server.send(301, "text/plain", "ok");
|
||||||
|
}
|
||||||
|
|
||||||
|
void web_brighter() {
|
||||||
|
setBrightness(display.getBrightness() + 10);
|
||||||
|
server.sendHeader("location", "/");
|
||||||
|
server.send(301, "text/plain", "ok");
|
||||||
|
}
|
||||||
|
|
||||||
|
void web_darker() {
|
||||||
|
setBrightness(max(1, display.getBrightness() - 10));
|
||||||
|
server.sendHeader("location", "/");
|
||||||
|
server.send(301, "text/plain", "ok");
|
||||||
|
}
|
||||||
|
|
||||||
|
void web_faster() {
|
||||||
|
setSpeed(config.speed * 1.1);
|
||||||
|
server.sendHeader("location", "/");
|
||||||
|
server.send(301, "text/plain", "ok");
|
||||||
|
}
|
||||||
|
|
||||||
|
void web_slower() {
|
||||||
|
setSpeed(config.speed / 1.1);
|
||||||
|
server.sendHeader("location", "/");
|
||||||
|
server.send(301, "text/plain", "ok");
|
||||||
|
}
|
||||||
8
src/server.h
Normal file
8
src/server.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef MEDIATABLE_SERVER_H
|
||||||
|
#define MEDIATABLE_SERVER_H
|
||||||
|
|
||||||
|
void server_setup();
|
||||||
|
|
||||||
|
void server_loop();
|
||||||
|
|
||||||
|
#endif
|
||||||
98
src/wifi.cpp
Normal file
98
src/wifi.cpp
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
#include "wifi.h"
|
||||||
|
#include "display.h"
|
||||||
|
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
#include <esp_sntp.h>
|
||||||
|
|
||||||
|
bool connected = false;
|
||||||
|
|
||||||
|
void ntp_setup();
|
||||||
|
|
||||||
|
uint32_t ip2int(const IPAddress &ip);
|
||||||
|
|
||||||
|
void timeSyncCallback(struct timeval *tv);
|
||||||
|
|
||||||
|
char *calculateGateway(char *calculatedGateway, size_t size);
|
||||||
|
|
||||||
|
void wifi_setup() {
|
||||||
|
WiFi.begin("HappyNet", "1Grausame!Sackratte7");
|
||||||
|
yield();
|
||||||
|
|
||||||
|
ArduinoOTA.onStart([]() {
|
||||||
|
Serial.print("\n\nOTA Update: ");
|
||||||
|
display.clear();
|
||||||
|
display.loop();
|
||||||
|
});
|
||||||
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||||
|
double ratio = (double) progress / (double) total;
|
||||||
|
Serial.printf("\rOTA Update: %3.0f%%", ratio * 100);
|
||||||
|
|
||||||
|
auto index = (uint16_t) round(ratio * (double) display.pixelCount);
|
||||||
|
auto color = (uint8_t) round(ratio * 255.0);
|
||||||
|
display.setIndex(index, 255 - color, color, 0);
|
||||||
|
display.loop();
|
||||||
|
});
|
||||||
|
ArduinoOTA.onEnd([]() {
|
||||||
|
Serial.println("\nOTA Success!\n");
|
||||||
|
display.clear();
|
||||||
|
display.loop();
|
||||||
|
});
|
||||||
|
ArduinoOTA.onError([](int error) {
|
||||||
|
Serial.println("\nOTA Failure!\n");
|
||||||
|
display.clear();
|
||||||
|
display.loop();
|
||||||
|
});
|
||||||
|
ArduinoOTA.begin();
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
void wifi_loop() {
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
bool hasIp = (uint32_t) WiFi.localIP() != 0;
|
||||||
|
if (!connected) {
|
||||||
|
if (hasIp) {
|
||||||
|
connected = true;
|
||||||
|
Serial.printf("WiFi connected: %s\n", WiFi.localIP().toString().c_str());
|
||||||
|
ntp_setup();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!hasIp) {
|
||||||
|
connected = false;
|
||||||
|
Serial.println("WiFi disconnected!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ntp_setup() {
|
||||||
|
char calculatedGateway[16] = {0};
|
||||||
|
calculateGateway(calculatedGateway, sizeof(calculatedGateway));
|
||||||
|
sntp_set_time_sync_notification_cb(timeSyncCallback);
|
||||||
|
Serial.printf("configTime(%s / %s / %s)\n", WiFi.gatewayIP().toString().c_str(), calculatedGateway, "pool.ntp.org");
|
||||||
|
configTime(3600, 3600, WiFi.gatewayIP().toString().c_str(), calculatedGateway, "pool.ntp.org");
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
char *calculateGateway(char *calculatedGateway, size_t size) {
|
||||||
|
uint32_t local = ip2int(WiFi.localIP());
|
||||||
|
uint32_t netmask = ip2int(WiFi.subnetMask());
|
||||||
|
uint32_t gateway = local & netmask + 1;
|
||||||
|
snprintf(
|
||||||
|
calculatedGateway,
|
||||||
|
size,
|
||||||
|
"%u.%u.%u.%u",
|
||||||
|
(gateway >> 24) & 0xFF,
|
||||||
|
(gateway >> 16) & 0xFF,
|
||||||
|
(gateway >> 8) & 0xFF,
|
||||||
|
(gateway >> 0) & 0xFF
|
||||||
|
);
|
||||||
|
return calculatedGateway;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t ip2int(const IPAddress &ip) {
|
||||||
|
return ((ip[0] * 256 + ip[1]) * 256 + ip[2]) * 256 + ip[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
void timeSyncCallback(struct timeval *tv) {
|
||||||
|
Serial.printf("timeSyncCallback: %ld\n", tv->tv_sec);
|
||||||
|
}
|
||||||
8
src/wifi.h
Normal file
8
src/wifi.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef MEDIATABLE_WIFI_H
|
||||||
|
#define MEDIATABLE_WIFI_H
|
||||||
|
|
||||||
|
void wifi_setup();
|
||||||
|
|
||||||
|
void wifi_loop();
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user