2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Widget library
|
|
|
|
~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The widget library allows common fragments of logic and drawing code to be
|
|
|
|
shared between applications.
|
|
|
|
"""
|
|
|
|
|
2020-02-03 23:35:16 +01:00
|
|
|
import icons
|
|
|
|
import watch
|
|
|
|
|
|
|
|
class BatteryMeter(object):
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Battery meter widget.
|
|
|
|
|
|
|
|
A simple battery meter with a charging indicator, will draw at the
|
|
|
|
top-right of the display.
|
|
|
|
"""
|
2020-02-03 23:35:16 +01:00
|
|
|
def __init__(self):
|
2020-02-04 09:43:49 +01:00
|
|
|
self.level = -2
|
2020-02-03 23:35:16 +01:00
|
|
|
|
|
|
|
def draw(self):
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Draw from meter (from scratch)."""
|
2020-02-04 09:43:49 +01:00
|
|
|
self.level = -2
|
2020-02-03 23:35:16 +01:00
|
|
|
self.update()
|
2020-04-05 10:48:03 +02:00
|
|
|
|
2020-02-03 23:35:16 +01:00
|
|
|
def update(self):
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Update the meter.
|
|
|
|
|
|
|
|
The update is lazy and won't redraw unless the level has changed.
|
|
|
|
"""
|
2020-02-04 09:43:49 +01:00
|
|
|
icon = icons.battery
|
2020-03-09 01:00:13 +01:00
|
|
|
draw = watch.drawable
|
2020-02-04 09:43:49 +01:00
|
|
|
|
2020-02-03 23:35:16 +01:00
|
|
|
if watch.battery.charging():
|
|
|
|
if self.level != -1:
|
2020-03-09 01:00:13 +01:00
|
|
|
draw.rleblit(icon, pos=(239-icon[0], 0), fg=0x7bef)
|
2020-02-03 23:35:16 +01:00
|
|
|
self.level = -1
|
|
|
|
else:
|
|
|
|
level = watch.battery.level()
|
|
|
|
if level == self.level:
|
|
|
|
return
|
|
|
|
|
|
|
|
if level > 96:
|
2020-02-04 09:43:49 +01:00
|
|
|
h = 24
|
2020-02-03 23:35:16 +01:00
|
|
|
rgb = 0x07e0
|
|
|
|
else:
|
2020-02-04 09:43:49 +01:00
|
|
|
h = level // 4
|
|
|
|
|
2020-02-03 23:35:16 +01:00
|
|
|
green = level // 3
|
|
|
|
red = 31-green
|
|
|
|
rgb = (red << 11) + (green << 6)
|
|
|
|
|
2020-02-04 09:43:49 +01:00
|
|
|
if (level > 5) ^ (self.level > 5):
|
|
|
|
if level > 5:
|
2020-03-09 01:00:13 +01:00
|
|
|
draw.rleblit(icon, pos=(239-icon[0], 0), fg=0x7bef)
|
2020-02-04 09:43:49 +01:00
|
|
|
else:
|
|
|
|
rgb = 0xf800
|
2020-03-09 01:00:13 +01:00
|
|
|
draw.rleblit(icon, pos=(239-icon[0], 0), fg=0xf800)
|
2020-02-04 09:43:49 +01:00
|
|
|
|
|
|
|
x = 239 - 30
|
|
|
|
w = 16
|
|
|
|
if 24 - h:
|
2020-03-09 01:00:13 +01:00
|
|
|
draw.fill(0, x, 14, w, 24 - h)
|
2020-02-03 23:35:16 +01:00
|
|
|
if h:
|
2020-03-09 01:00:13 +01:00
|
|
|
draw.fill(rgb, x, 38 - h, w, h)
|
2020-02-03 23:35:16 +01:00
|
|
|
|
2020-02-04 09:43:49 +01:00
|
|
|
self.level = level
|
2020-04-05 10:48:03 +02:00
|
|
|
|
|
|
|
class ScrollIndicator():
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Scrolling indicator.
|
|
|
|
|
|
|
|
A simple battery meter with a charging indicator, will draw at the
|
|
|
|
top-right of the display.
|
|
|
|
"""
|
2020-04-05 10:48:03 +02:00
|
|
|
def __init__(self, x=240-18, y=240-24):
|
|
|
|
self._pos = (x, y)
|
|
|
|
self.up = True
|
|
|
|
self.down = True
|
|
|
|
|
|
|
|
def draw(self):
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Draw from scrolling indicator.
|
|
|
|
|
|
|
|
For this simple widget :py:meth:`~.draw` is simply a synonym for
|
|
|
|
:py:meth:`~.update`.
|
|
|
|
"""
|
2020-04-05 10:48:03 +02:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Update from scrolling indicator."""
|
2020-04-05 10:48:03 +02:00
|
|
|
draw = watch.drawable
|
|
|
|
if self.up:
|
|
|
|
draw.rleblit(icons.up_arrow, pos=self._pos, fg=0x7bef)
|
|
|
|
if self.down:
|
|
|
|
draw.rleblit(icons.down_arrow, pos=(self._pos[0], self._pos[1] + 13), fg=0x7bef)
|