How to create an array of 16-bit words?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
BillG
Posts: 9
Joined: Fri Feb 05, 2016 4:12 pm

How to create an array of 16-bit words?

Post by BillG » Tue Aug 09, 2016 10:06 pm

I'd like the use the ADC in 12-bit resolution mode and write to a buffer of 16-bit words. How to create this array in uPython? Doing "a = array.array('i',range(100))" creates an array of 4-byte words. Is there something like bytearray but for words?

I guess there is plenty of RAM in the CPU and I could use 4-byte arrays for the buffer. But since my final goal is to send the ADC samples over the radio_fast link, I want to pack the samples efficiently into the transmission frame, which has a max payload of 32 bytes. What are the available functions for picking out individual words and bytes from a long word?

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

Re: How to create an array of 16-bit words?

Post by dhylands » Tue Aug 09, 2016 11:11 pm

According to the documentation:
https://docs.python.org/3/library/array.html
h or H should work.

Given a variable x, x & 0xffff will return the lower 16-bits. (x >> 16) & 0xffff will return the upper 16 bits (answering your question about what functions are available)

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: How to create an array of 16-bit words?

Post by deshipu » Wed Aug 10, 2016 3:52 pm

There is also the ustruct module that is useful for encoding or decoding binary data.

Post Reply