From d57eb7c94d35e7088aa5760e171615b5ee18873d Mon Sep 17 00:00:00 2001 From: HannesH_Dev <33062605+HannesGitH@users.noreply.github.com> Date: Tue, 15 Dec 2020 18:42:47 +0100 Subject: [PATCH 1/2] simplify bitwise operations removed some overhead in binary operations --- ChainableLED.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/ChainableLED.cpp b/ChainableLED.cpp index 39b6b02..7e5a3db 100644 --- a/ChainableLED.cpp +++ b/ChainableLED.cpp @@ -68,14 +68,11 @@ void ChainableLED::clk(void) void ChainableLED::sendByte(byte b) { - // Send one bit at a time, starting with the MSB - for (byte i=0; i<8; i++) + // Send one bit at a time, starting with the MSB (bit 7) + for (int i=7; i>=0; i--) { - // If MSB is 1, write one and clock it, else write 0 and clock - if ((b & 0x80) != 0) - digitalWrite(_data_pin, HIGH); - else - digitalWrite(_data_pin, LOW); + // send i-th bit and clock it + digitalWrite(_data_pin,(b>>i)&1) clk(); // Advance to the next bit to send @@ -86,13 +83,11 @@ void ChainableLED::sendByte(byte b) void ChainableLED::sendColor(byte red, byte green, byte blue) { // Start by sending a byte with the format "1 1 /B7 /B6 /G7 /G6 /R7 /R6" - byte prefix = 0b11000000; - if ((blue & 0x80) == 0) prefix|= 0b00100000; - if ((blue & 0x40) == 0) prefix|= 0b00010000; - if ((green & 0x80) == 0) prefix|= 0b00001000; - if ((green & 0x40) == 0) prefix|= 0b00000100; - if ((red & 0x80) == 0) prefix|= 0b00000010; - if ((red & 0x40) == 0) prefix|= 0b00000001; + byte prefix = 0xC0 //first to bits are set + | (( ~blue &0xC0)>>2) //flip 2 most significant bits of r and move to protocol specified location + | (( ~green &0xC0)>>4) //flip 2 most significant bits of g and move to protocol specified location + | (( ~red &0xC0)>>6) //flip 2 most significant bits of b and move to protocol specified location + sendByte(prefix); // Now must send the 3 colors From 76715cb9c992ac4569a4d8bec233207fa04d5434 Mon Sep 17 00:00:00 2001 From: HannesH_Dev <33062605+HannesGitH@users.noreply.github.com> Date: Sat, 27 Feb 2021 09:41:29 +0100 Subject: [PATCH 2/2] forgot two semicoli --- ChainableLED.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ChainableLED.cpp b/ChainableLED.cpp index 7e5a3db..02d29ae 100644 --- a/ChainableLED.cpp +++ b/ChainableLED.cpp @@ -72,7 +72,7 @@ void ChainableLED::sendByte(byte b) for (int i=7; i>=0; i--) { // send i-th bit and clock it - digitalWrite(_data_pin,(b>>i)&1) + digitalWrite(_data_pin,(b>>i)&1); clk(); // Advance to the next bit to send @@ -86,7 +86,7 @@ void ChainableLED::sendColor(byte red, byte green, byte blue) byte prefix = 0xC0 //first to bits are set | (( ~blue &0xC0)>>2) //flip 2 most significant bits of r and move to protocol specified location | (( ~green &0xC0)>>4) //flip 2 most significant bits of g and move to protocol specified location - | (( ~red &0xC0)>>6) //flip 2 most significant bits of b and move to protocol specified location + | (( ~red &0xC0)>>6); //flip 2 most significant bits of b and move to protocol specified location sendByte(prefix);