forked from 0xPIT/encoder
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClickEncoderTest.ino
More file actions
123 lines (105 loc) · 2.94 KB
/
ClickEncoderTest.ino
File metadata and controls
123 lines (105 loc) · 2.94 KB
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
#define WITH_LCD 1
#include <ClickEncoder.h>
#if defined (__STM32F1__)
// Build for STM32F103CB / STM32F103C8 ARM boards
#undef WITH_LCD
#define PIN_LCD PB1 // LCD is on PB1 on MapleMini
HardwareTimer timer(1);
#else
// Build for AVR boards
#define PIN_LCD PC13 // LCD usually on PC13 on AVR
#include <TimerOne.h>
#endif
#ifdef WITH_LCD
#include <LiquidCrystal.h>
#define LCD_RS 8
#define LCD_RW 9
#define LCD_EN 10
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
#define LCD_CHARS 20
#define LCD_LINES 4
LiquidCrystal lcd(LCD_RS, LCD_RW, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
#endif
ClickEncoder *encoder;
int16_t last, value;
void timerIsr() {
encoder->service();
}
#ifdef WITH_LCD
void displayAccelerationStatus() {
lcd.setCursor(0, 1);
lcd.print("Acceleration ");
lcd.print(encoder->getAccelerationEnabled() ? "on " : "off");
}
#endif
void setup() {
#if defined (__STM32F1__)
encoder = new ClickEncoder( PB12, PB13, PB14 );
pinMode( PIN_LCD, OUTPUT );
Serial.begin( 115200 );
// Wait for USB console to connect
while( 1 )
{ if( Serial.isConnected() ) break;
digitalWrite( PIN_LCD, 1 );
delay( 50 );
digitalWrite( PIN_LCD, 0 );
delay( 300 );
}
timer.pause();
timer.setPeriod( 1000 );
// Set up an interrupt on channel 1
timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer.setCompare( TIMER_CH1, 1 ); // Interrupt 1 count after each update
timer.attachCompare1Interrupt(timerIsr);
timer.refresh(); // Refresh the timer's count, prescale, and overflow
timer.resume(); // Start the timer counting
#else
Serial.begin(9600);
encoder = new ClickEncoder( A0, A1, A2 );
Timer1.initialize(1000);
Timer1.attachInterrupt(timerIsr);
#endif
#ifdef WITH_LCD
lcd.begin(LCD_CHARS, LCD_LINES);
lcd.clear();
displayAccelerationStatus();
#endif
last = -1;
}
void loop() {
value += encoder->getValue();
if (value != last) {
last = value;
Serial.print("Encoder Value: ");
Serial.println(value);
#ifdef WITH_LCD
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(value);
#endif
}
ClickEncoder::Button b = encoder->getButton();
if (b != ClickEncoder::Open) {
Serial.print("Button: ");
#define VERBOSECASE(label) case label: Serial.println(#label); break;
switch (b) {
VERBOSECASE(ClickEncoder::Pressed);
VERBOSECASE(ClickEncoder::Held)
VERBOSECASE(ClickEncoder::Released)
VERBOSECASE(ClickEncoder::Clicked)
case ClickEncoder::DoubleClicked:
Serial.println("ClickEncoder::DoubleClicked");
encoder->setAccelerationEnabled(!encoder->getAccelerationEnabled());
Serial.print(" Acceleration is ");
Serial.println((encoder->getAccelerationEnabled()) ? "enabled" : "disabled");
#ifdef WITH_LCD
displayAccelerationStatus();
#endif
break;
}
}
}