Skip to content

Instantly share code, notes, and snippets.

@sejoker
Created May 11, 2015
Embed
What would you like to do?
Egg timer with Pebble.js
var UI = require('ui');
var Vector2 = require('vector2');
var Vibe = require('ui/vibe');
var menu = new UI.Menu({
sections: [{
title: 'Egg size',
items: [{
title: 'Medium',
}, {
title: 'Large'
}, {
title: 'Extra-large'
}]
}]
});
var mdMenu = new UI.Menu({
sections: [{
title: 'Egg timer',
items: [{
title: 'Runny',
subtitle: '3m'
}, {
title: 'Soft',
subtitle: '4m'
}, {
title: 'Firm',
subtitle: '5m'
}]
}]
});
var lgMenu = new UI.Menu({
sections: [{
title: 'Egg timer',
items: [{
title: 'Runny',
subtitle: '4m'
}, {
title: 'Soft',
subtitle: '5m'
}, {
title: 'Firm',
subtitle: '6m'
}]
}]
});
var xlMenu = new UI.Menu({
sections: [{
title: 'Egg timer',
items: [{
title: 'Runny',
subtitle: '5m'
}, {
title: 'Soft',
subtitle: '6m'
}, {
title: 'Firm',
subtitle: '7m'
}]
}]
});
var timeouts = {
'3m': 18,
'4m': 24,
'5m': 30,
'6m': 36,
'7m': 42
};
menu.on('select', function(e) {
if (e.itemIndex === 0){
mdMenu.show();
} else if (e.itemIndex === 1){
lgMenu.show();
} else {
xlMenu.show();
}
});
mdMenu.on('select', onTimerSelect);
lgMenu.on('select', onTimerSelect);
xlMenu.on('select', onTimerSelect);
var globalIntervalId;
function onTimerSelect(e){
clearInterval(globalIntervalId);
var timeout = timeouts[e.item.subtitle];
globalIntervalId = timer(timeout);
}
menu.show();
var readyMessage = new UI.Card({
title: 'Done',
body: 'Your eggs are ready!'
});
function timer(timerInSec){
var intervalId = setInterval(function(){
timerInSec--;
if (timerInSec == 1){
Vibe.vibrate('double');
}
if (timerInSec > 0){
textfield.text(getTimeString(timerInSec));
} else {
readyMessage.show();
wind.hide();
clearInterval(globalIntervalId);
Vibe.vibrate('long');
}
}, 1000);
var wind = new UI.Window();
var textfield = new UI.Text({
position: new Vector2(0, 50),
size: new Vector2(144, 30),
font: 'bitham-42-light',
text: getTimeString(timerInSec),
textAlign: 'center'
});
wind.add(textfield);
wind.show();
wind.on('hide', function(){
clearInterval(globalIntervalId);
});
return intervalId;
}
function getTimeString(timeInSec){
var minutes = parseInt(timeInSec / 60);
var seconds = timeInSec % 60;
return minutes + ':' + (seconds < 10 ? ('0' + seconds) : seconds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment