pebble / hoelaat / src / nederlands.c

/** \file
 * Word clock, with Dutch time.
 *
 * Four lines:
 *
 * minutes (or blank)
 * over/voor/half (or blank)
 * HOUR (in bold)
 * 's ochtends / 's middags 
 *
 * \todo: When any line changes, it is pushed to the side
 *
 * een uur
 * een over een
 * kwart over een
 * tien voor half twee
 * half twee
 * vijf over half twee
 * kwart voor twee
 * 
 * And 's ochtends, 's middags, 's avonds, 's nachts
 * vier uur
 * half vier 
 */
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"

static Window window;

static const char * min_text;
static const char * rel_text;
static const char * hour_text;
static const char * ampm_text;

static TextLayer min_layer;
static TextLayer rel_layer;
static TextLayer hour_layer;
static TextLayer ampm_layer;

static const char *nums[] = {
	"",
	"één",
	"twee",
	"drie",
	"vier",
	"vijf",
	"zes",
	"zeven",
	"acht",
	"negen",
	"tien",
	"elf",
	"twaalf",
	"dertien",
	"veertien",
	"kwart",
};

static const char *
min_string(
	int i
)
{
	return nums[i];
}


static const char *
hour_string(
	int h
)
{
	if (h == 0 || h == 24)
		return "middernacht";

	if (h == 12)
		return "middag";

	if (h < 12)
		return nums[h];
	else
		return nums[h - 12];
}


static void
nederlands_format(
	int hour,
	int min
)
{
	if (min == 0)
	{
		min_text = "";
		rel_text = "";
	} else
	if (min <= 15)
	{
		// over the hour
		min_text = min_string(min);
		rel_text = "over";
	} else
	if (min < 30)
	{
		// over the kwart
		min_text = min_string(30 - min);
		rel_text = "voor half";
		hour++;
	} else
	if (min == 30)
	{
		// just the half
		min_text = "";
		rel_text = "half";
		hour++;
	} else
	if (min < 45)
	{
		// over the half
		min_text = min_string(min - 30);
		rel_text = "over half";
		hour++;
	} else
	if (min == 45)
	{
		// just the kwart
		min_text = "";
		rel_text = "kwart voor";
		hour++;
	} else
	if (min < 60)
	{
		// over the kwart
		min_text = min_string(60 - min);
		rel_text = "voor";
		hour++;
	}

	hour_text = hour_string(hour);

	// add am/pm like phrase
	if (hour == 0 || hour == 12 || hour == 24)
	{
		// nothing to do
		ampm_text = "";
	} else
	if (hour <= 6)
		ampm_text = "'s nachts";
	else
	if (hour <= 12)
		ampm_text = "'s morgens";
	else
	if (hour <= 18)
		ampm_text = "'s middags";
	else
	if (hour <= 24)
		ampm_text = "'s avonds";
}


PBL_APP_INFO(
	"Hoe laat is het?",
	"hudson",
	1, // Version
	INVALID_RESOURCE,
	APP_INFO_WATCH_FACE
);



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

	int hour = ptm->tm_hour;
	int min = ptm->tm_min;

	const char * old_min = min_text;
	const char * old_rel = rel_text;
	const char * old_hour = hour_text;
	const char * old_ampm = ampm_text;

	nederlands_format(hour,  min);

/*
	string_format_time(
		time_buffer,
		sizeof(time_buffer),
		"%H:%M",
		event->tick_time
	);
*/

	text_layer_set_text(&ampm_layer, ampm_text);
	text_layer_set_text(&hour_layer, hour_text);
	text_layer_set_text(&rel_layer, rel_text);
	text_layer_set_text(&min_layer, min_text);
}


static void
text_layer(
	TextLayer * layer,
	GRect frame,
	int font
)
{
	text_layer_init(layer, frame);
	text_layer_set_text(layer, "");
	text_layer_set_text_color(layer, GColorWhite);
	text_layer_set_background_color(layer, GColorClear);
	text_layer_set_font(layer, fonts_load_custom_font(resource_get_handle(font)));

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


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

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

	resource_init_current_app(&HOELAAT_RESOURCES);

	int y = 15;
	int h = 30;

	// Stack top to bottom
	text_layer(&ampm_layer, GRect(4, y + 3*h, 144, h+8), RESOURCE_ID_FONT_ANDALE_28);
	text_layer(&hour_layer, GRect(4, y + 2*h, 144, h+8), RESOURCE_ID_FONT_ANDALE_30_BLACK);
	text_layer(&rel_layer, GRect(4, y + 1*h, 144, h+8), RESOURCE_ID_FONT_ANDALE_30);
	text_layer(&min_layer, GRect(4, y + 0*h, 144, h+8), RESOURCE_ID_FONT_ANDALE_30);
}


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

	app_event_loop(params, &handlers);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.