I made my own controller!
This commit is contained in:
+77
-38
@@ -1,42 +1,73 @@
|
||||
// Mr. Christmas powerline RGB protocol
|
||||
// 7-bit command: PPP M BGR (MSB first)
|
||||
//
|
||||
// Idle HIGH = string powered; brief LOW pulses encode bits.
|
||||
// Short pause ~= 0, long pause ~= 1.
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
const int command_pin = D1; // MOSFET gate
|
||||
#else
|
||||
const int command_pin = 8;
|
||||
#endif
|
||||
|
||||
const int PULSE_DURATION = 100; // microseconds
|
||||
const int SHORT_PAUSE = 4964; // bit 0
|
||||
const int LONG_PAUSE = 16992; // bit 1
|
||||
|
||||
// 3bits: PPP
|
||||
enum Pattern {
|
||||
STEADY = 0,
|
||||
FADE = 0x10,
|
||||
SPARKLE = 0x20,
|
||||
FIREFLY= 0x30
|
||||
STEADY = 0, // 000
|
||||
FADE = 1, // 001
|
||||
SPARKLE = 2, // 010
|
||||
FIREFLY = 3, // 011
|
||||
TWINKLE = 4, // 100 - dark + colored flashes
|
||||
FAST_SPARKLE = 5, // 101
|
||||
ANTI_SPARKLE = 6, // 110 - black sparkles
|
||||
FAST_TWINKLE = 7, // 111
|
||||
};
|
||||
|
||||
// 4bits: MBGR
|
||||
// M=0: BGR (solid colors)
|
||||
// M=1: palette
|
||||
enum Color {
|
||||
OFF,
|
||||
RED,
|
||||
GREEN,
|
||||
YELLOW,
|
||||
BLUE,
|
||||
MAGENTA,
|
||||
CYAN,
|
||||
WHITE,
|
||||
MULTICOLOR
|
||||
COLOR_OFF = 0, // 0000
|
||||
RED = 1, // 0001
|
||||
GREEN = 2, // 0010
|
||||
YELLOW = 3, // 0011
|
||||
BLUE = 4, // 0100
|
||||
MAGENTA = 5, // 0101
|
||||
CYAN = 6, // 0110
|
||||
WHITE = 7, // 0111
|
||||
MULTI = 8, // 1000 - multi-color
|
||||
CHRISTMAS = 9, // 1001 - red/white/green
|
||||
JULY4 = 10, // 1010 - red/white/blue
|
||||
};
|
||||
|
||||
Pattern patterns[] = {STEADY, FADE, SPARKLE, FIREFLY};
|
||||
Color colors[] = {RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, MULTICOLOR, OFF};
|
||||
Pattern patterns[] = {
|
||||
STEADY, FADE, SPARKLE, FIREFLY, TWINKLE, FAST_SPARKLE, ANTI_SPARKLE, FAST_TWINKLE,
|
||||
};
|
||||
const char *pattern_names[] = {
|
||||
"Steady", "Fade", "Sparkle", "Firefly", "Twinkle", "FastSparkle", "AntiSparkle", "FastTwinkle",
|
||||
};
|
||||
|
||||
int PULSE_DURATION = 100; // microseconds
|
||||
int SHORT_PAUSE = 4964; // microseconds
|
||||
int LONG_PAUSE = 16992; // miccroseconds
|
||||
|
||||
// output pin to send the signals to
|
||||
int command_pin = 8;
|
||||
Color colors[] = {
|
||||
COLOR_OFF, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, MULTI, CHRISTMAS, JULY4,
|
||||
};
|
||||
const char *color_names[] = {
|
||||
"Off", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White", "Multi", "Christmas", "July4",
|
||||
};
|
||||
|
||||
/**
|
||||
* Send a single short pulse of "off".
|
||||
*/
|
||||
void send_pulse() {
|
||||
noInterrupts();
|
||||
digitalWrite(command_pin, LOW);
|
||||
delayMicroseconds(PULSE_DURATION);
|
||||
digitalWrite(command_pin, HIGH);
|
||||
interrupts();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send a single bit.
|
||||
*/
|
||||
@@ -45,45 +76,53 @@ void send_bit(bool b) {
|
||||
delayMicroseconds(b ? LONG_PAUSE : SHORT_PAUSE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send a whole command. Commands start with a 0 bit then six bits of data.
|
||||
* Send a whole 7-bit command (PPP M BGR), then an end pulse.
|
||||
*/
|
||||
void send_command(int cmd) {
|
||||
// start command
|
||||
send_bit(0);
|
||||
|
||||
for (int i = 0x20; i > 0; i >>= 1) {
|
||||
send_bit(cmd & i);
|
||||
for (int mask = 0x40; mask > 0; mask >>= 1) {
|
||||
send_bit(cmd & mask);
|
||||
}
|
||||
|
||||
// end command
|
||||
send_pulse();
|
||||
|
||||
// minimum time before sending another command
|
||||
delayMicroseconds(LONG_PAUSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a whole command based on a pattern and color.
|
||||
* Send a whole command based on a pattern and color/palette.
|
||||
*/
|
||||
void send_command(Pattern pattern, Color color) {
|
||||
send_command(pattern | color);
|
||||
send_command((pattern << 4) | color);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(command_pin, OUTPUT);
|
||||
digitalWrite(command_pin, HIGH);
|
||||
Serial.begin(9600);
|
||||
delay(500);
|
||||
Serial.println();
|
||||
Serial.println("Full demo");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(1000);
|
||||
// Demo: each pattern x colors/palettes.
|
||||
for (unsigned p = 0; p < sizeof(patterns) / sizeof(patterns[0]); p++) {
|
||||
for (unsigned c = 0; c < sizeof(colors) / sizeof(colors[0]); c++) {
|
||||
if (colors[c] == COLOR_OFF && patterns[p] != FAST_SPARKLE) {
|
||||
// Off is only meaningful with FastSparkle (dark + fast white sparkles).
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sample "demo" mode where it cycles between all the colors of all the patterns
|
||||
for (int p = 0; p < sizeof(patterns) / sizeof(Pattern); p++) {
|
||||
for (int c = 0; c < sizeof(colors) / sizeof(Color); c++) {
|
||||
send_command(patterns[p], colors[c]);
|
||||
Serial.print(pattern_names[p]);
|
||||
Serial.print(" + ");
|
||||
Serial.println(color_names[c]);
|
||||
delay(10000);
|
||||
}
|
||||
}
|
||||
|
||||
send_command(STEADY, COLOR_OFF);
|
||||
Serial.println("Off");
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user