Skip to content

Commit ec2291f

Browse files
committed
pass pixel add, write
1 parent 700f387 commit ec2291f

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

src/components/pixels/controller.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ PixelsController::~PixelsController() {
4747
/**************************************************************************/
4848
bool PixelsController::Handle_Pixels_Add(pb_istream_t *stream) {
4949
// Attempt to decode the istream into a PixelsAdd message
50-
if (!_pixels_model->DecodePixelsAdd(stream))
50+
if (!_pixels_model->DecodePixelsAdd(stream)) {
51+
WS_DEBUG_PRINTLN("[pixels]: Failed to decode PixelsAdd message!");
5152
return false;
53+
}
5254
wippersnapper_pixels_PixelsAdd *msg_add = _pixels_model->GetPixelsAddMsg();
5355
_pixel_strands[_num_strands] = new PixelsHardware();
5456

@@ -72,6 +74,13 @@ bool PixelsController::Handle_Pixels_Add(pb_istream_t *stream) {
7274
return false;
7375
}
7476

77+
// Increment the strand counter if initialization was successful
78+
if (did_init) {
79+
_num_strands++;
80+
WS_DEBUG_PRINT("[pixels]: Added strand #");
81+
WS_DEBUG_PRINTLN(_num_strands);
82+
}
83+
7584
return true;
7685
}
7786

@@ -93,12 +102,13 @@ bool PixelsController::Handle_Pixels_Write(pb_istream_t *stream) {
93102
_pixels_model->GetPixelsWriteMsg();
94103
uint16_t pin_data = atoi(msg_write->pixels_pin_data + 1);
95104
uint16_t idx = GetStrandIndex(pin_data);
96-
if (idx == -1) {
105+
if (idx == 0xFF) {
97106
WS_DEBUG_PRINTLN("[pixels]: Failed to find strand index!");
98107
return false;
99108
}
100109

101110
// Call hardware to fill the strand
111+
WS_DEBUG_PRINTLN("[pixels]: Filling strand!");
102112
_pixel_strands[idx]->FillStrand(msg_write->pixels_color);
103113
return true;
104114
}
@@ -122,7 +132,7 @@ bool PixelsController::Handle_Pixels_Remove(pb_istream_t *stream) {
122132

123133
uint16_t pin_data = atoi(msg_remove->pixels_pin_data + 1);
124134
uint16_t idx = GetStrandIndex(pin_data);
125-
if (idx == -1) {
135+
if (idx == 0xFF) {
126136
WS_DEBUG_PRINTLN("[pixels]: Failed to find strand index!");
127137
return false;
128138
}
@@ -134,17 +144,19 @@ bool PixelsController::Handle_Pixels_Remove(pb_istream_t *stream) {
134144

135145
/**************************************************************************/
136146
/*!
137-
@brief Handles a request to update the pixel strands
147+
@brief Gets the index of a strand by its data pin
138148
@param pin_data
139149
The desired data pin
140-
@returns Desired strand index, -1 if not found.
150+
@returns Desired strand index, or 0xFF if not found.
141151
*/
142152
/**************************************************************************/
143153
uint16_t PixelsController::GetStrandIndex(uint16_t pin_data) {
144-
for (int i = 0; i < _num_strands; i++) {
154+
for (uint8_t i = 0; i < _num_strands; i++) {
145155
if (_pixel_strands[i]->GetPinData() == pin_data) {
146156
return i;
147157
}
148158
}
149-
return -1;
159+
WS_DEBUG_PRINT("[pixels]: No strand found on pin ");
160+
WS_DEBUG_PRINTLN(pin_data);
161+
return 0xFF; // Sentinel value indicating "not found"
150162
}

src/components/pixels/controller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PixelsController {
4343
PixelsModel *_pixels_model = nullptr; ///< Pointer to the model class
4444
PixelsHardware *_pixel_strands[MAX_PIXEL_STRANDS] = {nullptr}; ///< Pointer to the hardware class
4545
uint8_t _num_strands; ///< Number of pixel strands
46-
uint16_t GetStrandIndex(uint16_t pin_data);
46+
uint16_t GetStrandIndex(uint16_t pin_data); // Returns 0xFF if not found
4747
};
4848
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
4949
#endif // WS_PIXELS_CONTROLLER_H

src/components/pixels/hardware.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ PixelsHardware::~PixelsHardware() {}
4747
/**************************************************************************/
4848
bool PixelsHardware::AddNeoPixel(uint16_t num_pixels, uint16_t pin_data,
4949
neoPixelType order, uint8_t brightness) {
50-
if (getStatusNeoPixelPin() == pin_data && WsV2.lockStatusNeoPixelV2)
50+
if (getStatusNeoPixelPin() == pin_data && WsV2.lockStatusNeoPixelV2) {
51+
WS_DEBUG_PRINTLN("[pixels] Releasing status pixel for use");
5152
ReleaseStatusPixel(); // Release the status pixel for use
53+
}
5254

5355
_neopixel = new Adafruit_NeoPixel(num_pixels, pin_data, order);
5456
_neopixel->begin();
@@ -131,6 +133,8 @@ bool PixelsHardware::AddStrand(wippersnapper_pixels_PixelsType type,
131133
_type = type;
132134
// Convert the pin string to an integer
133135
uint16_t p_data = atoi(pin_data + 1);
136+
_pin_data = p_data; // Store the pin data for later reference
137+
134138
if (_type == wippersnapper_pixels_PixelsType_PIXELS_TYPE_NEOPIXEL) {
135139
if (!AddNeoPixel(num_pixels, p_data, GetStrandOrderNeoPixel(order),
136140
(uint8_t)brightness)) {
@@ -144,6 +148,7 @@ bool PixelsHardware::AddStrand(wippersnapper_pixels_PixelsType type,
144148
WS_DEBUG_PRINTLN("[pixels] Failed to create DotStar strand!");
145149
return false;
146150
}
151+
return true;
147152
} else {
148153
WS_DEBUG_PRINTLN("[pixels] Unknown pixel type!");
149154
return false;
@@ -160,9 +165,13 @@ bool PixelsHardware::AddStrand(wippersnapper_pixels_PixelsType type,
160165
/**************************************************************************/
161166
void PixelsHardware::FillStrand(uint32_t color) {
162167
// Apply gamma correction to match IO Web
163-
uint32_t color_gamma = ApplyGammaCorrection(color);
164168
WS_DEBUG_PRINT("[pixels] Filling strand with color: ");
165-
WS_DEBUG_PRINT(color_gamma, HEX);
169+
WS_DEBUG_PRINTLN(color);
170+
171+
WS_DEBUG_PRINTLN("Applying gamma correction..");
172+
uint32_t color_gamma = ApplyGammaCorrection(color);
173+
WS_DEBUG_PRINT("[pixels] Filling strand with color_gamma: ");
174+
WS_DEBUG_PRINT(color_gamma);
166175
if (_type == wippersnapper_pixels_PixelsType_PIXELS_TYPE_NEOPIXEL) {
167176
_neopixel->fill(color_gamma);
168177
_neopixel->show();
@@ -183,7 +192,14 @@ void PixelsHardware::FillStrand(uint32_t color) {
183192
*/
184193
/**************************************************************************/
185194
uint32_t PixelsHardware::ApplyGammaCorrection(uint32_t color) {
195+
WS_DEBUG_PRINT("Type: ");
196+
WS_DEBUG_PRINTLN(_type);
186197
if (_type == wippersnapper_pixels_PixelsType_PIXELS_TYPE_NEOPIXEL) {
198+
WS_DEBUG_PRINTLN("Applying gamma to neopixel...");
199+
if (_neopixel == nullptr) {
200+
WS_DEBUG_PRINTLN("[pixels] No neopixel object found!");
201+
return color;
202+
}
187203
return _neopixel->gamma32(color);
188204
} else if (_type == wippersnapper_pixels_PixelsType_PIXELS_TYPE_DOTSTAR) {
189205
return _dotstar->gamma32(color);
@@ -235,7 +251,8 @@ uint16_t PixelsHardware::GetPinData() { return _pin_data; }
235251
@returns The color ordering for NeoPixel strands
236252
*/
237253
/**************************************************************************/
238-
neoPixelType PixelsHardware::GetStrandOrderNeoPixel(wippersnapper_pixels_PixelsOrder order) {
254+
neoPixelType
255+
PixelsHardware::GetStrandOrderNeoPixel(wippersnapper_pixels_PixelsOrder order) {
239256
switch (order) {
240257
case wippersnapper_pixels_PixelsOrder_PIXELS_ORDER_GRB:
241258
return NEO_GRB + NEO_KHZ800;
@@ -260,7 +277,8 @@ neoPixelType PixelsHardware::GetStrandOrderNeoPixel(wippersnapper_pixels_PixelsO
260277
@returns The color ordering for DotStar strands
261278
*/
262279
/**************************************************************************/
263-
uint8_t PixelsHardware::GetStrandOrderDotStar(wippersnapper_pixels_PixelsOrder order) {
280+
uint8_t
281+
PixelsHardware::GetStrandOrderDotStar(wippersnapper_pixels_PixelsOrder order) {
264282
switch (order) {
265283
case wippersnapper_pixels_PixelsOrder_PIXELS_ORDER_GRB:
266284
return DOTSTAR_GRB;

src/components/pixels/hardware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class PixelsHardware {
4646
uint16_t GetPinData();
4747
void FillStrand(uint32_t color);
4848
void RemoveStrand();
49+
4950
private:
5051
Adafruit_NeoPixel *_neopixel = nullptr; ///< Used for NeoPixel pixel strands
5152
Adafruit_DotStar *_dotstar = nullptr; ///< Used for DotStar pixel strands

src/components/pixels/model.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ wippersnapper_pixels_PixelsRemove *PixelsModel::GetPixelsRemoveMsg() {
9393
/**************************************************************************/
9494
bool PixelsModel::DecodePixelsWrite(pb_istream_t *stream) {
9595
_msg_pixels_write = wippersnapper_pixels_PixelsWrite_init_zero;
96+
WS_DEBUG_PRINTLN("Decoding PixelsWrite message...");
9697
return pb_decode(stream, wippersnapper_pixels_PixelsWrite_fields,
9798
&_msg_pixels_write);
9899
}

0 commit comments

Comments
 (0)