Skip to content

Instantly share code, notes, and snippets.

@mddub

mddub/main.c

Created Aug 16, 2016
Embed
What would you like to do?
no-appmessage-timeout: Pebble app
#include <pebble.h>
static Window *s_window;
static TextLayer *s_text_layer;
static char s_status[256];
static void clear_status() {
static char buf[16];
time_t now = time(NULL);
strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&now));
strcpy(s_status, buf);
strcat(s_status, " sending...\n");
text_layer_set_text(s_text_layer, s_status);
}
static void append_status(const char * message, AppMessageResult result) {
strcat(s_status, message);
if (result != APP_MSG_OK) {
static char buf[16];
strcat(s_status, ": ");
snprintf(buf, sizeof(buf), "%d", result);
strcat(s_status, buf);
}
strcat(s_status, "\n");
text_layer_set_text(s_text_layer, s_status);
}
static void send_message() {
clear_status();
DictionaryIterator *iter;
AppMessageResult begin_result = app_message_outbox_begin(&iter);
if (begin_result == APP_MSG_OK) {
append_status("outbox_begin success", APP_MSG_OK);
} else {
append_status("outbox_begin failed", begin_result);
return;
}
AppMessageResult send_result = app_message_outbox_send();
if (send_result == APP_MSG_OK) {
append_status("outbox_send success", APP_MSG_OK);
} else {
append_status("outbox_send failed", send_result);
}
}
static void out_sent_handler(DictionaryIterator *iterator, void *context) {
append_status("out sent", APP_MSG_OK);
}
static void out_failed_handler(DictionaryIterator *iterator, AppMessageResult result, void *context) {
append_status("out failed", result);
}
static void in_received_handler(DictionaryIterator *received, void *context) {
append_status("in received", APP_MSG_OK);
}
static void in_dropped_handler(AppMessageResult result, void *context) {
append_status("in dropped", result);
}
static void init_app_message() {
app_message_register_outbox_sent(out_sent_handler);
app_message_register_outbox_failed(out_failed_handler);
app_message_register_inbox_received(in_received_handler);
app_message_register_inbox_dropped(in_dropped_handler);
app_message_open(APP_MESSAGE_INBOX_SIZE_MINIMUM, APP_MESSAGE_OUTBOX_SIZE_MINIMUM);
}
static void click_handler(ClickRecognizerRef recognizer, void *context) {
send_message();
}
static void click_config_provider(void *context) {
window_single_click_subscribe(BUTTON_ID_SELECT, click_handler);
window_single_click_subscribe(BUTTON_ID_UP, click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, click_handler);
}
static void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
s_text_layer = text_layer_create(GRect(0, 0, bounds.size.w, bounds.size.h));
text_layer_set_text(s_text_layer, "Press a button");
layer_add_child(window_layer, text_layer_get_layer(s_text_layer));
}
static void window_unload(Window *window) {
text_layer_destroy(s_text_layer);
}
static void init(void) {
s_window = window_create();
window_set_click_config_provider(s_window, click_config_provider);
window_set_window_handlers(s_window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
const bool animated = true;
window_stack_push(s_window, animated);
init_app_message();
}
static void deinit(void) {
window_destroy(s_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
{
"name": "no-appmessage-timeout",
"author": "Mark Wilson",
"version": "1.0.0",
"keywords": ["pebble-app"],
"private": true,
"dependencies": {},
"pebble": {
"displayName": "no-appmessage-timeout",
"uuid": "88789b03-ddc4-4218-9fcd-f12a499ca65f",
"sdkVersion": "3",
"enableMultiJS": true,
"targetPlatforms": [
"aplite",
"basalt",
"chalk"
],
"watchapp": {
"watchface": false
},
"messageKeys": [
"dummy"
],
"resources": {
"media": []
}
}
}
#
# 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_program(source=ctx.path.ant_glob('src/**/*.c'), target=app_elf)
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_worker(source=ctx.path.ant_glob('worker_src/**/*.c'), target=worker_elf)
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/js/**/*.js', 'src/js/**/*.json']),
js_entry_file='src/js/app.js')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment