Pyboard + SSD1306

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Pyboard + SSD1306

Post by pythoncoder » Thu Jul 26, 2018 4:20 am

You may need to point the I2C switch at the device before instantiating it.
Peter Hinch
Index to my micropython libraries.

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: Pyboard + SSD1306

Post by doublevee » Mon Jul 30, 2018 8:10 am

Thank you Peter. I now have a screen with random dots, so something is happening :)

I don't understand though how the ssd1306.py library can work with the Pyboard?? I have copied a class below:

------------------------------------------------------------------------------------------
class SSD1306_I2C(SSD1306):
def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False):
self.i2c = i2c
self.addr = addr
self.temp = bytearray(2)
super().__init__(width, height, external_vcc)

def write_cmd(self, cmd):
self.temp[0] = 0x80 # Co=1, D/C#=0
self.temp[1] = cmd
self.i2c.writeto(self.addr, self.temp)

def write_data(self, buf):
self.temp[0] = self.addr << 1
self.temp[1] = 0x40 # Co=0, D/C#=1
self.i2c.start()
self.i2c.write(self.temp)
self.i2c.write(buf)
self.i2c.stop()
------------------------------------------------------------------------------------------
The function 'write_data' uses i2c.start, i2c.write and i2c.stop - none of these are supported on the Pyboard though?

I have tried to modify 'write_data' to remove the start and stop, and use i2c.writeto instead, but I don't think I am doing this correctly.

I would really appreciate your view on this please.

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

Re: Pyboard + SSD1306

Post by pythoncoder » Mon Jul 30, 2018 8:19 am

The Pyboard supports both pyb and machine. The latter is portable between platforms. The SSD1306 driver was written to be portable and hence uses machine. It has been tested on Pyboard, ESP8266 and ESP32 (possibly other platforms).

I suggest you connect your display directly to the Pyboard and get it working. Then introduce the I2C switch. That simplifies isolating any problems.
Peter Hinch
Index to my micropython libraries.

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: Pyboard + SSD1306

Post by doublevee » Mon Jul 30, 2018 10:43 am

Thanks Peter.

I am happy to try this (although I don't believe I2C comms is the problem) but I still don't understand how the command i2c.write (for example) will ever work in the SSD1306.py library? It always fails and is not a supported instruction as per the documentation around machine.I2C.

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

Re: Pyboard + SSD1306

Post by pythoncoder » Tue Jul 31, 2018 8:41 am

doublevee wrote:
Mon Jul 30, 2018 10:43 am
...I still don't understand how the command i2c.write (for example) will ever work in the SSD1306.py library? It always fails and is not a supported instruction as per the documentation around machine.I2C.
See the docs.
Peter Hinch
Index to my micropython libraries.

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: Pyboard + SSD1306

Post by doublevee » Wed Aug 01, 2018 7:00 am

Thanks Peter.

This is the exact documentation I am referring to, where i2c.write() is only supported on the ESP8266 (according to the text).

Using the command prompt on the Pyboard, I can confirm that i2c.write() returns:

>>> buf=bytearray(10)
>>> i2c.write(buf)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: I2C operation not supported

Whereas i2c.writeto() returns correctly:

>>> buf = bytearray(10)
>>> i2c.writeto(0x70,buf)
10

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

Re: Pyboard + SSD1306

Post by pythoncoder » Wed Aug 01, 2018 7:35 am

The doc is misleading when indicating that these functions are ESP8266 only. The following runs without error on my Pyboard:

Code: Select all

import machine
pscl = machine.Pin('Y9', machine.Pin.OPEN_DRAIN)
psda = machine.Pin('Y10', machine.Pin.OPEN_DRAIN)
i2c = machine.I2C(scl=pscl, sda=psda)
buf = bytearray(10)
i2c.start()
i2c.write(buf)
You might like to try the test program here. I've verified that this works with the latest firmware on a Pyboard, using the official driver and an I2C SSD1306 display.
Peter Hinch
Index to my micropython libraries.

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: Pyboard + SSD1306

Post by doublevee » Wed Aug 01, 2018 9:01 am

Aha!!!!!!
So are you saying that the machine.I2C class object is different when constructed specifying pins Vs just the bus number?
I will try this out later and report back. Thank you ever so much for all your help Peter - very appreciated.

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

Re: Pyboard + SSD1306

Post by pythoncoder » Wed Aug 01, 2018 5:22 pm

The docs could definitely be clearer on this. The pyb module uses bus number, whereas on machine I found it necessary to specify pins. The SSD1306 driver was written to be cross-platform and hence uses machine.
Peter Hinch
Index to my micropython libraries.

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: Pyboard + SSD1306

Post by doublevee » Thu Aug 02, 2018 8:25 am

I can confirm that everything leapt into life when I changed to specifying the pins.

When calling the object like this, is this a hardware or software implementation?

I do agree the documentation needs a tweak - I think it would be maybe useful to remove examples of calling machine.I2C using bus numbers?

Thanks again Peter!

Post Reply