Pyboard + SSD1306
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Pyboard + SSD1306
You may need to point the I2C switch at the device before instantiating it.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Pyboard + SSD1306
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.
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Pyboard + SSD1306
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.
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.
Index to my micropython libraries.
Re: Pyboard + SSD1306
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.
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Pyboard + SSD1306
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
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
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Pyboard + SSD1306
The doc is misleading when indicating that these functions are ESP8266 only. The following runs without error on my Pyboard:
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.
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)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Pyboard + SSD1306
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.
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Pyboard + SSD1306
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.
Index to my micropython libraries.
Re: Pyboard + SSD1306
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!
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!