Skip to content

Instantly share code, notes, and snippets.

@hashbangle
Created June 9, 2022 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashbangle/843b54666c1f66ee41c44661a42f45c6 to your computer and use it in GitHub Desktop.
Save hashbangle/843b54666c1f66ee41c44661a42f45c6 to your computer and use it in GitHub Desktop.
Shhh Clock is my first attempt at creating a Bangle.js 2 clock, based off the clock tutorial. As a demanding-baby-haver, I needed a way to quickly turn on Quiet Mode and turn down the brightness, preferably without looking. Tap the bottom half of the screen to toggle Shhh mode.
/*
// shhhclock.info
// Pasted this into the left side of the IDE.
require("Storage").write("shhhclock.info",{
"id":"shhhclock",
"name":"Shhh Clock",
"type":"clock",
"src":"shhhclock.app.js"
});
*/
const locale = require('locale');
const storage = require('Storage');
const SCREEN_WIDTH = g.getWidth();
const SCREEN_HEIGHT = g.getHeight();
const WIDGET_BAR_H = 24;
let settings = storage.readJSON('setting.json',true)||{};
// Preferences for type of quiet mode and brightness level.
let shhhOn = {
quiet: 1,
brightness: 0.1
};
let shhhOff = {
quiet: 0,
brightness: 1
};
// Draw timeout (from BW Clock)
// timeout used to update every minute
var drawTimeout;
// schedule a draw for the next minute
function queueDraw() {
if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = setTimeout(function() {
drawTimeout = undefined;
draw();
}, 60000 - (Date.now() % 60000));
}
function draw() {
// Reset the state of the graphics library
g.reset();
// Get current date/time
var dateObj = new Date();
var hours = dateObj.getHours(), minutes = dateObj.getMinutes();
if (hours > 12) hours = hours-12;
minutes = ("0"+minutes).substr(-2);
var dayOfWeek = locale.dow(dateObj, true).toUpperCase();
var date = dateObj.getDate();
var month = locale.month(dateObj, 1).toUpperCase();
var dateStr = dayOfWeek + "\n\n\n" + month;
// Queue draw again
queueDraw();
// Black bg with white text
g.setTheme({bg:"#000", fg: "#fff", dark:true});
g.setColor("#000000");
g.fillRect(0, WIDGET_BAR_H, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setColor("#ffffff");
// Draw shhh indicator if quiet mode is on
if (settings.quiet == 1) {
drawShhhIcon();
}
// Draw day/month part of date on the left
g.setFont("Vector",25);
g.setFontAlign(0,-1);
var w = g.stringWidth(dateStr);
g.drawString(dateStr, 3 + w/2, WIDGET_BAR_H + 30, false);
// Draw number part of date in larger font
g.setFont("Vector",50);
g.drawString(date, 3 + w/2, WIDGET_BAR_H + 55, false);
// draw current time on the right
g.setFont("Vector",80);
g.setFontAlign(1,1);
g.drawString(hours, SCREEN_WIDTH, SCREEN_HEIGHT - 70, false);
g.drawString(minutes, SCREEN_WIDTH, SCREEN_HEIGHT, false);
}
function drawShhhIcon() {
g.setColor("#ffffff");
g.setFont("Vector",18);
g.setFontAlign(-1,-1);
g.drawString("shhh...", 10, WIDGET_BAR_H + 3, false);
}
function clearShhhIcon() {
g.clearRect(3,WIDGET_BAR_H,60,WIDGET_BAR_H+20);
}
// Stop updates when LCD is off, restart when on (from BW Clock)
Bangle.on('lcdPower',on=>{
if (on) {
draw(); // draw immediately, queue redraw
} else { // stop draw timer
if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = undefined;
}
});
// Toggle Quiet Mode if bottom half of screen is tapped
Bangle.on('touch', function(btn, e){
if (e.y > parseInt(SCREEN_HEIGHT/2)) {
// Buzz to confirm tap
Bangle.buzz(40, 0.6);
if ((settings.quiet == 0) || (settings.quiet == undefined)) { // Turn shhh on
settings.quiet = shhhOn.quiet;
settings.brightness = shhhOn.brightness;
drawShhhIcon();
} else { // Turn shhh off
settings.quiet = shhhOff.quiet;
settings.brightness = shhhOff.brightness;
clearShhhIcon();
}
// Save new Quiet Mode setting
storage.writeJSON('setting.json',settings);
// Set new brightness
Bangle.setLCDBrightness(settings.brightness);
}
});
// Clear the screen once, at startup
g.clear();
// draw immediately at first
draw();
// Show launcher when middle button pressed
Bangle.setUI("clock");
// Load widgets
Bangle.loadWidgets();
Bangle.drawWidgets();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment