SNAP Hooks¶
SNAP hooks help provide a method for Event-Driven Programming in SNAPpy. There are a number of events in the
system that can invoke a SNAPpy function. Use the setHook()
function decorator to associate
your SNAPpy function with one the following events:
HOOK_STARTUP¶
- HOOK_STARTUP¶
Called on device bootup.
Example
@setHook(HOOK_STARTUP) def onBoot(): pass
HOOK_GPIN¶
- HOOK_GPIN¶
Called on transition of a monitored hardware pin.
- Parameters
pinNum (int) – The pin number of the pin that has transitioned.
isSet (bool) – A Boolean value indicating whether the pin is set.
Example
@setHook(HOOK_GPIN) def pinChg(pinNum, isSet): pass
See also
HOOK_1MS¶
- HOOK_1MS¶
Called every millisecond.
- Parameters
tick (int) – A rolling 16-bit integer incremented every millisecond, indicating the current count on the internal clock. The same counter is used for all four timing hooks.
Example
@setHook(HOOK_1MS) def doEvery1ms(tick): pass
HOOK_10MS¶
- HOOK_10MS¶
Called every 10 milliseconds.
- Parameters
tick (int) – A rolling 16-bit integer incremented every millisecond, indicating the current count on the internal clock. The same counter is used for all four timing hooks.
Example
@setHook(HOOK_10MS) def doEvery10ms(tick): pass
HOOK_100MS¶
- HOOK_100MS¶
Called every 100 milliseconds.
- Parameters
tick (int) – A rolling 16-bit integer incremented every millisecond, indicating the current count on the internal clock. The same counter is used for all four timing hooks.
Example
@setHook(HOOK_100MS) def doEvery100ms(tick): pass
HOOK_1S¶
- HOOK_1S¶
Called every second.
- Parameters
tick (int) – A rolling 16-bit integer incremented every millisecond, indicating the current count on the internal clock. The same counter is used for all four timing hooks.
Example
@setHook(HOOK_1S) def doEverySec(tick): pass
HOOK_STDIN¶
- HOOK_STDIN¶
Called when “user input” data is received.
- Parameters
data (str) – A data buffer containing one or more received characters.
Example
@setHook(HOOK_STDIN) def getInput(data): pass
See also
User Guide on The Switchboard
HOOK_STDOUT¶
- HOOK_STDOUT¶
Called when “user output” data is sent.
Example
@setHook(HOOK_STDOUT) def printed(): pass
See also
User Guide on The Switchboard
HOOK_RPC_SENT¶
- HOOK_RPC_SENT¶
Called when the buffer for an outgoing RPC call is cleared.
- Parameters
bufRef (int) – An integer reference to the packet that the RPC call attempted to send. This integer will correspond to the value returned from
getInfo(9)
when called immediately after an RPC call is made. The receipt of a value fromHOOK_RPC_SENT
does not necessarily indicate that the packet was sent and received successfully. It is an indication that SNAP has completed processing the packet.
Example
@setHook(HOOK_RPC_SENT) def rpcDone(bufRef): pass
See also