bitarrays for upython... anyone doing it
bitarrays for upython... anyone doing it
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
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
Re: bitarrays for upython... anyone doing it
You tried this?
https://docs.micropython.org/en/latest/ ... types.html
Sent from my iPhone using Tapatalk Pro
https://docs.micropython.org/en/latest/ ... types.html
Sent from my iPhone using Tapatalk Pro
Re: bitarrays for upython... anyone doing it
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.
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/
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/
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: bitarrays for upython... anyone doing it
@Delebel Aside from uctypes Python also provides for traditional bitfield hacking:
If you don't know them already, learn the Python bitwise operators (| & ^ << >> ~) and the equivalent assignment operators (|= etc.).
Code: Select all
>>> a = bytearray(5)
>>> a
bytearray(b'\x00\x00\x00\x00\x00')
>>> b = 3
>>> a[0] |= b << 2
>>> hex(a[0])
'0xc'
>>>
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: bitarrays for upython... anyone doing it
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 amAnd 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.
Re: bitarrays for upython... anyone doing it
This is the first time I realized the test cases are by definition examples!pfalcon wrote: ↑Tue Sep 18, 2018 6:10 amAnd 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.
You should mention this and link to them on the documentation site.
Re: bitarrays for upython... anyone doing it
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]
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]
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: bitarrays for upython... anyone doing it
That solution is fine. But here is an alternative idiom which I prefer as it uses pure Python with no modules:
Test using
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)
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.
Index to my micropython libraries.
Re: error within script versus at prompt no errors...???
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
Thanks Denis