Uart callback, can't pass context

#1

I’m trying to use the new uart callback function instead of polling. However I’m struggling to make good use of it, because I cannot pass any context with the callback. I’m also not very familiar with squirrel so I might be missing something.

It seems like I can pass a function defined in a class as callback, but when the callback is called it does not seem to be called in the class instance context, e.g. I cannot add the received data to an instance variable in that object.

Are the callback function only be usable in a global context, which seems to be a pretty limited use case or is there another way to make that work?

#2

See http://devwiki.electricimp.com/doku.php?id=garbagecollectorproblems - in the example, the callback is bound to a class instance with classfunction.bindenv(this).

#3

Awesome. Thank you, Hugo.

Using

serPort.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, readserial.bindenv(this));

instead of

serPort.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, readserial);

did the trick.

I don’t quite understand it why I need to explicitly bind ‘this’ as the class instance should always be bound. At least that’s what the squirrel docs say:

while by default a squirrel function call passes as environment object ‘this’, the object where the function was indexed from.

But probably this doesn’t apply when passing the function reference as callback.

Anyhow, it works now. Now I have to figure out why I just receive garbage from the UART …

#4

It’s because the callback is coming from the root context (ie, no squirrel is executing).

Always worth looping back (short tx/rx) to sanity check if you’re seeing garbage… though that won’t help debug a baudrate issue :slight_smile:

#5

yeah, I’m pretty sure it is a baudrate issue. Trying to attach an electric imp to a Honeywell Rondostat thermostat, which I flashed with an open source firmware. The amtel is running with an internal frequency, which isn’t very accurate I guess.

#6

The imp baudrate doesn’t need to be a “round” number (eg 9600bps, 19200bps); if you want to run at 9,250bps then just specify that in the uart configure call. Might help?

#7

Hey Hugo, this link is down.

Is there another link with similar documentation please? It would be helpful for me.

#8

Not sure what you’re asking about here; garbage collection is covered here https://developer.electricimp.com/resources/objectmanagement however the original question is about executing a function with a specific context, using bindenv. That’s covered here https://developer.electricimp.com/resources/bindenvy

1 Like