Hello!

Finally, this week I spend a couple of days playing with Visual Studio 2013 and C++ for Pebble. One of the topics I have pending is to learn is how to work with the motion sensor API.

The first thing we have to bear in mind, is that like the clock, accelerometer works as a service (link). We should subscribe to a specific event in the Init() in order to process the data, and then get data that the sensor sends us. In the following example, we see how in lines 30 and 31, he is subscribing to the event of the motion sensor and then the timer_callback sends this data as a LOG to the console.


#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();
}

If tomorrow I implement it in full mode, I show an example of the finished app.

Saludos @ La Finca

El Bruno

image image image Google

2 responses to “[#PEBBLE] Working with accelerometer”

  1. el desiempre Avatar
    el desiempre

    tio me aburres. De verdad, es necesario todo esto que publicas sobre peeble?, ya sabemos que te lo has comprado, no hace falta que presumas. Publica algo interesante sobre c# o ALM y dejate de pendejadas. Creo que esta vez sí, este va a ser el último articulo que lea tuyo.
    Cada vez tengo más claro que los mvp ahora mismo y hoy por hoy microsoft los regala.
    Que manera de perder el tiempo. Currar debes currar poco.

    Like

    1. En primer lugar, lamento que no te interese el tema del Pebble, a mi me fascina 😀
      Si lees más el blog lo lamentaré también, aunque el único feedback que poseo tuyo son los comentarios. Volviendo al Pebble, las capacidades que tiene son fabulosas, aunque lamentablemente si quieres realmente sacarle provecho necesitas hacerlo desde iOS o Android (qué se que es un tema que te interesa).
      Un Saludo

      Like

Leave a comment

Discover more from El Bruno

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

Continue reading