I’m somewhat puzzled by the behaviour of classes in Squirrel. In the following example (adapted from the Hall sensor example code), I am trying to create two Button instances, one for each button on the Hannah board. As per the Hall code, each instance is assigned to a new slot on the global table (using <- rather than =) - and I notice that if the instances are declared as variables in the normal way (i.e. local button1 = Button()…), the irqHandler function is not called. The first question: why is this?
Secondly, the real oddness. I have a log message in the constructor for Button, and when run, I can see that the constructor is called twice, with a different value for “pin” in each case. So far so good. However, no matter which button I press on the board, the irqHandler messages show that the pin is always 1. So it looks as though both Buttons have ended up with the same irqHandler.
I suspect that there is something odd occurring with the bindenv() call, though the Squirrel documentation is frankly rather crappy, so it’s hard to make out what’s really going on. I did explore the possibility of using free variables (as suggested in the Squirrel manual), but this did not work (as expected) due to the calling scope not containing the pin and irq variables (as these are local to Button).
To put it all another way, what’s a good way to have two separate button handlers which behave as expected?
P.S. Are there any plans to implement libraries for Imp code? I’m finding that the boilerplate for IoExpander ends up almost everywhere…