|
| 1 | +/* -*- C++ -*- */ |
| 2 | +/************** |
| 3 | +Sept. 2014 Rui Azevedo - ruihfazevedo(@rrob@)gmail.com |
| 4 | +
|
| 5 | +quick and dirty keyboard driver |
| 6 | +metaprog keyboard driver where N is the number of keys |
| 7 | +all keys are expected to be a pin (buttons) |
| 8 | +we can have reverse logic (pull-ups) by entering negative pin numbers |
| 9 | +ex: -A0 means: pin A0 normally high, low when button pushed (reverse logic) |
| 10 | +
|
| 11 | +***/ |
| 12 | + |
| 13 | +#ifndef RSITE_KEYIN_DEF_H |
| 14 | + #define RSITE_KEYIN_DEF_H |
| 15 | + #include "../menuDefs.h" |
| 16 | + |
| 17 | + namespace Menu { |
| 18 | + |
| 19 | + #include "keyMapDef.h" |
| 20 | + |
| 21 | + //if you hold/repeat a key for this ammount of time we will consider it an escape |
| 22 | + #ifndef ESCAPE_TIME |
| 23 | + #define ESCAPE_TIME 1500 |
| 24 | + #endif |
| 25 | + //emulate a stream keyboard, this is not using interrupts as a good driver should do |
| 26 | + // AND is not using a buffer either! |
| 27 | + template <int N> |
| 28 | + class keyIn:public menuIn { |
| 29 | + public: |
| 30 | + keyMap* keys; |
| 31 | + int lastkey; |
| 32 | + unsigned long pressMills=0; |
| 33 | + keyIn<N>(keyMap k[]):keys(k),lastkey(-1) {} |
| 34 | + void begin() { |
| 35 | + for(int n=0;n<N;n++) |
| 36 | + pinMode(keys[n].pin,keys[n].mode); |
| 37 | + |
| 38 | + } |
| 39 | + int available(void) { |
| 40 | + //MENU_DEBUG_OUT<<"available"<<endl; |
| 41 | + int ch=peek(); |
| 42 | + if (lastkey==-1) { |
| 43 | + lastkey=ch; |
| 44 | + pressMills=millis(); |
| 45 | + } else if (ESCAPE_TIME&&millis()-pressMills>ESCAPE_TIME) return 1; |
| 46 | + if (ch==lastkey) return 0; |
| 47 | + return 1; |
| 48 | + /*int cnt=0; |
| 49 | + for(int n=0;n<N;n++) { |
| 50 | + int8_t pin=keys[n].pin; |
| 51 | + if (digitalRead(pin<0?-pin:pin)!=(pin<0) ) cnt++; |
| 52 | + } |
| 53 | + return cnt;*/ |
| 54 | + } |
| 55 | + int peek(void) { |
| 56 | + //MENU_DEBUG_OUT<<"peek"<<endl; |
| 57 | + for(int n=0;n<N;n++) { |
| 58 | + int8_t pin=keys[n].pin; |
| 59 | + uint8_t mode = keys[n].mode == INPUT_PULLUP ? LOW : HIGH; |
| 60 | + if (digitalRead(pin) == mode ) return keys[n].code; |
| 61 | + } |
| 62 | + return -1; |
| 63 | + } |
| 64 | + int read() { |
| 65 | + //MENU_DEBUG_OUT<<"read"<<endl; |
| 66 | + int ch=peek(); |
| 67 | + if (ch==lastkey) return -1; |
| 68 | + int tmp=lastkey; |
| 69 | + bool longPress=ESCAPE_TIME&&millis()-pressMills>ESCAPE_TIME; |
| 70 | + //MENU_DEBUG_OUT<<"read lastkey="<<lastkey<<" ch="<<ch<<endl; |
| 71 | + //MENU_DEBUG_OUT<<"down time:"<<millis()-pressMills<<endl; |
| 72 | + pressMills=millis(); |
| 73 | + lastkey=ch; |
| 74 | + return longPress?options->getCmdChar(escCmd):tmp;//long press will result in escape |
| 75 | + } |
| 76 | + void flush() {} |
| 77 | + size_t write(uint8_t v) {return 0;} |
| 78 | + }; |
| 79 | + |
| 80 | + }//namespace Menu |
| 81 | + |
| 82 | +#endif |
0 commit comments