Skip to content

Commit 29a275c

Browse files
authored
simple example how to use StreamdeckFilter
1 parent 9773da8 commit 29a275c

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

src/example.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#
99

1010
import StreamDeck.StreamDeck as StreamDeck
11+
from StreamDeck.StreamDeckFilter import CallBackFilter, TempoFilter, StateChangedFilter, DebounceFilter
1112
import threading
1213
from PIL import Image, ImageDraw, ImageFont
14+
import json
1315

1416

1517
# 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):
4143

4244
# Recombine the B, G and R elements in the order the display expects them,
4345
# 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}
4547
return Image.merge("RGB", (rgb[order[0]], rgb[order[1]], rgb[order[2]])).tobytes()
4648

4749

@@ -55,6 +57,24 @@ def key_change_callback(deck, key, state):
5557
deck.close()
5658

5759

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+
5878
if __name__ == "__main__":
5979
manager = StreamDeck.DeviceManager()
6080
decks = manager.enumerate()
@@ -69,8 +89,25 @@ def key_change_callback(deck, key, state):
6989

7090
deck.set_brightness(30)
7191

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)
74111

75112
deck.set_key_callback(key_change_callback)
76113

0 commit comments

Comments
 (0)