2012-05-07 06:56:00 +00:00
|
|
|
/**
|
|
|
|
* \defgroup mherweg Martin Herweg's animations.
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file mherweg.c
|
|
|
|
* @brief Simple animations for getting started with developing for the Borg.
|
|
|
|
* @details The following animations were developed by Martin Herweg (hence the
|
|
|
|
* name) as a personal aid for getting familiar with developing for the
|
|
|
|
* Borg.
|
|
|
|
*
|
|
|
|
* Although these animations are rarely used among Borg owners, we left
|
|
|
|
* them in because of their simplicity in hopes that a novice Borg
|
|
|
|
* developer may find them useful.
|
|
|
|
* @author Martin Herweg
|
|
|
|
*/
|
|
|
|
|
2011-02-24 23:13:01 +00:00
|
|
|
#include "../compat/pgmspace.h"
|
|
|
|
#include "../random/prng.h"
|
|
|
|
#include "../config.h"
|
|
|
|
#include "../pixel.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
|
2012-05-07 06:56:00 +00:00
|
|
|
/** macro for simplifying flash memory access */
|
2011-08-31 18:59:04 +00:00
|
|
|
#define PGM(x) pgm_read_byte(&(x))
|
2011-02-24 23:13:01 +00:00
|
|
|
|
|
|
|
|
2011-09-02 08:02:03 +00:00
|
|
|
#if NUM_ROWS < 64 && NUM_COLS < 64
|
2012-05-07 06:56:00 +00:00
|
|
|
/** use 8 bit operands where feasible */
|
2011-08-31 18:59:04 +00:00
|
|
|
typedef signed char operand_t;
|
|
|
|
#else
|
2012-05-07 06:56:00 +00:00
|
|
|
/** use 16 bit operands if either width or height are >= 64 */
|
2011-08-31 18:59:04 +00:00
|
|
|
typedef int operand_t;
|
|
|
|
#endif
|
2011-02-24 23:13:01 +00:00
|
|
|
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
/**
|
|
|
|
* An implementation of Bresenham's line drawing algorithm.
|
|
|
|
* @param p1 first coordinate of the line
|
|
|
|
* @param p2 second coordinate of the line
|
|
|
|
* @param color brightness level of the line
|
|
|
|
*/
|
|
|
|
static void line(pixel p1,
|
|
|
|
pixel const p2,
|
|
|
|
unsigned char const color)
|
|
|
|
{
|
|
|
|
operand_t const dx = p1.x < p2.x ? p2.x - p1.x : p1.x - p2.x;
|
|
|
|
operand_t const sx = p1.x < p2.x ? 1 : -1;
|
|
|
|
operand_t const dy = p1.y < p2.y ? p2.y - p1.y : p1.y - p2.y;
|
|
|
|
operand_t const sy = p1.y < p2.y ? 1 : -1;
|
|
|
|
operand_t error = dx - dy;
|
2011-02-24 23:13:01 +00:00
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
while(1)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
setpixel(p1, color);
|
|
|
|
if ((p1.x == p2.x) && (p1.y == p2.y))
|
|
|
|
break;
|
|
|
|
operand_t const error2 = 2 * error;
|
|
|
|
if (error2 > -dy)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
error -= dy;
|
|
|
|
p1.x += sx;
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
2011-08-31 18:59:04 +00:00
|
|
|
if (error2 < dx)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
error += dx;
|
|
|
|
p1.y += sy;
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
/**
|
|
|
|
* Draws a rectangle at the given coordinates.
|
|
|
|
* @param p coordinate of the rectangle's upper right corner
|
|
|
|
* @param w width of the rectangle
|
|
|
|
* @param h height of the rectangle
|
|
|
|
* @param color brightness level of the rectangle
|
|
|
|
*/
|
2011-08-31 19:36:03 +00:00
|
|
|
static void filled_rectangle(pixel const p,
|
2011-08-31 18:59:04 +00:00
|
|
|
unsigned char const w,
|
|
|
|
unsigned char const h,
|
|
|
|
unsigned char const color)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
// emulate the rectangle via consecutive drawn lines
|
|
|
|
for (unsigned char y = p.y; y < (p.y + h); ++y)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
line((pixel){p.x, y}, (pixel){p.x + w - 1, y}, color);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
/**
|
|
|
|
* Draws a checkbox like figure.
|
|
|
|
*/
|
|
|
|
static void checkbox()
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
// some parameters regarding appearance and timing
|
|
|
|
unsigned char const color = NUMPLANE; // brightest color
|
|
|
|
int const delay = 250, shiftdelay = 30;
|
2011-02-24 23:13:01 +00:00
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
// draw a surrounding square
|
|
|
|
static pixel const square[] = {{0, 0}, {7, 0}, {7, 7}, {0, 7}, {0, 0}};
|
|
|
|
for (unsigned char i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
line(square[i], square[i + 1], color);
|
|
|
|
wait(delay);
|
|
|
|
}
|
2011-02-24 23:13:01 +00:00
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
// draw two diagonal lines
|
2011-09-01 21:56:41 +00:00
|
|
|
line((pixel){7, 7}, (pixel){0, 0}, color);
|
|
|
|
wait(delay);
|
|
|
|
line((pixel){0, 7}, (pixel){7, 0}, color);
|
|
|
|
wait(delay * 3);
|
2011-02-24 23:13:01 +00:00
|
|
|
|
2011-08-31 19:36:03 +00:00
|
|
|
// shift image to the right (shift_pximap_l() really shifts to the right)
|
2011-08-31 18:59:04 +00:00
|
|
|
for (unsigned char x = NUM_COLS; x--;)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
shift_pixmap_l();
|
2011-09-01 21:56:41 +00:00
|
|
|
wait(shiftdelay);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
/**
|
|
|
|
* Animated lines walking over the screen.
|
|
|
|
*/
|
|
|
|
static void movinglines()
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
// some parameters regarding appearance and timing
|
|
|
|
unsigned char const color = NUMPLANE; // brightest color
|
|
|
|
unsigned char const blank = 0;
|
|
|
|
int const delay = 100;
|
|
|
|
|
|
|
|
// a line walking to the right
|
|
|
|
line((pixel){NUM_COLS - 1, NUM_ROWS - 1}, (pixel){NUM_COLS - 1, 0}, color);
|
|
|
|
for (unsigned char x = 0; x < NUM_COLS; x++)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
shift_pixmap_l();
|
2011-09-01 21:56:41 +00:00
|
|
|
wait(delay);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
// a line walking from the lower to the upper border
|
|
|
|
for (unsigned char y = NUM_ROWS; y--;)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-09-01 21:56:41 +00:00
|
|
|
line((pixel){0, y}, (pixel){NUM_COLS - 1, y}, color);
|
|
|
|
wait(delay);
|
|
|
|
line((pixel){0, y}, (pixel){NUM_COLS - 1, y}, blank);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
// quickly moving cross hairs
|
|
|
|
for (unsigned char n = 0; n < 4; n++)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
for (unsigned char x = 0; x < NUM_COLS - 1; x++)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
unsigned char y = x % NUM_ROWS;
|
2011-09-01 21:56:41 +00:00
|
|
|
line((pixel){0, y}, (pixel){NUM_COLS - 1, y}, color);
|
|
|
|
line((pixel){x, 0}, (pixel){x, NUM_ROWS - 1}, color);
|
|
|
|
wait(delay / 2);
|
|
|
|
line((pixel){0, y}, (pixel){NUM_COLS - 1, y}, blank);
|
|
|
|
line((pixel){x, 0}, (pixel){x, NUM_ROWS - 1}, blank);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
/**
|
|
|
|
* Draws a gradient colored square.
|
|
|
|
*/
|
|
|
|
static void rectangle1()
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
// we want a centered square
|
|
|
|
unsigned char const xcenter = NUM_COLS / 2, ycenter = NUM_ROWS / 2;
|
|
|
|
// it should be as big as the borg's height
|
|
|
|
unsigned char size = NUM_ROWS;
|
|
|
|
// darkest color as a starting point for the gradient
|
|
|
|
unsigned char color = 0;
|
|
|
|
// wait about 500 ms between each frame
|
|
|
|
int const delay = 500;
|
|
|
|
|
|
|
|
// create a gradient by drawing shrinking rectangles on top of each other
|
2011-02-24 23:13:01 +00:00
|
|
|
clear_screen(0);
|
2011-08-31 18:59:04 +00:00
|
|
|
for (unsigned char x = 8; x > 0; x--)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
// draw the rectangle and wait for a moment
|
2011-02-24 23:13:01 +00:00
|
|
|
filled_rectangle((pixel){(xcenter - x), (ycenter - x)},
|
2011-08-31 18:59:04 +00:00
|
|
|
size, size, color);
|
2011-09-01 21:56:41 +00:00
|
|
|
wait(delay);
|
2011-08-31 18:59:04 +00:00
|
|
|
|
|
|
|
// iterate through all colors periodically
|
|
|
|
++color;
|
|
|
|
color %= (NUMPLANE + 1);
|
|
|
|
|
|
|
|
// shrink the dimensions of the succeeding rectangle
|
2011-02-24 23:13:01 +00:00
|
|
|
size -= 2;
|
|
|
|
}
|
|
|
|
|
2011-09-01 21:56:41 +00:00
|
|
|
wait(delay * 3);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
/**
|
|
|
|
* Draws randomly placed rectangles.
|
|
|
|
*/
|
|
|
|
static void rectangles()
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
unsigned char const blank = 0;
|
|
|
|
clear_screen(blank);
|
|
|
|
for (unsigned char n = 0; n < 60; n++)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-09-01 21:56:41 +00:00
|
|
|
// randomly choose position, dimensions and color (the rectangle may
|
|
|
|
// exceed the actual screen size, only width and height have to be > 0)
|
2011-08-31 18:59:04 +00:00
|
|
|
unsigned char const x = random8() % NUM_COLS;
|
|
|
|
unsigned char const y = random8() % NUM_ROWS;
|
2011-09-01 21:56:41 +00:00
|
|
|
unsigned char const w = random8() % (NUM_COLS / 2) + 1;
|
|
|
|
unsigned char const h = random8() % (NUM_ROWS / 2) + 1;
|
2011-08-31 18:59:04 +00:00
|
|
|
unsigned char const color = random8() % (NUMPLANE + 1);
|
|
|
|
|
|
|
|
filled_rectangle((pixel){x, y}, w, h, color);
|
|
|
|
|
|
|
|
// wait between 500 and 750 ms
|
2011-09-01 21:56:41 +00:00
|
|
|
wait(500 + random8());
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
/**
|
|
|
|
* Draws flashing slanted lines.
|
|
|
|
*/
|
|
|
|
static void lines1()
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
unsigned char const blank = 0;
|
|
|
|
clear_screen(blank);
|
|
|
|
for (unsigned char n = 0; n < 200; n++)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-09-01 21:56:41 +00:00
|
|
|
// randomly choose position, length and color
|
|
|
|
unsigned char const x1 = random8() % NUM_COLS;
|
|
|
|
unsigned char const y1 = random8() % NUM_ROWS;
|
|
|
|
unsigned char const x2 = random8() % NUM_COLS;
|
|
|
|
unsigned char const y2 = random8() % NUM_ROWS;
|
2011-08-31 18:59:04 +00:00
|
|
|
unsigned char const color = random8() % (NUMPLANE + 1);
|
|
|
|
|
2011-09-01 21:56:41 +00:00
|
|
|
line((pixel){x1, y1}, (pixel){x2, y2}, color);
|
2011-08-31 18:59:04 +00:00
|
|
|
wait(random8()); // wait up to 250 ms
|
2011-09-01 21:56:41 +00:00
|
|
|
line((pixel){x1, y1}, (pixel){x2, y2}, blank);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
/**
|
|
|
|
* Draws randomly placed dots.
|
|
|
|
*/
|
|
|
|
static void dots1()
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
// some parameters regarding appearance and timing
|
|
|
|
int const glimmer_delay = 100;
|
|
|
|
unsigned char const blank = 0;
|
|
|
|
clear_screen(blank);
|
2011-02-24 23:13:01 +00:00
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
// draw up to 50 dots
|
|
|
|
for (unsigned char n = 50; n--;)
|
2011-02-24 23:13:01 +00:00
|
|
|
{
|
2011-08-31 18:59:04 +00:00
|
|
|
// random coordinates
|
|
|
|
unsigned char x = random8() % NUM_COLS;
|
|
|
|
unsigned char y = random8() % NUM_ROWS;
|
2011-02-24 23:13:01 +00:00
|
|
|
|
2011-08-31 18:59:04 +00:00
|
|
|
// those dots are glimmering
|
2011-02-24 23:13:01 +00:00
|
|
|
static unsigned char const color[5] PROGMEM = {1, 2, 3, 2, 1};
|
|
|
|
for (unsigned char i = 0; i < 5; ++i)
|
|
|
|
{
|
2011-09-01 21:56:41 +00:00
|
|
|
setpixel((pixel){x, y}, PGM(color[i]));
|
2011-08-31 18:59:04 +00:00
|
|
|
wait(glimmer_delay);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
2011-08-31 18:59:04 +00:00
|
|
|
|
2011-09-02 08:02:03 +00:00
|
|
|
// wait up to 2.5 seconds before the next dot is drawn
|
2011-09-01 21:56:41 +00:00
|
|
|
wait(random8() * 10);
|
2011-02-24 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-31 18:59:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go through all of Martin's animations.
|
|
|
|
*/
|
|
|
|
void mherweg()
|
|
|
|
{
|
|
|
|
lines1();
|
|
|
|
dots1();
|
|
|
|
movinglines();
|
|
|
|
checkbox();
|
|
|
|
rectangle1();
|
|
|
|
rectangles();
|
|
|
|
}
|
2012-05-07 06:56:00 +00:00
|
|
|
|
|
|
|
/*@}*/
|