Control Surface master
MIDI Control Surface library for Arduino
Encoder.ipp
Go to the documentation of this file.
1#include "Encoder.h"
2
4
5inline int32_t Encoder::read() {
6 if (interrupts_in_use < 2) {
7 noInterrupts();
9 } else {
10 noInterrupts();
11 }
12 int32_t ret = encoder.position;
13 interrupts();
14 return ret;
15}
16
17inline int32_t Encoder::readAndReset() {
18 if (interrupts_in_use < 2) {
19 noInterrupts();
21 } else {
22 noInterrupts();
23 }
24 int32_t ret = encoder.position;
25 encoder.position = 0;
26 interrupts();
27 return ret;
28}
29
30inline void Encoder::write(int32_t p) {
31 noInterrupts();
32 encoder.position = p;
33 interrupts();
34}
35
36// _______ _______
37// Pin1 ______| |_______| |______ Pin1
38// negative <--- _______ _______ __ --> positive
39// Pin2 __| |_______| |_______| Pin2
40
41// new new old old
42// pin2 pin1 pin2 pin1 Result
43// ---- ---- ---- ---- ------
44// 0 0 0 0 no movement
45// 0 0 0 1 +1
46// 0 0 1 0 -1
47// 0 0 1 1 +2 (assume pin1 edges only)
48// 0 1 0 0 -1
49// 0 1 0 1 no movement
50// 0 1 1 0 -2 (assume pin1 edges only)
51// 0 1 1 1 +1
52// 1 0 0 0 +1
53// 1 0 0 1 -2 (assume pin1 edges only)
54// 1 0 1 0 no movement
55// 1 0 1 1 -1
56// 1 1 0 0 +2 (assume pin1 edges only)
57// 1 1 0 1 -1
58// 1 1 1 0 +1
59// 1 1 1 1 no movement
60
61/*
62 // Simple, easy-to-read "documentation" version :-)
63 //
64 void update(void) {
65 uint8_t s = state & 3;
66 if (digitalRead(pin1)) s |= 4;
67 if (digitalRead(pin2)) s |= 8;
68 switch (s) {
69 case 0: case 5: case 10: case 15:
70 break;
71 case 1: case 7: case 8: case 14:
72 position++; break;
73 case 2: case 4: case 11: case 13:
74 position--; break;
75 case 3: case 12:
76 position += 2; break;
77 default:
78 position -= 2; break;
79 }
80 state = (s >> 2);
81 }
82*/
83
84#if defined(__AVR__)
85#include "Encoder-AVR.ipp"
86#else
88 uint8_t p1val = DIRECT_PIN_READ(arg->pin1_register, arg->pin1_bitmask);
89 uint8_t p2val = DIRECT_PIN_READ(arg->pin2_register, arg->pin2_bitmask);
90 uint8_t state = arg->state & 3;
91 if (p1val)
92 state |= 4;
93 if (p2val)
94 state |= 8;
95 arg->state = (state >> 2);
96 switch (state) {
97 case 1:
98 case 7:
99 case 8:
100 case 14: arg->position++; return;
101 case 2:
102 case 4:
103 case 11:
104 case 13: arg->position--; return;
105 case 3:
106 case 12: arg->position += 2; return;
107 case 6:
108 case 9: arg->position -= 2; return;
109 }
110}
111#endif
112
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
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 write(int32_t p)
Set the absolute position to the given value.
Definition: Encoder.ipp:30
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
uint8_t interrupts_in_use
Definition: Encoder.h:112
int32_t read()
Read the current absolute position of the encoder.
Definition: Encoder.ipp:5