Skip to content

Arduino fade out Sketches #105

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 5 commits into
base: master
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
36 changes: 29 additions & 7 deletions KeyboardVisualizerCommon/LEDStrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,37 @@ void LEDStrip::InitializeHuePlus(char* ledstring)
strcpy(led_string, ledstring);

LPSTR source = NULL;
LPSTR channels = NULL;
LPSTR numleds = NULL;
LPSTR next = NULL;

source = strtok_s(ledstring, ",", &next);

//Check for selected channel 0=both, 1= Ch.1, 2= Ch.2
if (strlen(next))
{
channels = strtok_s(next, ",", &next);
}

switch (atoi(channels))
{
case 0:
channel = 0x00;
break;

case 1:
channel = 0x01;
break;

case 2:
channel = 0x02;
break;
}

//Check for the number of LEDs, sets the corresponding variable with the counter for the fans
if (strlen(next))
{
numleds = strtok_s(next, ",", &next);
numleds = strtok_s(next, ",", &next);
}

switch (atoi(numleds) / 8)
Expand Down Expand Up @@ -275,16 +297,16 @@ void LEDStrip::SetLEDsHuePlus(COLORREF pixels[64][256])
{
unsigned char *serial_buf;

serial_buf = new unsigned char[250]; //Size of Message always 5 XX Blocks (Mode Selection) + 3 XX for each LED (1 color)
//-> max of 40 LEDs per Channel (or 5 Fans a 8 LEDs) -> 125 Blocks (empty LEDs are written, too)
serial_buf = new unsigned char[hueSize]; //Size of Message always 5 XX Blocks (Mode Selection) + 3 XX for each LED (1 color)
//-> max of 40 LEDs per Channel (or 5 Fans a 8 LEDs) -> 125 Blocks (empty LEDs are written, too

serial_buf[0] = 0x4b;
serial_buf[1] = 0x00;
serial_buf[2] = 0x0e;
serial_buf[1] = channel;
serial_buf[2] = 0x0e;
serial_buf[3] = fans;
serial_buf[4] = 0x00;

for (int i = 5; i < 250; i++)
for (int i = 5; i < hueSize; i++)
{
//clearing the buf otherwise sometimes strange things are written to the COM Port
serial_buf[i] = 0x00;
Expand All @@ -299,7 +321,7 @@ void LEDStrip::SetLEDsHuePlus(COLORREF pixels[64][256])
serial_buf[idx + 7] = GetBValue(color);
}

serialport->serial_write((char *)serial_buf,250);
serialport->serial_write((char *)serial_buf, hueSize);
serialport->serial_flush_tx();

delete[] serial_buf;
Expand Down
2 changes: 2 additions & 0 deletions KeyboardVisualizerCommon/LEDStrip.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class LEDStrip
int baud_rate;
int num_leds;
int fans;
int channel;
const int hueSize = 125;

int * LEDStripXIndex;
int * LEDStripYIndex;
Expand Down
15 changes: 8 additions & 7 deletions KeyboardVisualizerQT/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,14 @@ bool parse_command_line(int argc, char *argv[])
printf(" - UDP : ledstrip=udp:client,port,num_leds\r\n");
printf(" - (ex.ledstrip=udp:192.168.1.5,1234,30)\r\n");
printf(" xmas - COM port, ex. xmas=COM2\r\n");
printf(" hueplus - HUE+ config:\r\n");
printf(" - hueplus=port,num_leds\r\n");
printf(" - num_leds: Fans * 8 ex. 3 Fans -> 24\r\n");
printf(" - Important for Fans: If you have connected fans on both channels only count the fans on the channel with the most fans\r\n");
printf(" ex. 3 Fans on Ch. 1 4 Fans on CH. 2: num_leds 32 for the 4 Fans\r\n");
printf(" For best Visualizer results don`t connect on one channel 3 fans more than on the other channel\r\n");
printf(" - (ex. hueplus=COM4,24\r\n");
printf(" hueplus - HUE+ config:\r\n");
printf(" - hueplus=port,channel,num_leds\r\n");
printf(" - channel: 0 -> both channels, 1 -> channel 1, 2 -> channel 2\r\n");
printf(" - num_leds: Fans * 8 ex. 3 Fans -> 24\r\n");
printf(" - Important for Fans: If you have connected fans on both channels only count the fans on the channel with the most fans\r\n");
printf(" ex. 3 Fans on Ch. 1 4 Fans on CH. 2: num_leds 32 for the 4 Fans\r\n");
printf(" For best Visualizer results don`t connect on one channel 3 fans more than on the other channel\r\n");
printf(" - (ex. hueplus=COM4,1,24\r\n");
return false;
}

Expand Down
15 changes: 8 additions & 7 deletions KeyboardVisualizerVC/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,14 @@ boolean parse_command_line(char * command_line)
printf(" - UDP : ledstrip=udp:client,port,num_leds\r\n");
printf(" - (ex.ledstrip=udp:192.168.1.5,1234,30)\r\n");
printf(" xmas - COM port, ex. xmas=COM2\r\n");
printf(" hueplus - HUE+ config:\r\n");
printf(" - hueplus=port,num_leds\r\n");
printf(" - num_leds: Fans * 8 ex. 3 Fans -> 24\r\n");
printf(" - Important for Fans: If you have connected fans on both channels only count the fans on the channel with the most fans\r\n");
printf(" ex. 3 Fans on Ch. 1 4 Fans on CH. 2: num_leds 32 for the 4 Fans\r\n");
printf(" For best Visualizer results don`t connect on one channel 3 fans more than on the other channel\r\n");
printf(" - (ex. hueplus=COM4,24\r\n");
printf(" hueplus - HUE+ config:\r\n");
printf(" - hueplus=port,channel,num_leds\r\n");
printf(" - channel: 0 -> both channels, 1 -> channel 1, 2 -> channel 2\r\n");
printf(" - num_leds: Fans * 8 ex. 3 Fans -> 24\r\n");
printf(" - Important for Fans: If you have connected fans on both channels only count the fans on the channel with the most fans\r\n");
printf(" ex. 3 Fans on Ch. 1 4 Fans on CH. 2: num_leds 32 for the 4 Fans\r\n");
printf(" For best Visualizer results don`t connect on one channel 3 fans more than on the other channel\r\n");
printf(" - (ex. hueplus=COM4,1,24\r\n");
return FALSE;
}

Expand Down
128 changes: 128 additions & 0 deletions WS2812B_LEDStrip_Arduino/WS2812B_LEDStrip_Arduino_Black.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*---------------------------------------------------------*\
| |
| WS2812B (NeoPixel) Serial Controlled LED Strip Driver |
| for use with Keyboard Visualizer VCUI |
| |
| Adam Honse (calcprogrammer1) 12/9/2016 |
| |
\*---------------------------------------------------------*/
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 10
#define LEDS 54
#define PACKET_SZ ( (LEDS * 3) + 3 )

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
unsigned char serial_buffer[PACKET_SZ];
unsigned int head = 0;
unsigned int start;
unsigned int checksum_1;
unsigned int checksum_0;
unsigned long previousMillis;

void setup()
{
Serial.begin(115200);
strip.begin();
strip.show();
previousMillis = millis();
}

void loop()
{
if( Serial.available())
{

serial_buffer[head] = Serial.read();

if( head >= (PACKET_SZ - 1) )
{
start = 0;
checksum_1 = head;
checksum_0 = head - 1;
head = 0;
}
else
{
start = head + 1;
checksum_1 = head;
if( head == 0 )
{
checksum_0 = PACKET_SZ - 1;
}
else
{
checksum_0 = head - 1;
}
head++;
}

if( serial_buffer[start] == 0xAA )
{
unsigned short sum = 0;

for( int i = 0; i < checksum_0; i++ )
{
sum += serial_buffer[i];
}

if( start > 0 )
{
for( int i = start; i < PACKET_SZ; i++ )
{
sum += serial_buffer[i];
}
}

//Test if valid write packet
if( ( ( (unsigned short)serial_buffer[checksum_0] << 8 ) | serial_buffer[checksum_1] ) == sum )
{
noInterrupts();
for( int i = 0; i < LEDS; i++ )
{
int idx = start + 1 + ( 3 * i );

if( idx >= (PACKET_SZ - 1) )
{
idx = idx - PACKET_SZ;
}

strip.setPixelColor(i, strip.Color(serial_buffer[idx], serial_buffer[idx+1], serial_buffer[idx+2]));

}
previousMillis = millis();
strip.show();
interrupts();

}
}
}
if ((millis() - previousMillis) > 1000)
{
for( int i = 0; i < LEDS; i++ )
{
strip.setPixelColor(i, strip.Color(0,0,0));
}
strip.show();
previousMillis=millis();
}
}




144 changes: 144 additions & 0 deletions WS2812B_LEDStrip_Arduino/WS2812B_LEDStrip_Arduino_FadeOut.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*---------------------------------------------------------*\
| |
| WS2812B (NeoPixel) Serial Controlled LED Strip Driver |
| for use with Keyboard Visualizer VCUI |
| |
| Adam Honse (calcprogrammer1) 12/9/2016 |
| |
\*---------------------------------------------------------*/
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 10
#define LEDS 54
#define PACKET_SZ ( (LEDS * 3) + 3 )

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
unsigned char serial_buffer[PACKET_SZ];
unsigned int head = 0;
unsigned int start;
unsigned int checksum_1;
unsigned int checksum_0;
unsigned long previousMillis;

void setup()
{
Serial.begin(115200);
strip.begin();
strip.show();
previousMillis = millis();
}

void loop()
{
if( Serial.available())
{

serial_buffer[head] = Serial.read();

if( head >= (PACKET_SZ - 1) )
{
start = 0;
checksum_1 = head;
checksum_0 = head - 1;
head = 0;
}
else
{
start = head + 1;
checksum_1 = head;
if( head == 0 )
{
checksum_0 = PACKET_SZ - 1;
}
else
{
checksum_0 = head - 1;
}
head++;
}

if( serial_buffer[start] == 0xAA )
{
unsigned short sum = 0;

for( int i = 0; i < checksum_0; i++ )
{
sum += serial_buffer[i];
}

if( start > 0 )
{
for( int i = start; i < PACKET_SZ; i++ )
{
sum += serial_buffer[i];
}
}

//Test if valid write packet
if( ( ( (unsigned short)serial_buffer[checksum_0] << 8 ) | serial_buffer[checksum_1] ) == sum )
{
noInterrupts();
for( int i = 0; i < LEDS; i++ )
{
int idx = start + 1 + ( 3 * i );

if( idx >= (PACKET_SZ - 1) )
{
idx = idx - PACKET_SZ;
}

strip.setPixelColor(i, strip.Color(serial_buffer[idx], serial_buffer[idx+1], serial_buffer[idx+2]));

}
previousMillis = millis();
strip.show();
interrupts();

}
}
}
if ((millis() - previousMillis) > 1000)
{
bool black = false;
uint32_t colorBlack = strip.Color(0,0,0);
while (!black)
{
for( int i = 0; i < LEDS; i++ )
{
if ( colorBlack != strip.getPixelColor(i))
{
uint32_t c = strip.getPixelColor(i);
int r = (uint8_t)(c >> 16);
int g = (uint8_t)(c >> 8);
int b = (uint8_t)c;
strip.setPixelColor(i, strip.Color(r*0.8,g*0.8,b*0.8));
}
else
{
black = true;
}
}
strip.show();
}
previousMillis=millis();
}
}




Loading