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 ?
MicroPython I2C commands
Re: MicroPython I2C commands
There are such simple operations (see the docs), but you can also write this function much more concise, like:
That still would assemble the buffer, but on the fly.
Code: Select all
def SetCursorPosition(col, row):
i2c.writeto(Display_Address, bytearray([CursorPosition, col, row]))
Re: MicroPython I2C commands
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.
-
- Posts: 30
- Joined: Sat Feb 25, 2017 8:24 am
Re: MicroPython I2C commands
Thanks for that Roberthh, worked a treat, I even managed a routine to write a float to the display;
I was reading the micro:bit documentation, that does not appear to have the equivalent in 'standard' Micropython of I2C.write(buf)
Code: Select all
def WriteFloat(num,places):
i2c.write(Display_Address, 'T' "%0.*f" % (places, num))
-
- Posts: 30
- Joined: Sat Feb 25, 2017 8:24 am
Re: MicroPython I2C commands
@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.
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.
Re: MicroPython I2C commands
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.
-
- Posts: 30
- Joined: Sat Feb 25, 2017 8:24 am
Re: MicroPython I2C commands
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.
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.