This commit is contained in:
Zhengyu Peng
2021-12-11 10:09:53 -05:00
parent ba57d5ba06
commit 89afd29632
54 changed files with 1 additions and 340 deletions

View File

@ -0,0 +1,83 @@
#include <Arduino.h>
#include "ui_controls.h"
namespace {
const static int kRepeatPeriod1 = 500;
const static int kRepeatPeriod1Count = 4;
const static int kRepeatPeriod2 = 100;
}
void ButtonGroup::addControl(Button &button) {
// prevent duplicated entry
for (const auto control : controls_) {
if (control == &button) {
return;
}
}
controls_.push_back(&button);
};
void ButtonGroup::addControl(Button *button) {
// prevent duplicated entry
for (const auto control : controls_) {
if (control == button) {
return;
}
}
controls_.push_back(button);
};
void ButtonGroup::addControls(std::vector<Button *> lists) {
for (auto button : lists) {
addControl(button);
}
}
void ButtonGroup::process() {
if (!callback_)
return;
if (pressed_) {
if (pressed_->isValueChanged() && pressed_->getValue() == 0) {
callback_(pressed_->_id, UP);
pressed_ = nullptr;
}
else if (millis() >= time_repeat_) {
callback_(pressed_->_id, REPEAT);
count_repeat_ += 1;
if (count_repeat_ < kRepeatPeriod1Count)
time_repeat_ = millis() + kRepeatPeriod1;
else
time_repeat_ = millis() + kRepeatPeriod2;
}
}
else {
for (auto control : controls_) {
if (control->isValueChanged() && control->getValue() == 1) {
// only 1 button can be pressed at the same time within the same group
pressed_ = control;
time_repeat_ = millis() + kRepeatPeriod1;
count_repeat_ = 0;
callback_(control->_id, DOWN);
return;
}
}
}
}
int ButtonGroup::getPressFlag() {
int flag = 0;
for (auto control : controls_) {
if (control->getValue() == 1) {
if (control->_id < 32)
flag |= (1<<control->_id);
}
}
return flag;
}
void LRemote_addControls(std::vector<LRemoteUIControl *> lists) {
for (auto control : lists) {
LRemote.addControl(*control);
}
}

View File

@ -0,0 +1,65 @@
#pragma once
#ifndef __LREMOTE_H__
#include <LRemote.h>
#define __LREMOTE_H__
#endif
class Label : public LRemoteLabel {
public:
Label(const String &text, uint8_t x, uint8_t y, uint8_t w, uint8_t h, RCColorType color = RC_GREY) : LRemoteLabel() {
setText(text);
setPos(x, y);
setSize(w, h);
setColor(color);
}
};
class Button : public LRemoteButton {
public:
Button(int id, const String &text, uint8_t x, uint8_t y, uint8_t w, uint8_t h, RCColorType color = RC_GREY) : LRemoteButton() {
setText(text);
setPos(x, y);
setSize(w, h);
setColor(color);
_id = id;
}
public:
int _id;
};
#include <vector>
#include <functional>
class ButtonGroup {
public:
enum mode {
NONE = 0,
DOWN,
UP,
REPEAT
};
void config(std::function<void(int, mode)> callback) {
callback_ = callback;
};
void addControl(Button &button);
void addControl(Button *button);
void addControls(std::vector<Button *> lists);
void process();
int getPressFlag();
public:
std::vector<Button *> controls_;
std::function<void(int, mode)> callback_ = nullptr;
Button *pressed_ = nullptr;
int time_repeat_ = 0;
int count_repeat_ = 0;
};
void LRemote_addControls(std::vector<LRemoteUIControl *> lists);