Skip to content

Marqdevx/syntax rework #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Arduino_APA102
version=1.0.0
version=1.0.1
author=Pablo Marquínez
maintainer=Arduino <info@arduino.cc>
sentence=Control rgb LEDs APA102
Expand Down
149 changes: 70 additions & 79 deletions src/Arduino_APA102.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,40 @@

#include <Arduino.h>

Arduino_APA102::Arduino_APA102(uint8_t newNumLeds, uint8_t newDataPin, uint8_t newClockPin)
{
free(_pixel);
#define LED_FRAME_RED(indexLed)(_numLeds * indexLed)
#define LED_FRAME_GREEN(indexLed)((_numLeds * indexLed) +1)
#define LED_FRAME_BLUE(indexLed)((_numLeds * indexLed) + 2)
#define LED_FRAME_BRIGHTNESS(indexLed)((_numLeds * indexLed) + 3)
#define LED_FRAME_CURRENT(indexLed)((_numLeds * indexLed) + 4)
#define LED_FRAME_FULL(indexLed)(indexLed * 5)

Arduino_APA102::Arduino_APA102(uint8_t newNumLeds, uint8_t newDataPin, uint8_t newClockPin){
free(_led);

uint16_t n = newNumLeds;
_numLeds = newNumLeds;
_SDA_pin = newDataPin;
_SCK_pin = newClockPin;

uint16_t bytes = n * 5; // R,G,B, Bright, Current
uint16_t bytes = n * 5; // R,G,B, Bright, Current

if ((_pixel = (uint8_t *)malloc(bytes))) {
if ((_led = (uint8_t *)malloc(bytes))){
_numLeds = n;
memset(_pixel,0,_numLeds * 5);
}
memset(_led, 0, LED_FRAME_FULL(_numLeds));
}
}

void Arduino_APA102::writeBuffer(int index, uint8_t dataR, uint8_t dataG, uint8_t dataB, uint8_t dataBrightness, uint8_t dataCurrent){
int n = 0;
_pixel[(n * 5)] = dataR;
_pixel[(n * 5)+ 1] = dataG;
_pixel[(n * 5)+ 2] = dataB;
_pixel[(n * 5)+ 3] = dataBrightness;
_pixel[(n * 5)+ 4] = dataCurrent;
_led[LED_FRAME_RED(index)] = dataR;
_led[LED_FRAME_GREEN(index)] = dataG;
_led[LED_FRAME_BLUE(index)] = dataB;
_led[LED_FRAME_BRIGHTNESS(index)] = dataBrightness;
_led[LED_FRAME_CURRENT(index)] = dataCurrent;
}

Arduino_APA102::~Arduino_APA102(){
free(_pixel);
free(_led);
}

void Arduino_APA102::begin(void){
Expand All @@ -58,7 +64,7 @@ void Arduino_APA102::begin(void){
digitalWrite(_SDA_pin, LOW);
digitalWrite(_SCK_pin, LOW);

clear(); // Default data, empty colors, full Current and Brightness
clear(); // Default data, empty colors, full Current and Brightness
}

void Arduino_APA102::end(void){
Expand All @@ -67,143 +73,128 @@ void Arduino_APA102::end(void){
}

void Arduino_APA102::show(void){

// 3 blocks of 32 bits:
// [0x00][0x00][0x00][0x00] - START LED, (startFrame)
// [LED | Current][B][G][R] - LED data
// 3bit|5bit 8b 8b 8b
// [0xFF][0xFF][0xFF][0xFF] - START LED (endFrame) - not all 1s, Adafruit_DotStar found the way to avoid the empty LEDs become white
// 3 blocks of 32 bits:
// [0x00][0x00][0x00][0x00] - START LED, (startFrame)
// [LED | Current][B][G][R] - LED data
// 3bit|5bit 8b 8b 8b
// [0xFF][0xFF][0xFF][0xFF] - START LED (endFrame) - not all 1s, Adafruit_DotStar found the way to avoid the empty LEDs become white

_startFrame();

// Get the data from the buffer
// Send to the line
// Each LED has 5 bytes
uint8_t *pColor = _pixel;

for (int c = 0; c < _numLeds; c++) {
uint8_t bright = _pixel[(_numLeds * c) +3];
uint16_t newRed = map(bright,0,100,0, _pixel[(_numLeds * c)]);;
uint16_t newGreen = map(bright,0,100,0, _pixel[(_numLeds * c) +1]);;
uint16_t newBlue = map(bright,0,100,0, _pixel[(_numLeds * c) +2]);;
for (int c = 0; c < _numLeds; c++){
uint8_t bright = _led[LED_FRAME_BRIGHTNESS(c)];

uint16_t newRed = map(bright, 0, 100, 0, _led[LED_FRAME_RED(c)]);
uint16_t newGreen = map(bright, 0, 100, 0, _led[LED_FRAME_GREEN(c)]);
uint16_t newBlue = map(bright, 0, 100, 0, _led[LED_FRAME_BLUE(c)]);

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

_write8(uint8_t(0b111 << 5 | current));
_write8(newBlue);
_write8(newGreen);
_write8(newRed);
}

_endFrame();

// Use it to know the "registers"
/*
for (int c = 0; c < _numLeds * 5; c++){
Serial.print("N : ");
Serial.print(c);
Serial.print(" ");
Serial.println(_pixel[c]);
}
*/

_endFrame();
}

void Arduino_APA102::setPixelColor(uint16_t indexLed, uint32_t newColor){
uint8_t newRed =uint8_t(newColor >> 16);
uint8_t newRed = uint8_t(newColor >> 16);
uint8_t newGreen = uint8_t(newColor >> 8);
uint8_t newBlue = uint8_t(newColor);
setPixelColor(indexLed,newRed,newGreen,newBlue);

setPixelColor(indexLed, newRed, newGreen, newBlue);
}

void Arduino_APA102::setPixelColor(uint16_t indexLed, uint8_t red, uint8_t green, uint8_t blue){
if(indexLed < 0 && indexLed > _numLeds){}else{
if (indexLed < 0 && indexLed > _numLeds){
}else{

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

_pixel[(indexLed* 5)] = red;
_pixel[(indexLed* 5)+1] = green;
_pixel[(indexLed* 5)+2] = blue;
_led[LED_FRAME_RED(indexLed)] = red;
_led[LED_FRAME_GREEN(indexLed)] = green;
_led[LED_FRAME_BLUE(indexLed)] = blue;
}
}

void Arduino_APA102::fill(uint32_t newColor , uint16_t startLed, uint16_t count ){
void Arduino_APA102::fill(uint32_t newColor, uint16_t startLed, uint16_t count){
for (int c = startLed; c < count; c++){
setPixelColor(c, newColor);
}
}

void Arduino_APA102::setBrightness(uint8_t newBrightness){ // Set a global Brightness
for (int i = 0; i< _numLeds;i++){
setBrightness(i , newBrightness);
// Set a global Brightness
void Arduino_APA102::setBrightness(uint8_t newBrightness){
for (int i = 0; i < _numLeds; i++){
setBrightness(i, newBrightness);
}
}

void Arduino_APA102::setBrightness(uint8_t indexLed, uint8_t newBrightness){ // Set individual LED brightness
newBrightness &= 0x64; //Max 100(dec)
_pixel[(indexLed* 5)+3] = newBrightness;

// Set individual LED brightness
void Arduino_APA102::setBrightness(uint8_t indexLed, uint8_t newBrightness){
_led[LED_FRAME_BRIGHTNESS(indexLed)] = newBrightness;
}

// 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)
void Arduino_APA102::setCurrent(uint8_t newCurrent){
for (int i = 0; i< _numLeds;i++){
setCurrent(i , newCurrent);
void Arduino_APA102::setCurrent(uint8_t newCurrent){
for (int i = 0; i < _numLeds; i++){
setCurrent(i, newCurrent);
}

}
// Set each LED's voltage
void Arduino_APA102::setCurrent(uint8_t indexLed, uint8_t newCurrent){
newCurrent &= 0x1F; //Max 31(dec)
_pixel[(indexLed* 5)+4] = newCurrent;
_led[(indexLed * 5) + 4] = newCurrent;
}

// Empty all he LEDs
// Default data
void Arduino_APA102::clear(){
memset(_pixel, 0, _numLeds);
memset(_led, 0, _numLeds);
for (int c = 0; c < _numLeds; c++){
_pixel[(_numLeds * c)] = 0;
_pixel[(_numLeds * c) +1] = 0;
_pixel[(_numLeds * c) +2] = 0;
_led[LED_FRAME_RED(c)] = 0;
_led[LED_FRAME_GREEN(c)] = 0;
_led[LED_FRAME_BLUE(c)] = 0;

_pixel[(_numLeds * c) +3] = 100; //Max bright 100%
_pixel[(_numLeds * c) +4] = 31; //Max Current, 0b11111
_led[LED_FRAME_BRIGHTNESS(c)] = 100; //Max bright 100%
_led[LED_FRAME_CURRENT(c)] = 31; //Max Current, 0b11111
}
}


//Protocol blocks

// Start frame block
void Arduino_APA102::_startFrame(){
for (int i = 0; i<4; i++){
for (int i = 0; i < 4; i++){
_write8(0x00);
}
}

// End frame block
void Arduino_APA102::_endFrame(){
for (int i = 0; i < ((_numLeds + 15) / 16); i++) { //Avoid to set the left LEDs white, taken from Adafruit_DotStar
_write8(0xFF); //
for (int i = 0; i < ((_numLeds + 15) / 16); i++){ //Avoid to set the left LEDs white, taken from Adafruit_DotStar
_write8(0xFF); //
}
}

// Send the data to the Line
void Arduino_APA102::_write8(uint8_t data){

// Convert the bit into a state H/L
for (uint8_t i = 8; i--; data <<= 1) {
for (uint8_t i = 8; i--; data <<= 1){

if (data & 0x80){
digitalWrite(_SDA_pin, HIGH);
}else{
digitalWrite(_SDA_pin, LOW);
}

// Pulse
digitalWrite(_SCK_pin, HIGH);
digitalWrite(_SCK_pin, LOW);
// Pulse
digitalWrite(_SCK_pin, HIGH);
digitalWrite(_SCK_pin, LOW);
}
}
19 changes: 8 additions & 11 deletions src/Arduino_APA102.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "Arduino.h"

class Arduino_APA102 {
class Arduino_APA102{
public:
Arduino_APA102(uint8_t numLEDs, uint8_t newDataPin, uint8_t newClockPin);
~Arduino_APA102();
Expand All @@ -33,19 +33,17 @@ class Arduino_APA102 {
void show(void);
void clear(void);


void setPixelColor(uint16_t indexLed, uint32_t newColor);
void setPixelColor(uint16_t indexLed, uint8_t red, uint8_t green, uint8_t blue);
void fill(uint32_t newColor , uint16_t startLed , uint16_t count );
void fill(uint32_t newColor, uint16_t startLed, uint16_t count);

void setBrightness(uint8_t newBrightness);
void setBrightness(uint8_t indexLed, uint8_t newBrightness);

void setCurrent(uint8_t newCurrent);
void setCurrent(uint8_t indexLed, uint8_t newCurrent);


uint32_t Color(uint8_t newRed, uint8_t newGreen, uint8_t newBlue) {

uint32_t Color(uint8_t newRed, uint8_t newGreen, uint8_t newBlue){
return (uint32_t)(newRed << 16 | newGreen << 8 | newBlue);
}

Expand All @@ -59,12 +57,11 @@ class Arduino_APA102 {
void _startFrame(void);
void _endFrame(void);

uint8_t _numLeds ;
uint8_t _SCK_pin ;
uint8_t _SDA_pin ;
uint8_t _numLeds;
uint8_t _SCK_pin;
uint8_t _SDA_pin;

uint8_t *_pixel; //5 bytes - R, G, B, Current, Btightness

uint8_t *_led; //5 bytes - R, G, B, Current, Btightnes
};

#endif