-
Notifications
You must be signed in to change notification settings - Fork 64
/
Atm_encoder.cpp
142 lines (128 loc) · 3.66 KB
/
Atm_encoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "Atm_encoder.hpp"
#include <limits.h>
// Loosely based on https://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino (Oleg Mazurov)
const char Atm_encoder::enc_states[16] = {0, (char)-1, 1, 0, 1, 0, 0, (char)-1, (char)-1, 0, 0, 1, 0, 1, (char)-1, 0};
Atm_encoder& Atm_encoder::begin( int pin1, int pin2, int divider /* = 1 */ ) {
// clang-format off
const static state_t state_table[] PROGMEM = {
/* ON_ENTER ON_LOOP ON_EXIT EVT_UP EVT_DOWN ELSE */
/* IDLE */ -1, LP_IDLE, -1, UP, DOWN, -1,
/* UP */ ENT_UP, -1, -1, -1, -1, IDLE,
/* DOWN */ ENT_DOWN, -1, -1, -1, -1, IDLE,
};
// clang-format on
Machine::begin( state_table, ELSE );
this->pin1 = pin1;
this->pin2 = pin2;
this->divider = divider;
pinMode( pin1, INPUT );
pinMode( pin2, INPUT );
digitalWrite( pin1, HIGH );
digitalWrite( pin2, HIGH );
min = INT_MIN;
max = INT_MAX;
value = 0;
return *this;
}
int Atm_encoder::event( int id ) {
switch ( id ) {
case EVT_UP:
return enc_direction == +1 && ( enc_counter % divider == 0 );
case EVT_DOWN:
return enc_direction == -1 && ( enc_counter % divider == 0 );
}
return 0;
}
void Atm_encoder::action( int id ) {
switch ( id ) {
case LP_IDLE:
enc_bits = ( ( enc_bits << 2 ) | ( digitalRead( pin1 ) << 1 ) | ( digitalRead( pin2 ) ) ) & 0x0f;
enc_direction = enc_states[enc_bits];
if ( enc_direction != 0 ) {
enc_counter = enc_counter + enc_direction;
if ( ( enc_counter != 0 ) && ( enc_counter % divider == 0 ) ) {
if ( !count( enc_direction ) ) {
enc_direction = 0;
}
}
}
return;
case ENT_UP:
onup.push( state(), 1 );
return;
case ENT_DOWN:
ondown.push( state(), 0 );
return;
}
}
Atm_encoder& Atm_encoder::range( int min, int max, bool wrap /* = false */ ) {
if ( min > max ) {
range_invert = true;
this->min = max;
this->max = min;
} else {
range_invert = false;
this->min = min;
this->max = max;
}
this->wrap = wrap;
if ( value < min || value > max ) {
value = min;
}
return *this;
}
Atm_encoder& Atm_encoder::set( int value ) {
this->value = range_invert ? map( value, min, max, max, min ) : value;
return *this;
}
Atm_encoder& Atm_encoder::onChange( Machine& machine, int event /* = 0 */ ) {
onup.set( &machine, event );
ondown.set( &machine, event );
return *this;
}
Atm_encoder& Atm_encoder::onChange( atm_cb_push_t callback, int idx /* = 0 */ ) {
onup.set( callback, idx );
ondown.set( callback, idx );
return *this;
}
Atm_encoder& Atm_encoder::onChange( bool status, Machine& machine, int event /* = 0 */ ) {
if ( status ) {
onup.set( &machine, event );
} else {
ondown.set( &machine, event );
}
return *this;
}
Atm_encoder& Atm_encoder::onChange( bool status, atm_cb_push_t callback, int idx /* = 0 */ ) {
if ( status ) {
onup.set( callback, idx );
} else {
ondown.set( callback, idx );
}
return *this;
}
int Atm_encoder::state( void ) {
return range_invert ? map( value, min, max, max, min ) : value;
}
bool Atm_encoder::count( int direction ) {
if ( (long)value + direction > max ) {
if ( wrap ) {
value = min;
} else {
return false;
}
} else if ( (long)value + direction < min ) {
if ( wrap ) {
value = max;
} else {
return false;
}
} else {
value += direction;
}
return true;
}
Atm_encoder& Atm_encoder::trace( Stream& stream ) {
Machine::setTrace( &stream, atm_serial_debug::trace, "ENCODER\0EVT_UP\0EVT_DOWN\0ELSE\0IDLE\0UP\0DOWN" );
return *this;
}