From 90e4c5a6d925c52b28a797cbcb33dc431431b477 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Mon, 20 Sep 2021 12:54:39 +0200 Subject: [PATCH] working on testing units --- README.md | 1 + mapping/GrandMA2.py | 12 +++++++- mapping/base.py | 27 +++++++++++++---- mapping/keystrokes.py | 69 ++++++++++++++++++++++++++++++++++++++++++- midiRebind.py | 3 ++ patch/test.ini | 4 +++ test.py | 22 ++++++++++++-- 7 files changed, 129 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 564172b..0a166c7 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,4 @@ To do so add a custom patch (or use a currently defined one, but chances are pat * Better note / cc / pc notation * Add Fade (in ms) from one value to another * Have a way to know last value for the note/cc (the last one sent/received) +* Make toggles a function modifying a variable/state and sending result diff --git a/mapping/GrandMA2.py b/mapping/GrandMA2.py index 2be292c..fece616 100644 --- a/mapping/GrandMA2.py +++ b/mapping/GrandMA2.py @@ -5,6 +5,9 @@ import re,configparser commandlist={1:"go",2:"stop",3:"resume",4:"timed_go",6:"set",7:"fire",10:"go_off"} # Doc says 11 is go_off, test says otherwise +def testfunction(val,**params): + print(val,params) + #A MessageParse object MUST be included in the file, the rest is implementation Specific class MessageParse(BasicMessageParse): """Parsing of a gma SyEx message this parser is used once and will return a different Action object every time it is called""" @@ -58,6 +61,7 @@ class Actions(BasicActions): """The list of action to perform when a message is read""" def __init__(self, interface): super(Actions, self).__init__(interface) + self.addFunction("testfunction",testfunction) # This funtion is MANDATORY, it makes the link between MessageParse and Action def __call__(self,match): @@ -92,6 +96,12 @@ class Actions(BasicActions): # No action linked to the trigger pass + # Same as above, import BasicMidiInterface as MidiInterface could be used instead # MidiInterface(config) -MidiInterface=BasicMidiInterface +class MidiInterface(BasicMidiInterface): + "Adding a couple of custom functions" + + def __init__(self,configfile): + super(MidiInterface,self).__init__(configfile) + self.functionlist["test"]=testfunction diff --git a/mapping/base.py b/mapping/base.py index 16edabf..63c2f5a 100644 --- a/mapping/base.py +++ b/mapping/base.py @@ -22,6 +22,8 @@ class BasicMidiInterface(object): self.input="" self.outputs={} + self.functionlist={} + self.varlist={} #self.outchannels #not as of now, maybe later outputmatch=re.compile("output(?P\d+)") for elt in self.config["interface"]: @@ -87,6 +89,8 @@ class BasicActions(object): self.config=midiInterface.config self.interface=midiInterface self.sections={} + # self.varlist={} + # self.functionlist={} # Very cheesy way to figure out notes from control, I should find a better way, but I can't be bothered to self.dec10=[str(x) for x in range(10)] @@ -98,6 +102,10 @@ class BasicActions(object): self.populateSections() #self.prettyprint() + # def addFunction(self,fname,function): + # "Add a function to the list of linked functions" + # self.functionlist[fname]=function + def populateSections(self): "Create all the call setups" for elt in self.config.sections(): @@ -178,7 +186,7 @@ class BasicActions(object): try: interF=self.interface.interfaceOut(int(interfaceout)) noteN=self.findNote(key,startkey,stopkey,note) - return [BasicMidiTrigger(interF,midiaction,noteN,intensity,binded)] + return [BasicMidiTrigger(interF,self,midiaction,noteN,intensity,binded)] except (KeyError): print("[WARNING] Can't find interface {} for note {} action {}, legal interfaces are {}".format(interfaceout,note,action,[i for i in self.interface.outputs.keys()])) return [] #This choice is not necessary the best, but I figure it's easier that forcing the config to be perfect @@ -227,6 +235,7 @@ class BasicActions(object): for elt in self.sections[sect][subsect]: print(" {} : {}".format(elt,self.sections[sect][subsect][elt])) + FTrue=lambda x: True RecognisedMessagesTypes={ "note":["note_on","note","velocity"], @@ -241,7 +250,7 @@ class MatchError(Exception): class BasicMidiTrigger(object): """A midi trigger, will do a specific action when called""" - def __init__(self,interface,messagetype,value,intensity,binded): + def __init__(self,interface,parent,messagetype,value,intensity,binded): #self.vprint=vprint self.output=interface self.messagetype=messagetype @@ -250,6 +259,8 @@ class BasicMidiTrigger(object): self.interface=interface self.valuefn=None self.binded=binded + self.parent=parent + message=RecognisedMessagesTypes[messagetype] attributes={} self.toggle=None @@ -316,9 +327,15 @@ class BasicMidiTrigger(object): def addspecfn(self,val): "Add a funtion (defined in the mapping) with eventual arguments specified in the mapping" try: - self.funct=mapping.__get_attribute__(val) - except: - print("[ERROR] Can't evaluate function {}".function(fn)) + # Lidl parsing incomming; + t_parse=val.split("(") + funct,params=t_parse[0],t_parse[1][:-1].split(',') + print(funct,params,self.interface.functionlist) + self.funct=self.interface.functionlist[funct](val,**params) + except KeyError: + print("[ERROR] Key {} not in list of functions".format(funct)) + # except: + # print("[ERROR] Can't evaluate function {}".format(val)) def addstate(self,valtrue,valfalse): "The message will now send two state values intead" diff --git a/mapping/keystrokes.py b/mapping/keystrokes.py index 408213e..ec4ce1e 100644 --- a/mapping/keystrokes.py +++ b/mapping/keystrokes.py @@ -1,4 +1,71 @@ #!/bin/python3 -import pyautogui +# import pyautogui +from mapping.base import BasicMessageParse,BasicActions,MatchError,BasicMidiInterface +import mido +import re,configparser + + +#A MessageParse object MUST be included in the file, the rest is implementation Specific +class MessageParse(BasicMessageParse): + """Parsing of a midi message this parser is used once and will return a different Action object every time it is called""" + + def __init__(self): + #The data is converted from hex to decimal for readability and usability by mido + self.type=None #note_off note_on control_change program_change sysex (technically all the others but we don't care much for it) + self.channel=0 #0 by default, but can be anything + self.note=None #0 - 127 (it will be the program in program_change etc...) + self.value=None #The actual value, sometimes None + + def __call__(self,message): + "Parse the message and return either a match or raise an error" + self.type=message.type + try: + if message.type=="note_off" or message.type=="note_on": + self.channel=message.channel + self.note=message.note + self.value=message.velocity + elif message.type=="control_change": + self.channel=message.channel + self.note=message.control + self.value=message.value + elif message.type=="program_change": + self.channel=message.channel + self.note=message.program + self.value=None + else: + raise MatchError("Message is not either note_off, note_on, control_change or program_change") + return self + except: + raise MatchError("Can't parse the data {}".format(message)) + + def __str__(self): + return "[ty:{}/note:{}/value:{}]@{}".format(self.type,self.note,self.value,self.channel) +# An Action object MUST be included in the file. +class Actions(BasicActions): + """The list of action to perform when a message is read""" + def __init__(self, interface): + super(Actions, self).__init__(interface) + self.listfunctions["presskey"]=presskey + # self.listfunctions[""]= + + # This funtion is MANDATORY, it makes the link between MessageParse and Action + def __call__(self,match): + "Make the link between the regex and the action" + id,value,page="1","1","*" + try: + listact=self.findAction(match.type,match.note,"*") + for act in listact: + act(match.value) + except: + # No action linked to the trigger + pass + + +def presskey(val,**params): + "Press the given keys" + print("Pressing key {},params {}".format(val,params)) + +# MidiInterface(config) +MidiInterface=BasicMidiInterface diff --git a/midiRebind.py b/midiRebind.py index 7861006..c16c919 100755 --- a/midiRebind.py +++ b/midiRebind.py @@ -51,3 +51,6 @@ if __name__ == '__main__': sys.exit() except OSError: print('[Error] The patch "patch/{}.ini" is ill-formed or non-existent'.format(args.patch)) + except TypeError: + print("[Error] Can't iterate ofver an emplty list, check the list of inputs for an available input") + # except KeyError diff --git a/patch/test.ini b/patch/test.ini index 7b6e6fb..30cb836 100644 --- a/patch/test.ini +++ b/patch/test.ini @@ -41,3 +41,7 @@ 38/toggle = 0/127 [fire/5] 15 = 1/cc/2;2/note/19/127 + +[custom] + 77 = 1/cc/0 + 77/funct = testfunction(123,zz) diff --git a/test.py b/test.py index 9f89695..cb3c51c 100644 --- a/test.py +++ b/test.py @@ -145,10 +145,28 @@ class mappingTest(minitest.simpleTestUnit): break except: self.addFailure("Toggle for key 38 isn't assigned") + st=False + if st: + self.addSuccess() + + self.currentTest("Testing loading of functions") + print(self.gm.functionlist) + self.addSuccess() + + self.currentTest("Testing custom function") + try: + st=True + acts=self.act.findAction("custom",77,"2") + for act in acts: + if not act.function: + st=False + self.addFailure("No custom function defined") + break + except: + self.addFailure("Can't use custom function") + st=False if st: self.addSuccess() - else: - self.addFailure() self.currentTest("Testing call") acts=self.act.findAction("fire",38,"1")