pot plugin w. example

This commit is contained in:
2018-11-17 02:45:54 +01:00
parent f79f869d55
commit be17cabfdc
85 changed files with 7864 additions and 103 deletions

12
src/GpioConfig.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef __GPIO_CONFIG_
#define __GPIO_CONFIG_
struct GpioConfig
{
int pin;
int threshold;
int pollInterval;
const char *topic;
};
#endif

23
src/PotPlugin.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "PotPlugin.h"
PotPlugin::PotPlugin(GpioConfig cfg){
config = cfg;
}
void PotPlugin::activate(Scheduler *userScheduler)
{
inputTask.set(TASK_MILLISECOND * config.pollInterval, TASK_FOREVER, std::bind(&PotPlugin::checkInput, this));
userScheduler->addTask(inputTask);
inputTask.enable();
PRINT_MSG(Serial, "PLUGIN", "PotPlugin activated");
}
void PotPlugin::checkInput()
{
// FIXME add range and threashold to config
int newVal = analogRead(config.pin);
if ((newVal >= currentVal + config.threshold || newVal <= currentVal - config.threshold))
{
publish(config.topic, String(newVal));
currentVal = newVal;
}
}

29
src/PotPlugin.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef __POT_PLUGIN_H__
#define __POT_PLUGIN_H__
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#include <functional>
#include <vector>
#include <FS.h>
#include <TaskSchedulerDeclarations.h>
#include <Plugin.h>
#include <utils/print.h>
#include <utils/misc.h>
#include <GpioConfig.h>
using namespace std;
using namespace std::placeholders;
class PotPlugin : public Plugin {
public:
Task inputTask;
int currentVal = 0;
GpioConfig config;
PotPlugin(GpioConfig cfg);
void activate(Scheduler* userScheduler);
void checkInput();
};
#endif

View File

@@ -1,11 +0,0 @@
#include "TemplatePlugin.h"
TemplatePlugin::TemplatePlugin(TemplateConfig cfg)
{
templateConfig = cfg;
}
void TemplatePlugin::activate(Scheduler *scheduler)
{
PRINT_MSG(Serial, "TEMPLATE", "plugin activated");
}

View File

@@ -1,26 +0,0 @@
#ifndef __TEMPLATE_PLUGIN__
#define __TEMPLATE_PLUGIN__
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#include <Plugin.h>
#include "utils/utils_print.h"
using namespace std;
using namespace std::placeholders;
struct TemplateConfig
{
const char *var;
};
class TemplatePlugin : public Plugin
{
public:
TemplateConfig templateConfig;
TemplatePlugin(TemplateConfig cfg);
void activate(Scheduler *scheduler);
};
#endif

View File

@@ -1,24 +0,0 @@
#ifndef __DEVICE_CONFIG__
#define __DEVICE_CONFIG__
// Scheduler config
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#define _TASK_PRIORITY
// Chip config
#define SPROCKET_TYPE "SPROCKET"
#define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 1000
// network config
#define SPROCKET_MODE 1
#define WIFI_CHANNEL 11
#define AP_SSID "sprocket"
#define AP_PASSWORD "th3r31sn0sp00n"
#define STATION_SSID "MyAP"
#define STATION_PASSWORD "th3r31sn0sp00n"
#define HOSTNAME "sprocket"
#define CONNECT_TIMEOUT 10000
#endif

View File

@@ -1,31 +0,0 @@
#include "config.h"
#include "WiFiNet.h"
#include "Sprocket.h"
#include "TemplatePlugin.h"
WiFiNet *network;
Sprocket *sprocket;
void setup()
{
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
sprocket->addPlugin(new TemplatePlugin({"sprocket"}));
network = new WiFiNet(
SPROCKET_MODE,
STATION_SSID,
STATION_PASSWORD,
AP_SSID,
AP_PASSWORD,
HOSTNAME,
CONNECT_TIMEOUT);
network->connect();
sprocket->activate();
}
void loop()
{
sprocket->loop();
yield();
}

18
src/examples/pot/config.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef __DEVICE_CONFIG__
#define __DEVICE_CONFIG__
// Scheduler
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#define _TASK_PRIORITY
// Chip
#define SPROCKET_TYPE "SPROCKET"
#define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 1000
// Pot
#define POT_THRESHOLD 16
#define POT_POLL_INTERVAL 50
#endif

30
src/examples/pot/main.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "config.h"
#include "Sprocket.h"
#include "PotPlugin.h"
Sprocket *sprocket;
void registerPot(Sprocket* s, int pin, const char* topic)
{
s->addPlugin(
new PotPlugin({pin, POT_THRESHOLD, POT_POLL_INTERVAL, topic}));
s->subscribe(topic, bind([](String label, String val){
PRINT_MSG(Serial, label.c_str(), val.c_str());
}, topic, _1));
}
void setup()
{
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
registerPot(sprocket, 36, "pot1");
registerPot(sprocket, 37, "pot2");
registerPot(sprocket, 38, "pot3");
registerPot(sprocket, 39, "pot4");
sprocket->activate();
}
void loop()
{
sprocket->loop();
yield();
}