I've written a JavaScript interpreter which runs on a VERY cheap board, the STM32VLDISCOVERY. It's currently £8 from Farnell and it's got bags of IO: http://uk.farnell.com/stmicroelectronics/stm32vldiscovery/stm32-value-line-discovery-kit/dp/2118806
It's all 3.3v, so no problem connecting it with the Pi.
The interpreter is here:
http://www.pur3.co.uk/espruino/
Just follow the instructions there to put it onto the STM32VLDISCOVERY board, then use the instructions below. Most stuff in 'code' boxes needs to be copy and pasted to a terminal:
We need to stop the Raspberry PI trying to use its serial port:
- Code: Select all
sudo nano /boot/cmdline.txt
Remove the text 'console=ttyAMA0,115200' and 'kgdboc=ttyAMA0,115200'. Press Ctrl+X to exit, then Y then enter to confirm.
- Code: Select all
sudo nano /etc/inittab
Remove the lines (down the bottom):
- Code: Select all
#Spawn a getty on Raspberry Pi serial line
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Press Ctrl+X to exit, then Y then enter to confirm.
Reboot:
- Code: Select all
sudo shutdown -r now
Then install a terminal app to test with:
- Code: Select all
sudo apt-get install minicom
And finally, we want to set our user up so that we can access the serial port without having to type 'sudo'. This allows us to 'dialout' of the Raspberry Pi:
- Code: Select all
sudo usermod -a -G dialout pi
And run it:
- Code: Select all
minicom -o -b 9600 -D /dev/ttyAMA0
Connect up the STM32VLDISCOVERY board:
- Code: Select all
PI STM32
P1-04 5V
P1-06 GND
P1-08 PA10
P1-10 PA9
For the locations, see: http://elinux.org/RPi_Low-level_peripherals
Note: On the Pi, P1-04/6/8/10 are 4 pins next to each other, down the long edge of the board. P1-02 is the pin right in the corner (don't use that one!) and P1-04 is the one next to it, P1-06 is the one after that, etc.
Now when you hit the black reset button on the STM32 board, you should see some text appear saying 'Espruino' in big letters. Sorted.
You can turn digital pins on and off using a command like digitalWrite(pin, value) - eg, writing digitalWrite("C9", 1); will turn the LED on.
Or copy and pasting the following will flash it 5 times every time you press the blue button:
- Code: Select all
setWatch(function() {
if (!digitalRead("A0")) return;
var led=10;
var flasher = setInterval(function() {
led--;
digitalWrite("C9",led&1);
if (led<=0) clearInterval(flasher);
}, 200);
}, "A0", true);
You can write complete JavaScript code in it - see the website http://www.pur3.co.uk/espruino/ for other stuff you can put in.
To exit the terminal app, press Ctrl-A then X, then hit enter to confirm.
So now you might want to actually control some IO from a program.
Set up serial port:
- Code: Select all
stty -F /dev/ttyAMA0 9600 -parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts ignbrk raw min 0 time 5
(note that raw,min + time 5 are important - they make cat only wait for 1/2 a sec for a reply)
Turn LED on:
- Code: Select all
echo -ne 'digitalWrite("C9",1);\r' > /dev/ttyAMA0
Turn LED off:
- Code: Select all
echo -ne 'digitalWrite("C9",0);\r' > /dev/ttyAMA0
Turn LED on, then off 1 second later - note that it returns immediately!:
- Code: Select all
echo -ne 'digitalWrite("C9",1);setTimeout("digitalWrite(\"C9\",0);",1000);\r' > /dev/ttyAMA0
You can also read inputs (for instance the button) with digitalRead("A0"), as well as Analog inputs (on the A port) using analogRead("A0"):
first - to set it up not to send any 'extra' data back:
- Code: Select all
echo -ne 'echo(false);\r' > /dev/ttyAMA0
and then:
- Code: Select all
echo -ne 'setTimeout("print(digitalRead(\"A0\"))",100);\r' > /dev/ttyAMA0;cat /dev/ttyAMA0
This is particularly nasty - can anyone think of a better way?
As you can run JavaScript while the Pi is off doing something else, you can also do stuff like this:
Every 10th of a second, read an analog value, and store it in a running average:
- Code: Select all
echo -ne 'echo(false);var a=0;var n=0;setInterval("a+=analogRead(\"A0\");n++;",100);\r' > /dev/ttyAMA0
Then at any point after, do this to read the average and zero it:
- Code: Select all
echo -ne 'setTimeout("print(a/n);a=0;n=0;",100);\r' > /dev/ttyAMA0;cat /dev/ttyAMA0
Try running the last command a few times, with/without the blue button pressed.
So you could do it as a cron job, and read what the average has been over the last hour...
Anyway, hope that's interesting to someone - let me know what you think!
I'd like to find some way to get support for this so I can document it better, improve the interpreter and port it to more boards.