-
Notifications
You must be signed in to change notification settings - Fork 0
/
launchpad.py
3516 lines (2916 loc) · 135 KB
/
launchpad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
# A Novation Launchpad control suite for Python.
#
# https://github.com/FMMT666/launchpad.py
#
# FMMT666(ASkr) 01/2013..09/2019..08/2020..05/2021
# www.askrprojects.net
#
#
#
# >>>
# >>> NOTICE FOR SPACE USERS:
# >>>
# >>> Yep, this one uses tabs. Tabs everywhere.
# >>> Deal with it :-)
# >>>
#
import string
import random
import sys
import array
from pygame import midi
from pygame import time
try:
from launchpad_py.charset import *
except ImportError:
try:
from charset import *
except ImportError:
sys.exit("error loading Launchpad charset")
##########################################################################################
### CLASS Midi
### Midi singleton wrapper
##########################################################################################
class Midi:
# instance created
instanceMidi = None
#---------------------------------------------------------------------------------------
#-- init
#-- Allow only one instance to be created
#---------------------------------------------------------------------------------------
def __init__( self ):
if Midi.instanceMidi is None:
try:
Midi.instanceMidi = Midi.__Midi()
except:
# TODO: maybe sth like sys.exit()?
print("unable to initialize MIDI")
Midi.instanceMidi = None
self.devIn = None
self.devOut = None
#---------------------------------------------------------------------------------------
#-- getattr
#-- Pass all unknown method calls to the inner Midi class __Midi()
#---------------------------------------------------------------------------------------
def __getattr__( self, name ):
return getattr( self.instanceMidi, name )
#-------------------------------------------------------------------------------------
#--
#-------------------------------------------------------------------------------------
def OpenOutput( self, midi_id ):
if self.devOut is None:
try:
# PyGame's default size of the buffer is 4096.
# Removed code to tune that...
self.devOut = midi.Output( midi_id, 0 )
except:
self.devOut = None
return False
return True
#-------------------------------------------------------------------------------------
#--
#-------------------------------------------------------------------------------------
def CloseOutput( self ):
if self.devOut is not None:
#self.devOut.close()
del self.devOut
self.devOut = None
#-------------------------------------------------------------------------------------
#--
#-------------------------------------------------------------------------------------
def OpenInput( self, midi_id, bufferSize = None ):
if self.devIn is None:
try:
# PyGame's default size of the buffer is 4096.
if bufferSize is None:
self.devIn = midi.Input( midi_id )
else:
# for experiments...
self.devIn = midi.Input( midi_id, bufferSize )
except:
self.devIn = None
return False
return True
#-------------------------------------------------------------------------------------
#--
#-------------------------------------------------------------------------------------
def CloseInput( self ):
if self.devIn is not None:
#self.devIn.close()
del self.devIn
self.devIn = None
#-------------------------------------------------------------------------------------
#--
#-------------------------------------------------------------------------------------
def ReadCheck( self ):
return self.devIn.poll()
#-------------------------------------------------------------------------------------
#--
#-------------------------------------------------------------------------------------
def ReadRaw( self ):
return self.devIn.read( 1 )
#-------------------------------------------------------------------------------------
#-- sends a single, short message
#-------------------------------------------------------------------------------------
def RawWrite( self, stat, dat1, dat2 ):
self.devOut.write_short( stat, dat1, dat2 )
#-------------------------------------------------------------------------------------
#-- Sends a list of messages. If timestamp is 0, it is ignored.
#-- Amount of <dat> bytes is arbitrary.
#-- [ [ [stat, <dat1>, <dat2>, <dat3>], timestamp ], [...], ... ]
#-- <datN> fields are optional
#-------------------------------------------------------------------------------------
def RawWriteMulti( self, lstMessages ):
self.devOut.write( lstMessages )
#-------------------------------------------------------------------------------------
#-- Sends a single system-exclusive message, given by list <lstMessage>
#-- The start (0xF0) and end bytes (0xF7) are added automatically.
#-- [ <dat1>, <dat2>, ..., <datN> ]
#-- Timestamp is not supported and will be sent as '0' (for now)
#-------------------------------------------------------------------------------------
def RawWriteSysEx( self, lstMessage, timeStamp = 0 ):
# There's a bug in PyGame's (Python 3) list-type message handling, so as a workaround,
# we'll use the string-type message instead...
#self.devOut.write_sys_ex( timeStamp, [0xf0] + lstMessage + [0xf7] ) # old Python 2
# array.tostring() deprecated in 3.9; quickfix ahead
try:
self.devOut.write_sys_ex( timeStamp, array.array('B', [0xf0] + lstMessage + [0xf7] ).tostring() )
except:
self.devOut.write_sys_ex( timeStamp, array.array('B', [0xf0] + lstMessage + [0xf7] ).tobytes() )
########################################################################################
### CLASS __Midi
### The rest of the Midi class, non Midi-device specific.
########################################################################################
class __Midi:
#-------------------------------------------------------------------------------------
#-- init
#-------------------------------------------------------------------------------------
def __init__( self ):
# exception handling moved up to Midi()
midi.init()
# but I can't remember why I put this one in here...
midi.get_count()
#-------------------------------------------------------------------------------------
#-- del
#-- This will never be executed, because no one knows, how many Launchpad instances
#-- exist(ed) until we start to count them...
#-------------------------------------------------------------------------------------
def __del__( self ):
#midi.quit()
pass
#-------------------------------------------------------------------------------------
#-- Returns a list of devices that matches the string 'name' and has in- or outputs.
#-------------------------------------------------------------------------------------
def SearchDevices( self, name, output = True, input = True, quiet = True ):
ret = []
for i in range( midi.get_count() ):
md = midi.get_device_info( i )
if name.lower() in str( md[1].lower() ):
if quiet == False:
print('%2d' % ( i ), md)
sys.stdout.flush()
if output == True and md[3] > 0:
ret.append( i )
if input == True and md[2] > 0:
ret.append( i )
return ret
#-------------------------------------------------------------------------------------
#-- Returns the first device that matches the string 'name'.
#-- NEW2015/02: added number argument to pick from several devices (if available)
#-------------------------------------------------------------------------------------
def SearchDevice( self, name, output = True, input = True, number = 0 ):
ret = self.SearchDevices( name, output, input )
if number < 0 or number >= len( ret ):
return None
return ret[number]
#-------------------------------------------------------------------------------------
#-- Return MIDI time
#-------------------------------------------------------------------------------------
def GetTime( self ):
return midi.time()
########################################################################################
### CLASS LaunchpadBase
###
########################################################################################
class LaunchpadBase( object ):
def __init__( self ):
self.midi = Midi() # midi interface instance (singleton)
self.idOut = None # midi id for output
self.idIn = None # midi id for input
# scroll directions
self.SCROLL_NONE = 0
self.SCROLL_LEFT = -1
self.SCROLL_RIGHT = 1
# LOL; That fixes a years old bug. Officially an idiot now :)
# def __delete__( self ):
def __del__( self ):
self.Close()
#-------------------------------------------------------------------------------------
#-- Opens one of the attached Launchpad MIDI devices.
#-------------------------------------------------------------------------------------
def Open( self, number = 0, name = "Launchpad" ):
self.idOut = self.midi.SearchDevice( name, True, False, number = number )
self.idIn = self.midi.SearchDevice( name, False, True, number = number )
if self.idOut is None or self.idIn is None:
return False
if self.midi.OpenOutput( self.idOut ) == False:
return False
return self.midi.OpenInput( self.idIn )
#-------------------------------------------------------------------------------------
#-- Checks if a device exists, but does not open it.
#-- Does not check whether a device is in use or other, strange things...
#-------------------------------------------------------------------------------------
def Check( self, number = 0, name = "Launchpad" ):
self.idOut = self.midi.SearchDevice( name, True, False, number = number )
self.idIn = self.midi.SearchDevice( name, False, True, number = number )
if self.idOut is None or self.idIn is None:
return False
return True
#-------------------------------------------------------------------------------------
#-- Closes this device
#-------------------------------------------------------------------------------------
def Close( self ):
self.midi.CloseInput()
self.midi.CloseOutput()
#-------------------------------------------------------------------------------------
#-- prints a list of all devices to the console (for debug)
#-------------------------------------------------------------------------------------
def ListAll( self, searchString = '' ):
self.midi.SearchDevices( searchString, True, True, False )
#-------------------------------------------------------------------------------------
#-- Clears the button buffer (The Launchpads remember everything...)
#-- Because of empty reads (timeouts), there's nothing more we can do here, but
#-- repeat the polls and wait a little...
#-------------------------------------------------------------------------------------
def ButtonFlush( self ):
doReads = 0
# wait for that amount of consecutive read fails to exit
while doReads < 3:
if self.midi.ReadCheck():
doReads = 0
self.midi.ReadRaw()
else:
doReads += 1
time.wait( 5 )
#-------------------------------------------------------------------------------------
#-- Returns a list of all MIDI events, empty list if nothing happened.
#-- Useful for debugging or checking new devices.
#-------------------------------------------------------------------------------------
def EventRaw( self ):
if self.midi.ReadCheck():
return self.midi.ReadRaw()
else:
return []
########################################################################################
### CLASS Launchpad
###
### For 2-color Launchpads with 8x8 matrix and 2x8 top/right rows
########################################################################################
class Launchpad( LaunchpadBase ):
# LED AND BUTTON NUMBERS IN RAW MODE (DEC):
#
# +---+---+---+---+---+---+---+---+
# |200|201|202|203|204|205|206|207| < AUTOMAP BUTTON CODES;
# +---+---+---+---+---+---+---+---+ Or use LedCtrlAutomap() for LEDs (alt. args)
#
# +---+---+---+---+---+---+---+---+ +---+
# | 0|...| | | | | | 7| | 8|
# +---+---+---+---+---+---+---+---+ +---+
# | 16|...| | | | | | 23| | 24|
# +---+---+---+---+---+---+---+---+ +---+
# | 32|...| | | | | | 39| | 40|
# +---+---+---+---+---+---+---+---+ +---+
# | 48|...| | | | | | 55| | 56|
# +---+---+---+---+---+---+---+---+ +---+
# | 64|...| | | | | | 71| | 72|
# +---+---+---+---+---+---+---+---+ +---+
# | 80|...| | | | | | 87| | 88|
# +---+---+---+---+---+---+---+---+ +---+
# | 96|...| | | | | |103| |104|
# +---+---+---+---+---+---+---+---+ +---+
# |112|...| | | | | |119| |120|
# +---+---+---+---+---+---+---+---+ +---+
#
#
# LED AND BUTTON NUMBERS IN XY MODE (X/Y)
#
# 0 1 2 3 4 5 6 7 8
# +---+---+---+---+---+---+---+---+
# | |1/0| | | | | | | 0
# +---+---+---+---+---+---+---+---+
#
# +---+---+---+---+---+---+---+---+ +---+
# |0/1| | | | | | | | | | 1
# +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | 2
# +---+---+---+---+---+---+---+---+ +---+
# | | | | | |5/3| | | | | 3
# +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | 4
# +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | 5
# +---+---+---+---+---+---+---+---+ +---+
# | | | | |4/6| | | | | | 6
# +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | 7
# +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | |8/8| 8
# +---+---+---+---+---+---+---+---+ +---+
#
#-------------------------------------------------------------------------------------
#-- reset the Launchpad
#-- Turns off all LEDs
#-------------------------------------------------------------------------------------
def Reset( self ):
self.midi.RawWrite( 176, 0, 0 )
#-------------------------------------------------------------------------------------
#-- Returns a Launchpad compatible "color code byte"
#-- NOTE: In here, number is 0..7 (left..right)
#-------------------------------------------------------------------------------------
def LedGetColor( self, red, green ):
led = 0
red = min( int(red), 3 ) # make int and limit to <=3
red = max( red, 0 ) # no negative numbers
green = min( int(green), 3 ) # make int and limit to <=3
green = max( green, 0 ) # no negative numbers
led |= red
led |= green << 4
return led
#-------------------------------------------------------------------------------------
#-- Controls a grid LED by its raw <number>; with <green/red> brightness: 0..3
#-- For LED numbers, see grid description on top of class.
#-------------------------------------------------------------------------------------
def LedCtrlRaw( self, number, red, green ):
if number > 199:
if number < 208:
# 200-207
self.LedCtrlAutomap( number - 200, red, green )
else:
if number < 0 or number > 120:
return
# 0-120
led = self.LedGetColor( red, green )
self.midi.RawWrite( 144, number, led )
#-------------------------------------------------------------------------------------
#-- Controls a grid LED by its coordinates <x> and <y> with <green/red> brightness 0..3
#-------------------------------------------------------------------------------------
def LedCtrlXY( self, x, y, red, green ):
if x < 0 or x > 8 or y < 0 or y > 8:
return
if y == 0:
self.LedCtrlAutomap( x, red, green )
else:
self.LedCtrlRaw( ( (y-1) << 4) | x, red, green )
#-------------------------------------------------------------------------------------
#-- Sends a list of consecutive, special color values to the Launchpad.
#-- Only requires (less than) half of the commands to update all buttons.
#-- [ LED1, LED2, LED3, ... LED80 ]
#-- First, the 8x8 matrix is updated, left to right, top to bottom.
#-- Afterwards, the algorithm continues with the rightmost buttons and the
#-- top "automap" buttons.
#-- LEDn color format: 00gg00rr <- 2 bits green, 2 bits red (0..3)
#-- Function LedGetColor() will do the coding for you...
#-- Notice that the amount of LEDs needs to be even.
#-- If an odd number of values is sent, the next, following LED is turned off!
#-- REFAC2015: Device specific.
#-------------------------------------------------------------------------------------
def LedCtrlRawRapid( self, allLeds ):
le = len( allLeds )
for i in range( 0, le, 2 ):
self.midi.RawWrite( 146, allLeds[i], allLeds[i+1] if i+1 < le else 0 )
# This fast version does not work, because the Launchpad gets confused
# by the timestamps...
#
# tmsg= []
# for i in range( 0, le, 2 ):
# # create a message
# msg = [ 146 ]
# msg.append( allLeds[i] )
# if i+1 < le:
# msg.append( allLeds[i+1] )
# # add it to the list
# tmsg.append( msg )
# # add a timestanp
# tmsg.append( self.midi.GetTime() + i*10 )
#
# self.midi.RawWriteMulti( [ tmsg ] )
#-------------------------------------------------------------------------------------
#-- "Homes" the next LedCtrlRawRapid() call, so it will start with the first LED again.
#-------------------------------------------------------------------------------------
def LedCtrlRawRapidHome( self ):
self.midi.RawWrite( 176, 1, 0 )
#-------------------------------------------------------------------------------------
#-- Controls an automap LED <number>; with <green/red> brightness: 0..3
#-- NOTE: In here, number is 0..7 (left..right)
#-------------------------------------------------------------------------------------
def LedCtrlAutomap( self, number, red, green ):
if number < 0 or number > 7:
return
red = max( 0, red )
red = min( 3, red )
green = max( 0, green )
green = min( 3, green )
led = self.LedGetColor( red, green )
self.midi.RawWrite( 176, 104 + number, led )
#-------------------------------------------------------------------------------------
#-- all LEDs on
#-- <colorcode> is here for backwards compatibility with the newer "Mk2" and "Pro"
#-- classes. If it's "0", all LEDs are turned off. In all other cases turned on,
#-- like the function name implies :-/
#-------------------------------------------------------------------------------------
def LedAllOn( self, colorcode = None ):
if colorcode == 0:
self.Reset()
else:
self.midi.RawWrite( 176, 0, 127 )
#-------------------------------------------------------------------------------------
#-- Sends character <char> in colors <red/green> and lateral offset <offsx> (-8..8)
#-- to the Launchpad. <offsy> does not have yet any function
#-------------------------------------------------------------------------------------
def LedCtrlChar( self, char, red, green, offsx = 0, offsy = 0 ):
char = ord( char )
if char < 0 or char > 255:
return
char *= 8
for i in range(0, 8*16, 16):
for j in range(8):
lednum = i + j + offsx
if lednum >= i and lednum < i + 8:
if CHARTAB[char] & 0x80 >> j:
self.LedCtrlRaw( lednum, red, green )
else:
self.LedCtrlRaw( lednum, 0, 0 )
char += 1
#-------------------------------------------------------------------------------------
#-- Scroll <text>, in colors specified by <red/green>, as fast as we can.
#-- <direction> specifies: -1 to left, 0 no scroll, 1 to right
#-- The delays were a dirty hack, but there's little to nothing one can do here.
#-- So that's how the <waitms> parameter came into play...
#-- NEW 12/2016: More than one char on display \o/
#-- IDEA: variable spacing for seamless scrolling, e.g.: "__/\_"
#-------------------------------------------------------------------------------------
def LedCtrlString( self, text, red, green, direction = None, waitms = 150 ):
limit = lambda n, mini, maxi: max(min(maxi, n), mini)
if direction == self.SCROLL_LEFT:
text += " "
for n in range( (len(text) + 1) * 8 ):
if n <= len(text)*8:
self.LedCtrlChar( text[ limit( ( n //16)*2 , 0, len(text)-1 ) ], red, green, 8- n %16 )
if n > 7:
self.LedCtrlChar( text[ limit( (((n-8)//16)*2) + 1, 0, len(text)-1 ) ], red, green, 8-(n-8)%16 )
time.wait(waitms)
elif direction == self.SCROLL_RIGHT:
# TODO: Just a quick hack (screen is erased before scrolling begins).
# Characters at odd positions from the right (1, 3, 5), with pixels at the left,
# e.g. 'C' will have artifacts at the left (pixel repeated).
text = " " + text + " " # just to avoid artifacts on full width characters
# for n in range( (len(text) + 1) * 8 - 1, 0, -1 ):
for n in range( (len(text) + 1) * 8 - 7, 0, -1 ):
if n <= len(text)*8:
self.LedCtrlChar( text[ limit( ( n //16)*2 , 0, len(text)-1 ) ], red, green, 8- n %16 )
if n > 7:
self.LedCtrlChar( text[ limit( (((n-8)//16)*2) + 1, 0, len(text)-1 ) ], red, green, 8-(n-8)%16 )
time.wait(waitms)
else:
for i in text:
for n in range(4): # pseudo repetitions to compensate the timing a bit
self.LedCtrlChar(i, red, green)
time.wait(waitms)
#-------------------------------------------------------------------------------------
#-- Returns True if a button event was received.
#-------------------------------------------------------------------------------------
def ButtonChanged( self ):
return self.midi.ReadCheck()
#-------------------------------------------------------------------------------------
#-- Returns the raw value of the last button change as a list:
#-- [ <button>, <True/False> ]
#-------------------------------------------------------------------------------------
def ButtonStateRaw( self ):
if self.midi.ReadCheck():
a = self.midi.ReadRaw()
return [ a[0][0][1] if a[0][0][0] == 144 else a[0][0][1] + 96, True if a[0][0][2] > 0 else False ]
else:
return []
#-------------------------------------------------------------------------------------
#-- Returns an x/y value of the last button change as a list:
#-- [ <x>, <y>, <True/False> ]
#-------------------------------------------------------------------------------------
def ButtonStateXY( self ):
if self.midi.ReadCheck():
a = self.midi.ReadRaw()
if a[0][0][0] == 144:
x = a[0][0][1] & 0x0f
y = ( a[0][0][1] & 0xf0 ) >> 4
return [ x, y+1, True if a[0][0][2] > 0 else False ]
elif a[0][0][0] == 176:
return [ a[0][0][1] - 104, 0, True if a[0][0][2] > 0 else False ]
return []
########################################################################################
### CLASS LaunchpadPro
###
### For 3-color "Pro" Launchpads with 8x8 matrix and 4x8 left/right/top/bottom rows
########################################################################################
class LaunchpadPro( LaunchpadBase ):
# LED AND BUTTON NUMBERS IN RAW MODE (DEC)
# WITH LAUNCHPAD IN "LIVE MODE" (PRESS SETUP, top-left GREEN).
#
# Notice that the fine manual doesn't know that mode.
# According to what's written there, the numbering used
# refers to the "PROGRAMMING MODE", which actually does
# not react to any of those notes (or numbers).
#
# +---+---+---+---+---+---+---+---+
# | 91| | | | | | | 98|
# +---+---+---+---+---+---+---+---+
#
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | 80| | 81| | | | | | | | | 89|
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | 70| | | | | | | | | | | 79|
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | 60| | | | | | | | 67| | | 69|
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | 50| | | | | | | | | | | 59|
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | 40| | | | | | | | | | | 49|
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | 30| | | | | | | | | | | 39|
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | 20| | | | 23| | | | | | | 29|
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | 10| | | | | | | | | | | 19|
# +---+ +---+---+---+---+---+---+---+---+ +---+
#
# +---+---+---+---+---+---+---+---+
# | 1| 2| | | | | | 8|
# +---+---+---+---+---+---+---+---+
#
#
# LED AND BUTTON NUMBERS IN XY CLASSIC MODE (X/Y)
#
# 9 0 1 2 3 4 5 6 7 8
# +---+---+---+---+---+---+---+---+
# |0/0| |2/0| | | | | | 0
# +---+---+---+---+---+---+---+---+
#
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | |0/1| | | | | | | | | | 1
# +---+ +---+---+---+---+---+---+---+---+ +---+
# |9/2| | | | | | | | | | | | 2
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | |5/3| | | | | 3
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | | | 4
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | | | 5
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | |4/6| | | | | | 6
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | | | 7
# +---+ +---+---+---+---+---+---+---+---+ +---+
# |9/8| | | | | | | | | | |8/8| 8
# +---+ +---+---+---+---+---+---+---+---+ +---+
#
# +---+---+---+---+---+---+---+---+
# | |1/9| | | | | | | 9
# +---+---+---+---+---+---+---+---+
#
#
# LED AND BUTTON NUMBERS IN XY PRO MODE (X/Y)
#
# 0 1 2 3 4 5 6 7 8 9
# +---+---+---+---+---+---+---+---+
# |1/0| |3/0| | | | | | 0
# +---+---+---+---+---+---+---+---+
#
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | |1/1| | | | | | | | | | 1
# +---+ +---+---+---+---+---+---+---+---+ +---+
# |0/2| | | | | | | | | | | | 2
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | |6/3| | | | | 3
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | | | 4
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | | | 5
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | |5/6| | | | | | 6
# +---+ +---+---+---+---+---+---+---+---+ +---+
# | | | | | | | | | | | | | 7
# +---+ +---+---+---+---+---+---+---+---+ +---+
# |0/8| | | | | | | | | | |9/8| 8
# +---+ +---+---+---+---+---+---+---+---+ +---+
#
# +---+---+---+---+---+---+---+---+
# | |2/9| | | | | | | 9
# +---+---+---+---+---+---+---+---+
#
COLORS = {'black':0, 'off':0, 'white':3, 'red':5, 'green':17 }
#-------------------------------------------------------------------------------------
#-- Opens one of the attached Launchpad MIDI devices.
#-- Uses search string "Pro", by default.
#-------------------------------------------------------------------------------------
# Overrides "LaunchpadBase" method
def Open( self, number = 0, name = "Pro" ):
retval = super( LaunchpadPro, self ).Open( number = number, name = name )
if retval == True:
# avoid sending this to an Mk2
if name.lower() == "pro":
self.LedSetMode( 0 )
return retval
#-------------------------------------------------------------------------------------
#-- Checks if a device exists, but does not open it.
#-- Does not check whether a device is in use or other, strange things...
#-- Uses search string "Launchpad Pro", by default.
#-------------------------------------------------------------------------------------
# Overrides "LaunchpadBase" method
def Check( self, number = 0, name = "Launchpad Pro" ):
return super( LaunchpadPro, self ).Check( number = number, name = name )
#-------------------------------------------------------------------------------------
#-- Sets the button layout (and codes) to the set, specified by <mode>.
#-- Valid options:
#-- 00 - Session, 01 - Drum Rack, 02 - Chromatic Note, 03 - User (Drum)
#-- 04 - Audio, 05 -Fader, 06 - Record Arm, 07 - Track Select, 08 - Mute
#-- 09 - Solo, 0A - Volume
#-- Until now, we'll need the "Session" (0x00) settings.
#-------------------------------------------------------------------------------------
# TODO: ASkr, Undocumented!
# TODO: return value
def LedSetLayout( self, mode ):
if mode < 0 or mode > 0x0d:
return
self.midi.RawWriteSysEx( [ 0, 32, 41, 2, 16, 34, mode ] )
time.wait(10)
#-------------------------------------------------------------------------------------
#-- Selects the Pro's mode.
#-- <mode> -> 0 -> "Ableton Live mode" (what we need)
#-- 1 -> "Standalone mode" (power up default)
#-------------------------------------------------------------------------------------
def LedSetMode( self, mode ):
if mode < 0 or mode > 1:
return
self.midi.RawWriteSysEx( [ 0, 32, 41, 2, 16, 33, mode ] )
time.wait(10)
#-------------------------------------------------------------------------------------
#-- Sets BPM for pulsing or flashing LEDs
#-- EXPERIMENTAL FAKE SHOW
#-- The Launchpad Pro (and Mk2) derive the LED's pulsing or flashing frequency from
#-- the repetive occurrence of MIDI beat clock messages (msg 248), 24 per beat.
#-- No timers/events here yet, so we fake it by sending the minimal amount of
#-- messages (25 for Pro, 26 for Mk2 (not kidding) => 28, probably safe value) once.
#-- The Pro and the Mk2 support 40..240 BPM, so the maximum time we block everything
#-- is, for 40 BPM:
#-- [ 1 / ( 40 BPM * 24 / 60s ) ] * 28 = 1.75s ; (acrually one less, 28-1)
#-- Due to the 1ms restriction, the BPMs get quite coarse towards the faster end:
#-- 250, 227, 208, 192, 178, 166, 156, 147, 138, 131...
#-------------------------------------------------------------------------------------
def LedCtrlBpm( self, bpm ):
bpm = min( int( bpm ), 240 ) # limit to upper 240
bpm = max( bpm, 40 ) # limit to lower 40
# basically int( 1000 / ( bpm * 24 / 60.0 ) ):
td = int( 2500 / bpm )
for _ in range( 28 ):
self.midi.RawWrite( 248, 0, 0 )
time.wait( td )
#-------------------------------------------------------------------------------------
#-- Returns an RGB colorcode by trying to find a color of a name given by string <name>.
#-- If nothing was found, Code 'black' (off) is returned.
#-------------------------------------------------------------------------------------
def LedGetColorByName( self, name ):
# should not be required
#if type( name ) is not str:
# return 0;
if name in LaunchpadPro.COLORS:
return LaunchpadPro.COLORS[name]
else:
return LaunchpadPro.COLORS['black']
#-------------------------------------------------------------------------------------
#-- Controls a grid LED by its position <number> and a color, specified by
#-- <red>, <green> and <blue> intensities, with can each be an integer between 0..63.
#-- If <blue> is omitted, this methos runs in "Classic" compatibility mode and the
#-- intensities, which were within 0..3 in that mode, are multiplied by 21 (0..63)
#-- to emulate the old brightness feeling :)
#-- Notice that each message requires 10 bytes to be sent. For a faster, but
#-- unfortunately "not-RGB" method, see "LedCtrlRawByCode()"
#-------------------------------------------------------------------------------------
def LedCtrlRaw( self, number, red, green, blue = None ):
if number < 0 or number > 99:
return
if blue is None:
blue = 0
red *= 21
green *= 21
limit = lambda n, mini, maxi: max(min(maxi, n), mini)
red = limit( red, 0, 63 )
green = limit( green, 0, 63 )
blue = limit( blue, 0, 63 )
self.midi.RawWriteSysEx( [ 0, 32, 41, 2, 16, 11, number, red, green, blue ] )
#-------------------------------------------------------------------------------------
#-- Controls a grid LED by its position <number> and a color code <colorcode>
#-- from the Launchpad's color palette.
#-- If <colorcode> is omitted, 'white' is used.
#-- This method should be ~3 times faster that the RGB version "LedCtrlRaw()", which
#-- uses 10 byte, system-exclusive MIDI messages.
#-------------------------------------------------------------------------------------
def LedCtrlRawByCode( self, number, colorcode = None ):
if number < 0 or number > 99:
return
# TODO: limit/check colorcode
if colorcode is None:
colorcode = LaunchpadPro.COLORS['white']
self.midi.RawWrite( 144, number, colorcode )
#-------------------------------------------------------------------------------------
#-- Same as LedCtrlRawByCode, but with a pulsing LED.
#-- Pulsing can be stoppped by another Note-On/Off or SysEx message.
#-------------------------------------------------------------------------------------
def LedCtrlPulseByCode( self, number, colorcode = None ):
if number < 0 or number > 99:
return
# TODO: limit/check colorcode
if colorcode is None:
colorcode = LaunchpadPro.COLORS['white']
# for Mk2: [ 0, 32, 41, 2, *24*, 40, *0*, number, colorcode ] (also an error in the docs)
self.midi.RawWriteSysEx( [ 0, 32, 41, 2, 16, 40, number, colorcode ] )
#-------------------------------------------------------------------------------------
#-- Same as LedCtrlPulseByCode, but with a dual color flashing LED.
#-- The first color is the one that is already enabled, the second one is the
#-- <colorcode> argument in this method.
#-- Flashing can be stoppped by another Note-On/Off or SysEx message.
#-------------------------------------------------------------------------------------
def LedCtrlFlashByCode( self, number, colorcode = None ):
if number < 0 or number > 99:
return
# TODO: limit/check colorcode
if colorcode is None:
colorcode = LaunchpadPro.COLORS['white']
# for Mk2: [ 0, 32, 41, 2, *24*, *35*, *0*, number, colorcode ] (also an error in the docs)
self.midi.RawWriteSysEx( [ 0, 32, 41, 2, 16, 35, number, colorcode ] )
#-------------------------------------------------------------------------------------
#-- Controls a grid LED by its coordinates <x>, <y> and <reg>, <green> and <blue>
#-- intensity values. By default, the old and compatible "Classic" mode is used
#-- (8x8 matrix left has x=0). If <mode> is set to "pro", x=0 will light up the round
#-- buttons on the left of the Launchpad Pro (not available on other models).
#-- This method internally uses "LedCtrlRaw()". Please also notice the comments
#-- in that one.
#-------------------------------------------------------------------------------------
def LedCtrlXY( self, x, y, red, green, blue = None, mode = "classic" ):
if x < 0 or x > 9 or y < 0 or y > 9:
return
# rotate matrix to the right, column 9 overflows from right to left, same row
if mode != "pro":
x = ( x + 1 ) % 10
# swap y
led = 90-(10*y) + x
self.LedCtrlRaw( led, red, green, blue )
#-------------------------------------------------------------------------------------
#-- Controls a grid LED by its coordinates <x>, <y> and its <colorcode>.
#-- By default, the old and compatible "Classic" mode is used (8x8 matrix left has x=0).
#-- If <mode> is set to "pro", x=0 will light up the round buttons on the left of the
#-- Launchpad Pro (not available on other models).
#-- About three times faster than the SysEx RGB method LedCtrlXY().
#-------------------------------------------------------------------------------------
def LedCtrlXYByCode( self, x, y, colorcode, mode = "classic" ):
if x < 0 or x > 9 or y < 0 or y > 9:
return
# rotate matrix to the right, column 9 overflows from right to left, same row
if mode != "pro":
x = ( x + 1 ) % 10
# swap y
led = 90-(10*y) + x
self.LedCtrlRawByCode( led, colorcode )
#-------------------------------------------------------------------------------------
#-- Pulses a grid LED by its coordinates <x>, <y> and its <colorcode>.
#-- By default, the old and compatible "Classic" mode is used (8x8 matrix left has x=0).
#-- If <mode> is set to "pro", x=0 will light up the round buttons on the left of the
#-- Launchpad Pro (not available on other models).
#-------------------------------------------------------------------------------------
def LedCtrlPulseXYByCode( self, x, y, colorcode, mode = "classic" ):
if x < 0 or x > 9 or y < 0 or y > 9:
return
# rotate matrix to the right, column 9 overflows from right to left, same row
if mode != "pro":
x = ( x + 1 ) % 10
# swap y
led = 90-(10*y) + x
self.LedCtrlPulseByCode( led, colorcode )
#-------------------------------------------------------------------------------------
#-- Flashes a grid LED by its coordinates <x>, <y> and its <colorcode>.
#-- By default, the old and compatible "Classic" mode is used (8x8 matrix left has x=0).
#-- If <mode> is set to "pro", x=0 will light up the round buttons on the left of the
#-- Launchpad Pro (not available on other models).
#-------------------------------------------------------------------------------------
def LedCtrlFlashXYByCode( self, x, y, colorcode, mode = "classic" ):
if x < 0 or x > 9 or y < 0 or y > 9:
return
# rotate matrix to the right, column 9 overflows from right to left, same row
if mode != "pro":
x = ( x + 1 ) % 10
# swap y
led = 90-(10*y) + x
self.LedCtrlFlashByCode( led, colorcode )