2011-12-15 04:43:40 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "../config.h"
|
|
|
|
#include "../pixel.h"
|
|
|
|
#include "../util.h"
|
|
|
|
#include "../compat/pgmspace.h"
|
|
|
|
#include "blackhole.h"
|
|
|
|
|
|
|
|
// macro for simplifying flash memory access
|
|
|
|
#define PGM(x) pgm_read_byte(&(x))
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-04 20:46:25 +00:00
|
|
|
* Integer variant of the sine function which takes an integer based angle
|
2011-12-15 04:43:40 +00:00
|
|
|
* (range from 0 to 63) where sin_i(64) corresponds to sin(2 * M_PI) * 64 and
|
2012-05-04 20:46:25 +00:00
|
|
|
* sin_i(16) corresponds to sin(M_PI_2) * 64 (each excluding the fractional
|
|
|
|
* part). It uses a lookup table which models one quarter of a full sine period
|
2011-12-15 04:43:40 +00:00
|
|
|
* and calculates the rest of the function from that quarter.
|
|
|
|
* @param angle angle based on an integer (range from 0 to 63)
|
2012-05-04 20:46:25 +00:00
|
|
|
* @return result of the sine function normalized to a range from -64 to 64
|
2011-12-15 04:43:40 +00:00
|
|
|
*/
|
2012-09-08 01:35:32 +00:00
|
|
|
static signed char sin_i(unsigned char angle) {
|
2011-12-15 04:43:40 +00:00
|
|
|
// the aforementioned lookup table
|
2012-05-04 20:46:25 +00:00
|
|
|
static signed char const sine_table[] PROGMEM =
|
2011-12-15 04:43:40 +00:00
|
|
|
{0, 6, 12, 19, 24, 30, 36, 41, 45, 49, 53, 56, 59, 61, 63, 64};
|
|
|
|
|
|
|
|
// determine correct index for the lookup table depending on the given angle
|
|
|
|
angle %= 64u;
|
|
|
|
unsigned char index;
|
|
|
|
if (angle < 16) {
|
|
|
|
index = angle;
|
|
|
|
} else if (angle < 32) {
|
2011-12-16 19:40:25 +00:00
|
|
|
index = 31 - angle;
|
2011-12-15 04:43:40 +00:00
|
|
|
} else if (angle < 48) {
|
|
|
|
index = angle - 32;
|
|
|
|
} else {
|
2011-12-16 19:40:25 +00:00
|
|
|
index = 63 - angle;
|
2011-12-15 04:43:40 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 20:46:25 +00:00
|
|
|
return (signed char)(PGM(sine_table[index])) * (angle < 32 ? 1 : -1);
|
2011-12-15 04:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-04 20:46:25 +00:00
|
|
|
* Integer variant of the cosine function which takes an integer based angle
|
2011-12-15 04:43:40 +00:00
|
|
|
* (range from 0 to 63) where cos_i(64) corresponds to cos(2 * M_PI) * 64 and
|
2012-05-04 20:46:25 +00:00
|
|
|
* cos_i(16) corresponds to cos(M_PI_2) * 64. It uses the sin_i function and
|
|
|
|
* shifts the given angle by 16 (which corresponds to a delta of 90 degrees).
|
2011-12-15 04:43:40 +00:00
|
|
|
* @param angle angle based on an integer (range from 0 to 63)
|
2012-05-04 20:46:25 +00:00
|
|
|
* @return result of the cosine function normalized to a range from -64 to 64
|
2011-12-15 04:43:40 +00:00
|
|
|
*/
|
|
|
|
static signed char cos_i(unsigned char const angle) {
|
|
|
|
return sin_i(angle + 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define NUM_CIRCLE 7
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a black hole like pattern (viewed from different perspectives).
|
|
|
|
*/
|
|
|
|
void blackhole(void) {
|
2012-05-04 20:46:25 +00:00
|
|
|
signed char firstRadius = 80, helpRadius, angle = 0, add = 0;
|
2011-12-15 04:43:40 +00:00
|
|
|
// initialize data
|
2012-05-04 20:46:25 +00:00
|
|
|
for (unsigned int k = 0; k < 800; k++) {
|
|
|
|
if (k > 300) {
|
2011-12-15 04:43:40 +00:00
|
|
|
add = k / 16;
|
2012-05-04 20:46:25 +00:00
|
|
|
}
|
2011-12-15 04:43:40 +00:00
|
|
|
helpRadius = firstRadius;
|
2012-05-04 20:46:25 +00:00
|
|
|
for (signed char i = 0; i < NUM_CIRCLE; i++) {
|
|
|
|
for (signed char j = 0; j < 64; j += 8) {
|
|
|
|
signed char a = (j & 0x08) ? 0 : 4;
|
|
|
|
pixel p;
|
|
|
|
p.x = (64 + cos_i(angle + j + a) * helpRadius / 64) >> 3;
|
|
|
|
p.y = (64 + sin_i(angle + add + j + a) * helpRadius / 64) >> 3;
|
|
|
|
if ((p.x < NUM_COLS) && (p.y < NUM_ROWS)) {
|
|
|
|
setpixel(p, 3);
|
2011-12-15 04:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
helpRadius = (helpRadius * 2) / 3;
|
|
|
|
}
|
|
|
|
wait(30);
|
|
|
|
clear_screen(0);
|
|
|
|
angle++;
|
|
|
|
firstRadius += 2;
|
2012-05-04 20:46:25 +00:00
|
|
|
if (firstRadius > 119) {
|
2011-12-15 04:43:40 +00:00
|
|
|
firstRadius = 80;
|
2012-05-04 20:46:25 +00:00
|
|
|
}
|
2011-12-15 04:43:40 +00:00
|
|
|
}
|
|
|
|
}
|