diff --git a/appinfo.json b/appinfo.json new file mode 100644 index 0000000..ea240ad --- /dev/null +++ b/appinfo.json @@ -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 + } +} diff --git a/resources/fonts/Helvetica-Bold.ttf b/resources/fonts/Helvetica-Bold.ttf new file mode 100644 index 0000000..193b51e Binary files /dev/null and b/resources/fonts/Helvetica-Bold.ttf differ diff --git a/resources/fonts/Helvetica-Regular.ttf b/resources/fonts/Helvetica-Regular.ttf new file mode 100644 index 0000000..7a78c3e Binary files /dev/null and b/resources/fonts/Helvetica-Regular.ttf differ diff --git a/resources/images/menu.png b/resources/images/menu.png new file mode 100644 index 0000000..a50ca1c Binary files /dev/null and b/resources/images/menu.png differ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..65f6861 --- /dev/null +++ b/src/main.c @@ -0,0 +1,206 @@ +#include + + +#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(); +} diff --git a/src/twatch.h b/src/twatch.h new file mode 100644 index 0000000..691eb24 --- /dev/null +++ b/src/twatch.h @@ -0,0 +1,42 @@ +#include +#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} + } +}; diff --git a/wscript b/wscript new file mode 100644 index 0000000..b20f58f --- /dev/null +++ b/wscript @@ -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 []) + \ No newline at end of file