pyqt写个星三角降压启动方式2

星三角降压启动用可以用类进行封装,就像博图FB块那样。把逻辑都在类里完成,和外界需要交互的暴露出接口。测试过程中,发现类中直接用定时器QTimer会出现问题。然后就把定时器放到外面了。然后测试功能正常。

from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *



class Motor:
    def __init__(self,isrun:bool=None,is_star_run:bool=None,is_tran_run:bool=None,fault:bool=None):
        self.is_main_run = isrun
        self.is_star_run = is_star_run
        self.is_tran_run = is_tran_run,
        self.fault = fault

    @staticmethod
    def get_bool(bytes:bytearray,address:int):
        byte_index = address//8
        bit_index = address % 8
        return bool(bytes[byte_index]>>bit_index & 1)
    @staticmethod
    def set_bool(bytes:bytearray,address:int,value:bool):
        if value not in {1,0,False,True}:
            raise TypeError(f"Value value:{value} is not a boolean expression.")
        byte_index = address // 8
        bit_index = address % 8
        index_value = 1<< bit_index

        current_value = Motor.get_bool(bytes,address)

        if current_value == value:
            return
        if value:
            bytes[byte_index] += index_value
        else:
            bytes[byte_index] -= index_value

    def __str__(self):
        return f'{self.is_main_run} {self.is_star_run} {self.is_tran_run} {self.fault}'





class StarMotor(QObject):
    def __init__(self,motor:Motor=None,tim:QTimer=None):
        self.interval = 3000
        self.timer = tim

        self.timer.setInterval(self.interval)
        self.motor = motor
        self.startTimer()

    def set_interval(self,val:int):
        self.interval = val


    def on_timer(self):
        print("timer is starting")
        self.motor.set_bool()

    def start(self,state,bytes,out1,out2,out3):
        def on_timer():
            self.motor.set_bool(bytes,out3,1)
            self.motor.set_bool(bytes,out2,0)
            print("三角启动")

        if state:
            if not all([self.motor.is_main_run,self.motor.is_star_run,self.motor.is_tran_run]):
                self.motor.set_bool(bytes,out1,1)
                self.motor.set_bool(bytes,out2,1)
                print("星启动")

            if self.motor.is_main_run and self.motor.is_star_run:
                if not self.timer.isActive():
                    self.timer.timeout.connect(on_timer)
                    self.timer.setSingleShot(True)
                    self.timer.start()


            if self.motor.fault:
                self.motor.set_bool(bytes,out1,0)
                self.motor.set_bool(bytes,out2,0)
                self.motor.set_bool(bytes,out3,0)
                print("fault")

        if not state:
            self.motor.set_bool(bytes, out1, 0)
            self.motor.set_bool(bytes, out2, 0)
            self.motor.set_bool(bytes, out3, 0)
            print("stop")









class Ui(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(600,600)
        self.init_ui()
        self.state = None
        self.motor = Motor()
        self.timer = QTimer(self)

        self.timer.timeout.connect(self.on_timer_loop)
        self.timer.start(100)
        self.bytes = bytearray(1)
        time2 = QTimer(self)
        self.d = StarMotor(self.motor,time2)
    def on_timer_loop(self):
        self.d.start(self.state,self.bytes,0,1,2)
        print(self.bytes)





    def init_ui(self):
        self.btn = QPushButton('start',self)
        self.btn2 = QPushButton('stop',self)
        self.btn3 = QPushButton('主运行',self)
        self.btn4 = QPushButton('星运行',self)
        self.btn5 = QPushButton('三角运行',self)
        self.btn6 = QPushButton('故障',self)

        self.btn.setCheckable(True)
        self.btn2.setCheckable(True)
        self.btn3.setCheckable(True)
        self.btn4.setCheckable(True)
        self.btn5.setCheckable(True)
        self.btn6.setCheckable(True)

        self.btn.move(100,30)
        self.btn2.move(100,80)
        self.btn3.move(100,130)
        self.btn4.move(100,230)
        self.btn5.move(100,330)
        self.btn6.move(100,430)

        self.btn.clicked.connect(self.btn_click)
        self.btn2.clicked.connect(self.btn_click2)
        self.btn3.clicked.connect(self.btn_click3)
        self.btn4.clicked.connect(self.btn_click4)
        self.btn5.clicked.connect(self.btn_click5)
        self.btn6.clicked.connect(self.btn_click6)

    def btn_click(self,checked):
        self.state = checked

    def btn_click2(self,checked):
        pass
    def btn_click3(self,checked):
        self.motor.is_main_run = checked
    def btn_click4(self,checked):
        self.motor.is_star_run=checked
    def btn_click5(self,checked):
        self.motor.is_tran_run = checked
    def btn_click6(self,checked):
        self.motor.fault=checked

















if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    win = Ui()
    win.show()

    sys.exit(app.exec())








相关推荐

  1. pyqt三角降压启动方式2

    2024-04-14 03:40:02       38 阅读
  2. 用python三子棋游戏

    2024-04-14 03:40:02       55 阅读
  3. Dockerfile怎么及运行启动方法

    2024-04-14 03:40:02       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-14 03:40:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-14 03:40:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-14 03:40:02       82 阅读
  4. Python语言-面向对象

    2024-04-14 03:40:02       91 阅读

热门阅读

  1. postgis使用

    2024-04-14 03:40:02       38 阅读
  2. photoshop基础学习笔记

    2024-04-14 03:40:02       42 阅读
  3. 第六周学习笔记DAY.1

    2024-04-14 03:40:02       38 阅读
  4. 100个好用的安全工具推荐

    2024-04-14 03:40:02       26 阅读
  5. C++ 传值调用

    2024-04-14 03:40:02       40 阅读
  6. 有趣的小知识(五).sh文件是什么

    2024-04-14 03:40:02       39 阅读
  7. CMakeLists使用总结

    2024-04-14 03:40:02       41 阅读
  8. 设计模式示例

    2024-04-14 03:40:02       40 阅读
  9. 机器学习算法

    2024-04-14 03:40:02       35 阅读
  10. Android沙盒机制

    2024-04-14 03:40:02       39 阅读
  11. 在 Ubuntu 系统上安装 Python 3.10.0

    2024-04-14 03:40:02       32 阅读