Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
elk_decoder.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <array>
8#include <cstdint>
9
10namespace lampda {
11namespace common {
13namespace elk {
14
15enum class Type : uint8_t
16{
17 INVALID = 0,
18 ONOFF,
23 MIC_ONOFF,
24 MIC_MODE,
26 LED_ORDER,
27 SET_TIME,
28 TIMING,
29};
30
31struct Package
32{
33 Type type = Type::INVALID;
34
35 uint8_t dataSize = 0;
36 std::array<uint8_t, 5> data;
37};
38
42inline bool decode_ELK_message(const uint8_t* msg, uint16_t len, Package& package)
43{
44 package.type = Type::INVALID;
45 package.dataSize = 0;
46
47 // basic check: ELK messages are 9 char long
48 if (len != 9)
49 return false;
50 // Must start with 7E and end with EF
51 if (msg[0] != 0x7E || msg[8] != 0xEF)
52 return false;
53
54 switch (msg[2])
55 {
56 case 0x01: // BRIGHTNESS [0-100]
57 {
58 if (msg[3] > 100)
59 return false;
60
61 package.type = Type::BRIGHTNESS;
62 package.data[0] = msg[3]; // [0; 100]
63 package.dataSize = 1;
64 return true;
65 }
66 case 0x02: // PATTERN_SPEED [0-100]
67 {
68 if (msg[3] > 100)
69 return false;
70
71 package.type = Type::PATTERN_SPEED;
72 package.data[0] = msg[3]; // [0; 100]
73 package.dataSize = 1;
74 return true;
75 }
76 case 0x03:
77 {
78 // PATTERN_SELECT [0-28]
79 if (msg[4] == 0x03)
80 {
81 const uint8_t patternId = (msg[3] >= 128) ? (msg[3] - 128) : msg[3];
82 if (patternId > 28)
83 return false;
84
85 package.type = Type::PATTERN_SELECT;
86 package.data[0] = patternId; // [0; 28]
87 package.dataSize = 1;
88 return true;
89 }
90 // MIC_MODE: 0 = Classic, 1 = Soft, 2 = Dynamic, 3 = Disco.
91 else if (msg[4] == 0x04)
92 {
93 const uint8_t micMode = (msg[3] >= 128) ? (msg[3] - 128) : msg[3];
94 if (micMode > 3)
95 return false;
96
97 package.type = Type::MIC_MODE;
98 package.data[0] = micMode; // [0; 3]
99 package.dataSize = 1;
100 return true;
101 }
102 break;
103 }
104 case 0x04: // ONOFF
105 {
106 if (msg[3] > 0x01)
107 return false;
108
109 package.type = Type::ONOFF;
110 package.data[0] = msg[3];
111 package.dataSize = 1;
112 return true;
113 }
114 case 0x05: // COLOR_SELECT
115 {
116 if (msg[3] == 0x03)
117 {
118 package.type = Type::COLOR_SELECT;
119 package.data[0] = msg[4];
120 package.data[1] = msg[5];
121 package.data[2] = msg[6];
122 package.dataSize = 3;
123 return true;
124 }
125 break;
126 }
127 case 0x06: // MIC_SENSITIVITY [0-100]
128 {
129 if (msg[3] > 100)
130 return false;
131
132 package.type = Type::MIC_SENSITIVITY;
133 package.data[0] = msg[3]; // [0; 100]
134 package.dataSize = 1;
135 return true;
136 }
137 case 0x07: // MIC_ONOFF
138 {
139 if (msg[3] > 0x01)
140 return false;
141
142 package.type = Type::MIC_ONOFF;
143 package.data[0] = msg[3];
144 package.dataSize = 1;
145 return true;
146 }
147
148 case 0x81: // LED_ORDER
149 {
150 if (msg[3] <= 0 or msg[3] > 3)
151 return false;
152 if (msg[4] <= 0 or msg[4] > 3)
153 return false;
154 if (msg[5] <= 0 or msg[5] > 3)
155 return false;
156 // should contain only one 1, one 2 and one 3
157 if (msg[3] + msg[4] + msg[5] != 1 + 2 + 3)
158 return false;
160
161 package.type = Type::LED_ORDER;
162 package.data[0] = msg[3];
163 package.data[1] = msg[4];
164 package.data[2] = msg[5];
165 package.dataSize = 3;
166 return true;
167 }
168 case 0x82: // TIMING
169 {
170 package.type = Type::TIMING;
171 package.data[0] = msg[3];
172 package.data[1] = msg[4];
173 package.data[2] = msg[5];
174 package.data[3] = msg[6];
175 package.data[4] = msg[7];
176 package.dataSize = 5;
177 return true;
178 }
179 case 0x83: // SET_TIME
180 {
181 package.type = Type::SET_TIME;
182 package.data[0] = msg[3];
183 package.data[1] = msg[4];
184 package.data[2] = msg[5];
185 package.data[3] = msg[6];
186 package.dataSize = 4;
187 return true;
188 }
189 default:
190 break;
191 }
192 return false;
193}
194
195} // namespace elk
196} // namespace common
197} // namespace lampda
bool decode_ELK_message(const uint8_t *msg, uint16_t len, Package &package)
Decode a given message, assuming it it a ELK control message.
Definition: elk_decoder.h:42
Type
Definition: elk_decoder.h:16
@ PATTERN_SPEED
set the current pattern speed
@ PATTERN_SELECT
select a display pattern in a list
@ BRIGHTNESS
set a brightness
@ COLOR_SELECT
set output as a target color
@ ONOFF
turn output onand off
@ MIC_SENSITIVITY
set microphone sensitivity
@ LED_ORDER
set the new RGB led ordering
@ SET_TIME
set the system time to a set time
@ MIC_ONOFF
turn on/off the microphone
@ TIMING
set a desired on/off time
@ MIC_MODE
set the pattern changes type with the microphone
Program scope.
Definition: control_fixed_modes.hpp:12
Definition: elk_decoder.h:32