class BtnInput
{
public:
typedef void (*BtnPressHandler)(bool v);
BtnInput(int buttonPin_, int pushLimit_){
buttonPin = buttonPin_;
pushLimit = pushLimit_;
}
void setup(){
pinMode(buttonPin, INPUT_PULLUP);
}
void setListener(BtnPressHandler eventBtnPress_) {
eventBtnPress = eventBtnPress_;
}
void dispatchBtnEvent(bool press) {
if (eventBtnPress) {
eventBtnPress(press);
}
}
void loop(){
int buttonState = digitalRead(buttonPin);
if(buttonState == LOW){
gauge++;
}else{
gauge--;
}
gauge = constrain(gauge, 0, pushLimit);
if (gauge >= pushLimit)
{
dispatchBtnEvent(true);
}
if (gauge <= 0)
{
dispatchBtnEvent(false);
}
}
private:
int buttonPin;
int gauge;
int pushLimit;
BtnPressHandler eventBtnPress = nullptr;
};
#include <M5AtomS3.h>
#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;
#include "BtnInput.h"
#define PUSH_LIMIT 20
const int buttonPin0 = 1;
char key0 = 'b';
BtnInput btnInput0 = BtnInput(buttonPin0, PUSH_LIMIT);
const int buttonPin1 = 2;
char key1 = 'c';
BtnInput btnInput1 = BtnInput(buttonPin1, PUSH_LIMIT);
void setup() {
M5.begin(true, true, false, false);
M5.Lcd.setTextSize(2);
M5.Lcd.println("USBHIDKeyboard");
btnInput0.setup();
btnInput1.setup();
btnInput0.setListener(&OnPress0);
btnInput1.setListener(&OnPress1);
Keyboard.begin();
USB.begin();
}
void OnPress0(bool press){
if(press){
Keyboard.press(key0);
}else{
Keyboard.release(key0);
}
}
void OnPress1(bool press){
if(press){
Keyboard.press(key1);
}else{
Keyboard.release(key1);
}
}
int gauge = 0;
bool isAct = false;
void loop() {
M5.update();
if (M5.Btn.isPressed()){
Keyboard.press('a');
}else{
Keyboard.release('a');
}
btnInput0.loop();
btnInput1.loop();
}