MicroPython I2C commands

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
LoRaTracker
Posts: 30
Joined: Sat Feb 25, 2017 8:24 am

MicroPython I2C commands

Post by LoRaTracker » Fri Dec 15, 2017 6:45 pm

I have been trying out Micropython, mainly on a micro:bit.

I have some programs that I have originally written for Arduino for addressing a FRAM, RTC and I2CDisplay backpack, that I am transferring to the mico:bit

Within Arduino I am used to this style of sending I2C commands;

void SetCursorPosition(byte col, byte row)
{
Wire.beginTransmission(0x08); //I2C Display address
Wire.write(CursorPosition); //Send cursor position command
Wire.write(col);
Wire.write(row);
Wire.endTransmission();
}

And this is the equivalent on the micro:bit, it does work.

def SetCursorPosition(col, row):
data = bytearray(3)
data[0]=CursorPosition
data[1]=col
data[2]=row
i2c.write(Display_Address, data)

Is there within Micropython the equivalent low level I2C commands (that are in Arduino) that avoids the need to assemble the data to send across I2C into a buffer ?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MicroPython I2C commands

Post by Roberthh » Fri Dec 15, 2017 7:38 pm

There are such simple operations (see the docs), but you can also write this function much more concise, like:

Code: Select all

def SetCursorPosition(col, row):
    i2c.writeto(Display_Address, bytearray([CursorPosition, col, row]))
That still would assemble the buffer, but on the fly.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: MicroPython I2C commands

Post by deshipu » Fri Dec 15, 2017 8:58 pm

Note that micro:bit has completely different commands than all the rest of MicroPython. In fact, the python on micro:bit shouldn't even be called MicroPython, it has so many differences. There are no low-level commands on the micro:bit, because it uses the mbed libraries, which don't give access to that.

LoRaTracker
Posts: 30
Joined: Sat Feb 25, 2017 8:24 am

Re: MicroPython I2C commands

Post by LoRaTracker » Sun Dec 17, 2017 11:53 am

Thanks for that Roberthh, worked a treat, I even managed a routine to write a float to the display;

Code: Select all

def WriteFloat(num,places):
    i2c.write(Display_Address, 'T' "%0.*f" % (places, num))
I was reading the micro:bit documentation, that does not appear to have the equivalent in 'standard' Micropython of I2C.write(buf)

LoRaTracker
Posts: 30
Joined: Sat Feb 25, 2017 8:24 am

Re: MicroPython I2C commands

Post by LoRaTracker » Sun Dec 17, 2017 11:58 am

@deshipu

Was it lack of program memory that was the reasons for the differences, they are somewhat annoying ?

The micro:bit is OK, but seriously lacking in program space. I was at PyconUK recently, the micro:bit guys were there, they suggested a larger program version was unlikely.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: MicroPython I2C commands

Post by deshipu » Sun Dec 17, 2017 8:05 pm

You would need to ask Damien for the exact reasons, but I think a large part of are two facts: 1. micro:bit is targeted at a different audience than pyboard, 2. when micropython for micro:bit was written, there was no "machine" module and no cross-platform standard yet.

LoRaTracker
Posts: 30
Joined: Sat Feb 25, 2017 8:24 am

Re: MicroPython I2C commands

Post by LoRaTracker » Mon Dec 18, 2017 4:02 pm

Thanks for the information.

Going down the 'machine' module makes a lot of sense, even on Pyboard. Some of the new platforms, ESP8266, ESP32 etc, are very low cost and being compatible at hardware level with the Pyboard and its clones is an atractive option.

Post Reply