Source

pebble_from_hodson / interval / src / interval.c

Full commit
/** \file
 * Interval timer
 */
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"

#define UUID {0xB5, 0xF1, 0xBC, 0xE4, 0x7B, 0x0F, 0x47, 0xBB, 0xAC, 0x8B, 0x94, 0xA2, 0x50, 0xEF, 0xCD, 0x5F}

PBL_APP_INFO(
	UUID,
	"Intervals",
	"hudson",
	2, 0, // Version
	INVALID_RESOURCE,
	APP_INFO_WATCH_FACE // should be an APP
);


static const int intervals[] = {
	 -300,
	 60, -30,
	 60, -30,
	 60, -30,
	 60, -30,
	 60, -30,
	 60, -30,
	 60, -30,
	 60, -300,
};

static const int interval_count = sizeof(intervals)/sizeof(*intervals);
static const int run_count = 8;
static int interval = 0;
static int remaining = 0;
static int walk = 0;


static Window window;
static TextLayer time_layer;
static TextLayer remaining_layer;
static Layer layer;
static char time_buffer[12];
static char remaining_buffer[12];


/**
 * Draw the bargraphs for the running segments.
 * [++++++]  // run
 */
static void
layer_update(
	Layer * const me,
	GContext * ctx
)
{
	const unsigned w = me->bounds.size.w;
	const unsigned h = me->bounds.size.h;
	const int bar_h = h / run_count;
	int y = 0;

	graphics_context_set_fill_color(ctx, GColorWhite);

	for (int i = 0 ; i < interval_count ; i++)
	{
		if (intervals[i] < 0)
			continue;

		int bar_w;
		if (i < interval)
			bar_w = 0;
		else
		if (i == interval)
			bar_w = (w * remaining) / intervals[i];
		else
			bar_w = w;

		graphics_fill_rect(ctx, GRect(0, y, bar_w, bar_h - 2), 2, GCornersAll);
		y += bar_h;
	}
}


static void
handle_tick(
	AppContextRef ctx,
	PebbleTickEvent * const event
)
{
	(void) ctx;
	const PblTm * const ptm = event->tick_time;

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

	text_layer_set_text(&time_layer, time_buffer);

	if (remaining != 0)
		remaining--;
	else
	if (interval < interval_count-1)
	{
		remaining = intervals[++interval];
		if (remaining < 0)
		{
			remaining = -remaining;
			walk = 1;
			vibes_long_pulse();
		} else {
			walk = 0;
			vibes_double_pulse();
		}
	}

	int min = remaining / 60;
	int sec = remaining % 60;

	if (walk)
	{
		text_layer_set_text_color(&remaining_layer, GColorBlack);
		text_layer_set_background_color(&remaining_layer, GColorWhite);
	} else {
		text_layer_set_text_color(&remaining_layer, GColorWhite);
		text_layer_set_background_color(&remaining_layer, GColorClear);
	}

	remaining_buffer[0] = '0' + (min / 10) % 10;
	remaining_buffer[1] = '0' + (min / 1) % 10;
	remaining_buffer[2] = ':';
	remaining_buffer[3] = '0' + (sec / 10) % 10;
	remaining_buffer[4] = '0' + (sec / 1) % 10;
	remaining_buffer[5] = '\0';

	text_layer_set_text(&remaining_layer, remaining_buffer);

	layer_mark_dirty(&layer);
}


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

	window_init(&window, "Odometer");
	window_stack_push(&window, true);
	window_set_background_color(&window, GColorBlack);

	resource_init_current_app(&RESOURCES);

	text_layer_init(&time_layer, GRect(30,-3,100,30));
	text_layer_init(&remaining_layer, GRect(7,26,132,44));

	text_layer_set_text_color(&time_layer, GColorWhite);
        text_layer_set_background_color(&time_layer, GColorClear);

	text_layer_set_text(&time_layer, "");
	text_layer_set_text(&remaining_layer, "");

	text_layer_set_font(&time_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28));
	text_layer_set_font(&remaining_layer, fonts_get_system_font(FONT_KEY_GOTHAM_42_BOLD));

	// Layer to draw the bargraphs
	layer_add_child(&window.layer, &time_layer.layer);
	layer_add_child(&window.layer, &remaining_layer.layer);

	layer_init(&layer, GRect(0,70,144,168-70));
	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,
		},
	};

	interval = -1;
	remaining = 0;

	app_event_loop(params, &handlers);
}