add callback support

This commit is contained in:
hathach committed 2017-09-14 01:09:03 +07:00
1 parent 337b683e9f
commit 23dca4bc75
6 files changed
+155 -10

No files matched your search

+16 -7
View File
@@ -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();
}
+4 -1
View File
@@ -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;
@@ -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.
*/
/**************************************************************************/
+43
View File
@@ -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_ */
@@ -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);
@@ -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 <Arduino.h>
#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");
}
}