diff --git a/libraries/RotaryEncoder/RotaryEncoder.cpp b/libraries/RotaryEncoder/RotaryEncoder.cpp index c4fdeca7..eac64d29 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.cpp +++ b/libraries/RotaryEncoder/RotaryEncoder.cpp @@ -38,6 +38,11 @@ class HwRotaryEncoder RotaryEncoder; +extern "C" +{ + void QDEC_IRQHandler(void); +} + /** * Initialize Hardware Encoder */ @@ -187,17 +192,21 @@ void HwRotaryEncoder::clearAbs(void) _abs = 0; } - -void QDEC_IRQHandler(void) +void HwRotaryEncoder::_irq_handler(void) { if ( (NRF_QDEC->INTENSET & QDEC_INTENSET_SAMPLERDY_Msk) && NRF_QDEC->EVENTS_SAMPLERDY ) { NRF_QDEC->EVENTS_SAMPLERDY = 0; -// if ( _cb ) -// { -// int32_t step = read(); -// if ( step ) _cb(step); -// } + if ( _cb ) + { + int32_t step = read(); + if ( step ) _cb(step); + } } } + +void QDEC_IRQHandler(void) +{ + RotaryEncoder._irq_handler(); +} diff --git a/libraries/RotaryEncoder/RotaryEncoder.h b/libraries/RotaryEncoder/RotaryEncoder.h index 9b3d8509..10b33f7f 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.h +++ b/libraries/RotaryEncoder/RotaryEncoder.h @@ -41,7 +41,7 @@ class HwRotaryEncoder { public: - typedef void (*callback_t) (int32_t step); + typedef void (*callback_t) (int step); HwRotaryEncoder(void) { @@ -68,6 +68,9 @@ class HwRotaryEncoder void setCallback(callback_t fp); + // Internal API + void _irq_handler(void); + private: int8_t _pina, _pinb; int8_t _pinled; diff --git a/libraries/RotaryEncoder/SwRotaryEncoder.cpp b/libraries/RotaryEncoder/SwRotaryEncoder.cpp new file mode 100644 index 00000000..342def98 --- /dev/null +++ b/libraries/RotaryEncoder/SwRotaryEncoder.cpp @@ -0,0 +1,38 @@ +/**************************************************************************/ +/*! + @file SwRotaryEncoder.cpp + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + + + diff --git a/libraries/RotaryEncoder/SwRotaryEncoder.h b/libraries/RotaryEncoder/SwRotaryEncoder.h new file mode 100644 index 00000000..c52fb4c0 --- /dev/null +++ b/libraries/RotaryEncoder/SwRotaryEncoder.h @@ -0,0 +1,43 @@ +/**************************************************************************/ +/*! + @file SwRotaryEncoder.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef SWROTARYENCODER_H_ +#define SWROTARYENCODER_H_ + + + + + +#endif /* SWROTARYENCODER_H_ */ diff --git a/libraries/RotaryEncoder/examples/OneKnob/OneKnob.ino b/libraries/RotaryEncoder/examples/OneKnob/OneKnob.ino index 71aef238..c3f3eeae 100644 --- a/libraries/RotaryEncoder/examples/OneKnob/OneKnob.ino +++ b/libraries/RotaryEncoder/examples/OneKnob/OneKnob.ino @@ -21,8 +21,8 @@ void setup() { Serial.begin(115200); - Serial.println("Bluefruit52 Hardware Encoder OneKnob Example"); - Serial.println("--------------------------------------------\n"); + Serial.println("Bluefruit52 Rotary Encoder Polling Example"); + Serial.println("------------------------------------------\n"); // Initialize Encoder RotaryEncoder.begin(PIN_A, PIN_B); diff --git a/libraries/RotaryEncoder/examples/OneKnobCallback/OneKnobCallback.ino b/libraries/RotaryEncoder/examples/OneKnobCallback/OneKnobCallback.ino new file mode 100644 index 00000000..3bbce068 --- /dev/null +++ b/libraries/RotaryEncoder/examples/OneKnobCallback/OneKnobCallback.ino @@ -0,0 +1,52 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include +#include "RotaryEncoder.h" + +#define PIN_A A0 +#define PIN_B A1 + +void setup() +{ + Serial.begin(115200); + Serial.println("Bluefruit52 Rotary Encoder Callback Example"); + Serial.println("-------------------------------------------\n"); + + // Initialize Encoder + RotaryEncoder.begin(PIN_A, PIN_B); + + // Set callback + RotaryEncoder.setCallback(encoder_callback); + + // Start encoder + RotaryEncoder.start(); +} + +void loop() +{ + // do nothing +} + +void encoder_callback(int step) +{ + if ( step > 0 ) + { + Serial.println("Left"); + }else + { + Serial.println("Right"); + } +} +