secret
Last active

Renard To GECE

  • Download Gist
renard_to_gece.pde
Processing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
#define XMAS_PIN 8
#define xmas_color_t uint16_t
 
#define XMAS_LIGHT_COUNT (50)
 
#define XMAS_CHANNEL_MAX (0xF)
#define XMAS_DEFAULT_INTENSITY (0xCC)
#define XMAS_COLOR(r,g,b) ((r)+((g)<<4)+((b)<<8))
#define XMAS_COLOR_WHITE XMAS_COLOR(XMAS_CHANNEL_MAX,XMAS_CHANNEL_MAX,XMAS_CHANNEL_MAX)
#define XMAS_COLOR_BLACK XMAS_COLOR(0,0,0)
 
bool sync;
 
void xmas_begin(uint8_t pin);
void xmas_one(uint8_t pin);
void xmas_zero(uint8_t pin);
void xmas_end(uint8_t pin);
 
void xmas_begin(uint8_t pin)
{
digitalWrite(pin,1);
delayMicroseconds(7);
digitalWrite(pin,0);
}
 
void xmas_one(uint8_t pin)
{
digitalWrite(pin,0);
delayMicroseconds(11); //This results in a 20 uS long low
digitalWrite(pin,1);
delayMicroseconds(7);
digitalWrite(pin,0);
}
 
void xmas_zero(uint8_t pin)
{
digitalWrite(pin,0);
delayMicroseconds(2);
digitalWrite(pin,1);
delayMicroseconds(20-3);
digitalWrite(pin,0);
}
 
void xmas_end(uint8_t pin)
{
digitalWrite(pin,0);
delayMicroseconds(40); // Can be made shorter
}
 
 
void xmas_set_color(uint8_t led,uint8_t intensity,xmas_color_t color, uint8_t pin)
{
uint8_t i;
 
xmas_begin(pin);
 
//6-Bit bulb address (MSB first)
for(i=6;i;i--,(led<<=1))
{
if(led&(1<<5))
xmas_one(pin);
else
xmas_zero(pin);
}
 
//8-Bit brightness (MSB first)
for(i=8;i;i--,(intensity<<=1))
{
if(intensity&(1<<7))
xmas_one(pin);
else
xmas_zero(pin);
}
 
//12 bit Color (Blue, Green, Red) (MSB first)
for(i=12;i;i--,(color<<=1))
{
if(color&(1<<11))
xmas_one(pin);
else
xmas_zero(pin);
}
 
xmas_end(pin);
}
 
void xmas_set_all_one_address(uint8_t pin)
{
for (int i = 0; i < XMAS_LIGHT_COUNT; ++i)
xmas_set_color(0,XMAS_DEFAULT_INTENSITY,XMAS_COLOR_BLACK,pin);
}
 
void setup()
{
delay(10);
Serial.begin(57600);
delay(10);
 
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
 
pinMode(XMAS_PIN, OUTPUT);
digitalWrite(XMAS_PIN, 0);
xmas_set_all_one_address(XMAS_PIN);
 
sync = false;
}
 
void wait_for_serial()
{
while ( ! Serial.available() > 0 ) { }
}
 
int renardReadBytes( uint8_t *bytes, uint8_t bytes_size )
{
int in_byte = 0;
int bytes_read;
 
for ( bytes_read = 0; bytes_read < bytes_size; )
{
wait_for_serial();
in_byte = Serial.read();
 
switch (in_byte)
{
case(0x7E): // We saw the sync byte, start over!
sync = true;
return bytes_read;
 
case(0x7D): // Skip the pad byte
continue;
 
case(0x7F): // Escape character, we need to read one more byte to get our actual data
wait_for_serial();
in_byte = Serial.read();
switch (in_byte)
{
case(0x2F): // renard wants an 0x7D
in_byte = 0x7D;
case(0x30): // renard wants an 0x7E
in_byte = 0x7E;
case(0x31): // renard wants an 0x7F
in_byte = 0x7F;
}
}
 
bytes[bytes_read++] = in_byte;
}
 
return bytes_read;
}
 
int renardRead( uint8_t *bytes, uint8_t byte_count )
{
int in_byte = 0;
 
while ( ! sync )
{
wait_for_serial();
in_byte = Serial.read();
if ( in_byte == 0x7E ) // Sync byte signifies start of packet
sync = true;
}
 
if ( sync )
{
sync = false;
wait_for_serial();
in_byte = Serial.read();
if ( in_byte == 0x80 ) // Read from here
{
return renardReadBytes(bytes, byte_count);
}
}
 
return 0;
}
 
void loop()
{
uint8_t bytes[4], bytes_read;
bytes_read = renardRead(&bytes[0], 4);
 
if ( bytes_read == 4 )
{
// We have now received 4 bytes of data per string, do something with them!
uint8_t red = bytes[0];
uint8_t green = bytes[1];
uint8_t blue = bytes[2];
uint8_t white = bytes[3];
 
 
red = ( red+(white/3) >= (XMAS_CHANNEL_MAX<<4) ? (XMAS_CHANNEL_MAX<<4) : red+(white/3) );
green = ( green+(white/3) >= (XMAS_CHANNEL_MAX<<4) ? (XMAS_CHANNEL_MAX<<4) : green+(white/3) );
blue = ( blue+(white/3) >= (XMAS_CHANNEL_MAX<<4) ? (XMAS_CHANNEL_MAX<<4) : blue+(white/3) );
 
xmas_set_color(0,XMAS_DEFAULT_INTENSITY,XMAS_COLOR((red>>4),(green>>4),(blue>>4)),XMAS_PIN);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.