PyDMXControl.effects.Intensity._Dim.Dim.callback()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 38
rs 7.9279
c 0
b 0
f 0
cc 7
nop 1
1
"""
2
 *  PyDMXControl: A Python 3 module to control DMX using OpenDMX or uDMX.
3
 *                Featuring fixture profiles, built-in effects and a web control panel.
4
 *  <https://github.com/MattIPv4/PyDMXControl/>
5
 *  Copyright (C) 2021 Matt Cowley (MattIPv4) ([email protected])
6
"""
7
8
from ..defaults import Effect
9
10
11
class Dim(Effect):
12
13
    def __init__(self, *args, **kwargs):
14
        super().__init__(*args, **kwargs)
15
16
        # Start of loop
17
        self.__start = None
18
19
    def callback(self):
20
        # New
21
        if self.__start is None:
22
            self.__start = self.fixture.controller.ticker.millis_now()
23
24
        offset = self.speed * self.offset  # Calculate offset duration
25
        delay = self.speed * self.delay  # Calculate delay period after effect
26
        total = self.speed + delay  # Calculate total loop time (speed + delay)
27
28
        start = self.__start + offset  # Account for initial offset
29
        since_start = self.fixture.controller.ticker.millis_now() - start  # Calculate time since effect started
30
        since_last = since_start % total  # Calculate time since this loop started
31
32
        # If in delay period
33
        if since_last > self.speed:
34
            self.fixture.set_channel('dimmer', 0)
35
            return
36
37
        # Get progress through this loop (excl delay)
38
        progress = since_last / self.speed
39
40
        # Ensure in range 0 <= p <= 1
41
        while progress > 1:
42
            progress = progress - 1
43
        while progress < 0:
44
            progress = 1 + progress
45
46
        # Flip half way through
47
        progress *= 2
48
        if progress > 1:
49
            progress = 1 - (progress - 1)
50
51
        # Ensure we reach 100%
52
        if progress >= 0.95:
53
            progress = 1
54
55
        # Apply dimmer
56
        self.fixture.set_channel('dimmer', int(255 * progress))
57
58
    def start(self):
59
        self.__start = None
60
        super().start()
61