forked from Saur0o0n/PIDKiln
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIDKiln_input.ino
201 lines (158 loc) · 6.52 KB
/
PIDKiln_input.ino
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
/*
** Pidkiln input (rotary encoder, buttons) subsystem
**
*/
// Other variables
//
// Vars for interrupt function to keep track on encoder
volatile int lastEncoded = 0;
volatile int encoderValue = 0;
volatile unsigned long encoderButton = 0;
// Global value of the encoder position
int lastencoderValueT = 0;
byte menu_pos=0,screen_pos=0;
// Tell compiler it's interrupt function - otherwise it won't work on ESP
void ICACHE_RAM_ATTR handleInterrupt ();
// What to do if button pressed in menu
//
void pressed_menu(){
switch(LCD_Menu){
case M_SCR_MAIN_VIEW: LCD_display_main_view(); break;
case M_LIST_PROGRAMS: LCD_display_programs(); break;
case M_QUICK_PROGRAM: LCD_Display_quick_program(0,0); break;
case M_INFORMATIONS: LCD_Display_info(); break;
case M_PREFERENCES: LCD_Display_prefs(); break;
case M_CONNECT_WIFI: LCD_Reconect_WiFi(); break;
case M_ABOUT: LCD_Display_about(); break;
case M_RESTART: Restart_ESP(); break;
default: break;
}
}
// Just redirect pressed button to separate functions
//
void button_Short_Press(){
DBG Serial.printf(" Short press. Current view %d\n",(int)LCD_State);
if(LCD_State==SCR_MENU) pressed_menu();
else if(LCD_State==SCR_MAIN_VIEW && LCD_Main==MAIN_VIEW3) LCD_display_mainv3(0,2);
else if(LCD_State==SCR_PROGRAM_LIST) LCD_Display_program_summary(0,0);
else if(LCD_State==SCR_PROGRAM_SHOW) LCD_Display_program_summary(0,2);
else if(LCD_State==SCR_PROGRAM_DELETE) LCD_Display_program_delete(0,1);
else if(LCD_State==SCR_PROGRAM_FULL) LCD_Display_program_summary(0,1);
else if(LCD_State==SCR_QUICK_PROGRAM) LCD_Display_quick_program(0,2);
else LCD_display_menu(); // if pressed something else - go back to menu
}
// Handle long press on encoder
//
void button_Long_Press(){
if(LCD_State==SCR_MENU){ // we are in menu - switch to main screen
LCD_State=SCR_MAIN_VIEW;
LCD_display_main_view();
return;
}else if(LCD_State==SCR_PROGRAM_SHOW){ // if we are showing program - go to program list
LCD_State=SCR_PROGRAM_LIST;
LCD_display_programs(); // LCD_Program is global
return;
}else{ // If we are in MAIN screen or Program list or in unknown area to to -> menu
LCD_State=SCR_MENU; // switching to menu
LCD_display_menu();
return;
}
}
// Handle or rotation encoder input
//
void Rotate(){
// If we are in MAIN screen view
if(LCD_State==SCR_MAIN_VIEW){
if(LCD_Main==MAIN_VIEW3 && Program_run_size){ // if we are on third screen and program is loaded - let the screen 3 know about turn
LCD_display_mainv3(encoderValue,1);
return;
}
if(encoderValue<0){ // other screens just go...
if(LCD_Main>MAIN_VIEW1) LCD_Main=(LCD_MAIN_View_enum)((int)LCD_Main-1);
LCD_display_main_view();
return;
}else{
if(LCD_Main<MAIN_end-1) LCD_Main=(LCD_MAIN_View_enum)((int)LCD_Main+1);
LCD_display_main_view();
return;
}
}else if(LCD_State==SCR_MENU){
DBG Serial.printf("[INPUT] Rotate, SCR_MENU: Encoder turn: %d, Sizeof menu %d, Menu nr %d, \n",encoderValue, M_END, LCD_Menu);
if(encoderValue<0){
if(LCD_Menu>M_SCR_MAIN_VIEW) LCD_Menu=(LCD_SCR_MENU_Item_enum)((int)LCD_Menu-1);
}else{
if(LCD_Menu<M_END-1) LCD_Menu=(LCD_SCR_MENU_Item_enum)((int)LCD_Menu+1);
}
LCD_display_menu();
return;
}else if(LCD_State==SCR_PROGRAM_LIST){
DBG Serial.printf("[INPUT] Rotate, PROGRAMS: Encoder turn: %d\n",encoderValue);
rotate_selected_program(encoderValue);
LCD_display_programs();
}else if(LCD_State==SCR_PROGRAM_SHOW) LCD_Display_program_summary(encoderValue,1);
else if(LCD_State==SCR_PROGRAM_DELETE) LCD_Display_program_delete(encoderValue,0);
else if(LCD_State==SCR_PROGRAM_FULL) LCD_Display_program_full(encoderValue);
else if(LCD_State==SCR_PREFERENCES) LCD_Display_prefs(encoderValue);
else if(LCD_State==SCR_QUICK_PROGRAM) LCD_Display_quick_program(encoderValue,1);
}
// Main input loop task function
//
void Input_Loop(void * parameter) {
for(;;){
if(encoderButton){
vTaskDelay( ENCODER_BUTTON_DELAY / portTICK_PERIOD_MS );
if(digitalRead(ENCODER0_BUTTON)!=LOW){ // Button is still pressed - skip, perhaps it's a long press
if(encoderButton+Long_Press>=millis()){ // quick press
DBG Serial.printf("[INPUT] Button pressed %f seconds\n",(float)(millis()-encoderButton)/1000);
button_Short_Press();
}else{ // long press
DBG Serial.printf("[INPUT] Button long pressed %f seconds\n",(float)(millis()-encoderButton)/1000);
button_Long_Press();
}
encoderButton=0;
}
}else if(encoderValue!=0){
vTaskDelay(ENCODER_ROTATE_DELAY / portTICK_PERIOD_MS);
Rotate(); // encoderValue is global..
DBG Serial.printf("[INPUT] Encoder rotated %d\n",encoderValue);
encoderValue=0;
}
yield();
}
}
// Interrupt parser for rotary encoder and it's button
//
void handleInterrupt() {
if(digitalRead(ENCODER0_BUTTON)==LOW){
encoderButton=millis();
}else{ // Those two events can be simultaneous - but this is also ok, usually user does not press and turn
int MSB = digitalRead(ENCODER0_PINA); //MSB = most significant bit
int LSB = digitalRead(ENCODER0_PINB); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue=1;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue=-1;
lastEncoded = encoded; //store this value for next time
}
}
// Setup all input pins and interrupts
//
void Setup_Input() {
pinMode(ENCODER0_PINA, INPUT);
pinMode(ENCODER0_PINB, INPUT);
pinMode(ENCODER0_BUTTON, INPUT);
digitalWrite(ENCODER0_PINA, HIGH); //turn pullup resistor on
digitalWrite(ENCODER0_PINB, HIGH); //turn pullup resistor on
digitalWrite(ENCODER0_BUTTON, HIGH); //turn pullup resistor on
attachInterrupt(ENCODER0_PINA, handleInterrupt, CHANGE);
attachInterrupt(ENCODER0_PINB, handleInterrupt, CHANGE);
attachInterrupt(ENCODER0_BUTTON, handleInterrupt, FALLING);
xTaskCreatePinnedToCore(
// xTaskCreate(
Input_Loop, /* Task function. */
"Input_loop", /* String with name of task. */
4096, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL,0); /* Task handle. */
}