-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_arduino.cpp
102 lines (82 loc) · 2.65 KB
/
led_arduino.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <Arduino.h>
#include <cstdint>
#include "led.h"
#include "peripherals.h"
#include "utility.h"
namespace peripherals {
class LED_Arduino : LED {
public:
static LED& Instance() {
static LED_Arduino led_static;
if (!led_static.is_initialized_) {
pinMode(kLED_DEFAULT_GPIO, OUTPUT);
digitalWrite(kLED_DEFAULT_GPIO, LOW);
led_static.is_initialized_ = true;
}
return led_static;
}
void Show(const bool on) override {
if (on) {
led_state_ = HIGH;
} else {
led_state_ = LOW;
}
digitalWrite(kLED_DEFAULT_GPIO, led_state_);
led_time_ = 0;
}
void Blink() override {
if (led_time_ == 0) {
led_time_ = peripherals::MillisecondsCounter();
if (on_time_ > off_time_) {
led_state_ = HIGH;
} else {
led_state_ = LOW;
}
digitalWrite(kLED_DEFAULT_GPIO, led_state_);
return;
}
auto current_time = peripherals::MillisecondsCounter();
auto elapsed_time = current_time - led_time_;
if (led_state_ == HIGH && elapsed_time > on_time_) {
led_state_ = LOW;
digitalWrite(kLED_DEFAULT_GPIO, led_state_);
led_time_ = current_time;
} else if (led_state_ == LOW && elapsed_time > off_time_) {
led_state_ = HIGH;
digitalWrite(kLED_DEFAULT_GPIO, led_state_);
led_time_ = current_time;
}
}
void SetBlinkParams(const float duty_on,
const uint16_t cycle_time_ms) override {
LED::SetBlinkParams(duty_on, cycle_time_ms);
on_time_ = duty_on_ * cycle_time_ms_;
off_time_ = (1.0f - duty_on_) * cycle_time_ms_;
}
private:
int led_state_;
uint32_t led_time_;
uint16_t on_time_;
uint16_t off_time_;
bool is_initialized_;
LED_Arduino()
: LED(),
led_state_{LOW},
led_time_{0},
on_time_(duty_on_ * cycle_time_ms_),
off_time_((1.0f - duty_on_) * cycle_time_ms_),
is_initialized_{false} {}
};
LED& LED::Instance() { return LED_Arduino::Instance(); }
} // namespace peripherals