Skip to content

Instantly share code, notes, and snippets.

@ismaelc
Created September 2, 2014 22:48
Show Gist options
  • Select an option

  • Save ismaelc/4c2bcccbe1b8f9fa279b to your computer and use it in GitHub Desktop.

Select an option

Save ismaelc/4c2bcccbe1b8f9fa279b to your computer and use it in GitHub Desktop.
Pebble Concur Mojio demo (C file)
// Copyright (c) 2014 Gabi Zuniga
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#include <pebble.h>
// standard defines
//
#define WIDTH 144
#define HEIGHT 168
// settings structure
//
struct {
char title[40];
char measure[40];
bool enabled;
uint32_t period;
} settings;
// javascript keys
//
enum {
KEY_COMMAND = 0,
KEY_INDEX = 1,
KEY_TITLE = 2,
KEY_MEASURE = 3,
KEY_ENABLED = 4,
KEY_PERIOD = 5
};
enum {
COMMAND_FETCH = 0,
COMMAND_NEXT = 1,
COMMAND_PREV = 2,
COMMAND_TOGGLE = 3
};
Window *window;
Layer *window_layer;
TextLayer *timeLayer;
static int entryIndex = 0;
static uint32_t counter = 0;
static bool displayingTrip = false;
TextLayer *titleLayer;
TextLayer *measureLayer;
static void send_request(int command) {
Tuplet command_tuple = TupletInteger(KEY_COMMAND, command);
Tuplet index_tuple = TupletInteger(KEY_INDEX, entryIndex);
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
if (iter == NULL)
return;
dict_write_tuplet(iter, &command_tuple);
dict_write_tuplet(iter, &index_tuple);
dict_write_end(iter);
app_message_outbox_send();
}
// incoming javascript messages
//
void fetch_msg(DictionaryIterator *iter, void *context)
{
Tuple *title = dict_find(iter, KEY_TITLE); // First line text
Tuple *measure = dict_find(iter, KEY_MEASURE); // Second line text
Tuple *enabled = dict_find(iter, KEY_ENABLED); // Enable polling
Tuple *period = dict_find(iter, KEY_PERIOD); // Polling period
if (title) {
snprintf(settings.title, sizeof(settings.title), "%.40s", title->value->cstring);
}
if (enabled) {
settings.enabled = (enabled->value->uint32 != 0);
}
if (measure) {
snprintf(settings.measure, sizeof(settings.measure), "%.40s", measure->value->cstring);
vibes_long_pulse();
}
if (period) {
settings.period = period->value->uint32;
}
displayingTrip = true;
}
// set our layer attributes
//
void set_layer_attr(Layer *parentLayer, TextLayer *textlayer, GFont font, GTextAlignment align, GColor color)
{
text_layer_set_text_alignment(textlayer, align);
if (color == GColorWhite) {
text_layer_set_text_color(textlayer, GColorWhite);
text_layer_set_background_color(textlayer, GColorClear);
} else {
text_layer_set_text_color(textlayer, GColorBlack);
text_layer_set_background_color(textlayer, GColorClear);
}
text_layer_set_font(textlayer, fonts_get_system_font(font));
layer_add_child(parentLayer, text_layer_get_layer(textlayer));
}
void handle_second(struct tm *tm, TimeUnits units_changed)
{
layer_mark_dirty(window_layer);
if (units_changed == SECOND_UNIT
&& settings.enabled
&& settings.period
&& !(++counter % settings.period)) {
send_request(COMMAND_FETCH);
}
}
// handle every minute
//
void handle_minute(struct tm *tm, TimeUnits units_changed)
{
layer_mark_dirty(window_layer);
}
// display handler: Update Clock
//
void display_handler(Layer *me, GContext *ctx)
{
static char time_text[] = "00:00";
time_t now = time(NULL);
struct tm *tm = localtime(&now);
strftime(time_text, sizeof(time_text), clock_is_24h_style() ? "%H:%M" : "%l:%M", tm);
text_layer_set_text(timeLayer, time_text);
text_layer_set_text(titleLayer, settings.title);
text_layer_set_text(measureLayer, settings.measure);
int width = (int)((WIDTH-20)*((float)tm->tm_sec / 60));
graphics_context_set_fill_color(ctx, GColorWhite);
graphics_fill_rect(ctx, GRect(10, 105-2, WIDTH-20, 6+2), 3, GCornerBottomLeft | GCornerBottomRight );
}
// bottom layer handler
//
void tracking_layer_handler(Layer *me, GContext *ctx)
{
graphics_context_set_fill_color(ctx, GColorWhite);
graphics_fill_rect(ctx, GRect(10, 38, WIDTH-20, 105-38-2), 3, GCornerTopLeft | GCornerTopRight);
}
static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
// counter = 0;
displayingTrip = false;
send_request(COMMAND_FETCH);
snprintf(settings.measure, sizeof(settings.measure), "Tracking Trip");
layer_mark_dirty(window_layer);
}
static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
counter = 0;
if (!displayingTrip) return;
displayingTrip = false;
send_request(COMMAND_PREV);
snprintf(settings.measure, sizeof(settings.measure), "Trip Expensed");
layer_mark_dirty(window_layer);
}
static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
counter = 0;
if (!displayingTrip) return;
displayingTrip = false;
send_request(COMMAND_NEXT);
snprintf(settings.measure, sizeof(settings.measure), "Trip Dismissed");
layer_mark_dirty(window_layer);
}
static void click_config_provider(void *context) {
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
}
static void in_dropped_handler(AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "App Message Dropped!");
}
static void out_failed_handler(DictionaryIterator *failed, AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "App Message Failed to Send!");
}
static void window_load(Window *window) {
// Window Layer
window_layer = layer_create(layer_get_frame(window_get_root_layer(window)));
layer_add_child(window_get_root_layer(window), window_layer);
layer_set_update_proc(window_layer, display_handler);
// Tracking Layer
Layer *trackingLayer = layer_create(GRect(0,0,WIDTH,105));
layer_add_child(window_layer, trackingLayer);
layer_set_update_proc(trackingLayer, tracking_layer_handler);
// Time Layer
timeLayer = text_layer_create(GRect(0,107, WIDTH,50));
set_layer_attr(window_layer, timeLayer, FONT_KEY_ROBOTO_BOLD_SUBSET_49, GTextAlignmentCenter, GColorWhite);
// Title Layer (1st row)
titleLayer = text_layer_create(GRect(0,5,WIDTH,28));
set_layer_attr(trackingLayer, titleLayer, FONT_KEY_GOTHIC_24_BOLD, GTextAlignmentCenter, GColorWhite);
// Measure Layer (2nd row)
measureLayer = text_layer_create(GRect(10,42,WIDTH-20,33+28));
set_layer_attr(trackingLayer, measureLayer, FONT_KEY_GOTHIC_24_BOLD, GTextAlignmentCenter, GColorBlack);
tick_timer_service_subscribe(SECOND_UNIT, &handle_second);
app_message_register_inbox_received(fetch_msg);
app_message_register_inbox_dropped(in_dropped_handler);
app_message_register_outbox_failed(out_failed_handler);
app_message_open(256,50); // in/out
}
static void window_unload(Window *window) {
}
static void init(void) {
window = window_create();
window_set_click_config_provider(window, click_config_provider);
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
const bool animated = true;
window_set_fullscreen(window, true);
window_stack_push(window, animated);
window_set_background_color(window, GColorBlack);
}
static void deinit(void) {
window_destroy(window);
}
int main(void) {
init();
APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window);
app_event_loop();
deinit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment