Question regarding inheritance (extending framebuf)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
pvangeel
Posts: 3
Joined: Sun Mar 18, 2018 8:29 pm

Question regarding inheritance (extending framebuf)

Post by pvangeel » Thu Mar 22, 2018 11:31 am

As I've seen in other display drivers it's common to extend the framebuf.FrameBuffer class.

However I'm fairly new to (micro)python and I'm seeing some weird results.

First of all, this code seems to work just fine:

class Super:
def __init__(self, name):
self.name = name
print(name)

class Sub(Super):
def __init__(self):
super().__init__('name')

Sub()

However trying to achieve the same thing with the FrameBuffer class (code below) keeps resulting in an error 'function missing 4 required positional arguments'

import framebuf

class Display(framebuf.FrameBuffer):
def __init__(self):
self.buffer = bytearray(1)
self.width = 1
self.height = 1
super().__init__(self.buffer, self.width, self.height, framebuf.GS4_HMSB)

Display()

So it seems it's impossible to define a new constructor for subclasses?

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

Re: Question regarding inheritance (extending framebuf)

Post by pythoncoder » Fri Mar 23, 2018 6:25 am

Your code runs here on a Pyboard V1.1 running firmware v1.9.3-362-g2ad555b-dirty on 2018-03-17. Are you running reasonably up to date firmware? The ability to subclass the framebuf class is fairly recent.

Note that, in general, subclassing built-in types is not fully supported - see this doc. As you showed, subclassing types defined in Python is OK. Secondly you will find an example where the framebuf class is subclassed in the official SSD1306 driver here. The constructor is very similar to yours.
Peter Hinch
Index to my micropython libraries.

pvangeel
Posts: 3
Joined: Sun Mar 18, 2018 8:29 pm

Re: Question regarding inheritance (extending framebuf)

Post by pvangeel » Fri Mar 23, 2018 1:45 pm

Hi, thanks for the fast reply.

I should have tested with the latest version (d'uh). On my osx build of micropython it also works. I'm using a WiPy which is unfortunately still on an older micropython version : (

Post Reply