Skip to content

Commit

Permalink
add video playback and improved stability
Browse files Browse the repository at this point in the history
  • Loading branch information
ganhailin committed Sep 1, 2021
1 parent 8476e9b commit 44eec06
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
35 changes: 25 additions & 10 deletions software/hidtest/kbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion software/hidtest/kbled.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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):
Expand Down
69 changes: 66 additions & 3 deletions software/hidtest/maingui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from PyQt5.QtGui import QPixmap

import sys
import cv2
import numpy as np
import threading

from kbled import keyboard_led
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 44eec06

Please sign in to comment.