import document from "document"; import clock from "clock"; import { display } from "display"; import { vibration } from "haptics"; import { preferences } from "user-settings"; import * as messaging from "messaging"; import * as fs from "fs"; import * as digits from "../common/digits"; import * as colors from "../common/colors"; import * as themes from "../common/themes"; var starttime, endtime; const debug = false; const colorTemplate = false; const colorCount = 25; var colorDef = [ null, themes.colors['whiteMarble'], themes.colors['blackMarble'] ]; var storedColorDef; var fileSuccess = true; console.log('reading color def'); try { storedColorDef = fs.readFileSync("colordef.txt", "cbor"); } catch(err) { fileSuccess = false; console.log('color def not found'); } if (fileSuccess) { colorDef = storedColorDef; } var storedColorSet; var fileSuccess = true; console.log('reading colors file'); try { storedColorSet = fs.readFileSync("colors.txt", "cbor"); } catch(err) { fileSuccess = false; console.log('colors file not found, scheduling initial color generation'); } if (fileSuccess) { colors.setCurrentColorSet(storedColorSet); } if (debug) { let touchArea = document.getElementById('touchArea'); display.autoOff = false; display.on = true; colors.initColors(colorDef, colorCount); touchArea.onclick = (e) => { colors.initColors(colorDef, colorCount); updateClock(true); } } function updateClock(forceRedraw) { starttime = new Date(); console.log('started updateClock with forceRedraw = '+forceRedraw); let currDate = new Date(); let hours = currDate.getHours(); let displayHours = hours; if (preferences.clockDisplay === '12h') { displayHours = displayHours % 12; displayHours = displayHours ? displayHours : 12; } let minutes = currDate.getMinutes(); let day = currDate.getDate(); let month = currDate.getMonth()+1; let newDigits = [ Math.floor(displayHours/10), displayHours%10, Math.floor(minutes/10), minutes%10, Math.floor(day/10), day%10, Math.floor(month/10), month%10, ]; if (debug) { newDigits[0] = 1; newDigits[1] = 7; newDigits[2] = 0; newDigits[3] = 4; newDigits[4] = 1; newDigits[5] = 3; newDigits[6] = 0; newDigits[7] = 7; } if (colorTemplate) { newDigits[0] = 'Ÿ'; newDigits[1] = 'ÿ'; newDigits[2] = 'Ÿ'; newDigits[3] = 'ÿ'; } endtime = new Date(); console.log('updateClock is ready for setDigits at '+(endtime-starttime)+' ms'); digits.setDigits(newDigits, forceRedraw); endtime = new Date(); console.log('updateClock finished setDigits at '+(endtime-starttime)+' ms'); } display.onchange = () => { if (!display.on) { colors.initColors(colorDef, colorCount); updateClock(true); } } clock.granularity = "minutes"; clock.ontick = () => updateClock(); updateClock(); messaging.peerSocket.onmessage = e => { console.log("Message received -> "+e.data.key+": "+e.data.newValue); display.poke(); vibration.start("bump"); let updateColors = false switch (e.data.key) { case 'digitColor': colorDef[1] = themes.colors[e.data.newValue]; updateColors = true; break; case 'ornamentColor': colorDef[2] = themes.colors[e.data.newValue]; updateColors = true; break; }; if (updateColors) { colors.initColors(colorDef, colorCount); updateClock(true); vibration.start("bump"); fs.writeFileSync("colordef.txt", colorDef, "cbor"); } } messaging.peerSocket.onopen = () => { console.log("Clockface opened socket"); }; messaging.peerSocket.close = () => { console.log("Clockface closed socket"); }; messaging.peerSocket.onerror = (err) => { console.log("Clockface socket error: " + err.code + " - " + err.message); } setTimeout(() => { colors.initColors(colorDef, colorCount); updateClock(true); }, 200);