diff --git a/software/hidtest/kbapi.py b/software/hidtest/kbapi.py index 1fba64e..4be4597 100644 --- a/software/hidtest/kbapi.py +++ b/software/hidtest/kbapi.py @@ -28,6 +28,7 @@ def __init__(self): self.hidruning = False self.isdeviceoen = False self.lock=threading.Lock() + self.thid = threading.Thread(target=self.hidthread) def newpackage(self, reportid, packageid, addr, len): buffer = bytearray(8) @@ -75,23 +76,27 @@ def writedata(self, addr, buffer: bytearray): time.sleep(0.001) wait -= 1 if wait == 0: - self.lock.release() + if self.lock.locked(): + self.lock.release() return -1 else: datain = self.inputdatas.get() reportid, retlen, packageid, retaddr = struct.unpack(self.Report_HEADER_Format, bytearray(datain[0:8])) if packageid != self.getretid(): # print('pkgid error') - self.lock.release() + if self.lock.locked(): + self.lock.release() return -2 # else: # print('succeed') if (retlen != datatosent): - self.lock.release() + if self.lock.locked(): + self.lock.release() return -3 datasize -= datatosent datapoint += datatosent - self.lock.release() + if self.lock.locked(): + self.lock.release() return datasize def readdata(self, addr, len: int): @@ -112,24 +117,28 @@ def readdata(self, addr, len: int): time.sleep(0.001) wait -= 1 if wait == 0: - self.lock.release() + if self.lock.locked(): + self.lock.release() return -1, retdata else: datain = self.inputdatas.get() reportid, retlen, packageid, retaddr = struct.unpack(self.Report_HEADER_Format, bytearray(datain[0:8])) if packageid != self.totalpackageid: # print('pkgid error') - self.lock.release() + if self.lock.locked(): + self.lock.release() return -2, retdata # else: # print('succeed') if (retlen != datatoget): - self.lock.release() + if self.lock.locked(): + self.lock.release() return -3, retdata retdata += bytearray(datain[8:8 + retlen]) datasize -= datatoget datapoint += datatoget - self.lock.release() + if self.lock.locked(): + self.lock.release() return datapoint, retdata def hidthread(self): @@ -171,12 +180,18 @@ def hidthread(self): break def init_hid_interface(self): + if self.thid.is_alive(): + self.stop_and_wait() + self.thid = threading.Thread(target=self.hidthread) self.hidruning = True - self.thid = threading.Thread(target=self.hidthread) self.thid.start() def stop_and_wait(self): self.hidruning =False - self.thid.join() + self.isdeviceoen=False + if self.thid.is_alive(): + self.thid.join() + self.thid = threading.Thread(target=self.hidthread) + def wait_for_kb(self,timeout=0): timesecond=timeout/1000; while not self.isdeviceoen: diff --git a/software/hidtest/kbled.py b/software/hidtest/kbled.py index 98fcf74..703fefb 100644 --- a/software/hidtest/kbled.py +++ b/software/hidtest/kbled.py @@ -122,7 +122,7 @@ def gammafunction(self, r, g, b): def play_frame_full(self, frame): frame = cv2.resize(frame, (128, int(self.keyh / self.keyw * 128)), interpolation=cv2.INTER_NEAREST) - self.play_frame(frame) + return self.play_frame(frame) def play_frame(self, frame): xsize = frame.shape[1] @@ -165,6 +165,7 @@ def play_frame(self, frame): colordata += struct.pack('BBBB', int(colorb), int(colorg), int(colorr), 0) keyid += 1 self.kb.writedata(0x9000, colordata) + return colordata def switchmode(self,mode:int): self.kb.writedata(0x8000, struct.pack('I', mode)) def getled(self): diff --git a/software/hidtest/maingui.py b/software/hidtest/maingui.py index 9deac62..c273a38 100644 --- a/software/hidtest/maingui.py +++ b/software/hidtest/maingui.py @@ -6,6 +6,8 @@ from PyQt5.QtGui import QPixmap import sys +import cv2 +import numpy as np import threading from kbled import keyboard_led @@ -19,25 +21,40 @@ def __init__(self): self.ui.tabWidget.setCurrentIndex(0) self.kb = keyboard_ctl() self.leds=keyboard_led(self.kb) + self.video_opened=False + self.video_running=False + self.leddata=bytearray(272) self.ui.mode_set_btn.clicked.connect(self.set_mode_func) self.ui.connect_btn.clicked.connect(self.connectfunc) self.ui.stop_btn.clicked.connect(self.stopfunc) + self.ui.video_play_btn.clicked.connect(self.videofunc) + + self.videothreadid = threading.Thread(target=self.videothread) + + self.videolock=threading.Lock() self.previewtimer=QTimer() self.previewtimer.timeout.connect(self.updateperview) + self.videotimer=QTimer() + self.videotimer.timeout.connect(self.videotimerfunc) + def connectfunc(self): self.kb.init_hid_interface() if(self.kb.wait_for_kb(1000)==False): self.ui.logbox.appendPlainText("Connect Failed\n") + self.kb.stop_and_wait() return self.startTimer() def stopfunc(self): self.stopTimer() + self.stopvideo() self.kb.stop_and_wait() def closeEvent(self, event): self.kb.stop_and_wait() + self.stopvideo() + def set_mode_func(self): if self.kb.isdeviceoen==False: return @@ -53,11 +70,13 @@ def stopTimer(self): self.previewtimer.stop() def updateperview(self): if(self.kb.wait_for_kb(1000)==False):return - lens,data=self.leds.getled() - if(lens<0):return + if(self.video_running==False): + lens,self.leddata=self.leds.getled() + if(lens<0): + return width=(self.ui.preview_box.width()-10) height=(self.ui.preview_box.height()-10) - img=self.leds.getpreview_rgb(data,(width,height)) + img=self.leds.getpreview_rgb(self.leddata,(width,height)) previewimg = QImage(img, width, height, width*3, QImage.Format_RGB888) @@ -68,6 +87,50 @@ def updateperview(self): self.prescene.addItem(self.preitem) self.ui.preview_box.setScene(self.prescene) + def videofunc(self): + if self.kb.wait_for_kb(100)==False: + return + if self.videothreadid.is_alive(): + self.video_running=False + self.videothreadid.join() + self.videolink=self.ui.video_link_box.text() + self.cvvideo=cv2.VideoCapture() + self.cvvideo.open(self.videolink) + if(self.cvvideo.isOpened()): + self.leds.switchmode(0xff) + fps=self.cvvideo.get(cv2.CAP_PROP_FPS) + self.videotimer.start(int(np.floor(1000/fps))) + self.video_opened=True + self.video_running=True + self.videothreadid = threading.Thread(target=self.videothread) + self.videothreadid.start() + def videotimerfunc(self): + if(self.videolock.locked()): + self.videolock.release() + def videothread(self): + kbresetflag=False + while self.video_running and self.video_opened: + self.videolock.acquire() + if kbresetflag: + self.leds.switchmode(0xff) + ret, frame1 = self.cvvideo.read() + if(ret): + if self.kb.wait_for_kb(100) == False: + kbresetflag=True + continue + self.leddata=self.leds.play_frame_full(frame1) + else: + self.cvvideo.set(cv2.CAP_PROP_POS_FRAMES,0) + # kb.stop_and_wait() + # sys.exit(0) + def stopvideo(self): + if self.videothreadid.is_alive(): + self.video_running=False + self.videolock.release() + self.videothreadid.join() + self.cvvideo.release() + self.video_opened=False + self.videotimer.stop() if __name__ == '__main__': app = QApplication(sys.argv)