using Toybox.Application as App; using Toybox.Application.Storage; using Toybox.Graphics as Gfx; using Toybox.Lang; using Toybox.Math; using Toybox.System as Sys; using Toybox.Time; using Toybox.Time.Gregorian as Cal; using Toybox.Weather; module C { // // Constants // public var WHITE = 0xffffff; // Gfx.COLOR_WHITE; public var BLACK = 0x000000; // Gfx.COLOR_BLACK; public var RED = 0xff0000; // Gfx.COLOR_RED; public var GREEN = 0x00ff00; // Gfx.COLOR_GREEN; public var BLUE = 0x0000ff; // Gfx.COLOR_BLUE; public var YELLOW = 0xffff00; // Gfx.COLOR_YELLOW; public var ORANGE = 0xff8800; // Gfx.COLOR_ORANGE; public var TRANSPARENT = Gfx.COLOR_TRANSPARENT; // // Settings variables // public var app_ver; public var ampm; public var ccw; public var quad_ul; public var quad_ur; public var quad_lr; public var quad_ll; public var temp; public var tz_cache; public var tz1 = 0; public var tz1_dst; public var tz2 = 0; public var tz2_dst; // // Dimensions of the watch // public var radius; public var center_x; public var center_y; public var dot_sz; // // Timezone info // public var tz1_info; public var tz2_info; // // Debug info // (:debug) var debug = true; (:release) var debug = false; public var dbg_ok = false; // // Debugging moon phase // var dbg_moon = false; var dbg_time; var dbg_dsecs; var dbg_msecs; var dbg_secs; public enum { DATA_NONE = 0, DATA_BATTERY, DATA_HEART, DATA_MOON, DATA_STEPS, DATA_CALORIES, DATA_DIST, DATA_ASCENT, DATA_ACTIVE, DATA_RESP, DATA_TEMP } public enum { GRAVITY_UL, GRAVITY_UR, GRAVITY_LR, GRAVITY_LL } public function logger(s) { if (debug) { Sys.println("DDD:" + s); } } public function pr_date(t, d) { if (d instanceof Time.Moment) { d = Cal.info(d, Time.FORMAT_SHORT); } C.logger(t + ":" + d.month + "/" + d.day + "/" + d.year + " " + d.hour + ":" + d.min.format("%02d") + ":" + d.sec.format("%02d")); } public function pr_memory(pre, suf) { var total, used, free; var scale; if (suf == null) { suf = 'u'; } switch (suf) { case 'k': case 'K': scale = 1000; break; case 'm': case 'M': scale = 1000*1000; break; case 'g': case 'G': scale = 1000*1000*1000; break; default: scale = 1; suf = ""; break; } var stats = Sys.getSystemStats(); total = stats.totalMemory / scale; used = stats.usedMemory / scale; free = stats.freeMemory / scale; logger(pre + ":" + total + suf + "=" + used + suf + "+" + free + suf); } public function pr_mem(pre) { pr_memory(pre, 'k'); } public function pr_xy(xy) { var v; var len = xy.size(); for (var i = 0; i < len; i += 1) { v = xy[i]; logger("xy[i] = " + v[0] + ", " + v[1]); } } function to_cart(r, a) { var x = r * Math.cos(a); var y = r * Math.sin(a); return [ x, y ]; } function to_polar(x, y) { var r = Math.sqrt((x*x) + (y*y)); var a = Math.atan2(x, y); return [ r, a ]; } function translate(vert, x, y, a) { var vs, p, c; var l = vert.size(); var v = new [l]; for (var i = 0; i < l; i += 1) { vs = vert[i]; p = C.to_polar(vs[0], vs[1]); c = C.to_cart(p[0], p[1] + a); c[0] += C.center_x; c[1] += C.center_y; v[i] = c; } return v; } function alpha(c, a) { return c | (a << 24); } function utc(now, off) { now = now.subtract(new Time.Duration(Sys.getClockTime().timeZoneOffset)); now = now.add(new Time.Duration(off)); return now; } // // Properties: // // ampm - what AM/PM indication to disply // ccw - hands run counter clockwise // tz1 - main timezone // tz2 - secondary timezone // cache - timezone cache // // Storage values: // // TZ1old - old main timezone // TZ2old - old secondary timezone // function settings() { var app = App.getApp(); app_ver = app.getProperty("version"); dbg_ok = app.getProperty("dbg_enable") ? true : false; ampm = app.getProperty("ampm"); if (ampm == null) { ampm = 0; } ccw = app.getProperty("ccw") ? -1 : 1; quad_ul = app.getProperty("quad_ul"); quad_ur = app.getProperty("quad_ur"); quad_lr = app.getProperty("quad_lr"); quad_ll = app.getProperty("quad_ll"); temp = app.getProperty("temp"); //quad_ur = C.DATA_DIST; //quad_ll = C.DATA_RESP; tz_cache = app.getProperty("tz_cache") ? true : false; tz1 = app.getProperty("tz1"); tz1_dst = app.getProperty("tz1_dst"); tz2 = app.getProperty("tz2"); tz2_dst = app.getProperty("tz2_dst"); logger("settings:am/pm - " + ampm); logger(" ccw - " + ccw); logger(" tz1 - " + tz1); logger(" tz2 - " + tz2); logger(" dbg - " + dbg_ok); var tz = new Tz(); tz1_info = tz.get(tz1, tz1_dst); tz2_info = tz.get(tz2, tz2_dst); var tz1_old = Storage.getValue("TZ1old"); var tz2_old = Storage.getValue("TZ2old"); if (tz1_old == null) { tz1_old = 0; tz2_old = 0; Storage.setValue("TZ1old", 0); Storage.setValue("TZ2old", 0); } //C.logger("tz2 old:" + tz2_old + " == " + tz2); if (tz2 != tz2_old) { var cache = new TzCache(); var idx = cache.index(tz2_info[4]); C.logger("tz2 info:" + tz2_info[4]); if (idx < 0) { cache.push(tz2_info[4], tz2); } //cache.prt(5); Storage.setValue("TZ2old", tz2); } } function do_dbg(partial, bitm, sec) { if (dbg_ok) { var str = partial + "," + bitm + "," + sec; var app = App.getApp(); app.setProperty("dbg_line", str); } } // // Note - we don't have to set the global variables for // the properties, this routine will always be folowed // by a call to settings() which will set the global // variables at that time. function reconfig(p_ampm, p_ccw, p_cache, p_tz1, p_tz2) { var info; C.logger("reconfig:" + p_ampm + ", " + p_ccw + ", " + p_cache + ", " + p_tz1 + ", " + p_tz2); var app = App.getApp(); var cache = new TzCache(); if (p_cache) { app.setProperty("tz_cache", false); cache.reset(); p_tz1 = 0; p_tz2 = 0; } app.setProperty("ampm", p_ampm); app.setProperty("ccw", p_ccw == -1); info = cache.get(p_tz1); tz1 = info[1]; app.setProperty("tz1", tz1); Storage.setValue("TZ1old", tz1); info = cache.get(p_tz2); tz2 = info[1]; app.setProperty("tz2", tz2); Storage.setValue("TZ2old", tz2); } function time_adj(now, tz_info) { //var info = Cal.info(Time.now(), Time.FORMAT_SHORT); //C.logger("time_adj:" + info.hour + ":" + info.min + " " + info.day + ", " + info.day_of_week); //C.logger("time_adj:" + tz_info[0] + ", " + tz_info[1] + ", " + tz_info[2] + ", " + tz_info[3]); if (tz_info[0] != 0) { now = utc(now, tz_info[0]*60); } var time = new MyTime(now, tz_info); return time; } function get_temp(now) { var w = Weather.getCurrentConditions(); if (w == null) { return [ "--", Rez.Drawables.temp, WHITE ]; } var t = w.temperature; if (t == null) { return [ "-", Rez.Drawables.temp, WHITE ]; } if (temp == 1) { t = ((t * 9.0)/5.0) + 32.0; } var delta = now.value() - w.observationTime.value(); var icon = Rez.Drawables.temp_red; var color = RED; if (delta < ((30*60) + (5*60))) { icon = Rez.Drawables.temp_green; color = GREEN; } else if (delta < ((1*60*60) + (5*60))) { icon = Rez.Drawables.temp_yellow; color = YELLOW; } else if (delta < ((2*60*60) + (5*60))) { icon = Rez.Drawables.temp_orange; color = ORANGE; } return [ t.format("%.0f"), icon, color ]; } function get_now() { var now = Time.now(); if (!dbg_moon) { return now; } var DAY_SECS = 60 * 60 * 24; if (dbg_dsecs == null) { dbg_dsecs = (now.value()/DAY_SECS).toNumber(); dbg_msecs = 0; dbg_secs = 0; } if (now.value() != dbg_time) { dbg_time = now.value(); dbg_secs += 1; if (dbg_secs >= 60) { dbg_secs = 0; } } if (dbg_secs == 4) { dbg_dsecs += 1; dbg_msecs += 1; dbg_secs = 59; } var nt = (dbg_dsecs * DAY_SECS) + (dbg_msecs * 60) + dbg_secs; //var tinfo = Cal.info(new Time.Moment(nt), Time.FORMAT_SHORT); //C.logger("get_now:" + tinfo.min + ":" + tinfo.sec + " => " + nt); return new Time.Moment(nt); } function set_fill(dc, color) { if (dc has :setFill) { dc.setFill(color); } else { dc.setColor(color, TRANSPARENT); } } }