led-strip / led-strip.c

  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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* 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 <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include "usb_debug_only.h"
#include "print.h"

#include "font-small.c"


// 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 ((int16_t) (32 * 5))
#define LED_SCALE 8

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();
}


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 | (g >> 1));
		send_byte(0x80 | (r >> 1));
		send_byte(0x80 | (b >> 1));
	}

	latch();
}


static volatile uint8_t do_change;
static volatile uint8_t pattern;


static void fade_pixels(
	uint8_t r,
	uint8_t g,
	uint8_t b
)
{
	uint16_t i;
	for (i = 0 ; i < NUM_LEDS ; i++)
	{
		uint8_t * const p = pixels[i];

		p[0] = (p[0] * (uint16_t) 31 + r) / 32;
		p[1] = (p[1] * (uint16_t) 31 + g) / 32;
		p[2] = (p[2] * (uint16_t) 31 + b) / 32;
	}
}


/******************************
 * Smooth fading between colors.
 */

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);


static void pattern_smooth(void)
{
	uint8_t cycle = 0;
	const uint16_t iter_count = 256;

	// total duration: 32 * 256 * 500 ms == 

	for (cycle = 0 ; cycle < 32 ; cycle++)
	{
		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++)
		{
			uint16_t i;
			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();
			_delay_ms(500);

			if (do_change)
				return;
		}
	}
}



/***************************
 * Chase pattern.
 */
struct sprite
{
	int16_t pos;
	int16_t v;
	uint8_t r;
	uint8_t b;
	uint8_t g;
};

static struct sprite sprites[] = {
	{ .v = +1, .r = 0xFF, .b = 0x00, .g = 0x00 },
	{ .v = +2, .r = 0x00, .b = 0xFF, .g = 0x00, .pos = NUM_LEDS*LED_SCALE/2 },
	{ .v = +3, .r = 0xFF, .b = 0xFF, .g = 0xFF },
	{ .v = +5, .r = 0x00, .b = 0xFF, .g = 0xFF },
	{ .v = +7, .r = 0x80, .b = 0x80, .g = 0x10 },
	{ .v = +11, .r = 0x00, .b = 0x00, .g = 0xFF },
	{ .v = +13, .r = 0xFF, .b = 0x00, .g = 0xFF },
};

static const int num_sprites = sizeof(sprites) / sizeof(*sprites);


static void pattern_chase(void)
{
	uint16_t cycle;
	for (cycle = 0 ; cycle < 512 ; cycle++)
	{
		// Compute the next position
		for (int i = 0 ; i < num_sprites ; i++)
		{
			struct sprite * const s = &sprites[i];
			int16_t pos = s->pos + s->v;

			if (pos >= NUM_LEDS * LED_SCALE)
			{
				pos = NUM_LEDS * LED_SCALE - 1;
				s->v = -s->v;
			} else
			if (pos < 0) {
				pos = 0;
				s->v = -s->v;
			}
				
			s->pos = pos;

			uint8_t * const p = pixels[pos / LED_SCALE];
			p[0] = (s->r * (uint16_t) 3 + p[0]) / 4;
			p[1] = (s->g * (uint16_t) 3 + p[1]) / 4;
			p[2] = (s->b * (uint16_t) 3 + p[2]) / 4;

			if (do_change)
				return;
		}

		send_pixels();
		fade_pixels(0, 0, 0);
		_delay_ms(30);
	}
}


/***********************
 * Decay to blue
 */
static void pattern_decay(void)
{
	uint8_t cycle;
	const uint8_t * const c = colors[0];

	for (cycle = 0 ; cycle < 32 ; cycle++)
	{
		fade_pixels(c[0], c[1], c[2]);
		send_pixels();
		_delay_ms(30);
	}
}


/******************
 * Attention getting flash.
 */
static void pattern_flash(void)
{
	uint8_t i;
	for (i = 0 ; i < 8 ; i++)
	{
		memset(pixels, 0, sizeof(pixels));
		send_pixels();
		_delay_ms(250);

		memset(pixels, 0xFF, sizeof(pixels));
		send_pixels();
		_delay_ms(250);
	};
}


/** Completely off */
static void pattern_off(void)
{
	memset(pixels, 0, sizeof(pixels));
	send_pixels();

	while (!do_change)
		;
}


/** Solid color */
static uint8_t solid[3];

static void pattern_solid(void)
{
	while (!do_change)
	{
		fade_pixels(solid[0], solid[1], solid[2]);
		send_pixels();
		_delay_ms(30);
	}
}


typedef void (*pattern_t)(void);

static const pattern_t patterns[] = {
	pattern_flash,
	pattern_decay,
	pattern_chase,
	pattern_decay,
	pattern_smooth,
	pattern_off,
	pattern_solid,
};

static const uint8_t num_patterns = sizeof(patterns) / sizeof(*patterns);

static inline __attribute__((noreturn)) void reset(void)
{
	cli();
	// disable watchdog, if enabled
	// disable all peripherals
	UDCON = 1;
	USBCON = (1<<FRZCLK);  // disable USB
	UCSR1B = 0;
	_delay_ms(5);

#if defined(__AVR_AT90USB162__)                // Teensy 1.0
	EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
	TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
	DDRB = 0; DDRC = 0; DDRD = 0;
	PORTB = 0; PORTC = 0; PORTD = 0;
	asm volatile("jmp 0x3E00");
#elif defined(__AVR_ATmega32U4__)              // Teensy 2.0
	EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
	TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
	DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
	PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
	asm volatile("jmp 0x7E00");
#elif defined(__AVR_AT90USB646__)              // Teensy++ 1.0
	EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
	TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
	DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
	PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
	asm volatile("jmp 0xFC00");
#elif defined(__AVR_AT90USB1286__)             // Teensy++ 2.0
	EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
	TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
	DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
	PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
	asm volatile("jmp 0x1FC00");
#endif 

	while(1)
		continue;
}


static void
command_process(
	const char * const line
)
{
	const char c = line[0];
	const char a = line[1];
	print("read: ");
	pchar(c);
	pchar(a);
	print("\n");

	if (c == 'M')
	{
		if (a < '0' || '0' + num_patterns < a)
			return;

		pattern = a - '0';
		do_change = 1;

		return;
	}

	if (c == 'C')
	{
		solid[0] = (line[1] - '0') << 5;
		solid[1] = (line[2] - '0') << 5;
		solid[2] = (line[3] - '0') << 5;
		pattern = 6;
		do_change = 1;
		return;
	}

	if (c == 'R')
	{
		print("RESET!\n");
		reset();
	}

	print("?\n");
}



ISR(USART1_RX_vect)
{
	LED_ON;
	const uint8_t c = UDR1;
	static char line[32];
	static uint8_t pos;

	line[pos++] = c;
	if (c == '\n' || pos == sizeof(line) - 1)
	{
		line[pos] = '\0';
		command_process(line);
		pos = 0;
	}

	LED_OFF;
}


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();

	// Enable the serial port interrupts
        // Set the port to 9600 K-baud, 1 start, 8 data, 1 parity
        UBRR1H = 0;
        UBRR1L = 103;
        UCSR1B = (1 << RXEN1) | (1 << RXCIE1);
        UCSR1C = (0 << UCSZ12) | (1 << UCSZ11) | (1 << UCSZ10);


	LED_ON;
	print("started up\n");

	DDRB |= 1 << 2;
	DDRB |= 1 << 1;

	latch();

	uint16_t i;
	for (i = 0 ; i < NUM_LEDS ; i++)
		pixels[i][0] = pixels[i][1] = pixels[i][2] = 0xFF;
	send_pixels();

	pattern = 0;

	while (1)
	{
		patterns[pattern]();

		if (do_change)
		{
			do_change = 0;
			continue;
		}

		// Do not select the 0th or last few patterns
		uint8_t next_pattern = pattern + 1;
		if (next_pattern >= num_patterns - 2)
			next_pattern = 1;
		pattern = next_pattern;
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.