Created
February 28, 2014 19:04
-
-
Save HealsCodes/9277512 to your computer and use it in GitHub Desktop.
Pebble on-screen accelerometer data display
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> | |
static Window *window; | |
static TextLayer *x_accel_layer, *y_accel_layer, *z_accel_layer; | |
static char accel_layer_text[3][15]; | |
static void process_accel_data(AccelData *data, uint32_t num_samples) | |
{ | |
memset(accel_layer_text, 0, sizeof(accel_layer_text)); | |
snprintf(accel_layer_text[0], 14, "X: %4d", data[num_samples - 1].x); | |
snprintf(accel_layer_text[1], 14, "Y: %4d", data[num_samples - 1].y); | |
snprintf(accel_layer_text[2], 14, "Z: %4d", data[num_samples - 1].z); | |
text_layer_set_text(x_accel_layer, accel_layer_text[0]); | |
text_layer_set_text(y_accel_layer, accel_layer_text[1]); | |
text_layer_set_text(z_accel_layer, accel_layer_text[2]); | |
} | |
static void window_load(Window *window) { | |
Layer *window_layer = window_get_root_layer(window); | |
GRect bounds = layer_get_bounds(window_layer); | |
x_accel_layer = text_layer_create((GRect) { .origin = { 0, 47 }, .size = { bounds.size.w, 20 } }); | |
y_accel_layer = text_layer_create((GRect) { .origin = { 0, 72 }, .size = { bounds.size.w, 20 } }); | |
z_accel_layer = text_layer_create((GRect) { .origin = { 0, 97 }, .size = { bounds.size.w, 20 } }); | |
text_layer_set_text_alignment(x_accel_layer, GTextAlignmentCenter); | |
text_layer_set_text_alignment(y_accel_layer, GTextAlignmentCenter); | |
text_layer_set_text_alignment(z_accel_layer, GTextAlignmentCenter); | |
layer_add_child(window_layer, text_layer_get_layer(x_accel_layer)); | |
layer_add_child(window_layer, text_layer_get_layer(y_accel_layer)); | |
layer_add_child(window_layer, text_layer_get_layer(z_accel_layer)); | |
accel_data_service_subscribe(1, process_accel_data); | |
} | |
static void window_unload(Window *window) { | |
accel_data_service_unsubscribe(); | |
text_layer_destroy(x_accel_layer); | |
text_layer_destroy(y_accel_layer); | |
text_layer_destroy(z_accel_layer); | |
} | |
static void init(void) { | |
window = window_create(); | |
window_set_window_handlers(window, (WindowHandlers) { | |
.load = window_load, | |
.unload = window_unload, | |
}); | |
const bool animated = true; | |
window_stack_push(window, animated); | |
} | |
static void deinit(void) { | |
window_destroy(window); | |
} | |
int main(void) { | |
init(); | |
APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window); | |
app_event_loop(); | |
deinit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment