Skip to content

Instantly share code, notes, and snippets.

@idiotandrobot
Last active January 3, 2016 15:41
Show Gist options
  • Save idiotandrobot/8f1267910ae384151ce8 to your computer and use it in GitHub Desktop.
Save idiotandrobot/8f1267910ae384151ce8 to your computer and use it in GitHub Desktop.
Pebble C
static BitmapLayer *s_background_layer;
static GBitmap *s_background_bitmap;
static void configure_background(Layer *window_layer, GRect bounds) {
s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_BACKGROUND);
s_background_layer = bitmap_layer_create(bounds);
bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
layer_add_child(window_layer, bitmap_layer_get_layer(s_background_layer));
}
// main_window_load
configure_background(window_layer, bounds);
static void deconfigure_background() {
gbitmap_destroy(s_background_bitmap);
bitmap_layer_destroy(s_background_layer);
}
// main_window_unload
deconfigure_background();
#define DAY_FONT RESOURCE_ID_FONT_PERFECT_DOS_20
#define DAY_TOP 24
#define DAY_TOP_ROUND 30
#define DAY_HEIGHT 25
static GFont s_day_font;
static TextLayer *s_day_layer;
static void update_day(struct tm *tick_time) {
static char s_day_buffer[7];
strftime(s_day_buffer, sizeof(s_day_buffer), "%a %d", tick_time);
text_layer_set_text(s_day_layer, s_day_buffer);
}
static void daytick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_day(tick_time);
}
static void configure_day(Layer *window_layer, GRect bounds) {
s_day_font = fonts_load_custom_font(resource_get_handle(DAY_FONT));
s_day_layer = text_layer_create(
GRect(0, PBL_IF_ROUND_ELSE(DAY_TOP_ROUND, DAY_TOP), bounds.size.w, DAY_HEIGHT));
text_layer_set_background_color(s_day_layer, GColorClear);
text_layer_set_text_color(s_day_layer, GColorWhite);
text_layer_set_text(s_day_layer, "Sun 01");
text_layer_set_font(s_day_layer, s_day_font);
text_layer_set_text_alignment(s_day_layer, GTextAlignmentCenter);
layer_add_child(window_layer, text_layer_get_layer(s_day_layer));
}
// main_window_load
configure_day(window_layer, bounds);
static void deconfigure_day() {
text_layer_destroy(s_day_layer);
fonts_unload_custom_font(s_day_font);
}
// main_window_unload
deconfigure_day();
// init
update_day(tick_time);
tick_timer_service_subscribe(DAY_UNIT, daytick_handler);
#include <pebble.h>
static Window *s_main_window;
static void main_window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
}
static void main_window_unload(Window *window) {
}
static void init() {
s_main_window = window_create();
window_set_background_color(s_main_window, GColorBlack);
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
window_stack_push(s_main_window, true);
time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
}
static void deinit() {
window_destroy(s_main_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
#define MONTH_FONT RESOURCE_ID_FONT_PERFECT_DOS_20
#define MONTH_TOP 116
#define MONTH_TOP_ROUND 122
#define MONTH_HEIGHT 25
static GFont s_month_font;
static TextLayer *s_month_layer;
static void update_month(struct tm *tick_time) {
static char s_month_buffer[7];
strftime(s_month_buffer, sizeof(s_month_buffer), "%b %y", tick_time);
text_layer_set_text(s_month_layer, s_month_buffer);
}
static void monthtick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_month(tick_time);
}
static void configure_month(Layer *window_layer,GRect bounds) {
s_month_font = fonts_load_custom_font(resource_get_handle(MONTH_FONT));
s_month_layer = text_layer_create(
GRect(0, PBL_IF_ROUND_ELSE(MONTH_TOP_ROUND, MONTH_TOP), bounds.size.w, MONTH_HEIGHT));
text_layer_set_background_color(s_month_layer, GColorClear);
text_layer_set_text_color(s_month_layer, GColorWhite);
text_layer_set_text(s_month_layer, "Jan 01");
text_layer_set_font(s_month_layer, s_month_font);
text_layer_set_text_alignment(s_month_layer, GTextAlignmentCenter);
layer_add_child(window_layer, text_layer_get_layer(s_month_layer));
}
// main_window_load
configure_month(window_layer, bounds);
static void deconfigure_month() {
text_layer_destroy(s_month_layer);
fonts_unload_custom_font(s_month_font);
}
// main_window_unload
deconfigure_month();
// init
update_month(tick_time);
tick_timer_service_subscribe(MONTH_UNIT, monthtick_handler);
#define TIME_FONT RESOURCE_ID_FONT_PERFECT_DOS_48
#define TIME_TOP 52
#define TIME_TOP_ROUND 58
#define TIME_HEIGHT 50
static GFont s_time_font;
static TextLayer *s_time_layer;
static void update_time(struct tm *tick_time) {
static char s_time_buffer[8];
strftime(s_time_buffer, sizeof(s_time_buffer), clock_is_24h_style() ?
"%H:%M" : "%I:%M", tick_time);
text_layer_set_text(s_time_layer, s_time_buffer);
}
static void timetick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_time(tick_time);
}
static void configure_time(Layer *window_layer, GRect bounds) {
s_time_font = fonts_load_custom_font(resource_get_handle(TIME_FONT));
s_time_layer = text_layer_create(
GRect(0, PBL_IF_ROUND_ELSE(TIME_TOP_ROUND, TIME_TOP), bounds.size.w, 50));
text_layer_set_background_color(s_time_layer, GColorClear);
text_layer_set_text_color(s_time_layer, GColorBlack);
text_layer_set_text(s_time_layer, "00:00");
text_layer_set_font(s_time_layer, s_time_font);
text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
layer_add_child(window_layer, text_layer_get_layer(s_time_layer));
}
// main_window_load
configure_time(window_layer, bounds);
static void deconfigure_time() {
text_layer_destroy(s_time_layer);
fonts_unload_custom_font(s_time_font);
}
// main_window_unload
deconfigure_time();
// init
update_time(tick_time);
tick_timer_service_subscribe(MINUTE_UNIT, timetick_handler);
#define WEATHER_FONT RESOURCE_ID_FONT_PERFECT_DOS_20
#define WEATHER_TOP 120
#define WEATHER_TOP_ROUND 125
#define WEATHER_HEIGHT 25
#define KEY_TEMPERATURE 0
#define KEY_CONDITIONS 1
static GFont s_weather_font;
static TextLayer *s_weather_layer;
static void inbox_received_callback(DictionaryIterator *iterator, void *context) {
// Store incoming information
static char temperature_buffer[8];
static char conditions_buffer[32];
static char weather_layer_buffer[32];
// Read tuples for data
Tuple *temp_tuple = dict_find(iterator, KEY_TEMPERATURE);
Tuple *conditions_tuple = dict_find(iterator, KEY_CONDITIONS);
// If all data is available, use it
if(temp_tuple && conditions_tuple) {
snprintf(temperature_buffer, sizeof(temperature_buffer), "%dC", (int)temp_tuple->value->int32);
snprintf(conditions_buffer, sizeof(conditions_buffer), "%s", conditions_tuple->value->cstring);
// Assemble full string and display
snprintf(weather_layer_buffer, sizeof(weather_layer_buffer), "%s, %s", temperature_buffer, conditions_buffer);
text_layer_set_text(s_weather_layer, weather_layer_buffer);
}
}
static void inbox_dropped_callback(AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Message dropped!");
}
static void outbox_failed_callback(DictionaryIterator *iterator, AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_ERROR, "Outbox send failed!");
}
static void outbox_sent_callback(DictionaryIterator *iterator, void *context) {
APP_LOG(APP_LOG_LEVEL_INFO, "Outbox send success!");
}
static void update_weather(struct tm *tick_time) {
if(tick_time->tm_min % 30 == 0) {
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
dict_write_uint8(iter, 0, 0);
app_message_outbox_send();
}
}
// tick_handler
update_weather(tick_time);
static void configure_weather(Layer *window_layer, GRect bounds) {
s_weather_layer = text_layer_create(
GRect(0, PBL_IF_ROUND_ELSE(WEATHER_TOP_ROUND, WEATHER_TOP), bounds.size.w, 25));
text_layer_set_background_color(s_weather_layer, GColorClear);
text_layer_set_text_color(s_weather_layer, GColorWhite);
text_layer_set_text_alignment(s_weather_layer, GTextAlignmentCenter);
text_layer_set_text(s_weather_layer, "Loading...");
s_weather_font = fonts_load_custom_font(resource_get_handle(WEATHER_FONT));
text_layer_set_font(s_weather_layer, s_weather_font);
layer_add_child(window_layer, text_layer_get_layer(s_weather_layer));
}
// main_window_load
configure_weather(window_layer, bounds);
static void deconfigure_weather() {
text_layer_destroy(s_weather_layer);
fonts_unload_custom_font(s_weather_font);
}
// main_window_unload
deconfigure_weather();
// init
app_message_register_inbox_received(inbox_received_callback);
app_message_register_inbox_dropped(inbox_dropped_callback);
app_message_register_outbox_failed(outbox_failed_callback);
app_message_register_outbox_sent(outbox_sent_callback);
app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
var myAPIKey = '';
var xhrRequest = function (url, type, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = function () {
callback(this.responseText);
};
xhr.open(type, url);
xhr.send();
};
function locationSuccess(pos) {
// Construct URL
var url = "http://api.openweathermap.org/data/2.5/weather?lat=" +
pos.coords.latitude + "&lon=" + pos.coords.longitude + '&appid=' + myAPIKey;
// Send request to OpenWeatherMap
xhrRequest(url, 'GET',
function(responseText) {
// responseText contains a JSON object with weather info
var json = JSON.parse(responseText);
// Temperature in Kelvin requires adjustment
var temperature = Math.round(json.main.temp - 273.15);
console.log("Temperature is " + temperature);
// Conditions
var conditions = json.weather[0].main;
console.log("Conditions are " + conditions);
// Assemble dictionary using our keys
var dictionary = {
"KEY_TEMPERATURE": temperature,
"KEY_CONDITIONS": conditions
};
// Send to Pebble
Pebble.sendAppMessage(dictionary,
function(e) {
console.log("Weather info sent to Pebble successfully!");
},
function(e) {
console.log("Error sending weather info to Pebble!");
}
);
}
);
}
function locationError(err) {
console.log("Error requesting location!");
}
function getWeather() {
navigator.geolocation.getCurrentPosition(
locationSuccess,
locationError,
{timeout: 15000, maximumAge: 60000}
);
}
// Listen for when the watchface is opened
Pebble.addEventListener('ready',
function(e) {
console.log("PebbleKit JS ready!");
// Get the initial weather
getWeather();
}
);
// Listen for when an AppMessage is received
Pebble.addEventListener('appmessage',
function(e) {
console.log("AppMessage received!");
getWeather();
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment