はじめに
Arduino側のスイッチが押されたことをPython側で感知しているだけ。
「pyserial」のインストールは不要。
PyQt5==5.15.0
PyQt5-sip==12.8.0
import sys
from PyQt5 import QtCore, QtSerialPort
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QImage, QPixmap
ql_x_position = (200, 400, 600, 200, 400, 600, 200, 400, 600)
qr_x_position = (1160, 1360, 1560, 1160, 1360, 1560, 1160, 1360, 1560)
q_y_position = (80, 80, 80, 280, 280, 280, 480, 480, 480)
a_x_position = (25, 215, 405, 595, 785, 975, 1165, 1355, 1545, 1735)
a_y_position = (700, 890)
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.question = True
self.serial = QtSerialPort.QSerialPort(
'COM5', baudRate=QtSerialPort.QSerialPort.Baud9600,
readyRead = self.receive)
self.serial.open(QtCore.QIODevice.ReadWrite)
def initUI(self):
self.label_list_left = [QLabel(self) for i in range(9)]
self.label_list_right = [QLabel(self) for i in range(9)]
for i in range(9):
self.label_list_left[i].setGeometry(QtCore.QRect(ql_x_position[i], q_y_position[i], 160, 160))
for i in range(9):
self.label_list_right[i].setGeometry(QtCore.QRect(qr_x_position[i], q_y_position[i], 160, 160))
self.label_list_2 = [QLabel(self) for i in range(10)]
self.label_list_3 = [QLabel(self) for i in range(10)]
for i in range(10):
self.label_list_2[i].setGeometry(QtCore.QRect(a_x_position[i], a_y_position[0], 160, 160))
for i in range(10):
self.label_list_3[i].setGeometry(QtCore.QRect(a_x_position[i], a_y_position[1], 160, 160))
def receive(self):
dummy = self.serial.readAll()
if self.question == True:
for i in range(9):
self.label_list_left[i].setPixmap(QPixmap.fromImage(image))
self.label_list_right[i].setPixmap(QPixmap.fromImage(image))
for i in range(10):
self.label_list_2[i].setPixmap(QPixmap.fromImage(image))
self.label_list_3[i].setPixmap(QPixmap.fromImage(image))
self.question = False
else:
for i in range(9):
self.label_list_left[i].clear()
self.label_list_right[i].clear()
for i in range(10):
self.label_list_2[i].clear()
self.label_list_3[i].clear()
self.question = True
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Escape:
self.close()
image = QImage('donut.png').scaled(160,160,QtCore.Qt.KeepAspectRatio)
app = QApplication(sys.argv)
ex =Window()
ex.showFullScreen()
sys.exit(app.exec_())
const int buttonON = LOW;
const int buttonOFF = HIGH;
const int buttonPin = 2;
int buttonState;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == buttonON) {
Serial.write(255);
delay(1500);
}
}