May 2019
Intermediate to advanced
542 pages
13h 37m
English
Now that our pins are configured for PWM, we need to create a method that will allow us to set the LED to a specific color by passing in red, green, and blue values. Most software RGB color implementations (including QColor) specify these values as 8-bit integers (0 to 255). Our PWM values, however, represent a duty cycle, which is expressed as a percentage (0 to 100).
Therefore, since we're going to need to convert numbers from the 0 to 255 range into the 0 to 100 range several times, let's start with a static method that will do such a conversion:
@staticmethod def convert(val): val = abs(val) val = val//2.55 val %= 101 return val
This method ensures that we'll get a valid duty cycle regardless of the input by using simple ...
Read now
Unlock full access