-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Encoder code, Encoder → AHEncoder
- Loading branch information
Showing
31 changed files
with
737 additions
and
1,072 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* can be used for changing effect parameters, volumes, pan and balance | ||
* controls, etc. | ||
* | ||
* @boards AVR, AVR USB, Nano Every, Due, Nano 33 IoT, Nano 33 BLE, Pi Pico, Teensy 3.x, ESP32 | ||
* @boards AVR, AVR USB, Nano Every, Due, Nano 33 IoT, Nano 33 BLE, Pi Pico, Teensy 3.x, ESP32, ESP8266 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tttapa
Author
Owner
|
||
* | ||
* Connections | ||
* ----------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#ifdef ARDUINO | ||
|
||
#include "AHEncoder.hpp" | ||
|
||
#include <AH/STL/utility> // std::swap | ||
|
||
BEGIN_CS_NAMESPACE | ||
|
||
#include "codegen/ISRs-def.ipp" | ||
|
||
AHEncoder::AHEncoder(uint8_t pinA, uint8_t pinB) | ||
: pins {pinA, pinB}, direct_pins {direct_pin_read(pinA), | ||
direct_pin_read(pinB)} { | ||
// It's much faster to use the GPIO registers directly, rather than | ||
// calling digitalRead every time we need to read a pin. | ||
// digitalRead looks up the register and bitmasks every time it's called | ||
// but here, we look them up once in the constructor. | ||
} | ||
|
||
AHEncoder::AHEncoder(AHEncoder &&other) { swap(*this, other); } | ||
|
||
AHEncoder &AHEncoder::operator=(AHEncoder &&other) { | ||
swap(*this, other); | ||
return *this; | ||
} | ||
|
||
void swap(AHEncoder &a, AHEncoder &b) { | ||
// First swap the normal member variables: | ||
std::swap(a.interrupts_in_use, b.interrupts_in_use); | ||
std::swap(a.pins, b.pins); | ||
|
||
// Next, update the pointers in interruptArgs: | ||
// When interrupts are in use, there is a global interrupt context | ||
// variable that holds a pointer to the encoders that are being swapped | ||
// or moved. | ||
// After moving, this pointer would no longer be valid, so it has to be | ||
// changed to point to the new encoder object. | ||
// Calling attachInterrupt is not necessary, because this should already | ||
// have happened in the begin method if interrupts_in_use is nonzero. | ||
// Before messing with the state variables that can be changed or | ||
// accessed from within an ISR, we have to disable interrupts. | ||
noInterrupts(); | ||
std::swap(a.state, b.state); | ||
std::swap(a.direct_pins, b.direct_pins); | ||
std::swap(a.position, b.position); | ||
if (a.interrupts_in_use > 0) { | ||
int int1 = digitalPinToInterrupt(a.pins[0]); | ||
if (int1 != NOT_AN_INTERRUPT) | ||
AHEncoder::interruptArgs[int1] = &a; | ||
int int2 = digitalPinToInterrupt(a.pins[1]); | ||
if (int2 != NOT_AN_INTERRUPT) | ||
AHEncoder::interruptArgs[int2] = &a; | ||
} | ||
if (b.interrupts_in_use > 0) { | ||
int int1 = digitalPinToInterrupt(b.pins[0]); | ||
if (int1 != NOT_AN_INTERRUPT) | ||
AHEncoder::interruptArgs[int1] = &b; | ||
int int2 = digitalPinToInterrupt(b.pins[1]); | ||
if (int2 != NOT_AN_INTERRUPT) | ||
AHEncoder::interruptArgs[int2] = &b; | ||
} | ||
interrupts(); | ||
} | ||
|
||
AHEncoder::~AHEncoder() { | ||
// If interrupts are in use, there are dangling pointers to the encoder | ||
// state in the global interrupt contexts. These have to be detached | ||
// when the encoder object is removed. | ||
end(); | ||
} | ||
|
||
void AHEncoder::begin() { | ||
pinMode(pins[0], INPUT_PULLUP); | ||
pinMode(pins[1], INPUT_PULLUP); | ||
// allow time for a passive R-C filter to charge | ||
// through the pullup resistors, before reading | ||
// the initial state | ||
delayMicroseconds(2000); | ||
uint8_t s = 0; | ||
if (direct_pins[0].read()) | ||
s |= 1; | ||
if (direct_pins[1].read()) | ||
s |= 2; | ||
state = s; | ||
|
||
attachInterruptCtx(digitalPinToInterrupt(pins[0])); | ||
attachInterruptCtx(digitalPinToInterrupt(pins[1])); | ||
#if defined(ARDUINO_ARCH_MBED) && !defined(ARDUINO_ARCH_RP2040) | ||
// https://github.com/arduino/ArduinoCore-mbed/issues/253 | ||
pinMode(pins[0], INPUT_PULLUP); | ||
pinMode(pins[1], INPUT_PULLUP); | ||
#endif | ||
} | ||
|
||
void AHEncoder::end() { | ||
if (interrupts_in_use > 0) { | ||
detachInterruptCtx(digitalPinToInterrupt(pins[0])); | ||
detachInterruptCtx(digitalPinToInterrupt(pins[1])); | ||
} | ||
} | ||
|
||
void AHEncoder::attachInterruptCtx(int interrupt) { | ||
if (interrupt != NOT_AN_INTERRUPT) { | ||
interruptArgs[interrupt] = this; | ||
++interrupts_in_use; | ||
#ifdef ARDUINO_ARCH_RP2040 | ||
gpio_set_irq_enabled_with_callback( | ||
interrupt, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, | ||
[](uint gpio, uint32_t) { | ||
if (auto arg = interruptArgs[gpio]) | ||
arg->update(); | ||
}); | ||
#else | ||
attachInterrupt(interrupt, EncoderISRs::getISR(interrupt), CHANGE); | ||
#endif | ||
} | ||
} | ||
|
||
void AHEncoder::detachInterruptCtx(int interrupt) { | ||
if (interrupt != NOT_AN_INTERRUPT) { | ||
#ifdef ARDUINO_ARCH_RP2040 | ||
gpio_set_irq_enabled(interrupt, GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, | ||
false); | ||
#else | ||
detachInterrupt(interrupt); | ||
#endif | ||
--interrupts_in_use; | ||
interruptArgs[interrupt] = nullptr; | ||
} | ||
} | ||
|
||
AHEncoder *AHEncoder::interruptArgs[CS_ENCODER_ARGLIST_SIZE] {}; | ||
|
||
END_CS_NAMESPACE | ||
|
||
#endif |
Oops, something went wrong.
i have problem compiling with a node mcu32 esp32 board