forked from prusa3d/MM-control-01
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Buttons.cpp
218 lines (189 loc) · 4.25 KB
/
Buttons.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! @file
#include "Buttons.h"
#include "shr16.h"
#include "tmc2130.h"
#include "mmctl.h"
#include "motion.h"
#include "permanent_storage.h"
#include "main.h"
const int ButtonPin = A2;
void settings_bowden_length();
//! @brief Select filament for bowden length calibration
//!
//! Filaments are selected by left and right buttons, calibration is activated by middle button.
//! Park position (one behind last filament) can be also selected.
//! Activating calibration in park position exits selector.
//!
void settings_select_filament()
{
while (1)
{
manual_extruder_selector();
if(Btn::middle == buttonClicked())
{
shr16_set_led(2 << 2 * (4 - active_extruder));
delay(500);
if (Btn::middle == buttonClicked())
{
if (active_extruder < 5) settings_bowden_length();
else
{
select_extruder(4);
select_extruder(0);
return;
}
}
}
}
}
//! @brief Show setup menu
//!
//! Items are selected by left and right buttons, activated by middle button.
//!
//! LED indication of states
//!
//! RG | RG | RG | RG | RG | meaning
//! -- | -- | -- | -- | -- | ------------------------
//! 11 | 00 | 00 | 00 | 01 | initial state, no action
//! 11 | 00 | 00 | 01 | 00 | setup bowden length
//! 11 | 00 | 01 | 00 | 00 | erase EEPROM if unlocked
//! 11 | 01 | 00 | 00 | 00 | unlock EEPROM erase
//! 11 | 00 | 00 | 00 | 00 | exit setup menu
//!
//! @n R - Red LED
//! @n G - Green LED
//! @n 1 - active
//! @n 0 - inactive
//!
void setupMenu()
{
shr16_set_led(0x000);
delay(200);
shr16_set_led(0x2aa);
delay(1200);
shr16_set_led(0x000);
delay(600);
int _menu = 0;
bool _exit = false;
bool eraseLocked = true;
do
{
shr16_set_led(1 << 2 * 4);
delay(1);
shr16_set_led(2 << 2 * 4);
delay(1);
shr16_set_led(2 << 2 * _menu);
delay(1);
switch (buttonClicked())
{
case Btn::right:
if (_menu > 0) { _menu--; delay(800); }
break;
case Btn::middle:
switch (_menu)
{
case 1:
settings_select_filament();
_exit = true;
break;
case 2:
if (!eraseLocked)
{
BowdenLength::eraseAll();
_exit = true;
}
break;
case 3: //unlock erase
eraseLocked = false;
break;
case 4: // exit menu
_exit = true;
break;
}
break;
case Btn::left:
if (_menu < 4) { _menu++; delay(800); }
break;
default:
break;
}
} while (!_exit);
shr16_set_led(0x000);
delay(400);
shr16_set_led(0x2aa);
delay(400);
shr16_set_led(0x000);
delay(400);
shr16_set_led(0x000);
shr16_set_led(1 << 2 * (4 - active_extruder));
}
//! @brief Set bowden length
//!
//! button | action
//! ------ | ------
//! left | increase bowden length / feed more filament
//! right | decrease bowden length / feed less filament
//! middle | store bowden length to EEPROM and exit
//!
//! This state is indicated by following LED pattern:
//!
//! RG | RG | RG | RG | RG
//! -- | -- | -- | -- | --
//! bb | 00 | 00 | 0b | 00
//!
//! @n R - Red LED
//! @n G - Green LED
//! @n 1 - active
//! @n 0 - inactive
//! @n b - blinking
//!
void settings_bowden_length()
{
// load filament above Bondtech gears to check correct length of bowden tube
if (!isFilamentLoaded)
{
BowdenLength bowdenLength;
load_filament_withSensor();
tmc2130_init_axis_current_normal(AX_PUL, 1, 30);
do
{
switch (buttonClicked())
{
case Btn::right:
if (bowdenLength.decrease())
{
move(0, 0, -bowdenLength.stepSize);
delay(400);
}
break;
case Btn::left:
if (bowdenLength.increase())
{
move(0, 0, bowdenLength.stepSize);
delay(400);
}
break;
default:
break;
}
shr16_set_led(1 << 2 * 4);
delay(10);
shr16_set_led(2 << 2 * 4);
delay(10);
shr16_set_led(2 << 2 * 1);
delay(50);
} while (buttonClicked() != Btn::middle);
unload_filament_withSensor();
}
}
//! @brief Is button pushed?
//!
//! @return button pushed
Btn buttonClicked()
{
int raw = analogRead(ButtonPin);
if (raw < 50) return Btn::right;
if (raw > 80 && raw < 100) return Btn::middle;
if (raw > 160 && raw < 180) return Btn::left;
return Btn::none;
}