rotary plugin

This commit is contained in:
2018-11-17 04:06:06 +01:00
parent be17cabfdc
commit dbaaeae8d9
7 changed files with 147 additions and 3 deletions

View File

@@ -32,4 +32,15 @@ build_flags = -std=c++14
upload_speed = ${common.upload_speed}
monitor_baud = ${common.monitor_baud}
src_filter = +<*> -<examples/> +<examples/pot/>
lib_deps = ${common.lib_deps}
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 = +<*> -<examples/> +<examples/rotary/>
lib_deps = ${common.lib_deps}
Ai Esp32 Rotary Encoder

View File

@@ -5,7 +5,7 @@ struct GpioConfig
{
int pin;
int threshold;
int pollInterval;
int updateInterval;
const char *topic;
};

View File

@@ -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");

32
src/RotaryPlugin.cpp Normal file
View File

@@ -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));
//}
}

45
src/RotaryPlugin.h Normal file
View File

@@ -0,0 +1,45 @@
#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 "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

View File

@@ -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

View File

@@ -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();
}