From f6b5e0506334fe4d6c3f57ad08146dfaaf834c91 Mon Sep 17 00:00:00 2001 From: aster94 Date: Tue, 9 Jan 2018 18:32:24 +0100 Subject: [PATCH 1/2] Add files via upload --- ClickEncoder.h | 383 ++++++++++++++++++++++++------------------------- 1 file changed, 188 insertions(+), 195 deletions(-) diff --git a/ClickEncoder.h b/ClickEncoder.h index 6c486ac..cf52ffb 100644 --- a/ClickEncoder.h +++ b/ClickEncoder.h @@ -1,196 +1,189 @@ -// ---------------------------------------------------------------------------- -// Rotary Encoder Driver with Acceleration -// Supports Click, DoubleClick, Long Click -// -// (c) 2010 karl@pitrich.com -// (c) 2014 karl@pitrich.com -// -// Timer-based rotary encoder logic by Peter Dannegger -// http://www.mikrocontroller.net/articles/Drehgeber -// ---------------------------------------------------------------------------- - -#ifndef __have__ClickEncoder_h__ -#define __have__ClickEncoder_h__ - -// ---Button defaults------------------------------------------------------------- - -#define BTN_DOUBLECLICKTIME 400 // second click within 400ms -#define BTN_HOLDTIME 1000 // report held button after 1s - - -// ---------------------------------------------------------------------------- - -#include -#if defined(__AVR__) -#include -#include -#include -#endif -#include "Arduino.h" - -// ---------------------------------------------------------------------------- - -#define ENC_NORMAL (1 << 1) // use Peter Danneger's decoder -#define ENC_FLAKY (1 << 2) // use Table-based decoder - -// ---------------------------------------------------------------------------- - -#ifndef ENC_DECODER -# define ENC_DECODER ENC_NORMAL -#endif - -#if ENC_DECODER == ENC_FLAKY -# ifndef ENC_HALFSTEP -# define ENC_HALFSTEP 1 // use table for half step per default -# endif -#endif - -// ---------------------------------------------------------------------------- -#if defined(__arm__) && (defined (__STM32F1__) || defined (__STM32F4__) ) - typedef WiringPinMode pinMode_t; -#else - typedef uint8_t pinMode_t; -#endif - -class ClickEncoder -{ -public: - typedef enum Button_e { - Open = 0, - Closed, - - Pressed, - Held, - Released, - - Clicked, - DoubleClicked - - } Button; - -public: - ClickEncoder(int8_t A, int8_t B, int8_t BTN = -1, - uint8_t stepsPerNotch = 4, bool active = LOW); - -#ifndef WITHOUT_BUTTON - explicit ClickEncoder(int8_t BTN, bool active = false); // Depricated. Use Digtial Button instead - -#endif - - void service(void); - int16_t getValue(void); - -#ifndef WITHOUT_BUTTON -public: - Button getButton(void); -#endif - -#ifndef WITHOUT_BUTTON -public: - void setDoubleClickTime(uint16_t durationMilliseconds) - { - buttonDoubleClickTime = durationMilliseconds; - } - -public: - void setHoldTime(uint16_t durationMilliseconds) - { - buttonHoldTime = durationMilliseconds; - } - -public: - void setDoubleClickEnabled(const bool &d) - { - doubleClickEnabled = d; - } - - const bool getDoubleClickEnabled() - { - return doubleClickEnabled; - } - -public: - void setButtonHeldEnabled(const bool &d) - { - buttonHeldEnabled = d; - } - - const bool getButtonHeldEnabled() - { - return buttonHeldEnabled; - } - -public: - void setButtonOnPinZeroEnabled(const bool &d) - { - buttonOnPinZeroEnabled = d; - } - - const bool getButtonOnPinZeroEnabled() - { - return buttonOnPinZeroEnabled; - } -#endif - -public: - void setAccelerationEnabled(const bool &a) - { - accelerationEnabled = a; - if (accelerationEnabled == false) { - acceleration = 0; - } - } - - const bool getAccelerationEnabled() - { - return accelerationEnabled; - } - -protected: - int8_t pinA; - int8_t pinB; - int8_t pinBTN; - bool pinsActive; - volatile int16_t delta; - volatile int16_t last; - volatile uint8_t steps; - volatile uint16_t acceleration; - bool accelerationEnabled; -#if ENC_DECODER != ENC_NORMAL - static const int8_t table[16]; -#endif -#ifndef WITHOUT_BUTTON - volatile Button button; - bool doubleClickEnabled; - bool buttonHeldEnabled; - bool buttonOnPinZeroEnabled = false; - bool analogInput = false; - uint16_t keyDownTicks = 0; - uint16_t doubleClickTicks = 0; - uint16_t buttonHoldTime = BTN_HOLDTIME; - uint16_t buttonDoubleClickTime = BTN_DOUBLECLICKTIME; - unsigned long lastButtonCheck = 0; - int16_t anlogActiveRangeLow = 0; - int16_t anlogActiveRangeHigh = 0; - bool getPinState(); -#endif -}; - -#ifndef WITHOUT_BUTTON -class AnalogButton : public ClickEncoder -{ - public: - explicit AnalogButton(int8_t BTN, int16_t rangeLow, int16_t rangeHigh); // Constructor for using analog input range as a button -}; - -class DigitalButton : public ClickEncoder -{ - public: - explicit DigitalButton(int8_t BTN, bool active = false); // Constructor for using a button only -}; - -#endif - -// ---------------------------------------------------------------------------- - +// ---------------------------------------------------------------------------- +// Rotary Encoder Driver with Acceleration +// Supports Click, DoubleClick, Long Click +// +// (c) 2010 karl@pitrich.com +// (c) 2014 karl@pitrich.com +// +// Timer-based rotary encoder logic by Peter Dannegger +// http://www.mikrocontroller.net/articles/Drehgeber +// ---------------------------------------------------------------------------- + +#ifndef __have__ClickEncoder_h__ +#define __have__ClickEncoder_h__ + +// ---Button defaults------------------------------------------------------------- + +#define BTN_DOUBLECLICKTIME 400 // second click within 400ms +#define BTN_HOLDTIME 1000 // report held button after 1s + + +// ---------------------------------------------------------------------------- + +#include +#if defined(__AVR__) +#include +#include +#include +#endif +#include "Arduino.h" + +// ---------------------------------------------------------------------------- + +#define ENC_NORMAL (1 << 1) // use Peter Danneger's decoder +#define ENC_FLAKY (1 << 2) // use Table-based decoder + +// ---------------------------------------------------------------------------- + +#ifndef ENC_DECODER +# define ENC_DECODER ENC_NORMAL +#endif + +#if ENC_DECODER == ENC_FLAKY +# ifndef ENC_HALFSTEP +# define ENC_HALFSTEP 1 // use table for half step per default +# endif +#endif + +// ---------------------------------------------------------------------------- +#if defined(__arm__) && (defined (__STM32F1__) || defined (__STM32F4__) ) + typedef WiringPinMode pinMode_t; +#else + typedef uint8_t pinMode_t; +#endif + +class ClickEncoder +{ +public: + + typedef enum Button_e { + Open = 0, + Closed, + + Pressed, + Held, + Released, + + Clicked, + DoubleClicked + + } Button; + + ClickEncoder(int8_t A, int8_t B, int8_t BTN = -1, + uint8_t stepsPerNotch = 4, bool active = LOW); + +#ifndef WITHOUT_BUTTON + explicit ClickEncoder(int8_t BTN, bool active = false); // Depricated. Use Digtial Button instead +#endif + + void service(void); + int16_t getValue(void); + +#ifndef WITHOUT_BUTTON + Button getButton(void); +#endif + +#ifndef WITHOUT_BUTTON + void setDoubleClickTime(uint16_t durationMilliseconds) + { + buttonDoubleClickTime = durationMilliseconds; + } + + + void setHoldTime(uint16_t durationMilliseconds) + { + buttonHoldTime = durationMilliseconds; + } + + void setDoubleClickEnabled(const bool &d) + { + doubleClickEnabled = d; + } + + const bool getDoubleClickEnabled() + { + return doubleClickEnabled; + } + + void setButtonHeldEnabled(const bool &d) + { + buttonHeldEnabled = d; + } + + const bool getButtonHeldEnabled() + { + return buttonHeldEnabled; + } + + void setButtonOnPinZeroEnabled(const bool &d) + { + buttonOnPinZeroEnabled = d; + } + + const bool getButtonOnPinZeroEnabled() + { + return buttonOnPinZeroEnabled; + } +#endif + + void setAccelerationEnabled(const bool &a) + { + accelerationEnabled = a; + if (accelerationEnabled == false) { + acceleration = 0; + } + } + + const bool getAccelerationEnabled() + { + return accelerationEnabled; + } + +protected: + int8_t pinA; + int8_t pinB; + int8_t pinBTN; + bool pinsActive; + volatile int16_t delta; + volatile int16_t last; + volatile uint8_t steps; + volatile uint16_t acceleration; + bool accelerationEnabled; +#if ENC_DECODER != ENC_NORMAL + static const int8_t table[16]; +#endif +#ifndef WITHOUT_BUTTON + volatile Button button; + bool doubleClickEnabled; + bool buttonHeldEnabled; + bool buttonOnPinZeroEnabled = false; + bool analogInput = false; + uint16_t keyDownTicks = 0; + uint16_t doubleClickTicks = 0; + uint16_t buttonHoldTime = BTN_HOLDTIME; + uint16_t buttonDoubleClickTime = BTN_DOUBLECLICKTIME; + unsigned long lastButtonCheck = 0; + int16_t anlogActiveRangeLow = 0; + int16_t anlogActiveRangeHigh = 0; + bool getPinState(); +#endif +}; + +#ifndef WITHOUT_BUTTON +class AnalogButton : public ClickEncoder +{ + public: + explicit AnalogButton(int8_t BTN, int16_t rangeLow, int16_t rangeHigh); // Constructor for using analog input range as a button +}; + +class DigitalButton : public ClickEncoder +{ + public: + explicit DigitalButton(int8_t BTN, bool active = false); // Constructor for using a button only +}; + +#endif + +// ---------------------------------------------------------------------------- + #endif // __have__ClickEncoder_h__ From 59a22ba44c823c7646da78830f6e27b7182b1481 Mon Sep 17 00:00:00 2001 From: aster94 Date: Tue, 9 Jan 2018 18:47:09 +0100 Subject: [PATCH 2/2] Add files via upload --- ClickEncoder.h | 383 +++++++++++++------------ examples/ArduinoStyle/ArduinoStyle.ino | 68 +++++ keywords.txt | 13 + 3 files changed, 276 insertions(+), 188 deletions(-) create mode 100644 examples/ArduinoStyle/ArduinoStyle.ino create mode 100644 keywords.txt diff --git a/ClickEncoder.h b/ClickEncoder.h index cf52ffb..6c486ac 100644 --- a/ClickEncoder.h +++ b/ClickEncoder.h @@ -1,189 +1,196 @@ -// ---------------------------------------------------------------------------- -// Rotary Encoder Driver with Acceleration -// Supports Click, DoubleClick, Long Click -// -// (c) 2010 karl@pitrich.com -// (c) 2014 karl@pitrich.com -// -// Timer-based rotary encoder logic by Peter Dannegger -// http://www.mikrocontroller.net/articles/Drehgeber -// ---------------------------------------------------------------------------- - -#ifndef __have__ClickEncoder_h__ -#define __have__ClickEncoder_h__ - -// ---Button defaults------------------------------------------------------------- - -#define BTN_DOUBLECLICKTIME 400 // second click within 400ms -#define BTN_HOLDTIME 1000 // report held button after 1s - - -// ---------------------------------------------------------------------------- - -#include -#if defined(__AVR__) -#include -#include -#include -#endif -#include "Arduino.h" - -// ---------------------------------------------------------------------------- - -#define ENC_NORMAL (1 << 1) // use Peter Danneger's decoder -#define ENC_FLAKY (1 << 2) // use Table-based decoder - -// ---------------------------------------------------------------------------- - -#ifndef ENC_DECODER -# define ENC_DECODER ENC_NORMAL -#endif - -#if ENC_DECODER == ENC_FLAKY -# ifndef ENC_HALFSTEP -# define ENC_HALFSTEP 1 // use table for half step per default -# endif -#endif - -// ---------------------------------------------------------------------------- -#if defined(__arm__) && (defined (__STM32F1__) || defined (__STM32F4__) ) - typedef WiringPinMode pinMode_t; -#else - typedef uint8_t pinMode_t; -#endif - -class ClickEncoder -{ -public: - - typedef enum Button_e { - Open = 0, - Closed, - - Pressed, - Held, - Released, - - Clicked, - DoubleClicked - - } Button; - - ClickEncoder(int8_t A, int8_t B, int8_t BTN = -1, - uint8_t stepsPerNotch = 4, bool active = LOW); - -#ifndef WITHOUT_BUTTON - explicit ClickEncoder(int8_t BTN, bool active = false); // Depricated. Use Digtial Button instead -#endif - - void service(void); - int16_t getValue(void); - -#ifndef WITHOUT_BUTTON - Button getButton(void); -#endif - -#ifndef WITHOUT_BUTTON - void setDoubleClickTime(uint16_t durationMilliseconds) - { - buttonDoubleClickTime = durationMilliseconds; - } - - - void setHoldTime(uint16_t durationMilliseconds) - { - buttonHoldTime = durationMilliseconds; - } - - void setDoubleClickEnabled(const bool &d) - { - doubleClickEnabled = d; - } - - const bool getDoubleClickEnabled() - { - return doubleClickEnabled; - } - - void setButtonHeldEnabled(const bool &d) - { - buttonHeldEnabled = d; - } - - const bool getButtonHeldEnabled() - { - return buttonHeldEnabled; - } - - void setButtonOnPinZeroEnabled(const bool &d) - { - buttonOnPinZeroEnabled = d; - } - - const bool getButtonOnPinZeroEnabled() - { - return buttonOnPinZeroEnabled; - } -#endif - - void setAccelerationEnabled(const bool &a) - { - accelerationEnabled = a; - if (accelerationEnabled == false) { - acceleration = 0; - } - } - - const bool getAccelerationEnabled() - { - return accelerationEnabled; - } - -protected: - int8_t pinA; - int8_t pinB; - int8_t pinBTN; - bool pinsActive; - volatile int16_t delta; - volatile int16_t last; - volatile uint8_t steps; - volatile uint16_t acceleration; - bool accelerationEnabled; -#if ENC_DECODER != ENC_NORMAL - static const int8_t table[16]; -#endif -#ifndef WITHOUT_BUTTON - volatile Button button; - bool doubleClickEnabled; - bool buttonHeldEnabled; - bool buttonOnPinZeroEnabled = false; - bool analogInput = false; - uint16_t keyDownTicks = 0; - uint16_t doubleClickTicks = 0; - uint16_t buttonHoldTime = BTN_HOLDTIME; - uint16_t buttonDoubleClickTime = BTN_DOUBLECLICKTIME; - unsigned long lastButtonCheck = 0; - int16_t anlogActiveRangeLow = 0; - int16_t anlogActiveRangeHigh = 0; - bool getPinState(); -#endif -}; - -#ifndef WITHOUT_BUTTON -class AnalogButton : public ClickEncoder -{ - public: - explicit AnalogButton(int8_t BTN, int16_t rangeLow, int16_t rangeHigh); // Constructor for using analog input range as a button -}; - -class DigitalButton : public ClickEncoder -{ - public: - explicit DigitalButton(int8_t BTN, bool active = false); // Constructor for using a button only -}; - -#endif - -// ---------------------------------------------------------------------------- - +// ---------------------------------------------------------------------------- +// Rotary Encoder Driver with Acceleration +// Supports Click, DoubleClick, Long Click +// +// (c) 2010 karl@pitrich.com +// (c) 2014 karl@pitrich.com +// +// Timer-based rotary encoder logic by Peter Dannegger +// http://www.mikrocontroller.net/articles/Drehgeber +// ---------------------------------------------------------------------------- + +#ifndef __have__ClickEncoder_h__ +#define __have__ClickEncoder_h__ + +// ---Button defaults------------------------------------------------------------- + +#define BTN_DOUBLECLICKTIME 400 // second click within 400ms +#define BTN_HOLDTIME 1000 // report held button after 1s + + +// ---------------------------------------------------------------------------- + +#include +#if defined(__AVR__) +#include +#include +#include +#endif +#include "Arduino.h" + +// ---------------------------------------------------------------------------- + +#define ENC_NORMAL (1 << 1) // use Peter Danneger's decoder +#define ENC_FLAKY (1 << 2) // use Table-based decoder + +// ---------------------------------------------------------------------------- + +#ifndef ENC_DECODER +# define ENC_DECODER ENC_NORMAL +#endif + +#if ENC_DECODER == ENC_FLAKY +# ifndef ENC_HALFSTEP +# define ENC_HALFSTEP 1 // use table for half step per default +# endif +#endif + +// ---------------------------------------------------------------------------- +#if defined(__arm__) && (defined (__STM32F1__) || defined (__STM32F4__) ) + typedef WiringPinMode pinMode_t; +#else + typedef uint8_t pinMode_t; +#endif + +class ClickEncoder +{ +public: + typedef enum Button_e { + Open = 0, + Closed, + + Pressed, + Held, + Released, + + Clicked, + DoubleClicked + + } Button; + +public: + ClickEncoder(int8_t A, int8_t B, int8_t BTN = -1, + uint8_t stepsPerNotch = 4, bool active = LOW); + +#ifndef WITHOUT_BUTTON + explicit ClickEncoder(int8_t BTN, bool active = false); // Depricated. Use Digtial Button instead + +#endif + + void service(void); + int16_t getValue(void); + +#ifndef WITHOUT_BUTTON +public: + Button getButton(void); +#endif + +#ifndef WITHOUT_BUTTON +public: + void setDoubleClickTime(uint16_t durationMilliseconds) + { + buttonDoubleClickTime = durationMilliseconds; + } + +public: + void setHoldTime(uint16_t durationMilliseconds) + { + buttonHoldTime = durationMilliseconds; + } + +public: + void setDoubleClickEnabled(const bool &d) + { + doubleClickEnabled = d; + } + + const bool getDoubleClickEnabled() + { + return doubleClickEnabled; + } + +public: + void setButtonHeldEnabled(const bool &d) + { + buttonHeldEnabled = d; + } + + const bool getButtonHeldEnabled() + { + return buttonHeldEnabled; + } + +public: + void setButtonOnPinZeroEnabled(const bool &d) + { + buttonOnPinZeroEnabled = d; + } + + const bool getButtonOnPinZeroEnabled() + { + return buttonOnPinZeroEnabled; + } +#endif + +public: + void setAccelerationEnabled(const bool &a) + { + accelerationEnabled = a; + if (accelerationEnabled == false) { + acceleration = 0; + } + } + + const bool getAccelerationEnabled() + { + return accelerationEnabled; + } + +protected: + int8_t pinA; + int8_t pinB; + int8_t pinBTN; + bool pinsActive; + volatile int16_t delta; + volatile int16_t last; + volatile uint8_t steps; + volatile uint16_t acceleration; + bool accelerationEnabled; +#if ENC_DECODER != ENC_NORMAL + static const int8_t table[16]; +#endif +#ifndef WITHOUT_BUTTON + volatile Button button; + bool doubleClickEnabled; + bool buttonHeldEnabled; + bool buttonOnPinZeroEnabled = false; + bool analogInput = false; + uint16_t keyDownTicks = 0; + uint16_t doubleClickTicks = 0; + uint16_t buttonHoldTime = BTN_HOLDTIME; + uint16_t buttonDoubleClickTime = BTN_DOUBLECLICKTIME; + unsigned long lastButtonCheck = 0; + int16_t anlogActiveRangeLow = 0; + int16_t anlogActiveRangeHigh = 0; + bool getPinState(); +#endif +}; + +#ifndef WITHOUT_BUTTON +class AnalogButton : public ClickEncoder +{ + public: + explicit AnalogButton(int8_t BTN, int16_t rangeLow, int16_t rangeHigh); // Constructor for using analog input range as a button +}; + +class DigitalButton : public ClickEncoder +{ + public: + explicit DigitalButton(int8_t BTN, bool active = false); // Constructor for using a button only +}; + +#endif + +// ---------------------------------------------------------------------------- + #endif // __have__ClickEncoder_h__ diff --git a/examples/ArduinoStyle/ArduinoStyle.ino b/examples/ArduinoStyle/ArduinoStyle.ino new file mode 100644 index 0000000..fcf5983 --- /dev/null +++ b/examples/ArduinoStyle/ArduinoStyle.ino @@ -0,0 +1,68 @@ +#include +#include + +int16_t oldEncPos, encPos; +uint8_t buttonState; + +#define pinA A2 +#define pinB A1 +#define pinSw A0 //switch +#define STEPS 4 + +ClickEncoder encoder(pinA, pinB, pinSw, STEPS); + +void setup() { + Serial.begin(9600); + + Timer1.initialize(1000); + Timer1.attachInterrupt(timerIsr); + + encoder.setAccelerationEnabled(true); + + Serial.print("Acceleration is "); + Serial.println((encoder.getAccelerationEnabled()) ? "enabled" : "disabled"); + + oldEncPos = -1; +} + +void loop() { + encPos += encoder.getValue(); + + if (encPos != oldEncPos) { + oldEncPos = encPos; + Serial.print("Encoder Value: "); + Serial.println(encPos); + } + + buttonState = encoder.getButton(); + + if (buttonState != 0) { + Serial.print("Button: "); Serial.println(buttonState); + switch (buttonState) { + case ClickEncoder::Open: //0 + break; + + case ClickEncoder::Closed: //1 + break; + + case ClickEncoder::Pressed: //2 + break; + + case ClickEncoder::Held: //3 + break; + + case ClickEncoder::Released: //4 + break; + + case ClickEncoder::Clicked: //5 + break; + + case ClickEncoder::DoubleClicked: //6 + break; + } + } +} + +void timerIsr() { + encoder.service(); +} diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..605d6ec --- /dev/null +++ b/keywords.txt @@ -0,0 +1,13 @@ +ClickEncoder KEYWORD1 + +setAccelerationEnabled KEYWORD2 +getAccelerationEnabled KEYWORD2 +getButton KEYWORD2 +getValue KEYWORD2 +service KEYWORD2 +setDoubleClickTime KEYWORD2 +setHoldTime KEYWORD2 +setDoubleClickEnabled KEYWORD2 +getDoubleClickEnabled KEYWORD2 +setButtonHeldEnabled KEYWORD2 +getButtonHeldEnabled KEYWORD2 \ No newline at end of file