Example #1
0
for x in range(6):
    Slider(
        (0,100,0),
        (255, 255, 0),
        (205 + (x*70), 20),
        (40, 390),
        None)

Slider(
    (0,100,0),
    (0, 255, 255),
    (205,430),
    (390,40),
    None)

ts.run()

running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == K_ESCAPE:
            ts.stop()
            sys.exit()

    screen.fill((0, 0, 0))

    render_widgets(screen)

    pygame.display.flip()

    time.sleep(0.01)
Example #2
0
            state = str(state)
            stateFile.close()
            if (state == "lcd"):
                SP.call('echo "hdmi" > /home/pi/swipi-retro/assets/currentDisplayMode', shell=True)
                SP.call(['sudo','/home/pi/swipi-retro/assets/hdmi_out'])
            else :
                SP.call('echo "lcd" > /home/pi/swipi-retro/assets/currentDisplayMode', shell=True)
                SP.call(['sudo','/home/pi/swipi-retro/assets/lcd_out'])


if __name__ == "__main__":
    global shouldRun
    shouldRun = True
    ts = Touchscreen()

    for touch in ts.touches:
        touch.on_press = read_and_emulate_mouse
        touch.on_release = read_and_emulate_mouse
        touch.on_move = read_and_emulate_mouse

    ts.run()

    while shouldRun:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            ts.stop()
            exit()

    ts.stop()
    exit()
Example #3
0
class Interface:
    def __init__(self, **kwargs):
        # define interface parameters
        self.screen_size = kwargs.get('screen_size', (800, 480))
        self.fill_color = kwargs.get('fill_color', (0, 0, 0))
        self.font_color = kwargs.get('font_color', (255, 255, 255))
        self.font_size = kwargs.get('font_size', 20)
        self.font = kwargs.get('font', "freesansbold.ttf")
        cmd = 'echo %d > /sys/class/backlight/rpi_backlight/brightness' % 64
        os.system(cmd)

        # define interface variables
        self.screen = None
        self.ts = Touchscreen()
        if not pygame.get_init():
            pygame.init()
        pygame.mouse.set_visible(0)
        self.screen = pygame.display.set_mode(self.screen_size)
        for touch in self.ts.touches:
            touch.on_press = self._touch_handler
            touch.on_release = self._touch_handler

        self.ts.run()
        self.screen.fill(self.fill_color)
        self.buttons = []
        self.texts = []
        self.button = []
        self.numpad = ''

    def _draw_button(self, button, color=None):
        if not color:
            color = button.color
        self.draw(button.name, button.x, button.y, button.w, button.h,
                  button.font_color, button.font_size, color)

    def _numpad_input(self, digit):
        if any(digit):
            self.numpad += digit
        else:
            self.numpad = self.numpad[0:-1]
        self.draw(self.numpad, 500, 0, 300, 100, (255, 255, 255), 40,
                  (0, 0, 0))

    def _touch_handler(self, event, touch):
        if event == TS_PRESS:
            self.button = []
            for button in self.buttons:
                if button.x + button.w > touch.x > button.x and button.y + button.h > touch.y > button.y:
                    self.button = button
                    self._draw_button(button, button.push_color)
                    return
        if event == TS_RELEASE:
            button = self.button
            if button:
                self._draw_button(button)
                button.pressed = True
                if isinstance(button.action, str):
                    exec(button.action)
                else:
                    button.action()

    def add_button(self, **kwargs):
        button = Button(**kwargs)
        self.buttons.append(button)
        self._draw_button(button)
        return button

    def remove_button(self, button):
        self.buttons.remove(button)

    def add_numpad(self):
        for i in range(1, 10):
            self.add_button(name=str(i),
                            x=numpy.mod(i - 1, 3) * 100 + 400,
                            y=numpy.floor((i - 1) / 3) * 100 + 100,
                            action='self._numpad_input("' + str(i) + '")')
        self.add_button(name='0',
                        x=700,
                        y=200,
                        action='self._numpad_input("0")')
        self.add_button(name='<-',
                        x=700,
                        y=300,
                        action='self._numpad_input("")')
        self.add_button(name='.',
                        x=700,
                        y=100,
                        action='self._numpad_input(".")')

    def add_esc(self):
        self.add_button(name='Esc',
                        x=750,
                        y=0,
                        w=50,
                        h=50,
                        action='self.exit()')

    def draw(self,
             text,
             x=0,
             y=0,
             w=800,
             h=480,
             color=(255, 255, 255),
             size=40,
             background=None):
        if background:
            pygame.draw.rect(self.screen, background, (x, y, w, h))
        font = pygame.font.Font(self.font, size)
        lines = []
        txt = ''
        line_width = 0
        nline = 0
        for word in text.split(' '):
            word_surface = font.render(word, True, color)
            word_width, word_height = word_surface.get_size()
            if line_width + word_width >= w:
                lines.append(txt)
                txt = ''
                line_width = 0
            txt += ' ' + word
            line_width += word_width + font.size(' ')[0]
        lines.append(txt)
        offset = (h - word_height * numpy.size(lines)) / 2
        for line in lines:
            nline += 1  # Start on new row.
            text_surf = font.render(line, True, color)
            text_rect = text_surf.get_rect()
            text_rect.center = ((x + (w / 2)), (y + offset + word_height *
                                                (nline - 1) + word_height / 2))
            self.screen.blit(text_surf, text_rect)
        pygame.display.update()

    def cleanup(self):
        self.screen.fill(self.fill_color)
        self.buttons = []
        self.button = []
        self.numpad = ''

    def exit(self):
        try:
            self.ts.stop()
        finally:
            if pygame.get_init():
                pygame.mouse.set_visible(1)
                pygame.quit()