Page 1 of 1
class DimLed ?
Posted: Wed Jan 24, 2018 5:18 pm
by Oberoid
I would like to create a class for a dimmed led.
creating a Led class that inherits from Pin works fine.
class Led(Pin):
def __init__(self, pin):
Pin(pin, Pin.OUT)
But know i would like to create a class DimLed.
I tried the following code.
class DimLed(PWM):
def__init_(self,pin):
PWM(Pin(pin))
That did not work out. I do not know how I should approach this problem.
- DimLed is a PWM
- DimLed has a PWM
or Led.dim(50) dimming is a function of Led.
Any idea's? References to some theory about this subject are also welcome. Grts, F.P.
(btw how can I set BBCode on, for my posts?)
Re: class DimLed ?
Posted: Thu Jan 25, 2018 2:56 pm
by Oberoid
The Signal class is probably an example how to handle this.
https://docs.micropython.org/en/latest/ ... ignal.html
But is written in c++
Re: class DimLed ?
Posted: Thu Jan 25, 2018 7:37 pm
by efahl
You're doing inheritance a bit wrong, should look like this completely untested code:
class DimLed(PWM):
def __init__(self, pin):
super().__init__(Pin(pin))
self.freq(1000) # Or whatever.
def dim(self, factor):
self.duty(some_function_of(factor))
(BBCode is enabled for you when you get sufficient posts. I'm in the same boat. On the bright side, we can use smilies.

)
Re: class DimLed ?
Posted: Fri Jan 26, 2018 7:34 am
by pythoncoder
Do you mean something like this? This is based on LED's with cathodes connected to 0V via a resistor.
Code: Select all
from machine import Pin, PWM
class DimLED():
def __init__(self, pin):
self.pwm = PWM(pin)
self.pwm.freq(100)
self.brightness(0)
def brightness(self, value):
value = max(0, min(100, value))
duty = int(10.23 * value)
self.pwm.duty(duty)
This can be tested with pin 2 but note that the blue LED is wired with anode connected to 3.3V and cathode connected to pin 2 (via a resistor). This means that a value of 0 turns the LED on. So bigger numbers produce a dimmer LED:
brightness(90) produces a dim glow.
Code: Select all
>>> p = Pin(2, Pin.OUT)
>>> p(1)
>>> p(0)
>>> d = DimLED(p)
>>> d.brightness(20)
>>> d.brightness(90)
>>> d.brightness(95)
Re: class DimLed ?
Posted: Wed Jan 31, 2018 10:07 am
by Oberoid
efahl and python coder, thanks for the reply.
The solutions you describe still need a Pin object as argument.
The idea is that the Pin object is created during initialization of the DimLed object
So DimLed(2) first creates a Pin object and then uses that object to create a PWM object.
from machine import Pin, PWM
Code: Select all
class DimLed():
def __init__(self, pin):
aPin = Pin(pin)
self.pwm = PWM(aPin)
self.pwm.freq(100)
self.brightness(0)
def brightness(self, value):
value = max(0, min(100, value))
duty = int(10.23 * value)
self.pwm.duty(duty)
led = DimLed(2)
led.brightness(20)
Re: class DimLed ?
Posted: Thu Feb 01, 2018 7:00 am
by pythoncoder
You can do that. However in MicroPython drivers it is common practice to pass objects to the constructor rather than instantiating them in the constructor. This allows the objects to be used elsewhere. For example you might instantiate an I2C or SPI object and pass that to the constructors of each of N devices. They would then be able to share the physical bus.
Re: class DimLed ?
Posted: Thu Mar 22, 2018 10:13 pm
by Oberoid
I've found an example from the gpiozero library:
https://gpiozero.readthedocs.io/en/stab ... brightness
Nice code.
But thats for the raspberry.
Grts.
Re: class DimLed ?
Posted: Fri Mar 23, 2018 5:38 am
by OutoftheBOTS_
@pythoncoder
Why did you
duty = int(10.23 * value) instead of
duty = int(value) ??
Edit I already worked it out. Duty isn't in scale of 0 to 100 but rather in 0 to 1023
