Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
neal committed Dec 19, 2013
0 parents commit 6662b56
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
.lock-waf*
src/js/config.js
src/js/pebble-js-app.js
resources/images/barcode.png
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# PebbleBucks 2.0

PebbleBucks is a Starbucks watch app for the Pebble Smartwatch. It shows your Starbucks barcode and your account information - including the dollar balance, time when it was last updated, number of stars and number of rewards.

Select button updates the account info with the latest data available.

Idea and images are from the original PebbleBucks by mattdonders.

## Requirements

* Place your barcode in `resources/images/barcode.png`.
* Take a screenshot of the mobile app.
* Crop the image to longer side being 168 pixels and shorter side being about 63 pixels.
* Rotate it 90° such that the height is longer than the width.
* Add your Starbucks credentials in `src/js/config.js`.

Note: By default, PebbleBucks uses my own API that scrapes the required data from starbucks.com. It is open sourced [here](), feel free to run it on your own server if you don't want to pass your credentials through mine. (although I try to keep my server very secure and the API does not keep any logs or store anything.)

## Build

$ sh build.sh

The `build.sh` script generates the `pebble-js-app.js` with your Starbucks credentials, so use that instead of the normal `pebble build`.

## Install

Build and install:

$ sh build.sh install

Only install:

$ pebble install

## Screenshot

![](http://cl.ineal.me/image/0m2t1T3s0O0t/PebbleBucks.png)
42 changes: 42 additions & 0 deletions appinfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"uuid": "716c56e6-44f7-4ed4-82a5-779dbc560b62",
"shortName": "PebbleBucks",
"longName": "PebbleBucks",
"companyName": "Neal",
"versionCode": 1,
"versionLabel": "1.0.0",
"watchapp": {
"watchface": false
},
"appKeys": {
"rewards": 0,
"stars": 1,
"balance": 2,
"status": 3
},
"resources": {
"media": [
{
"menuIcon": true,
"type": "png",
"name": "IMAGE_MENU_ICON",
"file": "images/icon.png"
},
{
"type": "png",
"name": "IMAGE_BARCODE",
"file": "images/barcode.png"
},
{
"type": "png",
"name": "IMAGE_REWARD",
"file": "images/reward.png"
},
{
"type": "png",
"name": "IMAGE_STAR",
"file": "images/star.png"
}
]
}
}
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rm src/js/pebble-js-app.js 2> /dev/null
cat src/js/*.js >> src/js/pebble-js-app.js
pebble build || { exit $?; }
if [ "$1" = "install" ]; then
pebble install --logs
fi
Binary file added resources/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/images/reward.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/images/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/js/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var api_url = 'http://ineal.me/starbucks/api';
var username = '';
var password = '';

80 changes: 80 additions & 0 deletions src/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var maxAppMessageTries = 3;
var appMessageRetryTimeout = 3000;
var appMessageTimeout = 100;
var appMessageQueue = [];

function sendAppMessage() {
if (appMessageQueue.length > 0) {
currentAppMessage = appMessageQueue[0];
currentAppMessage.numTries = currentAppMessage.numTries || 0;
currentAppMessage.transactionId = currentAppMessage.transactionId || -1;
if (currentAppMessage.numTries < maxAppMessageTries) {
console.log('Sending AppMessage to Pebble: ' + JSON.stringify(currentAppMessage.message));
Pebble.sendAppMessage(
currentAppMessage.message,
function(e) {
appMessageQueue.shift();
setTimeout(function() {
sendAppMessage();
}, appMessageTimeout);
}, function(e) {
console.log('Failed sending AppMessage for transactionId:' + e.data.transactionId + '. Error: ' + e.data.error.message);
appMessageQueue[0].transactionId = e.data.transactionId;
appMessageQueue[0].numTries++;
setTimeout(function() {
sendAppMessage();
}, appMessageRetryTimeout);
}
);
} else {
console.log('Failed sending AppMessage for transactionId:' + currentAppMessage.transactionId + '. Bailing. ' + JSON.stringify(currentAppMessage.message));
}
}
}

function fetch() {
var params = 'username=' + username + '&password=' + password;
var xhr = new XMLHttpRequest();
xhr.open('POST', api_url, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('Content-length', params.length);
xhr.setRequestHeader('Connection', 'close');
xhr.onload = function(e) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.responseText) {
res = JSON.parse(xhr.responseText);
rewards = res.rewards || '0';
stars = res.stars || '0';
balance = res.dollar_balance || '0';
balance = '$' + parseFloat(Math.round(balance * 100) / 100).toFixed(2);
status = res.dollar_balance_updated || '';
status = status.replace(' ', '\n');
appMessageQueue.push({'message': {'rewards': rewards, 'stars': stars, 'balance': balance, 'status': status}});
} else {
console.log('Invalid response received! ' + JSON.stringify(xhr));
appMessageQueue.push({'message': {'status': 'Invalid response!'}});
}
} else {
console.log('Request returned error code ' + xhr.status.toString());
appMessageQueue.push({'message': {'status': 'HTTP/1.1 ' + xhr.statusText}});
}
}
sendAppMessage();
}
xhr.onerror = function() {
console.log('HTTP request return error');
appMessageQueue.push({'message': {'status': 'Failed to connect!'}});
sendAppMessage();
};
xhr.send(params);
}

Pebble.addEventListener('ready', function(e) {
fetch();
});

Pebble.addEventListener('appmessage', function(e) {
fetch();
});

61 changes: 61 additions & 0 deletions src/libs/pebble-assist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/***
* Pebble Assist
* Copyright (C) 2013 Matthew Tole
***/

#pragma once

#define INFO(...) app_log(APP_LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
#define LOG(...) app_log(APP_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define WARN(...) app_log(APP_LOG_LEVEL_WARNING, __FILE__, __LINE__, __VA_ARGS__)
#define ERROR(...) app_log(APP_LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)

#ifndef MENU_CELL_BASIC_CELL_HEIGHT
#define MENU_CELL_BASIC_CELL_HEIGHT 44
#endif

#ifndef PEBBLE_HEIGHT
#define PEBBLE_HEIGHT 168
#endif

#ifndef PEBBLE_WIDTH
#define PEBBLE_WIDTH 144
#endif

#ifndef STATUS_HEIGHT
#define STATUS_HEIGHT 16
#endif

#define layer_add_to_window(layer, window) layer_add_child(window_get_root_layer(window), layer)
#define text_layer_add_to_window(layer, window) layer_add_child(window_get_root_layer(window), text_layer_get_layer(layer))
#define bitmap_layer_add_to_window(layer, window) layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(layer))
#define inverter_layer_add_to_window(layer, window) layer_add_child(window_get_root_layer(window), inverter_layer_get_layer(layer))
#define menu_layer_add_to_window(layer, window) layer_add_child(window_get_root_layer(window), menu_layer_get_layer(layer))
#define simple_menu_layer_add_to_window(layer, window) layer_add_child(window_get_root_layer(window), simple_menu_layer_get_layer(layer))
#define text_layer_set_system_font(layer, font) text_layer_set_font(layer, fonts_get_system_font(font))
#define text_layer_set_colours(layer, foreground, background) text_layer_set_text_color(layer, foreground); text_layer_set_background_color(layer, background)
#define text_layer_set_colors(layer, foreground, background) text_layer_set_text_color(layer, foreground); text_layer_set_background_color(layer, background)
#define layer_create_fullscreen(window) layer_create(layer_get_bounds(window_get_root_layer(window)));
#define text_layer_create_fullscreen(window) text_layer_create(layer_get_bounds(window_get_root_layer(window)));
#define menu_layer_create_fullscreen(window) menu_layer_create(layer_get_bounds(window_get_root_layer(window)));
#define action_bar_layer_create_and_add(action_bar, window) action_bar = action_bar_layer_create(); action_bar_layer_add_to_window(action_bar, window)

#define window_destroy_safe(window) if (window != NULL) { window_destroy(window); }
#define number_window_destroy_safe(window) if (window != NULL ) { number_window_destroy(window); }
#define text_layer_destroy_safe(layer) if (layer != NULL) { text_layer_destroy(layer); }
#define bitmap_layer_destroy_safe(layer) if (layer != NULL) { bitmap_layer_destroy(layer); }
#define layer_destroy_safe(layer) if (layer != NULL) { layer_destroy(layer); }
#define menu_layer_destroy_safe(layer) if (layer != NULL) { menu_layer_destroy(layer); }
#define simple_menu_layer_destroy_safe(layer) if (layer != NULL) { simple_menu_layer_destroy(layer); }
#define action_bar_layer_destroy_safe(layer) if (layer != NULL) { action_bar_layer_destroy(layer); }
#define scroll_layer_destroy_safe(layer) if (layer != NULL) { scroll_layer_destroy(layer); }
#define app_timer_cancel_safe(timer) if (timer != NULL) { app_timer_cancel(timer); timer = NULL; }

#define persist_read_int_safe(key, value) if (persist_exists(key)) { return persist_read_int(key); } else { return value; }
#define persist_read_bool_safe(key, value) if (persist_exists(key)) { return persist_read_bool(key); } else { return value; }

#define fonts_load_resource_font(resource) fonts_load_custom_font(resource_get_handle(resource))

#define action_bar_layer_clear_icons(action_bar) action_bar_layer_clear_icon(action_bar, BUTTON_ID_SELECT); action_bar_layer_clear_icon(action_bar, BUTTON_ID_DOWN); action_bar_layer_clear_icon(action_bar, BUTTON_ID_UP)

#define menu_layer_reload_data_and_mark_dirty(layer) menu_layer_reload_data(layer); layer_mark_dirty(menu_layer_get_layer(layer));
Loading

0 comments on commit 6662b56

Please sign in to comment.