Squirrel question

Hello all,

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? :slight_smile:

P.S. Are there any plans to implement libraries for Imp code? I’m finding that the boilerplate for IoExpander ends up almost everywhere…

I experienced the same issue but I believe it is because the buttons share the same irq pin. Instead of the squirrel code calling BOTH buttons irqHandler function , the code will only call the last registered irqHandler, in this case the last instantiated Button Class. (Which I’m not sure if this is a bug or expected behavior but that’s the “why” I believe)