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"
8
10using AH::interrupt_t;
11
12// Use IRAM_ATTR for ISRs to prevent ESP8266 resets
13#if defined(ESP8266) || defined(ESP32)
14#define CS_ENCODER_ISR_ATTR IRAM_ATTR
15#else
16#define CS_ENCODER_ISR_ATTR
17#endif
18
20#define CS_ENCODER_ARGLIST_SIZE CORE_NUM_INTERRUPT
21
24class AHEncoder {
25 public:
38
41 AHEncoder(const AHEncoder &) = delete;
44 AHEncoder &operator=(const AHEncoder &) = delete;
46 friend void swap(AHEncoder &a, AHEncoder &b);
47
49 AHEncoder(AHEncoder &&other);
52
54 ~AHEncoder();
55
59 void begin();
61 void end();
62
63 public:
65 int32_t read();
68 int32_t readAndReset(int32_t newpos = 0);
70 void write(int32_t p);
71
72 private:
73 using isr_func_t = void (*)();
75 template <interrupt_t NumISR = CS_ENCODER_ARGLIST_SIZE>
76 static isr_func_t get_isr(interrupt_t interrupt);
78 void attachInterruptCtx(interrupt_t interrupt);
80 void detachInterruptCtx(interrupt_t interrupt);
81
82 private:
84 uint8_t interrupts_in_use = 0;
85 uint8_t state = 0;
88
89 private:
95};
96
98
99#include "AHEncoder.ipp"
#define CS_ENCODER_ISR_ATTR
Definition AHEncoder.hpp:16
#define CS_ENCODER_ARGLIST_SIZE
Available number of interrupts.
Definition AHEncoder.hpp:20
AH::function_traits< decltype(::digitalWrite)>::argument_t< 0 > ArduinoPin_t
#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:24
AH::Array< ArduinoPin_t, 2 > pins
Definition AHEncoder.hpp:83
uint8_t state
Definition AHEncoder.hpp:85
AtomicPosition< int32_t > position
Definition AHEncoder.hpp:87
void(*)() isr_func_t
The type of a handler function.
Definition AHEncoder.hpp:73
static AHEncoder * instance_table[NUM_DIGITAL_PINS]
Array of pointers to all instances with active interrupts.
void write(int32_t p)
Set the absolute position to the given value.
AHEncoder(const AHEncoder &)=delete
Copy constructor: copying an Encoder object is semantically meaningless, so it has been deleted.
void attachInterruptCtx(interrupt_t interrupt)
Register the interrupt handler for this instance.
~AHEncoder()
Destructor, detaches the interrupts.
Definition AHEncoder.cpp:72
AHEncoder(ArduinoPin_t pinA, ArduinoPin_t pinB)
Constructor.
Definition AHEncoder.cpp:14
uint8_t interrupts_in_use
Definition AHEncoder.hpp:84
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
void detachInterruptCtx(interrupt_t interrupt)
Un-register the interrupt handler for this instance.
AHEncoder & operator=(const AHEncoder &)=delete
Copy assignment: copying an Encoder object is semantically meaningless, so it has been deleted.
static isr_func_t get_isr(interrupt_t interrupt)
Get a pointer to the interrupt handler function for the given interrupt.
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:86
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...