-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhc-binary-clock.app.js
1 lines (1 loc) · 3.01 KB
/
hc-binary-clock.app.js
1
let fullTimeDisplayTimout;const fontColor={regular:"#42c8e3",darker:"#3295a8",highlight:"#c21bac"},dateTime=()=>{const flattenArray=(array=[])=>[].concat.apply([],array),arrayFormatZeroPad=number=>{const numberStr=number.toString();return 1===numberStr.length?["0",numberStr]:numberStr.split("")},getCurrentTime=()=>{const now=new Date,timeArr=flattenArray([now.getHours(),now.getMinutes(),now.getSeconds()].map(arrayFormatZeroPad));return{value:timeArr,toString:function(){let timeStr="";return timeStr+=timeArr[0]+timeArr[1]+":",timeStr+=timeArr[2]+timeArr[3],timeStr}}},getCurrentDate=()=>{const now=new Date,dateArr=flattenArray([now.getDate(),now.getMonth()+1,now.getFullYear()].map(arrayFormatZeroPad));return{value:dateArr,toString:function(){let dateStr=""+dateArr[0]+dateArr[1]+"/";return dateStr+=dateArr[2]+dateArr[3]+"/",dateStr+=dateArr[4]+dateArr[5]+dateArr[6]+dateArr[7],dateStr}}};return{getCurrentDate:getCurrentDate,getCurrentTime:getCurrentTime}};let prevFrame=[];const drawClock=()=>{const numberOfColumns=6,drawFullGrid=!1,offset_x=40,offset_y=60,canvasWidth=160,colWidth=160/6,circleDiamater=160/6/2.5,findBinary=target=>[[0,0,0,0],[0,0,0,1],[0,0,1,0],[0,0,1,1],[0,1,0,0],[0,1,0,1],[0,1,1,0],[0,1,1,1],[1,0,0,0],[1,0,0,1]][target],drawColumn=(columnIndex=0,bits=[0,0,0,0])=>{g.setColor(fontColor.regular);const maxDotsPerColumn=[2,4,3,4,3,4],xPos=columnIndex*(160/6);let yPos=160/6/2;const drawDot=fn=>{"drawCircle"===fn&&g.setColor(fontColor.darker),g[fn](xPos+160/6/2+offset_x,yPos+offset_y,160/6/2.5),g.setColor(fontColor.regular)};for(let i=0;i<bits.length;i+=1)i+maxDotsPerColumn[columnIndex]>=4&&(prevFrame&&prevFrame[columnIndex]&&prevFrame[columnIndex][i]?bits[i]!==prevFrame[columnIndex][i]&&(g.clearRect(xPos+160/6/2-15+offset_x,yPos-15+offset_y,xPos+160/6/2+20+offset_x,yPos+20+offset_y),drawDot(bits[i]?"fillCircle":"drawCircle")):drawDot(bits[i]?"fillCircle":"drawCircle")),yPos+=160/6},data=dateTime().getCurrentTime().value.map(findBinary);for(let i=0;i<data.length;i+=1)drawColumn(i,data[i]);prevFrame=data,drawDate()};let prevDate;const drawDate=()=>{if(void 0!==fullTimeDisplayTimout)return;let curDate=dateTime().getCurrentDate();prevDate!=curDate.value&&(g.setColor(fontColor.darker),drawBottomMsg(curDate.toString()),g.setColor(fontColor.regular),prevDate=curDate.value)},drawBottomMsg=msg=>{const screenWidth=240;g.clearRect(0,g.getHeight()-60,240,240),g.setFontAlign(0,0),g.setFont("6x8",2),g.drawString(msg,g.getWidth()/2,g.getHeight()-30)};setWatch(()=>{if(void 0!==fullTimeDisplayTimout)return;let curTime=dateTime().getCurrentTime().toString();g.setColor(fontColor.highlight),drawBottomMsg(curTime),g.setColor(fontColor.regular),fullTimeDisplayTimout=setTimeout(()=>{clearTimeout(fullTimeDisplayTimout),fullTimeDisplayTimout=void 0,prevDate=void 0,drawDate()},3e3)},BTN1,{repeat:!0}),Bangle.on("lcdPower",on=>{on&&(Bangle.loadWidgets(),Bangle.drawWidgets(),drawClock())}),setWatch(Bangle.showLauncher,BTN2,{repeat:!1,edge:"falling"}),g.clear(),Bangle.loadWidgets(),Bangle.drawWidgets(),setInterval(()=>{drawClock()},1e3),drawClock();