C-like data types in µPy

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
AllanGErnst
Posts: 3
Joined: Tue Apr 03, 2018 2:22 pm

C-like data types in µPy

Post by AllanGErnst » Tue Apr 03, 2018 2:42 pm

Hello everybody

I’m a newbie with Python and MicroPython, so please be gentle :)
I’m making an embedded system using a stm32f429i-disc1 board running MicroPython.

I would like to make a 2D array but I found that the overhead is massive, if I use native MicroPython, so I’ve had a look at the uctypes library. Unfortunately I can’t really get my head around it.

Could somebody please tell me how to get as close to the c type “int twodim[5][10];” as possible? With a minimum amount overhead both with respect to time and memory.

Please include the top of the code, with the imports.

Thanks a lot

Best regards Allan

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: C-like data types in µPy

Post by OutoftheBOTS_ » Tue Apr 03, 2018 9:47 pm

Will this possibility give you what you want http://docs.micropython.org/en/latest/w ... array.html

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

Re: C-like data types in µPy

Post by pythoncoder » Wed Apr 04, 2018 7:58 am

What sort of data do you want to store in the array? 32 bit integers? 16 or 8 bit words? Bits? The overhead of Python lists is not high compared to arrays, but the overhead of storing small quantities in large words can be substantial.
Peter Hinch
Index to my micropython libraries.

AllanGErnst
Posts: 3
Joined: Tue Apr 03, 2018 2:22 pm

Re: C-like data types in µPy

Post by AllanGErnst » Wed Apr 04, 2018 8:22 am

Thanks guys.

Peter: Yes I forgot to mention that its for storing 8 bit unsigned ints.

Regards Allan

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: C-like data types in µPy

Post by OutoftheBOTS_ » Wed Apr 04, 2018 9:54 pm

I did just have a little play with storing 8bit unsigned ints (unsigned Char) in an aaaray and this was sucessful but I wasn't able to do the same with 2D array.
MicroPython ESP32_LoBo_v3.1.28 - 2018-03-29 on ESP32 board with ESP32
Type "help()" for more information.
>>> from array import *
>>> my_array = array('B', [1,2,3,4,5])
>>> for item in my_array: print(item)
...
1
2
3
4
5
>>>
But this didn't work
>>> my_array = array('B', [[1,2,3,4,5],[1,2,3,4,5]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert list to int
>>>

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: C-like data types in µPy

Post by dhylands » Wed Apr 04, 2018 10:58 pm

To store 8-bit ints you should probably use a bytearray. bytearrays are inherently one-dimensional, so I would create a wrapper class to make them look 2 dimensional and have it do the calculations. For example suppose I wanted a 2 dimensional array with 5 columns and 10 rows. You could allocate a 50-byte (5 * 10) entry byte-array, and given r and c calculate the index as r * 5 + c.

You could also use arrays but they're also inherently one dimensional, so they would need the same calculation.

You could also use a hybrid approach and create a 2 dimensional array by creating an array of byte arrays:

Code: Select all

>>> x = [bytearray(5), bytearray([1, 2, 3, 4, 5]), bytearray([6, 7, 8, 9, 0])]
>>> x
[bytearray(b'\x00\x00\x00\x00\x00'), bytearray(b'\x01\x02\x03\x04\x05'), bytearray(b'\x06\x07\x08\t\x00')]
>>> x[1][3]
4
>>> x[1][3] = 44
>>> x
[bytearray(b'\x00\x00\x00\x00\x00'), bytearray(b'\x01\x02\x03,\x05'), bytearray(b'\x06\x07\x08\t\x00')]
>>> x[1][3]
44
>>> ord(',')
44
The ASCII value of the comma in the second bytearray is 44. When printing bytearrays, it aways tries to use ASCII codes, and only uses the \x notation for non-ASCII values.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: C-like data types in µPy

Post by OutoftheBOTS_ » Thu Apr 05, 2018 1:09 am

@dhylands

I am a little confused by the output
>>> x[1][3] = 44
>>> x
[bytearray(b'\x00\x00\x00\x00\x00'), bytearray(b'\x01\x02\x03,\x05'), bytearray(b'\x06\x07\x08\t\x00')]
Why does position [1][3] seem to show 0x03 instead of 0x2c ????

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

Re: C-like data types in µPy

Post by pythoncoder » Thu Apr 05, 2018 7:01 am

The contents of x[1] viewed as ordinal numbers are 1,2,3,44,5. So x[1][3] can be represented as 44 or 0x2c or ASCII ",".
Peter Hinch
Index to my micropython libraries.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: C-like data types in µPy

Post by OutoftheBOTS_ » Thu Apr 05, 2018 7:59 am

@pythoncoder
Ahh it wasn't till I looked close that I saw the "," that represents 44 or 0x2c

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

Re: C-like data types in µPy

Post by pythoncoder » Fri Apr 06, 2018 6:07 am

We've all been there ;)
Peter Hinch
Index to my micropython libraries.

Post Reply