Source

pebble / cube / src / cube.c

Full commit
/** \file
 * Show off the 3D accelerometer data.
 */
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"

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


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

static void
bargraph_layer_update(
	Layer * const me,
	GContext * ctx
)
{
/*
	// Draw a white rectangle across the entire bottom
	const unsigned w = me->bounds.size.w;
	const unsigned h = me->bounds.size.h;
	graphics_context_set_fill_color(ctx, GColorWhite);
	graphics_fill_rect(ctx, GRect(0, 0, w, h), 0, GCornersAll);

	graphics_context_set_fill_color(ctx, GColorBlack);
	graphics_fill_rect(ctx, GRect(0, 0, (w * remaining) / 30, h), 4, GCornersRight);
*/
}


static unsigned
fmt_i16(
	char * buf,
	int16_t v_in
)
{
	uint16_t v = v_in;

	if (v_in < 0)
	{
		buf[0] = '-';
		v = -v_in;
	} else
	if (v_in > 0)
		buf[0] = '+';
	else
		buf[0] = ' ';

	buf[5] = '0' + v % 10; v /= 10;
	buf[4] = '0' + v % 10; v /= 10;
	buf[3] = '0' + v % 10; v /= 10;
	buf[2] = '0' + v % 10; v /= 10;
	buf[1] = '0' + v % 10;

	return 6;
}


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

	struct AccelData accel = { 12345, 9999, -9999 };
	accel_get_xyz(&accel);
	off += fmt_i16(time_buffer + off, accel.x);
	time_buffer[off++] = '\n';
	off += fmt_i16(time_buffer + off, accel.y);
	time_buffer[off++] = '\n';
	off += fmt_i16(time_buffer + off, accel.z);
	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);
}


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
	const unsigned bargraph_height = 20;
	layer_init(&bargraph_layer, GRect(0,168-bargraph_height-15,144,bargraph_height));
	bargraph_layer.update_proc = bargraph_layer_update;

	layer_add_child(&window.layer, &bargraph_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);
}