assembling commands for SPI

classic Classic list List threaded Threaded
11 messages Options
Richard Graham Richard Graham
Reply | Threaded
Open this post in threaded view
|

assembling commands for SPI

Hi elua people,

I am just getting started with elua. Looks pretty nice so far.

I need to pack together data to make some commands for setting various
registers on a device connected via SPI. I need to be able to set
individual bits and also pack 8 and 16 bit numbers. This seems like it
should be a common problem and I was wondering what the usual approach
to this is.

I see the 'pack' module, though it looks like it only supports packing
whole bytes at a minimum. Packing and unpacking at the individual bit
level is going to get messy.

I have found lua-bitstring, which looks like it should be great for this
purpose -- though I can hardly find any documentation or others using
it. I was wondering if anyone has managed to use this in elua? Or is
there something else I am missing?

Thanks,
--
Richard
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Thiago Naves Thiago Naves
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

Hi Richard,
You can use the bit module to do the usual bit operations (or, and, xor, ...):
http://bitop.luajit.org/api.html

To assemble multiple bytes, you could do
a = 3
b = 5
c = a..b
and have c = "35"
or spi.write(0, a, b)

--
Thiago


On Mon, Nov 18, 2013 at 4:06 AM, Richard Graham <[hidden email]> wrote:
Hi elua people,

I am just getting started with elua. Looks pretty nice so far.

I need to pack together data to make some commands for setting various
registers on a device connected via SPI. I need to be able to set
individual bits and also pack 8 and 16 bit numbers. This seems like it
should be a common problem and I was wondering what the usual approach
to this is.

I see the 'pack' module, though it looks like it only supports packing
whole bytes at a minimum. Packing and unpacking at the individual bit
level is going to get messy.

I have found lua-bitstring, which looks like it should be great for this
purpose -- though I can hardly find any documentation or others using
it. I was wondering if anyone has managed to use this in elua? Or is
there something else I am missing?

Thanks,
--
Richard
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev


_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
jbsnyder jbsnyder
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

There is a bit module included with eLua but it's actually based on bitlib and the API is a little different:


On Mon, Nov 18, 2013 at 6:48 AM, Thiago Naves <[hidden email]> wrote:
Hi Richard,
You can use the bit module to do the usual bit operations (or, and, xor, ...):
http://bitop.luajit.org/api.html

To assemble multiple bytes, you could do
a = 3
b = 5
c = a..b
and have c = "35"
or spi.write(0, a, b)

--
Thiago


On Mon, Nov 18, 2013 at 4:06 AM, Richard Graham <[hidden email]> wrote:
Hi elua people,

I am just getting started with elua. Looks pretty nice so far.

I need to pack together data to make some commands for setting various
registers on a device connected via SPI. I need to be able to set
individual bits and also pack 8 and 16 bit numbers. This seems like it
should be a common problem and I was wondering what the usual approach
to this is.

I see the 'pack' module, though it looks like it only supports packing
whole bytes at a minimum. Packing and unpacking at the individual bit
level is going to get messy.

I have found lua-bitstring, which looks like it should be great for this
purpose -- though I can hardly find any documentation or others using
it. I was wondering if anyone has managed to use this in elua? Or is
there something else I am missing?

Thanks,
--
Richard
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev


_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev



--
James Snyder
Biomedical Engineering
Northwestern University
ph: (847) 448-0386

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Richard Graham Richard Graham
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

Hi,

Thanks James and Thiago for your replies.

I am aware of the bit module, though as far as I can tell it is not
especially useful for me. As an example I need to assemble a 16 bit
command of the form:
  2 1-bit flags, followed by a 6-bit number followed by an 8-bit number.
This could be done with bit shifts but is cumbersome when I have a lot
of commands to do. There doesn't seem to be an easy way to cast a lua
number to a given precision. lua-bitstring seems to be a lot easier if
it can be made to work.

Also, from a quick look at the SPI code it seems that if you pass a
number as in your example, it will use lua_tointeger on the number and
send that. It then seems from luaconf.h that the actual number of bits
sent would depend on the platform or how lua is compiled. I suspect if I
need to control the number of bytes sent I would need to send the data
as a string.

Let me know if I have this wrong or if you have any thoughts.

Thanks,
--
Richard Graham


On 11/18/2013 10:51 AM, James Snyder wrote:

> There is a bit module included with eLua but it's actually based on
> bitlib and the API is a little different:
> http://www.eluaproject.net/doc/master/en_refman_gen_bit.html
>
>
> On Mon, Nov 18, 2013 at 6:48 AM, Thiago Naves <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi Richard,
>     You can use the bit module to do the usual bit operations (or, and,
>     xor, ...):
>     http://bitop.luajit.org/api.html
>
>     To assemble multiple bytes, you could do
>     a = 3
>     b = 5
>     c = a..b
>     and have c = "35"
>     or spi.write(0, a, b)
>
>     --
>     Thiago
>
>
>     On Mon, Nov 18, 2013 at 4:06 AM, Richard Graham <[hidden email]
>     <mailto:[hidden email]>> wrote:
>
>         Hi elua people,
>
>         I am just getting started with elua. Looks pretty nice so far.
>
>         I need to pack together data to make some commands for setting
>         various
>         registers on a device connected via SPI. I need to be able to set
>         individual bits and also pack 8 and 16 bit numbers. This seems
>         like it
>         should be a common problem and I was wondering what the usual
>         approach
>         to this is.
>
>         I see the 'pack' module, though it looks like it only supports
>         packing
>         whole bytes at a minimum. Packing and unpacking at the
>         individual bit
>         level is going to get messy.
>
>         I have found lua-bitstring, which looks like it should be great
>         for this
>         purpose -- though I can hardly find any documentation or others
>         using
>         it. I was wondering if anyone has managed to use this in elua? Or is
>         there something else I am missing?
>
>         Thanks,
>         --
>         Richard
>         _______________________________________________
>         eLua-dev mailing list
>         [hidden email] <mailto:[hidden email]>
>         https://lists.berlios.de/mailman/listinfo/elua-dev
>
>
>
>     _______________________________________________
>     eLua-dev mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://lists.berlios.de/mailman/listinfo/elua-dev
>
>
>
>
> --
> James Snyder
> Biomedical Engineering
> Northwestern University
> ph: (847) 448-0386
>
>
> _______________________________________________
> eLua-dev mailing list
> [hidden email]
> https://lists.berlios.de/mailman/listinfo/elua-dev
>

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
scdr scdr
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

In reply to this post by Richard Graham

Just a thought - have you looked at eLua bitarrays?
I haven't studied what the raw output does, but there is an outside chance one might be able to
define bitarrays with different element sizes and pass the contents back and forth.
Not sure that would be any less nasty than using bit module and shifts, but it is another approach.

As long as the format is consistent, not clear why using shifts cumbersome
(i.e. make a function to pack/unpack it) then all the bitshift nasties are hidden.

Bitstring looks like a nice find though.

> ----- Original Message -----
> From: Richard Graham
> Sent: 11/18/13 12:01 PM
> To: eLua Users and Development List (www.eluaproject.net)
> Subject: Re: [eLua-dev] assembling commands for SPI
>
> Hi,
>
> Thanks James and Thiago for your replies.
>
> I am aware of the bit module, though as far as I can tell it is not
> especially useful for me. As an example I need to assemble a 16 bit
> command of the form:
>  2 1-bit flags, followed by a 6-bit number followed by an 8-bit number.
> This could be done with bit shifts but is cumbersome when I have a lot
> of commands to do. There doesn't seem to be an easy way to cast a lua
> number to a given precision. lua-bitstring seems to be a lot easier if
> it can be made to work.
>
> Also, from a quick look at the SPI code it seems that if you pass a
> number as in your example, it will use lua_tointeger on the number and
> send that. It then seems from luaconf.h that the actual number of bits
> sent would depend on the platform or how lua is compiled. I suspect if I
> need to control the number of bytes sent I would need to send the data
> as a string.
>
> Let me know if I have this wrong or if you have any thoughts.
>
> Thanks,
> --
> Richard Graham
>
>
> On 11/18/2013 10:51 AM, James Snyder wrote:
> > There is a bit module included with eLua but it's actually based on
> > bitlib and the API is a little different:
> > http://www.eluaproject.net/doc/master/en_refman_gen_bit.html
> >
> >
> > On Mon, Nov 18, 2013 at 6:48 AM, Thiago Naves <[hidden email]
> > <mailto:[hidden email]>> wrote:
> >
> > Hi Richard,
> > You can use the bit module to do the usual bit operations (or, and,
> > xor, ...):
> > http://bitop.luajit.org/api.html
> >
> > To assemble multiple bytes, you could do
> > a = 3
> > b = 5
> > c = a..b
> > and have c = "35"
> > or spi.write(0, a, b)
> >
> > --
> > Thiago
> >
> >
> > On Mon, Nov 18, 2013 at 4:06 AM, Richard Graham <[hidden email]
> > <mailto:[hidden email]>> wrote:
> >
> > Hi elua people,
> >
> > I am just getting started with elua. Looks pretty nice so far.
> >
> > I need to pack together data to make some commands for setting
> > various
> > registers on a device connected via SPI. I need to be able to set
> > individual bits and also pack 8 and 16 bit numbers. This seems
> > like it
> > should be a common problem and I was wondering what the usual
> > approach
> > to this is.
> >
> > I see the 'pack' module, though it looks like it only supports
> > packing
> > whole bytes at a minimum. Packing and unpacking at the
> > individual bit
> > level is going to get messy.
> >
> > I have found lua-bitstring, which looks like it should be great
> > for this
> > purpose -- though I can hardly find any documentation or others
> > using
> > it. I was wondering if anyone has managed to use this in elua? Or is
> > there something else I am missing?
> >
> > Thanks,
> > --
> > Richard
> > _______________________________________________
> > eLua-dev mailing list
> > [hidden email] <mailto:[hidden email]>
> > https://lists.berlios.de/mailman/listinfo/elua-dev
> >
> >
> >
> > _______________________________________________
> > eLua-dev mailing list
> > [hidden email] <mailto:[hidden email]>
> > https://lists.berlios.de/mailman/listinfo/elua-dev
> >
> >
> >
> >
> > --
> > James Snyder
> > Biomedical Engineering
> > Northwestern University
> > ph: (847) 448-0386
> >
> >
> > _______________________________________________
> > eLua-dev mailing list
> > [hidden email]
> > https://lists.berlios.de/mailman/listinfo/elua-dev
> >
>
> _______________________________________________
> eLua-dev mailing list
> [hidden email]
> https://lists.berlios.de/mailman/listinfo/elua-dev

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
jbsnyder jbsnyder
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

In reply to this post by Richard Graham



On Mon, Nov 18, 2013 at 2:01 PM, Richard Graham <[hidden email]> wrote:
Hi,

Thanks James and Thiago for your replies.

I am aware of the bit module, though as far as I can tell it is not
especially useful for me. As an example I need to assemble a 16 bit
command of the form:
  2 1-bit flags, followed by a 6-bit number followed by an 8-bit number.
This could be done with bit shifts but is cumbersome when I have a lot
of commands to do. There doesn't seem to be an easy way to cast a lua
number to a given precision. lua-bitstring seems to be a lot easier if
it can be made to work.

The syntax for that looks nice to work with for packing and unpacking of definable length fields.

Depending on how it was written it might not be too hard to compile that as a module for eLua if it's generally ANSI C + the Lua API, but there might be other issues with its memory usage assumptions or total code size.  It does look like a nice way to pack data though.

With the current modules though you'd be looking at using the bit module with shifts or some abstraction on top of that.


Also, from a quick look at the SPI code it seems that if you pass a
number as in your example, it will use lua_tointeger on the number and
send that. It then seems from luaconf.h that the actual number of bits
sent would depend on the platform or how lua is compiled. I suspect if I
need to control the number of bytes sent I would need to send the data
as a string.

The actual SPI word size will be defined when you call spi.setup, but you're correct that the integer size defined at compile time would need to be large enough to hold each word.

As far as I know all of our platforms will have a lua integer size of 32-bits (a long) which is plenty for the word sizes most of the SPI interfaces support of 8 and 16-bits. You can check individual platforms in the type.h file.

I believe what should happen on each platform is that the lowest wordsize bits of the "data" parameter will be used out of the size of the number passed.  This will be regardless of the native Lua type which could be be double-precision floating point or 32-bit/64-bit integer.

I took a quick look at the string approach and that looks like it goes byte-by-byte so that may actually only work with 8-bit SPI wordsize.  We should probably do something about that or document it unless I've misinterpreted something.

I haven't  investigated ecdr's bitarrays suggestion (https://github.com/elua/elua/blob/master/src/modules/bitarray.c) but I think that has a fixed field size?



Let me know if I have this wrong or if you have any thoughts.

Thanks,
--
Richard Graham


On 11/18/2013 10:51 AM, James Snyder wrote:
> There is a bit module included with eLua but it's actually based on
> bitlib and the API is a little different:
> http://www.eluaproject.net/doc/master/en_refman_gen_bit.html
>
>
> On Mon, Nov 18, 2013 at 6:48 AM, Thiago Naves <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi Richard,
>     You can use the bit module to do the usual bit operations (or, and,
>     xor, ...):
>     http://bitop.luajit.org/api.html
>
>     To assemble multiple bytes, you could do
>     a = 3
>     b = 5
>     c = a..b
>     and have c = "35"
>     or spi.write(0, a, b)
>
>     --
>     Thiago
>
>
>     On Mon, Nov 18, 2013 at 4:06 AM, Richard Graham <[hidden email]
>     <mailto:[hidden email]>> wrote:
>
>         Hi elua people,
>
>         I am just getting started with elua. Looks pretty nice so far.
>
>         I need to pack together data to make some commands for setting
>         various
>         registers on a device connected via SPI. I need to be able to set
>         individual bits and also pack 8 and 16 bit numbers. This seems
>         like it
>         should be a common problem and I was wondering what the usual
>         approach
>         to this is.
>
>         I see the 'pack' module, though it looks like it only supports
>         packing
>         whole bytes at a minimum. Packing and unpacking at the
>         individual bit
>         level is going to get messy.
>
>         I have found lua-bitstring, which looks like it should be great
>         for this
>         purpose -- though I can hardly find any documentation or others
>         using
>         it. I was wondering if anyone has managed to use this in elua? Or is
>         there something else I am missing?
>
>         Thanks,
>         --
>         Richard
>         _______________________________________________
>         eLua-dev mailing list
>         [hidden email] <mailto:[hidden email]>
>         https://lists.berlios.de/mailman/listinfo/elua-dev
>
>
>
>     _______________________________________________
>     eLua-dev mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://lists.berlios.de/mailman/listinfo/elua-dev
>
>
>
>
> --
> James Snyder
> Biomedical Engineering
> Northwestern University
> ph: <a href="tel:%28847%29%20448-0386" value="+18474480386">(847) 448-0386
>
>
> _______________________________________________
> eLua-dev mailing list
> [hidden email]
> https://lists.berlios.de/mailman/listinfo/elua-dev
>

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev



--
James Snyder
Biomedical Engineering
Northwestern University
ph: (847) 448-0386

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Richard Graham Richard Graham
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

Hi,

Thanks for that. I think I should be able to manage with just the bit
module.

I didn't see the bitarrays module before. Looks like it might work,
though I couldn't find any documentation about this and the source is
not that easy to read at a quick look. Presumably this module is used
internally somewhere?

Another thing, I was wondering if anyone has compiled the elua modules
so they can be used with lua on the desktop? I would prefer to do most
of the development on the desktop but since some libraries are
different, i.e. the bit and bitlib api that is not to easy.

Thanks,
--
Richard Graham


On 11/18/2013 03:08 PM, James Snyder wrote:

>
>
>
> On Mon, Nov 18, 2013 at 2:01 PM, Richard Graham <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi,
>
>     Thanks James and Thiago for your replies.
>
>     I am aware of the bit module, though as far as I can tell it is not
>     especially useful for me. As an example I need to assemble a 16 bit
>     command of the form:
>       2 1-bit flags, followed by a 6-bit number followed by an 8-bit number.
>     This could be done with bit shifts but is cumbersome when I have a lot
>     of commands to do. There doesn't seem to be an easy way to cast a lua
>     number to a given precision. lua-bitstring seems to be a lot easier if
>     it can be made to work.
>
>
> The syntax for that looks nice to work with for packing and unpacking of
> definable length fields.
>
> Depending on how it was written it might not be too hard to compile that
> as a module for eLua if it's generally ANSI C + the Lua API, but there
> might be other issues with its memory usage assumptions or total code
> size.  It does look like a nice way to pack data though.
>
> With the current modules though you'd be looking at using the bit module
> with shifts or some abstraction on top of that.
>
>
>     Also, from a quick look at the SPI code it seems that if you pass a
>     number as in your example, it will use lua_tointeger on the number and
>     send that. It then seems from luaconf.h that the actual number of bits
>     sent would depend on the platform or how lua is compiled. I suspect if I
>     need to control the number of bytes sent I would need to send the data
>     as a string.
>
>
> The actual SPI word size will be defined when you call spi.setup, but
> you're correct that the integer size defined at compile time would need
> to be large enough to hold each word.
>
> As far as I know all of our platforms will have a lua integer size of
> 32-bits (a long) which is plenty for the word sizes most of the SPI
> interfaces support of 8 and 16-bits. You can check individual platforms
> in the type.h file.
>
> I believe what should happen on each platform is that the lowest
> wordsize bits of the "data" parameter will be used out of the size of
> the number passed.  This will be regardless of the native Lua type which
> could be be double-precision floating point or 32-bit/64-bit integer.
>
> I took a quick look at the string approach and that looks like it goes
> byte-by-byte so that may actually only work with 8-bit SPI wordsize.  We
> should probably do something about that or document it unless I've
> misinterpreted something.
>
> I haven't  investigated ecdr's bitarrays suggestion
> (https://github.com/elua/elua/blob/master/src/modules/bitarray.c) but I
> think that has a fixed field size?
>
>
>
>     Let me know if I have this wrong or if you have any thoughts.
>
>     Thanks,
>     --
>     Richard Graham
>
>
>     On 11/18/2013 10:51 AM, James Snyder wrote:
>     > There is a bit module included with eLua but it's actually based on
>     > bitlib and the API is a little different:
>     > http://www.eluaproject.net/doc/master/en_refman_gen_bit.html
>     >
>     >
>     > On Mon, Nov 18, 2013 at 6:48 AM, Thiago Naves
>     <[hidden email] <mailto:[hidden email]>
>     > <mailto:[hidden email] <mailto:[hidden email]>>>
>     wrote:
>     >
>     >     Hi Richard,
>     >     You can use the bit module to do the usual bit operations (or,
>     and,
>     >     xor, ...):
>     >     http://bitop.luajit.org/api.html
>     >
>     >     To assemble multiple bytes, you could do
>     >     a = 3
>     >     b = 5
>     >     c = a..b
>     >     and have c = "35"
>     >     or spi.write(0, a, b)
>     >
>     >     --
>     >     Thiago
>     >
>     >
>     >     On Mon, Nov 18, 2013 at 4:06 AM, Richard Graham <[hidden email]
>     >     <mailto:[hidden email] <mailto:[hidden email]>>> wrote:
>     >
>     >         Hi elua people,
>     >
>     >         I am just getting started with elua. Looks pretty nice so far.
>     >
>     >         I need to pack together data to make some commands for setting
>     >         various
>     >         registers on a device connected via SPI. I need to be able
>     to set
>     >         individual bits and also pack 8 and 16 bit numbers. This seems
>     >         like it
>     >         should be a common problem and I was wondering what the usual
>     >         approach
>     >         to this is.
>     >
>     >         I see the 'pack' module, though it looks like it only supports
>     >         packing
>     >         whole bytes at a minimum. Packing and unpacking at the
>     >         individual bit
>     >         level is going to get messy.
>     >
>     >         I have found lua-bitstring, which looks like it should be
>     great
>     >         for this
>     >         purpose -- though I can hardly find any documentation or
>     others
>     >         using
>     >         it. I was wondering if anyone has managed to use this in
>     elua? Or is
>     >         there something else I am missing?
>     >
>     >         Thanks,
>     >         --
>     >         Richard
>     >         _______________________________________________
>     >         eLua-dev mailing list
>     >         [hidden email]
>     <mailto:[hidden email]> <mailto:[hidden email]
>     <mailto:[hidden email]>>
>     >         https://lists.berlios.de/mailman/listinfo/elua-dev
>     >
>     >
>     >
>     >     _______________________________________________
>     >     eLua-dev mailing list
>     >     [hidden email] <mailto:[hidden email]>
>     <mailto:[hidden email] <mailto:[hidden email]>>
>     >     https://lists.berlios.de/mailman/listinfo/elua-dev
>     >
>     >
>     >
>     >
>     > --
>     > James Snyder
>     > Biomedical Engineering
>     > Northwestern University
>     > ph: (847) 448-0386 <tel:%28847%29%20448-0386>
>     >
>     >
>     > _______________________________________________
>     > eLua-dev mailing list
>     > [hidden email] <mailto:[hidden email]>
>     > https://lists.berlios.de/mailman/listinfo/elua-dev
>     >
>
>     _______________________________________________
>     eLua-dev mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://lists.berlios.de/mailman/listinfo/elua-dev
>
>
>
>
> --
> James Snyder
> Biomedical Engineering
> Northwestern University
> ph: (847) 448-0386
>
>
> _______________________________________________
> eLua-dev mailing list
> [hidden email]
> https://lists.berlios.de/mailman/listinfo/elua-dev
>

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
jbsnyder jbsnyder
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI




On Tue, Nov 19, 2013 at 4:28 PM, Richard Graham <[hidden email]> wrote:
Hi,

Thanks for that. I think I should be able to manage with just the bit
module.

I didn't see the bitarrays module before. Looks like it might work,
though I couldn't find any documentation about this and the source is
not that easy to read at a quick look. Presumably this module is used
internally somewhere?

Not sure if we use it internally anywhere, it may just not have gotten documented.  Here's one example of its usage to take a binary image put it in a bit array and then rotate it to be shown on an OLED display:

Here's a first pass at the API just from skimming the source a bit:

arrays are created either with a specific capacity, a source byte array (string) or an existing Lua array, packed into a uniform element size array
// Lua: array = bitarray.new( capacity, [element_size_bits], [fill] ), or
//      array = bitarray.new( "string", [element_size_bits] ), or
//      array = bitarray.new( lua_array, [element_size_bits] )

fields can be indexed like a lua table:
// Lua: value = array[ idx ]

can be assigned like a table:
// Lua: array[ key ] = value

get length similar to how one would do so for a string:
// Lua : size = #array

arrays can be converted into a string.  not sure what raw/seq means
// Lua: string = bitarray.tostring( array, ["raw"|"seq"] )

or a table (again, not sure about raw/seq):
// Lua: table = bitarray.totable( array, ["raw"|"seq"] )

And it looks like you can use bitarray.pairs which looks like it should behave like the built-in Lua pairs.



Another thing, I was wondering if anyone has compiled the elua modules
so they can be used with lua on the desktop? I would prefer to do most
of the development on the desktop but since some libraries are
different, i.e. the bit and bitlib api that is not to easy.

One quick way that should work is to build the luarpc desktop binary as described here:

If you already have the build environment for using the lua-based build system and you have compilers on your system to build native binaries you should be able to just run:
lua rpc-lua.lua

When you run the resulting binary (luarpc) you'll get a Lua REPL and the binary should include the modules like bit/pack/bitarray.

Best.

-jsnyder
 

Thanks,
--
Richard Graham


On 11/18/2013 03:08 PM, James Snyder wrote:
>
>
>
> On Mon, Nov 18, 2013 at 2:01 PM, Richard Graham <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hi,
>
>     Thanks James and Thiago for your replies.
>
>     I am aware of the bit module, though as far as I can tell it is not
>     especially useful for me. As an example I need to assemble a 16 bit
>     command of the form:
>       2 1-bit flags, followed by a 6-bit number followed by an 8-bit number.
>     This could be done with bit shifts but is cumbersome when I have a lot
>     of commands to do. There doesn't seem to be an easy way to cast a lua
>     number to a given precision. lua-bitstring seems to be a lot easier if
>     it can be made to work.
>
>
> The syntax for that looks nice to work with for packing and unpacking of
> definable length fields.
>
> Depending on how it was written it might not be too hard to compile that
> as a module for eLua if it's generally ANSI C + the Lua API, but there
> might be other issues with its memory usage assumptions or total code
> size.  It does look like a nice way to pack data though.
>
> With the current modules though you'd be looking at using the bit module
> with shifts or some abstraction on top of that.
>
>
>     Also, from a quick look at the SPI code it seems that if you pass a
>     number as in your example, it will use lua_tointeger on the number and
>     send that. It then seems from luaconf.h that the actual number of bits
>     sent would depend on the platform or how lua is compiled. I suspect if I
>     need to control the number of bytes sent I would need to send the data
>     as a string.
>
>
> The actual SPI word size will be defined when you call spi.setup, but
> you're correct that the integer size defined at compile time would need
> to be large enough to hold each word.
>
> As far as I know all of our platforms will have a lua integer size of
> 32-bits (a long) which is plenty for the word sizes most of the SPI
> interfaces support of 8 and 16-bits. You can check individual platforms
> in the type.h file.
>
> I believe what should happen on each platform is that the lowest
> wordsize bits of the "data" parameter will be used out of the size of
> the number passed.  This will be regardless of the native Lua type which
> could be be double-precision floating point or 32-bit/64-bit integer.
>
> I took a quick look at the string approach and that looks like it goes
> byte-by-byte so that may actually only work with 8-bit SPI wordsize.  We
> should probably do something about that or document it unless I've
> misinterpreted something.
>
> I haven't  investigated ecdr's bitarrays suggestion
> (https://github.com/elua/elua/blob/master/src/modules/bitarray.c) but I
> think that has a fixed field size?
>
>
>
>     Let me know if I have this wrong or if you have any thoughts.
>
>     Thanks,
>     --
>     Richard Graham
>
>
>     On 11/18/2013 10:51 AM, James Snyder wrote:
>     > There is a bit module included with eLua but it's actually based on
>     > bitlib and the API is a little different:
>     > http://www.eluaproject.net/doc/master/en_refman_gen_bit.html
>     >
>     >
>     > On Mon, Nov 18, 2013 at 6:48 AM, Thiago Naves
>     <[hidden email] <mailto:[hidden email]>
>     > <mailto:[hidden email] <mailto:[hidden email]>>>
>     wrote:
>     >
>     >     Hi Richard,
>     >     You can use the bit module to do the usual bit operations (or,
>     and,
>     >     xor, ...):
>     >     http://bitop.luajit.org/api.html
>     >
>     >     To assemble multiple bytes, you could do
>     >     a = 3
>     >     b = 5
>     >     c = a..b
>     >     and have c = "35"
>     >     or spi.write(0, a, b)
>     >
>     >     --
>     >     Thiago
>     >
>     >
>     >     On Mon, Nov 18, 2013 at 4:06 AM, Richard Graham <[hidden email]
>     >     <mailto:[hidden email] <mailto:[hidden email]>>> wrote:
>     >
>     >         Hi elua people,
>     >
>     >         I am just getting started with elua. Looks pretty nice so far.
>     >
>     >         I need to pack together data to make some commands for setting
>     >         various
>     >         registers on a device connected via SPI. I need to be able
>     to set
>     >         individual bits and also pack 8 and 16 bit numbers. This seems
>     >         like it
>     >         should be a common problem and I was wondering what the usual
>     >         approach
>     >         to this is.
>     >
>     >         I see the 'pack' module, though it looks like it only supports
>     >         packing
>     >         whole bytes at a minimum. Packing and unpacking at the
>     >         individual bit
>     >         level is going to get messy.
>     >
>     >         I have found lua-bitstring, which looks like it should be
>     great
>     >         for this
>     >         purpose -- though I can hardly find any documentation or
>     others
>     >         using
>     >         it. I was wondering if anyone has managed to use this in
>     elua? Or is
>     >         there something else I am missing?
>     >
>     >         Thanks,
>     >         --
>     >         Richard
>     >         _______________________________________________
>     >         eLua-dev mailing list
>     >         [hidden email]
>     <mailto:[hidden email]> <mailto:[hidden email]
>     <mailto:[hidden email]>>
>     >         https://lists.berlios.de/mailman/listinfo/elua-dev
>     >
>     >
>     >
>     >     _______________________________________________
>     >     eLua-dev mailing list
>     >     [hidden email] <mailto:[hidden email]>
>     <mailto:[hidden email] <mailto:[hidden email]>>
>     >     https://lists.berlios.de/mailman/listinfo/elua-dev
>     >
>     >
>     >
>     >
>     > --
>     > James Snyder
>     > Biomedical Engineering
>     > Northwestern University
>     > ph: <a href="tel:%28847%29%20448-0386" value="+18474480386">(847) 448-0386 <tel:%28847%29%20448-0386>
>     >
>     >
>     > _______________________________________________
>     > eLua-dev mailing list
>     > [hidden email] <mailto:[hidden email]>
>     > https://lists.berlios.de/mailman/listinfo/elua-dev
>     >
>
>     _______________________________________________
>     eLua-dev mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://lists.berlios.de/mailman/listinfo/elua-dev
>
>
>
>
> --
> James Snyder
> Biomedical Engineering
> Northwestern University
> ph: <a href="tel:%28847%29%20448-0386" value="+18474480386">(847) 448-0386
>
>
> _______________________________________________
> eLua-dev mailing list
> [hidden email]
> https://lists.berlios.de/mailman/listinfo/elua-dev
>

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev



--
James Snyder
Biomedical Engineering
Northwestern University
ph: (847) 448-0386

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
scdr scdr
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

In reply to this post by Richard Graham

For what it's worth, I drafted a little documentation for bitarray a while ago.
(Not much more extensive than James' list below.)
I just pushed it to my eLua repository, http://github.com/ecdr/elua
it is in the BitarrayDoc branch.
(I have not tested it yet, i.e. have not set up document building tools to see
if it will generate web page correctly.  Also haven't done spell check.)



> ----- Original Message -----
> From: James Snyder
> Sent: 11/19/13 03:40 PM
> To: eLua Users and Development List (www.eluaproject.net)
> Subject: Re: [eLua-dev] assembling commands for SPI
>
> On Tue, Nov 19, 2013 at 4:28 PM, Richard Graham <[hidden email]> wrote:
>
> > Hi,
> >
> > Thanks for that. I think I should be able to manage with just the bit
> > module.
> >
> > I didn't see the bitarrays module before. Looks like it might work,
> > though I couldn't find any documentation about this and the source is
> > not that easy to read at a quick look. Presumably this module is used
> > internally somewhere?
> >
>
> Not sure if we use it internally anywhere, it may just not have gotten
> documented. Here's one example of its usage to take a binary image put it
> in a bit array and then rotate it to be shown on an OLED display:
> http://wiki.eluaproject.net/Logo
>
> Here's a first pass at the API just from skimming the source a bit:
>
> arrays are created either with a specific capacity, a source byte array
> (string) or an existing Lua array, packed into a uniform element size array
> // Lua: array = bitarray.new( capacity, [element_size_bits], [fill] ), or
> // array = bitarray.new( "string", [element_size_bits] ), or
> // array = bitarray.new( lua_array, [element_size_bits] )
>
> fields can be indexed like a lua table:
> // Lua: value = array[ idx ]
>
> can be assigned like a table:
> // Lua: array[ key ] = value
>
> get length similar to how one would do so for a string:
> // Lua : size = #array
>
> arrays can be converted into a string. not sure what raw/seq means
> // Lua: string = bitarray.tostring( array, ["raw"|"seq"] )
>
> or a table (again, not sure about raw/seq):
> // Lua: table = bitarray.totable( array, ["raw"|"seq"] )
>
> And it looks like you can use bitarray.pairs which looks like it should
> behave like the built-in Lua pairs.
>
>
>
> > Another thing, I was wondering if anyone has compiled the elua modules
> > so they can be used with lua on the desktop? I would prefer to do most
> > of the development on the desktop but since some libraries are
> > different, i.e. the bit and bitlib api that is not to easy.
> >
>
> One quick way that should work is to build the luarpc desktop binary as
> described here:
> http://www.eluaproject.net/doc/master/en_using.html#rpc
>
> If you already have the build environment for using the lua-based build
> system and you have compilers on your system to build native binaries you
> should be able to just run:
> lua rpc-lua.lua
>
> When you run the resulting binary (luarpc) you'll get a Lua REPL and the
> binary should include the modules like bit/pack/bitarray.
>
> Best.
>
> -jsnyder
>
>

>
>
>
> --
> James Snyder
> Biomedical Engineering
> Northwestern University
> ph: (847) 448-0386

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Richard Graham Richard Graham
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

Hi,

Thank you for your replies and the documentation. Looks like a useful
package.

Regarding using the luarpc for local development; I was able to build it
fine and use the bit and pack modules; however I noticed it doesn't load
any of the lua modules such as string and math. Is this by design? It
seems the module code is compiled but can't be used. Anyway, I found I
was able to hack my desktop_conf.h so that these are loaded and now it
works fine. I can't imagine this is the best way to do it though.

Thanks,
--
Richard Graham

On 11/20/2013 11:13 PM, [hidden email] wrote:

>
> For what it's worth, I drafted a little documentation for bitarray a while ago.
> (Not much more extensive than James' list below.)
> I just pushed it to my eLua repository, http://github.com/ecdr/elua
> it is in the BitarrayDoc branch.
> (I have not tested it yet, i.e. have not set up document building tools to see
> if it will generate web page correctly.  Also haven't done spell check.)
>
>
>
>> ----- Original Message -----
>> From: James Snyder
>> Sent: 11/19/13 03:40 PM
>> To: eLua Users and Development List (www.eluaproject.net)
>> Subject: Re: [eLua-dev] assembling commands for SPI
>>
>> On Tue, Nov 19, 2013 at 4:28 PM, Richard Graham <[hidden email]> wrote:
>>
>>> Hi,
>>>
>>> Thanks for that. I think I should be able to manage with just the bit
>>> module.
>>>
>>> I didn't see the bitarrays module before. Looks like it might work,
>>> though I couldn't find any documentation about this and the source is
>>> not that easy to read at a quick look. Presumably this module is used
>>> internally somewhere?
>>>
>>
>> Not sure if we use it internally anywhere, it may just not have gotten
>> documented. Here's one example of its usage to take a binary image put it
>> in a bit array and then rotate it to be shown on an OLED display:
>> http://wiki.eluaproject.net/Logo
>>
>> Here's a first pass at the API just from skimming the source a bit:
>>
>> arrays are created either with a specific capacity, a source byte array
>> (string) or an existing Lua array, packed into a uniform element size array
>> // Lua: array = bitarray.new( capacity, [element_size_bits], [fill] ), or
>> // array = bitarray.new( "string", [element_size_bits] ), or
>> // array = bitarray.new( lua_array, [element_size_bits] )
>>
>> fields can be indexed like a lua table:
>> // Lua: value = array[ idx ]
>>
>> can be assigned like a table:
>> // Lua: array[ key ] = value
>>
>> get length similar to how one would do so for a string:
>> // Lua : size = #array
>>
>> arrays can be converted into a string. not sure what raw/seq means
>> // Lua: string = bitarray.tostring( array, ["raw"|"seq"] )
>>
>> or a table (again, not sure about raw/seq):
>> // Lua: table = bitarray.totable( array, ["raw"|"seq"] )
>>
>> And it looks like you can use bitarray.pairs which looks like it should
>> behave like the built-in Lua pairs.
>>
>>
>>
>>> Another thing, I was wondering if anyone has compiled the elua modules
>>> so they can be used with lua on the desktop? I would prefer to do most
>>> of the development on the desktop but since some libraries are
>>> different, i.e. the bit and bitlib api that is not to easy.
>>>
>>
>> One quick way that should work is to build the luarpc desktop binary as
>> described here:
>> http://www.eluaproject.net/doc/master/en_using.html#rpc
>>
>> If you already have the build environment for using the lua-based build
>> system and you have compilers on your system to build native binaries you
>> should be able to just run:
>> lua rpc-lua.lua
>>
>> When you run the resulting binary (luarpc) you'll get a Lua REPL and the
>> binary should include the modules like bit/pack/bitarray.
>>
>> Best.
>>
>> -jsnyder
>>
>>
>
>>
>>
>>
>> --
>> James Snyder
>> Biomedical Engineering
>> Northwestern University
>> ph: (847) 448-0386
>
> _______________________________________________
> eLua-dev mailing list
> [hidden email]
> https://lists.berlios.de/mailman/listinfo/elua-dev
>

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
jbsnyder jbsnyder
Reply | Threaded
Open this post in threaded view
|

Re: assembling commands for SPI

I've added those back in the desktop_conf.h (along with some other modules that would normally be included).

Best.

-jsnyder


On Thu, Nov 21, 2013 at 1:05 PM, Richard Graham <[hidden email]> wrote:
Hi,

Thank you for your replies and the documentation. Looks like a useful
package.

Regarding using the luarpc for local development; I was able to build it
fine and use the bit and pack modules; however I noticed it doesn't load
any of the lua modules such as string and math. Is this by design? It
seems the module code is compiled but can't be used. Anyway, I found I
was able to hack my desktop_conf.h so that these are loaded and now it
works fine. I can't imagine this is the best way to do it though.

Thanks,
--
Richard Graham

On 11/20/2013 11:13 PM, [hidden email] wrote:
>
> For what it's worth, I drafted a little documentation for bitarray a while ago.
> (Not much more extensive than James' list below.)
> I just pushed it to my eLua repository, http://github.com/ecdr/elua
> it is in the BitarrayDoc branch.
> (I have not tested it yet, i.e. have not set up document building tools to see
> if it will generate web page correctly.  Also haven't done spell check.)
>
>
>
>> ----- Original Message -----
>> From: James Snyder
>> Sent: 11/19/13 03:40 PM
>> To: eLua Users and Development List (www.eluaproject.net)
>> Subject: Re: [eLua-dev] assembling commands for SPI
>>
>> On Tue, Nov 19, 2013 at 4:28 PM, Richard Graham <[hidden email]> wrote:
>>
>>> Hi,
>>>
>>> Thanks for that. I think I should be able to manage with just the bit
>>> module.
>>>
>>> I didn't see the bitarrays module before. Looks like it might work,
>>> though I couldn't find any documentation about this and the source is
>>> not that easy to read at a quick look. Presumably this module is used
>>> internally somewhere?
>>>
>>
>> Not sure if we use it internally anywhere, it may just not have gotten
>> documented. Here's one example of its usage to take a binary image put it
>> in a bit array and then rotate it to be shown on an OLED display:
>> http://wiki.eluaproject.net/Logo
>>
>> Here's a first pass at the API just from skimming the source a bit:
>>
>> arrays are created either with a specific capacity, a source byte array
>> (string) or an existing Lua array, packed into a uniform element size array
>> // Lua: array = bitarray.new( capacity, [element_size_bits], [fill] ), or
>> // array = bitarray.new( "string", [element_size_bits] ), or
>> // array = bitarray.new( lua_array, [element_size_bits] )
>>
>> fields can be indexed like a lua table:
>> // Lua: value = array[ idx ]
>>
>> can be assigned like a table:
>> // Lua: array[ key ] = value
>>
>> get length similar to how one would do so for a string:
>> // Lua : size = #array
>>
>> arrays can be converted into a string. not sure what raw/seq means
>> // Lua: string = bitarray.tostring( array, ["raw"|"seq"] )
>>
>> or a table (again, not sure about raw/seq):
>> // Lua: table = bitarray.totable( array, ["raw"|"seq"] )
>>
>> And it looks like you can use bitarray.pairs which looks like it should
>> behave like the built-in Lua pairs.
>>
>>
>>
>>> Another thing, I was wondering if anyone has compiled the elua modules
>>> so they can be used with lua on the desktop? I would prefer to do most
>>> of the development on the desktop but since some libraries are
>>> different, i.e. the bit and bitlib api that is not to easy.
>>>
>>
>> One quick way that should work is to build the luarpc desktop binary as
>> described here:
>> http://www.eluaproject.net/doc/master/en_using.html#rpc
>>
>> If you already have the build environment for using the lua-based build
>> system and you have compilers on your system to build native binaries you
>> should be able to just run:
>> lua rpc-lua.lua
>>
>> When you run the resulting binary (luarpc) you'll get a Lua REPL and the
>> binary should include the modules like bit/pack/bitarray.
>>
>> Best.
>>
>> -jsnyder
>>
>>
>
>>
>>
>>
>> --
>> James Snyder
>> Biomedical Engineering
>> Northwestern University
>> ph: <a href="tel:%28847%29%20448-0386" value="+18474480386">(847) 448-0386
>
> _______________________________________________
> eLua-dev mailing list
> [hidden email]
> https://lists.berlios.de/mailman/listinfo/elua-dev
>

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev



--
James Snyder
Biomedical Engineering
Northwestern University
ph: (847) 448-0386

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev