Created
April 12, 2023 23:39
-
-
Save mokjpn/064994b32b3583ea2cdc109f9c9e7e5e to your computer and use it in GitHub Desktop.
M5StackのCM4Stackを用いてタイムレコーダーっぽいものを作る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# M5StackのCM4Stackを使って出退勤記録ボタンのようなものを作る | |
# このスクリプトはボタンだけで、別途用意したSubmitSyukkin.py, SubmitTaikin.py | |
# を起動する。CM4StackではSeleniumなども動くので幅広く応用できるかも。 | |
import sys | |
import os | |
import time | |
import subprocess | |
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton | |
from PyQt5.QtCore import Qt, QTimer | |
from PyQt5.QtGui import QCursor, QPixmap, QImage | |
from evdev import InputDevice, ecodes, list_devices | |
from PyQt5.QtCore import QThread, pyqtSignal | |
os.environ["QT_QPA_PLATFORM"] = "linuxfb" | |
# タッチイベントを処理するスレッド | |
class TouchEventThread(QThread): | |
syukkin_signal = pyqtSignal() | |
taikin_signal = pyqtSignal() | |
def run(self): | |
devices = [InputDevice(fn) for fn in list_devices()] | |
touch_device = None | |
for dev in devices: | |
if 'touch' in dev.name.lower(): | |
touch_device = dev | |
break | |
if touch_device is not None: | |
for event in touch_device.read_loop(): | |
self.handle_event(event) | |
else: | |
print("Touch device not found.") | |
# タッチイベントを処理する関数 | |
def handle_event(self,event): | |
global x,y | |
if event.type == ecodes.EV_ABS: | |
if event.code == ecodes.ABS_MT_POSITION_X: | |
x = event.value | |
elif event.code == ecodes.ABS_MT_POSITION_Y: | |
y = event.value | |
elif event.type == ecodes.EV_KEY and event.code == ecodes.BTN_TOUCH and event.value == 1: | |
if y < 160: | |
self.syukkin_signal.emit() | |
elif y >= 160: | |
self.taikin_signal.emit() | |
# ボタン表示とタッチイベントを処理するアプリケーション | |
class TouchButtonApp(QWidget): | |
def __init__(self): | |
super().__init__() | |
self.initUI() | |
self.touch_event_thread = TouchEventThread() | |
self.touch_event_thread.syukkin_signal.connect(self.syukkin_button.animateClick) | |
self.touch_event_thread.taikin_signal.connect(self.taikin_button.animateClick) | |
self.touch_event_thread.start() | |
# UIを初期化する関数 | |
def initUI(self): | |
self.setGeometry(0, 0, 240, 320) | |
self.syukkin_button = QPushButton('出勤', self) | |
self.syukkin_button.setGeometry(0, 0, 240, 160) | |
self.syukkin_button.setStyleSheet("background-color: green; color: white; font-size: 40px;") | |
self.syukkin_button.clicked.connect(self.syukkin_button_clicked) | |
self.taikin_button = QPushButton('退勤', self) | |
self.taikin_button.setGeometry(0, 160, 240, 160) | |
self.taikin_button.setStyleSheet("background-color: red; color: white; font-size: 40px;") | |
self.taikin_button.clicked.connect(self.taikin_button_clicked) | |
self.showFullScreen() | |
# 出勤ボタンがクリックされたときの関数 | |
def syukkin_button_clicked(self): | |
self.syukkin_button.setStyleSheet("background-color: white; color: green; font-size: 40px;") | |
QTimer.singleShot(500, self.reset_syukkin_button) | |
subprocess.Popen(['python', 'SubmitSyukkin.py']) | |
# 出勤ボタンの色を元に戻す関数 | |
def reset_syukkin_button(self): | |
self.syukkin_button.setStyleSheet("background-color: green; color: white; font-size: 40px;") | |
# 退勤ボタンがクリックされたときの関数 | |
def taikin_button_clicked(self): | |
self.taikin_button.setStyleSheet("background-color: white; color: red; font-size: 40px;") | |
QTimer.singleShot(500, self.reset_taikin_button) | |
subprocess.Popen(['python', 'SubmitTaikin.py']) | |
# 退勤ボタンの色を元に戻す関数 | |
def reset_taikin_button(self): | |
self.taikin_button.setStyleSheet("background-color: red; color: white; font-size: 40px;") | |
def main(): | |
app = QApplication(sys.argv) | |
app.setOverrideCursor(QCursor(QPixmap.fromImage(QImage(1, 1, QImage.Format_ARGB32)))) touch_button_app = TouchButtonApp() | |
x = 0 | |
y = 0 | |
sys.exit(app.exec_()) | |
if name == 'main': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment