Commits

Trammell Hudson committed bc94af5 Merge

merge

Comments (0)

Files changed (3)

+#include "sin_table.h"
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+
+static const uint8_t sin_table[] = {
+	0,
+	25,
+	50,
+	74,
+	98,
+	121,
+	142,
+	162,
+	181,
+	198,
+	213,
+	226,
+	237,
+	245,
+	251,
+	255,
+	255,
+};
+
+int8_t
+sin_lookup(
+	uint8_t theta
+)
+{
+	int sign = 1;
+
+	if (theta == 0)
+	{
+		return 0;
+	} else
+	if (theta < 64)
+	{
+		// q1, upward slope, normal theta, positive sign
+	} else
+	if (theta == 64)
+	{
+		return 127;
+	} else
+	if (theta < 128)
+	{
+		theta = 128 - theta; // downward slope, still positive
+	} else
+	if (theta == 128)
+	{
+		return 0;
+	} else
+	if (theta < 192)
+	{
+		// q3, downward slope, negative side
+		theta = theta - 128;
+		sign = -1;
+	} else
+	if (theta == 192)
+	{
+		return -128;
+	} else
+	{
+		// q4, upward slope, negative side
+		theta = 256 - theta;
+		sign = -1;
+	}
+
+	int16_t s1 = sin_table[(theta >> 2) + 0];
+	int16_t s2 = sin_table[(theta >> 2) + 1];
+	int8_t result = (s1 + ((s2 - s1) * (theta & 0x3)) / 4) / 2;
+
+	fprintf(stderr, "theta=%d %d+%d => %d %d => %d\n",
+		theta,
+		theta >> 2,
+		theta & 0x3,
+		s1,
+		s2,
+		result
+	);
+
+	if (sign == -1)
+		return -result;
+	else
+		return result;
+}
+
+
+#if 0
+int main(void)
+{
+	unsigned theta;
+
+	for (theta = 0 ; theta < 0x100 ; theta++)
+	{
+		double sx = sin_lookup(theta) / 128.0;
+		double sf = sin(theta * 2 * M_PI / 256.0);
+
+		printf("%.4f %.4f %.4f\n",
+			sx,
+			sf,
+			sf - sx
+		);
+	}
+}
+#endif
+/** \file
+ * Approximate sin function.
+ * theta goes from 0 == 0 Pi to 255 == 2 Pi
+ */
+#ifndef _sin_table_h_
+#define _sin_table_h_
+
+#include <stdint.h>
+
+static int8_t sin_lookup(uint8_t theta);
+static int8_t cos_lookup(uint8_t theta);
+
+#endif
+/** \file
+ * Draw an infinite starfield
+ */
+#include "app.h"
+#include "sin_table.h"
+
+#define MAX_STARS 64
+
+struct star
+{
+	uint8_t x; // scaled by 2
+	uint8_t y;
+	int8_t dx;
+	int8_t dy;
+	//uint8_t brightness;
+} __attribute__((packed));
+
+
+struct stars_app
+{
+	struct star stars[MAX_STARS];
+} __attribute__((packed));
+
+
+static void
+star_create(
+	struct star * const star
+)
+{
+	uint8_t theta = rand() % 0xFF;
+	star->sin_t = sin_table(theta);
+
+	star->x = SCREEN_WIDTH; // scaled by 2 == center
+	star->y = SCREEN_HEIGHT;
+
+	//star->brightness = rand() % 0xFF;
+}
+
+
+static void
+stars_init(void)
+{
+	for (int i=0 ; i < NUM_STARS ; i++)
+		star_create(&APP->stars[i]);
+}
+
+
+static void
+star_draw(
+	struct star * const star
+)
+{
+	// Turn off its current location
+	star_draw_point(star, COLOR_BLACK24);
+
+	// Check for age
+	int32_t x = star->x + star->dx;
+	int32_t y = star->y + star->dy;
+	if (x < 0 || x >= SCREEN_WIDTH
+	||  y < 0 || y >= SCREEN_HEIGHT)
+	{
+		star_create(star);
+		return;
+	}
+
+	// Update the new location
+	star->x = x;
+	star->y = y;
+
+	star_draw_point(star, COLOR_WHITE24);
+}
+
+
+
+
+static void
+stars_loop(
+	const struct pulse_time_tm * const now
+)
+{
+	for (int i=0 ; i < NUM_STARS ; i++)
+		star_draw(&APP->stars[i]);
+}
+
+
+const app_t stars_app = {
+	.init		= stars_init,
+	.loop		= stars_loop,
+	.button_down	= stars_init,
+};