-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger.cpp
More file actions
46 lines (40 loc) · 1.21 KB
/
trigger.cpp
File metadata and controls
46 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2025 Yasushi
// Licensed under the MIT License. See LICENSE for details.
#include "trigger.h"
Trigger::Trigger(const TriggerConfig& cfg) : config_(cfg) {}
void Trigger::begin() {
if (config_.type == TriggerType::Gpio) {
pinMode(config_.pin, config_.pinMode);
prevLevel_ = (config_.activeLevel == HIGH) ? LOW : HIGH; // 非押下を初期値に
}
}
bool Trigger::pressed() {
bool pressed = false;
if (config_.type == TriggerType::Touch) {
switch (config_.touch) {
case TouchButtonId::A:
pressed = M5.BtnA.wasPressed();
break;
case TouchButtonId::B:
pressed = M5.BtnB.wasPressed();
break;
case TouchButtonId::C:
default:
pressed = M5.BtnC.wasPressed();
break;
}
} else {
int level = digitalRead(config_.pin);
pressed = (prevLevel_ != config_.activeLevel) && (level == config_.activeLevel);
prevLevel_ = level;
}
if (pressed) {
prevPressMillis_ = lastPressMillis_;
lastPressMillis_ = millis();
}
return pressed;
}
bool Trigger::isDoubleTap(uint32_t windowMs) const {
if (lastPressMillis_ == 0 || prevPressMillis_ == 0) return false;
return (lastPressMillis_ - prevPressMillis_) <= windowMs;
}