Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 17 of 17

Thread: Squirrel language?

  1. #1

    Squirrel language?

    A few months ago I managed to compile a very minimalistic squirrel virtual machine on the Teensy 3.1. The only function that I implemented was 'print()' and then I got distracted by other projects. The squirrel language is pretty awesome though, and this VM on a Teensy idea is something I'm thinking about working on a bit more.

    The idea is that the VM runs on the Teensy and loads the bytecode program from an SD card. The actual script would be compiled into bytecode on a computer and then transferred to the SD card. I should mention the next Teensy is rumoured to have a uSD card slot.

    Advantages are
    • Size of bytecode / program is limited by size of SD card rather than FLASH
    • Given that the script loads from an SD card, the Squirrel script will have access to a file system.
    • No difficult compiler toolchains.
    • Squirrel has cooperative threads and memory management built in.
    • Squirrel is fast and is real-time enough for applications like video games (and possibly time critical control applications).
    • The interpreter is tiny and there is heaps of RAM left to have a huge (32KB maybe?) VM stack.


    The biggest hurdle of course is building Arduino functions into the VM. That'll be a huge task. Would the Teensy even have enough flash to handle a VM with all the popular libraries built in? Which libraries should be built into the VM and which ones could be implemented in bytecode?

    So really, before I start getting into this, I'm hoping to get some feedback on feasibility. Questions or feedback anyone?

  2. #2
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    The microPython port to Teensy might give a clue. That's a bytecode interpreter of course.
    As I recall reading here, the majority of Python 3 core is running and that's a lot, plus 6 or so popular libraries. That took up something like 80 or 90% of the flash on the Teensy 3.
    The "real" micropython port to the board of that name, starts with at least 1MB of flash.

    But if squirrel doesn't have the esoterics of Phython 3 (dictionaries, tuples, run time types, garbage collection, etc), maybe it'd be a better fit.

  3. #3
    The squirrel VM I've compiled takes up about 63% on a 3.1, and that includes the SdFat library and the Teensy core too of course. So by the looks it's actually bigger! Oh well, I expect the next Teensy will have stacks more flash.

    I've started to build in some generic Arduino functions now. Doing this port is actually a very simple process. I reckon I'll have a functioning blinky example by this evening.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,829
    I'd never heard of the Squirrel programming language before....

  5. #5
    Woo! I've got a blinky script working. Here's the script

    Code:
    pinMode(13, 1);
    
    while(true)
    {
    	print(millis());
    	print("\n");
    	digitalWrite(13, true);
    	delay(200);
    	digitalWrite(13, false);
    	delay(200);
    }
    At first glance, it's almost exactly like normal Arduino programming - only interpreted. Should I make a github for this?

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,829
    Quote Originally Posted by Camel View Post
    Should I make a github for this?
    Yes, please do!

  7. #7
    I nearly went with TeensyNuts for the project name, because a squirrel script is a 'nut'. But I think I'll go with ArduNuts instead.

    *edit: Nah stuff it, TeensyNuts is a much better name. Pun intended.
    https://github.com/supercamel/TeensyNuts
    Last edited by Camel; 09-28-2014 at 12:01 AM.

  8. #8
    Senior Member
    Join Date
    Sep 2013
    Location
    Hamburg, Germany
    Posts
    894
    So we could write scripts, store them on an SD card, and have them run by the teensy? That's how I understand the main.cpp code.

  9. #9
    Quote Originally Posted by christoph View Post
    So we could write scripts, store them on an SD card, and have them run by the teensy? That's how I understand the main.cpp code.
    Yes, that's it.

  10. #10
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Being unlearned in the language squirrel... I looked at blinky.nut. It looks like C to me. Huh?

  11. #11
    Quote Originally Posted by stevech View Post
    Being unlearned in the language squirrel... I looked at blinky.nut. It looks like C to me. Huh?
    It does, doesn't it? But its quite different. It's not a strongly typed language like C. I'm still learning it myself and so far I think it'd be best described as Python with C++ styled syntax. This is the coroutines example.

    Code:
    function coroutine_test(a,b)
    {
    	::print(a+" "+b+"\n");
    	local ret = ::suspend("suspend 1");
    	::print("the coroutine says "+ret+"\n");
    	ret = ::suspend("suspend 2");
    	::print("the coroutine says "+ret+"\n");
    	ret = ::suspend("suspend 3");
    	::print("the coroutine says "+ret+"\n");
    	return "I'm done"
    }
    
    local coro = ::newthread(coroutine_test);
    
    local susparam = coro.call("test","coroutine"); //starts the coroutine
    
    local i = 1;
    do
    {
    	::print("suspend passed ["+susparam+"]\n")
    	susparam = coro.wakeup("ciao "+i);
    	++i;
    }while(coro.getstatus()=="suspended")
    
    ::print("return passed ["+susparam+"]\n")

  12. #12
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Where is squirrel's reference document?
    I like Python's tuples

  13. #13
    Quote Originally Posted by stevech View Post
    Where is squirrel's reference document?
    I like Python's tuples
    http://squirreldoc.neonapple.com/v3/frame.html

    The reference isn't that great IMO. I think if I were to push on with this project I'd have to create my own language reference, as well as a reference for the Arduino-style functions.

  14. #14
    Senior Member mortonkopf's Avatar
    Join Date
    Apr 2013
    Location
    London, uk
    Posts
    912
    Electric Imp has some info on porting C++ (arduino) to squirrel:
    http://electricimp.com/docs/resources/arduinoport/

    but relates this to imp API functions / methods
    Last edited by mortonkopf; 09-24-2014 at 08:08 AM.

  15. #15
    Senior Member
    Join Date
    Sep 2013
    Location
    Hamburg, Germany
    Posts
    894
    Electric Imp has some info on porting C++ (arduino) to squirrel:
    That only explains how to use an API that previously was written in C++ and has already been ported to squirrel, but not the steps needed to actually make something known to squirrel - that's the harder part. And
    • Remove variable type declarations from the code
    • BUT do take note of the Arduino variable types - it may affect the outcome of the program

    made me chuckle. Nonetheless, I'll try squirrel. Has anyone made any comparisons between python and squirrel, speed- and memory-wise? AFAIK there's microPython out there.

  16. #16
    OK, I've gotten the Serial library working. It's not complete - only USB-Serial works, not the UARTs yet. Making functions available to the VM is clearly documented in the Squirrel manuals, but it took me ages to work out how to create a class in Squirrel. I'm still not sure if what I've done is the most elegant approach. Nonetheless, it works, and if anyone is in looking over it and perhaps working on adding more libraries I'd very much appreciate it.

    I'm also starting to realise that it's a huge pain in the **** to have to keep swapping around SD cards, compile scripts, copy files and so on. I'm thinking about making a simple serial interface that allows you to upload files, run scripts, select a script to run automatically at start up and so on. I'm thinking it should have a nice desktop app with a GUI which can automatically compile and upload scripts. Thoughts anyone?

  17. #17
    Member neilh20's Avatar
    Join Date
    Jan 2014
    Location
    N California
    Posts
    54
    I did an EI squirrel RS485 ModBus interface for the hw Imp (electricImp.com) and have had it running in prototype form for a year.
    I would guess there is far amount of machine specific implementation, but here is my EI code
    https://github.com/neilh10/ei_modbus
    The nut lives on the IMP001 and the agent is on the internet.
    The nice thing about the EI is its fast development, all interface is through a cloud interface, SD size package including WiFi but you have to live with its very constrained environment - 6 pins on IMP001, no onboard data flash storage, only reports to a cloud based Agent side. The IMPS use a constrained STM32 ARM implementation
    The cloud IDE interface also begins to suck when doing serious programming, and the method of programming the WiFi SSD is painful.

    I used it to test the accuracy of a specific RS485 sensor and compare it against some other sensors.
    https://thingspeak.com/channels/7282

    Overall, learning another languages peculiarities was painful.
    To implement it on the teensy3 might be fun, but I wonder how many people are bumping into the flash limitations.
    I believe EI used it so as to be able to provide a managed scaleable IoT environment for selling its programmable "pluggable SD flash card format with WiFi device connectivity" - the SD card has a small number of real world hw pins programmable with Squirrel .nuts, then two way comms over embedded WiFi link that punches out of firewalls to a cloud agent, and then needs something on the cloud to catch it.
    Last edited by neilh20; 10-05-2014 at 06:56 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •