diff --git a/.vscode/settings.json b/.vscode/settings.json index 2c55c67..d1faa96 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "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", + "PATH": "/home/master/.platformio/penv/bin:/home/master/.platformio/penv:/home/master/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/sbin:/usr/sbin", "PLATFORMIO_CALLER": "vscode" }, "files.associations": { diff --git a/lib/a21 b/lib/a21 new file mode 160000 index 0000000..9db8a9b --- /dev/null +++ b/lib/a21 @@ -0,0 +1 @@ +Subproject commit 9db8a9b4b17f5e25683ffec62175c36ecd33fba5 diff --git a/platformio.ini b/platformio.ini index 230f6d7..0053343 100644 --- a/platformio.ini +++ b/platformio.ini @@ -23,8 +23,6 @@ upload_speed = ${common.upload_speed} monitor_baud = ${common.monitor_baud} src_filter = +<*> - + - + lib_deps = ${common.lib_deps} -lib_ignore = - Ai Esp32 Rotary Encoder [env:pir] platform = ${common.platform} @@ -35,8 +33,6 @@ monitor_baud = ${common.monitor_baud} build_flags = -DSPROCKET_PRINT=1 src_filter = +<*> - + - + + lib_deps = ${common.lib_deps} -lib_ignore = - Ai Esp32 Rotary Encoder [env:audio] platform = ${common.platform} @@ -46,8 +42,6 @@ upload_speed = ${common.upload_speed} monitor_baud = ${common.monitor_baud} src_filter = +<*> - + - + lib_deps = ${common.lib_deps} -lib_ignore = - Ai Esp32 Rotary Encoder [env:analog_esp32] platform = espressif32 @@ -58,8 +52,17 @@ upload_speed = ${common.upload_speed} monitor_baud = ${common.monitor_baud} src_filter = +<*> - + - + lib_deps = ${common.lib_deps} -lib_ignore = - Ai Esp32 Rotary Encoder + +[env:rotary_ec11] +platform = ${common.platform} +board = ${common.board} +framework = ${common.framework} +upload_speed = ${common.upload_speed} +monitor_baud = ${common.monitor_baud} +src_filter = +<*> - + - + +lib_deps = ${common.lib_deps} +lib_ignore = Ai Esp32 Rotary Encoder + [env:rotary_esp32] platform = espressif32 @@ -69,8 +72,6 @@ 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 [env:combined_esp32] platform = espressif32 @@ -80,5 +81,4 @@ 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 +lib_deps = ${common.lib_deps} \ No newline at end of file diff --git a/src/examples/rotary/main.cpp b/src/examples/rotary/main.cpp index 80bc6a4..d6147f7 100644 --- a/src/examples/rotary/main.cpp +++ b/src/examples/rotary/main.cpp @@ -1,29 +1,100 @@ -#include "config.h" -#include "Sprocket.h" -#include "inputs/rotary/RotaryPlugin.h" -Sprocket *sprocket; -RotaryPlugin *r1; +// +// a21 — Arduino Toolkit. Example for EC11 class. +// Copyright (C) 2016-2017, Aleh Dzenisiuk. http://github.com/aleh/a21 +// -void addRotary(Sprocket *s, Plugin *r, const char *topic) -{ - String btnTopic = String(topic) + String("/button"); - s->subscribe(topic, bind([](String label, String val) { PRINT_MSG(Serial, label.c_str(), val.c_str()); }, topic, _1)); - s->subscribe(btnTopic, bind([](String label, String val) { PRINT_MSG(Serial, label.c_str(), val.c_str()); }, btnTopic, _1)); - s->addPlugin(r); +#include + +using namespace a21; + +// +// Here we assume that the pins A and B of a EC-11 rotary encoder are connected to pins 2 and 3 of the Arduino Uno +// board and the pin C is connected to the ground. +// + +EC11 encoder; + +// Using interrupt-based example by default, comment out or define as 0 to check polling. +#define DEMO_INTERRUPTS 1 + +#if DEMO_INTERRUPTS + +// +// Interrupt-based example. This is recommended, but it means only pins 2 and 3 can be used with Uno. +// + +const int encoderPinA = D3; +const int encoderPinB = D4; + +void pinDidChange() { + encoder.checkPins(digitalRead(encoderPinA), digitalRead(encoderPinB)); } -void setup() -{ - sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE}); - r1 = new RotaryPlugin({35, 34, 21, -1, 50, 0, 255, true, "rotary1"}); - r1->rotaryEncoder->setup([] { r1->rotaryEncoder->readEncoder_ISR(); }); - addRotary(sprocket, r1, "rotary1"); - sprocket->activate(); +void prepare() { + attachInterrupt(digitalPinToInterrupt(encoderPinA), pinDidChange, CHANGE); + attachInterrupt(digitalPinToInterrupt(encoderPinB), pinDidChange, CHANGE); +} + +#else + +// +// Polling allows to use the encoder with any digital input pin. +// + +const int encoderPinA = 2; +const int encoderPinB = 3; + +void prepare() { +} + +#endif // #if DEMO_INTERRUPTS + +void setup() { + + Serial.begin(115200); + Serial.println("EC11 Test"); + + // We can use internal pull-up with the encoder pins, assuming pin C is simply grounded. + pinMode(encoderPinA, INPUT_PULLUP); + pinMode(encoderPinB, INPUT_PULLUP); + + prepare(); +} + +static int value = 0; + +void loop() { + + EC11Event e; + if (encoder.read(&e)) { + + // OK, got an event waiting to be handled, its count field is number of steps since the last check. + + if (e.type == EC11Event::StepCW) { + // Clock-wise. + value += e.count; + } else { + // Counter clock-wise. + value -= e.count; + } + + Serial.println(value); + } + +#if DEMO_INTERRUPTS + + // Wait quite some time to demonstrate that we can check for events fairly infrequently and still not miss them. + delay(200); + +#else + + // With polling-style pin checking we can still read infrequently, but we need to poll the pins often enough. + for (int i = 0; i < 200; i++) { + encoder.checkPins(digitalRead(encoderPinA), digitalRead(encoderPinB)); + delay(1); + } + +#endif } -void loop() -{ - sprocket->loop(); - yield(); -} \ No newline at end of file diff --git a/src/inputs/rotary/RotaryPlugin.cpp b/src/inputs/rotary/RotaryPlugin.cpp_ similarity index 100% rename from src/inputs/rotary/RotaryPlugin.cpp rename to src/inputs/rotary/RotaryPlugin.cpp_ diff --git a/src/inputs/rotary/RotaryPlugin.h b/src/inputs/rotary/RotaryPlugin.h_ similarity index 100% rename from src/inputs/rotary/RotaryPlugin.h rename to src/inputs/rotary/RotaryPlugin.h_