Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created October 2, 2014 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/975c760b8bf69a123aa9 to your computer and use it in GitHub Desktop.
Save elbruno/975c760b8bf69a123aa9 to your computer and use it in GitHub Desktop.
Pebble - Comunication between the pebble smartwatch and a smartphone
#include <pebble.h>
static Window *window;
static TextLayer *text_layer;
enum {
KEY_EXCHANGEDATA = 0,
};
// END Interaction with JS
static void in_received_handler(DictionaryIterator *iter, void *context)
{
(void) context;
Tuple *t = dict_read_first(iter);
while (t != NULL)
{
process_tuple(t);
t = dict_read_next(iter);
}
}
void process_tuple(Tuple *t)
{
int key = t->key;
int value = t->value->int32;
char string_value[32];
strcpy(string_value, t->value->cstring);
switch (key) {
case KEY_EXCHANGEDATA:
snprintf(sample_buffer, sizeof("XX button: \u00B0C"), "%d button: \u00B0C", value);
text_layer_set_text(text_layer, (char*) &sample_buffer);
break;
}
}
void send_int(uint8_t key, uint8_t cmd)
{
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
Tuplet value = TupletInteger(key, cmd);
dict_write_tuplet(iter, &value);
app_message_outbox_send();
}
// END Interaction with JS
void select_click_handler(ClickRecognizerRef recognizer, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "click SELECT");
text_layer_set_text(text_layer, "Select");
send_int(0, 1);
}
void up_click_handler(ClickRecognizerRef recognizer, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "click UP");
text_layer_set_text(text_layer, "Up");
send_int(0, 2);
}
void down_click_handler(ClickRecognizerRef recognizer, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "click DOWN");
text_layer_set_text(text_layer, "Down");
send_int(0, 3);
}
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);
}
void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
text_layer = text_layer_create((GRect) { .origin = { 0, 72 }, .size = { bounds.size.w, 20 } });
text_layer_set_text(text_layer, "Press a button");
text_layer_set_text_alignment(text_layer, GTextAlignmentCenter);
layer_add_child(window_layer, text_layer_get_layer(text_layer));
}
void window_unload(Window *window) {
text_layer_destroy(text_layer);
}
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_stack_push(window, animated);
//Register AppMessage events
app_message_register_inbox_received(in_received_handler);
app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum()); //Largest possible input and output buffer sizes
}
void deinit(void) {
window_destroy(window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
Pebble.addEventListener('ready',
function (e) {
console.log("JS4 Pebble.addEventListener ready ");
});
Pebble.addEventListener("appmessage",
function (e) {
try {
var selectedButton = "undefined";
console.log(JSON.stringify(e.payload));
if (e.payload["exchangeData"] == 1)
{
console.log("Button SELECT");
selectedButton = "SELECT";
}
if (e.payload["exchangeData"] == 2)
{
console.log("Button UP");
selectedButton = "UP";
}
if (e.payload["exchangeData"] == 3)
{
console.log("Button DOWN");
selectedButton = "DOWN";
}
Pebble.sendAppMessage({ exchangeData: selectedButton },
function (e) {
console.log("Successfully delivered message with transactionId=" + e.data.transactionId);
},
function (e) {
console.log("Unable to deliver message with transactionId=" + e.data.transactionId + " Error is: " + e.error.message);
}
);
} catch (exc) {
console.log("exception" + exc.message);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment