Skip to content

Commit

Permalink
Add: Support FACES Encoder unit.
Browse files Browse the repository at this point in the history
  • Loading branch information
lovyan03 committed Jul 27, 2019
1 parent 0fca7f5 commit 83ddb26
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 5 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.3",
"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.3
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;

25 changes: 23 additions & 2 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 @@ -288,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 @@ -303,6 +305,25 @@ bool M5OnScreenKeyboard::loop() {
}
}
#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
#ifdef _M5JOYSTICK_H_
if (useJoyStick && JoyStick.update()) {
if (!JoyStick.isNeutral()) {
Expand Down
1 change: 1 addition & 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

0 comments on commit 83ddb26

Please sign in to comment.