-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotenc.h
78 lines (63 loc) · 2.11 KB
/
rotenc.h
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
/*
* rotenc.h
* This file is part of RotaryEncoder.
*
* RotaryEncoder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* your option) any later version.
*
* RotaryEncoder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RotaryEncoder. If not, see <http://www.gnu.org/licenses/>.
*
*
* Created: Oct 10, 2015
* Author: Thomas Heßling <[email protected]>
*
*/
/*
* This code is just a simple demonstration of how you can use a rotary encoder
* with software debounce. Note, that the debounce is only used when waiting for
* the first event (pin change), not the second. This implementation is,
* however, straight forward and left as an exercise for the reader/user. ;-)
*
*/
#ifndef ROTENC_H_
#define ROTENC_H_
#include <stdbool.h>
// port/pin configurations. Change them to match you circuit.
// ROTENC_DEBOUNCE sets the number of debounce waiting cycles.
#define ROTENC_DDR DDRC
#define ROTENC_PORT PORTC
#define ROTENC_PIN PINC
#define ROTENC_PIN_A PC0
#define ROTENC_PIN_B PC1
#define ROTENC_DEBOUNCE 1
/* Nothing to change beyond this line. */
// the different possible states
#define ROTENC_STATE_WAIT 0
#define ROTENC_STATE_WAIT_ASTAB 1
#define ROTENC_STATE_WAIT_BSTAB 2
#define ROTENC_STATE_WAIT_A 4
#define ROTENC_STATE_WAIT_B 8
#define ROTENC_STATE_ROT_CW 16
#define ROTENC_STATE_ROT_CCW 32
// helper struct to store position, state etc.
typedef struct {
uint8_t state;
uint8_t debounce_count;
uint16_t position;
} rotenc_state;
void rotenc_init();
void rotenc_update();
void rotenc_inc_position();
void rotenc_dec_position();
uint8_t rotenc_get_state();
uint16_t rotenc_get_position();
void rotenc_set_position(uint16_t pos);
#endif /* ROTENC_H_ */