Program Flow

Excuse my ignorance… but this is baffling me.

Typically doing C on an embedded system the entry point is alway main(). I guess in squirrel, it’s the top of the file ? However, once it gets to the bottom of the script what happens…Does execution just end ?

Take this example:
`int some_global = 1234;

void main(void)
{
int state =0;
init_some_hardware();
init_something_else();

while(1)
{
toggle_LED();
delay(10);

//state machine here
}
}
`

What would the equivalent in Squirrel be ?.

Thanks
Anand

When it gets to the bottom of the script, execution ends. But that execution is just the setup phase: it should have registered timers and/or event-handlers which then get called later on, when something has happened that needs the script’s attention. So in Squirrel, your example would look like this:

`some_global <- 1234;
state <- 0;
init some hardware();
init_something_else();

function statemachine() {
imp.wakeup(10, statemachine);
toggle LED();
}

imp.wakeup(10, statemachine);
`
The “imp.wakeup” call sets up a timer event handler, so that the “statemachine” function is called when the timer fires. You can also set up event handlers for GPIO interrupts, and at some point we’ll add them for the other sorts of wakeup event too.

Most of the time, the imp isn’t executing Squirrel at all. It’s sitting there in sleep mode, waiting for something to happen that will make it call back into the Squirrel. There’s a bit more on that here: http://devwiki.electricimp.com/doku.php?id=basiccodestructure

Peter

Thanks for the clarification. My (mis)understanding was that each time the IMP woke up that the entire script would run top down again each time.

Is gonna take a while to get my head around it coming from a an embedded C background where you know exactly where each cycle is going and where things a generally more deterministic.

Thanks
Anand

You’re right in that if you go to low power sleep (6uA), then it’ll start again at the top. imp.wakeup() just schedules a timed callback when the imp is awake. You can have multiple ones running.

You also get callbacks on IO changes, and soon on sample buffer fill, UART input traffic, etc.

When the system isn’t running squirrel code it deals with the network and if there’s nothing else to do, quiesces the system to the lowest power state that can be achieved for the peripherals that are in use.