# @name anothertime # @desc Always see time and other widgets # @author toinux@gmail.com # @version 1.0 # @config sc color "Seconds color" default=#FF00FF # @config ta select "Time animation" options=scroll,fade default=fade # @config tad slider "Time animation duration" min=100 max=5000 default=500 unit=ms # @config wsty select "Week style" default=dotted2 options=large,progress,dotted,dotted2 # @config wc color "Week days color" default=#00FFFF # @config wdc color "Current day color" default=#FF00FF # @config ssun bool "Week starts Sunday" default=false import math class MyApp var cur, prev var hh, mm var cur_min var widgets var index, wticks var current_widget, previous_widget var transition_start_ms, transition_duration_ms var week_style, week_color, day_color, start_sunday var sc, ta, tad var tc var temp_label, hum_label var day_label def init() self.cur = ["0", "0", "0", "0"] self.prev = ["0", "0", "0", "0"] self.cur_min = nil self.widgets = [ {"name": "date", "duration": 2}, {"name": "temperature", "duration": 6}, {"name": "humidity", "duration": 2} ] self.index = 0 self.current_widget = self.widgets[0]["name"] self.previous_widget = nil self.wticks = self.widgets[0]["duration"] self.transition_start_ms = nil self.transition_duration_ms = 1000 self.sc = store.get("sc") self.ta = store.get("ta") self.tad = store.get("tad") self.tc = settings.get("timeColor") if self.tc == nil self.tc = settings.get("textColor") end self.week_style = store.get("wsty") self.week_color = store.get("wc") self.day_color = store.get("wdc") self.start_sunday = store.get("ssun") self.temp_label = nil self.hum_label = nil self.day_label = str(day()) end def loop() if self.wticks <= 0 self.previous_widget = self.current_widget self.index = (self.index + 1) % size(self.widgets) self.current_widget = self.widgets[self.index]["name"] self.wticks = self.widgets[self.index]["duration"] self.transition_start_ms = epoch_ms() end self.wticks -= 1 var t = sensor.temperature() self.temp_label = t == nil ? "--" : str(round(t, 1)) var hmd = sensor.humidity() self.hum_label = hmd == nil ? "--" : str(round(hmd)) self.day_label = str(day()) var newtc = settings.get("timeColor") self.tc = newtc == nil ? settings.get("textColor") : newtc end def calculate_cur_prev() var h = hour() var m = minute() self.cur = [str(h / 10), str(h % 10), str(m / 10), str(m % 10)] self.hh = self.cur[0] + self.cur[1] self.mm = self.cur[2] + self.cur[3] var pm = m - 1 var ph = h if pm < 0 pm = 59 ph = (h - 1 < 0) ? 23 : h - 1 end self.prev = [str(ph / 10), str(ph % 10), str(pm / 10), str(pm % 10)] self.cur_min = m end def fade_color(color, pct) if color == nil return self.fade_color(0xFFFFFF, pct) end if pct >= 1.0 return color end var r = (color >> 16) & 0xFF var g = (color >> 8) & 0xFF var b = color & 0xFF return rgb(int(r * pct), int(g * pct), int(b * pct)) end def draw_time() var ems = epoch_ms() var xpos = 0 var ypos = 6 if second() % 2 > 0 var frac = (ems % 1000) / 1000.0 var pct = (math.cos(2 * math.pi * frac + math.pi) + 1) * 0.5 text(xpos + 8, ypos, ":", self.fade_color(self.tc, pct)) end var mms = ems % 60000 if mms < self.tad var pct = real(mms) / self.tad var i = 0 while i < 4 if self.cur[i] != self.prev[i] if self.ta == "scroll" var spos = int(pct * 8) text(xpos, ypos + spos - 8, self.cur[i], self.tc) text(xpos, ypos + spos, self.prev[i], self.tc) else var s = (pct < 0.5) ? self.prev[i] : self.cur[i] text(xpos, ypos, s, self.fade_color(self.tc, (math.cos(2 * math.pi * pct) + 1) * 0.5)) end else text(xpos, ypos, self.cur[i], self.tc) end xpos += 4 if i == 1 xpos += 2 end i += 1 end else text(0, 6, self.hh, self.tc) text(10, 6, self.mm, self.tc) end end def draw_seconds() var secw = 17 var ms_elapsed = epoch_ms() % 60000 var valeur = (ms_elapsed * secw) / 59000.0 var filled = int(valeur) var pct = valeur - filled var i = 0 while i < filled pixel(i, 7, self.sc) i += 1 end if filled < secw pixel(filled, 7, self.fade_color(self.sc, pct)) end end def draw_widget(name, pct) var ox = width() - 13 var oy = 0 var white = self.fade_color(0xFFFFFF, pct) var v = "?" if name == "date" var grey = self.fade_color(0x8B898C, pct) var blue = self.fade_color(0x0036FF, pct) rect_fill(ox, oy + 1, 5, 2, blue) rect_fill(ox, oy + 3, 5, 3, white) line(ox + 1, oy, ox + 1, oy + 1, grey) line(ox + 3, oy, ox + 3, oy + 1, grey) pixel(ox + 1, oy + 4, grey) pixel(ox + 3, oy + 4, grey) v = self.day_label elif name == "temperature" var red = self.fade_color(0xFF4C50, pct) pixel(ox + 2, oy, white) line(ox + 1, oy + 1, ox + 1, oy + 2, white) line(ox + 3, oy + 1, ox + 3, oy + 2, white) pixel(ox + 2, oy + 2, red) rect_fill(ox, oy + 3, 5, 2, white) rect_fill(ox + 1, oy + 3, 3, 2, red) rect_fill(ox + 1, oy + 5, 3, 1, white) v = self.temp_label elif name == "humidity" var l = self.fade_color(0xD5F6FF, pct) var m1 = self.fade_color(0xBDF3FF, pct) var m2 = self.fade_color(0x62E2FF, pct) var m3 = self.fade_color(0x40DAFE, pct) var d1 = self.fade_color(0x00AEFF, pct) var d2 = self.fade_color(0x0089CD, pct) pixel(ox + 2, oy, l) pixel(ox + 2, oy + 1, m1) pixel(ox + 1, oy + 2, white) pixel(ox + 2, oy + 2, m1) pixel(ox + 3, oy + 2, m2) pixel(ox, oy + 3, white) pixel(ox + 1, oy + 3, m1) pixel(ox + 2, oy + 3, m1) pixel(ox + 3, oy + 3, m2) pixel(ox + 4, oy + 3, d1) pixel(ox, oy + 4, white) pixel(ox + 1, oy + 4, m2) pixel(ox + 2, oy + 4, m2) pixel(ox + 3, oy + 4, m3) pixel(ox + 4, oy + 4, d2) pixel(ox + 1, oy + 5, d1) pixel(ox + 2, oy + 5, d1) pixel(ox + 3, oy + 5, d2) v = self.hum_label end text(ox + 6, 6, v, self.fade_color(0xFFFFFF, pct)) end def draw_week() var wd = self.start_sunday ? weekday() : (weekday() + 6) % 7 var xpos = 18 if self.week_style == "large" line(xpos, 7, 31, 7, self.week_color) line(xpos + wd * 2, 7, xpos + wd * 2 + 1, 7, self.day_color) elif self.week_style == "progress" line(xpos, 7, 31, 7, self.week_color) line(xpos, 7, xpos + wd * 2 + 1, 7, self.day_color) elif self.week_style == "dotted" line(xpos, 7, 31, 7, 0x000000) var i = 0 while i < 7 pixel(xpos + i * 2, 7, i == wd ? self.day_color : self.week_color) i += 1 end elif self.week_style == "dotted2" line(xpos, 7, 31, 7, 0x000000) var i = 0 var x = xpos while i < 7 if i == wd line(x, 7, x + 1, 7, self.day_color) x += 3 else pixel(x, 7, self.week_color) x += 2 end i += 1 end end end def draw() if minute() != self.cur_min self.calculate_cur_prev() end clear() self.draw_time() self.draw_seconds() self.draw_week() if self.transition_start_ms != nil var elapsed = epoch_ms() - self.transition_start_ms if elapsed >= self.transition_duration_ms self.transition_start_ms = nil self.draw_widget(self.current_widget, 1.0) else var pct = elapsed * 1.0 / self.transition_duration_ms var w = (pct < 0.5) ? self.previous_widget : self.current_widget self.draw_widget(w, (math.cos(2 * math.pi * pct) + 1) * 0.5) end else self.draw_widget(self.current_widget, 1.0) end end end return MyApp()