Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: Jim Bauwens on July 14, 2011, 04:29:29 pm

Title: Controlling external hardware through Lua
Post by: Jim Bauwens on July 14, 2011, 04:29:29 pm
Recently I got on the idea that the Lua print command in Nspire Lua might print to the serial console. To test this, I built a max232 TTL converter and hooked my nspire to my laptop. And yes, it printed to the serial console!
Then I ordered an Arduino Uno, with the idea to hook it up to my nspire and control stuff.

A few days ago I received it and I started working. I had some problems until today because the the voltage on serial port of the arduino was to high for my nspire, and caused a reset everytime I connected it. Today I discovered (with the help of Kerm and benryves) that its only does that when its getting its power through usb, and not the power connector. So I switched to another power supply and all was fine :)

The print command in Nspire has some limitations though, you can only send bytes with data 1 to 127, and its also not the only data that gets send to the serial port. To overcome these problems, I made a little simple protocol to talk with my arduino. A sample data packet (in ascii) looks like this:
Code: [Select]
nomOKOCEHONOKOFOOOO@BDFON\r\n"nom" defines the start of a data packets, and "\r\n" the end (it gets automatic added with print). 2 chars represent one byte that has been split in two (to be able to send 0-255).

My first real world example is controlling a little led display through my nspire. The arduino here just acts as a "proxy" to send the data to the display.

(http://bwns.be/jim/luaserial1.jpg)

(http://bwns.be/jim/luaserial2.jpg)


Sadly though I can send data (yet) from the arduino to my nspire, but if I do some work on reverse engineering the other dock connector pins, I think I'll be able to find a solution.

Edit:
Title: Re: Controlling external hardware through Lua
Post by: ztrumpet on July 14, 2011, 04:33:40 pm
Wow, that's awesome, Jim.  Great job. :D
Title: Re: Controlling external hardware through Lua
Post by: ruler501 on July 14, 2011, 04:57:39 pm
Wow just wow. This deserves a news. It is great news for nspire development.
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 14, 2011, 05:00:28 pm
Thanks both :)

I thinks it pretty cool because people can do really cool things with it :D
Title: Re: Controlling external hardware through Lua
Post by: ruler501 on July 14, 2011, 05:08:22 pm
Oh yeah and we don't even need C for this. I'm just amazed at the wonders this community can do
Title: Re: Controlling external hardware through Lua
Post by: renatose on July 14, 2011, 05:17:57 pm
this is good stuff, this is realy wonderful, I'm looking forward to some amazing things being done around this...
Title: Re: Controlling external hardware through Lua
Post by: Munchor on July 14, 2011, 05:35:43 pm
Great job Jim ;) I'd love a video :)
Title: Re: Controlling external hardware through Lua
Post by: ruler501 on July 14, 2011, 05:36:51 pm
We need to start our nspire controlled robot army lol

I agree with ephan could we see a video? What do you think you'll be able to do with this?
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 15, 2011, 04:00:08 am
I'll try to make a video as soon as possible :)

Quote
What do you think you'll be able to do with this?
Well, with the led display I just can display some text, kinda limited. But I can hook other things up to my arduino (an lcd for example) that can do much more. You could make little robots with it, but you just have the problem that you can't send anything back to the nspire. I'm trying to find a solution for this now.
Title: Re: Controlling external hardware through Lua
Post by: Juju on July 15, 2011, 04:11:28 am
Well, that's awesome Jim :D
Title: Re: Controlling external hardware through Lua
Post by: ExtendeD on July 15, 2011, 05:12:03 am
Nice idea jim :)
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 15, 2011, 06:06:43 am
Thanks :)

I added a video to the top post. Its not the most clear one though, but you should get the idea :)
Title: Re: Controlling external hardware through Lua
Post by: Netham45 on July 15, 2011, 06:10:13 am
Hook a better LCD up to the older Nspires. :D
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 15, 2011, 06:13:11 am
I might try to control a AV display, as other people have done that already with an arduino.
Title: Re: Controlling external hardware through Lua
Post by: ruler501 on July 15, 2011, 08:52:30 am
nice. I hope you figure out the input soon. Good luck
Title: Re: Controlling external hardware through Lua
Post by: cypressx1 on July 15, 2011, 09:09:22 am
Really good job.
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 15, 2011, 01:21:44 pm
Answer to Netham45:
The stuff that took so long to type was: 'while 1 do led("I") sleep(1) led("<3") sleep(1) led("Omnimaga") sleep(3) end'.
Its not the most easy thing to do while filming the whole thing, and typing with one hand ;)
Title: Re: Controlling external hardware through Lua
Post by: pianoman on July 15, 2011, 01:50:23 pm
Holy cow!
Amazing, Jim! :D
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 17, 2011, 11:55:22 am
In case anyone wants it, you can find the Lua and the C (for the arduino) source code here: http://bwns.be/jim/arduino-ti.zip
Title: Re: Controlling external hardware through Lua
Post by: Munchor on July 24, 2011, 05:09:38 pm
In case anyone wants it, you can find the Lua and the C (for the arduino) source code here: http://bwns.be/jim/arduino-ti.zip

Arduino takes Processing as code :P Not C? Or can it also take C? I'm pretty sure it's Processing though.
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 25, 2011, 01:29:02 am
Well, its Processing, but I think its valid C anyway :p
Title: Re: Controlling external hardware through Lua
Post by: Munchor on July 25, 2011, 07:53:07 am
Well, its Processing, but I think its valid C anyway :p

It's Processing then, and save it as .pde, not as .c :P
Title: Re: Controlling external hardware through Lua
Post by: pianoman on July 28, 2011, 12:41:12 pm
This is a bit off topic, but if you can control external hardware, can you also control external files on the calculator?
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 28, 2011, 01:05:48 pm
If you mean files on the local calculator, you can access libraries with Lua.
If you mean something like an sdcard, no, because we can't get yet hardware input .
There is though a chance that you can connect an sdcard to the dock, but more research has to be done on it.
Title: Re: Controlling external hardware through Lua
Post by: pianoman on July 29, 2011, 11:47:42 am
I meant can you get data from other lua files on the local calculator (i.e. level data, high scores, etc.)?
Title: Re: Controlling external hardware through Lua
Post by: ztrumpet on July 29, 2011, 12:06:25 pm
I know I'm a bit late, but wow.  That video is epic. :D
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 29, 2011, 12:33:26 pm
Thanks :D
Title: Re: Controlling external hardware through Lua
Post by: Adriweb on July 30, 2011, 01:11:02 am
I meant can you get data from other lua files on the local calculator (i.e. level data, high scores, etc.)?

Not really, the data has to be in the same .tns file, and then get retrieved with var.recall (http://wiki.inspired-lua.org/var.recall), which grabs a Maths activity variable and puts in in a Lua variable
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 30, 2011, 01:19:40 am
Adriweb, you can also use math.eval to execute a function in a library document (not in the current tns file) and return stuff saved there.
Title: Re: Controlling external hardware through Lua
Post by: Adriweb on July 30, 2011, 01:29:01 am
yep, but I think that if you have a lot of calculations to do, importing the variables in Lua then do all your calculations within lua (and then var.store() the final result) is much faster than doing all the calculations in a math.eval() call, as it's calling the math 'server' (as they call it) everytime ...

EDIT : but I think it's fine if you have only a few calculations to make
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on July 30, 2011, 01:32:50 am
Yeah, that is very true :)
Title: Re: Controlling external hardware through Lua
Post by: apcalc on July 31, 2011, 03:10:16 pm
That looks great, I wish I was as skilled with external hardware, as it looks to be quite interesting! :D
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on August 02, 2011, 01:48:13 am
Thanks apcalc :)
Title: Re: Controlling external hardware through Lua
Post by: ExtendeD on February 18, 2012, 02:47:57 am
jimbauwens, may be you would be interested by a native Lua extension for this, without the original print() limitations?
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on February 18, 2012, 04:38:23 am
ExtendeD, you don't know how much I was researching after I heard the news :D
The thing is with the file functions in os.h I can read/write to the serial port (uber awesome). But when reading stdin, it will block until there is data.
And since I can not check if there is data some way, I can't use this method.
There are two possibilities for me:
1) Directly talk to the serial hardware
2) Make stdin non blocking

2 can be done with fcntl() (from fcntl.h), and since it's a posix function it might be in the Nspire. But finding it is another task ^^.

If you have suggestions, please post them :D
Title: Re: Controlling external hardware through Lua
Post by: ExtendeD on February 18, 2012, 07:53:02 am
You should try low-level serial port access, it shouldn't be too difficult.
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on February 19, 2012, 09:02:25 am
Okay, I'll look at that.
But I have another question: is the method of reading the serial port the same on CX and classic?
(Since they use other uart hardware)
Title: Re: Controlling external hardware through Lua
Post by: compu on February 19, 2012, 10:37:21 am
No, they use different registers.
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on February 19, 2012, 02:14:57 pm
Okay, thanks for the information.
When I got some time I'll try to use this method.
Title: Re: Controlling external hardware through Lua
Post by: DJ Omnimaga on February 19, 2012, 02:30:24 pm
Sound via Ndless is possible, right?
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on February 19, 2012, 02:35:51 pm
Yes, it is :)
And now we can create modules that make it possible in Lua (and even better quality!) :)
Title: Re: Controlling external hardware through Lua
Post by: DJ Omnimaga on February 19, 2012, 02:37:18 pm
Ok that's good to hear then. I wonder, since the processor is 150 MHz, if you could have sound as complex as this?



Or even midi-based sound like in Doom?
Title: Re: Controlling external hardware through Lua
Post by: Jim Bauwens on February 19, 2012, 02:41:46 pm
Maybe, but I'm not sure.
Right now I don't have time to make a player, but if no one else does it, I'll make one.