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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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
Leave a comment