Thu, 10 Dec 2015 15:58:01 +0100
Manual mode added.
435 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2015 | |
3 | * | |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the mbsePi-apps | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * mbsePi-apps is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with ThermFerm; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
23 | #include "brewco.h" | |
24 | #include "lcd-pcf8574.h" | |
25 | #include "slcd.h" | |
26 | #include "keyboard.h" | |
27 | ||
28 | ||
29 | ||
30 | /* | |
31 | * 10 Milliseconds counts for a key to be short or long pressed. | |
32 | */ | |
33 | #define PRESS_NORMAL 5 | |
34 | #define PRESS_LONG 200 | |
35 | ||
36 | /* | |
37 | * LCD timeout in seconds | |
38 | */ | |
39 | #define LCD_SLEEP 120 | |
40 | ||
41 | /* | |
42 | * Menu timeout in seconds | |
43 | */ | |
44 | #define MENU_TIMEOUT 60 | |
45 | ||
46 | ||
47 | ||
48 | extern int my_shutdown; | |
49 | extern int debug; | |
50 | extern int setupmenu; | |
51 | extern uint16_t keys; | |
52 | extern uint16_t leds; | |
53 | extern int slcdHandle; | |
54 | ||
55 | int Key_Enter = FALSE; | |
56 | int Key_Return = FALSE; | |
57 | int Key_Up = FALSE; | |
58 | int Key_Down = FALSE; | |
59 | ||
60 | int previous_key = KEY_NONE; | |
61 | int menutimer = 0; | |
62 | ||
63 | ||
64 | int keypressed(void); | |
65 | ||
66 | ||
441 | 67 | |
68 | /* | |
69 | * Wait for a key. Return the pressed key. | |
70 | */ | |
71 | int keywait(void) | |
72 | { | |
73 | int key; | |
74 | ||
75 | do { | |
76 | usleep(50000); | |
77 | slcdDummy(slcdHandle); | |
78 | key = keycheck(); | |
79 | if (my_shutdown) | |
80 | return KEY_NONE; | |
81 | } while (key == KEY_NONE); | |
82 | ||
83 | fprintf(stdout, "keywait %d\n", key); | |
84 | return key; | |
85 | } | |
86 | ||
87 | ||
88 | ||
435 | 89 | /* |
90 | * Check for a key. Return last pressed key or none. | |
91 | */ | |
92 | int keycheck(void) | |
93 | { | |
94 | int key, retkey = KEY_NONE; | |
95 | ||
96 | key = keypressed(); | |
97 | if ((key == KEY_NONE) && (previous_key != KEY_NONE)) { | |
98 | retkey = previous_key; | |
99 | } | |
100 | ||
101 | previous_key = key; | |
102 | return retkey; | |
103 | } | |
104 | ||
105 | ||
106 | ||
107 | int keypressed(void) | |
108 | { | |
109 | if (Key_Enter && Key_Up && Key_Down && Key_Return) | |
110 | return KEY_ALL; | |
451 | 111 | if (Key_Up && Key_Down) |
112 | return KEY_ESCAPE; | |
435 | 113 | if (Key_Up) |
114 | return KEY_UP; | |
115 | if (Key_Down) | |
116 | return KEY_DOWN; | |
117 | if (Key_Enter) | |
118 | return KEY_ENTER; | |
119 | if (Key_Return) | |
120 | return KEY_RETURN; | |
121 | return KEY_NONE; | |
122 | } | |
123 | ||
124 | ||
125 | #ifdef HAVE_WIRINGPI_H | |
441 | 126 | PI_THREAD (my_keyboard_loop) |
435 | 127 | #else |
441 | 128 | void *my_keyboard_loop(void *threadid) |
435 | 129 | #endif |
130 | { | |
131 | int Return = 0, Enter = 0, Up = 0, Down = 0, Backlight = LCD_SLEEP, AnyKey = FALSE; | |
132 | time_t Last = (time_t)0, Now; | |
133 | ||
134 | #ifdef HAVE_WIRINGPI_H | |
135 | pinMode(PANEL_RETURN, INPUT); | |
136 | pinMode(PANEL_ENTER, INPUT); | |
137 | pinMode(PANEL_UP, INPUT); | |
138 | pinMode(PANEL_DOWN, INPUT); | |
139 | #endif | |
140 | ||
441 | 141 | syslog(LOG_NOTICE, "Thread my_keyboard_loop started"); |
438
7d1ec160d751
Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
435
diff
changeset
|
142 | if (debug) |
441 | 143 | fprintf(stdout, "Thread my_keyboard_loop started\n"); |
435 | 144 | |
145 | /* | |
146 | * Loop forever until the external shutdown variable is set. | |
147 | */ | |
148 | for (;;) { | |
149 | if (my_shutdown) | |
150 | break; | |
151 | ||
152 | #ifdef HAVE_WIRINGPI_H | |
153 | if (digitalRead(PANEL_ENTER) && ((keys & 0x0001) == 0)) { | |
154 | #else | |
155 | if ((keys & 0x0001) == 0) { | |
156 | #endif | |
157 | Enter = 0; | |
158 | Key_Enter = FALSE; | |
159 | } else { | |
160 | Enter++; | |
161 | if (Enter > PRESS_NORMAL) | |
162 | Key_Enter = TRUE; | |
163 | } | |
164 | ||
165 | #ifdef HAVE_WIRINGPI_H | |
166 | if (digitalRead(PANEL_RETURN) && ((keys & 0x0002) == 0)) { | |
167 | #else | |
168 | if ((keys & 0x0002) == 0) { | |
169 | #endif | |
170 | Return = 0; | |
171 | Key_Return = FALSE; | |
172 | } else { | |
173 | Return++; | |
174 | if (Return > PRESS_NORMAL) | |
175 | Key_Return = TRUE; | |
176 | } | |
177 | ||
178 | #ifdef HAVE_WIRINGPI_H | |
179 | if (digitalRead(PANEL_UP) && ((keys & 0x0008) == 0)) { | |
180 | #else | |
181 | if ((keys & 0x0008) == 0) { | |
182 | #endif | |
183 | Up = 0; | |
184 | Key_Up = FALSE; | |
185 | } else { | |
186 | Up++; | |
187 | if (Up > PRESS_NORMAL) | |
188 | Key_Up = TRUE; | |
189 | } | |
190 | ||
191 | #ifdef HAVE_WIRINGPI_H | |
192 | if (digitalRead(PANEL_DOWN) && ((keys & 0x0004) == 0)) { | |
193 | #else | |
194 | if ((keys & 0x0004) == 0) { | |
195 | #endif | |
196 | Down = 0; | |
197 | Key_Down = FALSE; | |
198 | } else { | |
199 | Down++; | |
200 | if (Down > PRESS_NORMAL) | |
201 | Key_Down = TRUE; | |
202 | } | |
203 | ||
204 | if (Key_Enter || Key_Return || Key_Up || Key_Down) { | |
205 | AnyKey = TRUE; | |
206 | /* | |
207 | * Any key is pressed. | |
208 | */ | |
209 | #ifdef HAVE_WIRINGPI_H | |
210 | piLock(LOCK_LCD); | |
211 | #endif | |
212 | setBacklight(1); | |
213 | #ifdef HAVE_WIRINGPI_H | |
214 | piUnlock(LOCK_LCD); | |
215 | #endif | |
216 | Backlight = LCD_SLEEP; | |
217 | menutimer = 0; | |
218 | } else { | |
219 | /* | |
220 | * No key pressed. | |
221 | */ | |
222 | AnyKey = FALSE; | |
223 | } | |
224 | ||
225 | Now = time(NULL); | |
226 | if (Now != Last) { | |
227 | Last = Now; | |
228 | ||
229 | if (AnyKey == FALSE) { | |
230 | if (Backlight == 1) { | |
231 | #ifdef HAVE_WIRINGPI_H | |
232 | piLock(LOCK_LCD); | |
233 | #endif | |
234 | setBacklight(0); | |
235 | #ifdef HAVE_WIRINGPI_H | |
236 | piUnlock(LOCK_LCD); | |
237 | #endif | |
238 | } | |
239 | if (Backlight > 0) { | |
240 | Backlight--; | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | /* | |
246 | * Loop 10 milliseconds | |
247 | */ | |
248 | usleep(10000); | |
249 | } | |
250 | ||
441 | 251 | syslog(LOG_NOTICE, "Thread my_keyboard_loop stopped"); |
438
7d1ec160d751
Added simulator configuration.
Michiel Broek <mbroek@mbse.eu>
parents:
435
diff
changeset
|
252 | if (debug) |
441 | 253 | fprintf(stdout, "Thread my_keyboard_loop stopped\n"); |
435 | 254 | return 0; |
255 | } | |
256 | ||
257 |