Commits

Trammell Hudson committed cd6ce7c

led strip works

Comments (0)

Files changed (2)

 
 
 # Target file name (without extension).
-TARGET = blinky
+TARGET = led-strip
 
 
 # List C source files here. (C dependencies are automatically generated.)
+/* LED strip driver
+ *
+ * Derived from:
+ * http://www.pjrc.com/teensy/
+ * Copyright (c) 2008, 2010 PJRC.COM, LLC
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <util/delay.h>
+#include "usb_debug_only.h"
+#include "print.h"
+
+
+// Teensy 2.0: LED is active high
+#if defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__)
+#define LED_ON		(PORTD |= (1<<6))
+#define LED_OFF		(PORTD &= ~(1<<6))
+
+// Teensy 1.0: LED is active low
+#else
+#define LED_ON	(PORTD &= ~(1<<6))
+#define LED_OFF	(PORTD |= (1<<6))
+#endif
+
+#define LED_CONFIG	(DDRD |= (1<<6))
+#define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))
+
+
+#define NUM_LEDS ((uint16_t) (32 * 5))
+
+static inline void clock(const uint8_t value)
+{
+	if (value)
+		PORTB |= (1 << 1);
+	else
+		PORTB &= ~(1 << 1);
+}
+
+
+static void send_zero(void)
+{
+	unsigned i;
+	PORTB &= ~(1 << 2);
+
+	for (i = 0 ; i < 8 ; i++)
+	{
+		clock(1);
+		clock(0);
+	}
+}
+
+
+static void send_byte(uint8_t v)
+{
+	unsigned bit;
+	for (bit = 0x80 ; bit ; bit >>= 1)
+	{
+		clock(0);
+		if (v & bit)
+			PORTB |=  (1 << 2);
+		else
+			PORTB &= ~(1 << 2);
+		clock(1);
+	}
+}
+
+
+static void latch(void)
+{
+	unsigned i;
+	const unsigned zeros = 3 * ((NUM_LEDS + 63) / 64);
+	for (i = 0 ; i < zeros ; i++)
+		send_zero();
+}
+
+
+int main(void)
+{
+	// set for 16 MHz clock, and make sure the LED is off
+	CPU_PRESCALE(0);
+	LED_CONFIG;
+	LED_OFF;
+
+	// initialize the USB, but don't want for the host to
+	// configure.  The first several messages sent will be
+	// lost because the PC hasn't configured the USB yet,
+	// but we care more about blinking than debug messages!
+	usb_init();
+
+	LED_ON;
+
+	DDRB |= 1 << 2;
+	DDRB |= 1 << 1;
+
+	latch();
+
+	uint16_t i;
+	//uint32_t pixels[NUM_LEDS];
+	uint16_t iter = 0;
+
+	while (1)
+	{
+		for (i = 0 ; i < NUM_LEDS ; i++)
+		{
+			//uint32_t c = pixels[i];
+			uint32_t c = i == iter ? 0xFFFFFF : 0;
+
+			uint8_t r = c >> 16;
+			uint8_t g = c >>  8;
+			uint8_t b = c >>  0;
+			send_byte(0x80 | (r >> 1));
+			send_byte(0x80 | (g >> 1));
+			send_byte(0x80 | (b >> 1));
+		}
+
+		if (++iter == NUM_LEDS)
+			iter = 0;
+
+		latch();
+	}
+}