Hola!

Por fin, creo que esta semana podré dedicar un par de días a programar un poco con Visual Studio 2013 para el Pebble. Uno de los temas que tengo pendiente es ver como funciona la API del sensor de movimiento.

Lo primero que tenemos que tener en cuenta, es que al igual que el reloj, el sensor de movimiento funciona como un servicio (link). Es decir, debemos suscribirnos al mismo en Init() para poder procesar los datos, y luego con un timer procesar los datos que nos envía el sensor. En el siguiente ejemplo, podemos ver cómo en las líneas 30 y 31, se realiza la suscripción al evento del sensor de movimiento y luego el timer_callback envía estos datos como un LOG a la consola.


#include <pebble.h>
#define MATH_PI 3.141592653589793238462
#define NUM_DISCS 20
#define DISC_DENSITY 0.25
#define ACCEL_RATIO 0.05
#define ACCEL_STEP_MS 50
Window *window;
TextLayer *text_layer;
static AppTimer *timer;
static void timer_callback(void *data) {
AccelData accel = (AccelData) { .x = 0, .y = 0, .z = 0 };
accel_service_peek(&accel);
APP_LOG(APP_LOG_LEVEL_DEBUG, "x:" + accel.x);
APP_LOG(APP_LOG_LEVEL_DEBUG, "y:" + accel.y);
APP_LOG(APP_LOG_LEVEL_DEBUG, "z:" + accel.z);
}
void init(void) {
window = window_create();
text_layer = text_layer_create(GRect(0, 0, 144, 154));
text_layer_set_text(text_layer, "Acel Sample!");
text_layer_set_font(text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_text_alignment(text_layer, GTextAlignmentCenter);
layer_add_child(window_get_root_layer(window), text_layer_get_layer(text_layer));
window_stack_push(window, true);
accel_data_service_subscribe(0, NULL);
timer = app_timer_register(ACCEL_STEP_MS, timer_callback, NULL);
}
void handle_deinit(void) {
accel_data_service_unsubscribe();
text_layer_destroy(text_layer);
window_destroy(window);
}
int main(void) {
init();
app_event_loop();
handle_deinit();
}

Si mañana lo implemento en modo completo, muestro un ejemplo de la app terminada.

Saludos @ Home

El Bruno

image image image Google

Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading