From 0daea60bc5fe54159b7baf44a962e91376be3b27 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 9 May 2015 01:24:23 +0200 Subject: [PATCH 1/2] Added ability to use custom receive handler Added ability to use custom receive handler to receive custom messages in own app --- appinfo.json | 52 +++++++++++++++++++++++++--------------------- src/netdownload.c | 13 +++++++----- src/netdownload.h | 6 +++++- src/pebble-faces.c | 14 +++++++++++-- 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/appinfo.json b/appinfo.json index c59d6dc..595308d 100644 --- a/appinfo.json +++ b/appinfo.json @@ -1,26 +1,30 @@ { - "uuid": "9f1e3aed-98f8-41ec-9bff-2c15fa4f3c24", - "shortName": "PbFaces", - "longName": "Pebble Faces", - "companyName": "MakeAwesomeHappen", - "versionCode": 3, - "versionLabel": "1.2", - "sdkVersion": "3", - "targetPlatforms": [ - "aplite", - "basalt" - ], - "watchapp": { - "watchface": false - }, - "appKeys": { - "NETDL_DATA": 1768777472, - "NETDL_BEGIN": 1768777473, - "NETDL_END": 1768777474, - "NETDL_CHUNK_SIZE": 1768777475, - "NETDL_URL": 1768777476 - }, - "resources": { - "media": [] - } + "appKeys": { + "NETDL_BEGIN": 1768777473, + "NETDL_CHUNK_SIZE": 1768777475, + "NETDL_DATA": 1768777472, + "NETDL_END": 1768777474, + "NETDL_URL": 1768777476 + }, + "capabilities": [ + "" + ], + "companyName": "MakeAwesomeHappen", + "longName": "Pebble Faces", + "projectType": "native", + "resources": { + "media": [] + }, + "sdkVersion": "3", + "shortName": "PbFaces", + "targetPlatforms": [ + "aplite", + "basalt" + ], + "uuid": "9f1e3aed-98f8-41ec-9bff-2c15fa4f3c24", + "versionCode": 3, + "versionLabel": "1.2", + "watchapp": { + "watchface": false + } } diff --git a/src/netdownload.c b/src/netdownload.c index 8d9bc62..5a4c6c8 100644 --- a/src/netdownload.c +++ b/src/netdownload.c @@ -1,12 +1,13 @@ #include "netdownload.h" -NetDownloadContext* netdownload_create_context(NetDownloadCallback callback) { +NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback) { NetDownloadContext *ctx = malloc(sizeof(NetDownloadContext)); ctx->length = 0; ctx->index = 0; ctx->data = NULL; ctx->callback = callback; + ctx->custom_handler_callback = custom_handler_callback; return ctx; } @@ -18,8 +19,8 @@ void netdownload_destroy_context(NetDownloadContext *ctx) { free(ctx); } -void netdownload_initialize(NetDownloadCallback callback) { - NetDownloadContext *ctx = netdownload_create_context(callback); +void netdownload_initialize(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback) { + NetDownloadContext *ctx = netdownload_create_context(callback, custom_handler_callback); APP_LOG(APP_LOG_LEVEL_DEBUG, "NetDownloadContext = %p", ctx); app_message_set_context(ctx); @@ -27,7 +28,7 @@ void netdownload_initialize(NetDownloadCallback callback) { app_message_register_inbox_dropped(netdownload_dropped); app_message_register_outbox_sent(netdownload_out_success); app_message_register_outbox_failed(netdownload_out_failed); - APP_LOG(APP_LOG_LEVEL_DEBUG, "Max buffer sizes are %li / %li", app_message_inbox_size_maximum(), app_message_outbox_size_maximum()); + APP_LOG(APP_LOG_LEVEL_DEBUG, "Max buffer sizes are in:%li / out:%li", app_message_inbox_size_maximum(), app_message_outbox_size_maximum()); app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum()); } @@ -111,7 +112,9 @@ void netdownload_receive(DictionaryIterator *iter, void *context) { } break; default: - APP_LOG(APP_LOG_LEVEL_WARNING, "Unknown key in dict: %lu", tuple->key); + APP_LOG(APP_LOG_LEVEL_WARNING, "Unknown key in dict: %lu, forwarding to custom handler", tuple->key); + if (ctx->custom_handler_callback != NULL) + ctx->custom_handler_callback(iter, context); break; } } diff --git a/src/netdownload.h b/src/netdownload.h index d354106..ab4184c 100644 --- a/src/netdownload.h +++ b/src/netdownload.h @@ -1,3 +1,4 @@ +#pragma once #include /* The key used to transmit download data. Contains byte array. */ @@ -20,6 +21,7 @@ typedef struct { } NetDownload; typedef void (*NetDownloadCallback)(NetDownload *image); +typedef void (*CustomReceivedHandler)(DictionaryIterator *received, void *context); typedef struct { /* size of the data buffer allocated */ @@ -30,9 +32,11 @@ typedef struct { uint32_t index; /* Callback to call when we are done loading the data */ NetDownloadCallback callback; + /* Callback to call when received any other messages */ + CustomReceivedHandler custom_handler_callback; } NetDownloadContext; -NetDownloadContext* netdownload_create_context(NetDownloadCallback callback); +NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback); void netdownload_initialize(); void netdownload_deinitialize(); diff --git a/src/pebble-faces.c b/src/pebble-faces.c index 9e1c42f..c928747 100644 --- a/src/pebble-faces.c +++ b/src/pebble-faces.c @@ -68,7 +68,7 @@ void download_complete_handler(NetDownload *download) { printf("Heap free is %u bytes", heap_bytes_free()); #ifdef PBL_PLATFORM_APLITE - GBitmap *bmp = gbitmap_create_with_png_data(download->data, download->length); + GBitmap *bmp = gbitmap_create_with_png_data(download->data, download->length); #else GBitmap *bmp = gbitmap_create_from_png_data(download->data, download->length); #endif @@ -95,10 +95,19 @@ void tap_handler(AccelAxisType accel, int32_t direction) { show_next_image(); } +static void down_click_handler(ClickRecognizerRef recognizer, void *context) { + show_next_image(); +} + +static void click_config_provider(void *context) { + // Register the ClickHandlers + window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler); +} + static void init(void) { // Need to initialize this first to make sure it is there when // the window_load function is called by window_stack_push. - netdownload_initialize(download_complete_handler); + netdownload_initialize(download_complete_handler, NULL); window = window_create(); #ifdef PBL_SDK_2 @@ -109,6 +118,7 @@ static void init(void) { .unload = window_unload, }); const bool animated = true; + window_set_click_config_provider(window, click_config_provider); window_stack_push(window, animated); accel_tap_service_subscribe(tap_handler); From e5beb761ab43037fdfb4b93332b527889c9e7347 Mon Sep 17 00:00:00 2001 From: Jack Date: Thu, 14 May 2015 23:09:25 +0200 Subject: [PATCH 2/2] added error callback added ability for an error callback, so we can act if an error occurs --- appinfo.json | 1 - src/netdownload.c | 13 ++++++++++--- src/netdownload.h | 5 ++++- src/pebble-faces.c | 4 ++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/appinfo.json b/appinfo.json index 595308d..5a1d6cc 100644 --- a/appinfo.json +++ b/appinfo.json @@ -22,7 +22,6 @@ "basalt" ], "uuid": "9f1e3aed-98f8-41ec-9bff-2c15fa4f3c24", - "versionCode": 3, "versionLabel": "1.2", "watchapp": { "watchface": false diff --git a/src/netdownload.c b/src/netdownload.c index 5a4c6c8..db774a3 100644 --- a/src/netdownload.c +++ b/src/netdownload.c @@ -1,6 +1,6 @@ #include "netdownload.h" -NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback) { +NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback, CustomErrorHandler custom_error_callback) { NetDownloadContext *ctx = malloc(sizeof(NetDownloadContext)); ctx->length = 0; @@ -8,6 +8,7 @@ NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, Cus ctx->data = NULL; ctx->callback = callback; ctx->custom_handler_callback = custom_handler_callback; + ctx->custom_error_callback = custom_error_callback; return ctx; } @@ -19,8 +20,8 @@ void netdownload_destroy_context(NetDownloadContext *ctx) { free(ctx); } -void netdownload_initialize(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback) { - NetDownloadContext *ctx = netdownload_create_context(callback, custom_handler_callback); +void netdownload_initialize(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback, CustomErrorHandler custom_error_callback) { + NetDownloadContext *ctx = netdownload_create_context(callback, custom_handler_callback, custom_error_callback); APP_LOG(APP_LOG_LEVEL_DEBUG, "NetDownloadContext = %p", ctx); app_message_set_context(ctx); @@ -140,7 +141,10 @@ char *translate_error(AppMessageResult result) { } void netdownload_dropped(AppMessageResult reason, void *context) { + NetDownloadContext *ctx = (NetDownloadContext*) context; APP_LOG(APP_LOG_LEVEL_ERROR, "Dropped message! Reason given: %s", translate_error(reason)); + if (ctx->custom_error_callback != NULL) + ctx->custom_error_callback(NULL, reason, context); } void netdownload_out_success(DictionaryIterator *iter, void *context) { @@ -148,6 +152,9 @@ void netdownload_out_success(DictionaryIterator *iter, void *context) { } void netdownload_out_failed(DictionaryIterator *iter, AppMessageResult reason, void *context) { + NetDownloadContext *ctx = (NetDownloadContext*) context; APP_LOG(APP_LOG_LEVEL_DEBUG, "Failed to send message. Reason = %s", translate_error(reason)); + if (ctx->custom_error_callback != NULL) + ctx->custom_error_callback(iter, reason, context); } diff --git a/src/netdownload.h b/src/netdownload.h index ab4184c..3bff859 100644 --- a/src/netdownload.h +++ b/src/netdownload.h @@ -22,6 +22,7 @@ typedef struct { typedef void (*NetDownloadCallback)(NetDownload *image); typedef void (*CustomReceivedHandler)(DictionaryIterator *received, void *context); +typedef void (*CustomErrorHandler)(DictionaryIterator *iter, AppMessageResult reason, void *context); typedef struct { /* size of the data buffer allocated */ @@ -34,9 +35,11 @@ typedef struct { NetDownloadCallback callback; /* Callback to call when received any other messages */ CustomReceivedHandler custom_handler_callback; + /* Callback to call when any error occurs */ + CustomErrorHandler custom_error_callback; } NetDownloadContext; -NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback); +NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback, CustomErrorHandler custom_error_callback); void netdownload_initialize(); void netdownload_deinitialize(); diff --git a/src/pebble-faces.c b/src/pebble-faces.c index c928747..0a2df74 100644 --- a/src/pebble-faces.c +++ b/src/pebble-faces.c @@ -16,7 +16,7 @@ static char *images[] = { "http://assets.getpebble.com.s3-website-us-east-1.amazonaws.com/pebble-faces/thomas.png", "http://assets.getpebble.com.s3-website-us-east-1.amazonaws.com/pebble-faces/matt.png", "http://assets.getpebble.com.s3-website-us-east-1.amazonaws.com/pebble-faces/katharine.png", - "http://assets.getpebble.com.s3-website-us-east-1.amazonaws.com/pebble-faces/katherine.png", + "http://assets.getpebble.com.s3-website-us-east-1.amazonaws.com/pebble-faces/katherinexxx.png", "http://assets.getpebble.com.s3-website-us-east-1.amazonaws.com/pebble-faces/alex.png", "http://assets.getpebble.com.s3-website-us-east-1.amazonaws.com/pebble-faces/lukasz.png" }; @@ -107,7 +107,7 @@ static void click_config_provider(void *context) { static void init(void) { // Need to initialize this first to make sure it is there when // the window_load function is called by window_stack_push. - netdownload_initialize(download_complete_handler, NULL); + netdownload_initialize(download_complete_handler, NULL, NULL); window = window_create(); #ifdef PBL_SDK_2