-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled.cpp
92 lines (82 loc) · 2.22 KB
/
led.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
#include "led.h"
LED LED::fromJson(JsonVariant& json_led)
{
JsonVariant json_color = json_led["color"];
const Color color = Color::fromJson(json_color);
const LEDPattern pattern = static_cast<LEDPattern>(json_led["pattern"].as<uint8_t>());
return LED(color, pattern);
}
void LED::begin(Adafruit_NeoPixel* const neopixel)
{
neopixel->begin();
}
void LED::update(Adafruit_NeoPixel* const neopixel)
{
if (!_is_enabled)
{
// 無効
_brightness_count = 0;
_brightness_reverse = false;
neopixel->setBrightness(0);
neopixel->show();
return;
}
// カウント
_brightness_count++;
// Patternによって変更
switch (_pattern)
{
case LEDPattern::Blink:
// 点灯/消灯
if (_brightness_reverse)
{
_brightness = brightness_min;
}
else
{
_brightness = brightness_max;
}
// カウント上限のとき反転
if (_brightness_count >= brightness_count_max)
{
_brightness_reverse = !_brightness_reverse;
}
break;
case LEDPattern::Fade:
// 明るさ変更
if (_brightness_reverse)
{
_brightness--;
}
else
{
_brightness++;
}
// 上限/下限で反転
if (_brightness <= brightness_min)
{
_brightness_reverse = false;
}
else if (_brightness >= brightness_max)
{
_brightness_reverse = true;
}
break;
default:
_brightness_reverse = false;
_brightness = brightness_max;
break;
}
// カウントリセット
if (_brightness_count >= brightness_count_max)
{
_brightness_count = 0;
}
const uint32_t neopixel_color = neopixel->Color(_color.getRed(), _color.getGreen(), _color.getBlue());
for (size_t i = 0; i < neopixel_num; i++)
{
neopixel->setPixelColor(i, neopixel_color);
}
neopixel->setBrightness(_brightness);
neopixel->show();
}