Skip to content

Commit cfeac50

Browse files
committed
rename to allow aternative map that was impossible to make both compatible because we are using brace-encloser initialize lists and just replacing the map would break compatibility...
#218
1 parent 1bba52f commit cfeac50

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ArduinoMenu library
2-
version=4.15.8
2+
version=4.15.9
33
author=Rui Azevedo, [email protected]
44
maintainer=neu-rah, [email protected]
55
sentence=Generic menu/interactivity system

src/menuIO/altKeyIn.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

src/menuIO/altKeyMapDef.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* -*- C++ -*- */
2+
3+
#ifndef RSITE_ARDUINO_MENU_KEYMAP
4+
#define RSITE_ARDUINO_MENU_KEYMAP
5+
6+
struct keyMap {
7+
int8_t pin;
8+
int8_t code;
9+
uint8_t mode;
10+
};
11+
12+
#endif

src/menuIO/keyIn.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ ex: -A0 means: pin A0 normally high, low when button pushed (reverse logic)
3333
keyIn<N>(keyMap k[]):keys(k),lastkey(-1) {}
3434
void begin() {
3535
for(int n=0;n<N;n++)
36-
pinMode(keys[n].pin,keys[n].mode);
37-
36+
if (keys[n].pin<0) pinMode(-keys[n].pin,INPUT_PULLUP);
37+
else pinMode(keys[n].pin,INPUT);
3838
}
3939
int available(void) {
4040
//MENU_DEBUG_OUT<<"available"<<endl;
@@ -56,8 +56,7 @@ ex: -A0 means: pin A0 normally high, low when button pushed (reverse logic)
5656
//MENU_DEBUG_OUT<<"peek"<<endl;
5757
for(int n=0;n<N;n++) {
5858
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;
59+
if (digitalRead(pin<0?-pin:pin)!=(pin<0) ) return keys[n].code;
6160
}
6261
return -1;
6362
}

src/menuIO/keyMapDef.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
struct keyMap {
77
int8_t pin;
88
int8_t code;
9-
uint8_t mode;
109
};
1110

1211
#endif

0 commit comments

Comments
 (0)