Skip to content

Commit 19ed9b8

Browse files
committed
Fix spacing and naming format
1 parent 2ae41f8 commit 19ed9b8

File tree

2 files changed

+78
-90
lines changed

2 files changed

+78
-90
lines changed

src/Arduino_APA102.cpp

Lines changed: 70 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,40 @@
2121

2222
#include <Arduino.h>
2323

24-
Arduino_APA102::Arduino_APA102(uint8_t newNumLeds, uint8_t newDataPin, uint8_t newClockPin)
25-
{
26-
free(_pixel);
24+
#define LED_FRAME_RED(indexLed)(_numLeds * indexLed)
25+
#define LED_FRAME_GREEN(indexLed)((_numLeds * indexLed) +1)
26+
#define LED_FRAME_BLUE(indexLed)((_numLeds * indexLed) + 2)
27+
#define LED_FRAME_BRIGHTNESS(indexLed)((_numLeds * indexLed) + 3)
28+
#define LED_FRAME_CURRENT(indexLed)((_numLeds * indexLed) + 4)
29+
#define LED_FRAME_FULL(indexLed)(indexLed * 5)
30+
31+
Arduino_APA102::Arduino_APA102(uint8_t newNumLeds, uint8_t newDataPin, uint8_t newClockPin){
32+
free(_led);
2733

2834
uint16_t n = newNumLeds;
2935
_numLeds = newNumLeds;
3036
_SDA_pin = newDataPin;
3137
_SCK_pin = newClockPin;
3238

33-
uint16_t bytes = n * 5; // R,G,B, Bright, Current
39+
uint16_t bytes = n * 5; // R,G,B, Bright, Current
3440

35-
if ((_pixel = (uint8_t *)malloc(bytes))) {
41+
if ((_led = (uint8_t *)malloc(bytes))){
3642
_numLeds = n;
37-
memset(_pixel,0,_numLeds * 5);
38-
}
43+
memset(_led, 0, LED_FRAME_FULL(_numLeds));
44+
}
3945
}
4046

4147
void Arduino_APA102::writeBuffer(int index, uint8_t dataR, uint8_t dataG, uint8_t dataB, uint8_t dataBrightness, uint8_t dataCurrent){
4248
int n = 0;
43-
_pixel[(n * 5)] = dataR;
44-
_pixel[(n * 5)+ 1] = dataG;
45-
_pixel[(n * 5)+ 2] = dataB;
46-
_pixel[(n * 5)+ 3] = dataBrightness;
47-
_pixel[(n * 5)+ 4] = dataCurrent;
49+
_led[LED_FRAME_RED(index)] = dataR;
50+
_led[LED_FRAME_GREEN(index)] = dataG;
51+
_led[LED_FRAME_BLUE(index)] = dataB;
52+
_led[LED_FRAME_BRIGHTNESS(index)] = dataBrightness;
53+
_led[LED_FRAME_CURRENT(index)] = dataCurrent;
4854
}
4955

5056
Arduino_APA102::~Arduino_APA102(){
51-
free(_pixel);
57+
free(_led);
5258
}
5359

5460
void Arduino_APA102::begin(void){
@@ -58,7 +64,7 @@ void Arduino_APA102::begin(void){
5864
digitalWrite(_SDA_pin, LOW);
5965
digitalWrite(_SCK_pin, LOW);
6066

61-
clear(); // Default data, empty colors, full Current and Brightness
67+
clear(); // Default data, empty colors, full Current and Brightness
6268
}
6369

6470
void Arduino_APA102::end(void){
@@ -67,143 +73,128 @@ void Arduino_APA102::end(void){
6773
}
6874

6975
void Arduino_APA102::show(void){
70-
71-
// 3 blocks of 32 bits:
72-
// [0x00][0x00][0x00][0x00] - START LED, (startFrame)
73-
// [LED | Current][B][G][R] - LED data
74-
// 3bit|5bit 8b 8b 8b
75-
// [0xFF][0xFF][0xFF][0xFF] - START LED (endFrame) - not all 1s, Adafruit_DotStar found the way to avoid the empty LEDs become white
76+
// 3 blocks of 32 bits:
77+
// [0x00][0x00][0x00][0x00] - START LED, (startFrame)
78+
// [LED | Current][B][G][R] - LED data
79+
// 3bit|5bit 8b 8b 8b
80+
// [0xFF][0xFF][0xFF][0xFF] - START LED (endFrame) - not all 1s, Adafruit_DotStar found the way to avoid the empty LEDs become white
7681

7782
_startFrame();
78-
83+
7984
// Get the data from the buffer
8085
// Send to the line
8186
// Each LED has 5 bytes
82-
uint8_t *pColor = _pixel;
8387

84-
for (int c = 0; c < _numLeds; c++) {
85-
uint8_t bright = _pixel[(_numLeds * c) +3];
86-
87-
uint16_t newRed = map(bright,0,100,0, _pixel[(_numLeds * c)]);;
88-
uint16_t newGreen = map(bright,0,100,0, _pixel[(_numLeds * c) +1]);;
89-
uint16_t newBlue = map(bright,0,100,0, _pixel[(_numLeds * c) +2]);;
88+
for (int c = 0; c < _numLeds; c++){
89+
uint8_t bright = _led[LED_FRAME_BRIGHTNESS(c)];
90+
91+
uint16_t newRed = map(bright, 0, 100, 0, _led[LED_FRAME_RED(c)]);
92+
uint16_t newGreen = map(bright, 0, 100, 0, _led[LED_FRAME_GREEN(c)]);
93+
uint16_t newBlue = map(bright, 0, 100, 0, _led[LED_FRAME_BLUE(c)]);
9094

91-
uint8_t current = uint8_t(_pixel[(_numLeds * c) +4]);
95+
uint8_t current = uint8_t(_led[LED_FRAME_CURRENT(c)]);
9296

9397
_write8(uint8_t(0b111 << 5 | current));
9498
_write8(newBlue);
9599
_write8(newGreen);
96100
_write8(newRed);
97101
}
98-
99-
_endFrame();
100-
101-
// Use it to know the "registers"
102-
/*
103-
for (int c = 0; c < _numLeds * 5; c++){
104-
Serial.print("N : ");
105-
Serial.print(c);
106-
Serial.print(" ");
107-
Serial.println(_pixel[c]);
108-
}
109-
*/
102+
103+
_endFrame();
110104
}
111105

112106
void Arduino_APA102::setPixelColor(uint16_t indexLed, uint32_t newColor){
113-
uint8_t newRed =uint8_t(newColor >> 16);
107+
uint8_t newRed = uint8_t(newColor >> 16);
114108
uint8_t newGreen = uint8_t(newColor >> 8);
115109
uint8_t newBlue = uint8_t(newColor);
116-
117-
setPixelColor(indexLed,newRed,newGreen,newBlue);
110+
111+
setPixelColor(indexLed, newRed, newGreen, newBlue);
118112
}
119113

120114
void Arduino_APA102::setPixelColor(uint16_t indexLed, uint8_t red, uint8_t green, uint8_t blue){
121-
if(indexLed < 0 && indexLed > _numLeds){}else{
115+
if (indexLed < 0 && indexLed > _numLeds){
116+
}else{
122117

123-
uint8_t brightness = _pixel[(indexLed* 5)+3];
118+
uint8_t brightness = _led[LED_FRAME_BRIGHTNESS(indexLed)];
124119

125-
_pixel[(indexLed* 5)] = red;
126-
_pixel[(indexLed* 5)+1] = green;
127-
_pixel[(indexLed* 5)+2] = blue;
120+
_led[LED_FRAME_RED(indexLed)] = red;
121+
_led[LED_FRAME_GREEN(indexLed)] = green;
122+
_led[LED_FRAME_BLUE(indexLed)] = blue;
128123
}
129124
}
130125

131-
void Arduino_APA102::fill(uint32_t newColor , uint16_t startLed, uint16_t count ){
126+
void Arduino_APA102::fill(uint32_t newColor, uint16_t startLed, uint16_t count){
132127
for (int c = startLed; c < count; c++){
133128
setPixelColor(c, newColor);
134129
}
135130
}
136131

137-
void Arduino_APA102::setBrightness(uint8_t newBrightness){ // Set a global Brightness
138-
for (int i = 0; i< _numLeds;i++){
139-
setBrightness(i , newBrightness);
132+
// Set a global Brightness
133+
void Arduino_APA102::setBrightness(uint8_t newBrightness){
134+
for (int i = 0; i < _numLeds; i++){
135+
setBrightness(i, newBrightness);
140136
}
141137
}
142138

143-
void Arduino_APA102::setBrightness(uint8_t indexLed, uint8_t newBrightness){ // Set individual LED brightness
144-
newBrightness &= 0x64; //Max 100(dec)
145-
_pixel[(indexLed* 5)+3] = newBrightness;
146-
139+
// Set individual LED brightness
140+
void Arduino_APA102::setBrightness(uint8_t indexLed, uint8_t newBrightness){
141+
_led[LED_FRAME_BRIGHTNESS(indexLed)] = newBrightness;
147142
}
148143

149144
// Set global Max current, its NOT brightness, depending on the color its going to look different even if it has the same max current and amoumt of color (LEDs color's voltage)
150-
void Arduino_APA102::setCurrent(uint8_t newCurrent){
151-
for (int i = 0; i< _numLeds;i++){
152-
setCurrent(i , newCurrent);
145+
void Arduino_APA102::setCurrent(uint8_t newCurrent){
146+
for (int i = 0; i < _numLeds; i++){
147+
setCurrent(i, newCurrent);
153148
}
154-
155149
}
156150
// Set each LED's voltage
157151
void Arduino_APA102::setCurrent(uint8_t indexLed, uint8_t newCurrent){
158-
newCurrent &= 0x1F; //Max 31(dec)
159-
_pixel[(indexLed* 5)+4] = newCurrent;
152+
_led[(indexLed * 5) + 4] = newCurrent;
160153
}
161154

162155
// Empty all he LEDs
163156
// Default data
164157
void Arduino_APA102::clear(){
165-
memset(_pixel, 0, _numLeds);
158+
memset(_led, 0, _numLeds);
166159
for (int c = 0; c < _numLeds; c++){
167-
_pixel[(_numLeds * c)] = 0;
168-
_pixel[(_numLeds * c) +1] = 0;
169-
_pixel[(_numLeds * c) +2] = 0;
160+
_led[LED_FRAME_RED(c)] = 0;
161+
_led[LED_FRAME_GREEN(c)] = 0;
162+
_led[LED_FRAME_BLUE(c)] = 0;
170163

171-
_pixel[(_numLeds * c) +3] = 100; //Max bright 100%
172-
_pixel[(_numLeds * c) +4] = 31; //Max Current, 0b11111
164+
_led[LED_FRAME_BRIGHTNESS(c)] = 100; //Max bright 100%
165+
_led[LED_FRAME_CURRENT(c)] = 31; //Max Current, 0b11111
173166
}
174167
}
175168

176-
177169
//Protocol blocks
178170

179171
// Start frame block
180172
void Arduino_APA102::_startFrame(){
181-
for (int i = 0; i<4; i++){
173+
for (int i = 0; i < 4; i++){
182174
_write8(0x00);
183175
}
184176
}
185177

186178
// End frame block
187179
void Arduino_APA102::_endFrame(){
188-
for (int i = 0; i < ((_numLeds + 15) / 16); i++) { //Avoid to set the left LEDs white, taken from Adafruit_DotStar
189-
_write8(0xFF); //
180+
for (int i = 0; i < ((_numLeds + 15) / 16); i++){ //Avoid to set the left LEDs white, taken from Adafruit_DotStar
181+
_write8(0xFF); //
190182
}
191183
}
192184

193185
// Send the data to the Line
194186
void Arduino_APA102::_write8(uint8_t data){
195-
196187
// Convert the bit into a state H/L
197-
for (uint8_t i = 8; i--; data <<= 1) {
198-
188+
for (uint8_t i = 8; i--; data <<= 1){
189+
199190
if (data & 0x80){
200191
digitalWrite(_SDA_pin, HIGH);
201192
}else{
202193
digitalWrite(_SDA_pin, LOW);
203194
}
204195

205-
// Pulse
206-
digitalWrite(_SCK_pin, HIGH);
207-
digitalWrite(_SCK_pin, LOW);
196+
// Pulse
197+
digitalWrite(_SCK_pin, HIGH);
198+
digitalWrite(_SCK_pin, LOW);
208199
}
209200
}

src/Arduino_APA102.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "Arduino.h"
2424

25-
class Arduino_APA102 {
25+
class Arduino_APA102{
2626
public:
2727
Arduino_APA102(uint8_t numLEDs, uint8_t newDataPin, uint8_t newClockPin);
2828
~Arduino_APA102();
@@ -33,19 +33,17 @@ class Arduino_APA102 {
3333
void show(void);
3434
void clear(void);
3535

36-
3736
void setPixelColor(uint16_t indexLed, uint32_t newColor);
3837
void setPixelColor(uint16_t indexLed, uint8_t red, uint8_t green, uint8_t blue);
39-
void fill(uint32_t newColor , uint16_t startLed , uint16_t count );
38+
void fill(uint32_t newColor, uint16_t startLed, uint16_t count);
4039

4140
void setBrightness(uint8_t newBrightness);
4241
void setBrightness(uint8_t indexLed, uint8_t newBrightness);
4342

4443
void setCurrent(uint8_t newCurrent);
4544
void setCurrent(uint8_t indexLed, uint8_t newCurrent);
46-
47-
48-
uint32_t Color(uint8_t newRed, uint8_t newGreen, uint8_t newBlue) {
45+
46+
uint32_t Color(uint8_t newRed, uint8_t newGreen, uint8_t newBlue){
4947
return (uint32_t)(newRed << 16 | newGreen << 8 | newBlue);
5048
}
5149

@@ -59,12 +57,11 @@ class Arduino_APA102 {
5957
void _startFrame(void);
6058
void _endFrame(void);
6159

62-
uint8_t _numLeds ;
63-
uint8_t _SCK_pin ;
64-
uint8_t _SDA_pin ;
60+
uint8_t _numLeds;
61+
uint8_t _SCK_pin;
62+
uint8_t _SDA_pin;
6563

66-
uint8_t *_pixel; //5 bytes - R, G, B, Current, Btightness
67-
64+
uint8_t *_led; //5 bytes - R, G, B, Current, Btightnes
6865
};
6966

7067
#endif

0 commit comments

Comments
 (0)