diff --git a/platformio.ini b/platformio.ini index 5016fee..a9b161f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -32,4 +32,15 @@ build_flags = -std=c++14 upload_speed = ${common.upload_speed} monitor_baud = ${common.monitor_baud} src_filter = +<*> - + -lib_deps = ${common.lib_deps} \ No newline at end of file +lib_deps = ${common.lib_deps} + +[env:rotary_esp32] +platform = espressif32 +board = esp32dev +framework = ${common.framework} +build_flags = -std=c++14 +upload_speed = ${common.upload_speed} +monitor_baud = ${common.monitor_baud} +src_filter = +<*> - + +lib_deps = ${common.lib_deps} + Ai Esp32 Rotary Encoder \ No newline at end of file diff --git a/src/GpioConfig.h b/src/GpioConfig.h index 28e90c9..7a223e2 100644 --- a/src/GpioConfig.h +++ b/src/GpioConfig.h @@ -5,7 +5,7 @@ struct GpioConfig { int pin; int threshold; - int pollInterval; + int updateInterval; const char *topic; }; diff --git a/src/PotPlugin.cpp b/src/PotPlugin.cpp index 21b1796..4c5f757 100644 --- a/src/PotPlugin.cpp +++ b/src/PotPlugin.cpp @@ -5,7 +5,7 @@ PotPlugin::PotPlugin(GpioConfig cfg){ } void PotPlugin::activate(Scheduler *userScheduler) { - inputTask.set(TASK_MILLISECOND * config.pollInterval, TASK_FOREVER, std::bind(&PotPlugin::checkInput, this)); + inputTask.set(TASK_MILLISECOND * config.updateInterval, TASK_FOREVER, std::bind(&PotPlugin::checkInput, this)); userScheduler->addTask(inputTask); inputTask.enable(); PRINT_MSG(Serial, "PLUGIN", "PotPlugin activated"); diff --git a/src/RotaryPlugin.cpp b/src/RotaryPlugin.cpp new file mode 100644 index 0000000..df50c2d --- /dev/null +++ b/src/RotaryPlugin.cpp @@ -0,0 +1,32 @@ +#include "RotaryPlugin.h" + +RotaryPlugin::RotaryPlugin(RotaryConfig cfg) +{ + config = cfg; + rotaryEncoder = new AiEsp32RotaryEncoder(config.pinA, config.pinB, config.pinButton, config.pinVcc); + rotaryEncoder->begin(); + rotaryEncoder->setBoundaries(config.lowerBound, config.upperBound, config.circulateValues); +} +void RotaryPlugin::activate(Scheduler *userScheduler) +{ + + inputTask.set(TASK_MILLISECOND * config.updateInterval, TASK_FOREVER, std::bind(&RotaryPlugin::checkInput, this)); + userScheduler->addTask(inputTask); + inputTask.enable(); + PRINT_MSG(Serial, "PLUGIN", "RotaryPlugin activated"); +} + +void RotaryPlugin::checkInput() +{ + rotaryEncoder->enable(); + if (rotaryEncoder->encoderChanged()) + { + int encReading = rotaryEncoder->readEncoder(); + publish(config.topic, String(encReading)); + } + //ButtonState newBtnState = rotaryEncoder->currentButtonState(); + //if(newBtnState != btnState){ + // btnState = newBtnState; + // publish(config.topicButton, String(btnState)); + //} +} diff --git a/src/RotaryPlugin.h b/src/RotaryPlugin.h new file mode 100644 index 0000000..c4a3b14 --- /dev/null +++ b/src/RotaryPlugin.h @@ -0,0 +1,45 @@ +#ifndef __POT_PLUGIN_H__ +#define __POT_PLUGIN_H__ + +#define _TASK_SLEEP_ON_IDLE_RUN +#define _TASK_STD_FUNCTION + +#include +#include +#include +#include +#include +#include +#include +#include "AiEsp32RotaryEncoder.h" + +using namespace std; +using namespace std::placeholders; + +struct RotaryConfig +{ + int pinA; + int pinB; + int pinButton; + int pinVcc; + int updateInterval; + int lowerBound; + int upperBound; + bool circulateValues; + const char* topic; + //const char* topicButton; +}; + +class RotaryPlugin : public Plugin { + public: + Task inputTask; + int currentVal = 0; + //ButtonState btnState = BUT_RELEASED; + AiEsp32RotaryEncoder* rotaryEncoder; + RotaryConfig config; + RotaryPlugin(RotaryConfig cfg); + void activate(Scheduler* userScheduler); + void checkInput(); +}; + +#endif diff --git a/src/examples/rotary/config.h b/src/examples/rotary/config.h new file mode 100644 index 0000000..453a0e8 --- /dev/null +++ b/src/examples/rotary/config.h @@ -0,0 +1,27 @@ +#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 + +/* +connecting Rotary encoder +CLK (A pin) - to any microcontroler intput pin with interrupt +DT (B pin) - to any microcontroler intput pin with interrupt +SW (button pin) - to any microcontroler intput pin +VCC - to microcontroler VCC (then set ROTARY_ENCODER_VCC_PIN -1) +GND - to microcontroler GND +*/ +#define ROTARY_ENCODER_A_PIN 35 +#define ROTARY_ENCODER_B_PIN 34 +#define ROTARY_ENCODER_BUTTON_PIN 21 +#define ROTARY_ENCODER_VCC_PIN -1 /*put -1 of Rotary encoder Vcc is connected directly to 3,3V; else you can use declared output pin for powering rotary encoder */ + +#endif \ No newline at end of file diff --git a/src/examples/rotary/main.cpp b/src/examples/rotary/main.cpp new file mode 100644 index 0000000..eb881c1 --- /dev/null +++ b/src/examples/rotary/main.cpp @@ -0,0 +1,29 @@ +#include "config.h" +#include "Sprocket.h" +#include "RotaryPlugin.h" + +Sprocket *sprocket; +RotaryPlugin *r1; + +void registerPlugin(Sprocket *s, Plugin *r, const char *topic) +{ + s->addPlugin(r); + 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}); + //r1 = new RotaryPlugin({35, 34, 21, -1, 50, 0, 255, true, "rotary1_enc", "rotary1_btn"}); + r1 = new RotaryPlugin({35, 34, 21, -1, 50, 0, 255, true, "rotary1"}); + r1->rotaryEncoder->setup([] { r1->rotaryEncoder->readEncoder_ISR(); }); + + registerPlugin(sprocket, r1, "rotary1"); + sprocket->activate(); +} + +void loop() +{ + sprocket->loop(); + yield(); +} \ No newline at end of file