2021-02-03 00:30:34 +00:00
|
|
|
/**
|
2021-04-07 21:45:50 +00:00
|
|
|
* Adrian Kirk 2021-02
|
|
|
|
* Sliding text clock inspired by the Pebble
|
|
|
|
* clock with the same name
|
|
|
|
*/
|
|
|
|
|
|
|
|
const color_schemes = [
|
2022-06-27 17:04:45 +00:00
|
|
|
{
|
|
|
|
name: "white",
|
|
|
|
background : [1.0,1.0,1.0],
|
|
|
|
main_bar: [0.0,0.0,0.0],
|
2022-06-28 10:08:43 +00:00
|
|
|
other_bars: [0.1,0.1,0.1],
|
2022-06-27 17:04:45 +00:00
|
|
|
},
|
2021-04-07 21:45:50 +00:00
|
|
|
{
|
|
|
|
name: "black",
|
|
|
|
background : [0.0,0.0,0.0],
|
|
|
|
main_bar: [1.0,1.0,1.0],
|
2022-06-28 10:08:43 +00:00
|
|
|
other_bars: [0.9,0.9,0.9],
|
2021-04-07 21:45:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "red",
|
|
|
|
background : [1.0,0.0,0.0],
|
|
|
|
main_bar: [1.0,1.0,0.0],
|
|
|
|
other_bars: [0.85,0.85,0.85]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "grey",
|
|
|
|
background : [0.5,0.5,0.5],
|
|
|
|
main_bar: [1.0,1.0,1.0],
|
|
|
|
other_bars: [0.0,0.0,0.0],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "purple",
|
|
|
|
background : [1.0,0.0,1.0],
|
|
|
|
main_bar: [1.0,1.0,0.0],
|
|
|
|
other_bars: [0.85,0.85,0.85]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "blue",
|
|
|
|
background : [0.4,0.7,1.0],
|
|
|
|
main_bar: [1.0,1.0,1.0],
|
|
|
|
other_bars: [0.9,0.9,0.9]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
let color_scheme_index = 0;
|
|
|
|
|
2021-02-17 01:43:21 +00:00
|
|
|
|
2021-04-07 21:45:50 +00:00
|
|
|
/**
|
|
|
|
* The Watch Display
|
|
|
|
*/
|
|
|
|
|
|
|
|
function bg_color(){
|
|
|
|
return color_schemes[color_scheme_index].background;
|
|
|
|
}
|
|
|
|
|
|
|
|
function main_color(){
|
|
|
|
return color_schemes[color_scheme_index].main_bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
function other_color(){
|
|
|
|
return color_schemes[color_scheme_index].other_bars;
|
|
|
|
}
|
|
|
|
|
|
|
|
let command_stack_high_priority = [];
|
|
|
|
let command_stack_low_priority = [];
|
|
|
|
|
|
|
|
function next_command(){
|
|
|
|
command = command_stack_high_priority.pop();
|
|
|
|
if(command == null){
|
|
|
|
//console.log("Low priority command");
|
|
|
|
command = command_stack_low_priority.pop();
|
|
|
|
} else {
|
|
|
|
//console.log("High priority command");
|
|
|
|
}
|
|
|
|
if(command != null){
|
|
|
|
command.call();
|
|
|
|
} else {
|
|
|
|
//console.log("no command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset_commands(){
|
|
|
|
command_stack_high_priority = [];
|
|
|
|
command_stack_low_priority = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function has_commands(){
|
|
|
|
return command_stack_high_priority.length > 0 ||
|
2022-06-27 17:04:45 +00:00
|
|
|
command_stack_low_priority.length > 0;
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
2021-02-17 01:43:21 +00:00
|
|
|
|
2021-02-03 00:30:34 +00:00
|
|
|
class ShiftText {
|
2021-02-17 01:43:21 +00:00
|
|
|
/**
|
2021-04-07 21:45:50 +00:00
|
|
|
* Class Responsible for shifting text around the screen
|
|
|
|
*
|
|
|
|
* This is a object that initializes itself with a position and
|
|
|
|
* text after which you can tell it where you want to move to
|
|
|
|
* using the moveTo method and it will smoothly move the text across
|
|
|
|
* at the selected frame rate and speed
|
|
|
|
*/
|
2021-02-03 00:30:34 +00:00
|
|
|
constructor(x,y,txt,font_name,
|
2021-04-07 21:45:50 +00:00
|
|
|
font_size,speed_x,speed_y,freq_millis,
|
|
|
|
color,
|
2022-07-30 09:53:21 +00:00
|
|
|
bg_color,
|
|
|
|
row_context){
|
2021-02-03 00:30:34 +00:00
|
|
|
this.x = x;
|
|
|
|
this.tgt_x = x;
|
2021-02-04 00:29:35 +00:00
|
|
|
this.init_x = x;
|
2021-02-03 00:30:34 +00:00
|
|
|
this.y = y;
|
|
|
|
this.tgt_y = y;
|
2021-02-04 00:29:35 +00:00
|
|
|
this.init_y = y;
|
2021-02-03 00:30:34 +00:00
|
|
|
this.txt = txt;
|
2021-02-04 00:29:35 +00:00
|
|
|
this.init_txt = txt;
|
2021-02-03 00:30:34 +00:00
|
|
|
this.font_name = font_name;
|
|
|
|
this.font_size = font_size;
|
|
|
|
this.speed_x = Math.abs(speed_x);
|
|
|
|
this.speed_y = Math.abs(speed_y);
|
|
|
|
this.freq_millis = freq_millis;
|
2021-04-07 21:45:50 +00:00
|
|
|
this.color = color;
|
|
|
|
this.bg_color = bg_color;
|
2022-07-30 09:53:21 +00:00
|
|
|
this.row_context = row_context;
|
2021-02-03 00:30:34 +00:00
|
|
|
this.finished_callback=null;
|
2021-02-04 00:29:35 +00:00
|
|
|
this.timeoutId = null;
|
|
|
|
}
|
2022-07-30 09:53:21 +00:00
|
|
|
getRowContext(){ return this.row_context;}
|
2021-04-07 21:45:50 +00:00
|
|
|
setColor(color){
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
setBgColor(bg_color){
|
|
|
|
this.bg_color = bg_color;
|
|
|
|
}
|
2021-04-18 08:31:25 +00:00
|
|
|
reset(hard_reset) {
|
2021-04-07 21:45:50 +00:00
|
|
|
//console.log("reset");
|
2021-02-04 00:29:35 +00:00
|
|
|
this.hide();
|
|
|
|
this.x = this.init_x;
|
|
|
|
this.y = this.init_y;
|
2021-04-18 08:31:25 +00:00
|
|
|
if (hard_reset) {
|
|
|
|
this.txt = this.init_txt;
|
|
|
|
}
|
2021-02-04 00:29:35 +00:00
|
|
|
this.show();
|
|
|
|
if(this.timeoutId != null){
|
2021-04-07 21:45:50 +00:00
|
|
|
clearTimeout(this.timeoutId);
|
2021-02-04 00:29:35 +00:00
|
|
|
}
|
2021-02-03 00:30:34 +00:00
|
|
|
}
|
|
|
|
show() {
|
2021-04-27 11:05:39 +00:00
|
|
|
g.setFontAlign(-1,-1,0);
|
2021-02-03 00:30:34 +00:00
|
|
|
g.setFont(this.font_name,this.font_size);
|
2021-04-07 21:45:50 +00:00
|
|
|
g.setColor(this.color[0],this.color[1],this.color[2]);
|
2021-02-03 00:30:34 +00:00
|
|
|
g.drawString(this.txt, this.x, this.y);
|
|
|
|
}
|
|
|
|
hide(){
|
2021-04-27 11:05:39 +00:00
|
|
|
g.setFontAlign(-1,-1,0);
|
2021-02-03 00:30:34 +00:00
|
|
|
g.setFont(this.font_name,this.font_size);
|
2021-04-07 21:45:50 +00:00
|
|
|
//console.log("bgcolor:" + this.bg_color);
|
|
|
|
g.setColor(this.bg_color[0],this.bg_color[1],this.bg_color[2]);
|
2021-02-03 00:30:34 +00:00
|
|
|
g.drawString(this.txt, this.x, this.y);
|
|
|
|
}
|
|
|
|
setText(txt){
|
|
|
|
this.txt = txt;
|
|
|
|
}
|
|
|
|
setTextPosition(txt,x,y){
|
|
|
|
this.hide();
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.txt = txt;
|
2022-07-30 15:08:43 +00:00
|
|
|
//console.log("setTextPosition: (" + x + "," + y + ") " + txt);
|
2021-02-03 00:30:34 +00:00
|
|
|
this.show();
|
|
|
|
}
|
2021-02-04 00:29:35 +00:00
|
|
|
setTextXPosition(txt,x){
|
|
|
|
this.hide();
|
|
|
|
this.x = x;
|
|
|
|
this.txt = txt;
|
2022-07-30 15:08:43 +00:00
|
|
|
//console.log("setTextXPosition: (" + x + ") " + txt);
|
2021-02-04 00:29:35 +00:00
|
|
|
this.show();
|
|
|
|
}
|
2021-02-06 00:36:22 +00:00
|
|
|
setTextYPosition(txt,y){
|
|
|
|
this.hide();
|
|
|
|
this.y = y;
|
|
|
|
this.txt = txt;
|
|
|
|
this.show();
|
|
|
|
}
|
2021-02-03 00:30:34 +00:00
|
|
|
moveTo(new_x,new_y){
|
|
|
|
this.tgt_x = new_x;
|
|
|
|
this.tgt_y = new_y;
|
2022-07-30 15:08:43 +00:00
|
|
|
//console.log("moveTo: (" + this.tgt_x + "," + this.tgt_y + ") ");
|
2021-02-03 00:30:34 +00:00
|
|
|
this._doMove();
|
|
|
|
}
|
2021-02-04 00:29:35 +00:00
|
|
|
moveToX(new_x){
|
|
|
|
this.tgt_x = new_x;
|
|
|
|
this._doMove();
|
|
|
|
}
|
|
|
|
moveToY(new_y){
|
|
|
|
this.tgt_y = new_y;
|
|
|
|
this._doMove();
|
|
|
|
}
|
2022-07-30 15:08:43 +00:00
|
|
|
scrollInFromBottom(txt,to_y){
|
|
|
|
if(to_y == null)
|
|
|
|
to_y = this.init_y;
|
|
|
|
|
|
|
|
//console.log("scrollInFromBottom y:" + this.y + "->" + to_y + " -> " + txt)
|
|
|
|
this.setTextPosition(txt, this.init_x, g.getHeight() - 2*this.speed_x);
|
|
|
|
this.moveTo(this.init_x,to_y);
|
|
|
|
}
|
2022-07-29 21:49:48 +00:00
|
|
|
scrollInFromLeft(txt,to_x){
|
2022-07-30 15:08:43 +00:00
|
|
|
if(to_x == null)
|
|
|
|
to_x = this.init_x;
|
|
|
|
|
|
|
|
//console.log("scrollInFromLeft x:" + this.x + "->" + to_x + " -> " + txt)
|
|
|
|
this.setTextPosition(txt, -txt.length * this.font_size - this.font_size, this.init_y);
|
|
|
|
this.moveTo(to_x,this.init_y);
|
2022-07-29 21:49:48 +00:00
|
|
|
}
|
|
|
|
scrollInFromRight(txt,to_x){
|
2022-07-30 15:08:43 +00:00
|
|
|
if(to_x == null)
|
|
|
|
to_x = this.init_x;
|
|
|
|
|
|
|
|
//console.log("scrollInFromRight x:" + this.x + "->" + to_x + " -> " + txt)
|
|
|
|
this.setTextPosition(txt, g.getWidth() + this.font_size, this.init_y);
|
|
|
|
this.moveTo(to_x,this.init_y);
|
2022-07-29 21:49:48 +00:00
|
|
|
}
|
|
|
|
scrollOffToLeft(){
|
2022-07-30 15:08:43 +00:00
|
|
|
//console.log("scrollOffToLeft");
|
|
|
|
this.moveTo(-this.txt.length * this.font_size, this.init_y);
|
2022-07-29 21:49:48 +00:00
|
|
|
}
|
|
|
|
scrollOffToRight(){
|
2022-07-30 15:08:43 +00:00
|
|
|
//console.log("scrollOffToRight");
|
|
|
|
this.moveTo(g.getWidth() + this.font_size, this.init_y);
|
|
|
|
}
|
|
|
|
scrollOffToBottom(){
|
|
|
|
//console.log("scrollOffToBottom");
|
|
|
|
this.moveTo(this.init_x,g.getHeight() + this.font_size);
|
2022-07-29 21:49:48 +00:00
|
|
|
}
|
2021-02-03 00:30:34 +00:00
|
|
|
onFinished(finished_callback){
|
|
|
|
this.finished_callback = finished_callback;
|
|
|
|
}
|
|
|
|
/**
|
2021-04-07 21:45:50 +00:00
|
|
|
* private internal method for directing the text move.
|
|
|
|
* It will see how far away we are from the target coords
|
|
|
|
* and move towards the target at the defined speed.
|
|
|
|
*/
|
2021-02-03 00:30:34 +00:00
|
|
|
_doMove(){
|
|
|
|
this.hide();
|
|
|
|
// move closer to the target in the x direction
|
2021-04-07 21:45:50 +00:00
|
|
|
var diff_x = this.tgt_x - this.x;
|
|
|
|
var finished_x = false;
|
2021-02-03 00:30:34 +00:00
|
|
|
if(Math.abs(diff_x) <= this.speed_x){
|
|
|
|
this.x = this.tgt_x;
|
|
|
|
finished_x = true;
|
|
|
|
} else {
|
|
|
|
if(diff_x > 0){
|
|
|
|
this.x += this.speed_x;
|
|
|
|
} else {
|
|
|
|
this.x -= this.speed_x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// move closer to the target in the y direction
|
2021-04-07 21:45:50 +00:00
|
|
|
var diff_y = this.tgt_y - this.y;
|
|
|
|
var finished_y = false;
|
2021-02-03 00:30:34 +00:00
|
|
|
if(Math.abs(diff_y) <= this.speed_y){
|
|
|
|
this.y = this.tgt_y;
|
|
|
|
finished_y = true;
|
|
|
|
} else {
|
|
|
|
if(diff_y > 0){
|
|
|
|
this.y += this.speed_y;
|
|
|
|
} else {
|
|
|
|
this.y -= this.speed_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.show();
|
2021-02-04 00:29:35 +00:00
|
|
|
this.timeoutId = null;
|
2021-04-07 21:45:50 +00:00
|
|
|
var finished = finished_x & finished_y;
|
2021-02-03 00:30:34 +00:00
|
|
|
if(!finished){
|
2021-02-04 00:29:35 +00:00
|
|
|
this.timeoutId = setTimeout(this._doMove.bind(this), this.freq_millis);
|
2021-02-03 00:30:34 +00:00
|
|
|
} else if(this.finished_callback != null){
|
2021-04-07 21:45:50 +00:00
|
|
|
//console.log("finished - calling:" + this.finished_callback);
|
2021-02-03 00:30:34 +00:00
|
|
|
this.finished_callback.call();
|
|
|
|
this.finished_callback = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 21:49:48 +00:00
|
|
|
function bangleVersion(){
|
|
|
|
return (g.getHeight()>200)? 1 : 2;
|
|
|
|
}
|
2022-07-30 15:08:43 +00:00
|
|
|
var DISPLAY_TEXT_X = 20;
|
2022-07-29 21:49:48 +00:00
|
|
|
|
|
|
|
var style = {
|
2022-07-30 10:59:43 +00:00
|
|
|
fg_color: (row_props)=>(row_props.major_minor === 'major')? main_color(): other_color(),
|
2022-07-30 15:08:43 +00:00
|
|
|
clock_text_speed: 5,
|
2022-07-29 21:49:48 +00:00
|
|
|
y_init: (bangleVersion()<2)? 34 : 50,
|
2022-07-30 10:59:43 +00:00
|
|
|
//row_height: (row_props)=>(row_props.major_minor == 'major')? (bangleVersion()<2)? 40 : 30: (bangleVersion()<2)? 35 : 25,
|
|
|
|
row_height: (row_props)=>(row_props.major_minor === 'major')? (bangleVersion()<2)? 40 : 50: (bangleVersion()<2)? 35 : 15,
|
2022-07-30 15:08:43 +00:00
|
|
|
//row_y: (row_props, last_y, row_height) => row_props.info_type === 'date'? g.getHeight() - 2*row_height : last_y,
|
|
|
|
row_y: (row_props, last_y, row_height) => row_props.info_type === 'date'? 34 : last_y + 20,
|
|
|
|
row_x: (row_props, last_x) => row_props.info_type === 'date'? 60 : last_x,
|
|
|
|
// random
|
|
|
|
scrollIn: (d,txt)=> {
|
|
|
|
var random = Math.random();
|
|
|
|
if (d.getRowContext().info_type === 'date') {
|
|
|
|
if (random > 0.5)
|
|
|
|
d.scrollInFromRight(txt);
|
|
|
|
else
|
|
|
|
d.scrollInFromLeft(txt);
|
|
|
|
} else {
|
|
|
|
if (random < 0.33) {
|
|
|
|
d.scrollInFromRight(txt);
|
|
|
|
} else if (random < 0.66) {
|
|
|
|
d.scrollInFromLeft(txt);
|
|
|
|
} else {
|
|
|
|
d.scrollInFromBottom(txt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
//scrollIn: (d,txt)=>(d.getRowContext().info_type === 'date')? d.scrollInFromRight(txt) : d.scrollInFromBottom(txt),
|
|
|
|
|
|
|
|
//scrollIn: (d,txt)=>d.scrollInFromRight(txt),
|
2022-07-29 21:49:48 +00:00
|
|
|
//scrollIn: (d,txt,to_x)=>d.scrollInFromLeft(txt,to_x),
|
2022-07-30 15:08:43 +00:00
|
|
|
scrollOff: (d)=>{
|
|
|
|
var random = Math.random();
|
|
|
|
if (d.getRowContext().info_type === 'date') {
|
|
|
|
if (random > 0.5)
|
|
|
|
d.scrollOffToRight();
|
|
|
|
else
|
|
|
|
d.scrollOffToLeft();
|
|
|
|
} else {
|
|
|
|
if (random < 0.33) {
|
|
|
|
d.scrollOffToRight();
|
|
|
|
} else if (random < 0.66) {
|
|
|
|
d.scrollOffToLeft();
|
|
|
|
} else {
|
|
|
|
d.scrollOffToBottom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//scrollOff: (d)=>(d.getRowContext().info_type === 'date')? d.scrollOffToLeft() : d.scrollOffToBottom()
|
|
|
|
//scrollOff: (d)=>d.scrollOffToLeft()
|
2022-07-29 21:49:48 +00:00
|
|
|
//scrollOff: (d)=>d.scrollOffToRight()
|
|
|
|
};
|
|
|
|
|
2021-04-07 21:45:50 +00:00
|
|
|
// a list of display rows
|
2022-07-26 23:01:56 +00:00
|
|
|
var row_displays;
|
|
|
|
|
2022-07-29 21:49:48 +00:00
|
|
|
function init_display() {
|
2021-09-29 08:19:43 +00:00
|
|
|
row_displays = [];
|
2022-07-30 15:08:43 +00:00
|
|
|
var y = style.y_init;
|
2022-07-29 21:49:48 +00:00
|
|
|
var date_rows = date_formatter.formatDate(new Date());
|
2022-07-27 22:37:55 +00:00
|
|
|
for (var i=0;i<date_rows.length;i++) {
|
2022-07-30 09:53:21 +00:00
|
|
|
var row_props = date_formatter.rowProperties(i);
|
|
|
|
console.log("row info[" + i + "]=" + row_props.major_minor)
|
|
|
|
var row_height = style.row_height(row_props);
|
2022-07-30 10:59:43 +00:00
|
|
|
y = style.row_y(row_props,y,row_height);
|
2022-07-30 15:08:43 +00:00
|
|
|
var x = style.row_x(row_props,DISPLAY_TEXT_X);
|
2022-07-30 09:53:21 +00:00
|
|
|
var color = style.fg_color(row_props);
|
2022-07-27 22:37:55 +00:00
|
|
|
row_displays.push(
|
2022-07-30 15:08:43 +00:00
|
|
|
new ShiftText(x,
|
2022-07-27 22:37:55 +00:00
|
|
|
y,
|
|
|
|
'',
|
|
|
|
"Vector",
|
|
|
|
row_height,
|
2022-07-30 15:08:43 +00:00
|
|
|
style.clock_text_speed,
|
|
|
|
style.clock_text_speed,
|
2022-07-27 22:37:55 +00:00
|
|
|
10,
|
2022-07-30 09:53:21 +00:00
|
|
|
color,
|
|
|
|
bg_color(),
|
|
|
|
row_props
|
2022-07-27 22:37:55 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
y += row_height;
|
2021-09-29 08:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 17:04:45 +00:00
|
|
|
|
2021-02-06 00:36:22 +00:00
|
|
|
|
2021-04-07 21:45:50 +00:00
|
|
|
function nextColorTheme(){
|
|
|
|
color_scheme_index += 1;
|
2022-07-29 21:49:48 +00:00
|
|
|
if(color_scheme_index >= color_schemes.length){
|
2021-04-07 21:45:50 +00:00
|
|
|
color_scheme_index = 0;
|
2021-02-04 00:29:35 +00:00
|
|
|
}
|
2022-07-29 21:49:48 +00:00
|
|
|
//console.log("changing color scheme to " + color_schemes[color_scheme_index].name)
|
|
|
|
updateColorScheme();
|
2021-04-19 11:05:50 +00:00
|
|
|
reset_clock(true);
|
|
|
|
draw_clock();
|
|
|
|
}
|
|
|
|
|
2022-07-29 21:49:48 +00:00
|
|
|
function updateColorScheme(){
|
|
|
|
var bgcolor = bg_color();
|
|
|
|
for(var i=0; i<row_displays.length; i++){
|
2022-07-30 09:53:21 +00:00
|
|
|
row_displays[i].setColor(style.fg_color(row_displays[i].getRowContext()) );
|
2022-07-29 21:49:48 +00:00
|
|
|
row_displays[i].setBgColor(bgcolor);
|
|
|
|
}
|
|
|
|
g.setColor(bgcolor[0],bgcolor[1],bgcolor[2]);
|
|
|
|
g.fillRect(0, 24, g.getWidth(), g.getHeight());
|
2021-02-09 07:22:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 08:31:25 +00:00
|
|
|
function reset_clock(hard_reset){
|
|
|
|
console.log("reset_clock hard_reset:" + hard_reset);
|
|
|
|
|
2022-07-29 21:49:48 +00:00
|
|
|
updateColorScheme();
|
2021-04-18 08:31:25 +00:00
|
|
|
if(!hard_reset && last_draw_time != null){
|
|
|
|
// If its not a hard reset then we want to reset the
|
|
|
|
// rows set to the last time. If the last time is too long
|
|
|
|
// ago then we fast forward to 1 min ago.
|
|
|
|
// In this way the watch wakes by scrolling
|
|
|
|
// off the last time and scroll on the new time
|
|
|
|
var reset_time = last_draw_time;
|
|
|
|
var last_minute_millis = Date.now() - 60000;
|
|
|
|
if(reset_time.getTime() < last_minute_millis){
|
2021-04-19 11:05:50 +00:00
|
|
|
reset_time = display_time(new Date(last_minute_millis));
|
2021-04-18 08:31:25 +00:00
|
|
|
}
|
|
|
|
var rows = date_formatter.formatDate(reset_time);
|
|
|
|
for (var i = 0; i < rows.length; i++) {
|
|
|
|
row_displays[i].hide();
|
2022-07-30 15:08:43 +00:00
|
|
|
row_displays[i].speed_x = style.clock_text_speed;
|
|
|
|
row_displays[i].x = row_displays[i].init_x;
|
2021-04-18 08:31:25 +00:00
|
|
|
row_displays[i].y = row_displays[i].init_y;
|
|
|
|
if(row_displays[i].timeoutId != null){
|
|
|
|
clearTimeout(row_displays[i].timeoutId);
|
|
|
|
}
|
|
|
|
row_displays[i].setText(rows[i]);
|
|
|
|
row_displays[i].show();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// do a hard reset and clear everything out
|
|
|
|
for (var i = 0; i < row_displays.length; i++) {
|
2022-07-30 15:08:43 +00:00
|
|
|
row_displays[i].speed_x = style.clock_text_speed;
|
2021-04-18 08:31:25 +00:00
|
|
|
row_displays[i].reset(hard_reset);
|
|
|
|
}
|
2021-02-04 01:39:43 +00:00
|
|
|
}
|
2021-04-07 21:45:50 +00:00
|
|
|
reset_commands();
|
2021-02-04 00:29:35 +00:00
|
|
|
}
|
2021-02-01 02:16:56 +00:00
|
|
|
|
2021-04-07 21:45:50 +00:00
|
|
|
let last_draw_time = null;
|
2021-04-19 11:05:50 +00:00
|
|
|
const next_minute_boundary_secs = 10;
|
|
|
|
|
|
|
|
function display_time(date){
|
|
|
|
if(date.getSeconds() > 60 - next_minute_boundary_secs){
|
|
|
|
console.log("forwarding to next minute");
|
|
|
|
return new Date(date.getTime() + next_minute_boundary_secs * 1000);
|
|
|
|
} else {
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 21:45:50 +00:00
|
|
|
|
2021-02-01 02:16:56 +00:00
|
|
|
function draw_clock(){
|
2021-04-07 21:45:50 +00:00
|
|
|
var date = new Date();
|
2021-04-19 11:05:50 +00:00
|
|
|
|
|
|
|
// we don't want the time to be displayed
|
|
|
|
// and then immediately be trigger another time
|
2021-04-07 21:45:50 +00:00
|
|
|
if(last_draw_time != null &&
|
2021-04-19 11:05:50 +00:00
|
|
|
Date.now() - last_draw_time.getTime() < next_minute_boundary_secs * 1000 &&
|
2021-04-07 21:45:50 +00:00
|
|
|
has_commands() ){
|
|
|
|
console.log("skipping draw clock");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
last_draw_time = date;
|
|
|
|
}
|
|
|
|
reset_commands();
|
2021-04-19 11:05:50 +00:00
|
|
|
date = display_time(date);
|
|
|
|
console.log("draw_clock:" + last_draw_time.toISOString() + " display:" + date.toISOString());
|
2022-06-27 17:04:45 +00:00
|
|
|
|
2021-04-07 21:45:50 +00:00
|
|
|
var rows = date_formatter.formatDate(date);
|
|
|
|
var display;
|
|
|
|
for (var i = 0; i < rows.length; i++) {
|
2021-02-04 00:29:35 +00:00
|
|
|
display = row_displays[i];
|
2021-04-07 21:45:50 +00:00
|
|
|
var txt = rows[i];
|
|
|
|
//console.log(i + "->" + txt);
|
2021-02-04 00:29:35 +00:00
|
|
|
display_row(display,txt);
|
|
|
|
}
|
2021-04-07 21:45:50 +00:00
|
|
|
// If the dateformatter has not returned enough
|
2022-07-26 23:01:56 +00:00
|
|
|
// rows then treat the remaining rows as empty
|
2021-04-11 14:18:30 +00:00
|
|
|
for (var j = i; j < row_displays.length; j++) {
|
2021-02-11 22:54:53 +00:00
|
|
|
display = row_displays[j];
|
2021-04-07 21:45:50 +00:00
|
|
|
//console.log(i + "->''(empty)");
|
2021-02-11 22:54:53 +00:00
|
|
|
display_row(display,'');
|
|
|
|
}
|
2021-04-07 21:45:50 +00:00
|
|
|
next_command();
|
2021-02-17 01:43:21 +00:00
|
|
|
//console.log(date);
|
2021-02-04 00:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function display_row(display,txt){
|
2021-04-11 14:18:30 +00:00
|
|
|
if(display == null) {
|
2022-07-29 21:49:48 +00:00
|
|
|
console.log("no display for text:" + txt);
|
2021-04-11 14:18:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-26 23:01:56 +00:00
|
|
|
if(display.txt == null || display.txt === ''){
|
|
|
|
if(txt !== '') {
|
2021-04-09 22:33:21 +00:00
|
|
|
command_stack_high_priority.unshift(
|
|
|
|
function () {
|
|
|
|
//console.log("move in new:" + txt);
|
|
|
|
display.onFinished(next_command);
|
2022-07-29 21:49:48 +00:00
|
|
|
//display.scrollInFromRight(txt, DISPLAY_TEXT_X);
|
2022-07-30 15:08:43 +00:00
|
|
|
style.scrollIn(display,txt)
|
2021-04-09 22:33:21 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2022-07-26 23:01:56 +00:00
|
|
|
} else if(txt !== display.txt && display.txt != null){
|
2021-04-07 21:45:50 +00:00
|
|
|
command_stack_high_priority.push(
|
|
|
|
function(){
|
|
|
|
//console.log("move out:" + txt);
|
|
|
|
display.onFinished(next_command);
|
2022-07-29 21:49:48 +00:00
|
|
|
//display.moveToX(-display.txt.length * display.font_size);
|
|
|
|
//display.scrollOffToLeft();
|
|
|
|
style.scrollOff(display);
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
command_stack_low_priority.push(
|
|
|
|
function(){
|
|
|
|
//console.log("move in:" + txt);
|
|
|
|
display.onFinished(next_command);
|
2022-07-30 15:08:43 +00:00
|
|
|
style.scrollIn(display,txt);
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
2021-02-03 00:30:34 +00:00
|
|
|
);
|
|
|
|
} else {
|
2021-04-07 21:45:50 +00:00
|
|
|
command_stack_high_priority.push(
|
|
|
|
function(){
|
|
|
|
//console.log("move in2:" + txt);
|
2022-07-30 15:08:43 +00:00
|
|
|
display.setTextPosition(txt,display.init_x, display.init_y);
|
2021-04-07 21:45:50 +00:00
|
|
|
next_command();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* called from load_settings on startup to
|
|
|
|
* set the color scheme to named value
|
|
|
|
*/
|
|
|
|
function set_colorscheme(colorscheme_name){
|
|
|
|
console.log("setting color scheme:" + colorscheme_name);
|
|
|
|
for (var i=0; i < color_schemes.length; i++) {
|
2022-07-26 23:01:56 +00:00
|
|
|
if(color_schemes[i].name === colorscheme_name){
|
2021-04-07 21:45:50 +00:00
|
|
|
color_scheme_index = i;
|
|
|
|
console.log("match");
|
2022-07-29 21:49:48 +00:00
|
|
|
updateColorScheme();
|
2021-04-07 21:45:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-02-01 02:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 23:01:56 +00:00
|
|
|
const Locale = require('locale');
|
|
|
|
class DigitDateTimeFormatter {
|
2022-07-30 09:53:21 +00:00
|
|
|
constructor() {
|
|
|
|
this.row_props =[
|
2022-07-30 10:59:43 +00:00
|
|
|
{major_minor: 'major', info_type: 'time'},
|
|
|
|
{major_minor: 'minor', info_type: 'date'},
|
2022-07-30 09:53:21 +00:00
|
|
|
]
|
|
|
|
}
|
2022-07-26 23:01:56 +00:00
|
|
|
name(){return "Digital";}
|
|
|
|
shortName(){return "digit";}
|
|
|
|
|
|
|
|
format00(num){
|
|
|
|
var value = (num | 0);
|
|
|
|
if(value > 99 || value < 0)
|
|
|
|
throw "must be between in range 0-99";
|
|
|
|
if(value < 10)
|
|
|
|
return "0" + value.toString();
|
|
|
|
else
|
|
|
|
return value.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
formatDate(now){
|
|
|
|
var hours = now.getHours() ;
|
|
|
|
|
|
|
|
var time_txt = this.format00(hours) + ":" + this.format00(now.getMinutes());
|
|
|
|
var date_txt = Locale.dow(now,1) + " " + this.format00(now.getDate());
|
|
|
|
return [time_txt,date_txt];
|
|
|
|
}
|
2022-07-30 09:53:21 +00:00
|
|
|
|
|
|
|
rowProperties(row_no) {
|
|
|
|
return this.row_props[row_no];
|
|
|
|
}
|
2022-07-26 23:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var date_formatter = new DigitDateTimeFormatter();
|
|
|
|
function set_dateformat(shortname){
|
|
|
|
console.log("setting date format:" + shortname);
|
|
|
|
try {
|
|
|
|
if (date_formatter == null || date_formatter.shortName() !== shortname) {
|
|
|
|
date_formatter = require("slidingtext.locale." + shortname + ".js");
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
2022-07-26 23:01:56 +00:00
|
|
|
} catch(e){
|
|
|
|
console.log("Failed to load " + shortname);
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 17:04:45 +00:00
|
|
|
var enable_live_controls = false;
|
2021-04-07 21:45:50 +00:00
|
|
|
const PREFERENCE_FILE = "slidingtext.settings.json";
|
|
|
|
/**
|
|
|
|
* Called on startup to set the watch to the last preference settings
|
|
|
|
*/
|
2022-07-26 23:01:56 +00:00
|
|
|
function load_settings() {
|
2021-09-29 08:19:43 +00:00
|
|
|
var setScheme = false;
|
2022-07-26 23:01:56 +00:00
|
|
|
try {
|
2022-06-27 17:04:45 +00:00
|
|
|
var settings = require("Storage").readJSON(PREFERENCE_FILE);
|
2022-07-26 23:01:56 +00:00
|
|
|
if (settings != null) {
|
2021-04-07 21:45:50 +00:00
|
|
|
console.log("loaded:" + JSON.stringify(settings));
|
2022-07-26 23:01:56 +00:00
|
|
|
if (settings.date_format != null) {
|
|
|
|
set_dateformat(settings.date_format);
|
|
|
|
init_display();
|
|
|
|
}
|
|
|
|
if (settings.color_scheme != null) {
|
2021-04-07 21:45:50 +00:00
|
|
|
set_colorscheme(settings.color_scheme);
|
2021-09-29 08:19:43 +00:00
|
|
|
setScheme = true;
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
2022-07-26 23:01:56 +00:00
|
|
|
if (settings.enable_live_controls == null) {
|
2022-07-29 21:49:48 +00:00
|
|
|
settings.enable_live_controls = (bangleVersion() <= 1);
|
2022-06-27 17:04:45 +00:00
|
|
|
}
|
|
|
|
enable_live_controls = settings.enable_live_controls;
|
2021-04-07 21:45:50 +00:00
|
|
|
} else {
|
|
|
|
console.log("no settings to load");
|
2022-07-01 18:51:00 +00:00
|
|
|
enable_live_controls = (bangleVersion() <= 1);
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
2022-07-01 18:51:00 +00:00
|
|
|
console.log("enable_live_controls=" + enable_live_controls);
|
2022-07-26 23:01:56 +00:00
|
|
|
} catch (e) {
|
2021-04-07 21:45:50 +00:00
|
|
|
console.log("failed to load settings:" + e);
|
|
|
|
}
|
2022-07-26 23:01:56 +00:00
|
|
|
if(row_displays == null){
|
|
|
|
init_display();
|
|
|
|
}
|
2021-09-29 08:19:43 +00:00
|
|
|
// just set up as default
|
2022-07-26 23:01:56 +00:00
|
|
|
if (!setScheme) {
|
|
|
|
init_display();
|
2022-07-29 21:49:48 +00:00
|
|
|
updateColorScheme();
|
2022-07-26 23:01:56 +00:00
|
|
|
}
|
2022-07-29 21:49:48 +00:00
|
|
|
enable_live_controls = true
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called on button press to save down the last preference settings
|
|
|
|
*/
|
|
|
|
function save_settings(){
|
|
|
|
var settings = {
|
2022-06-28 10:08:43 +00:00
|
|
|
date_format : date_formatter.shortName(),
|
2021-04-07 21:45:50 +00:00
|
|
|
color_scheme : color_schemes[color_scheme_index].name,
|
2022-06-27 17:04:45 +00:00
|
|
|
enable_live_controls: enable_live_controls
|
2021-04-07 21:45:50 +00:00
|
|
|
};
|
|
|
|
console.log("saving:" + JSON.stringify(settings));
|
|
|
|
require("Storage").writeJSON(PREFERENCE_FILE,settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
function button3pressed() {
|
2022-07-29 21:49:48 +00:00
|
|
|
console.log("button3pressed enable_live_controls=" + enable_live_controls);
|
2022-06-27 17:04:45 +00:00
|
|
|
if (enable_live_controls) {
|
|
|
|
nextColorTheme();
|
|
|
|
reset_clock(true);
|
|
|
|
draw_clock();
|
|
|
|
save_settings();
|
|
|
|
}
|
2021-04-07 21:45:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 00:36:22 +00:00
|
|
|
// The interval reference for updating the clock
|
|
|
|
let intervalRef = null;
|
|
|
|
|
2021-02-01 02:16:56 +00:00
|
|
|
function clearTimers(){
|
2021-04-18 08:31:25 +00:00
|
|
|
if(intervalRef != null) {
|
2021-02-01 02:16:56 +00:00
|
|
|
clearInterval(intervalRef);
|
|
|
|
intervalRef = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function startTimers(){
|
2021-04-07 21:45:50 +00:00
|
|
|
var date = new Date();
|
|
|
|
var secs = date.getSeconds();
|
|
|
|
var nextMinuteStart = 60 - secs;
|
2021-02-17 01:43:21 +00:00
|
|
|
//console.log("scheduling clock draw in " + nextMinuteStart + " seconds");
|
2021-02-01 02:16:56 +00:00
|
|
|
setTimeout(scheduleDrawClock,nextMinuteStart * 1000);
|
|
|
|
draw_clock();
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:05:50 +00:00
|
|
|
/**
|
|
|
|
* confirms that a redraw is needed by checking the last redraw time and
|
|
|
|
* the lcd state of the UI
|
|
|
|
* @returns {boolean|*}
|
|
|
|
*/
|
|
|
|
function shouldRedraw(){
|
|
|
|
return last_draw_time != null &&
|
|
|
|
Date.now() - last_draw_time.getTime() > next_minute_boundary_secs * 1000
|
|
|
|
&& Bangle.isLCDOn();
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:30:34 +00:00
|
|
|
function scheduleDrawClock(){
|
2021-04-18 08:31:25 +00:00
|
|
|
clearTimers();
|
|
|
|
if (Bangle.isLCDOn()) {
|
2021-04-19 11:05:50 +00:00
|
|
|
console.log("schedule draw of clock");
|
2021-04-18 08:31:25 +00:00
|
|
|
intervalRef = setInterval(() => {
|
2022-07-29 21:49:48 +00:00
|
|
|
if (!shouldRedraw()) {
|
|
|
|
console.log("draw clock callback - skipped redraw");
|
|
|
|
} else {
|
|
|
|
console.log("draw clock callback");
|
|
|
|
draw_clock();
|
|
|
|
}
|
|
|
|
}, 60 * 1000
|
2021-04-19 11:05:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (shouldRedraw()) {
|
|
|
|
draw_clock();
|
|
|
|
} else {
|
|
|
|
console.log("scheduleDrawClock - skipped redraw");
|
|
|
|
}
|
2021-04-18 08:31:25 +00:00
|
|
|
} else {
|
|
|
|
console.log("scheduleDrawClock - skipped not visible");
|
|
|
|
}
|
2021-02-01 02:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Bangle.on('lcdPower', (on) => {
|
|
|
|
if (on) {
|
|
|
|
console.log("lcdPower: on");
|
|
|
|
Bangle.drawWidgets();
|
2021-04-18 08:31:25 +00:00
|
|
|
reset_clock(false);
|
2021-02-01 02:16:56 +00:00
|
|
|
startTimers();
|
|
|
|
} else {
|
|
|
|
console.log("lcdPower: off");
|
2021-04-18 08:31:25 +00:00
|
|
|
reset_clock(false);
|
2021-02-01 02:16:56 +00:00
|
|
|
clearTimers();
|
|
|
|
}
|
|
|
|
});
|
2021-04-07 21:45:50 +00:00
|
|
|
|
2021-02-01 02:16:56 +00:00
|
|
|
g.clear();
|
2021-04-07 21:45:50 +00:00
|
|
|
load_settings();
|
2021-02-01 02:16:56 +00:00
|
|
|
Bangle.loadWidgets();
|
|
|
|
Bangle.drawWidgets();
|
2021-04-07 21:45:50 +00:00
|
|
|
|
2021-02-01 02:16:56 +00:00
|
|
|
startTimers();
|
2021-07-28 08:55:22 +00:00
|
|
|
// Show launcher when button pressed
|
2021-09-29 08:19:43 +00:00
|
|
|
Bangle.setUI("clockupdown", d=>{
|
|
|
|
if (d>0) button3pressed();
|
|
|
|
});
|