From bf1595544d4a9f15e31798f4a1d07c2e1f7c6bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Sat, 2 Nov 2019 00:55:05 +0100 Subject: [PATCH] Initial commit, Scrolling text_layer --- .gitignore | 1 + .lock-waf_linux2_build | 8 +++ .vscode/c_cpp_properties.json | 17 +++++ .vscode/settings.json | 5 ++ package.json | 31 +++++++++ src/c/bustimes.c | 124 ++++++++++++++++++++++++++++++++++ src/pkjs/index.js | 123 +++++++++++++++++++++++++++++++++ wscript | 54 +++++++++++++++ 8 files changed, 363 insertions(+) create mode 100644 .gitignore create mode 100644 .lock-waf_linux2_build create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/settings.json create mode 100644 package.json create mode 100644 src/c/bustimes.c create mode 100644 src/pkjs/index.js create mode 100644 wscript diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/.lock-waf_linux2_build b/.lock-waf_linux2_build new file mode 100644 index 0000000..af8e01d --- /dev/null +++ b/.lock-waf_linux2_build @@ -0,0 +1,8 @@ +argv = ['/sdk/.pebble-sdk/SDKs/current/sdk-core/pebble/waf', 'configure'] +environ = {'PEBBLE_TOOLCHAIN_PATH': '/sdk/arm-cs-tools/bin', 'TERM': 'xterm', 'SHLVL': '2', 'PHONESIM_PATH': '/sdk/.env//bin/pypkjs', 'HOSTNAME': 'd40507156f4d', 'PYTHONHOME': '/sdk/.pebble-sdk/SDKs/current/sdk-core/../.env', 'NODE_PATH': '/sdk/.pebble-sdk/SDKs/current/sdk-core/../node_modules', 'PWD': '/work', 'NO_GCE_CHECK': 'False', 'SDK_VERSION': '4.3', 'PATH': '/sdk/arm-cs-tools/bin:/sdk/bin:/sdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'HOME': '/sdk', 'DISPLAY': ':1', '_': '/sdk/.env/bin/python', 'NOCLIMB': '1'} +files = ['/work/wscript'] +hash = 6211277306917996440 +options = {'files': '', 'jobs': 4, 'verbose': 0, 'nocache': False, 'progress_bar': 0, 'no_groups': False, 'distcheck_args': None, 'top': '', 'sandbox': False, 'destdir': '', 'keep': 0, 'zones': '', 'debug': False, 'prefix': '/usr/local/', 'timestamp': None, 'download': False, 'force': False, 'targets': '', 'out': ''} +out_dir = '/work/build' +run_dir = '/work' +top_dir = '/work' diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..14c4288 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/home/phillipk/.pebble-sdk/SDKs/4.3/sdk-core/pebble/basalt/include" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a5f494 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "pebble.h": "c" + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..745fc55 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "capabilities": ["location"], + "name": "bustimes", + "author": "Phillip Kühne", + "version": "1.0.0", + "keywords": ["pebble-app"], + "private": true, + "dependencies": {}, + "pebble": { + "displayName": "bustimes", + "uuid": "8138e7a1-58ef-49d9-8572-6c00c46baa67", + "sdkVersion": "3", + "enableMultiJS": true, + "targetPlatforms": [ + "basalt", + "chalk", + "diorite" + ], + "watchapp": { + "watchface": false + }, + "messageKeys": [ + "NAME", + "TIMES", + "NUM" + ], + "resources": { + "media": [] + } + } +} diff --git a/src/c/bustimes.c b/src/c/bustimes.c new file mode 100644 index 0000000..8572abd --- /dev/null +++ b/src/c/bustimes.c @@ -0,0 +1,124 @@ +#include + +static Window *s_main_window; +static TextLayer *s_name_header_layer; +static ScrollLayer *s_scroll_layer; +static TextLayer *s_times_layer; +Layer *window_layer; + + +static void main_window_load(Window *window) { + // Get information about the Window + window_layer = window_get_root_layer(window); + GRect bounds = layer_get_bounds(window_layer); + GRect max_text_bounds = GRect(0, 0, bounds.size.w, 2000); + + // Create the TextLayer with specific bounds + s_name_header_layer = text_layer_create( + GRect(0, 0, bounds.size.w, 24)); + + + + + s_times_layer = text_layer_create(max_text_bounds); + + // Initialize the scroll layer + s_scroll_layer = scroll_layer_create( + GRect(0, 24, bounds.size.w, bounds.size.h-24 )); + + scroll_layer_set_click_config_onto_window(s_scroll_layer, window); + + // Improve the layout to be more like a watchface + text_layer_set_background_color(s_name_header_layer, GColorDarkGreen); + text_layer_set_text_color(s_name_header_layer, GColorWhite); + text_layer_set_text(s_name_header_layer, "Suche..."); + text_layer_set_font(s_name_header_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD)); + text_layer_set_text_alignment(s_name_header_layer, GTextAlignmentCenter); + + // Improve the layout to be more like a watchface + text_layer_set_background_color(s_times_layer, GColorWhite); + text_layer_set_text_color(s_times_layer, GColorBlack); + text_layer_set_text(s_times_layer, ""); + text_layer_set_font(s_times_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD)); + text_layer_set_text_alignment(s_times_layer, GTextAlignmentLeft); + + + + // Add it as a child layer to the Window's root layer + layer_add_child(window_layer, text_layer_get_layer(s_name_header_layer)); + +} + + +static void main_window_unload(Window *window) { + // Destroy TextLayer + text_layer_destroy(s_name_header_layer); + text_layer_destroy(s_times_layer); + scroll_layer_destroy(s_scroll_layer); + +} + +static void inbox_received_callback(DictionaryIterator *iterator, void *context) { + // Store incoming information + static char name_buffer[50]; + static char times_buffer[512]; + // Read tuples for data + Tuple *name_tuple = dict_find(iterator, MESSAGE_KEY_NAME); + Tuple *times_tuple = dict_find(iterator, MESSAGE_KEY_TIMES); + snprintf(name_buffer, sizeof(name_buffer), "%s", name_tuple->value->cstring); + snprintf(times_buffer, sizeof(times_buffer), "%s", times_tuple->value->cstring); + text_layer_set_text(s_name_header_layer, name_buffer); + text_layer_set_text(s_times_layer, times_buffer); + + GRect bounds = layer_get_bounds(window_layer); + GSize max_size = text_layer_get_content_size(s_times_layer); + text_layer_set_size(s_times_layer, max_size); + scroll_layer_set_content_size(s_scroll_layer, GSize(bounds.size.w, max_size.h + 4)); + layer_add_child(window_layer, scroll_layer_get_layer(s_scroll_layer)); + scroll_layer_add_child(s_scroll_layer, text_layer_get_layer(s_times_layer)); +} + +static void inbox_dropped_callback(AppMessageResult reason, void *context) { + APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped!"); +} + +static void outbox_failed_callback(DictionaryIterator *iterator, AppMessageResult reason, void *context) { + APP_LOG(APP_LOG_LEVEL_ERROR, "Outbox send failed!"); +} + +static void outbox_sent_callback(DictionaryIterator *iterator, void *context) { + APP_LOG(APP_LOG_LEVEL_INFO, "Outbox send success!"); +} + +static void init() { + // Create main Window element and assign to pointer + s_main_window = window_create(); + + // Set handlers to manage the elements inside the Window + window_set_window_handlers(s_main_window, (WindowHandlers) { + .load = main_window_load, + .unload = main_window_unload + }); + + // Show the Window on the watch, with animated=true + window_stack_push(s_main_window, true); + // Register callbacks + app_message_register_inbox_received(inbox_received_callback); + // Open AppMessage + const int inbox_size = 512; + const int outbox_size = 512; + app_message_open(inbox_size, outbox_size); + app_message_register_inbox_dropped(inbox_dropped_callback); + app_message_register_outbox_failed(outbox_failed_callback); + app_message_register_outbox_sent(outbox_sent_callback); +} + +static void deinit() { + window_destroy(s_main_window); +} + +int main(void) { + init(); + app_event_loop(); + deinit(); +} diff --git a/src/pkjs/index.js b/src/pkjs/index.js new file mode 100644 index 0000000..4582682 --- /dev/null +++ b/src/pkjs/index.js @@ -0,0 +1,123 @@ +function cutPadString(string, length) { + let outstring = "" + if (string.length > length) { + outstring = string.substring(0, length - 3) + "..." + } else { + if (string.length < length) { + outstring = string.padEnd(length) + } else { + outstring = string + } + } + return outstring +} + +function getTime(stringTime) { + let d = new Date(stringTime) + return String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0") +} + +function diff_minutes(date) { + + dt = new Date(date); + + var diff = (dt.getTime() - Date.now()) / 1000; + diff /= 60; + return Math.abs(Math.round(diff)); + +} + +function locationSuccess(pos) { + console.log("Location Found!") + // Construct URL + var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance + //var url = 'https://simple-hafas.phillipathome.dynv6.net' + var url = 'http://192.168.2.101:3030' + // Send request to OpenWeatherMap + xmlhttp.open("POST", url,true); + xmlhttp.setRequestHeader("Content-Type", "application/json"); + xmlhttp.onreadystatechange = function () { + if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { + var json = JSON.parse(xmlhttp.responseText); + var timesString = ""; + json.departures.forEach(departure => { + let delay = "" + if (departure.delay != 0) { + if (departure.delay>0) { + delay = "(+" + departure.delay + ")" + } else { + delay = "(" + departure.delay + ")" + + } + } + + + let timePart = "" + if (diff_minutes(departure.time)>15) { + timePart = getTime(departure.time) + } else { + timePart = diff_minutes(departure.time) + "min" + } + + timesString += (cutPadString(departure.line, 4) + " " + cutPadString(departure.direction, 14) + "\n" + cutPadString(timePart, 6) + delay + "\n-----------------------\n") + }); + + + // Assemble dictionary using our keys + var dictionary = { + 'NAME': json.name, + 'NUM' : json.departures.length, + 'TIMES': timesString, + }; + + // Send to Pebble + Pebble.sendAppMessage(dictionary, + function (e) { + console.log('Departure times sent to Pebble successfully!'); + }, + function (e) { + console.log('Error sending Departure times to Pebble!'); + } + ); + } + // Listen for when an AppMessage is received + Pebble.addEventListener('appmessage', + function (e) { + console.log('AppMessage received!'); + getTimes(); + } + ); + }; + xmlhttp.send(JSON.stringify({ + "lat": pos.coords.latitude, + "lon": pos.coords.longitude + })); + +} + + + + +function locationError(err) { + console.log('Error requesting location!'); +} + +function getTimes() { + navigator.geolocation.getCurrentPosition( + locationSuccess, + locationError, { + timeout: 15000, + maximumAge: 60000 + } + ); +} + +// Listen for when the watchface is opened +Pebble.addEventListener('ready', + function (e) { + console.log('PebbleKit JS ready!'); + + // Get the initial data + getTimes(); + } +); \ No newline at end of file diff --git a/wscript b/wscript new file mode 100644 index 0000000..5238bc8 --- /dev/null +++ b/wscript @@ -0,0 +1,54 @@ +# +# This file is the default set of rules to compile a Pebble application. +# +# Feel free to customize this to your needs. +# +import os.path + +top = '.' +out = 'build' + + +def options(ctx): + ctx.load('pebble_sdk') + + +def configure(ctx): + """ + This method is used to configure your build. ctx.load(`pebble_sdk`) automatically configures + a build for each valid platform in `targetPlatforms`. Platform-specific configuration: add your + change after calling ctx.load('pebble_sdk') and make sure to set the correct environment first. + Universal configuration: add your change prior to calling ctx.load('pebble_sdk'). + """ + ctx.load('pebble_sdk') + + +def build(ctx): + ctx.load('pebble_sdk') + + build_worker = os.path.exists('worker_src') + binaries = [] + + cached_env = ctx.env + for platform in ctx.env.TARGET_PLATFORMS: + ctx.env = ctx.all_envs[platform] + ctx.set_group(ctx.env.PLATFORM_NAME) + app_elf = '{}/pebble-app.elf'.format(ctx.env.BUILD_DIR) + ctx.pbl_build(source=ctx.path.ant_glob('src/c/**/*.c'), target=app_elf, bin_type='app') + + if build_worker: + worker_elf = '{}/pebble-worker.elf'.format(ctx.env.BUILD_DIR) + binaries.append({'platform': platform, 'app_elf': app_elf, 'worker_elf': worker_elf}) + ctx.pbl_build(source=ctx.path.ant_glob('worker_src/c/**/*.c'), + target=worker_elf, + bin_type='worker') + else: + binaries.append({'platform': platform, 'app_elf': app_elf}) + ctx.env = cached_env + + ctx.set_group('bundle') + ctx.pbl_bundle(binaries=binaries, + js=ctx.path.ant_glob(['src/pkjs/**/*.js', + 'src/pkjs/**/*.json', + 'src/common/**/*.js']), + js_entry_file='src/pkjs/index.js')