diff --git a/main/arch-avr.mk b/main/arch-avr.mk index cb962d0..9757094 100644 --- a/main/arch-avr.mk +++ b/main/arch-avr.mk @@ -3,9 +3,9 @@ # notice and this notice are preserved. This file is offered as-is, # without any warranty. -MCU = atmega328p -DF_CPU = 16000000 -AVRDUDE_FLAGS=-c usbasp +MCU = attiny45 +DF_CPU = 8000000 +AVRDUDE_FLAGS=-c avrisp -b 19200 -P /dev/ttyACM0 CC = avr-gcc CFLAGS = -c -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(DF_CPU) -Os \ diff --git a/main/core/avr/clock.c b/main/core/avr/clock.c index 74e3ede..b5280cd 100644 --- a/main/core/avr/clock.c +++ b/main/core/avr/clock.c @@ -24,7 +24,7 @@ #include "../init.h" #include "../clock.h" -#define TIMER_MAX 250 +#define TIMER_MAX 125 #define TIMER_INC 4 @@ -40,7 +40,7 @@ void init_clock(void) { TCCR0A |= (1 << WGM01); // CTC mode TCCR0B |= (1 << CS02); // prescaler: 256 OCR0A = TIMER_MAX; - TIMSK0 |= (1 << OCIE0A); + TIMSK |= (1 << OCIE0A); sei(); } diff --git a/main/core/avr/pix.c b/main/core/avr/pix.c index ba83379..a3bdc00 100644 --- a/main/core/avr/pix.c +++ b/main/core/avr/pix.c @@ -2,7 +2,7 @@ * pix.c * This file is part of litepix * - * Copyright (C) 2014, 2015 - Florian Rommel + * Copyright (C) 2014, 2015 - Florian Rommel, Michael Nieß * * litepix is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,150 +27,84 @@ #define OUT_PORT (PORTB) #define OUT_DR (DDRB) -#define OUT_PIN_1 (PB0) -#define OUT_PIN_2 (PB1) -#define OUT_PIN_3 (PB2) - -#define START_1 (0) -#define START_2 (PIX_NUM_BYTES/3) -#define START_3 (PIX_NUM_BYTES/3*2) - +#define OUT_PIN (PB4) +#define START (0) uint8_t pix_canvas[PIX_NUM_BYTES] = {0}; - -static void rearrange(void) { - uint8_t *ps = &pix_canvas[60]; - uint8_t *pe = &pix_canvas[120]; - - asm ( - " ldi r18, 10\n\t" - " ldi r19, 6\n\t" - - "rearr: ld r20, %a[ps]\n\t" - " ldd r21, %a[ps]+1\n\t" - " ldd r22, %a[ps]+2\n\t" - " ld r25, -%a[pe]\n\t" - " ld r24, -%a[pe]\n\t" - " ld r23, -%a[pe]\n\t" - " st %a[pe], r20\n\t" - " std %a[pe]+1, r21\n\t" - " std %a[pe]+2, r22\n\t" - " st %a[ps]+, r23\n\t" - " st %a[ps]+, r24\n\t" - " st %a[ps]+, r25\n\t" - " dec r18\n\t" - " brne rearr\n\t" - " subi r28,-90\n\t" //TODO - " sbci r29,-1\n\t" - " subi r30,-150\n\t" //TODO - " sbci r31,-1\n\t" - " ldi r18, 10\n\t" - " dec r19\n\t" - " brne rearr\n\t" - :: - [ps] "y" (ps), - [pe] "z" (pe) - : "r18", "r19", "r20", "r21", - "r22", "r23", "r24", "r25" - ); +//switch pixel 3 with 5 +static void rearrange(void) { + uint8_t tmp1 = pix_canvas[9]; + uint8_t tmp2 = pix_canvas[10]; + uint8_t tmp3 = pix_canvas[11]; + pix_canvas[9] = pix_canvas[15]; + pix_canvas[10] = pix_canvas[16]; + pix_canvas[11] = pix_canvas[17]; + pix_canvas[15] = tmp1; + pix_canvas[16] = tmp2; + pix_canvas[17] = tmp3; } void init_pix(void) { - OUT_DR |= (1 << OUT_PIN_1); - OUT_DR |= (1 << OUT_PIN_2); - OUT_DR |= (1 << OUT_PIN_3); - OUT_PORT &= ~(1 << OUT_PIN_1); - OUT_PORT &= ~(1 << OUT_PIN_2); - OUT_PORT &= ~(1 << OUT_PIN_3); + OUT_DR |= (1 << OUT_PIN); + OUT_PORT &= ~(1 << OUT_PIN); } - void pix_render(void) { - rearrange(); - + rearrange(); uint8_t tmp_sreg = SREG; cli(); - uint8_t *p1 = &pix_canvas[START_1]; - uint8_t *p2 = &pix_canvas[START_2]; - uint8_t *p3 = &pix_canvas[START_3]; - uint8_t d1 = *p1++; - uint8_t d2 = *p2++; - uint8_t d3 = *p3++; + uint8_t *p = &pix_canvas[START]; + uint8_t d = *p++; - const uint8_t mask = _BV(OUT_PIN_1) | _BV(OUT_PIN_2) | _BV(OUT_PIN_3); + const uint8_t mask = _BV(OUT_PIN); const uint8_t low = OUT_PORT & (~mask); const uint8_t high = OUT_PORT | mask; uint8_t nbits = 7; uint8_t tmp = low; - uint16_t nbytes = PIX_NUM_BYTES/3; + uint8_t nbytes = PIX_NUM_BYTES; asm volatile( - "start: nop\n\t" - " nop\n\t" - " nop\n\t" - "set1: out %[ioport], %[portdown]\n\t" - " lsl %[data1]\n\t" - " brcc a1\n\t" - " sbr %[tmp], %[bit1]\n\t" - "a1: lsl %[data2]\n\t" - " brcc a2\n\t" - " sbr %[tmp], %[bit2]\n\t" - "a2: out %[ioport], %[portup]\n\t" - " lsl %[data3]\n\t" - " brcc a3\n\t" - " sbr %[tmp], %[bit3]\n\t" - "a3: nop\n\t" - " dec %[bitcount]\n\t" - "set0: out %[ioport], %[tmp] \n\t" - " mov %[tmp], %[portdown]\n\t" - " brne start\n\t" - - " lsl %[data1]\n\t" - " brcc b1\n\t" - " sbr %[tmp], %[bit1]\n\t" - "b1: lsl %[data2]\n\t" - " out %[ioport], %[portdown]\n\t" - " brcc b2\n\t" - " sbr %[tmp], %[bit2]\n\t" - "b2: ld %[data1], %a[ptr1]+\n\t" + "set1: out %[ioport], %[portdown]\n\t" + " lsl %[data1]\n\t" + " brcc a1\n\t" + " sbr %[tmp], %[bit1]\n\t" + "a1: out %[ioport], %[portup]\n\t" + " nop\n\t" + " dec %[bitcount]\n\t" + "set0: out %[ioport], %[tmp]\n\t" + " mov %[tmp], %[portdown]\n\t" + " brne set1\n\t" + " nop\n\t" + "b1: out %[ioport], %[portdown]\n\t" + " lsl %[data1]\n\t" + " brcc b2\n\t" + " sbr %[tmp], %[bit1]\n\t" + "b2: ld %[data1], %a[ptr1]+\n\t" // ^^^ - " ld %[data2], %a[ptr2]+\n\t" - // ^^^ - " out %[ioport], %[portup]\n\t" - " lsl %[data3]\n\t" - " brcc b3\n\t" - " sbr %[tmp], %[bit3]\n\t" - "b3: ld %[data3], %a[ptr3]+\n\t" - // ^^^ - " out %[ioport], %[tmp] \n\t" - " ldi %[bitcount], 7\n\t" + " nop\n\t" + " out %[ioport], %[portup]\n\t" + " nop\n\t" + " nop\n\t" + "b3: ldi %[bitcount], 7\n\t" + " out %[ioport], %[tmp]\n\t" " mov %[tmp], %[portdown]\n\t" - " sbiw %[bytecount], 1\n\t" - // ^^^ - " brne set1\n\t" - - " nop\n\t" + " subi %[bytecount], 1\n\t" + " brne set1\n\t" " out %[ioport], %[portdown]\n\t" :: [ioport] "I" (_SFR_IO_ADDR(OUT_PORT)), [portup] "l" (high), [portdown] "l" (low), [bitcount] "d" (nbits), - [ptr1] "e" (p1), - [ptr2] "e" (p2), - [ptr3] "e" (p3), - [data1] "d" (d1), - [data2] "d" (d2), - [data3] "d" (d3), - [bit1] "I" (1 << OUT_PIN_1), - [bit2] "I" (1 << OUT_PIN_2), - [bit3] "I" (1 << OUT_PIN_3), + [ptr1] "e" (p), + [data1] "d" (d), + [bit1] "I" (1 << OUT_PIN), [tmp] "d" (tmp), - [bytecount] "w" (nbytes) + [bytecount] "d" (nbytes) ); SREG = tmp_sreg; diff --git a/main/core/pix.h b/main/core/pix.h index 92400be..7c61def 100644 --- a/main/core/pix.h +++ b/main/core/pix.h @@ -26,9 +26,9 @@ #include -#define PIX_NUM_PIXELS (240) -#define PIX_WIDTH (20) -#define PIX_HEIGHT (12) +#define PIX_NUM_PIXELS (9) +#define PIX_WIDTH (3) +#define PIX_HEIGHT (3) #define PIX_NUM_BYTES (PIX_NUM_PIXELS*3) diff --git a/main/core/sim/pix.c b/main/core/sim/pix.c index 84c3fcb..65c2493 100644 --- a/main/core/sim/pix.c +++ b/main/core/sim/pix.c @@ -2,7 +2,7 @@ * pix.c * This file is part of litepix * - * Copyright (C) 2015 - Florian Rommel + * Copyright (C) 2015 - Florian Rommel, Michael Nieß * * litepix is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,27 +34,26 @@ static uint8_t save_canvas[PIX_NUM_BYTES] = {0}; static GMutex save_canvas_mutex; static GtkWidget *window; - +const int size = 100; +const int offset = 10; static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, gpointer data) { - const int offset = 10; - cairo_set_source_rgb(cr, 0, 0, 0); - cairo_rectangle(cr, 0, 0, 620, 380); + cairo_rectangle(cr, 0, 0, (size*PIX_HEIGHT)+2*offset, (size*PIX_WIDTH)+2*offset); cairo_fill(cr); g_mutex_lock(&save_canvas_mutex); uint8_t *p = save_canvas; int i, j; - for (i = 0; i < 12; i++) { - for (j = 0; j < 20; j++) { + for (i = 0; i < PIX_WIDTH; i++) { + for (j = 0; j < PIX_HEIGHT; j++) { uint8_t g = *p++; uint8_t r = *p++; uint8_t b = *p++; cairo_set_source_rgb(cr, ((double)r)/256, ((double)g)/256, ((double)b)/256); - cairo_rectangle(cr, j*30+offset, i*30+offset, 28, 28); + cairo_rectangle(cr, j*size+offset, i*size+offset, size-2, size-2); cairo_fill(cr); } } @@ -90,7 +89,7 @@ void init_pix(void) { G_CALLBACK(on_window_destroy), NULL); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); - gtk_window_set_default_size(GTK_WINDOW(window), 620, 380); + gtk_window_set_default_size(GTK_WINDOW(window), (size*PIX_HEIGHT)+2*offset, (size*PIX_WIDTH)+2*offset); gtk_window_set_title(GTK_WINDOW(window), "Simulator"); gtk_widget_show_all(window); diff --git a/main/util/setter.c b/main/util/setter.c new file mode 100644 index 0000000..de55dcf --- /dev/null +++ b/main/util/setter.c @@ -0,0 +1,55 @@ +/* + * setter.c + * This file is part of litepix + * + * Copyright (C) 2015 - Michael Nieß + * + * litepix is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * litepix is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with litepix. If not, see . + */ + + +#include +#include +#include "setter.h" +#include "../core/pix.h" + +//Set a pixels color +void set_pixel(uint8_t* canvas, uint8_t index, uint8_t color[3]){ + uint8_t i = index * 3; + canvas[i++] = color[1]; + canvas[i++] = color[0]; + canvas[i] = color[2]; +} + +//Set the whole canvas to one color +void set_full(uint8_t* canvas, uint8_t color[3]) { + uint8_t i; + uint8_t* p = canvas; + for (i = 0; i < PIX_NUM_PIXELS; i++) { + *p++ = color[1]; + *p++ = color[0]; + *p++ = color[2]; + } +} +//Set random colors in whole canvas +void set_random(uint8_t* canvas){ + uint8_t i; + uint8_t* p = canvas; + for (i = 0; i < PIX_NUM_PIXELS; i++) { + *p++ = rand() % 256; + *p++ = rand() % 256; + *p++ = rand() % 256; + } +} + diff --git a/main/core/input.h b/main/util/setter.h similarity index 74% rename from main/core/input.h rename to main/util/setter.h index 6a441a1..c67f247 100644 --- a/main/core/input.h +++ b/main/util/setter.h @@ -1,8 +1,8 @@ /* - * input.h + * setter.h * This file is part of litepix * - * Copyright (C) 2015 - Florian Rommel + * Copyright (C) 2015 - Michael Nieß * * litepix is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,20 +19,15 @@ */ -#ifndef _CORE__INPUT_H_ -#define _CORE__INPUT_H_ +#ifndef _UTIL__SETTER_H_ +#define _UTIL__SETTER_H_ #include -#include +void set_pixel(uint8_t* pic, uint8_t index, uint8_t color[3]); -typedef struct { - uint8_t dev; - uint8_t data; -} t_input; - - -bool input_test(t_input* input); +void set_full(uint8_t* pic, uint8_t color[3]); +void set_random(uint8_t* pic); #endif diff --git a/main/util/transitions.c b/main/util/transitions.c index cd77c4d..4c6cf7b 100644 --- a/main/util/transitions.c +++ b/main/util/transitions.c @@ -2,7 +2,7 @@ * transitions.c * This file is part of litepix * - * Copyright (C) 2015 - Florian Rommel + * Copyright (C) 2015 - Florian Rommel, Michael Nieß * * litepix is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -42,41 +42,19 @@ static void shuffle(uint8_t *arr, uint8_t size) { } } - -static void calculate_parameters(uint32_t duration, - int16_t* interval, uint8_t* steps) { - if (duration > TWO_HOURS) duration = TWO_HOURS; - - if (duration >= DEFAULT_SAMPLE * BYTE_SIZE) { - *steps = BYTE_MAX; - float f = duration; - f = f / BYTE_SIZE; - *interval = f + 0.5; - } else { - *interval = DEFAULT_SAMPLE; - float f = duration; - f = f / DEFAULT_SAMPLE; - *steps = f + 0.5; - } -} - - +//Fade from current canvas to a given canvas in a specific time void tr_fade_p(uint8_t* pic, uint32_t duration, uint8_t* mask) { - uint8_t step = 0; - uint8_t step_count; - int16_t interval; - calculate_parameters(duration, &interval, &step_count); - - t_timer timer = TIMER_INIT(interval); + uint8_t step = 0; + t_timer timer = TIMER_INIT(16); - while (step < step_count) { + while (step < (duration / 16)) { - if (timer_test(&timer, interval)) { + if (timer_test(&timer, 16)) { uint8_t i; uint8_t* p_dest = pix_canvas; uint8_t* p_src = pic; - uint8_t x = step_count - step; + uint8_t x = (duration / 16) - step; for (i = 0; i < PIX_NUM_PIXELS; i++) { if ((mask == NULL) || bitmap_get(mask, i)) { @@ -109,109 +87,26 @@ void tr_fade_p(uint8_t* pic, uint32_t duration, uint8_t* mask) { } } - -void tr_dissolve_p(uint8_t* pic, uint32_t duration, uint8_t* mask) { - uint8_t step = 0; - uint8_t d_sum = 0; - uint8_t step_count; - int16_t interval; - calculate_parameters(duration, &interval, &step_count); - - uint8_t i, j; - uint8_t order_size = 0; - if (mask == NULL) order_size = PIX_NUM_PIXELS; - else { - for (i = 0; i < PIX_NUM_PIXELS; i++) { - if (bitmap_get(mask, i)) order_size++; - } - } - uint8_t order[order_size]; - for (i = 0, j= 0; i < PIX_NUM_PIXELS; i++) { - if ((mask == NULL) || bitmap_get(mask, i)) { - order[j] = i; - j++; - } - } - shuffle(order, order_size); - - t_timer timer = TIMER_INIT(interval); - - while (step < step_count) { - if (timer_test(&timer, interval)) { - uint8_t d = (order_size - d_sum) / (step_count - step); - uint16_t i = d_sum; - uint8_t n; - - for (n = d; n > 0; n--) { - uint16_t p = order[i] * 3; - pix_canvas[p] = pic[p]; - pix_canvas[p+1] = pic[p+1]; - pix_canvas[p+2] = pic[p+2]; - i++; - } - - step++; - d_sum += d; - pix_render(); - } - } -} - - -void tr_roll_p(uint8_t* pic, uint32_t duration, tr_direction direction, - uint8_t* mask) { - uint8_t step = 0; - uint8_t step_count; - int16_t interval; - calculate_parameters(duration, &interval, &step_count); - - // calculate direction dependant vars - uint8_t row_size, dist, col_dist, substep_count; - if (direction <= tr_right_left) { - row_size = PIX_HEIGHT; - dist = PIX_WIDTH; - col_dist = 1; - substep_count = step_count / PIX_WIDTH; - if (substep_count == 0) substep_count = 1; - step_count = substep_count * PIX_WIDTH; - } - else { - row_size = PIX_WIDTH; - dist = 1; - col_dist = PIX_WIDTH; - substep_count = step_count / PIX_HEIGHT; - if (substep_count == 0) substep_count = 1; - step_count = substep_count * PIX_HEIGHT; +//switch random pixels to new canvas-pixels until all pixels are changed +void tr_dissolve_p(uint8_t* pic, uint32_t duration) { + uint8_t i; + uint8_t order[PIX_NUM_PIXELS]; + uint32_t interval = duration / PIX_NUM_PIXELS; + for (i = 0; i < PIX_NUM_PIXELS; i++) { + order[i] = i; } + shuffle(order, PIX_NUM_PIXELS); - t_timer timer = TIMER_INIT(interval); - - while (step < step_count) { - if (timer_test(&timer, interval)) { - uint8_t column = step / substep_count; - uint8_t substep = step - (column * substep_count); - int8_t x = substep_count - substep; - - if (direction == tr_right_left) - column = PIX_WIDTH - column - 1; - else if (direction == tr_bottom_top) - column = PIX_HEIGHT - column - 1; - - uint8_t i, ib = column * col_dist; - for (i = 0; i < row_size; i++) { - if ((mask == NULL) || bitmap_get(mask, ib)) { - uint8_t *p1 = &pix_canvas[ib * 3]; - uint8_t *p2 = &pic[ib * 3]; - *p1 += (*p2 - *p1) / x; - p1++; p2++; - *p1 += (*p2 - *p1) / x; - p1++; p2++; - *p1 += (*p2 - *p1) / x; - } - ib += dist; - } - - step++; + t_timer timer = TIMER_INIT(0); + i = 0; + while (i < PIX_NUM_PIXELS) { + if (timer_test(&timer, interval)) { + uint16_t p = order[i] * 3; + pix_canvas[p] = pic[p]; + pix_canvas[p+1] = pic[p+1]; + pix_canvas[p+2] = pic[p+2]; + + i++; pix_render(); } } diff --git a/main/util/transitions.h b/main/util/transitions.h index e947e71..7f98d8a 100644 --- a/main/util/transitions.h +++ b/main/util/transitions.h @@ -2,7 +2,7 @@ * transitions.h * This file is part of litepix * - * Copyright (C) 2015 - Florian Rommel + * Copyright (C) 2015 - Florian Rommel, Michael Nieß * * litepix is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,10 +40,6 @@ typedef enum { void tr_fade_p(uint8_t* pic, uint32_t duration, uint8_t mask[TR_MASK_SIZE]); -void tr_dissolve_p(uint8_t* pic, uint32_t duration, uint8_t mask[TR_MASK_SIZE]); - -void tr_roll_p(uint8_t* pic, uint32_t duration, tr_direction direction, - uint8_t mask[TR_MASK_SIZE]); - +void tr_dissolve_p(uint8_t* pic, uint32_t duration); #endif