Pebble Polling Features - sahajss/knowledge_base GitHub Wiki
#Pebble
Pebble is a very versatile and developer friendly smart watch and has an extensive and easy to understand API that has made my project much easier to develop. In this short tutorial I will give you some basic sample code that you need to set up an app that takes data from the pebbles 3d accelerometer.
Here is the basic code you need to create a functioning screen:
static void main_window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect window_bounds = layer_get_bounds(window_layer);
// Create output TextLayer
s_output_layer = text_layer_create(GRect(5, 0, window_bounds.size.w - 10, window_bounds.size.h));
text_layer_set_font(s_output_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24));
text_layer_set_text(s_output_layer, "No data yet.");
text_layer_set_overflow_mode(s_output_layer, GTextOverflowModeWordWrap);
layer_add_child(window_layer, text_layer_get_layer(s_output_layer));
}
static void main_window_unload(Window *window) {
// Destroy output TextLayer
text_layer_destroy(s_output_layer);
}
static void init() {
// Create main Window
s_main_window = window_create();
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
window_stack_push(s_main_window, true);
}
static void deinit() {
// Destroy main Window
window_destroy(s_main_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
Here's the init() code needed to set up a working data polling feature:
// Subscribe to the accelerometer data service
int num_samples = 3;
accel_data_service_subscribe(num_samples, data_handler);
// Choose update rate
accel_service_set_sampling_rate(ACCEL_SAMPLING_100HZ);
Here's the method to collect the data:
static void data_handler(AccelData *data, uint32_t num_samples) {
// Long lived buffer
static char s_buffer[128];
// Compose string of all data
snprintf(s_buffer, sizeof(s_buffer),
"N X,Y,Z\n0 %d,%d,%d\n1 %d,%d,%d\n2 %d,%d,%d",
data[0].x, data[0].y, data[0].z,
data[1].x, data[1].y, data[1].z,
data[2].x, data[2].y, data[2].z
);
//Show the data
text_layer_set_text(s_output_layer, s_buffer);
}
Using this code you can make a basic data collection app and build off of it to make a more complex app.