Skip to content

Commit 3de1ca1

Browse files
committed
linting and extra comments
1 parent 315a231 commit 3de1ca1

File tree

10 files changed

+77
-50
lines changed

10 files changed

+77
-50
lines changed

examples/tiny_fx/examples/director/effectpackage.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
from picofx.mono import StaticFX, BlinkFX, FlashFX, FlickerFX, PulseFX, RandomFX, NoneFX
33
from picofx.colour import StaticRGBFX, BlinkRGBFX, FlashRGBFX, FlickerRGBFX, PulseRGBFX, RandomRGBFX
44

5+
56
class EffectPackage:
67
# The EffectPackage class mainly keeps all the methods all together which adjust the effect.
78
# It holds a list of EffectSettings objects, one for each channel, and this should
89
# initialise to have one member for each output on the device.
9-
def __init__(self, board, colour, num_outputs):
10+
11+
def __init__(self, colour, num_outputs):
1012
# First we load in everything from the settings file.
1113
self.colour = colour
1214
loaded_settings = self.load_settings()
15+
1316
# Then we create a new entry for each output on the board,
1417
# using the imported settings if they're there and making a
1518
# fresh new EffectSettings with default values if not.
1619
self.channel_settings = []
17-
20+
1821
for i in range(num_outputs):
1922
if len(loaded_settings) > 0:
2023
new_effect = loaded_settings.pop(0)
@@ -24,6 +27,7 @@ def __init__(self, board, colour, num_outputs):
2427

2528
def load_settings(self):
2629
new_settings = []
30+
2731
# To load the settings we first open the file...
2832
with open("settings.txt", "r") as settingsfile:
2933
# ...then loop through reading each line in turn.
@@ -38,7 +42,7 @@ def load_settings(self):
3842
# If the line begins with "Mono" or "RGB" as specified in the colour
3943
# parameter, then we start a new internal loop to
4044
# populate values into a new instance of EffectSettings.
41-
45+
4246
if line.split(" ")[0] == self.colour:
4347
type = EffectSettings.default_values["type"]
4448
brightness = EffectSettings.default_values["brightness"]
@@ -278,12 +282,14 @@ def cycle_fx(self, channel, player):
278282
# To change effect within a provided channel, we just
279283
# pull the right EffectSettings from the list, get its current type
280284
# and add one, looping around to zero if necessary.
281-
# Then write that new effect type to the EffectSettings.
285+
# The while loop will skip over any that aren't marked as
286+
# valid effects for the channel type.
287+
# Then we write that new effect type to the EffectSettings.
282288
if self.colour == "Mono":
283289
valid_effects = (0, 1, 2, 3, 4, 5, 6)
284290
elif self.colour == "RGB":
285291
valid_effects = (0, 7, 8, 9, 10, 11, 12)
286-
292+
287293
effect = self.channel_settings[channel - 1]
288294
old_effect_type = effect.type
289295
new_effect_type = (old_effect_type + 1) % 13
@@ -485,17 +491,24 @@ def alter_chaos(self, channel, amount):
485491
effect.bright_max_time = new_bmax
486492
effect.dim_min_time = new_dmin
487493
effect.dim_max_time = new_dmax
488-
494+
489495
def alter_hue(self, channel, amount):
496+
# Changing hue is basically a little state machine. To go round the colour
497+
# wheel starting with red for example, we need to keep red at 100%
498+
# and slowly increase green. When it hits 100% we've got yellow, and then green
499+
# stays at 100% as red decreases. Once red is at zero we've got just green, then
500+
# we start increasing blue and so on and so on.
501+
490502
effect = self.channel_settings[channel - 1]
491503

492504
if effect.type in (0, 1, 2, 3, 4, 5, 6):
493505
return
494-
506+
495507
red = effect.red
496508
green = effect.green
497509
blue = effect.blue
498-
510+
511+
# These checks are what determine which colour value we should be increasing or decreasing.
499512
if red == 255 and not green == 255 and blue == 0:
500513
green += amount
501514
elif not red == 0 and green == 255 and blue == 0:
@@ -508,19 +521,23 @@ def alter_hue(self, channel, amount):
508521
red += amount
509522
elif red == 255 and green == 0 and not blue == 0:
510523
blue -= amount
511-
524+
512525
effect.red = self.clamp(red, 0, 255)
513526
effect.green = self.clamp(green, 0, 255)
514527
effect.blue = self.clamp(blue, 0, 255)
515-
528+
516529
print(effect.red, effect.green, effect.blue)
517-
530+
518531
def alter_hue2(self, channel, amount):
532+
# This is just for the dimmer hue on the FlickerRGB effect.
533+
# It works just the same as the hue above. If there's no second colour set,
534+
# it defaults to starting at the hue of the main colour.
535+
519536
effect = self.channel_settings[channel - 1]
520537

521538
if effect.type in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12):
522539
return
523-
540+
524541
if None in (effect.red2, effect.green2, effect.blue2):
525542
red = effect.red
526543
green = effect.green
@@ -529,7 +546,7 @@ def alter_hue2(self, channel, amount):
529546
red = effect.red2
530547
green = effect.green2
531548
blue = effect.blue2
532-
549+
533550
if red == 255 and not green == 255 and blue == 0:
534551
green += amount
535552
elif not red == 0 and green == 255 and blue == 0:
@@ -542,9 +559,9 @@ def alter_hue2(self, channel, amount):
542559
red += amount
543560
elif red == 255 and green == 0 and not blue == 0:
544561
blue -= amount
545-
562+
546563
effect.red2 = self.clamp(red, 0, 255)
547564
effect.green2 = self.clamp(green, 0, 255)
548565
effect.blue2 = self.clamp(blue, 0, 255)
549-
550-
print(effect.red2, effect.green2, effect.blue2)
566+
567+
print(effect.red2, effect.green2, effect.blue2)

examples/tiny_fx/examples/director/main.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
"""
1212

1313
# Variables
14-
tiny = TinyFX() # Create a new TinyFX object to interact with the board
15-
monoplayer = MonoPlayer(tiny.outputs) # Create a new effect player to control TinyFX's mono outputs
16-
rgbplayer = ColourPlayer(tiny.rgb) # Create a new effect player to control TinyFX's RGB output
17-
mono_effects = EffectPackage(tiny, "Mono", 6) # Create a new EffectPackage to take care of all the different effect settings for each mono channel.
18-
rgb_effects = EffectPackage(tiny, "RGB", 1) # Create a new EffectPackage to take care of all the different effect settings for the RGB channel.
14+
tiny = TinyFX() # Create a new TinyFX object to interact with the board
15+
monoplayer = MonoPlayer(tiny.outputs) # Create a new effect player to control TinyFX's mono outputs
16+
rgbplayer = ColourPlayer(tiny.rgb) # Create a new effect player to control TinyFX's RGB output
17+
mono_effects = EffectPackage("Mono", 6) # Create a new EffectPackage to take care of all the different effect settings for each mono channel.
18+
rgb_effects = EffectPackage("RGB", 1) # Create a new EffectPackage to take care of all the different effect settings for the RGB channel.
1919
mono_channel = 1
2020
run_mode = 0
2121

@@ -26,17 +26,19 @@ class Mode:
2626
RGB = 2
2727

2828

29-
def select_channel(dir, repeat):
30-
# Selecting a channel just involves incrementing or decrementing the selected_channel variable,
31-
# looping it round if it goes above the total number of channels or below zero.
32-
29+
def rotate(dir, repeat):
30+
# If the TinyFX is just running this button doesn't do anything.
3331
if run_mode == Mode.Running:
3432
return
35-
33+
34+
# However, if we're in RGB editing mode then we're altering the hue.
3635
if run_mode == Mode.RGB:
3736
rgb_effects.alter_hue(0, dir * 5)
3837
rgb_effects.update_effects_list(rgbplayer)
39-
38+
39+
# And if we're in mono editing mode this selects the channel.
40+
# Selecting a channel just involves incrementing or decrementing the selected_channel variable,
41+
# looping it round if it goes above the total number of channels or below zero.
4042
elif run_mode == Mode.Mono and not repeat:
4143
global mono_channel
4244

@@ -46,7 +48,7 @@ def select_channel(dir, repeat):
4648
if mono_channel < 1:
4749
mono_channel = len(mono_effects.channel_settings)
4850

49-
# Otherwise we just loop through each channel and undim it if it's the selected
51+
# Then we just loop through each channel and undim it if it's the selected
5052
# channel or dim it if it's not.
5153
for i in range(len(mono_effects.channel_settings)):
5254
effect = mono_effects.channel_settings[i]
@@ -55,7 +57,7 @@ def select_channel(dir, repeat):
5557
else:
5658
effect.dim()
5759

58-
# This is important when any changes are made. It takes the settings data from the
60+
# Calling this is important when any changes are made. It takes the settings data from the
5961
# channel settings and actually applies them to the player class.
6062
mono_effects.update_effects_list(monoplayer)
6163

@@ -64,11 +66,11 @@ def toggle_mono(channel=0):
6466
# This controls the quick mute for each channel. It goes straight into the
6567
# player and sets the brightness to zero, or to whatever's in the
6668
# settings for that channel if it's already at zero.
67-
69+
6870
if channel > 0:
6971
effect = monoplayer.effects[channel - 1]
7072
old_brightness = mono_effects.channel_settings[channel - 1].brightness
71-
73+
7274
else:
7375
effect = rgbplayer.effects[0]
7476
old_brightness = rgb_effects.channel_settings[channel - 1].brightness
@@ -82,22 +84,31 @@ def toggle_mono(channel=0):
8284
brightness = old_brightness
8385
effect.brightness = brightness
8486

87+
8588
def saving_handler():
8689
# Saving is simpler than loading, first we put in a bit of text directing the user to the instructions:
8790
text = "# SETTINGS FILE\n#\n# For instructions, check readme.txt\n#\n"
8891

92+
# Then add in each channel's settings.
8993
text += mono_effects.save_settings()
9094
text += rgb_effects.save_settings()
95+
9196
# Finally save the file.
9297
with open("settings.txt", "w") as settingsfile:
9398
settingsfile.write(text)
94-
99+
100+
95101
def mode_handler():
102+
# Here we're just switching between the running mode, mono editing mode and
103+
# RGB editing mode.
96104
global run_mode
97105

98106
run_mode += 1
99107
if run_mode > 2:
100108
run_mode = 0
109+
110+
# That's the actual switch done, the rest just dims or brightens LEDs
111+
# appropriately for the current mode.
101112
if run_mode == Mode.Running:
102113
for i in range(len(mono_effects.channel_settings)):
103114
mono_effects.channel_settings[i].undim()
@@ -118,6 +129,7 @@ def mode_handler():
118129
mono_effects.update_effects_list(monoplayer)
119130
rgb_effects.update_effects_list(rgbplayer)
120131

132+
121133
def common_handler(button):
122134
# This handles presses on the buttons common to all effect types.
123135
# It gets passed which button was pressed and depending on which it was,
@@ -130,12 +142,12 @@ def common_handler(button):
130142

131143
if run_mode == Mode.Running:
132144
return
133-
145+
134146
if run_mode == Mode.Mono:
135147
player = monoplayer
136148
effects = mono_effects
137149
selected_channel = mono_channel
138-
150+
139151
if run_mode == Mode.RGB:
140152
player = rgbplayer
141153
effects = rgb_effects
@@ -172,7 +184,7 @@ def number_handler(number_button, repeat):
172184
selected_channel = mono_channel
173185
effects = mono_effects
174186
effect = effects.channel_settings[selected_channel - 1]
175-
187+
176188
elif run_mode == Mode.RGB:
177189
player = rgbplayer
178190
selected_channel = 0
@@ -225,7 +237,7 @@ def number_handler(number_button, repeat):
225237
elif number_button == 9:
226238
if effect.type in (3, 9) and not repeat:
227239
effects.alter_flashes(selected_channel, 1)
228-
240+
229241
elif number_button == 0:
230242
if effect.type == 10:
231243
effects.alter_hue2(0, 5)
@@ -243,8 +255,8 @@ def number_handler(number_button, repeat):
243255
# This binds functions to each of the Pimoroni remote's buttons for what happens when they're pressed, and when they're held.
244256
# For the most part they're set to pass to a handler for the reasons mentioned above.
245257
remote = PimoroniRemote()
246-
remote.bind("CLOCKWISE", (select_channel, 1,False), on_repeat=(select_channel, 1,True))
247-
remote.bind("ANTICLOCK", (select_channel, -1, False), on_repeat=(select_channel, -1,True))
258+
remote.bind("CLOCKWISE", (rotate, 1, False), on_repeat=(rotate, 1, True))
259+
remote.bind("ANTICLOCK", (rotate, -1, False), on_repeat=(rotate, -1, True))
248260
remote.bind("MENU/ACTION", (mode_handler), on_repeat=None)
249261
remote.bind("1/RED", (number_handler, 1, False), on_repeat=(number_handler, 1, True))
250262
remote.bind("2/GREEN", (number_handler, 2, False), on_repeat=(number_handler, 2, True))

picofx/colour/blinkRGB.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from picofx import Cycling
66

7+
78
class BlinkRGBFX(Cycling):
89
def __init__(self, speed=1, phase=0.0, duty=0.5, brightness=1.0, r=255, g=0, b=0):
910
super().__init__(speed)
@@ -20,12 +21,9 @@ def __call__(self):
2021
r = self.red * self.brightness
2122
g = self.green * self.brightness
2223
b = self.blue * self.brightness
23-
24+
2425
return max(min(r, 255), 0), \
2526
max(min(g, 255), 0), \
2627
max(min(b, 255), 0)
2728
else:
2829
return 0, 0, 0
29-
30-
31-

picofx/colour/flashRGB.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def __call__(self):
3636
r = self.red * self.brightness
3737
g = self.green * self.brightness
3838
b = self.blue * self.brightness
39-
39+
4040
return max(min(r, 255), 0), \
4141
max(min(g, 255), 0), \
4242
max(min(b, 255), 0)
4343
else:
4444
return 0, 0, 0
45-
return 0, 0, 0
45+
return 0, 0, 0

picofx/colour/flickerRGB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __call__(self):
4242
r = self.red1 * self.brightness
4343
g = self.green1 * self.brightness
4444
b = self.blue1 * self.brightness
45-
45+
4646
return max(min(r, 255), 0), \
4747
max(min(g, 255), 0), \
4848
max(min(b, 255), 0)

picofx/colour/pulseRGB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __call__(self):
2121
r = self.red * self.brightness * ((math.sin(angle) + 1) / 2.0)
2222
g = self.green * self.brightness * ((math.sin(angle) + 1) / 2.0)
2323
b = self.blue * self.brightness * ((math.sin(angle) + 1) / 2.0)
24-
24+
2525
return max(min(r, 255), 0), \
2626
max(min(g, 255), 0), \
2727
max(min(b, 255), 0)

picofx/colour/randRGB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __call__(self):
2121
r = self.red * self.__brightness
2222
g = self.green * self.__brightness
2323
b = self.blue * self.__brightness
24-
24+
2525
return max(min(r, 255), 0), \
2626
max(min(g, 255), 0), \
2727
max(min(b, 255), 0)

picofx/colour/staticRGB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __call__(self):
1313
r = self.red * self.brightness
1414
g = self.green * self.brightness
1515
b = self.blue * self.brightness
16-
16+
1717
return max(min(r, 255), 0), \
1818
max(min(g, 255), 0), \
1919
max(min(b, 255), 0)

picofx/mono/blink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class BlinkFX(Cycling):
9-
def __init__(self, speed=1, phase=0.0, duty=0.5, brightness = 1.0):
9+
def __init__(self, speed=1, phase=0.0, duty=0.5, brightness=1.0):
1010
super().__init__(speed)
1111
self.phase = phase
1212
self.duty = duty

picofx/mono/none.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
class NoneFX:
66
def __call__(self):
7-
return 0.0
7+
return 0.0

0 commit comments

Comments
 (0)