Skip to content

Commit b16ec6b

Browse files
committed
Add support for sections
1 parent f7b0877 commit b16ec6b

File tree

7 files changed

+271
-16
lines changed

7 files changed

+271
-16
lines changed

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ branches:
1919
only:
2020
- master
2121
- /^travis.*/
22-
- /^v.*/
2322

2423
stages:
2524
- name: test
@@ -31,9 +30,9 @@ jobs:
3130
- CC=gcc-6
3231
- CXX=g++-6
3332
script:
34-
- mkdir -p build/run
35-
- cd build/run
36-
- cmake -DCMAKE_BUILD_TYPE=Debug ../..
33+
- mkdir -p build
34+
- cd build
35+
- cmake -DCMAKE_BUILD_TYPE=Debug ..
3736
- make
3837
- make test_with_coverage
3938

src/AnimationSender.hpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "AnimationData.hpp"
4242
#include "AnimationInfo.hpp"
4343
#include "EndAnimation.hpp"
44+
#include "Section.hpp"
4445
#include "StripInfo.hpp"
4546

4647
using json = nlohmann::json;
@@ -66,7 +67,8 @@ class AnimationSender {
6667

6768
void (* newEndAnimationAction)(EndAnimation) = nullptr;
6869

69-
// void (*newSectionAction)(Section) = nullptr;
70+
void (*newSectionAction)(Section) = nullptr;
71+
7072
void (* newStripInfoAction)(StripInfo) = nullptr;
7173

7274
void (* connectAction)(std::string, int) = nullptr;
@@ -137,10 +139,12 @@ class AnimationSender {
137139
if (sender.newEndAnimationAction != nullptr) sender.newEndAnimationAction(e);
138140
sender.running_animations->erase(e.id);
139141
} else if (std::strcmp(type, "SECT") == 0) {
140-
// TODO
142+
Section sect = Section(json::parse(remainingData));
143+
if (sender.newSectionAction != nullptr) sender.newSectionAction(sect);
144+
sender.sections->insert(std::pair<std::string, Section>(sect.name, sect));
141145
} else if (std::strcmp(type, "SINF") == 0) {
142146
StripInfo i = StripInfo(json::parse(remainingData));
143-
sender.info = &i;
147+
sender.stripInfo = &i;
144148
if (sender.newStripInfoAction != nullptr) sender.newStripInfoAction(i);
145149
} else {
146150

@@ -162,12 +166,13 @@ class AnimationSender {
162166
}
163167

164168
public:
165-
StripInfo * info{};
169+
StripInfo * stripInfo{};
166170

167171
static const char * delimiter;
168172

169173
safe::map<std::string, AnimationData> * running_animations;
170174
safe::map<std::string, AnimationInfo> * supported_animations;
175+
safe::map<std::string, Section> * sections;
171176

172177
AnimationSender(const std::string & host, int port) {
173178
host_name = host;
@@ -186,11 +191,14 @@ class AnimationSender {
186191

187192
running_animations = new safe::map<std::string, AnimationData>;
188193
supported_animations = new safe::map<std::string, AnimationInfo>;
194+
sections = new safe::map<std::string, Section>;
189195
}
190196

191197
~AnimationSender() {
192198
delete running_animations;
193199
delete supported_animations;
200+
delete sections;
201+
delete stripInfo;
194202
}
195203

196204
int start() {
@@ -217,6 +225,9 @@ class AnimationSender {
217225
running = false;
218226
pthread_cancel(receiver_handle);
219227
if (disconnectAction != nullptr) disconnectAction(host_name, port_num);
228+
supported_animations->clear();
229+
running_animations->clear();
230+
sections->clear();
220231
return 0;
221232
}
222233

@@ -278,10 +289,10 @@ class AnimationSender {
278289
return *this;
279290
}
280291

281-
// AnimationSender & setOnNewSectionCallback(void (*action)(Section)) {
282-
// newSectionAction = action;
283-
// return *this;
284-
// }
292+
AnimationSender & setOnNewSectionCallback(void (*action)(Section)) {
293+
newSectionAction = action;
294+
return *this;
295+
}
285296

286297
AnimationSender & setOnNewStripInfoCallback(void (* action)(StripInfo)) {
287298
newStripInfoAction = action;

src/Section.hpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2019-2020 AnimatedLEDStrip
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
#ifndef ANIMATEDLEDSTRIP_SECTION_HPP
24+
#define ANIMATEDLEDSTRIP_SECTION_HPP
25+
26+
#include <iostream>
27+
#include <string>
28+
#include <nlohmann/json.hpp>
29+
30+
class Section {
31+
public:
32+
std::string name = "";
33+
int startPixel = -1;
34+
int endPixel = -1;
35+
int physicalStart = -1;
36+
int numLEDs = 0;
37+
38+
Section & setName(const std::string & n) {
39+
name.assign(n);
40+
return *this;
41+
}
42+
43+
Section & setName(const char * n) {
44+
name.assign(n);
45+
return *this;
46+
}
47+
48+
Section & setStartPixel(int s) {
49+
startPixel = s;
50+
return *this;
51+
}
52+
53+
Section & setEndPixel(int e) {
54+
endPixel = e;
55+
return *this;
56+
}
57+
58+
Section & setPhysicalStart(int p) {
59+
physicalStart = p;
60+
return *this;
61+
}
62+
63+
Section & setNumLEDs(int n) {
64+
numLEDs = n;
65+
return *this;
66+
}
67+
68+
Section() = default;
69+
70+
explicit Section(nlohmann::json data) {
71+
if (data["name"].is_string())
72+
setName(data["name"].get<std::string>());
73+
else if (!data["name"].is_null())
74+
std::cerr << "Bad type for name" << data["name"].type_name() << std::endl;
75+
76+
if (data["startPixel"].is_number_integer())
77+
setStartPixel(data["startPixel"].get<int>());
78+
else if (!data["startPixel"].is_null())
79+
std::cerr << "Bad type for startPixel" << data["startPixel"].type_name() << std::endl;
80+
81+
if (data["endPixel"].is_number_integer())
82+
setEndPixel(data["endPixel"].get<int>());
83+
else if (!data["endPixel"].is_null())
84+
std::cerr << "Bad type for endPixel" << data["endPixel"].type_name() << std::endl;
85+
86+
if (data["physicalStart"].is_number_integer())
87+
setPhysicalStart(data["physicalStart"].get<int>());
88+
else if (!data["physicalStart"].is_null())
89+
std::cerr << "Bad type for physicalStart" << data["physicalStart"].type_name() << std::endl;
90+
91+
if (data["numLEDs"].is_number_integer())
92+
setNumLEDs(data["numLEDs"].get<int>());
93+
else if (!data["numLEDs"].is_null())
94+
std::cerr << "Bad type for numLEDs" << data["numLEDs"].type_name() << std::endl;
95+
}
96+
97+
int json(char ** buff) const {
98+
std::string data = "SECT:{";
99+
100+
data.append(R"("name":")");
101+
data.append(name);
102+
103+
data.append(R"(","startPixel":)");
104+
data.append(std::to_string(startPixel));
105+
106+
data.append(R"(,"endPixel":)");
107+
data.append(std::to_string(endPixel));
108+
109+
data.append("}");
110+
111+
std::strcpy(*buff, data.c_str());
112+
return data.size();
113+
}
114+
};
115+
116+
#endif //ANIMATEDLEDSTRIP_SECTION_HPP

src/StripInfo.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
* THE SOFTWARE.
2121
*/
2222

23-
#include <string>
24-
#include <nlohmann/json.hpp>
25-
2623
#ifndef ANIMATEDLEDSTRIP_STRIPINFO_HPP
2724
#define ANIMATEDLEDSTRIP_STRIPINFO_HPP
2825

26+
#include <iostream>
27+
#include <string>
28+
#include <nlohmann/json.hpp>
29+
2930
class StripInfo {
3031
public:
3132
int numLEDs = 0;
@@ -50,7 +51,7 @@ class StripInfo {
5051
return *this;
5152
}
5253

53-
StripInfo & setFileName(const std::string f) {
54+
StripInfo & setFileName(const std::string & f) {
5455
fileName.assign(f);
5556
return *this;
5657
}

test/AnimationDataTest.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ namespace {
173173
data.json(&data_str);
174174
EXPECT_STREQ(data_str,
175175
"DATA:{\"animation\":\"Meteor\",\"colors\":[{\"colors\":[255,65280]},{\"colors\":[16711680]}],\"center\":50,\"continuous\":false,\"delay\":10,\"delayMod\":1.500000,\"direction\":\"BACKWARD\",\"distance\":45,\"id\":\"TEST\",\"section\":\"SECT\",\"spacing\":5}");
176+
delete[] data_str;
176177
}
177178

178179
TEST(AnimationData, FromJSON) {

test/SectionTest.hpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2019-2020 AnimatedLEDStrip
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
24+
#ifndef ANIMATEDLEDSTRIP_SECTIONTEST_HPP
25+
#define ANIMATEDLEDSTRIP_SECTIONTEST_HPP
26+
27+
#include "Section.hpp"
28+
#include "nlohmann/json.hpp"
29+
#include "gtest/gtest.h"
30+
31+
32+
namespace {
33+
34+
TEST(Section, DefaultConstructor) {
35+
const Section sect = Section();
36+
37+
EXPECT_STREQ(sect.name.c_str(), "");
38+
EXPECT_EQ(sect.startPixel, -1);
39+
EXPECT_EQ(sect.endPixel, -1);
40+
EXPECT_EQ(sect.physicalStart, -1);
41+
EXPECT_EQ(sect.numLEDs, 0);
42+
}
43+
44+
TEST(Section, SetName) {
45+
Section sect = Section();
46+
EXPECT_STREQ(sect.name.c_str(), "");
47+
char test[5];
48+
strcpy(test, "Sect");
49+
sect.setName(test);
50+
EXPECT_STREQ(sect.name.c_str(), "Sect");
51+
std::string test2 = "TestSect";
52+
sect.setName(test2);
53+
EXPECT_STREQ(sect.name.c_str(), "TestSect");
54+
}
55+
56+
TEST(Section, SetStartPixel) {
57+
Section sect = Section();
58+
EXPECT_EQ(sect.startPixel, -1);
59+
sect.setStartPixel(5);
60+
EXPECT_EQ(sect.startPixel, 5);
61+
}
62+
63+
TEST(Section, SetEndPixel) {
64+
Section sect = Section();
65+
EXPECT_EQ(sect.endPixel, -1);
66+
sect.setEndPixel(5);
67+
EXPECT_EQ(sect.endPixel, 5);
68+
}
69+
70+
TEST(Section, SetPhysicalStart) {
71+
Section sect = Section();
72+
EXPECT_EQ(sect.physicalStart, -1);
73+
sect.setPhysicalStart(5);
74+
EXPECT_EQ(sect.physicalStart, 5);
75+
}
76+
77+
TEST(Section, SetNumLEDs) {
78+
Section sect = Section();
79+
EXPECT_EQ(sect.numLEDs, 0);
80+
sect.setNumLEDs(5);
81+
EXPECT_EQ(sect.numLEDs, 5);
82+
}
83+
84+
TEST(Section, JSON) {
85+
Section sect = Section();
86+
87+
sect.setName("Section");
88+
sect.setStartPixel(15);
89+
sect.setEndPixel(30);
90+
91+
char * data_str = new char[1000];
92+
sect.json(&data_str);
93+
EXPECT_STREQ(data_str, "SECT:{\"name\":\"Section\",\"startPixel\":15,\"endPixel\":30}");
94+
delete[] data_str;
95+
}
96+
97+
TEST(Section, FromJSON) {
98+
std::string data_str = R"({"name":"NewSection","startPixel":20,"endPixel":55,"physicalStart":40,"numLEDs":36})";
99+
100+
nlohmann::json data_json = nlohmann::json::parse(data_str);
101+
Section sect = Section(data_json);
102+
103+
EXPECT_STREQ(sect.name.c_str(), "NewSection");
104+
EXPECT_EQ(sect.startPixel, 20);
105+
EXPECT_EQ(sect.endPixel, 55);
106+
EXPECT_EQ(sect.physicalStart, 40);
107+
EXPECT_EQ(sect.numLEDs, 36);
108+
}
109+
110+
TEST(Section, FromBadJSON) {
111+
std::string data_str = "{}";
112+
113+
nlohmann::json data_json = nlohmann::json::parse(data_str);
114+
Section sect = Section(data_json);
115+
116+
EXPECT_STREQ(sect.name.c_str(), "");
117+
EXPECT_EQ(sect.startPixel, -1);
118+
EXPECT_EQ(sect.endPixel, -1);
119+
EXPECT_EQ(sect.physicalStart, -1);
120+
EXPECT_EQ(sect.numLEDs, 0);
121+
}
122+
123+
}
124+
125+
#endif //ANIMATEDLEDSTRIP_SECTIONTEST_HPP

test/run_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
#include "AnimationDataTest.hpp"
2424
#include "AnimationInfoTest.hpp"
25+
#include "AnimationSender.hpp"
2526
#include "ColorContainerTest.hpp"
2627
#include "EndAnimationTest.hpp"
28+
#include "SectionTest.hpp"
2729
#include "StripInfoTest.hpp"
2830
#include "gtest/gtest.h"
2931

0 commit comments

Comments
 (0)