Example #1
0
class BaseScreen(Screen):
    def __init__(self):
        super().__init__()
        Label((0, 0), font=font14, value='Dialog box demonstration.')
        Label((0, 100),
              font=font10,
              value='User written and gdialog generated')
        self.lbl_result = Label((10, 50),
                                font=font10,
                                fontcolor=WHITE,
                                width=70,
                                border=2,
                                fgcolor=RED,
                                bgcolor=DARKGREEN)
        # User written dialog
        fwdbutton(195, 242, UserDialogBox, text='User')
        # Dialog built using gdialog.py DialogBox
        dialog_elements = (('Yes', GREEN), ('No', RED), ('Foo', YELLOW))
        fwdbutton(0,
                  242,
                  DialogBox,
                  text='Gen',
                  args=(font14, ),
                  kwargs={
                      'elements': dialog_elements,
                      'label': 'Test dialog'
                  })
        quitbutton(390, 242)

    def on_open(self):
        self.lbl_result.value(Aperture.value())
class BaseScreen(Screen):
    def __init__(self):
        super().__init__()
        # Scale with custom variable and legends.
        Label((0, 0), font=font14, value='FM radio scale 88-108MHz.')

        def legendcb(f):
            return '{:2.0f}'.format(88 + ((f + 1) / 2) * (108 - 88))

        self.scale = Scale((0, 30),
                           font10,
                           legendcb=legendcb,
                           width=350,
                           height=60,
                           bgcolor=BLACK,
                           fgcolor=GREEN,
                           pointercolor=RED,
                           fontcolor=YELLOW)
        self.reg_task(self.radio())
        # Scale with varying color.
        Label((0, 130),
              font=font14,
              value='Default scale -1 to +1, varying colors.')

        def tickcb(f, c):
            if f > 0.8:
                return RED
            if f < -0.8:
                return BLUE
            return c

        self.lbl_result = Label((0, 240),
                                font=font14,
                                fontcolor=WHITE,
                                width=70,
                                border=2,
                                fgcolor=RED,
                                bgcolor=DARKGREEN)
        self.scale1 = Scale((0, 160),
                            font10,
                            tickcb=tickcb,
                            width=350,
                            height=60,
                            bgcolor=BLACK,
                            fgcolor=GREEN,
                            pointercolor=RED,
                            fontcolor=YELLOW)
        self.reg_task(self.default())
        quitbutton(390, 242)


# COROUTINES

    async def radio(self):
        cv = 88.0  # Current value
        val = 108.0  # Target value
        while True:
            v1, v2 = val, cv
            steps = 200
            delta = (val - cv) / steps
            for _ in range(steps):
                cv += delta
                # Map user variable to -1.0..+1.0
                self.scale.value(2 * (cv - 88) / (108 - 88) - 1)
                await asyncio.sleep_ms(200)
            val, cv = v2, v1

    async def default(self):
        cv = -1.0  # Current
        val = 1.0
        while True:
            v1, v2 = val, cv
            steps = 400
            delta = (val - cv) / steps
            for _ in range(steps):
                cv += delta
                self.scale1.value(cv)
                self.lbl_result.value('{:4.3f}'.format(cv))
                await asyncio.sleep_ms(250)
            val, cv = v2, v1
Example #3
0
class KnobScreen(Screen):
    def __init__(self):
        super().__init__()
        Button((390, 240),
               font=IFONT16,
               callback=self.quit,
               fgcolor=RED,
               text='Quit',
               shape=RECTANGLE,
               width=80,
               height=30)
        dial = VectorDial((120, 0), fgcolor=YELLOW, border=2)
        hrs = Pointer(dial)
        mins = Pointer(dial)
        k0 = Knob((0, 0),
                  fgcolor=GREEN,
                  bgcolor=(0, 0, 80),
                  color=(168, 63, 63),
                  border=2,
                  cb_end=self.callback,
                  cbe_args=['Knob1'],
                  cb_move=self.knob_moved,
                  cbm_args=(mins, 0.9))
        k1 = Knob((0, 120),
                  fgcolor=WHITE,
                  border=2,
                  arc=pi * 1.5,
                  cb_end=self.callback,
                  cbe_args=['Knob2'],
                  cb_move=self.knob_moved,
                  cbm_args=(hrs, 0.7))
        Label((0, 225), font=font10, value='Arc = 270 degs.')
        # Dropdown
        self.lbl_dd = Label((120, 120),
                            font=IFONT16,
                            width=100,
                            border=2,
                            bgcolor=(0, 40, 0),
                            fgcolor=RED)
        self.dropdown = Dropdown(
            (280, 0),
            font=IFONT16,
            width=100,
            callback=self.cbdb,
            elements=('Dog', 'Cat', 'Rat', 'Goat', 'Snake', 'Pig'))

        btnrst = Button((280, 70),
                        font=IFONT16,
                        callback=self.set_dropdown,
                        fgcolor=BLUE,
                        text='Reset',
                        shape=RECTANGLE,
                        width=80,
                        height=30)  # Test of set by value
        btnsnake = Button((280, 120),
                          font=IFONT16,
                          callback=self.set_bytext,
                          args=('Snake', ),
                          fgcolor=CYAN,
                          fontcolor=BLACK,
                          text='Snake',
                          shape=RECTANGLE,
                          width=80,
                          height=30)  # test set by text
        # Listbox
        self.lbl_lb = Label((120, 150),
                            font=IFONT16,
                            width=100,
                            border=2,
                            bgcolor=(0, 40, 0),
                            fgcolor=RED)
        listbox = Listbox(
            (370, 70),
            font=IFONT16,
            width=105,
            bgcolor=GREY,
            fgcolor=YELLOW,
            select_color=BLUE,
            elements=('aardvark', 'zebra', 'armadillo', 'warthog'),
            callback=self.cblb)
        # On/Off toggle grey style
        self.lbl_style = Label((170, 210),
                               font=font10,
                               value='Current style: grey')
        bstyle = ButtonList(self.cb_style)
        bstyle.add_button((170, 240),
                          font=IFONT16,
                          fontcolor=WHITE,
                          height=30,
                          width=90,
                          fgcolor=RED,
                          shape=RECTANGLE,
                          text='Dim',
                          args=(False, ))
        bstyle.add_button((170, 240),
                          font=IFONT16,
                          fontcolor=WHITE,
                          height=30,
                          width=90,
                          fgcolor=GREEN,
                          shape=RECTANGLE,
                          text='Grey',
                          args=(True, ))
        # On/Off toggle enable/disable
        bs = ButtonList(self.cb_en_dis)
        self.lst_en_dis = (bstyle, k0, k1, self.dropdown, listbox, btnrst,
                           btnsnake)
        bs.add_button((280, 240),
                      font=IFONT16,
                      fontcolor=BLACK,
                      height=30,
                      width=90,
                      fgcolor=GREEN,
                      shape=RECTANGLE,
                      text='Disable',
                      args=(True, ))
        bs.add_button((280, 240),
                      font=IFONT16,
                      fontcolor=BLACK,
                      height=30,
                      width=90,
                      fgcolor=RED,
                      shape=RECTANGLE,
                      text='Enable',
                      args=(False, ))


# CALLBACKS
# cb_end occurs when user stops touching the control

    def callback(self, knob, control_name):
        print('{} returned {}'.format(control_name, knob.value()))

    def knob_moved(self, knob, pointer, length):
        val = knob.value()  # range 0..1
        pointer.value(length * cmath.exp(-2j * val * pi))

    def quit(self, button):
        Screen.shutdown()

    def cb_en_dis(self, button, disable):
        for item in self.lst_en_dis:
            item.greyed_out(disable)

    def cb_style(self, button, desaturate):
        self.lbl_style.value(''.join(
            ('Current style: ', 'grey' if desaturate else 'dim')))
        Screen.set_grey_style(desaturate=desaturate)

    def cbdb(self, dropdown):
        self.lbl_dd.value(dropdown.textvalue())

    def cblb(self, listbox):
        self.lbl_lb.value(listbox.textvalue())

    def set_dropdown(self, button):
        self.dropdown.value(0)

    def set_bytext(self, button, txt):
        self.dropdown.textvalue(txt)
    def __init__(self):
        super().__init__()
        # These tables contain args that differ between members of a set of related buttons
        table = [
            {
                'fgcolor': GREEN,
                'text': 'Yes',
                'args': ('Oui', 2),
                'fontcolor': (0, 0, 0)
            },
            {
                'fgcolor': RED,
                'text': 'No',
                'args': ('Non', 2)
            },
            {
                'fgcolor': BLUE,
                'text': '???',
                'args': ('Que?', 2)
            },  # 'fill': False to see effect
            {
                'fgcolor': GREY,
                'text': 'Rats',
                'args': ('Rats', 2)
            },
        ]
        # Highlight buttons: only tabulate data that varies
        table_highlight = [
            {
                'text': 'P',
                'args': ('p', 2)
            },
            {
                'text': 'Q',
                'args': ('q', 2)
            },
            {
                'text': 'R',
                'args': ('r', 2)
            },
            {
                'text': 'S',
                'args': ('s', 2)
            },
        ]
        # A Buttonset with two entries
        table_buttonset = [
            {
                'fgcolor': GREEN,
                'shape': CLIPPED_RECT,
                'text': 'Start',
                'args': ('Live', 2)
            },
            {
                'fgcolor': RED,
                'shape': CLIPPED_RECT,
                'text': 'Stop',
                'args': ('Die', 2)
            },
        ]

        table_radiobuttons = [
            {
                'text': '1',
                'args': ('1', 3)
            },
            {
                'text': '2',
                'args': ('2', 3)
            },
            {
                'text': '3',
                'args': ('3', 3)
            },
            {
                'text': '4',
                'args': ('4', 3)
            },
        ]

        labels = {
            'width': 70,
            'fontcolor': WHITE,
            'border': 2,
            'fgcolor': RED,
            'bgcolor': (0, 40, 0),
            'font': font14,
        }

        # Uncomment this line to see 'skeleton' style greying-out:
        #        Screen.tft.grey_color()

        # Labels
        self.lstlbl = []
        for n in range(5):
            self.lstlbl.append(Label((390, 40 * n), **labels))

        self.lst_en_dis = []  # Controls affected by disable button.
        # Button assortment
        x = 0
        for t in table:
            self.lst_en_dis.append(
                Button((x, 0),
                       font=font14,
                       shape=CIRCLE,
                       callback=self.callback,
                       **t))
            x += 70

# Highlighting buttons
        x = 0
        for t in table_highlight:
            self.lst_en_dis.append(
                Button((x, 60),
                       fgcolor=CYAN,
                       shape=CIRCLE,
                       fontcolor=BLACK,
                       litcolor=WHITE,
                       font=font14,
                       callback=self.callback,
                       **t))
            x += 70


# Pad
        lbl_pad = Label((0, 200),
                        value='touch me',
                        fontcolor=WHITE,
                        border=2,
                        fgcolor=RED,
                        bgcolor=DARKGREEN,
                        font=font14)
        pad = Pad((0, 200),
                  width=lbl_pad.width,
                  height=lbl_pad.height,
                  onrelease=False,
                  callback=lambda _: lbl_pad.value('Short'),
                  lp_callback=lambda _: lbl_pad.value('Long'))
        self.lst_en_dis.append(pad)

        # Start/Stop toggle
        self.bs = ButtonList(self.callback)
        self.bs0 = None
        for t in table_buttonset:  # Buttons overlay each other at same location
            button = self.bs.add_button((0, 240),
                                        font=font14,
                                        fontcolor=BLACK,
                                        height=30,
                                        **t)
            if self.bs0 is None:  # Save for reset button callback
                self.bs0 = button
        self.lst_en_dis.append(self.bs)

        # Radio buttons
        x = 0
        self.rb = RadioButtons(BLUE, self.callback)  # color of selected button
        self.rb0 = None
        for t in table_radiobuttons:
            button = self.rb.add_button((x, 140),
                                        font=font14,
                                        fontcolor=WHITE,
                                        fgcolor=(0, 0, 90),
                                        height=40,
                                        width=40,
                                        **t)
            if self.rb0 is None:  # Save for reset button callback
                self.rb0 = button
            x += 60
        self.lst_en_dis.append(self.rb)
        # Checkbox
        self.cb1 = Checkbox((340, 0), callback=self.cbcb, args=(0, ))
        self.cb2 = Checkbox((340, 40),
                            fillcolor=RED,
                            callback=self.cbcb,
                            args=(1, ))
        self.lst_en_dis.extend([self.cb1, self.cb2])
        # Reset button
        self.lbl_reset = Label((200, 210),
                               font=font10,
                               value='Reset also responds to long press')
        self.btn_reset = Button((300, 240),
                                font=font14,
                                height=30,
                                width=80,
                                fgcolor=BLUE,
                                shape=RECTANGLE,
                                text='Reset',
                                fill=True,
                                callback=self.cbreset,
                                args=(4, ),
                                onrelease=False,
                                lp_callback=self.callback,
                                lp_args=('long', 4))
        # Quit
        self.btn_quit = Button((390, 240),
                               font=font14,
                               height=30,
                               width=80,
                               fgcolor=RED,
                               shape=RECTANGLE,
                               text='Quit',
                               callback=self.quit)
        # Enable/Disable toggle
        self.bs_en = ButtonList(self.cb_en_dis)
        self.bs_en.add_button((200, 240),
                              font=font14,
                              fontcolor=BLACK,
                              height=30,
                              width=90,
                              fgcolor=GREEN,
                              shape=RECTANGLE,
                              text='Disable',
                              args=(True, ))
        self.bs_en.add_button((200, 240),
                              font=font14,
                              fontcolor=BLACK,
                              height=30,
                              width=90,
                              fgcolor=RED,
                              shape=RECTANGLE,
                              text='Enable',
                              args=(False, ))