Commits

Trammell Hudson committed 7ed225b

Draw multiple colored sprites

Comments (0)

Files changed (1)

 #define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))
 
 
-#define NUM_LEDS ((uint16_t) (32 * 5))
+#define NUM_LEDS ((int16_t) (32 * 5))
 
 static inline void clock(const uint8_t value)
 {
 		send_zero();
 }
 
+static uint8_t pixels[NUM_LEDS][3];
+
+static void send_pixels(void)
+{
+	uint16_t i;
+	for (i = 0 ; i < NUM_LEDS ; i++)
+	{
+		uint8_t * const p = pixels[i];
+		uint8_t r = p[0];
+		uint8_t g = p[1];
+		uint8_t b = p[2];
+
+		send_byte(0x80 | (r >> 1));
+		send_byte(0x80 | (g >> 1));
+		send_byte(0x80 | (b >> 1));
+	}
+
+	latch();
+}
+
+static void fade_pixels(void)
+{
+	uint16_t i;
+	for (i = 0 ; i < NUM_LEDS ; i++)
+	{
+		uint8_t * const p = pixels[i];
+		p[0] >>= 1;
+		p[1] >>= 1;
+		p[2] >>= 1;
+	}
+}
+
+struct sprite
+{
+	int16_t pos;
+	int16_t v;
+	uint8_t r;
+	uint8_t b;
+	uint8_t g;
+};
+
 
 
 int main(void)
 	latch();
 
 	uint16_t i;
-	uint8_t pixels[NUM_LEDS][3];
 	for (i = 0 ; i < NUM_LEDS ; i++)
-		pixels[i][0] = pixels[i][1] = pixels[i][2] = 0;
-	uint16_t iter = 0;
-	uint8_t round = 0;
+		pixels[i][0] = pixels[i][1] = pixels[i][2] = 0xFF;
+	send_pixels();
+	//while(1);
+
+	struct sprite sprites[] = {
+		{ .v = +1, .r = 0xFF, .b = 0x00, .g = 0x00 },
+		{ .v = -1, .r = 0x00, .b = 0xFF, .g = 0x00 },
+		{ .v = +3, .r = 0xFF, .b = 0xFF, .g = 0xFF },
+		{ .v = +4, .r = 0x00, .b = 0xFF, .g = 0xFF },
+		{ .v = -2, .r = 0x00, .b = 0x00, .g = 0xFF, .pos = NUM_LEDS/2 },
+		{ .v = +2, .r = 0xFF, .b = 0x00, .g = 0xFF, .pos = NUM_LEDS/2 },
+	};
+	const int num_sprites = sizeof(sprites) / sizeof(*sprites);
 
 	while (1)
 	{
-		for (i = 0 ; i < NUM_LEDS ; i++)
+		send_pixels();
+		fade_pixels();
+
+		// Compute the next position
+		for (int i = 0 ; i < num_sprites ; i++)
 		{
-			//uint8_t r = pixels[i][0];
-			//uint8_t g = pixels[i][1];
-			//uint8_t b = pixels[i][2];
-			uint8_t r = i == iter && round & 4 ? 0xFF : 0;
-			uint8_t g = i == iter && round & 2 ? 0xFF : 0;
-			uint8_t b = i == iter && round & 1 ? 0xFF : 0;
+			struct sprite * const s = &sprites[i];
+			int16_t pos = s->pos + s->v;
+
+			if (pos >= NUM_LEDS)
+			{
+				pos = NUM_LEDS - 1;
+				s->v = -s->v;
+			} else
+			if (pos < 0) {
+				pos = 0;
+				s->v = -s->v;
+			}
 				
-			send_byte(0x80 | (r >> 1));
-			send_byte(0x80 | (g >> 1));
-			send_byte(0x80 | (b >> 1));
+			s->pos = pos;
+
+			uint8_t * const p = pixels[pos];
+			p[0] = s->r;
+			p[1] = s->g;
+			p[2] = s->b;
 		}
 
-		latch();
-
-		if (++iter == NUM_LEDS)
-		{
-			round++;
-			iter = 0;
-		}
-
-		for (i = 0 ; i < NUM_LEDS ; i++)
-		{
-			pixels[i][0]++;
-			pixels[i][1]++;
-			pixels[i][2]++;
-		}
-
-		for (i = 0 ; i < 32000 ; i++)
-			asm("nop");
+		_delay_ms(30);
 	}
 }