tested software encoder

This commit is contained in:
hathach committed 2017-09-14 23:09:05 +07:00
1 parent 97114cb54d
commit 2e61dab738
6 files changed
+68 -10

No files matched your search

+2 -1
View File
@@ -151,7 +151,8 @@ int32_t HwRotaryEncoder::read(void)
// Trigger READ CLR ACC
NRF_QDEC->TASKS_RDCLRACC = 1;
int32_t val = NRF_QDEC->ACCREAD;
// Nordic QDEC CW is negative, CCW is positve --> invert
int32_t val = -(NRF_QDEC->ACCREAD);
// Add to absolute value and buffered step
_abs += val;
+11 -1
View File
@@ -65,12 +65,15 @@ static voidFuncPtr _pina_irq_ptr[SW_ROTARY_ENCODER_MAX_INSTANCE] =
_pina_irqn_4, _pina_irqn_5, _pina_irqn_6, _pina_irqn_7
};
bool SwRotaryEncoder::begin(void)
bool SwRotaryEncoder::begin(uint8_t pina, uint8_t pinb)
{
// Add to pointer array
VERIFY ( _encoder_count < SW_ROTARY_ENCODER_MAX_INSTANCE);
_encoder_ptr[_encoder_count] = this;
_pina = pina;
_pinb = pinb;
pinMode(_pina, INPUT);
pinMode(_pinb, INPUT);
@@ -81,6 +84,11 @@ bool SwRotaryEncoder::begin(void)
_encoder_count++;
}
void SwRotaryEncoder::stop(void)
{
detachInterrupt(_pina);
}
int32_t SwRotaryEncoder::read(void)
{
int32_t ret = _abs - _last_read;
@@ -116,6 +124,8 @@ void SwRotaryEncoder::_irq_handler(void)
{
_abs--;
}
_a_last = val;
}
}
+4 -4
View File
@@ -43,16 +43,16 @@
class SwRotaryEncoder
{
public:
SwRotaryEncoder(uint8_t pina, uint8_t pinb)
SwRotaryEncoder(void)
{
_pina = pina;
_pinb = pinb;
_pina = _pinb = 0;
_a_last = 0;
_last_read = _abs = 0;
}
bool begin(void);
bool begin(uint8_t pina, uint8_t pinb);
void stop(void);
int32_t read(void);
int32_t readAbs(void);
@@ -43,10 +43,10 @@ void encoder_callback(int step)
{
if ( step > 0 )
{
Serial.println("Left");
Serial.println("Right");
}else
{
Serial.println("Right");
Serial.println("Left");
}
}
@@ -39,10 +39,10 @@ void loop()
{
if ( value > 0 )
{
Serial.println("Left");
Serial.println("Right");
}else
{
Serial.println("Right");
Serial.println("Left");
}
}
}
@@ -0,0 +1,47 @@
/*********************************************************************
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
SwRotaryEncoder swEncoder;
void setup()
{
Serial.begin(115200);
Serial.println("Bluefruit52 SW Rotary Encoder Polling Example");
Serial.println("---------------------------------------------\n");
// Initialize Encoder
swEncoder.begin(PIN_A, PIN_B);
}
void loop()
{
int value = swEncoder.read();
if (value)
{
if ( value > 0 )
{
Serial.println("Right");
}else
{
Serial.println("Left");
}
}
}