Paso 3: Programación del guijarro
Configurar una cuenta de piedra en cloudpebble
- Asegúrese de que el PC y el Smartphone están en la misma red local y luego encienda el desarrollador conexión en tu aplicación para smartphone guijarro tiempo Opciones--> conexión programador
- Cree un nuevo proyecto vacío, agregue un nuevo archivo de origen denominado "main.c" y luego pone el siguiente código a la misma
#include <pebble.h> #define TIMEOUT_MS 1000 #define MAX_READ_SIZE 100 static Window *s_main_window; static TextLayer *s_status_layer; static TextLayer *s_attr_text_layer; static char s_text_buffer1[20]; static SmartstrapAttribute *s_raw_attribute; static SmartstrapAttribute *s_attr_attribute; static void prv_update_text(void) { if (smartstrap_service_is_available(0x1001)) { text_layer_set_text(s_status_layer, "Connected!"); } else { text_layer_set_text(s_status_layer, "Connecting..."); } } static void prv_did_read(SmartstrapAttribute *attr, SmartstrapResult result, const uint8_t *data, size_t length) { char str[50] = " "; if (attr == s_attr_attribute) { APP_LOG(APP_LOG_LEVEL_DEBUG, "did_read(s_attr_attribute, %d, %d)", result, length); if (result == SmartstrapResultOk ) { memcpy(str, data, length); str[length] = '\0'; snprintf(s_text_buffer1, 50, "%s", str); text_layer_set_text(s_attr_text_layer, s_text_buffer1); APP_LOG(APP_LOG_LEVEL_DEBUG, "message: %s)", str); } }else { APP_LOG(APP_LOG_LEVEL_ERROR, "did_read(<%p>, %d)", attr, result); } } static void prv_did_write(SmartstrapAttribute *attr, SmartstrapResult result) { if (attr == s_attr_attribute) { APP_LOG(APP_LOG_LEVEL_DEBUG, "did_write(s_attr_attribute, %d)", result); } else { APP_LOG(APP_LOG_LEVEL_ERROR, "did_write(<%p>, %d)", attr, result); } } static void prv_write_button_action(uint8_t button_value, uint8_t click_type) { SmartstrapResult result; if (!smartstrap_service_is_available(smartstrap_attribute_get_service_id(s_attr_attribute))) { APP_LOG(APP_LOG_LEVEL_DEBUG, "s_attr_attribute is not available"); return; } // get the write buffer uint8_t *buffer = NULL; size_t length = 0; result = smartstrap_attribute_begin_write(s_attr_attribute, &buffer, &length); if (result != SmartstrapResultOk) { APP_LOG(APP_LOG_LEVEL_ERROR, "Write of s_attr_attribute failed with result %d", result); return; } // write the data into the buffer buffer[0] = button_value; buffer[1] = click_type; // send it off result = smartstrap_attribute_end_write(s_attr_attribute, 2, false); if (result != SmartstrapResultOk) { APP_LOG(APP_LOG_LEVEL_ERROR, "Write of s_attr_attribute failed with result %d", result); } } static void prv_availablility_status_changed(SmartstrapServiceId service_id, bool is_available) { APP_LOG(APP_LOG_LEVEL_DEBUG, "Availability for 0x%x is %d", service_id, is_available); prv_update_text(); } static void prv_notified(SmartstrapAttribute *attr) { SmartstrapResult result; if (attr == s_attr_attribute) { APP_LOG(APP_LOG_LEVEL_DEBUG, "notified(s_attr_attribute)"); result = smartstrap_attribute_read(s_attr_attribute); if(result != SmartstrapResultOk) { APP_LOG(APP_LOG_LEVEL_ERROR, "Error reading attribute (<%p>, %d)", attr, result); } } else { APP_LOG(APP_LOG_LEVEL_ERROR, "notified(<%p>)", attr); } } static void up_click_handler(ClickRecognizerRef recognizer, void *context) { prv_write_button_action(0x02, 0x01); } static void select_click_handler(ClickRecognizerRef recognizer, void *context) { prv_write_button_action(0x03, 0x01); } static void down_click_handler(ClickRecognizerRef recognizer, void *context) { prv_write_button_action(0x04, 0x01); } static void click_config_provider(void *context) { window_single_click_subscribe(BUTTON_ID_UP, up_click_handler); window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler); window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler); } static void prv_main_window_load(Window *window) { s_status_layer = text_layer_create(GRect(0, 15, 144, 40)); text_layer_set_font(s_status_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28)); prv_update_text(); text_layer_set_text_color(s_status_layer, GColorBlack); text_layer_set_background_color(s_status_layer, GColorClear); text_layer_set_text_alignment(s_status_layer, GTextAlignmentCenter); text_layer_set_overflow_mode(s_status_layer, GTextOverflowModeWordWrap); layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_status_layer)); s_attr_text_layer = text_layer_create(GRect(0, 60, 144, 40)); text_layer_set_font(s_attr_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28)); text_layer_set_text(s_attr_text_layer, "-"); text_layer_set_text_color(s_attr_text_layer, GColorBlack); text_layer_set_background_color(s_attr_text_layer, GColorClear); text_layer_set_text_alignment(s_attr_text_layer, GTextAlignmentCenter); text_layer_set_overflow_mode(s_attr_text_layer, GTextOverflowModeWordWrap); layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_attr_text_layer)); } static void prv_main_window_unload(Window *window) { text_layer_destroy(s_status_layer); } static void prv_init(void) { s_main_window = window_create(); window_set_click_config_provider(s_main_window, click_config_provider); window_set_window_handlers(s_main_window, (WindowHandlers) { .load = prv_main_window_load, .unload = prv_main_window_unload }); window_stack_push(s_main_window, true); SmartstrapHandlers handlers = (SmartstrapHandlers) { .availability_did_change = prv_availablility_status_changed, .did_write = prv_did_write, .did_read = prv_did_read, .notified = prv_notified }; smartstrap_subscribe(handlers); smartstrap_set_timeout(50); s_raw_attribute = smartstrap_attribute_create(0, 0, 2000); s_attr_attribute = smartstrap_attribute_create(0x1001, 0x1001, 20); } static void prv_deinit(void) { window_destroy(s_main_window); smartstrap_unsubscribe(); smartstrap_attribute_destroy(s_raw_attribute); smartstrap_attribute_destroy(s_attr_attribute); }int main(void) { prv_init(); APP_LOG(APP_LOG_LEVEL_DEBUG, "STARTING APP"); if (s_attr_attribute && s_raw_attribute) { app_event_loop(); } APP_LOG(APP_LOG_LEVEL_DEBUG, "ENDING APP"); prv_deinit(); }
4 en la ficha Configuración desactive construir aplitas puesto que estamos construyendo para funcionar con relojes de tiempo guijarro
5. en la pestaña compilación seleccione teléfono en vez de emulador para programar nuestro reloj real
6. ejecute el código haciendo clic en el botón play