You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Hi,

    Sorry I'm a bit late replying here - was away all weekend. I think it's all been explained, but hopefully this is more of a summary:

    Espruino uses interrupts internally, for quite a lot of things - setWatch, digitalPulse, serial, timers, waveforms, and so on. However that's only native code - JavaScript itself doesn't execute in an interrupt. If an interrupt needs JavaScript to be run then it puts a message in a queue (often with an accurate timestamp), and the JavaScript is run when the interpreter is next ready to do it.

    If Espruino is idle it'll basically have exactly the same effect as if you had an interrupt - but if it's busy then you'll have to wait a small time until the code is executed.

    The decision not to allow JS in interrupts is actually for a few reasons:

    • it makes the interpreter smaller and faster
    • it makes it much easier to be sure that there aren't any bugs in the interpreter itself
    • it's really easy to shoot yourself in the foot with interrupts - even in JS :)
    • The JavaScript code isn't executed anywhere near as quickly as C - so if you're trying to do something super-fast you probably need to find another way to do it anyway.

    Having said all that, you can use peek and poke to do basically anything - including setting up your own interrupts (which would have to run native code, not JavaScript). It's just not easy. I am planning on adding something that'll let you execute native code (created via the inline assembler) on interrupts though, so that should improve the situation.

    In the mean time, it's actually pretty easy to compile Espruino yourself, and you can then add your own bits of C code (which can run on interrupts).

About

Avatar for Gordon @Gordon started