Works on all three platforms

This commit is contained in:
Neale Pickett 2015-12-26 19:52:43 -07:00
parent af1cb03d36
commit 68b211ae5e
7 changed files with 356 additions and 0 deletions

46
appinfo.json Normal file
View File

@ -0,0 +1,46 @@
{
"appKeys": {},
"capabilities": [
""
],
"companyName": "dartcatcher@gmail.com",
"longName": "Twatch Rings",
"projectType": "native",
"resources": {
"media": [
{
"characterRegex": "[0-9]",
"file": "fonts/Helvetica-Regular.ttf",
"name": "FONT_24",
"targetPlatforms": null,
"type": "font"
},
{
"file": "images/menu.png",
"menuIcon": true,
"name": "MENU_IMAGE",
"targetPlatforms": null,
"type": "png"
},
{
"characterRegex": "[0-9]",
"file": "fonts/Helvetica-Bold.ttf",
"name": "FONT_36",
"targetPlatforms": null,
"type": "font"
}
]
},
"sdkVersion": "3",
"shortName": "Twatch Rings",
"targetPlatforms": [
"aplite",
"basalt",
"chalk"
],
"uuid": "029c47c0-f364-4d8f-bf66-abd35a9008f8",
"versionLabel": "1.0",
"watchapp": {
"watchface": true
}
}

Binary file not shown.

Binary file not shown.

BIN
resources/images/menu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 463 B

206
src/main.c Normal file
View File

@ -0,0 +1,206 @@
#include <pebble.h>
#define MINLEN 58
#define HRCIRCLER 30
#define NRINGS 12
#define MINCIRCLER 20
#define fatness 14
#define fat(x) (fatness * x)
static Window *window;
static Layer *s_hr_layer, *s_min_layer, *s_sec_layer;
GRect display_bounds;
GPoint center, mincenter;
int32_t min_angle, sec_angle;
char hrstr[3], minstr[3];
bool min_even;
bool bt_connected;
#define nringcolors 5
GColor *rings[nringcolors] = {
&GColorVividCerulean,
&GColorChromeYellow,
&GColorCobaltBlue,
&GColorDarkGray,
&GColorWhite,
};
static void min_update_proc(Layer *layer, GContext *ctx) {
graphics_context_set_fill_color(ctx, *rings[0]);
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
for (int i = 0; i < NRINGS; i += 1) {
graphics_context_set_fill_color(ctx, *rings[(NRINGS - i) % nringcolors]);
graphics_fill_circle(ctx, mincenter, MINCIRCLER + (NRINGS - i) * fatness);
}
// center dot
if (bt_connected) {
graphics_context_set_fill_color(ctx, GColorBlack);
graphics_context_set_text_color(ctx, GColorWhite);
} else {
graphics_context_set_fill_color(ctx, GColorYellow);
graphics_context_set_text_color(ctx, GColorBlack);
}
graphics_fill_circle(ctx, mincenter, MINCIRCLER);
GRect textbox = {
.origin = {
.x = mincenter.x - 20,
.y = mincenter.y - 16,
},
.size = {
.w = 40,
.h = 40,
}
};
graphics_draw_text(ctx, minstr,
fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD),
textbox,
GTextOverflowModeWordWrap,
GTextAlignmentCenter,
NULL);
}
static void hr_update_proc(Layer *layer, GContext *ctx) {
// Draw a black circle
graphics_context_set_fill_color(ctx, GColorBlack);
graphics_fill_circle(ctx, center, HRCIRCLER);
// Write the current hour text in it
GRect textbox = {
.origin = {
.x = center.x - HRCIRCLER,
.y = center.y - HRCIRCLER + 4,
},
.size = {
.w = HRCIRCLER * 2,
.h = HRCIRCLER,
}
};
graphics_context_set_text_color(ctx, GColorWhite);
graphics_draw_text(ctx, (*hrstr=='0')?(hrstr+1):(hrstr),
fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD),
textbox,
GTextOverflowModeWordWrap,
GTextAlignmentCenter,
NULL);
}
static void sec_update_proc(Layer *layer, GContext *ctx) {
GRect secbox = {
.origin = {
.x = mincenter.x - (40+2)/2,
.y = mincenter.y - (40+2)/2,
},
.size = {
.w = 40+2,
.h = 40+2,
}
};
graphics_context_set_fill_color(ctx, GColorVividCerulean);
if (min_even) {
graphics_fill_radial(ctx, secbox, GOvalScaleModeFitCircle, 2, 0, sec_angle);
} else {
graphics_fill_radial(ctx, secbox, GOvalScaleModeFitCircle, 2, sec_angle, DEG_TO_TRIGANGLE(360));
}
}
static void handle_tick(struct tm *tick_time, TimeUnits units_changed) {
if (units_changed && HOUR_UNIT) {
if (clock_is_24h_style()) {
strftime(hrstr, sizeof(hrstr), "%H", tick_time);
} else {
strftime(hrstr, sizeof(hrstr), "%I", tick_time);
}
layer_mark_dirty(s_hr_layer);
}
if (units_changed && MINUTE_UNIT) {
strftime(minstr, sizeof(minstr), "%M", tick_time);
min_angle = DEG_TO_TRIGANGLE(tick_time->tm_min * 6);
mincenter.x = (int16_t)(sin_lookup(min_angle) * MINLEN / TRIG_MAX_RATIO) + center.x;
mincenter.y = (int16_t)(-cos_lookup(min_angle) * MINLEN / TRIG_MAX_RATIO) + center.y;
layer_mark_dirty(s_min_layer);
min_even = (tick_time->tm_min % 2 == 0);
}
if (units_changed && SECOND_UNIT) {
sec_angle = DEG_TO_TRIGANGLE(tick_time->tm_sec * 6);
layer_mark_dirty(s_sec_layer);
}
}
static void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
display_bounds = layer_get_bounds(window_layer);
center = grect_center_point(&display_bounds);
// Minutes
s_min_layer = layer_create(display_bounds);
layer_set_update_proc(s_min_layer, min_update_proc);
// Seconds
s_sec_layer = layer_create(display_bounds);
layer_set_update_proc(s_sec_layer, sec_update_proc);
// Hours
s_hr_layer = layer_create(display_bounds); // XXX: Perhaps this is too big.
layer_set_update_proc(s_hr_layer, hr_update_proc);
layer_add_child(window_layer, s_min_layer);
layer_add_child(window_layer, s_hr_layer);
layer_add_child(window_layer, s_sec_layer);
time_t now = time(NULL);
struct tm *tick_time = localtime(&now);
handle_tick(tick_time, HOUR_UNIT | MINUTE_UNIT | SECOND_UNIT);
}
static void window_unload(Window *window) {
layer_destroy(s_hr_layer);
layer_destroy(s_min_layer);
layer_destroy(s_sec_layer);
}
static void bt_handler(bool connected) {
bt_connected = connected;
if (! connected) {
vibes_double_pulse();
}
layer_mark_dirty(s_min_layer);
}
static void init() {
window = window_create();
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
window_stack_push(window, true);
tick_timer_service_subscribe(HOUR_UNIT | MINUTE_UNIT | SECOND_UNIT, handle_tick);
bluetooth_connection_service_subscribe(bt_handler);
bt_connected = bluetooth_connection_service_peek();
}
static void deinit() {
tick_timer_service_unsubscribe();
window_destroy(window);
}
int main() {
init();
app_event_loop();
deinit();
}

42
src/twatch.h Normal file
View File

@ -0,0 +1,42 @@
#include <pebble.h>
#pragma once
#define SECOND_LEN PBL_IF_ROUND_ELSE(77, 72)
#define SECOND_RADIUS 7
static const GPathInfo SECOND_HAND_POINTS = {
4,
(GPoint []) {
{2, -70},
{2, PBL_IF_ROUND_ELSE(-90, -85)},
{-2, PBL_IF_ROUND_ELSE(-90, -85)},
{-2, -70}
}
};
#define MINUTE_LEN PBL_IF_ROUND_ELSE(-81, -78)
static const GPathInfo MINUTE_HAND_POINTS = {
7,
(GPoint []) {
{4, 0},
{4, MINUTE_LEN},
{3, MINUTE_LEN - 2},
{0, MINUTE_LEN - 3},
{-3, MINUTE_LEN - 2},
{-4, MINUTE_LEN},
{-4, 0}
}
};
#define HOUR_LEN PBL_IF_ROUND_ELSE(-55, -53)
static const GPathInfo HOUR_HAND_POINTS = {
7,
(GPoint []) {
{5, 0},
{5, HOUR_LEN},
{4, HOUR_LEN -2},
{0, HOUR_LEN - 3},
{-4, HOUR_LEN - 2},
{-5, HOUR_LEN},
{-5, 0}
}
};

62
wscript Normal file
View File

@ -0,0 +1,62 @@
#
# This file is the default set of rules to compile a Pebble project.
#
# Feel free to customize this to your needs.
#
import os.path
try:
from sh import CommandNotFound, jshint, cat, ErrorReturnCode_2
hint = jshint
except (ImportError, CommandNotFound):
hint = None
top = '.'
out = 'build'
def options(ctx):
ctx.load('pebble_sdk')
def configure(ctx):
ctx.load('pebble_sdk')
def build(ctx):
if False and hint is not None:
try:
hint([node.abspath() for node in ctx.path.ant_glob("src/**/*.js")], _tty_out=False) # no tty because there are none in the cloudpebble sandbox.
except ErrorReturnCode_2 as e:
ctx.fatal("\nJavaScript linting failed (you can disable this in Project Settings):\n" + e.stdout)
# Concatenate all our JS files (but not recursively), and only if any JS exists in the first place.
ctx.path.make_node('src/js/').mkdir()
js_paths = ctx.path.ant_glob(['src/*.js', 'src/**/*.js'])
if js_paths:
ctx(rule='cat ${SRC} > ${TGT}', source=js_paths, target='pebble-js-app.js')
has_js = True
else:
has_js = False
ctx.load('pebble_sdk')
build_worker = os.path.exists('worker_src')
binaries = []
for p in ctx.env.TARGET_PLATFORMS:
ctx.set_env(ctx.all_envs[p])
ctx.set_group(ctx.env.PLATFORM_NAME)
app_elf='{}/pebble-app.elf'.format(p)
ctx.pbl_program(source=ctx.path.ant_glob('src/**/*.c'),
target=app_elf)
if build_worker:
worker_elf='{}/pebble-worker.elf'.format(p)
binaries.append({'platform': p, 'app_elf': app_elf, 'worker_elf': worker_elf})
ctx.pbl_worker(source=ctx.path.ant_glob('worker_src/**/*.c'),
target=worker_elf)
else:
binaries.append({'platform': p, 'app_elf': app_elf})
ctx.set_group('bundle')
ctx.pbl_bundle(binaries=binaries, js='pebble-js-app.js' if has_js else [])