Hola!
En el post de hace 2 días, puse un pequeño ejemplo sobre cómo se utilizan los archivos JS en el mundo de desarrollo de Pebble. Esto archivos se ejecutan en el smartphone asociado con el pebble y hoy, voy a extender el ejemplo anterior para mostrar como se puede enviar información desde el JS hacia el Pebble.
Sobre el ejemplo anterior he modificado el JS con los siguientes cambios
– De acuerdo a la opción que se recibe desde el Pebble, se almacena un valor específico en la variable selectedButton (línea 9)
– Al final de la evaluación, se envía este valor hacia el Pebble utilizando Pebble.sendAppMessage() (línea 27). En el mismo se define que item del diccionario de mensajes se utiliza y además tenemos la posibilidad de utilizar callbacks para procesar los errores y el Ok de la funcion.
En el archivo C tenemos los siguientes cambios
– En la línea 95 se realiza la suscripción a los mensajes de entrada en la función in_received_handler
– Esta función (línea 11) procesa cada uno de los elementos que se reciben en el diccionario de intercambio de mensajes
– Finalmente en la línea 28, se evalua el mensaje y se muestra la información que se recibe desde el Pebble
| #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); | |
| } | |
| } | |
| ); |
Referencia: https://elbruno.com/2014/09/30/pebble-como-utilizar-un-archivo-js-desde-el-pebble/
Saludos @ Home
El Bruno
Leave a Reply