Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to use custom receive handler #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions appinfo.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
{
"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",
"versionLabel": "1.2",
"watchapp": {
"watchface": false
}
}
20 changes: 15 additions & 5 deletions src/netdownload.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "netdownload.h"

NetDownloadContext* netdownload_create_context(NetDownloadCallback callback) {
NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback, CustomErrorHandler custom_error_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;
ctx->custom_error_callback = custom_error_callback;

return ctx;
}
Expand All @@ -18,16 +20,16 @@ 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, 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);

app_message_register_inbox_received(netdownload_receive);
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());
}

Expand Down Expand Up @@ -111,7 +113,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;
}
}
Expand All @@ -137,14 +141,20 @@ 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) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "Message sent.");
}

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);
}

9 changes: 8 additions & 1 deletion src/netdownload.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <pebble.h>

/* The key used to transmit download data. Contains byte array. */
Expand All @@ -20,6 +21,8 @@ typedef struct {
} NetDownload;

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 */
Expand All @@ -30,9 +33,13 @@ 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;
/* Callback to call when any error occurs */
CustomErrorHandler custom_error_callback;
} NetDownloadContext;

NetDownloadContext* netdownload_create_context(NetDownloadCallback callback);
NetDownloadContext* netdownload_create_context(NetDownloadCallback callback, CustomReceivedHandler custom_handler_callback, CustomErrorHandler custom_error_callback);

void netdownload_initialize();
void netdownload_deinitialize();
Expand Down
16 changes: 13 additions & 3 deletions src/pebble-faces.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
Expand Down Expand Up @@ -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
Expand All @@ -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, NULL);

window = window_create();
#ifdef PBL_SDK_2
Expand All @@ -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);
Expand Down