Skip to content

Commit 9634f34

Browse files
authored
Merge pull request #123 from reaper7/master
new input device PCF8574KeyIn.h
2 parents 0380f94 + 685728d commit 9634f34

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

src/menuIO/PCF8574KeyIn.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* -*- C++ -*- */
2+
3+
/**************
4+
todo: write description
5+
***/
6+
7+
#ifndef __PCF8574KeyIn_h__
8+
#define __PCF8574KeyIn_h__
9+
10+
#include "../menu.h"
11+
#include <Wire.h>
12+
13+
namespace Menu {
14+
15+
#include "keyMapDef.h"
16+
17+
#ifndef BOUNCE_TICK
18+
#define BOUNCE_TICK 30
19+
#endif
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, int _dev=0x20, int _sda=SDA, int _scl=SCL> //default pcf8574 address=0x20
28+
class PCF8574KeyIn:public menuIn {
29+
public:
30+
keyMap* keys;
31+
int lastkey;
32+
TwoWire iicWire;
33+
unsigned long pressMills=0;
34+
PCF8574KeyIn<N, _dev, _sda, _scl>(keyMap k[]):keys(k),lastkey(-1) {}
35+
void begin() {
36+
iicWire.begin(_sda, _scl);
37+
}
38+
int available(void) {
39+
int ch=peek();
40+
if (lastkey==-1) {
41+
lastkey=ch;
42+
pressMills=millis();
43+
}
44+
else if (ch == -1 && millis()-pressMills < BOUNCE_TICK)
45+
{
46+
lastkey = -1; //released = it's bounce. reset lastkey
47+
return 0;
48+
}
49+
else if (ch != -1 && millis()-pressMills > BOUNCE_TICK) return 1;
50+
else if (ESCAPE_TIME&&millis()-pressMills>ESCAPE_TIME) return 1;
51+
52+
if (ch==lastkey) return 0;
53+
54+
return 1;
55+
}
56+
int peek(void) {
57+
//Serial<<"peek"<<endl;
58+
59+
iicWire.requestFrom(_dev, 1);
60+
uint8_t val = iicWire.read();
61+
62+
for(int n=0;n<N;n++) {
63+
int8_t pin=keys[n].pin;
64+
if (((val)&1<<pin)==0) {
65+
//Serial<<"key "<<pin<<" pressed"<<endl;
66+
return keys[n].code;
67+
}
68+
}
69+
return -1;
70+
}
71+
int read() {
72+
//Serial<<"read"<<endl;
73+
int ch=peek();
74+
if (ch==lastkey) return -1;
75+
int tmp=lastkey;
76+
bool longPress=ESCAPE_TIME&&millis()-pressMills>ESCAPE_TIME;
77+
//Serial<<"read lastkey="<<lastkey<<" ch="<<ch<<endl;
78+
//Serial<<"down time:"<<millis()-pressMills<<endl;
79+
pressMills=millis();
80+
lastkey=ch;
81+
return longPress?options->getCmdChar(escCmd):tmp;//long press will result in escape
82+
}
83+
void flush() {}
84+
size_t write(uint8_t v) {return 0;}
85+
};
86+
87+
}//namespace Menu
88+
89+
#endif

0 commit comments

Comments
 (0)