8
8
#
9
9
10
10
import StreamDeck .StreamDeck as StreamDeck
11
+ from StreamDeck .StreamDeckFilter import CallBackFilter , TempoFilter , StateChangedFilter , DebounceFilter
11
12
import threading
12
13
from PIL import Image , ImageDraw , ImageFont
14
+ import json
13
15
14
16
15
17
# Custom tiles with run-time generated text and custom image via the PIL module
@@ -41,7 +43,7 @@ def get_key_image(deck, key, state):
41
43
42
44
# Recombine the B, G and R elements in the order the display expects them,
43
45
# and convert the resulting image to a sequence of bytes
44
- rgb = { "R" : r , "G" : g , "B" : b }
46
+ rgb = {"R" : r , "G" : g , "B" : b }
45
47
return Image .merge ("RGB" , (rgb [order [0 ]], rgb [order [1 ]], rgb [order [2 ]])).tobytes ()
46
48
47
49
@@ -55,6 +57,24 @@ def key_change_callback(deck, key, state):
55
57
deck .close ()
56
58
57
59
60
+ class MyEncoder (json .JSONEncoder ):
61
+ def default (self , obj ):
62
+ if isinstance (obj , CallBackFilter ):
63
+ return obj .json_serialize ()
64
+ else :
65
+ super ().default (obj )
66
+
67
+
68
+ def json_decode (x ):
69
+ if isinstance (x , dict ):
70
+ key , value = next (iter (x .items ()))
71
+ if key in ['CallBackFilter' , 'TempoFilter' , 'DebounceFilter' , 'StateChangedFilter' ]:
72
+ cbf_class = globals ()[key ]
73
+ return cbf_class .json_deserialize (value )
74
+ return x
75
+
76
+
77
+
58
78
if __name__ == "__main__" :
59
79
manager = StreamDeck .DeviceManager ()
60
80
decks = manager .enumerate ()
@@ -69,8 +89,25 @@ def key_change_callback(deck, key, state):
69
89
70
90
deck .set_brightness (30 )
71
91
72
- for key in range (deck .key_count ()):
73
- deck .set_key_image (key , get_key_image (deck , key , False ))
92
+ for key in range (deck .key_count ()):
93
+ if key % 5 == 0 : # column 5
94
+ deck .filter [key ] = TempoFilter (default_state = True , tempo_delay = 0.5 )
95
+ elif key % 5 == 1 : # column 4
96
+ deck .filter [key ] = TempoFilter (default_state = False )
97
+ elif key % 5 == 2 : # column 3
98
+ deck .filter [key ] = DebounceFilter (default_state = True , key_delay = 2 )
99
+ elif key % 5 == 3 : # column 2
100
+ deck .filter [key ] = DebounceFilter (default_state = False )
101
+
102
+ deck .set_key_image (key , get_key_image (deck , key , deck .last_key_states [key ]))
103
+
104
+ # serialize, deserialize a simple button class
105
+ Button = {'name' : "somebutton" , 'cbfilter' :TempoFilter ()}
106
+ print (Button )
107
+ s = json .dumps (Button , cls = MyEncoder )
108
+ print (s )
109
+ Button = json .loads (s , object_hook = json_decode )
110
+ print (Button )
74
111
75
112
deck .set_key_callback (key_change_callback )
76
113
0 commit comments