-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathbase.h
172 lines (152 loc) · 4.36 KB
/
base.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* base.h
*
* MIT License
*
* Copyright (c) 2023 awawa-dev
*
* https://github.com/awawa-dev/HyperSerialPico
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef BASE_H
#define BASE_H
class Base
{
// LED strip number
int ledsNumber = 0;
// NeoPixelBusLibrary primary object
LED_DRIVER* ledStrip1 = nullptr;
// NeoPixelBusLibrary second object
LED_DRIVER2* ledStrip2 = nullptr;
// frame is set and ready to render
bool readyToRender = false;
public:
// static data buffer for the loop
volatile uint8_t buffer[MAX_BUFFER + 1] = {0};
// handle to tasks
TaskHandle_t processDataHandle = nullptr;
TaskHandle_t processSerialHandle = nullptr;
// semaphore to synchronize them
semaphore_t serialSemaphore;
semaphore_t receiverSemaphore;
// current queue position
volatile int queueCurrent = 0;
// queue end position
volatile int queueEnd = 0;
inline int getLedsNumber()
{
return ledsNumber;
}
inline LED_DRIVER* getLedStrip1()
{
return ledStrip1;
}
inline LED_DRIVER2* getLedStrip2()
{
return ledStrip2;
}
void initLedStrip(int count)
{
if (ledStrip1 != nullptr)
{
delete ledStrip1;
ledStrip1 = nullptr;
}
if (ledStrip2 != nullptr)
{
delete ledStrip2;
ledStrip2 = nullptr;
}
ledsNumber = count;
#if defined(SECOND_SEGMENT_START_INDEX)
if (ledsNumber > SECOND_SEGMENT_START_INDEX)
{
#if defined(NEOPIXEL_RGBW) || defined(NEOPIXEL_RGB)
ledStrip1 = new LED_DRIVER(SECOND_SEGMENT_START_INDEX, DATA_PIN);
ledStrip2 = new LED_DRIVER2(ledsNumber - SECOND_SEGMENT_START_INDEX, DATA_PIN);
#else
ledStrip1 = new LED_DRIVER(SECOND_SEGMENT_START_INDEX);
ledStrip1->Begin(CLOCK_PIN, 12, DATA_PIN, 15);
ledStrip2 = new LED_DRIVER2(ledsNumber - SECOND_SEGMENT_START_INDEX);
ledStrip2->Begin(SECOND_SEGMENT_CLOCK_PIN, 12, SECOND_SEGMENT_DATA_PIN, 15);
#endif
}
#endif
if (ledStrip1 == nullptr)
{
#if defined(NEOPIXEL_RGBW) || defined(NEOPIXEL_RGB)
ledStrip1 = new LED_DRIVER(ledsNumber, DATA_PIN);
#else
ledStrip1 = new LED_DRIVER(ledsNumber, SPI_INTERFACE, DATA_PIN, CLOCK_PIN);
#endif
}
}
/**
* @brief Check if there is already prepared frame to display
*
* @return true
* @return false
*/
inline bool hasLateFrameToRender()
{
return readyToRender;
}
inline void dropLateFrame()
{
readyToRender = false;
}
inline void renderLeds(bool newFrame)
{
if (newFrame)
readyToRender = true;
if (readyToRender &&
(ledStrip1 != nullptr && ledStrip1->isReadyBlocking()))
{
statistics.increaseShow();
readyToRender = false;
// display segments
#if defined(SECOND_SEGMENT_START_INDEX)
ledStrip1->renderAllLanes();
#else
ledStrip1->renderSingleLane();
#endif
}
}
inline bool setStripPixel(uint16_t pix, ColorDefinition &inputColor)
{
if (pix < ledsNumber)
{
#if defined(SECOND_SEGMENT_START_INDEX)
if (pix < SECOND_SEGMENT_START_INDEX)
ledStrip1->SetPixel(pix, inputColor);
else
{
#if defined(SECOND_SEGMENT_REVERSED)
ledStrip2->SetPixel(ledsNumber - pix - 1, inputColor);
#else
ledStrip2->SetPixel(pix - SECOND_SEGMENT_START_INDEX, inputColor);
#endif
}
#else
ledStrip1->SetPixel(pix, inputColor);
#endif
}
return (pix + 1 < ledsNumber);
}
} base;
#endif