Проблема с изменением приложения kivy для ввода в arduino через raspberry pi и python - PullRequest
0 голосов
/ 10 октября 2019

кто-нибудь может мне помочь? Я подключил Arduino к Raspberry Pi, потому что у него больше контактов. Я создал с помощью Python и KIVY GUI, но приложение не меняет фон метки или текст, когда он получает сигнал от Arduino. Я перепробовал много примеров, и они работают. Можете ли вы показать мне, где я делаю ошибку. Это только начало проекта.

Код Python:

    from sys import platform
    from kivy.app import App
    from kivy.clock import Clock
    from kivy.core.window import Window
    from kivy.utils import get_color_from_hex
    from kivy.lang import Builder
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.label import Label
    from kivy.uix.button import Button
    from kivy.properties import ListProperty
    from kivy.graphics import Color
    from kivy.uix.boxlayout import BoxLayout
    import pyfirmata
    from pyfirmata import ArduinoMega, util
    from pyfirmata.util import Iterator
    import serial.tools.list_ports
    from datetime import datetime, date, time



    # Decide operation system
    if platform == 'linux' or platform == 'linux2':
        def serial_ports_linux():
            ports = list(serial.tools.list_ports.comports())  
            for port_no, description, address in ports:
                if 'ACM' in description:
                    return port_no
                if 'Arduino' in description:
                    return port_no
                if 'Serial' in description:
                    return port_no
        board = ArduinoMega(serial_ports_linux())
    elif platform == 'win32':
        def serial_ports_win():
            ports = list(serial.tools.list_ports.comports())  
            for port_no, description, address in ports:
                if 'Arduino' in description:
                    return port_no
                if 'CH340' in description:
                    return port_no
        board = ArduinoMega(serial_ports_win())

    board.digital[2].mode = pyfirmata.INPUT
    board.digital[4].mode = pyfirmata.INPUT
    it = util.Iterator(board)
    it.start()
    step_1 = 0
    step_2 = 0




    class MainWidget(FloatLayout):



        def update(self,dt):
            board.digital[1].write(1)
            self.ids.pin02.value = board.digital[1].read()
            if board.digital[2].read() == 1:
                self.ids.pin02.text.replace('')





    class HalungApp(App):
        def build(self):
            Clock.schedule_interval(self.root.update, 0)
        def on_stop(self):
            board.exit()

    if __name__ == '__main__':
        Window.clearcolor = get_color_from_hex('F0F0F0')
        # Window.fullscreen = True
        HalungApp().run()

Код KIVY:

    <SLabel@Label>:
        bcolor: .245,.245,.245,1
        canvas.before:
            Color:
                rgba: self.bcolor
            Rectangle:
                pos: self.pos
                size: self.size

    <MainWidget>:
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'top'
            ScreenManager:
                size_hint: 1, .9
                id: _screen_manager
                Screen:
                    name: 'screen1'
                    BoxLayout: 
                        orientation: 'vertical'
                        padding: 50
                        BoxLayout:
                            orientation: 'horizontal'
                            Image:
                                source: 'halung_logo.png'
                                size: self.texture_size
                            Label:
                                text: "04-016059-001"
                                color: 0,0,0,1
                                bold: True
                                font_size: 40
                        BoxLayout:
                            orientation: 'horizontal'
                            Label:
                                text: "Connections"
                                color: 0,0,0,1
                                bold: True
                                font_size: 30
                        BoxLayout:
                            orientation: 'horizontal'
                            SLabel:
                                id: pin02
                                text: "1K1/2 - E1/L1"
                            SLabel:
                                id: pin04
                                text: "1K1/4 - E1/L2"
                            SLabel:
                                text: "1K1/6 - E1/L3"
                            SLabel:
                                text: "K2/2 - E2/L1"
                        BoxLayout:
                            orientation: 'horizontal'
                            SLabel:
                                text: "K2/4 - E2/L2"
                            SLabel:
                                text: "K2/6 - E2/L3"
                            SLabel:
                                text: "K3/2 - M1(U1)"
                            SLabel:
                                text: "K3/4 - M1(V1)"
                        BoxLayout:
                            orientation: 'horizontal'
                            SLabel:
                                text: "K3/6 - M1(W1)"
                            SLabel:
                                text: "PE(M1) - PE(M1)"
                            SLabel:
                                text: "PE(E1) - PE(E1)"
                            SLabel:
                                text: "M1(U2) - M1(W2)"
                            SLabel:
                                text: "M1(U2) - M1(V2)"
                        BoxLayout:
                            orientation: 'horizontal'
                            Label:
                                text: "Cable Ties"
                                color: 0,0,0,1
                                bold: True
                                font_size: 30
                        BoxLayout:
                            orientation: 'horizontal'
                            SLabel:
                                text: "Cable Tie 1"
                            SLabel:
                                text: "Cable Tie 2"
                            SLabel:
                                text: "Cable Tie 3"
                            SLabel:
                                text: "Cable Tie 4"
                        BoxLayout:
                            orientation: 'horizontal'
                            SLabel:
                                text: "Cable Tie 5"
                            SLabel:
                                text: "Cable Tie 6"
                            SLabel:
                                text: "Cable Tie 7"
                Screen:
                    name: 'screen2'
                    Label: 
                        text: 'Another Screen'
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'bottom'
            BoxLayout:
                orientation: 'horizontal'
                size_hint: 1, .1
                Button:
                    text: 'Time for cable: '
                    on_press: _screen_manager.current = 'screen1'
                Button:
                    text: 'Total: '
                    text: 'Total: '
                    on_press: _screen_manager.current = 'screen2'

...