def aclock(): uv = lambda phi : cmath.rect(1, phi) # Return a unit vector of phase phi pi = cmath.pi days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday') months = ('Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec') # Instantiate CWriter CWriter.set_textpos(ssd, 0, 0) # In case previous tests have altered it wri = CWriter(ssd, arial10, GREEN, BLACK, verbose=False) wri.set_clip(True, True, False) # Instantiate displayable objects dial = Dial(wri, 2, 2, height = 75, ticks = 12, bdcolor=None, label=120, pip=False) # Border in fg color lbltim = Label(wri, 5, 85, 35) hrs = Pointer(dial) mins = Pointer(dial) secs = Pointer(dial) hstart = 0 + 0.7j # Pointer lengths and position at top mstart = 0 + 0.92j sstart = 0 + 0.92j while True: t = utime.localtime() hrs.value(hstart * uv(-t[3]*pi/6 - t[4]*pi/360), YELLOW) mins.value(mstart * uv(-t[4] * pi/30), YELLOW) secs.value(sstart * uv(-t[5] * pi/30), RED) lbltim.value('{:02d}.{:02d}.{:02d}'.format(t[3], t[4], t[5])) dial.text('{} {} {} {}'.format(days[t[6]], t[2], months[t[1] - 1], t[0])) refresh(ssd) utime.sleep(1)
class VerticalSliderScreen(Screen): def __init__(self): super().__init__() labels = { 'width' : 50, 'fontcolor' : WHITE, 'border' : 2, 'fgcolor' : RED, 'bgcolor' : DARKGREEN, 'font' : font10, } quitbutton() self.dial = Dial((109, 0), fgcolor = YELLOW, border = 2, pointers = (0.9, 0.7)) self.lbl_result = Label((109, 80), **labels) self.master = Slider((0, 5), font = font6, fgcolor = YELLOW, fontcolor = WHITE, legends = ('0', '5', '10'), cb_end = self.callback, cbe_args = ('Master',), cb_move = self.master_moved, value=0.5, border = 2) self.slave = Slider((60, 5), fgcolor = GREEN, cbe_args = ('Slave',), cb_move = self.slave_moved, border = 2) self.reg_task(self.coro()) # On/Off toggle: enable/disable quit button and one slider bs = ButtonList(self.cb_en_dis) lst_en_dis = [self.slave, self.master] button = bs.add_button((109, 53), font = font10, fontcolor = BLACK, fgcolor = GREEN, text = 'Dis', args = [True, lst_en_dis]) button = bs.add_button((109, 53), font = font10, fontcolor = BLACK, fgcolor = RED, text = 'En', args = [False, lst_en_dis]) # CALLBACKS # cb_end occurs when user stops touching the control def callback(self, slider, device): print('{} returned {}'.format(device, slider.value())) def master_moved(self, slider): val = slider.value() self.slave.value(val) self.lbl_result.value(to_string(val)) def cb_en_dis(self, button, disable, itemlist): for item in itemlist: item.greyed_out(disable) # Slave has had its slider moved (by user or by having value altered) def slave_moved(self, slider): val = slider.value() if val > 0.8: slider.color(RED) else: slider.color(GREEN) self.lbl_result.value(to_string(val)) # COROUTINE async def coro(self): angle = 0 while True: await asyncio.sleep_ms(100) delta = self.slave.value() angle += pi * 2 * delta / 10 self.dial.value(angle) self.dial.value(angle /10, 1)
def __init__(self): super().__init__() labels = { 'width' : 50, 'fontcolor' : WHITE, 'border' : 2, 'fgcolor' : RED, 'bgcolor' : DARKGREEN, 'font' : font10, } quitbutton() self.dial = Dial((109, 0), fgcolor = YELLOW, border = 2, pointers = (0.9, 0.7)) self.lbl_result = Label((109, 80), **labels) self.master = Slider((0, 5), font = font6, fgcolor = YELLOW, fontcolor = WHITE, legends = ('0', '5', '10'), cb_end = self.callback, cbe_args = ('Master',), cb_move = self.master_moved, value=0.5, border = 2) self.slave = Slider((60, 5), fgcolor = GREEN, cbe_args = ('Slave',), cb_move = self.slave_moved, border = 2) self.reg_task(self.coro()) # On/Off toggle: enable/disable quit button and one slider bs = ButtonList(self.cb_en_dis) lst_en_dis = [self.slave, self.master] button = bs.add_button((109, 53), font = font10, fontcolor = BLACK, fgcolor = GREEN, text = 'Dis', args = [True, lst_en_dis]) button = bs.add_button((109, 53), font = font10, fontcolor = BLACK, fgcolor = RED, text = 'En', args = [False, lst_en_dis])
def __init__(self, writer, row, col, *, height=50, width=10, fgcolor=None, bgcolor=None, ptcolor=None, bdcolor=None, divisions=5, label=None, style=0, legends=None, value=None): super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor) self.divisions = divisions if label is not None: Label(writer, row + height + 3, col, label) self.style = style if legends is not None: # Legends x = col + width + 4 y = row + height dy = 0 if len(legends) <= 1 else height / (len(legends) - 1) yl = y - writer.height / 2 # Start at bottom for legend in legends: Label(writer, int(yl), x, legend) yl -= dy self.ptcolor = ptcolor if ptcolor is not None else self.fgcolor self.value(value)
class CheckboxScreen(Screen): def __init__(self): super().__init__() self.cb1 = Checkbox((0, 0), callback=self.cbcb, args=(0, )) self.cb2 = Checkbox((0, 30), fillcolor=RED, callback=self.cbcb, args=(1, )) self.lstlbl = [Label((30, 0), **labels), Label((30, 30), **labels)] self.lbl_result = Label((0, 106), **labels) backbutton() self.btn_reset = Button((109, 80), font=font10, fgcolor=BLUE, text='Reset', fill=True, callback=self.cbreset, onrelease=False, lp_callback=self.callback, lp_args=('long', )) def cbreset(self, button): self.cb1.value(0) self.cb2.value(0) self.lbl_result.value('Short') def callback(self, button, arg): self.lbl_result.value(arg) def cbcb(self, checkbox, idx_label): if checkbox.value(): self.lstlbl[idx_label].value('True') else: self.lstlbl[idx_label].value('False')
def multi_fields(): ssd.fill(0) refresh(ssd) Writer.set_textpos(ssd, 0, 0) # In case previous tests have altered it wri = Writer(ssd, small, verbose=False) wri.set_clip(False, False, False) nfields = [] dy = small.height() + 6 y = 2 col = 15 width = wri.stringlen('99.99') for txt in ('X:', 'Y:', 'Z:'): Label(wri, y, 0, txt) nfields.append(Label(wri, y, col, width, bdcolor=None)) # Draw border y += dy random = xorshift64star(2**24 - 1) for _ in range(10): for field in nfields: value = random() / 167772 field.value('{:5.2f}'.format(value)) refresh(ssd) utime.sleep(1) Label(wri, 0, 64, ' DONE ', True) refresh(ssd)
def multi_fields(t): print('multi_fields') refresh(ssd, True) # Clear any prior image nfields = [] dy = wri.height + 6 y = 2 col = 15 width = wri.stringlen('99.99') for txt in ('X:', 'Y:', 'Z:'): Label(wri, y, 0, txt) # Use wri default colors nfields.append(Label(wri, y, col, width, bdcolor=None)) # Specify a border, color TBD y += dy end = utime.ticks_add(utime.ticks_ms(), t * 1000) while utime.ticks_diff(end, utime.ticks_ms()) > 0: for field in nfields: value = int.from_bytes(uos.urandom(3), 'little') / 167772 overrange = None if value < 70 else YELLOW if value < 90 else RED field.value('{:5.2f}'.format(value), fgcolor=overrange, bdcolor=overrange) refresh(ssd) utime.sleep(1) Label(wri, 0, 64, ' OK ', True, fgcolor=RED) refresh(ssd) utime.sleep(1)
def __init__(self): super().__init__() Label((0, 0), font=font10, value='Dialog box demo.') Label((0, 20), font=font10, value='User written and') Label((0, 40), font=font10, value='auto generated') self.lbl_result = Label((0, 80), font=font10, fontcolor=WHITE, width=70, border=2, fgcolor=RED, bgcolor=DARKGREEN) # User written dialog fwdbutton(54, 107, UserDialogBox, text='User') # Dialog built using DialogBox class dialog_elements = (('Yes', GREEN), ('No', RED), ('Foo', YELLOW)) fwdbutton(0, 107, DialogBox, text='Gen', args=(font10, ), kwargs={ 'elements': dialog_elements, 'label': 'Test dialog' }) quitbutton()
def __init__(self): super().__init__() Label((0, 0), font = font10, value = 'plot module demo') Label((0, 22), font = font10, value = 'RT: simulate realtime') fwdbutton(0, 51, PolarScreen, 'Polar') fwdbutton(0, 79, XYScreen, 'XY') fwdbutton(0, 107, RealtimeScreen, 'RT') fwdbutton(60, 51, PolarORScreen, 'Over') fwdbutton(60, 79, DiscontScreen, 'Lines') quitbutton()
def __init__(self): super().__init__() quitbutton() # Dropdown self.lbl_dd = Label((0, 80), font=font10, width=60, border=2, bgcolor=DARKGREEN, fgcolor=RED) self.dropdown = Dropdown((0, 0), font=font10, width=65, callback=self.cbdb, elements=('Dog', 'Cat', 'Rat', 'Goat', 'Pig')) # Listbox self.listbox = Listbox( (80, 0), font=font10, width=79, bgcolor=GREY, fgcolor=YELLOW, select_color=BLUE, elements=('aardvark', 'zebra', 'armadillo', 'warthog'), callback=self.cblb) self.btnrep = Button((0, 40), height=20, font=font10, callback=self.cbrep, fgcolor=RED, text='Report', shape=RECTANGLE, width=60) # Enable/Disable toggle self.bs_en = ButtonList(self.cb_en_dis) self.bs_en.add_button((0, 107), font=font10, fontcolor=BLACK, height=20, width=60, fgcolor=GREEN, shape=RECTANGLE, text='Disable', args=(True, )) self.bs_en.add_button((0, 107), font=font10, fontcolor=BLACK, height=20, width=60, fgcolor=RED, shape=RECTANGLE, text='Enable', args=(False, ))
class RadioScreen(Screen): def __init__(self): super().__init__() table = [ { 'text': '1', 'args': ('one', ) }, { 'text': '2', 'args': ('two', ) }, { 'text': '3', 'args': ('three', ) }, { 'text': '4', 'args': ('four', ) }, ] Label((0, 0), font=font10, value='Radio Buttons') x = 0 self.rb = RadioButtons(BLUE, self.callback) # color of selected button self.rb0 = None for t in table: button = self.rb.add_button((x, 30), shape=CIRCLE, font=font10, fontcolor=WHITE, fgcolor=DARKBLUE, height=30, width=30, **t) if self.rb0 is None: # Save for reset button callback self.rb0 = button x += 43 self.lbl_result = Label((0, 106), **labels) backbutton() self.btn_reset = Button((109, 80), font=font10, fgcolor=BLUE, text='Reset', fill=True, callback=self.cbreset, onrelease=False, lp_callback=self.callback, lp_args=('long', )) def callback(self, button, arg): self.lbl_result.value(arg) def cbreset(self, button): self.rb.value(self.rb0) self.lbl_result.value('Short')
def pyqt5_render(self, layout, form, visible=True): self.widget_label = Label(self.label) if self.computed: answer_result = str(self.evaluate_answer(form)) self.widget = Label(answer_result) else: self.widget = self.answer_type.pyqt5_default_widget() if not visible: self.widget.hide() self.widget_label.hide() layout.addRow(self.widget_label, self.widget)
def show(self): super().show() # Draw or erase border val = super().value() wri = self.writer dev = self.device width = self.width height = self.height legends = self.legends x0 = self.col x1 = self.col + width y0 = self.row y1 = self.row + height if self.divisions > 0: dy = height / (self.divisions) # Tick marks for tick in range(self.divisions + 1): ypos = int(y0 + dy * tick) dev.hline(x0 + 2, ypos, x1 - x0 - 4, self.fgcolor) if legends is not None: # Legends dy = 0 if len(legends) <= 1 else height / (len(legends) -1) yl = y1 - wri.height / 2 # Start at bottom for legend in legends: Label(wri, int(yl), x1 + 4, legend) yl -= dy y = int(y1 - val * height) # y position of slider if self.style == self.LINE: dev.hline(x0, y, width, self.ptcolor) # Draw pointer else: w = width / 2 dev.fill_rect(int(x0 + w - 2), y, 4, y1 - y, self.ptcolor)
def graph(): row, col, ht, wd = 5, 140, 75, 150 def populate(): x = -0.998 while x < 1.01: z = 6 * pi * x y = sin(z) / z yield x, y x += 0.05 g = CartesianGraph(wri, row, col, height=ht, width=wd, bdcolor=False) curve2 = Curve(g, None, populate()) Label(wri, row + ht + 5, col - 10, '-2.0 t: secs') Label(wri, row + ht + 5, col - 8 + int(wd // 2), '0.0') Label(wri, row + ht + 5, col - 10 + wd, '2.0')
def __init__(self): super().__init__() labels = {'fontcolor' : WHITE, 'border' : 2, 'fgcolor' : RED, 'bgcolor' : DARKGREEN, 'font' : font6, } fwdbutton(BackScreen) backbutton() # Set up clock display: instantiate labels lblday = Label((80, 0), width = 79, **labels) lblmon = Label((99, 23), width = 60, **labels) lblyr = Label((99, 46), width = 60, **labels) lbltim = Label((99, 69), width = 60, **labels) dial = VectorDial((0, 0), height = 80, ticks = 12, fgcolor = GREEN, pip = GREEN) self.reg_task(aclock(dial, lblday, lblmon, lblyr, lbltim))
def __init__(self, font, *, elements, location=(20, 20), label=None, bgcolor=DARKGREEN, buttonwidth=25, closebutton=True): height = 100 spacing = 5 buttonwidth = max( max([get_stringsize(x[0], font)[0] for x in elements]) + 4, buttonwidth) buttonheight = max(get_stringsize('x', font)[1], 20) nelements = len(elements) width = spacing + (buttonwidth + spacing) * nelements if label is not None: width = max(width, get_stringsize(label, font)[0] + 2 * spacing) super().__init__(location, height, width, bgcolor=bgcolor) x = self.location[ 0] + spacing # Coordinates relative to physical display gap = 0 if nelements > 1: gap = ((width - 2 * spacing) - nelements * buttonwidth) // (nelements - 1) y = self.location[1] + self.height - buttonheight - 10 if label is not None: Label((x, self.location[1] + 25), font=font, bgcolor=bgcolor, value=label) for text, color in elements: Button((x, y), height=buttonheight, width=buttonwidth, font=font, fontcolor=BLACK, fgcolor=color, text=text, shape=RECTANGLE, callback=self.back, args=(text, )) x += buttonwidth + gap if closebutton: x, y = get_stringsize('X', font) size = max(x, y, 20) Button( (self.location[0] + width - (size + 1), self.location[1] + 1), height=size, width=size, font=font, fgcolor=RED, text='X', shape=RECTANGLE, callback=self.back, args=('Close', ))
def clock(x): print('Clock test.') refresh(ssd, True) # Clear any prior image lbl = Label(wri, 5, 85, 'Clock') dial = Dial(wri, 5, 5, height = 75, ticks = 12, bdcolor=None, label=50) # Border in fg color hrs = Pointer(dial) mins = Pointer(dial) hrs.value(0 + 0.7j, RED) mins.value(0 + 0.9j, YELLOW) dm = cmath.rect(1, -cmath.pi/30) # Rotate by 1 minute (CW) dh = cmath.rect(1, -cmath.pi/1800) # Rotate hours by 1 minute for n in range(x): refresh(ssd) utime.sleep_ms(200) mins.value(mins.value() * dm, YELLOW) hrs.value(hrs.value() * dh, RED) dial.text('ticks: {}'.format(n)) lbl.value('Done')
def __init__(self): super().__init__() self.cb1 = Checkbox((0, 0), callback=self.cbcb, args=(0, )) self.cb2 = Checkbox((0, 30), fillcolor=RED, callback=self.cbcb, args=(1, )) self.lstlbl = [Label((30, 0), **labels), Label((30, 30), **labels)] self.lbl_result = Label((0, 106), **labels) backbutton() self.btn_reset = Button((109, 80), font=font10, fgcolor=BLUE, text='Reset', fill=True, callback=self.cbreset, onrelease=False, lp_callback=self.callback, lp_args=('long', ))
def __init__(self): super().__init__() Label((0, 0), font = font10, value = 'plot module: gens') fwdbutton(0, 23, Tseq, 'TSeq') fwdbutton(0, 51, PolarScreen, 'Polar') fwdbutton(0, 79, XYScreen, 'XY') fwdbutton(0, 107, RealtimeScreen, 'RT') fwdbutton(60, 51, PolarORScreen, 'Over') fwdbutton(60, 79, DiscontScreen, 'Lines') quitbutton()
def __init__(self, writer, row, col, *, height=50, width=10, fgcolor=None, bgcolor=None, ptcolor=None, bdcolor=None, divisions=5, label=None, style=0, legends=None, value=None): super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor) self.divisions = divisions if label is not None: Label(writer, row + height + 3, col, label) self.style = style self.legends = legends self.ptcolor = ptcolor if ptcolor is not None else self.fgcolor self.value(value)
async def multi_fields(evt): wri = Writer(ssd, small, verbose=False) wri.set_clip(False, False, False) nfields = [] dy = small.height() + 10 y = 80 col = 20 width = wri.stringlen('99.990') for txt in ('X:', 'Y:', 'Z:'): Label(wri, y, 0, txt) nfields.append(Label(wri, y, col, width, bdcolor=None)) # Draw border y += dy random = xorshift64star(2**24 - 1) while True: for _ in range(10): for field in nfields: value = random() / 167772 field.value('{:5.2f}'.format(value)) await evt.wait()
def __init__(self): super().__init__() # Scale with custom variable and legends. def legendcb(f): return '{:2.0f}'.format(88 + ((f + 1) / 2) * (108 - 88)) self.scale = Scale((0, 0), font6, width=159, legendcb=legendcb, fgcolor=GREEN, pointercolor=RED, fontcolor=YELLOW) self.reg_task(self.radio()) # Scale with varying color. def tickcb(f, c): if f > 0.8: return RED if f < -0.8: return BLUE return c self.lbl_result = Label((0, 105), font=font10, fontcolor=WHITE, width=70, border=2, fgcolor=RED, bgcolor=DARKGREEN) self.scale1 = Scale((0, 70), font6, width=159, tickcb=tickcb, fgcolor=GREEN, pointercolor=RED, fontcolor=YELLOW) self.reg_task(self.default()) quitbutton()
def __init__(self, writer, row, col, *, height=50, fgcolor=None, bgcolor=None, bdcolor=False, ticks=4, label=None, style=0, pip=None): super().__init__(writer, row, col, height, height, fgcolor, bgcolor, bdcolor) self.style = style self.pip = self.fgcolor if pip is None else pip if label is not None: self.label = Label(writer, row + height + 3, col, label) radius = int(height / 2) self.radius = radius self.ticks = ticks self.xorigin = col + radius self.yorigin = row + radius self.vectors = set()
def show(self): tft = self.tft bw = self.border height = self.height - 2 * bw width = self.pot_dimension # Length of slot x = self.location[ 0] + bw + self.slidewidth // 2 # Allow space left and right slot for slider at extremes y = self.location[1] + bw if self._value is None or self.redraw: # Initialising self.redraw = False self.render_slide(tft, self.bgcolor) # Erase slide if it exists dy = height // 2 - 2 # slot is 4 pixels wide tft.draw_rectangle(x, y + dy, x + width, y + height - dy, self.fgcolor) if self.divisions > 0: dx = width / (self.divisions) # Tick marks for tick in range(self.divisions + 1): xpos = int(x + dx * tick) tft.draw_vline(xpos, y + 1, dy, self.fgcolor) tft.draw_vline(xpos, y + 2 + height // 2, dy, self.fgcolor) # Add half slot width # Legends: if redrawing, they are already on the Screen's display list if self.legends is not None and not self.drawn: if len(self.legends) <= 1: dx = 0 else: dx = width / (len(self.legends) - 1) xl = x font = self.font for legend in self.legends: offset = get_stringsize(legend, self.font)[0] / 2 loc = int(xl - offset), y - self.font.height() - bw - 1 Label(loc, font=font, fontcolor=tft.text_fgcolor, value=legend) xl += dx self.save_background(tft) if self._value is None: self.value(self._initial_value, show=False) # prevent recursion self.render_bg(tft) self.slide_x = self.update(tft) # Reflect new value in slider position self.save_background(tft) color = self.slidecolor if self.slidecolor is not None else self.fgcolor self.render_slide(tft, color) self.drawn = True
def show(self): tft = self.tft bw = self.border width = self.width - 2 * bw height = self.pot_dimension # Height of slot x = self.location[0] + bw y = self.location[ 1] + bw + self.slideheight // 2 # Allow space above and below slot if self._value is None or self.redraw: # Initialising self.redraw = False self.render_slide(tft, self.bgcolor) # Erase slide if it exists dx = width // 2 - 2 tft.draw_rectangle(x + dx, y, x + width - dx, y + height, self.fgcolor) if self.divisions > 0: dy = height / (self.divisions) # Tick marks for tick in range(self.divisions + 1): ypos = int(y + dy * tick) tft.draw_hline(x + 1, ypos, dx, self.fgcolor) tft.draw_hline(x + 2 + width // 2, ypos, dx, self.fgcolor) # Add half slot width # Legends: if redrawing, they are already on the Screen's display list if self.legends is not None and not self.drawn: if len(self.legends) <= 1: dy = 0 else: dy = height / (len(self.legends) - 1) yl = y + height # Start at bottom fhdelta = self.font.height() / 2 font = self.font for legend in self.legends: loc = (x + self.width, int(yl - fhdelta)) Label(loc, font=font, fontcolor=tft.text_fgcolor, value=legend) yl -= dy self.save_background(tft) if self._value is None: self.value(self._initial_value, show=False) # Prevent recursion self.render_bg(tft) self.slide_y = self.update(tft) # Reflect new value in slider position self.save_background(tft) color = self.slidecolor if self.slidecolor is not None else self.fgcolor self.render_slide(tft, color) self.drawn = True
def __init__(self): super().__init__() layout = QHBoxLayout(self) self.hint = Label() self.hint.font_size = size.INPUT_TEXT self.line_edit = LineEdit() self.line_edit.font_size = size.INPUT_TEXT self.line_edit.validator = "pinyin" layout.addWidget(self.hint) layout.addWidget(self.line_edit) layout.setSizeConstraint(QLayout.SetMinAndMaxSize)
def __init__(self): super().__init__() table = [ {'text' : 'Clock', 'args' : (ClockScreen,)}, {'text' : 'Compass', 'args' : (VecScreen,)}, ] quitbutton() Label((0, 0), font = font10, value = 'Display format') def rbcb(button, screen): # RadioButton callback Screen.change(screen) rb = RadioButtons(BLUE, rbcb) # color of selected button y = 25 for t in table: rb.add_button((0, y), font = font10, fgcolor = DARKBLUE, width = 100, fontcolor = WHITE, **t) y += 40
def labels(): row = 100 col = 0 Label(wri_large, row, col, 'Seismograph') col = 140 Label(wri, row, col + 0, 'Event time') Label(wri, row, col + 60, '01:35', bdcolor=None) Label(wri, row, col + 95, 'UTC') row = 115 Label(wri, row, col + 0, 'Event date') Label(wri, row, col + 60, '6th Jan 2021', bdcolor=None)
def __init__(self): super().__init__() # tabulate data that varies between buttons table = [ { 'text': 'F', 'args': ('fwd', ) }, { 'text': 'B', 'args': ('back', ) }, { 'text': 'U', 'args': ('up', ) }, { 'text': 'D', 'args': ('down', ) }, ] Label((0, 0), font=font10, value='Highlight Buttons') # Highlighting buttons lbl_result = Label((0, 106), **labels) def cbbtn(button, arg): lbl_result.value(arg) x = 0 for t in table: Button((x, 30), shape=CIRCLE, fgcolor=GREY, fontcolor=BLACK, litcolor=WHITE, font=font10, callback=cbbtn, height=30, **t) x += 43 lbl_pad = Label((0, 76), value='touch me', fontcolor=WHITE, border=2, fgcolor=RED, bgcolor=DARKGREEN, font=font10) pad = Pad((0, 76), width=lbl_pad.width, onrelease=False, callback=lambda _: lbl_pad.value('Short'), lp_callback=lambda _: lbl_pad.value('Long')) backbutton()
def aclock(): rtc = pyb.RTC() uv = lambda phi: cmath.rect(1, phi) # Return a unit vector of phase phi pi = cmath.pi days = ('Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun') months = ('Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec') # Instantiate Writer Writer.set_textpos(ssd, 0, 0) # In case previous tests have altered it wri = Writer(ssd, font_small, verbose=False) wri.set_clip(True, True, False) wri_tim = Writer(ssd, font_large, verbose=False) wri_tim.set_clip(True, True, False) # Instantiate displayable objects dial = Dial(wri, 2, 2, height=215, ticks=12, bdcolor=None, pip=True) lbltim = Label(wri_tim, 50, 230, '00.00.00') lbldat = Label(wri, 100, 230, 100) hrs = Pointer(dial) mins = Pointer(dial) hstart = 0 + 0.7j # Pointer lengths and position at top mstart = 0 + 0.92j while True: t = rtc.datetime( ) # (year, month, day, weekday, hours, minutes, seconds, subseconds) hang = -t[4] * pi / 6 - t[5] * pi / 360 # Angles of hands in radians mang = -t[5] * pi / 30 if abs(hang - mang) < pi / 360: # Avoid visually confusing overlap of hands hang += pi / 30 # by making hr hand lag slightly hrs.value(hstart * uv(hang)) mins.value(mstart * uv(mang)) lbltim.value('{:02d}.{:02d}'.format(t[4], t[5])) lbldat.value('{} {} {} {}'.format(days[t[3] - 1], t[2], months[t[1] - 1], t[0])) refresh(ssd) # Power saving: only refresh every 30s for _ in range(30): upower.lpdelay(1000) ssd.update() # Toggle VCOM