rename pot to analog as it is quite a generic way to read the input

This commit is contained in:
2018-11-17 11:30:46 +01:00
parent 816963240d
commit baafaa441a
6 changed files with 26 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
;[platformio] ;[platformio]
;env_default = Pot, Pot_esp32 ;env_default = analog
[common] [common]
framework = arduino framework = arduino
@@ -15,25 +15,25 @@ lib_deps =
https://gitlab.com/wirelos/sprocket-lib.git#develop https://gitlab.com/wirelos/sprocket-lib.git#develop
[env:pot] [env:analog]
platform = ${common.platform} platform = ${common.platform}
board = ${common.board} board = ${common.board}
framework = ${common.framework} framework = ${common.framework}
upload_speed = ${common.upload_speed} upload_speed = ${common.upload_speed}
monitor_baud = ${common.monitor_baud} monitor_baud = ${common.monitor_baud}
src_filter = +<*> -<examples/> +<examples/pot/> -<inputs/> +<inputs/pot> src_filter = +<*> -<examples/> +<examples/analog/> -<inputs/> +<inputs/analog>
lib_deps = ${common.lib_deps} lib_deps = ${common.lib_deps}
lib_ignore = lib_ignore =
Ai Esp32 Rotary Encoder Ai Esp32 Rotary Encoder
[env:pot_esp32] [env:analog_esp32]
platform = espressif32 platform = espressif32
board = esp32dev board = esp32dev
framework = ${common.framework} framework = ${common.framework}
build_flags = -std=c++14 build_flags = -std=c++14
upload_speed = ${common.upload_speed} upload_speed = ${common.upload_speed}
monitor_baud = ${common.monitor_baud} monitor_baud = ${common.monitor_baud}
src_filter = +<*> -<examples/> +<examples/pot/> -<inputs/> +<inputs/pot> src_filter = +<*> -<examples/> +<examples/analog/> -<inputs/> +<inputs/analog>
lib_deps = ${common.lib_deps} lib_deps = ${common.lib_deps}
lib_ignore = lib_ignore =
Ai Esp32 Rotary Encoder Ai Esp32 Rotary Encoder
@@ -56,6 +56,6 @@ framework = ${common.framework}
build_flags = -std=c++14 build_flags = -std=c++14
upload_speed = ${common.upload_speed} upload_speed = ${common.upload_speed}
monitor_baud = ${common.monitor_baud} monitor_baud = ${common.monitor_baud}
src_filter = +<*> +<inputs/pot> +<inputs/rotary> -<examples/> +<examples/combined/> src_filter = +<*> +<inputs/analog> +<inputs/rotary> -<examples/> +<examples/combined/>
lib_deps = ${common.lib_deps} lib_deps = ${common.lib_deps}
Ai Esp32 Rotary Encoder Ai Esp32 Rotary Encoder

View File

@@ -1,13 +1,13 @@
#include "config.h" #include "config.h"
#include "Sprocket.h" #include "Sprocket.h"
#include "inputs/pot/PotPlugin.h" #include "inputs/analog/AnalogInputPlugin.h"
Sprocket *sprocket; Sprocket *sprocket;
void registerPot(Sprocket* s, int pin, const char* topic) void registerPot(Sprocket* s, int pin, const char* topic)
{ {
s->addPlugin( s->addPlugin(
new PotPlugin({pin, POT_THRESHOLD, POT_POLL_INTERVAL, topic})); new AnalogInputPlugin({pin, POT_THRESHOLD, POT_POLL_INTERVAL, topic}));
s->subscribe(topic, bind([](String label, String val){ s->subscribe(topic, bind([](String label, String val){
PRINT_MSG(Serial, label.c_str(), val.c_str()); PRINT_MSG(Serial, label.c_str(), val.c_str());
}, topic, _1)); }, topic, _1));

View File

@@ -1,11 +1,11 @@
#include "config.h" #include "config.h"
#include "Sprocket.h" #include "Sprocket.h"
#include "inputs/rotary/RotaryPlugin.h" #include "inputs/rotary/RotaryPlugin.h"
#include "inputs/pot/PotPlugin.h" #include "inputs/analog/AnalogInputPlugin.h"
Sprocket *sprocket; Sprocket *sprocket;
RotaryPlugin *r1; RotaryPlugin *r1;
PotPlugin* p1; AnalogInputPlugin* p1;
void addRotary(Sprocket *s, Plugin *r, const char *topic) void addRotary(Sprocket *s, Plugin *r, const char *topic)
{ {
@@ -15,13 +15,12 @@ void addRotary(Sprocket *s, Plugin *r, const char *topic)
s->addPlugin(r); s->addPlugin(r);
} }
void addPot(Sprocket* s, int pin, const char* topic) void addAnalogInput(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){ s->subscribe(topic, bind([](String label, String val){
PRINT_MSG(Serial, label.c_str(), val.c_str()); PRINT_MSG(Serial, label.c_str(), val.c_str());
}, topic, _1)); }, topic, _1));
s->addPlugin(new AnalogInputPlugin({pin, POT_THRESHOLD, POT_POLL_INTERVAL, topic}));
} }
void setup() void setup()
@@ -30,10 +29,10 @@ void setup()
r1 = new RotaryPlugin({35, 34, 21, -1, 50, 0, 255, true, "rotary1"}); r1 = new RotaryPlugin({35, 34, 21, -1, 50, 0, 255, true, "rotary1"});
r1->rotaryEncoder->setup([] { r1->rotaryEncoder->readEncoder_ISR(); }); r1->rotaryEncoder->setup([] { r1->rotaryEncoder->readEncoder_ISR(); });
addRotary(sprocket, r1, "rotary1"); addRotary(sprocket, r1, "rotary1");
addPot(sprocket, 36, "pot1"); addAnalogInput(sprocket, 36, "pot1");
addPot(sprocket, 37, "pot2"); addAnalogInput(sprocket, 37, "pot2");
addPot(sprocket, 38, "pot3"); addAnalogInput(sprocket, 38, "pot3");
addPot(sprocket, 39, "pot4"); addAnalogInput(sprocket, 39, "pot4");
sprocket->activate(); sprocket->activate();
} }

View File

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

View File

@@ -1,5 +1,5 @@
#ifndef __POT_PLUGIN_H__ #ifndef __ANALOG_INPUT__PLUGIN_H__
#define __POT_PLUGIN_H__ #define __ANALOG_INPUT__PLUGIN_H__
#define _TASK_SLEEP_ON_IDLE_RUN #define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION #define _TASK_STD_FUNCTION
@@ -16,12 +16,12 @@
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
class PotPlugin : public Plugin { class AnalogInputPlugin : public Plugin {
public: public:
Task inputTask; Task inputTask;
int currentVal = 0; int currentVal = 0;
GpioConfig config; GpioConfig config;
PotPlugin(GpioConfig cfg); AnalogInputPlugin(GpioConfig cfg);
void activate(Scheduler* userScheduler); void activate(Scheduler* userScheduler);
void checkInput(); void checkInput();
}; };