1
2use enum_iterator::Sequence;
3
4type EadkKeyboardState = u64;
6
7#[allow(dead_code)]
13#[derive(Clone, Copy, PartialEq, Eq, Sequence, Debug)]
14#[repr(u8)]
15pub enum Key {
16 Left = 0,
17 Up = 1,
18 Down = 2,
19 Right = 3,
20 Ok = 4,
21 Back = 5,
22 Home = 6,
23 OnOff = 8,
24 Shift = 12,
25 Alpha = 13,
26 Xnt = 14,
27 Var = 15,
28 Toolbox = 16,
29 Backspace = 17,
30 Exp = 18,
31 Ln = 19,
32 Log = 20,
33 Imaginary = 21,
34 Comma = 22,
35 Power = 23,
36 Sine = 24,
37 Cosine = 25,
38 Tangent = 26,
39 Pi = 27,
40 Sqrt = 28,
41 Square = 29,
42 Seven = 30,
43 Eight = 31,
44 Nine = 32,
45 LeftParenthesis = 33,
46 RightParenthesis = 34,
47 Four = 36,
48 Five = 37,
49 Six = 38,
50 Multiplication = 39,
51 Division = 40,
52 One = 42,
53 Two = 43,
54 Three = 44,
55 Plus = 45,
56 Minus = 46,
57 Zero = 48,
58 Dot = 49,
59 Ee = 50,
60 Ans = 51,
61 Exe = 52,
62}
63
64
65#[derive(Clone, Copy)]
78pub struct KeyboardState(EadkKeyboardState);
79
80impl Default for KeyboardState {
81 fn default() -> Self {
83 Self::new()
84 }
85}
86
87impl KeyboardState {
88 pub fn scan() -> Self {
90 Self::from_raw(unsafe { eadk_keyboard_scan() })
91 }
92
93 pub fn new() -> Self {
95 KeyboardState(0)
96 }
97
98 pub fn from_raw(state: EadkKeyboardState) -> Self {
100 Self(state)
101 }
102
103 pub fn key_down(&self, key: Key) -> bool {
107 (self.0 >> (key as u8)) & 1 != 0
108 }
109
110 pub fn get_just_pressed(&self, old: KeyboardState) -> Self {
112 KeyboardState(self.0 & (!old.0))
113 }
114
115 pub fn get_just_realeased(&self, old: KeyboardState) -> Self {
117 KeyboardState((!self.0) & old.0)
118 }
119}
120
121#[allow(dead_code)]
123#[derive(Clone, Copy, PartialEq, Eq)]
124#[repr(u16)]
125pub enum Event {
126 Left = 0,
127 Up = 1,
128 Down = 2,
129 Right = 3,
130 Ok = 4,
131 Back = 5,
132 Shift = 12,
133 Alpha = 13,
134 Xnt = 14,
135 Var = 15,
136 Toolbox = 16,
137 Backspace = 17,
138 Exp = 18,
139 Ln = 19,
140 Log = 20,
141 Imaginary = 21,
142 Comma = 22,
143 Power = 23,
144 Sine = 24,
145 Cosine = 25,
146 Tangent = 26,
147 Pi = 27,
148 Sqrt = 28,
149 Square = 29,
150 Seven = 30,
151 Eight = 31,
152 Nine = 32,
153 LeftParenthesis = 33,
154 RightParenthesis = 34,
155 Four = 36,
156 Five = 37,
157 Six = 38,
158 Multiplication = 39,
159 Division = 40,
160 One = 42,
161 Two = 43,
162 Three = 44,
163 Plus = 45,
164 Minus = 46,
165 Zero = 48,
166 Dot = 49,
167 Ee = 50,
168 Ans = 51,
169 Exe = 52,
170 ShiftLeft = 54,
171 ShiftUp = 55,
172 ShiftDown = 56,
173 ShiftRight = 57,
174 AlphaLock = 67,
175 Cut = 68,
176 Copy = 69,
177 Paste = 70,
178 Clear = 71,
179 LeftBracket = 72,
180 RightBracket = 73,
181 LeftBrace = 74,
182 RightBrace = 75,
183 Underscore = 76,
184 Sto = 77,
185 Arcsine = 78,
186 Arccosine = 79,
187 Arctangent = 80,
188 Equal = 81,
189 Lower = 82,
190 Greater = 83,
191 Colon = 122,
192 Semicolon = 123,
193 DoubleQuotes = 124,
194 Percent = 125,
195 LowerA = 126,
196 LowerB = 127,
197 LowerC = 128,
198 LowerD = 129,
199 LowerE = 130,
200 LowerF = 131,
201 LowerG = 132,
202 LowerH = 133,
203 LowerI = 134,
204 LowerJ = 135,
205 LowerK = 136,
206 LowerL = 137,
207 LowerM = 138,
208 LowerN = 139,
209 LowerO = 140,
210 LowerP = 141,
211 LowerQ = 142,
212 LowerR = 144,
213 LowerS = 145,
214 LowerT = 146,
215 LowerU = 147,
216 LowerV = 148,
217 LowerW = 150,
218 LowerX = 151,
219 LowerY = 152,
220 LowerZ = 153,
221 Space = 154,
222 Question = 156,
223 Exclamation = 157,
224 UpperA = 180,
225 UpperB = 181,
226 UpperC = 182,
227 UpperD = 183,
228 UpperE = 184,
229 UpperF = 185,
230 UpperG = 186,
231 UpperH = 187,
232 UpperI = 188,
233 UpperJ = 189,
234 UpperK = 190,
235 UpperL = 191,
236 UpperM = 192,
237 UpperN = 193,
238 UpperO = 194,
239 UpperP = 195,
240 UpperQ = 196,
241 UpperR = 198,
242 UpperS = 199,
243 UpperT = 200,
244 UpperU = 201,
245 UpperV = 202,
246 UpperW = 204,
247 UpperX = 205,
248 UpperY = 206,
249 UpperZ = 207,
250}
251
252impl Event {
253 pub fn is_digit(&self) -> bool {
255 matches!(
256 self,
257 Event::Zero
258 | Event::One
259 | Event::Two
260 | Event::Three
261 | Event::Four
262 | Event::Five
263 | Event::Six
264 | Event::Seven
265 | Event::Eight
266 | Event::Nine
267 )
268 }
269
270 pub fn to_digit(&self) -> Option<u8> {
272 match self {
273 Event::Zero => Some(0),
274 Event::One => Some(1),
275 Event::Two => Some(2),
276 Event::Three => Some(3),
277 Event::Four => Some(4),
278 Event::Five => Some(5),
279 Event::Six => Some(6),
280 Event::Seven => Some(7),
281 Event::Eight => Some(8),
282 Event::Nine => Some(9),
283 _ => None,
284 }
285 }
286
287 pub fn is_alphanumeric(&self) -> bool {
289 !matches!(
290 self,
291 Event::Left
292 | Event::Up
293 | Event::Down
294 | Event::Right
295 | Event::Ok
296 | Event::Back
297 | Event::Shift
298 | Event::Alpha
299 | Event::Backspace
300 | Event::Toolbox
301 | Event::Exe
302 | Event::AlphaLock
303 | Event::Cut
304 | Event::Copy
305 | Event::Paste
306 | Event::Clear
307 | Event::ShiftLeft
308 | Event::ShiftUp
309 | Event::ShiftDown
310 | Event::ShiftRight
311 )
312 }
313
314 pub fn to_alphanumeric(&self) -> Option<&'static str> {
327 match self {
328 Event::Zero => Some("0"),
329 Event::One => Some("1"),
330 Event::Two => Some("2"),
331 Event::Three => Some("3"),
332 Event::Four => Some("4"),
333 Event::Five => Some("5"),
334 Event::Six => Some("6"),
335 Event::Seven => Some("7"),
336 Event::Eight => Some("8"),
337 Event::Nine => Some("9"),
338
339 Event::Space => Some(" "),
340 Event::Question => Some("?"),
341 Event::Exclamation => Some("!"),
342 Event::Comma => Some(","),
343 Event::Colon => Some(":"),
344 Event::Semicolon => Some(";"),
345 Event::DoubleQuotes => Some("\""),
346 Event::Percent => Some("%"),
347 Event::Plus => Some("+"),
348 Event::Minus => Some("-"),
349 Event::Multiplication => Some("*"),
350 Event::Division => Some("/"),
351 Event::Dot => Some("."),
352 Event::Equal => Some("="),
353 Event::Underscore => Some("_"),
354 Event::Greater => Some(">"),
355 Event::Lower => Some("<"),
356 Event::Sto => Some("→"),
357 Event::Pi => Some("π"),
358 Event::Imaginary => Some("i"),
359 Event::Square => Some("^2"),
360 Event::Power => Some("^"),
361 Event::LeftParenthesis => Some("("),
362 Event::RightParenthesis => Some(")"),
363 Event::LeftBrace => Some("{"),
364 Event::RightBrace => Some("}"),
365 Event::LeftBracket => Some("["),
366 Event::RightBracket => Some("]"),
367 Event::Ee => Some("*10^"),
368
369 Event::Log => Some("log("),
370 Event::Ln => Some("ln("),
371 Event::Sine => Some("sin("),
372 Event::Cosine => Some("cos("),
373 Event::Tangent => Some("tan("),
374 Event::Sqrt => Some("sqrt("),
375Event::Xnt => Some("xnt("),
377 Event::Var => Some("var("),
378 Event::Exp => Some("exp("),
379 Event::Arcsine => Some("arsin("),
380 Event::Arccosine => Some("arcos("),
381 Event::Arctangent => Some("artan("),
382
383 Event::UpperA => Some("A"),
384 Event::UpperB => Some("B"),
385 Event::UpperC => Some("C"),
386 Event::UpperD => Some("D"),
387 Event::UpperE => Some("E"),
388 Event::UpperF => Some("F"),
389 Event::UpperG => Some("G"),
390 Event::UpperH => Some("H"),
391 Event::UpperI => Some("I"),
392 Event::UpperJ => Some("J"),
393 Event::UpperK => Some("K"),
394 Event::UpperL => Some("L"),
395 Event::UpperM => Some("M"),
396 Event::UpperN => Some("N"),
397 Event::UpperO => Some("O"),
398 Event::UpperP => Some("P"),
399 Event::UpperQ => Some("Q"),
400 Event::UpperR => Some("R"),
401 Event::UpperS => Some("S"),
402 Event::UpperT => Some("T"),
403 Event::UpperU => Some("U"),
404 Event::UpperV => Some("V"),
405 Event::UpperW => Some("W"),
406 Event::UpperX => Some("X"),
407 Event::UpperY => Some("Y"),
408 Event::UpperZ => Some("Z"),
409
410 Event::LowerA => Some("a"),
411 Event::LowerB => Some("b"),
412 Event::LowerC => Some("c"),
413 Event::LowerD => Some("d"),
414 Event::LowerE => Some("e"),
415 Event::LowerF => Some("f"),
416 Event::LowerG => Some("g"),
417 Event::LowerH => Some("h"),
418 Event::LowerI => Some("i"),
419 Event::LowerJ => Some("j"),
420 Event::LowerK => Some("k"),
421 Event::LowerL => Some("l"),
422 Event::LowerM => Some("m"),
423 Event::LowerN => Some("n"),
424 Event::LowerO => Some("o"),
425 Event::LowerP => Some("p"),
426 Event::LowerQ => Some("q"),
427 Event::LowerR => Some("r"),
428 Event::LowerS => Some("s"),
429 Event::LowerT => Some("t"),
430 Event::LowerU => Some("u"),
431 Event::LowerV => Some("v"),
432 Event::LowerW => Some("w"),
433 Event::LowerX => Some("x"),
434 Event::LowerY => Some("y"),
435 Event::LowerZ => Some("z"),
436 _=> None,
437 }
438 }
439}
440
441unsafe extern "C" {
442 fn eadk_event_get(timeout: &i32) -> Event;
443}
444
445unsafe extern "C" {
446 fn eadk_keyboard_scan() -> EadkKeyboardState;
447}
448
449pub fn event_get(timeout: i32) -> Event {
450 unsafe { eadk_event_get(&timeout) }
451}