Control Surface main
MIDI Control Surface library for Arduino
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"
6
8
9// Use IRAM_ATTR for ISRs to prevent ESP8266 resets
10#if defined(ESP8266) || defined(ESP32)
11#define CS_ENCODER_ISR_ATTR IRAM_ATTR
12#else
13#define CS_ENCODER_ISR_ATTR
14#endif
15
16// Largest interrupt number.
17#define CS_ENCODER_ARGLIST_SIZE CORE_NUM_INTERRUPT
18
21class AHEncoder {
22 public:
34 AHEncoder(uint8_t pinA, uint8_t pinB);
35
38 AHEncoder(const AHEncoder &) = delete;
41 AHEncoder &operator=(const AHEncoder &) = delete;
43 friend void swap(AHEncoder &a, AHEncoder &b);
44
46 AHEncoder(AHEncoder &&other);
49
51 ~AHEncoder();
52
56 void begin();
58 void end();
59
60 public:
62 int32_t read();
65 int32_t readAndReset(int32_t newpos = 0);
67 void write(int32_t p);
68
69 private:
70 using isr_func_t = void (*)();
72 template <unsigned NumISR = CS_ENCODER_ARGLIST_SIZE>
73 static isr_func_t get_isr(unsigned interrupt);
75 void attachInterruptCtx(int interrupt);
77 void detachInterruptCtx(int interrupt);
78
79 private:
80 uint8_t pins[2];
81 uint8_t interrupts_in_use = 0;
82 uint8_t state = 0;
85
86 private:
92};
93
95
96#include "AHEncoder.ipp"
#define CS_ENCODER_ISR_ATTR
Definition: AHEncoder.hpp:13
#define CS_ENCODER_ARGLIST_SIZE
Definition: AHEncoder.hpp:17
#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:21
uint8_t state
Definition: AHEncoder.hpp:82
AtomicPosition< int32_t > position
Definition: AHEncoder.hpp:84
void(*)() isr_func_t
The type of a handler function.
Definition: AHEncoder.hpp:70
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.
Definition: AHEncoder.hpp:91
void attachInterruptCtx(int interrupt)
Register the interrupt handler for this instance.
Definition: AHEncoder.cpp:100
void write(int32_t p)
Set the absolute position to the given value.
Definition: AHEncoder.ipp:31
void detachInterruptCtx(int interrupt)
Un-register the interrupt handler for this instance.
Definition: AHEncoder.cpp:121
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:9
~AHEncoder()
Destructor, detaches the interrupts.
Definition: AHEncoder.cpp:63
uint8_t pins[2]
Definition: AHEncoder.hpp:80
uint8_t interrupts_in_use
Definition: AHEncoder.hpp:81
int32_t read()
Read the current absolute position of the encoder.
Definition: AHEncoder.ipp:19
void end()
Disable the interrupts used by this encoder.
Definition: AHEncoder.cpp:93
int32_t readAndReset(int32_t newpos=0)
Read the current absolute position of the encoder and reset it to zero afterwards.
Definition: AHEncoder.ipp:25
void begin()
Initialize this encoder by enabling the pull-up resistors and attaching the interrupts handlers (if i...
Definition: AHEncoder.cpp:70
AHEncoder & operator=(const AHEncoder &)=delete
Copy assignment: copying an Encoder object is semantically meaningless, so it has been deleted.
DirectPinRead direct_pins[2]
Definition: AHEncoder.hpp:83
friend void swap(AHEncoder &a, AHEncoder &b)
Swap two Encoder objects.
Definition: AHEncoder.cpp:25
void update()
Private handler function that is called from the ISR.
Definition: AHEncoder.ipp:62