class DimLed ?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Oberoid
Posts: 6
Joined: Sun Nov 12, 2017 11:26 am

class DimLed ?

Post by Oberoid » Wed Jan 24, 2018 5:18 pm

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?)

Oberoid
Posts: 6
Joined: Sun Nov 12, 2017 11:26 am

Re: class DimLed ?

Post by Oberoid » Thu Jan 25, 2018 2:56 pm

The Signal class is probably an example how to handle this.
https://docs.micropython.org/en/latest/ ... ignal.html

But is written in c++

efahl
Posts: 15
Joined: Sat Dec 23, 2017 2:02 pm

Re: class DimLed ?

Post by efahl » Thu Jan 25, 2018 7:37 pm

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. :lol: )

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: class DimLed ?

Post by pythoncoder » Fri Jan 26, 2018 7:34 am

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)
Peter Hinch
Index to my micropython libraries.

Oberoid
Posts: 6
Joined: Sun Nov 12, 2017 11:26 am

Re: class DimLed ?

Post by Oberoid » Wed Jan 31, 2018 10:07 am

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)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: class DimLed ?

Post by pythoncoder » Thu Feb 01, 2018 7:00 am

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.

Oberoid
Posts: 6
Joined: Sun Nov 12, 2017 11:26 am

Re: class DimLed ?

Post by Oberoid » Thu Mar 22, 2018 10:13 pm

I've found an example from the gpiozero library:
https://gpiozero.readthedocs.io/en/stab ... brightness
Nice code.

But thats for the raspberry.
Grts.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: class DimLed ?

Post by OutoftheBOTS_ » Fri Mar 23, 2018 5:38 am

@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 :)

Post Reply