limitlessledその3

PythonGUIプログラミングをするときはだいたいPyQtを使っています。あまり使い慣れていないということもありまして一つのGUIプログラムを作るのに非常に時間がかかっていたのですが、この前いじっているときにようやっとコツを掴んだというか理解したのである程度のものは作れるようになりました。

そこで前々から作ろうと思っていたlimitlessledのタイマーを作ったので公開してみようと思います。

まず、自分の環境についてですが

  • macOS Sierra 10.12.3
  • anaconda3-4.1.0

です。どこまで環境を書けばいいのかわからないのですが、もう少し詳しく書くと、まずmacにhomebrewを入れて、その後pyenvを入れました。pyenvからanacondaを入れました。anacondaにはPyQtが標準で入ってたはずですが、入っていなければ

conda install pyqt4

とかでインストールできると思います。なので、あとはlimitlessledのPythonライブラリであるledcontrollerのインストールのみで自分のプログラムは起動できるはずです。ledcontrollerのインストールは上記のように

conda install ledcontroller

でいけるはずです。

あとanacondaのPATHなんかは通しておいてください。

実際のプログラムは以下になります。

# -*- coding: utf-8 -*-


import sys
import PyQt4.QtGui as gui
import PyQt4.QtCore as core
import ledcontroller as led
import time
import threading as th

IP = 'xxx.xxx.xx.xx'
LED = led.LedController(IP, group_1='white', group_2='white', group_3='white', group_4='white')

class UI(gui.QMainWindow):
    def __init__(self):
        super().__init__()
        self.cancel = False
        self.progress = 0
    
    def initUI(self):
        exitGUI = gui.QApplication.style().standardIcon(gui.QStyle.SP_TitleBarCloseButton)
        exitAction = gui.QAction(exitGUI, 'Quit Milight', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(gui.qApp.quit)
        
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('Milight')
        fileMenu.addAction(exitAction)
        menubar.setNativeMenuBar(False)
        
        #group_box1
        #hbox1
        tim = gui.QComboBox(parent = self)
        tim.activated[str].connect(self._activate)
        times = ['30', '60', '90', '120']
        tim.addItems(times)
        cancel = gui.QPushButton('cancel', parent = self)

        hbox1 = gui.QHBoxLayout()       
        hbox1.addWidget(tim)
        hbox1.addStretch(1)
        hbox1.addWidget(cancel)
        
        #hbox2
        self.pbar = gui.QProgressBar(parent = self)
        self.percentage = gui.QLabel('0%', parent = self)
        
        def stop_progress():
            self.cancel = True
        cancel.clicked.connect(stop_progress)
        
        hbox2 = gui.QHBoxLayout()
        hbox2.addWidget(self.pbar)
        hbox2.addWidget(self.percentage)        
        
        vbox1 = gui.QVBoxLayout()
        vbox1.addLayout(hbox1)
        vbox1.addLayout(hbox2)

        group_box1 = gui.QGroupBox('Timer', parent = self)
        group_box1.setLayout(vbox1)
        
        #group_box2
        warm = gui.QPushButton('Warm')
        warm.clicked.connect(LED.warmer)
        cool = gui.QPushButton('Cool')
        cool.clicked.connect(LED.cooler)

        
        hbox3 = gui.QHBoxLayout()
        hbox3.addWidget(warm)
        hbox3.addWidget(cool)

        
        group_box2 = gui.QGroupBox('Cool and Warm')
        group_box2.setLayout(hbox3)
        
        on = gui.QPushButton('ON', parent = self)
        on.clicked.connect(LED.on)
        off = gui.QPushButton('OFF', parent = self)
        off.clicked.connect(LED.off)
        
        hbox4 = gui.QHBoxLayout()
        hbox4.addStretch(1)
        hbox4.addWidget(on)
        hbox4.addWidget(off)
        
        Gbox = gui.QVBoxLayout()
        Gbox.addWidget(group_box1)
        Gbox.addWidget(group_box2)
        Gbox.addLayout(hbox4)
        
        w = gui.QWidget(parent = self)
        w.setLayout(Gbox)
        self.setCentralWidget(w)
        
        self.setGeometry(100, 150, 250, 250)
        self.setWindowTitle('Milight')
        self.show()
    
    def _activate(self, text):
        print(time.ctime())
        minutes = int(text)
        self.cancel = False
        
        thread1 = th.Thread(target=self._ti, args =(minutes,))
        thread1.start()
    
    def _ti(self, minutes):
        for i in range(minutes*60):
            if(self.cancel):
                break;
            self.progress = (i+1)/(minutes*60)*100
            time.sleep(1)
            self.pbar.setValue(self.progress)
            self.percentage.setText('{}%'.format(round(self.progress)))
        if(not self.cancel):
            LED.off()
        self.pbar.setValue(0)
        self.percentage.setText('0%')
            
        


def main():
    app = gui.QApplication(sys.argv)
    ui = UI()
    ui.initUI()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

IPと書かれているところはlimitlessledのwi-fiブリッジのIPアドレスを入力してください。自分は固定IPを振っています。あと新しくlimitlessled dual whiteを購入してそちらを使っているので一番最初の

LED = led.LedController(IP, group_1='white', group_2='white', group_3='white', group_4='white')

のところで

group_x = 'white'

としています。この前で使っていたRGBWの方はこのような引数を与える必要はなかったのですがdual whiteでは必要になるようです。 環境さえ整えばmacじゃなくてもlinuxwindowsとかでも動くんじゃないかなと思います。 あと不具合というほどのことでもないのですが、自分の環境ではこのプログラムを起動して最前面にしておかないとTimerが動いてくれない時があります。

初めてまともなGUIを自分で組めました。わりと達成感がありますね。これからも色々作っていけたらいいなと思います。

起動したときのスクショは以下のようになります。

f:id:jinpei0908:20170218094932p:plain