C-like data types in µPy
-
- Posts: 3
- Joined: Tue Apr 03, 2018 2:22 pm
C-like data types in µPy
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
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
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: C-like data types in µPy
Will this possibility give you what you want http://docs.micropython.org/en/latest/w ... array.html
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: C-like data types in µPy
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.
Index to my micropython libraries.
-
- Posts: 3
- Joined: Tue Apr 03, 2018 2:22 pm
Re: C-like data types in µPy
Thanks guys.
Peter: Yes I forgot to mention that its for storing 8 bit unsigned ints.
Regards Allan
Peter: Yes I forgot to mention that its for storing 8 bit unsigned ints.
Regards Allan
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: C-like data types in µPy
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.
But this didn't workMicroPython 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
>>>
>>> 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
>>>
Re: C-like data types in µPy
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: 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.
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
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: C-like data types in µPy
@dhylands
I am a little confused by the output
I am a little confused by the output
Why does position [1][3] seem to show 0x03 instead of 0x2c ????>>> 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')]
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: C-like data types in µPy
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.
Index to my micropython libraries.
-
- Posts: 847
- Joined: Mon Nov 20, 2017 10:18 am
Re: C-like data types in µPy
@pythoncoder
Ahh it wasn't till I looked close that I saw the "," that represents 44 or 0x2c
Ahh it wasn't till I looked close that I saw the "," that represents 44 or 0x2c
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact: