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?)
class DimLed ?
Re: class DimLed ?
The Signal class is probably an example how to handle this.
https://docs.micropython.org/en/latest/ ... ignal.html
But is written in c++
https://docs.micropython.org/en/latest/ ... ignal.html
But is written in c++
Re: class DimLed ?
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. )
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. )
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: class DimLed ?
Do you mean something like this? This is based on LED's with cathodes connected to 0V via a resistor.
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
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)
Code: Select all
>>> p = Pin(2, Pin.OUT)
>>> p(1)
>>> p(0)
>>> d = DimLED(p)
>>> d.brightness(20)
>>> d.brightness(90)
>>> d.brightness(95)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: class DimLed ?
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
led = DimLed(2)
led.brightness(20)
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)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: class DimLed ?
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.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: class DimLed ?
I've found an example from the gpiozero library:
https://gpiozero.readthedocs.io/en/stab ... brightness
Nice code.
But thats for the raspberry.
Grts.
https://gpiozero.readthedocs.io/en/stab ... brightness
Nice code.
But thats for the raspberry.
Grts.
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: class DimLed ?
@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
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