Skip to content

Commit f8e7894

Browse files
committed
Implement NeoPixel configuration in hardware
1 parent 5576abc commit f8e7894

File tree

7 files changed

+120
-46
lines changed

7 files changed

+120
-46
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void Wippersnapper_V2::provision() {
117117
}
118118

119119
WsV2._display->enableLogging();
120-
releaseStatusLED(); // don't use status LED if we are using the display
120+
ReleaseStatusPixel(); // don't use status LED if we are using the display
121121
// UI Setup
122122
WsV2._ui_helper = new ws_display_ui_helper(WsV2._display);
123123
WsV2._ui_helper->set_bg_black();

src/components/digitalIO/controller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool DigitalIOController::Handle_DigitalIO_Add(pb_istream_t *stream) {
7676

7777
// Check if the provided pin is also the status LED pin
7878
if (_dio_hardware->IsStatusLEDPin(pin_name))
79-
releaseStatusLED();
79+
ReleaseStatusPixel();
8080

8181
// Deinit the pin if it's already in use
8282
if (GetPinIdx(pin_name) != -1)

src/components/pixels/controller.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ PixelsController::~PixelsController() {
4444
*/
4545
/**************************************************************************/
4646
bool PixelsController::Handle_Pixels_Add(pb_istream_t *stream) {
47+
// Attempt to decode the istream
48+
if (!_pixels_model->DecodePixelsAdd(stream))
49+
return false;
50+
// Get the decoded message
51+
wippersnapper_pixels_PixelsAdd *msg_add = _pixels_model->GetPixelsAddMsg();
52+
_pixel_strands[_num_strands] = new PixelsHardware();
53+
54+
// TODO: Call ConfigureStrand()!
55+
bool did_init = false;
56+
did_init = _pixel_strands[_num_strands]->ConfigureStrand(
57+
msg_add->pixels_type, msg_add->pixels_ordering, msg_add->pixels_num,
58+
msg_add->pixels_brightness, msg_add->pixels_pin_data,
59+
msg_add->pixels_pin_dotstar_clock);
60+
if (!did_init)
61+
WS_DEBUG_PRINTLN("[pixels] Failed to create strand!");
62+
// TODO: Implement the wippersnapper_pixels_PixelsAdded message back to the
63+
// broker here
4764
}
4865

4966
/**************************************************************************/
@@ -54,8 +71,7 @@ bool PixelsController::Handle_Pixels_Add(pb_istream_t *stream) {
5471
@returns True if successful, False otherwise
5572
*/
5673
/**************************************************************************/
57-
bool PixelsController::Handle_Pixels_Write(pb_istream_t *stream) {
58-
}
74+
bool PixelsController::Handle_Pixels_Write(pb_istream_t *stream) {}
5975

6076
/**************************************************************************/
6177
/*!
@@ -65,5 +81,4 @@ bool PixelsController::Handle_Pixels_Write(pb_istream_t *stream) {
6581
@returns True if successful, False otherwise
6682
*/
6783
/**************************************************************************/
68-
bool PixelsController::Handle_Pixels_Remove(pb_istream_t *stream) {
69-
}
84+
bool PixelsController::Handle_Pixels_Remove(pb_istream_t *stream) {}

src/components/pixels/hardware.cpp

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,90 @@
2121
*/
2222
/**************************************************************************/
2323
PixelsHardware::PixelsHardware() {
24+
_type = wippersnapper_pixels_PixelsType_PIXELS_TYPE_UNSPECIFIED;
2425
}
2526

2627
/**************************************************************************/
2728
/*!
2829
@brief Destructs a PixelsHardware object
2930
*/
3031
/**************************************************************************/
31-
PixelsHardware::~PixelsHardware() {
32+
PixelsHardware::~PixelsHardware() {}
33+
34+
bool PixelsHardware::AddNeoPixel(uint16_t num_pixels, uint16_t pin_data,
35+
neoPixelType order, uint8_t brightness) {
36+
if (getStatusNeoPixelPin() == pin_data && WsV2.lockStatusNeoPixelV2)
37+
ReleaseStatusPixel(); // Release the status pixel for use
38+
39+
_neopixel = new Adafruit_NeoPixel((uint16_t)num_pixels, pin_data, order);
40+
_neopixel->begin();
41+
_neopixel->setBrightness((uint8_t)brightness);
42+
_neopixel->clear();
43+
_neopixel->show();
44+
// Check if the NeoPixel object was created successfully
45+
if (_neopixel->numPixels() != num_pixels)
46+
return false;
47+
48+
WS_DEBUG_PRINT("[pixels] Added NeoPixel strand on pin ");
49+
WS_DEBUG_PRINT(pin_data);
50+
return true;
3251
}
3352

34-
/**************************************************************************/
35-
/*!
36-
@brief Configures a pixel strand
37-
@param pin_data
38-
Data pin for the pixel strand
39-
@param pin_clock
40-
Clock pin for DotStar pixel strands
41-
@param type
42-
Type of pixel strand (NeoPixel, DotStar)
43-
@param order
44-
Color ordering of pixels
45-
@param num_pixels
46-
Number of pixels in the strand
47-
@param brightness
48-
Initial brightness (0-255)
49-
@returns True if successful, False otherwise
50-
*/
51-
/**************************************************************************/
52-
bool PixelsHardware::ConfigurePixelStrand(uint8_t pin_data, uint8_t pin_clock,
53-
wippersnapper_pixels_PixelsType type,
54-
wippersnapper_pixels_PixelsOrder order,
55-
uint32_t num_pixels, uint32_t brightness) {
53+
bool PixelsHardware::ConfigureStrand(wippersnapper_pixels_PixelsType type,
54+
wippersnapper_pixels_PixelsOrder order,
55+
uint32_t num_pixels, uint32_t brightness,
56+
const char *pin_data,
57+
const char *pin_clock) {
58+
_type = type;
59+
// Convert the pin string to an integer
60+
uint16_t p_data = atoi(pin_data + 1);
61+
// pin_clock is OPTIONALLY passed for a dotstar
62+
if (pin_clock != nullptr)
63+
uint16_t p_clock = atoi(pin_clock + 1);
64+
// Generics, TODO
65+
66+
// TODO: Wrap the initialization into a function instead of within the
67+
// conditional
68+
if (_type == wippersnapper_pixels_PixelsType_PIXELS_TYPE_NEOPIXEL) {
69+
if (getStatusNeoPixelPin() == p_data && WsV2.lockStatusNeoPixelV2) {
70+
ReleaseStatusPixel(); // Release the status pixel for use
71+
}
72+
if (!AddNeoPixel(num_pixels, p_data, GetStrandOrder(order),
73+
(uint8_t)brightness)) {
74+
WS_DEBUG_PRINTLN("[pixels] Failed to create NeoPixel strand!");
75+
return false;
76+
}
77+
return true;
78+
} else if (_type == wippersnapper_pixels_PixelsType_PIXELS_TYPE_DOTSTAR) {
79+
// TODO! DOTSTAR
80+
} else {
81+
// TODO! Signal!!!
82+
return false;
83+
}
84+
return true;
85+
}
86+
87+
void PixelsHardware::begin() {
88+
// TODO:
89+
// https://github.com/adafruit/Adafruit_Wippersnapper_Arduino/blob/main/src/components/pixels/ws_pixels.cpp#L258
90+
}
91+
92+
neoPixelType
93+
PixelsHardware::GetStrandOrder(wippersnapper_pixels_PixelsOrder order) {
94+
switch (order) {
95+
case wippersnapper_pixels_PixelsOrder_PIXELS_ORDER_GRB:
96+
return NEO_GRB + NEO_KHZ800;
97+
case wippersnapper_pixels_PixelsOrder_PIXELS_ORDER_GRBW:
98+
return NEO_GRBW + NEO_KHZ800;
99+
case wippersnapper_pixels_PixelsOrder_PIXELS_ORDER_RGB:
100+
return NEO_RGB + NEO_KHZ800;
101+
case wippersnapper_pixels_PixelsOrder_PIXELS_ORDER_RGBW:
102+
return NEO_RGBW + NEO_KHZ800;
103+
case wippersnapper_pixels_PixelsOrder_PIXELS_ORDER_BRG:
104+
return NEO_BRG + NEO_KHZ800;
105+
default:
106+
return NEO_GRB + NEO_KHZ800;
107+
}
56108
}
57109

58110
/**************************************************************************/
@@ -64,8 +116,7 @@ bool PixelsHardware::ConfigurePixelStrand(uint8_t pin_data, uint8_t pin_clock,
64116
32-bit color value
65117
*/
66118
/**************************************************************************/
67-
void PixelsHardware::SetPixelColor(uint8_t pin_data, uint32_t color) {
68-
}
119+
void PixelsHardware::SetPixelColor(uint8_t pin_data, uint32_t color) {}
69120

70121
/**************************************************************************/
71122
/*!
@@ -74,5 +125,4 @@ void PixelsHardware::SetPixelColor(uint8_t pin_data, uint32_t color) {
74125
Data pin for the pixel strand
75126
*/
76127
/**************************************************************************/
77-
void PixelsHardware::deinit(uint8_t pin_data) {
78-
}
128+
void PixelsHardware::deinit(uint8_t pin_data) {}

src/components/pixels/hardware.h

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
* @brief This struct represents a NeoPixel or DotStar strand.
2323
*/
2424
struct PixelStrand {
25-
uint8_t pin_data; ///< Data pin
26-
uint8_t pin_clock; ///< Clock pin (for DotStar)
27-
wippersnapper_pixels_PixelsType type; ///< Pixel type
28-
wippersnapper_pixels_PixelsOrder order; ///< Color ordering
29-
uint32_t num_pixels; ///< Number of pixels
30-
uint32_t brightness; ///< Current brightness (0-255)
31-
};
25+
uint8_t pin_data; ///< Data pin
26+
uint8_t pin_clock; ///< Clock pin (for DotStar)
27+
wippersnapper_pixels_PixelsType type; ///< Pixel type
28+
wippersnapper_pixels_PixelsOrder order; ///< Color ordering
29+
uint32_t num_pixels; ///< Number of pixels
30+
uint32_t brightness; ///< Current brightness (0-255)
31+
};
3232

3333
/**************************************************************************/
3434
/*!
@@ -39,12 +39,21 @@ class PixelsHardware {
3939
public:
4040
PixelsHardware();
4141
~PixelsHardware();
42-
bool ConfigurePixelStrand(uint8_t pin_data, uint8_t pin_clock,
43-
wippersnapper_pixels_PixelsType type,
44-
wippersnapper_pixels_PixelsOrder order,
45-
uint32_t num_pixels, uint32_t brightness);
42+
bool ConfigureStrand(wippersnapper_pixels_PixelsType type,
43+
wippersnapper_pixels_PixelsOrder order,
44+
uint32_t num_pixels, uint32_t brightness,
45+
const char *pin_data, const char *pin_clock);
46+
void begin();
4647
void SetPixelColor(uint8_t pin_data, uint32_t color);
4748
void deinit(uint8_t pin_data);
49+
bool AddNeoPixel(uint16_t num_pixels, uint16_t pin_data, neoPixelType order,
50+
uint8_t brightness);
51+
// helpers
52+
neoPixelType GetStrandOrder(wippersnapper_pixels_PixelsOrder order);
53+
4854
private:
55+
Adafruit_NeoPixel *_neopixel = nullptr; ///< Used for NeoPixel pixel strands
56+
Adafruit_DotStar *_dotstar = nullptr; ///< Used for DotStar pixel strands
57+
wippersnapper_pixels_PixelsType _type;
4958
};
5059
#endif // WS_PIXELS_HARDWARE_H

src/components/statusLED/Wippersnapper_StatusLED.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void initStatusLED() {
9999
@brief De-initializes the status LED and releases pin.
100100
*/
101101
/****************************************************************************/
102-
void releaseStatusLED() {
102+
void ReleaseStatusPixel() {
103103
#ifdef USE_STATUS_NEOPIXEL
104104
delete statusPixel; // Deallocate Adafruit_NeoPixel object, set data pin back
105105
// to INPUT.

src/components/statusLED/Wippersnapper_StatusLED.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ typedef enum ws_led_status_t {
6060

6161
// Status LED
6262
void initStatusLED();
63-
void releaseStatusLED();
63+
void ReleaseStatusPixel();
6464
int16_t getStatusNeoPixelPin();
6565
int16_t getStatusDotStarDataPin();
6666
uint32_t ledStatusStateToColor(ws_led_status_t statusState);

0 commit comments

Comments
 (0)