Exemple #1
0
button_mask = const((1 << BUTTON_RIGHT)
                    | (1 << BUTTON_DOWN)
                    | (1 << BUTTON_LEFT)
                    | (1 << BUTTON_UP)
                    | (1 << BUTTON_SEL)
                    | (1 << BUTTON_A)
                    | (1 << BUTTON_B))

i2c_bus = board.I2C()

ss = Seesaw(i2c_bus, 0x5E)

ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP)

while True:
    buttons = ss.digital_read_bulk(button_mask)
    if not buttons & (1 << BUTTON_RIGHT):
        print("Button RIGHT pressed")

    if not buttons & (1 << BUTTON_DOWN):
        print("Button DOWN pressed")

    if not buttons & (1 << BUTTON_LEFT):
        print("Button LEFT pressed")

    if not buttons & (1 << BUTTON_UP):
        print("Button UP pressed")

    if not buttons & (1 << BUTTON_SEL):
        print("Button SEL pressed")
Exemple #2
0
class MiniTFTFeatherWing:
    """Class representing an `Mini Color TFT with Joystick FeatherWing
       <https://www.adafruit.com/product/3321>`_.

       Automatically uses the feather's I2C bus."""

    _button_mask = ((1 << BUTTON_RIGHT) | (1 << BUTTON_DOWN) |
                    (1 << BUTTON_LEFT) | (1 << BUTTON_UP) | (1 << BUTTON_SEL) |
                    (1 << BUTTON_A) | (1 << BUTTON_B))

    #pylint: disable-msg=too-many-arguments
    def __init__(self, address=0x5E, i2c=None, spi=None, cs=None, dc=None):
        displayio.release_displays()
        if i2c is None:
            i2c = board.I2C()
        if spi is None:
            spi = board.SPI()
        if cs is None:
            cs = board.D5
        if dc is None:
            dc = board.D6
        self._ss = Seesaw(i2c, address)
        self._ss.pin_mode_bulk(self._button_mask, self._ss.INPUT_PULLUP)
        self._ss.pin_mode(8, self._ss.OUTPUT)
        self._ss.digital_write(8, True)  # Reset the Display via Seesaw
        self._backlight = PWMOut(self._ss, 5)
        self._backlight.duty_cycle = 0
        display_bus = displayio.FourWire(spi, command=dc, chip_select=cs)
        self.display = ST7735R(display_bus,
                               width=160,
                               height=80,
                               colstart=24,
                               rotation=270,
                               bgr=True)

    #pylint: enable-msg=too-many-arguments

    @property
    def backlight(self):
        """
        Return the current backlight duty cycle value
        """
        return self._backlight.duty_cycle / 255

    @backlight.setter
    def backlight(self, brightness):
        """
        Set the backlight duty cycle
        """
        self._backlight.duty_cycle = int(255 * min(max(brightness, 0.0), 1.0))

    @property
    def buttons(self):
        """
        Return a set of buttons with current push values
        """
        try:
            button_values = self._ss.digital_read_bulk(self._button_mask)
        except OSError:
            return Buttons(*[
                False
                for button in (BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT,
                               BUTTON_RIGHT, BUTTON_A, BUTTON_B, BUTTON_SEL)
            ])
        return Buttons(*[
            not button_values & (1 << button)
            for button in (BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT,
                           BUTTON_A, BUTTON_B, BUTTON_SEL)
        ])
class MiniTFTFeatherWing:
    """Class representing an `Mini Color TFT with Joystick FeatherWing
       <https://www.adafruit.com/product/3321>`_.

       Automatically uses the feather's I2C bus."""

    _button_mask = ((1 << BUTTON_RIGHT) | (1 << BUTTON_DOWN) |
                    (1 << BUTTON_LEFT) | (1 << BUTTON_UP) | (1 << BUTTON_SEL) |
                    (1 << BUTTON_A) | (1 << BUTTON_B))

    def __init__(self, address=0x5E, i2c=None, spi=None):
        if i2c is None:
            i2c = board.I2C()
        if spi is None:
            spi = board.SPI()
        self._ss = Seesaw(i2c, address)
        self._backlight = PWMOut(self._ss, 5)
        self._backlight.duty_cycle = 0
        displayio.release_displays()
        while not spi.try_lock():
            pass
        spi.configure(baudrate=24000000)
        spi.unlock()
        self._ss.pin_mode(8, self._ss.OUTPUT)
        self._ss.digital_write(8, True)  # Reset the Display via Seesaw
        display_bus = displayio.FourWire(spi,
                                         command=board.D6,
                                         chip_select=board.D5)
        self.display = ST7735R(display_bus,
                               width=160,
                               height=80,
                               colstart=24,
                               rotation=270,
                               bgr=True)
        self._ss.pin_mode_bulk(self._button_mask, self._ss.INPUT_PULLUP)

    @property
    def backlight(self):
        """
        Return the current backlight duty cycle value
        """
        return self._backlight.duty_cycle / 255

    @backlight.setter
    def backlight(self, brightness):
        """
        Set the backlight duty cycle
        """
        self._backlight.duty_cycle = int(255 *
                                         min(max(1 - brightness, 0.0), 1.0))

    @property
    def buttons(self):
        """
        Return a set of buttons with current push values
        """
        button_values = self._ss.digital_read_bulk(self._button_mask)
        return Buttons(*[
            not button_values & (1 << button)
            for button in (BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT,
                           BUTTON_A, BUTTON_B, BUTTON_SEL)
        ])
Exemple #4
0
ss.pin_mode_bulk(button_mask, ss.INPUT_PULLUP)

last_x = 0
last_y = 0

while True:
    x = ss.analog_read(2)
    y = ss.analog_read(3)

    if  (abs(x - last_x) > 3) or (abs(y - last_y) > 3):
        print(x, y)
        last_x = x
        last_y = y

    buttons = ss.digital_read_bulk(button_mask)
    if not buttons & (1 << BUTTON_RIGHT):
        print("Button A pressed")

    if not buttons & (1 << BUTTON_DOWN):
        print("Button B pressed")

    if not buttons & (1 << BUTTON_LEFT):
        print("Button Y pressed")

    if not buttons & (1 << BUTTON_UP):
        print("Button x pressed")

    if not buttons & (1 << BUTTON_SEL):
        print("Button SEL pressed")
Exemple #5
0
class MiniTFTFeatherWing_T:
    """
    Row display for MiniTFT Featherwing
    """
    def __init__(self):
        self.reset_pin = 8
        self.i2c = board.I2C()
        self.ss = Seesaw(self.i2c, 0x5E)
        self.ss.pin_mode(self.reset_pin, self.ss.OUTPUT)

        self.spi = board.SPI()
        self.tft_cs = board.D5
        self.tft_dc = board.D6
        self._auto_show = True

        displayio.release_displays()
        self.display_bus = displayio.FourWire(self.spi,
                                              command=self.tft_dc,
                                              chip_select=self.tft_cs)

        self.ss.digital_write(self.reset_pin, True)
        self.display = ST7735R(self.display_bus,
                               width=160,
                               height=80,
                               colstart=24,
                               rotation=270,
                               bgr=True)

        self.nbr_rows = 5
        self.row_height = int(self.display.height / self.nbr_rows)
        self.row_vpos = [
            int(i * self.row_height) for i in range(self.nbr_rows)
        ]
        self.row_bkgnd_color = [
            0x808080, 0xFF0000, 0x000040, 0x0000FF, 0xAA0088
        ]
        self.row_text_color = [
            0xFFFFFF, 0xFFFF00, 0xFF0000, 0xFFFF00, 0xFAFAFA
        ]
        self.row_text = ['r1', 'r2', 'r3', 'r4', 'r5']
        # -----------------------------------------------------------------------------------
        # Button handler
        # -----------------------------------------------------------------------------------
        self.btn_mat = []
        self.btn_mat.append([1 << BUTTON_RIGHT, 0, 0, False, 'right'])
        self.btn_mat.append([1 << BUTTON_DOWN, 0, 0, False, 'down'])
        self.btn_mat.append([1 << BUTTON_LEFT, 0, 0, False, 'left'])
        self.btn_mat.append([1 << BUTTON_UP, 0, 0, False, 'up'])
        self.btn_mat.append([1 << BUTTON_SEL, 0, 0, False, 'select'])
        self.btn_mat.append([1 << BUTTON_A, 0, 0, False, 'A'])
        self.btn_mat.append([1 << BUTTON_B, 0, 0, False, 'B'])
        # print(btn_mat)
        self.btn_mask = 0
        for mask_indx in range(len(self.btn_mat)):
            self.btn_mask = self.btn_mask | self.btn_mat[mask_indx][0]
            # print(btn_mask)
        self.ss.pin_mode_bulk(self.btn_mask, self.ss.INPUT_PULLUP)

        self.state_dict = {
            'idle': 0,
            'pressed': 1,
            'pressed_deb': 2,
            'released': 3,
            'released_deb': 4
        }

        self.btn_repeat_time = 1.0
        self.btn_deb_time = 0.1

    def print_at(self, row_indx, txt):
        """
        Print one row a specific row number 0-4

        """
        try:
            self.row_text[row_indx] = txt
        except IndexError:
            self.row_text[self.nbr_rows -
                          1] = 'incorrect row index: ' + str(row_indx)
        if self._auto_show:
            self.show_rows()

    def show_rows(self):
        """
        Show printed rows on the display
        - use print_at to fill the rows first
        - background colors can be changed using the background_color function
        - text colors can be changed using the text_color function
        call show_rows when text and colors are OK
        """
        row_disp = displayio.Group(max_size=10)
        self.display.show(row_disp)
        print('show')
        for i in range(self.nbr_rows):

            # Draw row rectangles
            row_bitmap = displayio.Bitmap(self.display.width, self.row_height,
                                          1)
            row_palette = displayio.Palette(1)
            row_palette[0] = self.row_bkgnd_color[i]
            row_block = displayio.TileGrid(row_bitmap,
                                           pixel_shader=row_palette,
                                           x=0,
                                           y=self.row_vpos[i])
            row_disp.append(row_block)
            text_group = displayio.Group(max_size=10,
                                         scale=1,
                                         x=8 + i,
                                         y=6 + i * self.row_height)
            text_area = label.Label(terminalio.FONT,
                                    text=self.row_text[i],
                                    color=self.row_text_color[i])
            text_group.append(text_area)  # Subgroup for text scaling
            row_disp.append(text_group)

    def background_color(self, row_indx, color_code):
        """
        change background color for one row
        row_indx = 0-4
        color_code = 24 bit binary input 0xrrggbb
        """
        try:
            self.row_bkgnd_color[row_indx] = color_code
        except IndexError:
            self.row_text[self.nbr_rows -
                          1] = 'incorrect row index: ' + str(row_indx)

    def text_color(self, row_indx, color_code):
        """
        change text color for one row
        row_indx = 0-4
        color_code = 24 bit binary input 0xrrggbb
        """
        try:
            self.row_text_color[row_indx] = color_code
        except IndexError:
            self.row_text[self.nbr_rows -
                          1] = 'incorrect row index: ' + str(row_indx)

    @property
    def auto_show(self):
        return self._auto_show

    @auto_show.setter
    def auto_show(self, t_f):
        _auto_show = t_f

    @property
    def backlight(self):
        """
        Return the current backlight duty cycle value
        """
        return self._backlight.duty_cycle / 255

    @backlight.setter
    def backlight(self, brightness):
        """
        Set the backlight duty cycle
        """
        self._backlight.duty_cycle = int(255 * min(max(brightness, 0.0), 1.0))

    # -----------------------------------------------------------------------------------
    def scan(self):
        """
        Class reading the buttons on Mini Color TFT with Joystick FeatherWing
        key pressed-released states implemented
        Keys are repeated if pressed over a time limit
        call btn_scan() as often as possible. no upper limited but slow scanning will
        decrease the performance
        btn_read() returns one key at a time as text. when no key is pressed an
        empty string will be reurned
        * Author Tom Hoglund 2019
        """
        time_now = time.monotonic()
        buttons = self.ss.digital_read_bulk(self.btn_mask)

        for mask_indx in range(len(self.btn_mat)):
            if not buttons & self.btn_mat[mask_indx][0]:
                if self.btn_mat[mask_indx][1] == self.state_dict['idle']:
                    self.btn_mat[mask_indx][1] = self.state_dict['pressed']
                    self.btn_mat[mask_indx][2] = time_now
                elif self.btn_mat[mask_indx][1] == self.state_dict['pressed']:
                    if time_now > self.btn_mat[mask_indx][
                            2] + self.btn_deb_time:
                        self.btn_mat[mask_indx][1] = self.state_dict[
                            'pressed_deb']
                        self.btn_mat[mask_indx][2] = time_now
                elif self.btn_mat[mask_indx][1] == self.state_dict[
                        'pressed_deb']:
                    if time_now > self.btn_mat[mask_indx][
                            2] + self.btn_repeat_time:
                        # btn_mat[mask_indx][1] = state_dict['pressed_deb']
                        self.btn_mat[mask_indx][2] = time_now
                        self.btn_mat[mask_indx][3] = True

            else:
                if self.btn_mat[mask_indx][1] == self.state_dict[
                        'pressed_deb']:
                    self.btn_mat[mask_indx][1] = self.state_dict['released']
                    self.btn_mat[mask_indx][2] = time_now
                elif self.btn_mat[mask_indx][1] == self.state_dict['released']:
                    if time_now > self.btn_mat[mask_indx][
                            2] + self.btn_deb_time:
                        self.btn_mat[mask_indx][1] = self.state_dict[
                            'released_deb']
                        self.btn_mat[mask_indx][3] = True
            if self.btn_mat[mask_indx][1] == self.state_dict['released_deb']:
                if not self.btn_mat[mask_indx][3]:
                    self.btn_mat[mask_indx][1] = self.state_dict['idle']
                    self.btn_mat[mask_indx][2] = time_now

    def read(self):
        ret_val = ''
        for mask_indx in range(len(self.btn_mat)):
            if self.btn_mat[mask_indx][3]:
                self.btn_mat[mask_indx][3] = False
                self.btn_mat[mask_indx][1] = self.state_dict['idle']
                ret_val = self.btn_mat[mask_indx][4]
                break
        return ret_val