Skip to content

Commit

Permalink
Merge branch 'patch-1' and fix #101
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyu-Zhao committed Sep 19, 2022
1 parent 6fb8bfe commit 3243098
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "git",
"url": "https://github.com/m5stack/M5Core2.git"
},
"version": "0.1.4",
"version": "0.1.5",
"frameworks": "arduino",
"platforms": "espressif32",
"headers": "M5Core2.h"
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=M5Core2
version=0.1.4
version=0.1.5
author=M5Stack
maintainer=M5Stack
sentence=Library for M5Stack Core2 development kit
Expand Down
18 changes: 11 additions & 7 deletions src/utility/M5Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,12 @@ bool Button::releasedFor(uint32_t ms) {

uint32_t Button::lastChange() { return (_lastChange); }

void Button::addHandler(void (*fn)(Event&), uint16_t eventMask /* = E_ALL */) {
void Button::addHandler(EventHandlerCallback fn,
uint16_t eventMask /* = E_ALL */) {
BUTTONS->addHandler(fn, eventMask, this, nullptr);
}

void Button::delHandlers(void (*fn)(Event&) /* = nullptr */) {
void Button::delHandlers(EventHandlerCallback fn /* = nullptr */) {
BUTTONS->delHandlers(fn, this, nullptr);
}

Expand Down Expand Up @@ -511,7 +512,8 @@ void M5Buttons::fireEvent(uint8_t finger, uint16_t type, Point& from, Point& to,
}
}

void M5Buttons::addHandler(void (*fn)(Event&), uint16_t eventMask /* = E_ALL */,
void M5Buttons::addHandler(EventHandlerCallback fn,
uint16_t eventMask /* = E_ALL */,
Button* button /* = nullptr */,
Gesture* gesture /* = nullptr */
) {
Expand All @@ -523,12 +525,13 @@ void M5Buttons::addHandler(void (*fn)(Event&), uint16_t eventMask /* = E_ALL */,
_eventHandlers.push_back(handler);
}

void M5Buttons::delHandlers(void (*fn)(Event&) /* = nullptr */,
void M5Buttons::delHandlers(EventHandlerCallback fn /* = nullptr */,
Button* button /* = nullptr */,
Gesture* gesture /* = nullptr */
) {
for (int i = _eventHandlers.size() - 1; i >= 0; --i) {
if (fn && fn != _eventHandlers[i].fn) continue;
// this doesn't compile anymore
//if (fn && fn != _eventHandlers[i].fn) continue;
if (button && _eventHandlers[i].button != button) continue;
if (gesture && _eventHandlers[i].gesture != gesture) continue;
_eventHandlers.erase(_eventHandlers.begin() + i);
Expand Down Expand Up @@ -610,11 +613,12 @@ bool Gesture::test(Point& from, Point& to, uint16_t duration) {

bool Gesture::wasDetected() { return _detected; }

void Gesture::addHandler(void (*fn)(Event&), uint16_t eventMask /* = E_ALL */) {
void Gesture::addHandler(EventHandlerCallback fn,
uint16_t eventMask /* = E_ALL */) {
BUTTONS->addHandler(fn, eventMask, nullptr, this);
}

void Gesture::delHandlers(void (*fn)(Event&) /* = nullptr */) {
void Gesture::delHandlers(EventHandlerCallback fn /* = nullptr */) {
BUTTONS->delHandlers(fn, nullptr, this);
}

Expand Down
17 changes: 10 additions & 7 deletions src/utility/M5Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@
class Gesture;

#include <Arduino.h>
#include <functional>
#include <Free_Fonts.h>
#include <M5Display.h>

Expand Down Expand Up @@ -801,6 +802,8 @@ class Event {
Gesture* gesture;
};

typedef std::function<void(Event&)> EventHandlerCallback;

class Button : public Zone {
public:
static std::vector<Button*> instances;
Expand Down Expand Up @@ -836,8 +839,8 @@ class Button : public Zone {
bool pressedFor(uint32_t ms, uint32_t continuous_time);
bool releasedFor(uint32_t ms);
bool wasReleasefor(uint32_t ms);
void addHandler(void (*fn)(Event&), uint16_t eventMask = E_ALL);
void delHandlers(void (*fn)(Event&) = nullptr);
void addHandler(EventHandlerCallback fn, uint16_t eventMask = E_ALL);
void delHandlers(EventHandlerCallback fn = nullptr);
char* getName();
uint32_t lastChange();
Event event;
Expand Down Expand Up @@ -903,8 +906,8 @@ class Gesture {
int16_t instanceIndex();
bool test(Point& from, Point& to, uint16_t duration);
bool wasDetected();
void addHandler(void (*fn)(Event&), uint16_t eventMask = E_ALL);
void delHandlers(void (*fn)(Event&) = nullptr);
void addHandler(EventHandlerCallback fn, uint16_t eventMask = E_ALL);
void delHandlers(EventHandlerCallback fn = nullptr);
char* getName();
Zone fromZone;
Zone toZone;
Expand All @@ -924,7 +927,7 @@ struct EventHandler {
uint16_t eventMask;
Button* button;
Gesture* gesture;
void (*fn)(Event&);
EventHandlerCallback fn;
};

class M5Buttons {
Expand All @@ -941,9 +944,9 @@ class M5Buttons {
void (*drawFn)(Button& b, ButtonColors bc);
void fireEvent(uint8_t finger, uint16_t type, Point& from, Point& to,
uint16_t duration, Button* button, Gesture* gesture);
void addHandler(void (*fn)(Event&), uint16_t eventMask = E_ALL,
void addHandler(EventHandlerCallback fn, uint16_t eventMask = E_ALL,
Button* button = nullptr, Gesture* gesture = nullptr);
void delHandlers(void (*fn)(Event&), Button* button, Gesture* gesture);
void delHandlers(EventHandlerCallback fn, Button* button, Gesture* gesture);
Event event;

protected:
Expand Down

0 comments on commit 3243098

Please sign in to comment.