From: Jan "Yenya" Kasprzak Date: Sun, 22 Dec 2013 11:32:24 +0000 (+0100) Subject: rgb-led-string: single-direction white stars X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=946b14619a2e67ddf5b8c06e5d41c812b221083a;p=tinyboard.git rgb-led-string: single-direction white stars --- diff --git a/projects/rgb-led-string/Makefile b/projects/rgb-led-string/Makefile index 2fa9344..4a18af0 100644 --- a/projects/rgb-led-string/Makefile +++ b/projects/rgb-led-string/Makefile @@ -1,6 +1,6 @@ PROGRAM=rgbstring -SRC=version.c main.c logging.c +SRC=version.c main.c logging.c serial.c OBJ=$(SRC:.c=.o) @@ -8,7 +8,7 @@ MCU=attiny45 AVRDUDE_MCU=$(MCU) AVRDUDE_PROGRAMMER=usbasp -CFLAGS=-Wall -Os -mmcu=$(MCU) -DUSE_LOGGING=1 -DF_CPU=1000000UL -std=gnu99 +CFLAGS=-Wall -Os -mmcu=$(MCU) -DUSE_LOGGING=1 -DF_CPU=8000000UL -std=gnu99 LDFLAGS= AVRDUDE_FLAGS= -p$(AVRDUDE_MCU) -c $(AVRDUDE_PROGRAMMER) diff --git a/projects/rgb-led-string/main.c b/projects/rgb-led-string/main.c index 4c82f48..dc96861 100644 --- a/projects/rgb-led-string/main.c +++ b/projects/rgb-led-string/main.c @@ -4,17 +4,86 @@ #include "rgbstring.h" +unsigned char jiffies; + +typedef struct { + unsigned char type : 4; + unsigned char order : 4; +} pixel_t; + +pixel_t pixels[STRIP_SIZE]; + + int main(void) { + unsigned char i, start, rgb, jiffies; init_log(); + init_serial(); + + _delay_ms(3000/8); + CLKPR = _BV(CLKPCE); + CLKPR = 0; - log_set_state(3); + for (i = 0; i < STRIP_SIZE; i+= 23) + pixels[i].type = 2; - DDRB |= _BV(PB2); while (1) { - PORTB |= _BV( PB2 ); - _delay_ms(200); - PORTB &=~ _BV( PB2 ); - _delay_ms(200); + jiffies++; + if ((jiffies & 7) == 0) { + pixels[start].type = 1; + pixels[start].order = 14; + } + + if ((jiffies & 7) == 3) { + pixels[start].type = 2; + pixels[start].order = 0; + start += 19; + } + + start += 63; + if (start >= STRIP_SIZE) + start -= STRIP_SIZE; + + for (i = 0; i < STRIP_SIZE; i++) { + unsigned char type = pixels[i].type; + unsigned char order = pixels[i].order; + + switch (type) { + case 0: + send_rgb(0, 0, 4); + break; + case 1: + case 3: + send_rgb( + 6+(1 << (order/2)), + 6+(1 << (order/2)), + 6+(1 << (order/2)) + ); + pixels[i].type = type == 3 ? 2 : 0;; + if (order > 1 && i) { + pixels[i-1].type = pixels[i-1].type == 2 ? 3 : 1; + pixels[i-1].order = order-1; + } + break; + case 2: + if (order >= 8) { + send_rgb(1 << ((15-order) / 2), 0, 0); + } else { + send_rgb(1 << (order/2), 0, 0); + } + if (++order >= 15) + pixels[i].type = 0; + pixels[i].order = order; + break; + } + } + + end_frame(); + + _delay_ms(50); +#if 0 + log_byte(0xFa); + log_flush(); +#endif } } diff --git a/projects/rgb-led-string/rgbstring.h b/projects/rgb-led-string/rgbstring.h index 565deef..a9dc950 100644 --- a/projects/rgb-led-string/rgbstring.h +++ b/projects/rgb-led-string/rgbstring.h @@ -18,70 +18,12 @@ void inline log_byte(unsigned char byte) { } void inline log_word(uint16_t word) { } #endif -/* adc.c */ -#define PWMLED_ADC_SHIFT 1 /* 1<<1 measurements per single callback */ -extern volatile unsigned char need_battery_adc; -extern volatile unsigned char need_pwmled_adc; -extern volatile unsigned char adc_enabled; -void init_adc(); -void susp_adc(); -void start_next_adc(); - -/* pwm.c */ -#define PWM_MAX 0xFF -extern volatile unsigned char pwm_enabled; -void init_pwm(); -void susp_pwm(); -void pwm_off(); -void pwm_set(uint8_t stride); - -/* pwmled.c */ -void init_pwmled(); -void pwmled_adc(uint16_t adcval); -void pwmled_set_target(unsigned char mode); -void pwmled_on_off(unsigned char on); - -/* pattern.c */ -void init_pattern(); -void patterns_next_tick(); -void led_set_pattern(unsigned char led, unsigned char bits_len, - unsigned char bits_start, unsigned char *data); -void led_set_number_pattern(unsigned char led, - unsigned char num, unsigned char inv); -void pattern_reload(); - -/* buttons.c */ -void init_buttons(); -void susp_buttons(); -void timer_check_buttons(); -unsigned char buttons_wait_for_release(); -void status_led_on_off(unsigned char on); - -/* battery.c */ -void battery_adc(); -void init_battery(); -unsigned char battery_gauge(); - -/* control.c */ -void init_control(); -void long_press_start(); -void long_press(); -void short_press(); -void brake_on(); -void brake_off(); -void pwmled_pattern_select(unsigned char led); -void status_led_pattern_select(unsigned char led); -#define ERR_BATTERY 1 -#define ERR_PWMLED 2 -void set_error(unsigned char err); - -/* wdt.c */ -extern volatile uint16_t jiffies; -void init_wdt(); -void susp_wdt(); - -/* main.c */ -void power_down(); +/* serial.c */ +#define STRIP_SIZE 160 +void init_serial(); +void zero_frame(); +void end_frame(); +void send_rgb(unsigned char r, unsigned char g, unsigned char b); #endif /* !LIGHTS_H__ */ diff --git a/projects/rgb-led-string/serial.c b/projects/rgb-led-string/serial.c new file mode 100644 index 0000000..706f6e6 --- /dev/null +++ b/projects/rgb-led-string/serial.c @@ -0,0 +1,88 @@ +#include +#include +#include + +#include "rgbstring.h" + +void init_serial() +{ + PORTB &= ~(_BV(PB2) | _BV(PB1)); + DDRB |= _BV(PB2) | _BV(PB1); + +#if 0 + TCCR0A = _BV(WGM01) | _BV(WGM00); + TCCR0B = _BV(WGM02) | _BV(CS00); + OCR0A = 2; +#endif + + zero_frame(); +} + +static void send_byte(unsigned char b) +{ + unsigned char i, mask; + +#if 0 + USIDR = b; + USISR = _BV(USIOIF); + USICR = _BV(USIWM0) | _BV(USICS0); + + while (!(USISR & _BV(USIOIF))) + ; +#endif + +#if 1 + USIDR = b; + USISR = _BV(USIOIF); + + while ( (USISR & _BV(USIOIF)) == 0 ) { + USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK); + USICR = _BV(USIWM0) | _BV(USICS1) | _BV(USICLK) | _BV(USITC); + } +#endif + +#if 0 + for (i = 0; i < 8; i++) { + USICR = _BV(USIWM0) | _BV(USITC); + USICR = _BV(USIWM0) | _BV(USITC) | _BV(USICLK); + } +#endif + +#if 0 + for (i = 0; i < 8; i++) { + PORTB &= ~_BV(PB2); // clock low + if (b & 0x80) // data bit on or off + PORTB |= _BV(PB1); + else + PORTB &= ~_BV(PB1); + b <<= 1; + PORTB |= _BV(PB2); // clock high + } +#endif +} + +void end_frame() +{ + PORTB &= ~_BV(PB2); // clock low + _delay_us(1000); +} + +void send_rgb(unsigned char r, unsigned char g, unsigned char b) +{ + send_byte(r); + send_byte(g); + send_byte(b); +} + + +void zero_frame() +{ + unsigned char i; + + for (i = 0; i < STRIP_SIZE; i++) { + send_rgb(0, 0, 0); + } + + end_frame(); +} +