botservices – CodeBot’s event loop

Event-driven Services for CodeBot.

The module provides the BotServices object, which lets you connect callback functions to react to events.

Example of hooking a callback function:

from botservices import BotServices
bot = BotServices()

# Define callback function
def handle_button(buttons):
    if buttons[0]:
        # Do something amazing when BTN-0 is pressed!

# Hook the callback.
bot.on_button(handle_button)
class botservices.BotServices[source]

CodeBot event loop built on botcore module.

clear_last()[source]

Clear last-value for all sensors, forcing event callbacks on next poll with new current values

config_ls(thresh, reflective_line)[source]

Configure line sensors.

Parameters
  • thresh (int) – Sensor level for boundary between line and background. Sensor range is 0 (bright) to 4095 (dark).

  • reflective_line (bool) – Set to True for a bright line on dark background, False for dark line against light background.

config_prox(power, sensitivity)[source]

Configure proximity sensors.

Parameters
  • power (int) – Emitter power (1-8).

  • sensitivity (int) – Sensor sensitivity (0-100). Percent range from 0=least to 100=most sensitive.

loop()[source]

Enter event loop. Call this after registering ‘on_EVENT’ event callback functions.

on_accel(cb)[source]

Attach callback to Accelerometer events.

Parameters

cb – Callback function to be called when event happens. The function will be called with a single parameter, a list of 3 bools corresponding to X, Y, Z axes of acceleration.

on_button(cb)[source]

Attach callback to Button Press events.

Parameters

cb – Callback function to be called when event happens. The function will be called with a single parameter, a list of 2 bools.

on_line(cb)[source]

Attach callback to Line Sensor events.

Parameters

cb – Callback function to be called when event happens. The function will be called with a single parameter, a list of 5 bools.

on_prox(cb)[source]

Attach callback to Proximity Sensor events.

Parameters

cb – Callback function to be called when event happens. The function will be called with a single parameter, a list of 2 bools.

on_timeout(cb, timeout_ms, arg=None)[source]

Schedule callback to occur after specified timeout.

Parameters
  • cb – Callback function to be called after time period expires. Note: For a repeating timer, the callback function can use this function to re-register itself.

  • timeout_ms (int) – Timeout in milliseconds, after which callback will be called.

  • arg – Optional argument that will be passed to callback function cb(arg).

stop()[source]

Exit the event loop.

class botservices.CancelableCallback(cb)[source]

Wrapper for callbacks you’d like to be able to cancel.

Example:

cb = CancelableCallback(my_event)
bot.on_timeout(cb, 1000)
...
cb.cancel()
cancel()[source]

Cancel this callback.

Note: callback remains on event list, just does nothing when called.