Control Surface master
MIDI Control Surface library for Arduino
Encoder.h
Go to the documentation of this file.
1/* Encoder Library, for measuring quadrature encoded signals
2 * http://www.pjrc.com/teensy/td_libs_Encoder.html
3 * Copyright (c) 2011,2013 PJRC.COM, LLC - Paul Stoffregen <paul@pjrc.com>
4 *
5 * April-September 2020 - Pieter P:
6 * make encoders moveable and add begin method for deterministic
7 * initialization, large cleanup
8 * Version 1.2 - fix -2 bug in C-only code
9 * Version 1.1 - expand to support boards with up to 60 interrupts
10 * Version 1.0 - initial release
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
29 */
30
31#pragma once
32
33#include <AH/Arduino-Wrapper.h>
34#include <Settings/NamespaceSettings.hpp>
35
37#include "utility/move.h"
39
41
42#define ENCODER_ARGLIST_SIZE CORE_NUM_INTERRUPT
43
49 volatile IO_REG_TYPE *pin1_register;
50 volatile IO_REG_TYPE *pin2_register;
51 IO_REG_TYPE pin1_bitmask;
52 IO_REG_TYPE pin2_bitmask;
53 uint8_t state;
54 int32_t position;
55};
56
59class Encoder {
60 public:
72 Encoder(uint8_t pin1, uint8_t pin2);
73
76 Encoder(const Encoder &) = delete;
79 Encoder &operator=(const Encoder &) = delete;
80
82 Encoder(Encoder &&other);
84 Encoder &operator=(Encoder &&other);
85
87 ~Encoder();
88
92 void begin();
94 void end();
95
96 public:
98 int32_t read();
101 int32_t readAndReset();
103 void write(int32_t p);
104
105 private:
106 void attachInterruptCtx(int interrupt);
107 void detachInterruptCtx(int interrupt);
108
109 private:
111 uint8_t pin1, pin2;
112 uint8_t interrupts_in_use = 0;
113
114
115 public:
119 static void update(Encoder_internal_state_t *arg);
123};
124
126
127#include "Encoder.ipp"
#define ENCODER_ARGLIST_SIZE
Definition: Encoder.h:42
IO_REG_TYPE pin1_bitmask
Definition: Encoder.h:51
volatile IO_REG_TYPE * pin1_register
Definition: Encoder.h:49
volatile IO_REG_TYPE * pin2_register
Definition: Encoder.h:50
IO_REG_TYPE pin2_bitmask
Definition: Encoder.h:52
All the data needed by interrupts is consolidated into this ugly struct to facilitate assembly langua...
Definition: Encoder.h:48
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Class for reading quadrature encoders, based on http://www.pjrc.com/teensy/td_libs_Encoder....
Definition: Encoder.h:59
Encoder(uint8_t pin1, uint8_t pin2)
Constructor.
Definition: Encoder.cpp:9
Encoder(const Encoder &)=delete
Copy constructor: copying an Encoder object is semantically meaningless, so it has been deleted.
Encoder_internal_state_t encoder
Definition: Encoder.h:110
int32_t readAndReset()
Read the current absolute position of the encoder and reset it to zero afterwards.
Definition: Encoder.ipp:17
void attachInterruptCtx(int interrupt)
Definition: Encoder.cpp:94
void write(int32_t p)
Set the absolute position to the given value.
Definition: Encoder.ipp:30
void detachInterruptCtx(int interrupt)
Definition: Encoder.cpp:102
static Encoder_internal_state_t * interruptArgs[CORE_NUM_INTERRUPT]
Similarly to update(), don't use these interrupt handler arguments from your sketch.
Definition: Encoder.h:122
static void update(Encoder_internal_state_t *arg)
update() is not meant to be called from outside Encoder, but it is public to allow static interrupt r...
Definition: Encoder-AVR.ipp:1
~Encoder()
Destructor.
Definition: Encoder.cpp:62
uint8_t interrupts_in_use
Definition: Encoder.h:112
int32_t read()
Read the current absolute position of the encoder.
Definition: Encoder.ipp:5
void end()
Disable the interrupts used by this encoder.
Definition: Encoder.cpp:87
void begin()
Initialize this encoder by enabling the pull-up resistors and attaching the interrupts handlers (if i...
Definition: Encoder.cpp:69
uint8_t pin1
Definition: Encoder.h:111
uint8_t pin2
Definition: Encoder.h:111
Encoder & operator=(const Encoder &)=delete
Copy assignment: copying an Encoder object is semantically meaningless, so it has been deleted.