Squirrel language?

Status
Not open for further replies.

Camel

Well-known member
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?
 
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.
 
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.
 
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?
 
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.
 
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")
 
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.
 
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?
 
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:
Status
Not open for further replies.
Back
Top