Source

pebble / cube / src / teatime.c

Full commit
/** \file
 * Show off the 3D accelerometer data.
 */
#include <pebble_os.h>
#include <pebble_app.h>
#include <pebble_fonts.h>
#include "draw.h"
#include "mathutil.h"
#include "camera.h"
#include "wireframe.h"
#include "fmt.h"

PBL_APP_INFO(
	"Teatime",
	"hudson",
	1, // Version
	INVALID_RESOURCE,
	APP_INFO_WATCH_FACE
);


static Window window;
static TextLayer time_layer;
static Layer layer;

static camera_t camera;
static int32_t euler[3];

// Bring in a model to render
//#include "teapot.h"
#include "cube.h"

#define ARRAY_COUNT(x) (sizeof(x) / sizeof(*x))
static pixel_t pixels[ARRAY_COUNT(vertices)];


static void
layer_update(
	Layer * const me,
	GContext * ctx
)
{
	euler[0] += 1;
	static uint8_t t;
	if (1 || (t++) % 30 == 0)
	{
		euler[1] += 1024;
		//if (euler[1]++ == 64)
			//euler[1] = -63;
	}

	camera_setup(&camera, 30000, euler);

	// Fill in the screen with black
	const unsigned w = me->bounds.size.w;
	const unsigned h = me->bounds.size.h;
	graphics_context_set_fill_color(ctx, GColorBlack);
	graphics_fill_rect(ctx, GRect(0, t, 12, 12), 0, GCornersAll);
	//graphics_context_set_fill_color(ctx, GColorWhite);

#if 1
	pixel_t temp_pixels[ARRAY_COUNT(pixels)];
	wireframe_draw(
		ctx,
		&camera,
		ARRAY_COUNT(vertices),
		vertices,
		ARRAY_COUNT(edges),
		edges,
		pixels,
		temp_pixels
	);
#else
	for (int i = 0 ; i < ARRAY_COUNT(vertices) ; i++)
	{
		const vertex_t * const v = &vertices[i];
		pixel_t * const p = &pixels[i];
		draw_pixel(COLOR_BLACK24, p->x, p->y);
		camera_project(&camera, v, p);
		draw_pixel(COLOR_GREEN, p->x, p->y);
	}
#endif
}


static void
handle_tick(
	AppContextRef ctx,
	PebbleTickEvent * const event
)
{
	(void) ctx;
	const PblTm * const ptm = event->tick_time;
	static char time_buffer[64];
	unsigned off = 0;

/*
	for (int i = 0 ; i < 8 ; i ++)
	{
		off += fmt_i32(time_buffer + off, sin_lookup(8192*i), 10);
		time_buffer[off++] = '\n';
	}
*/

	string_format_time(
		time_buffer + off,
		sizeof(time_buffer) - off,
		"%H:%M:%S",
		ptm
	);

	text_layer_set_text(&time_layer, time_buffer);
	layer_mark_dirty(&layer);
}


static void
handle_init(
	AppContextRef ctx
)
{
	(void) ctx;

	window_init(&window, "Main");
	window_stack_push(&window, true);

	text_layer_init(&time_layer, GRect(0,0,144,168));
	text_layer_set_text(&time_layer, "");
	text_layer_set_font(&time_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18));
	layer_add_child(&window.layer, &time_layer.layer);

	// Bargraph layer is across the bottom
	layer_init(&layer, GRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT));
	layer.update_proc = layer_update;

	layer_add_child(&window.layer, &layer);
}


void
pbl_main(
	void * const params
)
{
	PebbleAppHandlers handlers = {
		.init_handler	= &handle_init,
		.tick_info	= {
			.tick_handler = &handle_tick,
			.tick_units = SECOND_UNIT,
		},
	};

	app_event_loop(params, &handlers);
}