Added more to the handle

This commit is contained in:
Maximilien Rigaut 2021-10-02 18:30:47 +02:00
parent 00af547169
commit d15d95d7c4
2 changed files with 190 additions and 46 deletions

View File

@ -1,13 +1,14 @@
###########################
# IMPORTS
###########################
# FUNCTION DEFINES
_VERBOSE=4
nopeF=lambda x:None
def printL(label=""):
def printl(label=""):
def _pl(*args):
print(label,*args)
return _pl
@ -29,83 +30,184 @@ if _VERBOSE>=4:
###########################
# CLASS DEFINES
class midiPageHandler(self):
class midiPageHandler(object):
"Page Handler, with some methods to manage a midi remote more easily"
def __init__(self,noteArray,control):
def __init__(self,table,pages=1):
"""The noteArray contains a list of virtual lines and the corresponding notes"""
self._modifier=[]
self._basevalues=[]
self._activemodifier=[]
self._activebasevalues=[]
self._changes=[]
self._listPossibleValues={"none":0x0}
self._posToNote=[]
self._noteToPos=[]
self._basevalues=[] # Store states given by the system or the machine
self._activebasevalues=[] # Store the values of the active page
self._changedbasevalues=[] # Store the active values, not yet updated
self._heightV=0
self._widthV=0
self._changes=set()
self._listPossibleValues={None:0b0}
self.__maxV=0b0
self._page=0
self._maxpages=pages
self._maxN=0
self._makeNotes(table)
####################
# Manage attributes
def _makeNotes(self,table):
"Make the note conversion tables and update everything"
self._heightV,self._widthV=len(table),len(table[0])
for i,j in self.allIndexes():
self._maxN=max(table[i][j],self._maxN)
self._noteToPos=[(None,None)]*(self._maxN+1)
self._posToNote=[[table[i][j] for i in range(self._widthV)] for j in range(self._heightV)]
for i,j in self.allIndexes():
self._noteToPos[table[i][j]]=(i,j)
def initValues(self):
"Init all tables with default values"
self._basevalues=[[[0 for i in range(self._widthV)] for j in range(self._heightV)] for k in range(self._maxpages)]
self._activebasevalues=self._basevalues[self._page]
self._changedbasevalues=[[0 for i in range(self._widthV)] for j in range(self._heightV)]
def _updateSize(self):
"Not the best way to add the table, but sometimes necessary"
self._heightV=len(self._activebasevalues)
if self._heightV:
self._widthV=len(self._activebasevalues[0])
else:
self._widthV=0
def addPossibleValue(self,value):
"Add a possible value in the dict"
if value in self._listPossibleValues:
wprint("Value {} is already in the dictionnaty of possible values")
return False
else:
self.__maxV*=2
self._listPossibleValues[value]=self.__maxV
return True
def __call__(self,page,linecol):
"Return the value of the table at the active linecol & page"
v1=getTableElt(self._basevalues[page],linecol)
v2=getTableElt(self._modifier[page],linecol)
if len(linecol)==1 or linecol[1]==None:
return [v1[i]|v2[i] for i in range(len(v1))]
return v1|v2
def newChangedValues(self):
"Copy the values of the active values into the changeValues"
for i,j in self.allIndexes():
self._changedbasevalues[i][j]=self._activebasevalues[i][j]
####################
# Page Changes
def changePage(self,page):
"Try to change page"
try:
self._activemodifier=self._modifier[page]
self._activebasevalues=self._basevalues[page]
self.fullRedraw(self.page,page)
self.newChangedValues()
self._redrawPageChange(self._page,page)
self._page=page
except KeyError as e:
eprint("Can't change active page to ({})".format(page))
eprint("Can't set active page to ({})".format(page))
print(e)
def fullRedraw(self):
def _redrawPageChange(self,page1,page2):
"Add each different value between the two pages to the list of redraw"
for i in range(self._modifier):
for j in range(self._modifier):
if self._activelines
def changeBaseValues(self,linecol,val):
"""Change the base values and add a notice
Usually one of the base states, not a modifier"""
def changeModifiers(self,linecol,val):
"Add a modifier to a position, add a notice if changed"
if page1==page2: # Do nothing
return
for i,j in self.allIndexes():
if self._changedbasevalues[i][j]!=self._activebasevalues[i][j]:
self.addChange((i,j))
####################
# Manage attributes
def fullRedraw(self,redrawAll=False):
"Redraw every changed value"
if redrawAll: # Trigger a full redraw of everything
pass
####################
# Value Access methods
def addChange(self,linecol):
"Add a change to be acted"
self._changes.add(linecol)
def listChanges(self):
"Yield the list of position to be updated"
for change in self._changes:
yield change
def applyChanges(self):
"Apply all changes and erase them, then reload another _changedbasevalues"
pass
#####################
# Value modification methods
def addModifier(self,linecol,modifier):
"Add the modifier to the list of pending changes"
pass
######################
# Table utilities
def getTableElt(self,table,linecol):
"Get an element from a table, either _line or _basevalues"
if len(linecol)==1: # Return a specific line
return table[linecol[0]]
if linecol[0]==None: # Return a single column, pretty awkward
return [table[i][linecol[1]] for i in range(len(table))]
if linecol[1]==None: # Return a single line
return table[linecol[0]]
return table[linecol[0],linecol[1]]
def setTableElt(self,table,linecol,value):
"Set an element in a table, DOEN'T NOTIFIES"
def getTableLine(self,table,line):
"Get a line from a table"
return table[i]
def __getelement__(self,linecol):
"Obtain the value associated with a position"
def getTableColumn(self,table,col):
"Get a column from a table"
return [table[i][col] for i in range(len(table))]
def setTableElt(self,table,linecol,val):
"Set an element in a table (No notifications)"
table[linecol[0]][linecol[1]]=val
def setTableLine(self,table,line,value):
"Change a line in a table (No notifications) (no copy())"
table[line]=value
def setTableCol(self,table,col,values):
"Change a column to a specific list of values (No notifications)"
for i in range(len(values)):
table[i][col]=values[i]
def __setelement__(self,linecol,setas):
"Element"
if len(linecol)==1 or linecol[1]==None: #Replace an entire line
self._lines[linecol[0]]=setas
self._lastchange=[]
####################
# Operator definitions
####################
# I/O Utilities
def allIndexes(self):
"Yield all (x,y) tuples of positions for the active table"
for i in range(self._heightV):
for j in range(self._widthV):
yield (i,j)
def __repr__(self):
"Give a small representation of the handler"
return 'midiPageHandler@{} ({})({}x{})'.format(id(self),self._page,self._heightV,self._widthV)
def prettyPrint(self,table=None):
"Give a nice print"
lineM='[{:3}] '*self._widthV
sep = "*"+'------'*(self._widthV)+"*"
if table==None:
table=self._basevalues
print(sep)
print(' '+self.__repr__())
for page in self._basevalues:
print(sep)
for line in page:
print(" "+lineM.format(*line))
print(sep)
class AkaiAPCMini(midiPageHandler):
"A specific implementation of the page handler"
def __init__(self):
super(AkaiAPCMini,self).__init__([[i+(7-j)*8 for i in range(8)] for j in range(8)])
# Add the two control lines
self.initValues()
#self.changePage(0)
self._updateSize()
self.changePage(0)
if __name__ == '__main__':
ak=AkaiAPCMini()
ak.prettyPrint()

View File

@ -0,0 +1,42 @@
from mapping.base import BasicMessageParse,BasicActions,MatchError,BasicMidiInterface
import mido
from mapping.midi import MessageParse
from mapping.midi import Actions
_COLORS={"black":0,"green":1,"blinking_green":2,"red":3,"blinking_red":4,"yellow":5,"blinking_yellow":6}
#################################################
# GLOBAL CONTROL
###
#import midiPageHandler
#create a var thingy
def _onload(self):
"Send a reset colors"
import time,sys
#vars.output=self.outputs[vars.interface_nb]
def pagepress(trigger,val,note,*params):
"A key was pressed on the pagebuttons area"
#i,j=noteToPos(note)
try:
pass
#_ACTIONS[vars.page][i+8*j](trigger,i,j,val,*params)
# dprint("[Info] : Action received ({}:{}) - Note {:2} ({:3})".format(i,j,note,val))
except TypeError as e:
eprint("[Error] : Unassigned pagebutton ({}:{}) - Note {:2} ({:3})".format(i,j,note,val))
eprint(e)
####################################################
# INTERFACE DEFINITION
#####
class MidiInterface(BasicMidiInterface):
"Adding a couple of custom functions"
def __init__(self,configfile):
super(MidiInterface,self).__init__(configfile)
_onload(self)
self.functionlist["pagepress"]=pagepress