Commits

Trammell Hudson  committed 41138f0

Smooth color changes

  • Participants
  • Parent commits 69f1d6a

Comments (0)

Files changed (1)

 }
 
 
+static const uint8_t colors[][3] =
+{
+	{ 0x00, 0x00, 0xFF },	// blue
+	{ 0xFF, 0xFF, 0xFF },	// white
+	{ 0xFF, 0xFF, 0x00 },   // yellow
+	{ 0xFF, 0x00, 0x00 },	// red
+	{ 0x00, 0x80, 0xFF },	// cyan
+	{ 0x00, 0xFF, 0x00 },	// green
+	{ 0xFF, 0xFF, 0xFF },	// white
+};
+
+static const uint8_t num_colors = sizeof(colors) / sizeof(*colors);
+
+
 int main(void)
 {
 	// set for 16 MHz clock, and make sure the LED is off
 		pixels[i][0] = pixels[i][1] = pixels[i][2] = 0xFF;
 	send_pixels();
 
-	uint8_t x = 0;
-	int dir = 1;
+
+	uint8_t cycle = 0;
+	const uint16_t iter_count = 256;
 
 	while (1)
 	{
-		memset(pixels, x, sizeof(pixels));
-		if (dir)
+		const uint8_t * const c0 = colors[(cycle + 0) % num_colors];
+		const uint8_t * const c1 = colors[(cycle + 1) % num_colors];
+		//const uint8_t * const c2 = colors[(cycle + 2) % num_colors];
+
+		for (uint16_t iter = 0 ; iter < iter_count ; iter++)
 		{
-			x++;
-			if (x == 0xFF)
-				dir = 0;
-		} else {
-			x--;
-			if (x == 0x10)
-				dir = 1;
+			for (i = 0 ; i < NUM_LEDS ; i++)
+			{
+				uint8_t * const p = pixels[i];
+				const uint16_t s0 = iter_count - iter;
+				const uint16_t s1 = iter;
+				p[0] = (c0[0] * s0 + c1[0] * s1) / iter_count;
+				p[1] = (c0[1] * s0 + c1[1] * s1) / iter_count;
+				p[2] = (c0[2] * s0 + c1[2] * s1) / iter_count;
+			}
+
+			send_pixels();
 		}
 
-		send_pixels();
-		fade_pixels();
-	}
-
-
-	while (1)
-	{
-		send_pixels();
-
-		// Smoothly fade the colors
-		uint8_t * const e1 = pixels[0];
-		uint8_t * const e2 = pixels[NUM_LEDS-1];
-
-		e2[0] = 0xFF;
-		e2[1] = 0xFF;
-		e2[2] = 0xFF;
-
-		e1[0] = 0x00;
-		e1[1] = 0x00;
-		e1[2] = 0x00;
-
-		for (uint16_t i = 1 ; i < NUM_LEDS-1 ; i++)
-		{
-			uint8_t * const p = pixels[i];
-			for (uint8_t j = 0 ; j < 3 ; j++)
-			{
-				uint16_t x = e1[j] * i + e2[j] * (NUM_LEDS-i);
-				x /= NUM_LEDS;
-				p[j] = x;
-			}
-		}
-
-		_delay_ms(30);
+		cycle++;
+		_delay_ms(10);
 	}
 }