Here’s something weird - my Imp keeps going from Online -> Offline and back online every 30 seconds or so. I’ve read posts in the past, where the solution was to log a message every ~5minutes to prevent the NAT from tearing down the connection - but that doesn’t seem to be the issue I’m facing.
OPB’s and Phil’s solution is the idiomatic way of doing it, and the easiest to understand. But for purely academic interest: Squirrel can in fact “continue where it left off” if you use a generator. Generators are poorly-documented even by Squirrel standards, but here is some code that does the same thing as your original Squirrel:
`gen <- function() {
i <- 0;
while(1) {
i++;
server.log("i is now "+i);
yield imp.wakeup(1,function() { resume gen; });
}
Thanks @Hugo, @Peter. I was reading the documentation on the wakeup() and it states that the resolution only goes up to centiseconds (0.01s), whereas I need it to be 0.0002seconds (20 usec).
Any suggestions on how I can get finer resolution using this, or any other method?
If you need a long sleep, seconds say, then use imp.wakeup(). If you need a short sleep, then use imp.sleep(). Using imp.sleep() only confuses the server if you stay asleep for tens of seconds in total, and, hopefully, if you’re sleeping for tens of seconds then that sleep needn’t be microsecond-accurate.
Though do bear in mind that even executing a line of Squirrel takes a noticeable time at the microsecond level. Really tight timing can’t be achieved by bit-banging directly from Squirrel; you might need to use a SPI port instead, or the sampler or fixed-frequency DAC, to have the timing taken care of in hardware.