Browse Source

fix: movewin.activate() fix:midi re-open,blink

micha 1 year ago
parent
commit
eff894c23d
6 changed files with 139 additions and 100 deletions
  1. 59 56
      lib/simplemidi_wraper.py
  2. 7 1
      remote/apcmini.py
  3. 6 6
      tksdl/dmx.py
  4. 15 17
      tksdl/fix.py
  5. 45 19
      tksdl/midi.py
  6. 7 1
      tool/movewin.py

+ 59 - 56
lib/simplemidi_wraper.py

@@ -56,80 +56,76 @@ class simplemidi(object):
             self.__midi = os.open( self.__name, option)
                 
             self.__mode = "a"
-            
+            self.is_open = 1
         except OSError as e:
-            print("File", self.__name, "ERR: {0} ".format(e.args) )
-
-            try:
-                self.__mode = "rw"
-                option = os.O_RDONLY | os.O_NONBLOCK
-                self.__midi = os.open( self.__name, option)
-                print("DEVICE MODE:",self.__mode)
-            except OSError as e:
-                print("File", self.__name, "ERR: {0} ".format(e.args) )
-                input()
-                sys.exit()
+            print("__open File", self.__name, "ERR: {0} ".format(e.args) )
+            print(" sleep 1sec...")
+            time.sleep(1)
+            self.is_open = 0
                 
         print("DEVICE MODE:",self.__mode)
+
     def init(self):
         #placeholder pygame
         pass
+
     def get_device_info(self,nr):
         if nr == 1:
             return "simplemidi", self.__device
-        else:
+        return None
 
-            return None
     def write_delayed(self,data):
         #import thread        
         thread.start_new_thread(self._write_delayed,([data,0.01],)) #midi writeloop
         thread.start_new_thread(self._write_delayed,([data,0.1],)) #midi writeloop
         thread.start_new_thread(self._write_delayed,([data,1],)) #midi writeloop
+
     def _write_delayed(self,data):
         time.sleep(data[1])
         self.write(data[0])
                     
     def write(self,data):
-        self.__lock.acquire() 
-        # change midi file to write mode
-        if self.__mode == "rw":
-            os.close(self.__midi)
-            option = os.O_WRONLY | os.O_NONBLOCK
-            self.__midi = os.open( self.__name, option)
+        try:
+            self.__lock.acquire() 
+            # change midi file to write mode
+            if self.__mode == "rw":
+                os.close(self.__midi)
+                option = os.O_WRONLY | os.O_NONBLOCK
+                self.__midi = os.open( self.__name, option)
 
-        if len(data) == 3:
-            msg = ""
-            
-            try:
-                msg = chr(int(data[0])) + chr(int(data[1])) + chr(int(data[2]) ) 
-                #if data[0] != 191:
-                #    print(data#[msg])
-                os.write(self.__midi, bytes(msg,"utf-8")  )
-            except Exception as e:# SyntaxError:print("midi err",[msg,data ])
-                print("midi-single-write:", e, data)
-                #self.__close() #STOPPING MIDI ...
-                #self.__open()
-        elif len(data) > 3:
-            #print("multi sending---------------------------")
-            for i in data:
-                if len(i) == 3:
-                    msg = ""
-                    
-                    try:
-                        msg = chr(int(i[0])) + chr(int(i[1])) + chr(int(i[2])) 
-                        print([msg])
-                        os.write(self.__midi, msg  )
-                    except Exception as e:
-                        pass
-                        print("midi-multi-write:", e, data)
+            if len(data) == 3:
+                msg = ""
                 
-        
-        # change midi file to read mode
-        if self.__mode == "rw":
-            os.close(self.__midi)
-            option = os.O_RDONLY | os.O_NONBLOCK
-            self.__midi = os.open( self.__name, option)
-        self.__lock.release()
+                try:
+                    msg = chr(int(data[0])) + chr(int(data[1])) + chr(int(data[2]) ) 
+                    os.write(self.__midi, bytes(msg,"utf-8")  )
+                except Exception as e:# SyntaxError:print("midi err",[msg,data ])
+                    print("midi-single-write:", e, data)
+                    time.sleep(1)
+                    self.__open()
+
+            elif len(data) > 3:
+                #print("multi sending---------------------------")
+                for i in data:
+                    if len(i) == 3:
+                        msg = ""
+                        
+                        try:
+                            msg = chr(int(i[0])) + chr(int(i[1])) + chr(int(i[2])) 
+                            print([msg])
+                            os.write(self.__midi, msg  )
+                        except Exception as e:
+                            pass
+                            print("midi-multi-write:", e, data)
+                    
+            
+            # change midi file to read mode
+            if self.__mode == "rw":
+                os.close(self.__midi)
+                option = os.O_RDONLY | os.O_NONBLOCK
+                self.__midi = os.open( self.__name, option)
+        finally:
+            self.__lock.release()
         
     def read(self,count=3):
         self.__lock.acquire()
@@ -140,6 +136,7 @@ class simplemidi(object):
         
     def poll(self,sysex=0):
         self.__lock.acquire() 
+        ok = 0
         try: 
             if sysex:
                 self.__data = os.read(self.__midi, 1) #read abort if no data to read
@@ -153,10 +150,16 @@ class simplemidi(object):
                 except IndexError as e:
                     print("File", self.__name, "ERR: {0} ".format(e.args) ,[inp])
                         
-            self.__lock.release()
-            return 1
-        except OSError: 
+            ok = 1
+        except KeyboardInterrupt as e:
+            raise e
+        except: # OSError: 
             time.sleep(0.01) # CPU STRESSLESS
+        finally:
             self.__lock.release()
-            return 0
+
+        return ok
+
+
+
 

+ 7 - 1
remote/apcmini.py

@@ -60,6 +60,9 @@ class MAIN():
     def __init__(self):
         self.buf = []
         self.dbg = 0
+        self.blink   = -1
+        self.is_open = -1
+        self.midi = midi 
     def loop(self):
         release = 0
         if 0:
@@ -181,7 +184,7 @@ class MAIN():
 
             if time.time()>last_t+0.5:
                 last_t = time.time()
-                print("blink",blink)
+                #print("blink",blink)
                 if blink:
                     blink = 0
                     midi.write([144,82,YELLOW])
@@ -189,6 +192,9 @@ class MAIN():
                     blink = 1
                     midi.write([144,82,BLACK])
 
+                self.is_open = midi.is_open
+                self.blink = blink
+
 
 if __name__ == "__main__":
     import _thread as thread

+ 6 - 6
tksdl/dmx.py

@@ -43,17 +43,17 @@ import tool.sdl_elm as sdl_elm
 
 
 #CAPTION = 'LibreLight DMX '
-CAPTION += ':{}'.format(random.randint(100,999))
+#CAPTION += ':{}'.format(random.randint(100,999))
 
 import tool.git as git
 CAPTION += git.get_all()
 
 
-_id = movewin.winfo(CAPTION)
-c1 = movewin.movewin(_id,main_size[0],main_size[1]) #800,500)
-os.system(c1)
-c1 = movewin.activate(_id)
-os.system(c1)
+#_id = movewin.winfo(CAPTION)
+#c1 = movewin.movewin(_id,main_size[0],main_size[1]) #800,500)
+#os.system(c1)
+#c1 = movewin.activate(_id)
+#os.system(c1)
 
 pg.display.set_caption(CAPTION)
 

+ 15 - 17
tksdl/fix.py

@@ -13,20 +13,30 @@ sys.path.insert(0,"/opt/LibreLight/Xdesk/")
 print(sys.path)
 print()
 
+CAPTION = 'LibreLight FIXTURE-LIST '
 
-import pathlib
 
+sys.path.insert(0,"/opt/LibreLight/Xdesk/")
+import tool.movewin as movewin
+import tool.git as git
+
+#CAPTION += ':{}'.format(random.randint(100,999))
+CAPTION += git.get_all()
+
+import pathlib
 _file_path=pathlib.Path(__file__)
 print("file:",_file_path)
 
-import tool.movewin as movewin
+#_id = movewin.winfo(CAPTION)
+#c1 = movewin.movewin(_id,200,50)
+#os.system(c1)
+#c1 = movewin.activate(_id)
+#os.system(c1)
 
-CAPTION = 'LibreLight FIXTURE-LIST '
 movewin.check_is_started(CAPTION,_file_path)
 
 
 
-
 # ===== GUI =========
 import pygame
 import pygame.gfxdraw
@@ -45,21 +55,9 @@ pygame.display.set_icon(icon)
 import tool.movewin as movewin
 import tool.sdl_elm as sdl_elm
 
+pg.display.set_caption(CAPTION)
 
-#CAPTION = 'LibreLight DMX '
-CAPTION += ':{}'.format(random.randint(100,999))
-
-import tool.git as git
-CAPTION += git.get_all()
-
-
-_id = movewin.winfo(CAPTION)
-c1 = movewin.movewin(_id,main_size[0],main_size[1]) #800,500)
-os.system(c1)
-c1 = movewin.activate(_id)
-os.system(c1)
 
-pg.display.set_caption(CAPTION)
 
 font0  = pygame.font.SysFont("freesans",10)
 font0b = pygame.font.SysFont("freesansbold",10)

+ 45 - 19
tksdl/midi.py

@@ -8,19 +8,29 @@ import os
 import sys
 import json
 
+CAPTION = 'LibreLight SDL-MIDI '
+
 sys.path.insert(0,"/opt/LibreLight/Xdesk/")
-#print(sys.path)
-#print()
+import tool.movewin as movewin
+import tool.git as git
 
-import pathlib
+#CAPTION += ':{}'.format(random.randint(100,999))
+CAPTION += git.get_all()
 
+import pathlib
 _file_path=pathlib.Path(__file__)
 print("file:",_file_path)
+movewin.check_is_started(CAPTION,_file_path)
+
+#_id = movewin.winfo(CAPTION)
+#c1 = movewin.movewin(_id,200,50)
+#os.system(c1)
+#c1 = movewin.activate(_id)
+#os.system(c1)
+
+
 
-import tool.movewin as movewin
 
-CAPTION = 'LibreLight SDL-MIDI '
-movewin.check_is_started(CAPTION,_file_path,sleep=0)
 
 
 # ===== GUI =========
@@ -34,26 +44,23 @@ window = pygame.display.set_mode(main_size,pg.RESIZABLE,32)
 
 pg = pygame
 pygame.init()
-pygame.mixer.quit()
-clock = pygame.time.Clock()
+pg.display.set_caption(CAPTION)
 icon = pygame.image.load('icon/scribble.png')
 pygame.display.set_icon(icon)
 
+pygame.mixer.quit()
+clock = pygame.time.Clock()
+
 import tool.movewin as movewin
 import tool.sdl_elm as sdl_elm
 
-CAPTION += ':{}'.format(random.randint(100,999))
 
-import tool.git as git
-CAPTION += git.get_all()
+#_id = movewin.winfo(CAPTION)
+#c1 = movewin.movewin(_id,200,50)
+#os.system(c1)
+#c1 = movewin.activate(_id)
+#os.system(c1)
 
-_id = movewin.winfo(CAPTION)
-c1 = movewin.movewin(_id,200,50)
-os.system(c1)
-c1 = movewin.activate(_id)
-os.system(c1)
-
-pg.display.set_caption(CAPTION)
 
 import lib.zchat as chat
 cmd_client = chat.Client(port=30003)
@@ -230,6 +237,20 @@ while 1:
         window.blit(fr,(330,10+r ))
         r+=10
 
+    r = 2
+    try:
+        rgb = [10,10,10]
+        if apc_main.blink:
+            rgb = [110,110,110]
+
+        pygame.draw.rect(window,rgb,[200,r,60,25])
+        fr = font15.render("BLINK:"+str(apc_main.blink)  ,1, (200,200,200))
+        window.blit(fr,(200,r ))
+        r+=10
+        fr = font15.render("open:"+str(apc_main.is_open)  ,1, (200,200,200))
+        window.blit(fr,(200,r ))
+    except Exception as e:print(e)
+
     r = 10
     fr = font15.render("EXEC:"  ,1, (200,100,200))
     window.blit(fr,(10,10+r ))
@@ -288,6 +309,7 @@ while 1:
         print("event",event)
         if event.type == pygame.QUIT:
             pygame.quit()
+            #time.sleep(1)
             sys.exit(0)
         elif event.type == pygame.VIDEORESIZE:
             scrsize = event.size
@@ -334,5 +356,9 @@ while 1:
     if resize_changed:# = True
         screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
 
-    clock.tick(10)
+    try:
+        clock.tick(10)
+    except KeyboardInterrupt as e:
+        pygame.quit()
+        raise e
 

+ 7 - 1
tool/movewin.py

@@ -46,7 +46,11 @@ def search_process(_file_path,exact=1):
     count = 0
     out = []
     for pid in pids:
-        p = psutil.Process(pid)
+        try:
+            p = psutil.Process(pid)
+        except psutil.NoSuchProcess:
+            break
+
         ps = p.cmdline()
 
         if len(ps) < 2:
@@ -135,5 +139,7 @@ def check_is_started(CAPTION,_file_path,sleep=0):
         _ids = winfo(search)
         for _id in _ids:
             c3  = activate(_id)
+            print("check_is_started CMD:",c3)
             os.system(c3)
+        time.sleep(1)
         sys.exit()