update Encoder to singleton pattern
This commit is contained in:
1 parent
9177cb48db
commit
10e72573a5
3 files changed
+59
-15
No files matched your search
@@ -36,11 +36,17 @@
|
||||
|
||||
#include "RotaryEncoder.h"
|
||||
|
||||
class RotaryEncoder Qei;
|
||||
|
||||
/**
|
||||
* Initialize Hardware Encoder
|
||||
*/
|
||||
void RotaryEncoder::begin(void)
|
||||
void RotaryEncoder::begin(int8_t pina, int8_t pinb, int8_t pinled)
|
||||
{
|
||||
_pina = pina;
|
||||
_pinb = pinb;
|
||||
_pinled = pinled;
|
||||
|
||||
// default sample period
|
||||
NRF_QDEC->SAMPLEPER = QDEC_SAMPLEPER_SAMPLEPER_128us;
|
||||
|
||||
@@ -67,8 +73,11 @@ void RotaryEncoder::begin(void)
|
||||
void RotaryEncoder::start(void)
|
||||
{
|
||||
// Enable IRQ
|
||||
// NVIC_ClearPendingIRQ(IRQn);
|
||||
// NVIC_EnableIRQ(IRQn);
|
||||
if ( NRF_QDEC->INTENSET )
|
||||
{
|
||||
NVIC_ClearPendingIRQ(QDEC_IRQn);
|
||||
NVIC_EnableIRQ(QDEC_IRQn);
|
||||
}
|
||||
|
||||
// Enable
|
||||
NRF_QDEC->ENABLE = 1;
|
||||
@@ -79,6 +88,11 @@ void RotaryEncoder::start(void)
|
||||
|
||||
void RotaryEncoder::stop(void)
|
||||
{
|
||||
if ( NRF_QDEC->INTENSET )
|
||||
{
|
||||
NVIC_DisableIRQ(QDEC_IRQn);
|
||||
}
|
||||
|
||||
// Stop
|
||||
NRF_QDEC->TASKS_STOP = 1;
|
||||
|
||||
@@ -118,6 +132,19 @@ void RotaryEncoder::setReporter(int8_t sample_num)
|
||||
}
|
||||
}
|
||||
|
||||
void RotaryEncoder::setCallback(callback_t fp)
|
||||
{
|
||||
_cb = fp;
|
||||
|
||||
if (fp)
|
||||
{
|
||||
NRF_QDEC->INTENSET = QDEC_INTENSET_SAMPLERDY_Msk;
|
||||
}else
|
||||
{
|
||||
NRF_QDEC->INTENCLR = QDEC_INTENCLR_SAMPLERDY_Msk;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t RotaryEncoder::read(void)
|
||||
{
|
||||
// Trigger READ CLR ACC
|
||||
@@ -160,7 +187,17 @@ void RotaryEncoder::clearAbs(void)
|
||||
_abs = 0;
|
||||
}
|
||||
|
||||
|
||||
void QDEC_IRQHandler(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);
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -41,17 +41,18 @@
|
||||
class RotaryEncoder
|
||||
{
|
||||
public:
|
||||
RotaryEncoder(uint8_t pina, uint8_t pinb, int8_t pinled = -1)
|
||||
typedef void (*callback_t) (int32_t step);
|
||||
|
||||
RotaryEncoder(void)
|
||||
{
|
||||
_pina = pina;
|
||||
_pinb = pinb;
|
||||
_pinled = pinled;
|
||||
_pina = _pinb = _pinled = -1;
|
||||
|
||||
_abs = 0;
|
||||
_step = 0;
|
||||
_cb = NULL;
|
||||
}
|
||||
|
||||
void begin(void);
|
||||
void begin(int8_t pina, int8_t pinb, int8_t pinled = -1);
|
||||
|
||||
void setSampler(uint8_t period);
|
||||
void setDebounce(bool enable);
|
||||
@@ -65,15 +66,20 @@ class RotaryEncoder
|
||||
void writeAbs(int32_t value);
|
||||
void clearAbs(void);
|
||||
|
||||
void setCallback(callback_t fp);
|
||||
|
||||
private:
|
||||
uint8_t _pina, _pinb;
|
||||
int8_t _pinled;
|
||||
int8_t _pina, _pinb;
|
||||
int8_t _pinled;
|
||||
|
||||
// Note For each turn, encoder generate 2 transitions
|
||||
int32_t _abs; // Absolute position
|
||||
int32_t _step; // Moving step that take intermidiate transition in account
|
||||
int32_t _step; // Moving step that take intermediate transition in account
|
||||
|
||||
callback_t _cb;
|
||||
};
|
||||
|
||||
extern class RotaryEncoder Qei;
|
||||
|
||||
|
||||
#endif /* ROTARYENCODER_H_ */
|
||||
@@ -15,7 +15,8 @@
|
||||
#include <Arduino.h>
|
||||
#include "RotaryEncoder.h"
|
||||
|
||||
RotaryEncoder encoder(A0, A1);
|
||||
#define PIN_A A0
|
||||
#define PIN_B A1
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -24,15 +25,15 @@ void setup()
|
||||
Serial.println("--------------------------------------------\n");
|
||||
|
||||
// Initialize Encoder
|
||||
encoder.begin();
|
||||
Qei.begin(PIN_A, PIN_B);
|
||||
|
||||
// Start encoder
|
||||
encoder.start();
|
||||
Qei.start();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int value = encoder.read();
|
||||
int value = Qei.read();
|
||||
|
||||
if (value)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user