-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlinkLED.hpp
70 lines (59 loc) · 1.92 KB
/
BlinkLED.hpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once
// clang-format off
/* === MODULE MANIFEST ===
module_name: BlinkLED
module_description: 控制 LED 闪烁的简单模块 / A simple module to control LED blinking
constructor_args:
- blink_cycle: 250
required_hardware: led/LED/led1/LED1
repository: https://github.com/xrobot-org/BlinkLED
=== END MANIFEST === */
// clang-format on
#include "app_framework.hpp"
#include "gpio.hpp"
#include "libxr_def.hpp"
#include "timer.hpp"
template <typename HardwareContainer>
class BlinkLED : public LibXR::Application {
public:
BlinkLED(HardwareContainer &hw, LibXR::ApplicationManager &app,
uint32_t blink_cycle)
: led_(hw.template FindOrExit<LibXR::GPIO>(
{"led", "LED", "led1", "LED1"})),
timer_handle_(
LibXR::Timer::CreateTask(BlinkTaskFun, this, blink_cycle)) {
UNUSED(app);
LibXR::Timer::Add(timer_handle_);
LibXR::Timer::Start(timer_handle_);
auto error_callback = LibXR::Callback<const char *, uint32_t>::Create(
[](bool in_isr, BlinkLED<HardwareContainer> *led, const char *file,
uint32_t line) {
UNUSED(file);
UNUSED(line);
LibXR::Timer::Stop(led->timer_handle_);
if (!in_isr) {
while (true) {
led->led_->Write(false);
LibXR::Thread::Sleep(125);
led->led_->Write(true);
LibXR::Thread::Sleep(125);
led->led_->Write(false);
LibXR::Thread::Sleep(500);
led->led_->Write(true);
LibXR::Thread::Sleep(500);
}
}
},
this);
LibXR::Assert::RegisterFatalErrorCB(error_callback);
}
static void BlinkTaskFun(BlinkLED<HardwareContainer> *blink) {
blink->flag_ = !blink->flag_;
blink->led_->Write(blink->flag_);
}
void OnMonitor() override {}
private:
bool flag_ = false;
LibXR::GPIO *led_;
LibXR::Timer::TimerHandle timer_handle_;
};