Skip to content

Commit

Permalink
Merge pull request #2 from lovyan03/unstable
Browse files Browse the repository at this point in the history
stable
  • Loading branch information
lovyan03 authored Jul 27, 2019
2 parents 4e8d647 + 83ddb26 commit 47eb076
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ OnScreenKeyboard which can be operated with 3 button.
M5Stack本体の3ボタンで操作できるオンスクリーンキーボード。
簡単な文字入力にお使いいただけます。

Support FACES Keyboard and GameBoy unit.
Support FACES Keyboard and GameBoy and Encoder unit.
Support PLUS Encoder unit.
Support JoyStick unit.
Support CardKB unit.
Expand Down Expand Up @@ -56,6 +56,7 @@ M5OnScreenKeyboard m5osk;
m5osk.useCardKB = true; // CARDKB unit support.
m5osk.useJoyStick = true; // JoyStick unit support.
m5osk.usePLUSEncoder = true; // PLUS Encoder unit support.
m5osk.useFACESEncoder = true;// FACES Encoder unit support.
// m5osk.swapBtnBC = true; // BtnB/BtnC KeyAssign swap.
m5osk.setup();
Expand Down
1 change: 1 addition & 0 deletions examples/nouseTextbox/nouseTextbox.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void setup() {
m5osk.useCardKB = true; // CARDKB unit support.
m5osk.useJoyStick = true; // JoyStick unit support.
m5osk.usePLUSEncoder = true; // PLUS Encoder unit support.
m5osk.useFACESEncoder = true;// FACES Encoder unit support.

}
void loop() {
Expand Down
1 change: 1 addition & 0 deletions examples/simple/simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void setup() {
m5osk.useCardKB = true; // CardKB unit support.
m5osk.useJoyStick = true; // JoyStick unit support.
m5osk.usePLUSEncoder = true; // PLUS Encoder unit support.
m5osk.useFACESEncoder = true;// FACES Encoder unit support.

/*
// style change example.
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {
"name": "M5Stack"
},
"version": "0.3.2",
"version": "0.3.4",
"framework": "arduino",
"platforms": "espressif32",
"build": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=M5Stack_OnScreenKeyboard
version=0.3.2
version=0.3.4
author=lovyan03
maintainer=Lovyan <[email protected]>
sentence=OnScreenKeyboard for M5Stack
Expand Down
43 changes: 43 additions & 0 deletions src/M5FACESEncoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <M5FACESEncoder.h>

M5FACESEncoder FACESEncoder;

bool M5FACESEncoder::update()
{
if (!Wire.requestFrom(_addr, 2)) return false;
_time = millis();
_oldUpDown = _upDown;
_oldPress = _press;
bool press = false;
while (Wire.available()){
_raw = Wire.read();
_rawsum += _raw;
press = (Wire.read() == 0);
}
_upDown = _rawsum;
if (_upDown != 0) _rawsum = 0;

if (press != (0 != _oldPress)) _lastChange = _time;
if (press) {
if (!_oldPress) {
_press = 1;
} else
if (1 == _oldPress && (_time - _lastChange >= msecHold)) {
_press = 2;
}
} else {
_press = 0;
}

return true;
}

void M5FACESEncoder::led(int led_index, int r, int g, int b)
{
Wire.beginTransmission(_addr);
Wire.write(led_index);
Wire.write(r);
Wire.write(g);
Wire.write(b);
Wire.endTransmission();
}
48 changes: 48 additions & 0 deletions src/M5FACESEncoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef _M5FACESENCODER_H_
#define _M5FACESENCODER_H_

#include <M5Stack.h>

class M5FACESEncoder
{
public:
uint16_t msecHold = 300;

void setAddr(int8_t addr) { _addr = addr; }

bool update();

int8_t rawValue() const { return _raw; }
bool wasUp() const { return _upDown > 0; }
bool wasDown() const { return _upDown < 0; }

bool wasClicked() const { return _oldPress == 1 && _press == 0; }
bool wasHold() const { return _oldPress == 1 && _press == 2; }
bool isHolding() const { return _oldPress == 2 && _press == 2; }

bool isPressed() const { return _press; }
bool isReleased() const { return !_press; }
bool wasPressed() const { return !_oldPress && _press; }
bool wasReleased() const { return _oldPress && !_press; }
bool pressedFor(uint32_t ms) const { return (_press && _time - _lastChange >= ms); }
bool releasedFor(uint32_t ms) const { return (!_press && _time - _lastChange >= ms); }

void led(int led_index, int r, int g, int b);

private:
int8_t _ledpos = 0;
int8_t _addr = 0x5E;
int8_t _raw = 0;
int8_t _rawsum = 0;
int8_t _upDown = 0;
int8_t _oldUpDown = 0;
uint8_t _press = 0; // 0:release 1:click 2:holding
uint8_t _oldPress = 0;
uint32_t _time = 0;
uint32_t _lastChange = 0;
};

#endif

extern M5FACESEncoder FACESEncoder;

81 changes: 67 additions & 14 deletions src/M5OnScreenKeyboard.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <M5OnScreenKeyboard.h>

#include <M5PLUSEncoder.h>
#include <M5FACESEncoder.h>
#include <M5JoyStick.h>

bool M5OnScreenKeyboard::useTextbox = true;
Expand All @@ -9,6 +10,7 @@ bool M5OnScreenKeyboard::useFACES = false;
bool M5OnScreenKeyboard::useCardKB = false;
bool M5OnScreenKeyboard::useJoyStick = false;
bool M5OnScreenKeyboard::usePLUSEncoder = false;
bool M5OnScreenKeyboard::useFACESEncoder = false;
bool M5OnScreenKeyboard::swapBtnBC = false;

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

void M5OnScreenKeyboard::setup(const String& value) {
#ifdef ARDUINO_ODROID_ESP32
_btnHeight = 0;
#else
_btnHeight = M5ButtonDrawer::height;
_btnDrawer.setText("","","");
_btnDrawer.draw(true);
#endif
setString(value);
_tbl = 0;
_col = 0;
Expand All @@ -156,6 +163,27 @@ bool M5OnScreenKeyboard::loop() {
bool canRepeat = _repeat == 0 || (_msec - _msecLast) >= (1 < _repeat ? msecRepeat : msecHold);

bool press = false;

#ifdef ARDUINO_ODROID_ESP32
if (M5.BtnStart .wasPressed()) { return false; }
if (M5.BtnSelect.wasPressed()) { press = true; switchTable(); }
if (M5.BtnA .isPressed()) { press = true; if (canRepeat) { ++_repeat; pressKey(); } }
if (M5.BtnB .isPressed()) { press = true; if (canRepeat) { ++_repeat; pressKey(BS); } }
if (M5.JOY_X.isAxisPressed() != DPAD_V_NONE) {
press = true;
if (canRepeat) {
++_repeat;
_col += (M5.JOY_X.isAxisPressed() == DPAD_V_HALF) ? 1 : -1;
}
}
if (M5.JOY_Y.isAxisPressed() != DPAD_V_NONE) {
press = true;
if (canRepeat) {
++_repeat;
_row += (M5.JOY_Y.isAxisPressed() == DPAD_V_HALF) ? 1 : -1;
}
}
#else
Button& btnB(swapBtnBC ? M5.BtnC : M5.BtnB);
Button& btnC(swapBtnBC ? M5.BtnB : M5.BtnC);

Expand Down Expand Up @@ -193,6 +221,7 @@ bool M5OnScreenKeyboard::loop() {
switch (_state) {
case LEFTRIGHT: if (++_repeat < COLUMNCOUNT) --_col; break;
case UPDOWN: if (++_repeat < ROWCOUNT) --_row; break;
default: break;
}
}
}
Expand Down Expand Up @@ -222,6 +251,7 @@ bool M5OnScreenKeyboard::loop() {
&& btnC.releasedFor(msecMorseInput)
&& _morseInputBuf) { ++_repeat; inputMorse(); }
break;
default: break;
}
}
if (useFACES && Wire.requestFrom(0x08, 1)) {
Expand Down Expand Up @@ -260,8 +290,8 @@ bool M5OnScreenKeyboard::loop() {
if (usePLUSEncoder && PLUSEncoder.update()) {
switch (_state) {
case LEFTRIGHT: // left right moving
if (PLUSEncoder.wasUp()) { --_col; }
if (PLUSEncoder.wasDown()) { ++_col; }
if (PLUSEncoder.wasUp()) { ++_col; }
if (PLUSEncoder.wasDown()) { --_col; }
if (PLUSEncoder.wasHold()) { switchTable(); break; }
if (PLUSEncoder.wasClicked()) { _state = UPDOWN; }
break;
Expand All @@ -271,6 +301,26 @@ bool M5OnScreenKeyboard::loop() {
if (PLUSEncoder.wasHold()) { _state = LEFTRIGHT; }
if (PLUSEncoder.wasClicked()) { ++_repeat; pressKey(); _state = LEFTRIGHT; }
break;
default: break;
}
}
#endif
#ifdef _M5FACESENCODER_H_
if (useFACESEncoder && FACESEncoder.update()) {
switch (_state) {
case LEFTRIGHT: // left right moving
if (FACESEncoder.wasUp()) { ++_col; }
if (FACESEncoder.wasDown()) { --_col; }
if (FACESEncoder.wasHold()) { switchTable(); break; }
if (FACESEncoder.wasClicked()) { _state = UPDOWN; }
break;
case UPDOWN: // up down moving
if (FACESEncoder.wasUp()) { --_row; }
if (FACESEncoder.wasDown()) { ++_row; }
if (FACESEncoder.wasHold()) { _state = LEFTRIGHT; }
if (FACESEncoder.wasClicked()) { ++_repeat; pressKey(); _state = LEFTRIGHT; }
break;
default: break;
}
}
#endif
Expand All @@ -289,6 +339,7 @@ bool M5OnScreenKeyboard::loop() {
if (JoyStick.wasClicked()) { ++_repeat; pressKey(); }
if (JoyStick.wasHold()) { switchTable(); }
}
#endif
#endif
if (oldCol != _col
|| oldRow != _row
Expand Down Expand Up @@ -321,19 +372,20 @@ bool M5OnScreenKeyboard::loop() {
, M5.Lcd.fontHeight(font)
, (_msec / 150) % 2 ? textboxBackColor : textboxFontColor);
}
#ifndef ARDUINO_ODROID_ESP32
updateButton();
_btnDrawer.draw();
#endif
return true;
}
void M5OnScreenKeyboard::close() {
int y = getY(-1);
M5.Lcd.fillRect(0, y, M5.Lcd.width(), M5.Lcd.height() - y, 0);
_state == APPEAR;
clearString();
}

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

void M5OnScreenKeyboard::updateButton() {
if (M5.BtnA.isPressed() || _fn) {
Expand All @@ -348,11 +400,12 @@ void M5OnScreenKeyboard::updateButton() {
case LEFTRIGHT: _btnDrawer.setText("Panel/Left" , swapBtnBC?"Right":"Row", swapBtnBC?"Row":"Right"); break;
case UPDOWN: _btnDrawer.setText("Panel/Up" , swapBtnBC?"Down" :"Ok" , swapBtnBC?"Ok" :"Down" ); break;
case MORSE: _btnDrawer.setText("Panel/Fn" , "." , "_" ); break;
default: break;
}
}
}
void M5OnScreenKeyboard::switchTable() {
_tbl = ++_tbl % (TABLECOUNT - (useOver0x80Chars?0:1));
_tbl = (_tbl + 1) % (TABLECOUNT - (useOver0x80Chars ? 0 : 1));
}

bool M5OnScreenKeyboard::inputKB(char key)
Expand Down Expand Up @@ -474,16 +527,16 @@ void M5OnScreenKeyboard::drawKeyTop(int c, int r, int x, int y, int kh)
char* str = tbl;
char code = _chartbl[_tbl][r][c];
switch (code) {
case '\t': str = "TAB"; break;
case '\r': str = "CR"; break;
case '\n': str = "LF"; break;
case BS : str = "BS"; break;
case DEL : str = "DEL"; break;
case LEFT: str = "<<"; break;
case RIGH: str = ">>"; break;
case '\t': str = (char*)PROGMEM "TAB"; break;
case '\r': str = (char*)PROGMEM "CR"; break;
case '\n': str = (char*)PROGMEM "LF"; break;
case BS : str = (char*)PROGMEM "BS"; break;
case DEL : str = (char*)PROGMEM "DEL"; break;
case LEFT: str = (char*)PROGMEM "<<"; break;
case RIGH: str = (char*)PROGMEM ">>"; break;
}
uint16_t color = fontColor[_col == c && _row == r ? 1 : 0];
int fy = min(y + (kh - fh + 1) / 2 + moffset, M5.Lcd.height() - M5ButtonDrawer::height - fh);
int fy = min(y + (kh - fh + 1) / 2 + moffset, M5.Lcd.height() - _btnHeight - fh);
M5.Lcd.setTextColor(color);
M5.Lcd.drawCentreString(str, x + 16, fy, font);
if (_state == MORSE) {
Expand Down Expand Up @@ -542,7 +595,7 @@ void M5OnScreenKeyboard::drawTextbox() {

void M5OnScreenKeyboard::drawKeyboard(int h) {
if (h < 0) h = keyHeight * ROWCOUNT;
int y = M5.Lcd.height() - M5ButtonDrawer::height - h;
int y = M5.Lcd.height() - _btnHeight - h;
for (int c = 0; c < COLUMNCOUNT; ++c) {
int x = getX(c);
drawColumn(c, x, y, h);
Expand Down
2 changes: 2 additions & 0 deletions src/M5OnScreenKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class M5OnScreenKeyboard
static bool useCardKB;
static bool useJoyStick;
static bool usePLUSEncoder;
static bool useFACESEncoder;
static bool swapBtnBC;

static uint16_t fontColor[2];
Expand Down Expand Up @@ -65,6 +66,7 @@ class M5OnScreenKeyboard
uint8_t _morseInputBuf;
bool _flgFACESKB;
M5ButtonDrawer _btnDrawer;
uint8_t _btnHeight;

int getX(int col) const;
int getY(int row) const;
Expand Down

0 comments on commit 47eb076

Please sign in to comment.