-
Notifications
You must be signed in to change notification settings - Fork 1
/
lightbar.cpp
107 lines (88 loc) · 2.26 KB
/
lightbar.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
103
104
105
106
107
#include "lightbar.h"
Lightbar::Lightbar(Radio *radio, uint32_t serial, const char *name)
{
this->radio = radio;
this->serial = serial;
this->name = name;
this->serialString = "0x" + String(this->serial, HEX);
}
Lightbar::~Lightbar()
{
}
uint32_t Lightbar::getSerial()
{
return this->serial;
}
const String Lightbar::getSerialString()
{
return this->serialString;
}
const char *Lightbar::getName()
{
return this->name;
}
void Lightbar::sendRawCommand(Command command, byte options)
{
this->radio->sendCommand(serial, command, options);
}
void Lightbar::sendRawCommand(Command command)
{
this->radio->sendCommand(serial, command);
}
void Lightbar::onOff()
{
this->sendRawCommand(Lightbar::Command::ON_OFF);
onState = !onState;
}
void Lightbar::setOnOff(bool on)
{
if (onState != on)
this->onOff();
}
void Lightbar::brighter()
{
this->sendRawCommand(Lightbar::Command::BRIGHTER);
}
void Lightbar::dimmer()
{
this->sendRawCommand(Lightbar::Command::DIMMER);
}
void Lightbar::warmer()
{
this->sendRawCommand(Lightbar::Command::WARMER);
}
void Lightbar::cooler()
{
this->sendRawCommand(Lightbar::Command::COOLER);
}
void Lightbar::reset()
{
this->sendRawCommand(Lightbar::Command::RESET);
}
void Lightbar::pair()
{
this->sendRawCommand(Lightbar::Command::RESET);
}
void Lightbar::setTemperature(uint8_t value)
{
// Send max value first, then set to the desired value. See
// https://github.com/lamperez/xiaomi-lightbar-nrf24?tab=readme-ov-file#command-codes
// for details.
this->sendRawCommand(Lightbar::Command::COOLER, 0x0 - 16);
this->sendRawCommand(Lightbar::Command::WARMER, (byte)value);
}
void Lightbar::setMiredTemperature(uint mireds)
{
mireds = max(mireds, (uint)153);
mireds = min(mireds, (uint)370);
float amount = ((1 - ((mireds - 153) * 1.0 / (370 - 153) * 1.0)) * 15) + 0.5;
this->setTemperature((uint8_t)amount);
}
void Lightbar::setBrightness(uint8_t value)
{
// Send max value first, then set to the desired value. See
// https://github.com/lamperez/xiaomi-lightbar-nrf24?tab=readme-ov-file#command-codes
// for details.
this->sendRawCommand(Lightbar::Command::DIMMER, 0x0 - 16);
this->sendRawCommand(Lightbar::Command::BRIGHTER, (byte)value);
}