Skip to content

Commit 47eb076

Browse files
authored
Merge pull request #2 from lovyan03/unstable
stable
2 parents 4e8d647 + 83ddb26 commit 47eb076

File tree

9 files changed

+166
-17
lines changed

9 files changed

+166
-17
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ OnScreenKeyboard which can be operated with 3 button.
1212
M5Stack本体の3ボタンで操作できるオンスクリーンキーボード。
1313
簡単な文字入力にお使いいただけます。
1414

15-
Support FACES Keyboard and GameBoy unit.
15+
Support FACES Keyboard and GameBoy and Encoder unit.
1616
Support PLUS Encoder unit.
1717
Support JoyStick unit.
1818
Support CardKB unit.
@@ -56,6 +56,7 @@ M5OnScreenKeyboard m5osk;
5656
m5osk.useCardKB = true; // CARDKB unit support.
5757
m5osk.useJoyStick = true; // JoyStick unit support.
5858
m5osk.usePLUSEncoder = true; // PLUS Encoder unit support.
59+
m5osk.useFACESEncoder = true;// FACES Encoder unit support.
5960
// m5osk.swapBtnBC = true; // BtnB/BtnC KeyAssign swap.
6061
6162
m5osk.setup();

examples/nouseTextbox/nouseTextbox.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void setup() {
1515
m5osk.useCardKB = true; // CARDKB unit support.
1616
m5osk.useJoyStick = true; // JoyStick unit support.
1717
m5osk.usePLUSEncoder = true; // PLUS Encoder unit support.
18+
m5osk.useFACESEncoder = true;// FACES Encoder unit support.
1819

1920
}
2021
void loop() {

examples/simple/simple.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void setup() {
1313
m5osk.useCardKB = true; // CardKB unit support.
1414
m5osk.useJoyStick = true; // JoyStick unit support.
1515
m5osk.usePLUSEncoder = true; // PLUS Encoder unit support.
16+
m5osk.useFACESEncoder = true;// FACES Encoder unit support.
1617

1718
/*
1819
// style change example.

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"name": "M5Stack"
1616
},
17-
"version": "0.3.2",
17+
"version": "0.3.4",
1818
"framework": "arduino",
1919
"platforms": "espressif32",
2020
"build": {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=M5Stack_OnScreenKeyboard
2-
version=0.3.2
2+
version=0.3.4
33
author=lovyan03
44
maintainer=Lovyan <[email protected]>
55
sentence=OnScreenKeyboard for M5Stack

src/M5FACESEncoder.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <M5FACESEncoder.h>
2+
3+
M5FACESEncoder FACESEncoder;
4+
5+
bool M5FACESEncoder::update()
6+
{
7+
if (!Wire.requestFrom(_addr, 2)) return false;
8+
_time = millis();
9+
_oldUpDown = _upDown;
10+
_oldPress = _press;
11+
bool press = false;
12+
while (Wire.available()){
13+
_raw = Wire.read();
14+
_rawsum += _raw;
15+
press = (Wire.read() == 0);
16+
}
17+
_upDown = _rawsum;
18+
if (_upDown != 0) _rawsum = 0;
19+
20+
if (press != (0 != _oldPress)) _lastChange = _time;
21+
if (press) {
22+
if (!_oldPress) {
23+
_press = 1;
24+
} else
25+
if (1 == _oldPress && (_time - _lastChange >= msecHold)) {
26+
_press = 2;
27+
}
28+
} else {
29+
_press = 0;
30+
}
31+
32+
return true;
33+
}
34+
35+
void M5FACESEncoder::led(int led_index, int r, int g, int b)
36+
{
37+
Wire.beginTransmission(_addr);
38+
Wire.write(led_index);
39+
Wire.write(r);
40+
Wire.write(g);
41+
Wire.write(b);
42+
Wire.endTransmission();
43+
}

src/M5FACESEncoder.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef _M5FACESENCODER_H_
2+
#define _M5FACESENCODER_H_
3+
4+
#include <M5Stack.h>
5+
6+
class M5FACESEncoder
7+
{
8+
public:
9+
uint16_t msecHold = 300;
10+
11+
void setAddr(int8_t addr) { _addr = addr; }
12+
13+
bool update();
14+
15+
int8_t rawValue() const { return _raw; }
16+
bool wasUp() const { return _upDown > 0; }
17+
bool wasDown() const { return _upDown < 0; }
18+
19+
bool wasClicked() const { return _oldPress == 1 && _press == 0; }
20+
bool wasHold() const { return _oldPress == 1 && _press == 2; }
21+
bool isHolding() const { return _oldPress == 2 && _press == 2; }
22+
23+
bool isPressed() const { return _press; }
24+
bool isReleased() const { return !_press; }
25+
bool wasPressed() const { return !_oldPress && _press; }
26+
bool wasReleased() const { return _oldPress && !_press; }
27+
bool pressedFor(uint32_t ms) const { return (_press && _time - _lastChange >= ms); }
28+
bool releasedFor(uint32_t ms) const { return (!_press && _time - _lastChange >= ms); }
29+
30+
void led(int led_index, int r, int g, int b);
31+
32+
private:
33+
int8_t _ledpos = 0;
34+
int8_t _addr = 0x5E;
35+
int8_t _raw = 0;
36+
int8_t _rawsum = 0;
37+
int8_t _upDown = 0;
38+
int8_t _oldUpDown = 0;
39+
uint8_t _press = 0; // 0:release 1:click 2:holding
40+
uint8_t _oldPress = 0;
41+
uint32_t _time = 0;
42+
uint32_t _lastChange = 0;
43+
};
44+
45+
#endif
46+
47+
extern M5FACESEncoder FACESEncoder;
48+

src/M5OnScreenKeyboard.cpp

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <M5OnScreenKeyboard.h>
22

33
#include <M5PLUSEncoder.h>
4+
#include <M5FACESEncoder.h>
45
#include <M5JoyStick.h>
56

67
bool M5OnScreenKeyboard::useTextbox = true;
@@ -9,6 +10,7 @@ bool M5OnScreenKeyboard::useFACES = false;
910
bool M5OnScreenKeyboard::useCardKB = false;
1011
bool M5OnScreenKeyboard::useJoyStick = false;
1112
bool M5OnScreenKeyboard::usePLUSEncoder = false;
13+
bool M5OnScreenKeyboard::useFACESEncoder = false;
1214
bool M5OnScreenKeyboard::swapBtnBC = false;
1315

1416
uint16_t M5OnScreenKeyboard::fontColor[2] = {0xFFFF, 0xFFFF};
@@ -128,8 +130,13 @@ void M5OnScreenKeyboard::clearString() {
128130
}
129131

130132
void M5OnScreenKeyboard::setup(const String& value) {
133+
#ifdef ARDUINO_ODROID_ESP32
134+
_btnHeight = 0;
135+
#else
136+
_btnHeight = M5ButtonDrawer::height;
131137
_btnDrawer.setText("","","");
132138
_btnDrawer.draw(true);
139+
#endif
133140
setString(value);
134141
_tbl = 0;
135142
_col = 0;
@@ -156,6 +163,27 @@ bool M5OnScreenKeyboard::loop() {
156163
bool canRepeat = _repeat == 0 || (_msec - _msecLast) >= (1 < _repeat ? msecRepeat : msecHold);
157164

158165
bool press = false;
166+
167+
#ifdef ARDUINO_ODROID_ESP32
168+
if (M5.BtnStart .wasPressed()) { return false; }
169+
if (M5.BtnSelect.wasPressed()) { press = true; switchTable(); }
170+
if (M5.BtnA .isPressed()) { press = true; if (canRepeat) { ++_repeat; pressKey(); } }
171+
if (M5.BtnB .isPressed()) { press = true; if (canRepeat) { ++_repeat; pressKey(BS); } }
172+
if (M5.JOY_X.isAxisPressed() != DPAD_V_NONE) {
173+
press = true;
174+
if (canRepeat) {
175+
++_repeat;
176+
_col += (M5.JOY_X.isAxisPressed() == DPAD_V_HALF) ? 1 : -1;
177+
}
178+
}
179+
if (M5.JOY_Y.isAxisPressed() != DPAD_V_NONE) {
180+
press = true;
181+
if (canRepeat) {
182+
++_repeat;
183+
_row += (M5.JOY_Y.isAxisPressed() == DPAD_V_HALF) ? 1 : -1;
184+
}
185+
}
186+
#else
159187
Button& btnB(swapBtnBC ? M5.BtnC : M5.BtnB);
160188
Button& btnC(swapBtnBC ? M5.BtnB : M5.BtnC);
161189

@@ -193,6 +221,7 @@ bool M5OnScreenKeyboard::loop() {
193221
switch (_state) {
194222
case LEFTRIGHT: if (++_repeat < COLUMNCOUNT) --_col; break;
195223
case UPDOWN: if (++_repeat < ROWCOUNT) --_row; break;
224+
default: break;
196225
}
197226
}
198227
}
@@ -222,6 +251,7 @@ bool M5OnScreenKeyboard::loop() {
222251
&& btnC.releasedFor(msecMorseInput)
223252
&& _morseInputBuf) { ++_repeat; inputMorse(); }
224253
break;
254+
default: break;
225255
}
226256
}
227257
if (useFACES && Wire.requestFrom(0x08, 1)) {
@@ -260,8 +290,8 @@ bool M5OnScreenKeyboard::loop() {
260290
if (usePLUSEncoder && PLUSEncoder.update()) {
261291
switch (_state) {
262292
case LEFTRIGHT: // left right moving
263-
if (PLUSEncoder.wasUp()) { --_col; }
264-
if (PLUSEncoder.wasDown()) { ++_col; }
293+
if (PLUSEncoder.wasUp()) { ++_col; }
294+
if (PLUSEncoder.wasDown()) { --_col; }
265295
if (PLUSEncoder.wasHold()) { switchTable(); break; }
266296
if (PLUSEncoder.wasClicked()) { _state = UPDOWN; }
267297
break;
@@ -271,6 +301,26 @@ bool M5OnScreenKeyboard::loop() {
271301
if (PLUSEncoder.wasHold()) { _state = LEFTRIGHT; }
272302
if (PLUSEncoder.wasClicked()) { ++_repeat; pressKey(); _state = LEFTRIGHT; }
273303
break;
304+
default: break;
305+
}
306+
}
307+
#endif
308+
#ifdef _M5FACESENCODER_H_
309+
if (useFACESEncoder && FACESEncoder.update()) {
310+
switch (_state) {
311+
case LEFTRIGHT: // left right moving
312+
if (FACESEncoder.wasUp()) { ++_col; }
313+
if (FACESEncoder.wasDown()) { --_col; }
314+
if (FACESEncoder.wasHold()) { switchTable(); break; }
315+
if (FACESEncoder.wasClicked()) { _state = UPDOWN; }
316+
break;
317+
case UPDOWN: // up down moving
318+
if (FACESEncoder.wasUp()) { --_row; }
319+
if (FACESEncoder.wasDown()) { ++_row; }
320+
if (FACESEncoder.wasHold()) { _state = LEFTRIGHT; }
321+
if (FACESEncoder.wasClicked()) { ++_repeat; pressKey(); _state = LEFTRIGHT; }
322+
break;
323+
default: break;
274324
}
275325
}
276326
#endif
@@ -289,6 +339,7 @@ bool M5OnScreenKeyboard::loop() {
289339
if (JoyStick.wasClicked()) { ++_repeat; pressKey(); }
290340
if (JoyStick.wasHold()) { switchTable(); }
291341
}
342+
#endif
292343
#endif
293344
if (oldCol != _col
294345
|| oldRow != _row
@@ -321,19 +372,20 @@ bool M5OnScreenKeyboard::loop() {
321372
, M5.Lcd.fontHeight(font)
322373
, (_msec / 150) % 2 ? textboxBackColor : textboxFontColor);
323374
}
375+
#ifndef ARDUINO_ODROID_ESP32
324376
updateButton();
325377
_btnDrawer.draw();
378+
#endif
326379
return true;
327380
}
328381
void M5OnScreenKeyboard::close() {
329382
int y = getY(-1);
330383
M5.Lcd.fillRect(0, y, M5.Lcd.width(), M5.Lcd.height() - y, 0);
331-
_state == APPEAR;
332384
clearString();
333385
}
334386

335387
int M5OnScreenKeyboard::getX(int col) const { return col * KEYWIDTH; }
336-
int M5OnScreenKeyboard::getY(int row) const { return M5.Lcd.height() - M5ButtonDrawer::height - (ROWCOUNT - row) * keyHeight; }
388+
int M5OnScreenKeyboard::getY(int row) const { return M5.Lcd.height() - _btnHeight - (ROWCOUNT - row) * keyHeight; }
337389

338390
void M5OnScreenKeyboard::updateButton() {
339391
if (M5.BtnA.isPressed() || _fn) {
@@ -348,11 +400,12 @@ void M5OnScreenKeyboard::updateButton() {
348400
case LEFTRIGHT: _btnDrawer.setText("Panel/Left" , swapBtnBC?"Right":"Row", swapBtnBC?"Row":"Right"); break;
349401
case UPDOWN: _btnDrawer.setText("Panel/Up" , swapBtnBC?"Down" :"Ok" , swapBtnBC?"Ok" :"Down" ); break;
350402
case MORSE: _btnDrawer.setText("Panel/Fn" , "." , "_" ); break;
403+
default: break;
351404
}
352405
}
353406
}
354407
void M5OnScreenKeyboard::switchTable() {
355-
_tbl = ++_tbl % (TABLECOUNT - (useOver0x80Chars?0:1));
408+
_tbl = (_tbl + 1) % (TABLECOUNT - (useOver0x80Chars ? 0 : 1));
356409
}
357410

358411
bool M5OnScreenKeyboard::inputKB(char key)
@@ -474,16 +527,16 @@ void M5OnScreenKeyboard::drawKeyTop(int c, int r, int x, int y, int kh)
474527
char* str = tbl;
475528
char code = _chartbl[_tbl][r][c];
476529
switch (code) {
477-
case '\t': str = "TAB"; break;
478-
case '\r': str = "CR"; break;
479-
case '\n': str = "LF"; break;
480-
case BS : str = "BS"; break;
481-
case DEL : str = "DEL"; break;
482-
case LEFT: str = "<<"; break;
483-
case RIGH: str = ">>"; break;
530+
case '\t': str = (char*)PROGMEM "TAB"; break;
531+
case '\r': str = (char*)PROGMEM "CR"; break;
532+
case '\n': str = (char*)PROGMEM "LF"; break;
533+
case BS : str = (char*)PROGMEM "BS"; break;
534+
case DEL : str = (char*)PROGMEM "DEL"; break;
535+
case LEFT: str = (char*)PROGMEM "<<"; break;
536+
case RIGH: str = (char*)PROGMEM ">>"; break;
484537
}
485538
uint16_t color = fontColor[_col == c && _row == r ? 1 : 0];
486-
int fy = min(y + (kh - fh + 1) / 2 + moffset, M5.Lcd.height() - M5ButtonDrawer::height - fh);
539+
int fy = min(y + (kh - fh + 1) / 2 + moffset, M5.Lcd.height() - _btnHeight - fh);
487540
M5.Lcd.setTextColor(color);
488541
M5.Lcd.drawCentreString(str, x + 16, fy, font);
489542
if (_state == MORSE) {
@@ -542,7 +595,7 @@ void M5OnScreenKeyboard::drawTextbox() {
542595

543596
void M5OnScreenKeyboard::drawKeyboard(int h) {
544597
if (h < 0) h = keyHeight * ROWCOUNT;
545-
int y = M5.Lcd.height() - M5ButtonDrawer::height - h;
598+
int y = M5.Lcd.height() - _btnHeight - h;
546599
for (int c = 0; c < COLUMNCOUNT; ++c) {
547600
int x = getX(c);
548601
drawColumn(c, x, y, h);

src/M5OnScreenKeyboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class M5OnScreenKeyboard
1313
static bool useCardKB;
1414
static bool useJoyStick;
1515
static bool usePLUSEncoder;
16+
static bool useFACESEncoder;
1617
static bool swapBtnBC;
1718

1819
static uint16_t fontColor[2];
@@ -65,6 +66,7 @@ class M5OnScreenKeyboard
6566
uint8_t _morseInputBuf;
6667
bool _flgFACESKB;
6768
M5ButtonDrawer _btnDrawer;
69+
uint8_t _btnHeight;
6870

6971
int getX(int col) const;
7072
int getY(int row) const;

0 commit comments

Comments
 (0)