pass scheduler to app

This commit is contained in:
2018-06-02 19:30:20 +02:00
parent 392e39734c
commit e0cb8a9583
13 changed files with 328 additions and 11 deletions

35
.vscode/settings.json vendored
View File

@@ -2,5 +2,40 @@
"terminal.integrated.env.linux": {
"PATH": "/home/master/.platformio/penv/bin:/home/master/.platformio/penv:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl",
"PLATFORMIO_CALLER": "vscode"
},
"files.associations": {
"array": "cpp",
"deque": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"initializer_list": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"numeric": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"type_traits": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"utility": "cpp"
}
}

18
library.json Normal file
View File

@@ -0,0 +1,18 @@
{
"name": "sprocket-core",
"keywords": "esp8266, sprocket, stack",
"description": "Core stack for sprockets",
"authors":
{
"name": "Patrick Balsiger",
"email": "frupii@gmail.com",
"url": "https://gitlab.com/0x1d"
},
"repository":
{
"type": "git",
"url": "https://gitlab.com/wirelos/sprocket-core/"
},
"frameworks": "arduino",
"platforms": "espressif8266"
}

View File

@@ -8,7 +8,28 @@
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html
[env:esp12e]
[platformio]
env_default = build
[common]
framework = arduino
lib_deps =
ESPAsyncWifiManager
ESP Async WebServer
ESPAsyncTCP
TaskScheduler
; painlessMesh
[env:build]
#platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
#platform = https://github.com/platformio/platform-espressif8266.git
#platform = espressif8266@~1.6.0
platform = espressif8266
board = esp12e
framework = arduino
upload_speed = 921600
monitor_baud = 115200
framework = ${common.framework}
lib_deps = ${common.lib_deps}
#build_flags = -DLED_PIN=2 -g
;upload_port = /dev/ttyUSB0
;upload_port = 192.168.1.168

28
src/App.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef _APP_H_
#define _APP_H_
#include <TaskSchedulerDeclarations.h>
//#include "Sprocket.h"
#include "AppStack.h"
/* template <class T>
class App {
private:
T* stack;
public:
App();
App(T* stack);
};
*/
class App {
protected:
//Sprocket* sprocket;
public:
/* App(Sprocket* sprkt){
sprocket = sprkt;
} */
virtual void activate(Scheduler* scheduler);
};
#endif

12
src/AppStack.cpp Normal file
View File

@@ -0,0 +1,12 @@
#include "AppStack.h"
AppStack::AppStack(){
}
void AppStack::begin(){
}
void AppStack::loop(){
}

15
src/AppStack.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef __APPSTACK_H_INCLUDED__
#define __APPSTACK_H_INCLUDED__
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
//#include <TaskSchedulerDeclarations.h>
class AppStack {
public:
AppStack();
void begin();
void loop();
};
#endif

24
src/Sprocket.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include "Sprocket.h"
Sprocket::Sprocket(){
Serial.println("init sprocket");
}
Sprocket* Sprocket::use(AppStack* stk){
stack = stk;
return this;
}
Sprocket* Sprocket::addTask(Task& tsk){
scheduler.addTask(tsk);
tsk.enable();
}
Sprocket* Sprocket::registerApp(App& app){
app.activate(&scheduler);
}
void Sprocket::loop(){
scheduler.execute();
//stack->loop();
}

25
src/Sprocket.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef __SPROCKET_H__
#define __SPROCKET_H__
#include <TaskSchedulerDeclarations.h>
#include <Arduino.h>
#include "WiFiNet.h"
#include "AppStack.h"
#include "App.h"
class Sprocket {
private:
AppStack* stack;
Scheduler scheduler;
public:
Sprocket();
Sprocket* use(AppStack* stack);
Sprocket* addTask(Task& tsk);
Sprocket* registerApp(App& app);
void loop();
};
#endif

35
src/WebStack.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef __WEBSTACK_H_INCLUDED__
#define __WEBSTACK_H_INCLUDED__
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <AppStack.h>
class WebStack : public AppStack {
public:
AsyncWebServer* server;
AsyncWebSocket* socket;
WebStack() : AppStack(){
server = new AsyncWebServer(80);
initWebServer();
}
WebStack(AsyncWebServer* webServer) : AppStack(){
server = webServer;
initWebServer();
}
WebStack(AsyncWebServer* webServer, AsyncWebSocket* webSocket) : AppStack(){
server = webServer;
socket = webSocket;
initWebServer();
}
void initWebServer(){
//server->serveStatic(WEBSERVER_ROOT, SPIFFS, WEBSERVER_HTDOCS).setDefaultFile(WEBSERVER_DEFAULT_FILE);
//server->on(WEBCONFIG_GET_HEAP, HTTP_GET, HTTP_GET_HEAP);
}
void begin() {
server->begin();
AppStack::begin();
}
};
#endif

49
src/WiFiNet.h Normal file
View File

@@ -0,0 +1,49 @@
#ifndef __WIFI_NET_H__
#define __WIFI_NET_H__
#if defined(ESP8266)
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <ESP8266mDNS.h>
#else
#include <WiFi.h>
#endif
#include <WebStack.h>
#include <DNSServer.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
class WiFiNet {
private:
AsyncWiFiManager* wifiManager;
AsyncWebServer* server;
DNSServer* dns;
const char* hostName = "foo";
public:
WiFiNet(){
//server = new AsyncWebServer(80);
dns = new DNSServer();
}
WiFiNet* use(WebStack* stack) {
server = stack->server;
return this;
}
void connect(/* char const *apName, char const *apPassword */){
wifiManager = new AsyncWiFiManager(server, dns);
wifiManager->autoConnect(/* apName, apPassword */);
Serial.println("Hostname: " + String(hostName));
WiFi.hostname(hostName);
startMDNS();
}
void startMDNS(){
if (!MDNS.begin(hostName)) {
Serial.println("Error setting up MDNS responder!");
} else {
Serial.println("mDNS responder started");
MDNS.addService("http", "tcp", 80);
}
}
};
#endif

24
src/examples/ExampleApp.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef __EXAMPLE_APP__
#define __EXAMPLE_APP__
//#include "Sprocket.h"
#include <TaskSchedulerDeclarations.h>
#include "App.h"
class ExampleApp : public App {
public:
Task someTask;
ExampleApp() /* App(sprkt) */ {
Serial.println("joo");
}
void activate(Scheduler* scheduler) {
Serial.println("activate");
someTask.set(TASK_SECOND, TASK_FOREVER, [this](){
Serial.println("do stuff in task");
});
scheduler->addTask(someTask);
someTask.enable();
}
};
#endif

View File

@@ -0,0 +1,40 @@
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#include <TaskScheduler.h>
#include "Sprocket.h"
#include "AppStack.h"
//#include "WiFiNet.h"
#include "ExampleApp.h"
#define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 3000
Sprocket sprocket;
AppStack stack;
WiFiNet net;
ExampleApp app;
void setup() {
Serial.begin(SERIAL_BAUD_RATE);
SPIFFS.begin();
delay(STARTUP_DELAY);
Serial.println("setup");
//net.use(&stack)->connect();
//sprocket.use(&stack);
sprocket.registerApp(app);
//stack.begin();
}
void loop() {
sprocket.loop();
yield();
}

View File

@@ -1,9 +0,0 @@
#include <Arduino.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}