Skip to content

Commit 7276d25

Browse files
Code style cleanup of Nanoleaf code - comment style, controller pointer name
1 parent 79b4987 commit 7276d25

File tree

4 files changed

+208
-128
lines changed

4 files changed

+208
-128
lines changed

Controllers/NanoleafController/NanoleafController.cpp

Lines changed: 119 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,60 @@ long APIRequest(std::string method, std::string location, std::string URI, json*
2323

2424
CURL* curl = curl_easy_init();
2525

26-
// Set remote URL.
26+
/*-------------------------------------------------------------*\
27+
| Set remote URL. |
28+
\*-------------------------------------------------------------*/
2729
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
2830
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
2931

30-
// Don't bother trying IPv6, which would increase DNS resolution time.
32+
/*-------------------------------------------------------------*\
33+
| Don't bother trying IPv6, which would increase DNS resolution |
34+
| time. |
35+
\*-------------------------------------------------------------*/
3136
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
3237

33-
// Don't wait forever, time out after 10 seconds.
38+
/*-------------------------------------------------------------*\
39+
| Don't wait forever, time out after 10 seconds. |
40+
\*-------------------------------------------------------------*/
3441
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
3542

36-
// Follow HTTP redirects if necessary.
43+
/*-------------------------------------------------------------*\
44+
| Follow HTTP redirects if necessary. |
45+
\*-------------------------------------------------------------*/
3746
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
3847

3948
if(request_data)
4049
{
41-
// LOG_DEBUG("[Nanoleaf] Sending data: %s", request_data->dump().c_str());
4250
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, request_data->dump().c_str());
4351
}
4452

45-
// Response information.
46-
long httpCode(0);
47-
std::unique_ptr<std::string> httpData(new std::string());
53+
/*-------------------------------------------------------------*\
54+
| Response information. |
55+
\*-------------------------------------------------------------*/
56+
long httpCode(0);
57+
std::unique_ptr<std::string> httpData(new std::string());
4858

49-
// Hook up data handling function.
59+
/*-------------------------------------------------------------*\
60+
| Hook up data handling function. |
61+
\*-------------------------------------------------------------*/
5062
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
5163

52-
/*---------------------------------------------------------*\
53-
| Hook up data container (will be passed as the last |
54-
| parameter to the callback handling function). Can be any |
55-
| pointer type, since it will internally be passed as a |
56-
| void pointer. |
57-
\*---------------------------------------------------------*/
64+
/*-------------------------------------------------------------*\
65+
| Hook up data container (will be passed as the last parameter |
66+
| to the callback handling function). Can be any pointer type, |
67+
| since it will internally be passed as a void pointer. |
68+
\*-------------------------------------------------------------*/
5869
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
5970

60-
// Run our HTTP GET command, capture the HTTP response code, and clean up.
71+
/*-------------------------------------------------------------*\
72+
| Run our HTTP GET command, capture the HTTP response code, and |
73+
| clean up. |
74+
\*-------------------------------------------------------------*/
6175
curl_easy_perform(curl);
6276
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
6377
curl_easy_cleanup(curl);
6478

65-
if (httpCode/100 == 2)
79+
if((httpCode / 100) == 2)
6680
{
6781
if(response_data)
6882
{
@@ -79,22 +93,22 @@ long APIRequest(std::string method, std::string location, std::string URI, json*
7993

8094
NanoleafController::NanoleafController(std::string a_address, int a_port, std::string a_auth_token)
8195
{
82-
address = a_address;
83-
port = a_port;
84-
auth_token = a_auth_token;
85-
location = address+":"+std::to_string(port);
96+
address = a_address;
97+
port = a_port;
98+
auth_token = a_auth_token;
99+
location = address + ":" + std::to_string(port);
86100

87101
json data;
88102
if(APIRequest("GET", location, "/api/v1/"+auth_token, nullptr, &data) == 200)
89103
{
90-
name = data["name"];
91-
serial = data["serialNo"];
92-
manufacturer = data["manufacturer"];
93-
firmware_version = data["firmwareVersion"];
94-
model = data["model"];
104+
name = data["name"];
105+
serial = data["serialNo"];
106+
manufacturer = data["manufacturer"];
107+
firmware_version = data["firmwareVersion"];
108+
model = data["model"];
95109

96-
brightness = data["state"]["brightness"]["value"];
97-
selectedEffect = data["effects"]["select"];
110+
brightness = data["state"]["brightness"]["value"];
111+
selectedEffect = data["effects"]["select"];
98112

99113
for(json::const_iterator it = data["effects"]["effectsList"].begin(); it != data["effects"]["effectsList"].end(); ++it)
100114
{
@@ -131,46 +145,52 @@ void NanoleafController::Unpair(std::string address, int port, std::string auth_
131145
{
132146
const std::string location = address+":"+std::to_string(port);
133147

134-
// We really don't care if this fails.
148+
/*-------------------------------------------------------------*\
149+
| We really don't care if this fails. |
150+
\*-------------------------------------------------------------*/
135151
APIRequest("DELETE", location, "/api/v1/"+auth_token, nullptr, nullptr);
136152
}
137153

138154
void NanoleafController::UpdateLEDs(std::vector<RGBColor>& colors)
139155
{
140-
// Requires StartExternalControl() to have been called prior.
156+
/*-------------------------------------------------------------*\
157+
| Requires StartExternalControl() to have been called prior. |
158+
\*-------------------------------------------------------------*/
141159

142160
if(model == NANOLEAF_LIGHT_PANELS_MODEL)
143161
{
144-
uint8_t size = panel_ids.size();
162+
uint8_t size = panel_ids.size();
145163

146-
uint8_t* message = (uint8_t*)malloc(size*7+6+1);
164+
uint8_t* message = (uint8_t*)malloc(size*7+6+1);
147165

148-
message[0] = (uint8_t)size;
166+
message[0] = (uint8_t)size;
149167

150-
for (int i = 0; i < size; i++)
168+
for(int i = 0; i < size; i++)
151169
{
152-
message[7*i+0+1] = (uint8_t)panel_ids[i];
153-
message[7*i+1+1] = (uint8_t)1;
154-
message[7*i+2+1] = (uint8_t)RGBGetRValue(colors[i]);
155-
message[7*i+3+1] = (uint8_t)RGBGetGValue(colors[i]);
156-
message[7*i+4+1] = (uint8_t)RGBGetBValue(colors[i]);
157-
message[7*i+5+1] = (uint8_t)0;
158-
message[7*i+6+1] = (uint8_t)0;
170+
message[( 7 * i) + 0 + 1] = (uint8_t)panel_ids[i];
171+
message[( 7 * i) + 1 + 1] = (uint8_t)1;
172+
message[( 7 * i) + 2 + 1] = (uint8_t)RGBGetRValue(colors[i]);
173+
message[( 7 * i) + 3 + 1] = (uint8_t)RGBGetGValue(colors[i]);
174+
message[( 7 * i) + 4 + 1] = (uint8_t)RGBGetBValue(colors[i]);
175+
message[( 7 * i) + 5 + 1] = (uint8_t)0;
176+
message[( 7 * i) + 6 + 1] = (uint8_t)0;
159177
}
160178

161179
external_control_socket.udp_write(reinterpret_cast<char*>(message), size*7+6+1);
162180
}
163181
else if(model == NANOLEAF_CANVAS_MODEL)
164182
{
165-
// Insert V2 protocol implementation here.
183+
/*---------------------------------------------------------*\
184+
| Insert V2 protocol implementation here. |
185+
\*---------------------------------------------------------*/
166186
}
167187
}
168188

169189
void NanoleafController::StartExternalControl()
170190
{
171191
json request;
172-
request["write"]["command"] = "display";
173-
request["write"]["animType"] = "extControl";
192+
request["write"]["command"] = "display";
193+
request["write"]["animType"] = "extControl";
174194

175195
if(model == NANOLEAF_LIGHT_PANELS_MODEL)
176196
{
@@ -182,7 +202,7 @@ void NanoleafController::StartExternalControl()
182202
}
183203

184204
json response;
185-
if(APIRequest("PUT", location, "/api/v1/"+auth_token+"/effects", &request, &response)/100 == 2)
205+
if((APIRequest("PUT", location, "/api/v1/"+auth_token+"/effects", &request, &response) / 100) == 2)
186206
{
187207
external_control_socket.udp_client(response["streamControlIpAddr"].get<std::string>().c_str(), std::to_string(response["streamControlPort"].get<int>()).c_str());
188208

@@ -194,7 +214,8 @@ void NanoleafController::SelectEffect(std::string effect_name)
194214
{
195215
json request;
196216
request["select"] = effect_name;
197-
if(APIRequest("PUT", location, "/api/v1/"+auth_token+"/effects", &request)/100 == 2)
217+
218+
if((APIRequest("PUT", location, "/api/v1/"+auth_token+"/effects", &request) / 100) == 2)
198219
{
199220
selectedEffect = effect_name;
200221
}
@@ -204,8 +225,59 @@ void NanoleafController::SetBrightness(int a_brightness)
204225
{
205226
json request;
206227
request["brightness"]["value"] = a_brightness;
207-
if(APIRequest("PUT", location, "/api/v1/"+auth_token+"/state", &request)/100 == 2)
228+
229+
if((APIRequest("PUT", location, "/api/v1/"+auth_token+"/state", &request) / 100) == 2)
208230
{
209231
brightness = a_brightness;
210232
}
211233
}
234+
235+
std::string NanoleafController::GetAuthToken()
236+
{
237+
return auth_token;
238+
};
239+
240+
std::string NanoleafController::GetName()
241+
{
242+
return name;
243+
};
244+
245+
std::string NanoleafController::GetSerial()
246+
{
247+
return serial;
248+
};
249+
250+
std::string NanoleafController::GetManufacturer()
251+
{
252+
return manufacturer;
253+
};
254+
255+
std::string NanoleafController::GetFirmwareVersion()
256+
{
257+
return firmware_version;
258+
};
259+
260+
std::string NanoleafController::GetModel()
261+
{
262+
return model;
263+
};
264+
265+
std::vector<std::string>& NanoleafController::GetEffects()
266+
{
267+
return effects;
268+
};
269+
270+
std::vector<int>& NanoleafController::GetPanelIds()
271+
{
272+
return panel_ids;
273+
};
274+
275+
std::string NanoleafController::GetSelectedEffect()
276+
{
277+
return selectedEffect;
278+
};
279+
280+
int NanoleafController::GetBrightness()
281+
{
282+
return brightness;
283+
};

Controllers/NanoleafController/NanoleafController.h

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,55 @@
77
\*-----------------------------------------*/
88

99
#pragma once
10+
1011
#include "RGBController.h"
1112
#include "net_port.h"
1213

13-
#define NANOLEAF_DIRECT_MODE_EFFECT_NAME "*Dynamic*"
14-
15-
#define NANOLEAF_LIGHT_PANELS_MODEL "NL22"
16-
#define NANOLEAF_CANVAS_MODEL "NL29"
14+
#define NANOLEAF_DIRECT_MODE_EFFECT_NAME "*Dynamic*"
15+
#define NANOLEAF_LIGHT_PANELS_MODEL "NL22"
16+
#define NANOLEAF_CANVAS_MODEL "NL29"
1717

1818
class NanoleafController
1919
{
2020
public:
21-
static std::string Pair(std::string address, int port);
22-
static void Unpair(std::string address, int port, std::string auth_token);
23-
2421
NanoleafController(std::string a_address, int a_port, std::string a_auth_token);
2522

26-
void SelectEffect(std::string effect_name);
27-
void StartExternalControl();
28-
void SetBrightness(int a_brightness);
29-
// Requires External Control to have been started.
30-
void UpdateLEDs(std::vector<RGBColor>& colors);
31-
32-
std::string GetAuthToken() { return auth_token; };
33-
std::string GetName() { return name; };
34-
std::string GetSerial() { return serial; };
35-
std::string GetManufacturer() { return manufacturer; };
36-
std::string GetFirmwareVersion() { return firmware_version; };
37-
std::string GetModel() { return model; };
38-
std::vector<std::string>& GetEffects() { return effects; };
39-
std::vector<int>& GetPanelIds() { return panel_ids; };
40-
std::string GetSelectedEffect() { return selectedEffect; };
41-
int GetBrightness() { return brightness; };
23+
static std::string Pair(std::string address, int port);
24+
static void Unpair(std::string address, int port, std::string auth_token);
25+
26+
void SelectEffect(std::string effect_name);
27+
void StartExternalControl();
28+
void SetBrightness(int a_brightness);
29+
void UpdateLEDs(std::vector<RGBColor>& colors);
30+
31+
std::string GetAuthToken();
32+
std::string GetName();
33+
std::string GetSerial();
34+
std::string GetManufacturer();
35+
std::string GetFirmwareVersion();
36+
std::string GetModel();
37+
std::vector<std::string>& GetEffects();
38+
std::vector<int>& GetPanelIds();
39+
std::string GetSelectedEffect();
40+
int GetBrightness();
4241

4342
private:
44-
net_port external_control_socket;
43+
net_port external_control_socket;
4544

46-
std::string address;
47-
int port;
48-
std::string location;
49-
std::string auth_token;
45+
std::string address;
46+
int port;
47+
std::string location;
48+
std::string auth_token;
5049

51-
std::string name;
52-
std::string serial;
53-
std::string manufacturer;
54-
std::string firmware_version;
55-
std::string model;
50+
std::string name;
51+
std::string serial;
52+
std::string manufacturer;
53+
std::string firmware_version;
54+
std::string model;
5655

57-
std::vector<std::string> effects;
58-
std::vector<int> panel_ids;
56+
std::vector<std::string> effects;
57+
std::vector<int> panel_ids;
5958

60-
std::string selectedEffect;
61-
int brightness;
59+
std::string selectedEffect;
60+
int brightness;
6261
};

0 commit comments

Comments
 (0)