bitarrays for upython... anyone doing it

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Delebel
Posts: 48
Joined: Thu May 25, 2017 2:21 pm
Contact:

bitarrays for upython... anyone doing it

Post by Delebel » Mon Sep 17, 2018 7:26 pm

Well my quest to figure out how to use bitfields is still unsuccessful as I could not from the examples in the docs get any code to work for me . I'm now investigating if its possible to use bitarrays that are available for stock Python. It seems to install in the Thonny ide I'm using but that only is operational if I select the virtual interpreter. Running directly from the pyboard interpreter the import bitarray statement fails.
I'm soliciting anyone who might have already gone down that road. My application is fairly simple I need to read and write byte size data via an SPI port (that is working for me). Once the byte is acquired I need to control individual bits (read/write) to read several one wire DHT22 sensors. I've tried direct bitwise manipulations but the results were plagued with negative numbers as the native variable is a full word (16 bits). I know I could read the DHT22s directly with individual ios from the pyboard but I wish to have my complete home automation be a single SPI port via an MCP23S17 io expander. This would provide a quick swap method to go from my current traditional C controller to the pyboard and keep my home automation online while I learn upython.

Thanks in advance...Denis

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: bitarrays for upython... anyone doing it

Post by jickster » Mon Sep 17, 2018 7:38 pm

You tried this?

https://docs.micropython.org/en/latest/ ... types.html


Sent from my iPhone using Tapatalk Pro

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: bitarrays for upython... anyone doing it

Post by pfalcon » Tue Sep 18, 2018 6:10 am

And this: https://github.com/micropython/micropyt ... _le.py#L24 . Every feature in MicroPython is covered by a test. 1 test == 1 example. There're currently 952 tests under test/ in MicroPython https://github.com/micropython/micropyt ... ster/tests , aka 952 examples, so saying that there're not enough examples in MicroPython (like you do in your other post) is a bit unfair.

Also, if you would explain in what way exactly you were unsuccessful with bitfields (and which bitfields you mean in the first place), someone might help you. But try to explain to your rubber duck first.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

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

Re: bitarrays for upython... anyone doing it

Post by pythoncoder » Tue Sep 18, 2018 8:39 am

@Delebel Aside from uctypes Python also provides for traditional bitfield hacking:

Code: Select all

>>> a = bytearray(5)
>>> a
bytearray(b'\x00\x00\x00\x00\x00')
>>> b = 3
>>> a[0] |= b << 2
>>> hex(a[0])
'0xc'
>>> 
If you don't know them already, learn the Python bitwise operators (| & ^ << >> ~) and the equivalent assignment operators (|= etc.).
Peter Hinch
Index to my micropython libraries.

Delebel
Posts: 48
Joined: Thu May 25, 2017 2:21 pm
Contact:

Re: bitarrays for upython... anyone doing it

Post by Delebel » Tue Sep 18, 2018 3:29 pm

Thank for you're response. I had a look at the link and I will try to implement the code for my application. Yes I apologize for my lack of patience with micropython and I must realize that my inexperience with Python in general is compounding my learning curve with the environment. Thanks again.
pfalcon wrote:
Tue Sep 18, 2018 6:10 am
And this: https://github.com/micropython/micropyt ... _le.py#L24 . Every feature in MicroPython is covered by a test. 1 test == 1 example. There're currently 952 tests under test/ in MicroPython https://github.com/micropython/micropyt ... ster/tests , aka 952 examples, so saying that there're not enough examples in MicroPython (like you do in your other post) is a bit unfair.

Also, if you would explain in what way exactly you were unsuccessful with bitfields (and which bitfields you mean in the first place), someone might help you. But try to explain to your rubber duck first.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: bitarrays for upython... anyone doing it

Post by jickster » Tue Sep 18, 2018 5:12 pm

pfalcon wrote:
Tue Sep 18, 2018 6:10 am
And this: https://github.com/micropython/micropyt ... _le.py#L24 . Every feature in MicroPython is covered by a test. 1 test == 1 example. There're currently 952 tests under test/ in MicroPython https://github.com/micropython/micropyt ... ster/tests , aka 952 examples, so saying that there're not enough examples in MicroPython (like you do in your other post) is a bit unfair.

Also, if you would explain in what way exactly you were unsuccessful with bitfields (and which bitfields you mean in the first place), someone might help you. But try to explain to your rubber duck first.
This is the first time I realized the test cases are by definition examples!
You should mention this and link to them on the documentation site.

Delebel
Posts: 48
Joined: Thu May 25, 2017 2:21 pm
Contact:

Re: bitarrays for upython... anyone doing it

Post by Delebel » Thu Sep 20, 2018 2:59 pm

Well I finally got some bits read back so I'm posting some code in case it could be useful for other newbies out there

import uctypes

bite_desc = { # break byte into 8 individual bit fields
"bit0": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 1 << uctypes.BF_LEN,
"bit1": uctypes.BFUINT8 | 0 | 1 << uctypes.BF_POS | 1 << uctypes.BF_LEN,
"bit2": uctypes.BFUINT8 | 0 | 2 << uctypes.BF_POS | 1 << uctypes.BF_LEN,
"bit3": uctypes.BFUINT8 | 0 | 3 << uctypes.BF_POS | 1 << uctypes.BF_LEN,
"bit4": uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 1 << uctypes.BF_LEN,
"bit5": uctypes.BFUINT8 | 0 | 5 << uctypes.BF_POS | 1 << uctypes.BF_LEN,
"bit6": uctypes.BFUINT8 | 0 | 6 << uctypes.BF_POS | 1 << uctypes.BF_LEN,
"bit7": uctypes.BFUINT8 | 0 | 7 << uctypes.BF_POS | 1 << uctypes.BF_LEN,
}

bite = bytearray(b"01") # define single byte array
bite = MCP23S17.mcp_readA(mcp_recv) # other code gets the port value from a MCP23S17 io expander
S_bite = uctypes.struct(uctypes.addressof(bite), bite_desc, uctypes.UINT8) # S_bite points to bite
print(hex(S_bite.bit0))
print(hex(S_bite.bit1))
print(hex(S_bite.bit2))
print(hex(S_bite.bit3))

P.S. One thing that got me is that the S_bite stucture needs to be reset every time bite changes or else it doesn't get updated
Now I can move on to work with interrupts so I can read multiple DHT22, (one per bit)
onewire temp/humid sensors via the MCP23S17.

Thanks for the help


You tried this?

https://docs.micropython.org/en/latest/ ... types.html


Sent from my iPhone using Tapatalk Pro
[/quote]

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: bitarrays for upython... anyone doing it

Post by jickster » Thu Sep 20, 2018 7:39 pm

Delebel wrote:
Thu Sep 20, 2018 2:59 pm
If this is solved, prepend "[SOLVED]"

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

Re: bitarrays for upython... anyone doing it

Post by pythoncoder » Sat Sep 22, 2018 5:37 am

That solution is fine. But here is an alternative idiom which I prefer as it uses pure Python with no modules:

Code: Select all

class Foo():
    def __init__(self):
        self.bits = bytearray(1)

    def __getitem__(self, n):
        return (self.bits[0] >> n) & 1

    def __setitem__(self, n, v):
        if v:
            self.bits[0] |= v << n
        else:
            self.bits[0] &= ~(1 << n)
Test using

Code: Select all

foo = Foo()
foo[2] = 1  # Set bit 2
print(foo[2], hex(foo.bits[0]))

foo[2] = 0
foo[7] = 1
print(foo[2], hex(foo.bits[0]))
Peter Hinch
Index to my micropython libraries.

Delebel
Posts: 48
Joined: Thu May 25, 2017 2:21 pm
Contact:

Re: error within script versus at prompt no errors...???

Post by Delebel » Mon Sep 24, 2018 3:58 pm

I get errors with a script yet seemingly the same code typed directly at REPL prompt work fine. Any thoughts on where the error originates from as a script versus at the prompt. See attached jpeg screen capture.

Thanks Denis
UpythonError.jpg
UpythonError.jpg (74.93 KiB) Viewed 5811 times

Post Reply