Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
AHEncoder.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "AtomicPosition.hpp"
4#include "DirectPinRead.hpp"
5#include "NumInterrupts.hpp"
7
9
10// Use IRAM_ATTR for ISRs to prevent ESP8266 resets
11#if defined(ESP8266) || defined(ESP32)
12#define CS_ENCODER_ISR_ATTR IRAM_ATTR
13#else
14#define CS_ENCODER_ISR_ATTR
15#endif
16
17// Largest interrupt number.
18#define CS_ENCODER_ARGLIST_SIZE CORE_NUM_INTERRUPT
19
22class AHEncoder {
23 public:
35 AHEncoder(uint8_t pinA, uint8_t pinB);
36
39 AHEncoder(const AHEncoder &) = delete;
42 AHEncoder &operator=(const AHEncoder &) = delete;
44 friend void swap(AHEncoder &a, AHEncoder &b);
45
47 AHEncoder(AHEncoder &&other);
50
52 ~AHEncoder();
53
57 void begin();
59 void end();
60
61 public:
63 int32_t read();
66 int32_t readAndReset(int32_t newpos = 0);
68 void write(int32_t p);
69
70 private:
71 using isr_func_t = void (*)();
73 template <unsigned NumISR = CS_ENCODER_ARGLIST_SIZE>
74 static isr_func_t get_isr(unsigned interrupt);
76 void attachInterruptCtx(int interrupt);
78 void detachInterruptCtx(int interrupt);
79
80 private:
82 uint8_t interrupts_in_use = 0;
83 uint8_t state = 0;
86
87 private:
93};
94
96
97#include "AHEncoder.ipp"
#define CS_ENCODER_ISR_ATTR
Definition AHEncoder.hpp:14
#define CS_ENCODER_ARGLIST_SIZE
Definition AHEncoder.hpp:18
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Class for reading quadrature encoders, heavily influenced by http://www.pjrc.com/teensy/td_libs_Encod...
Definition AHEncoder.hpp:22
uint8_t state
Definition AHEncoder.hpp:83
AtomicPosition< int32_t > position
Definition AHEncoder.hpp:85
void(*)() isr_func_t
The type of a handler function.
Definition AHEncoder.hpp:71
static isr_func_t get_isr(unsigned interrupt)
Get a pointer to the interrupt handler function for the given interrupt.
static AHEncoder * instance_table[NUM_DIGITAL_PINS]
Array of pointers to all instances with active interrupts.
void attachInterruptCtx(int interrupt)
Register the interrupt handler for this instance.
void write(int32_t p)
Set the absolute position to the given value.
void detachInterruptCtx(int interrupt)
Un-register the interrupt handler for this instance.
AHEncoder(const AHEncoder &)=delete
Copy constructor: copying an Encoder object is semantically meaningless, so it has been deleted.
AHEncoder(uint8_t pinA, uint8_t pinB)
Constructor.
Definition AHEncoder.cpp:14
~AHEncoder()
Destructor, detaches the interrupts.
Definition AHEncoder.cpp:72
uint8_t interrupts_in_use
Definition AHEncoder.hpp:82
AH::Array< uint8_t, 2 > pins
Definition AHEncoder.hpp:81
int32_t read()
Read the current absolute position of the encoder.
void end()
Disable the interrupts used by this encoder.
int32_t readAndReset(int32_t newpos=0)
Read the current absolute position of the encoder and reset it to zero afterwards.
void begin()
Initialize this encoder by enabling the pull-up resistors and attaching the interrupts handlers (if i...
Definition AHEncoder.cpp:79
AHEncoder & operator=(const AHEncoder &)=delete
Copy assignment: copying an Encoder object is semantically meaningless, so it has been deleted.
friend void swap(AHEncoder &a, AHEncoder &b)
Swap two Encoder objects.
Definition AHEncoder.cpp:34
void update()
Private handler function that is called from the ISR.
AH::Array< DirectPinRead, 2 > direct_pins
Definition AHEncoder.hpp:84
A class that reads and filters an analog input.