Commanduino Library¶
This is the documentation for the Commanduino controller modules. Here, you will find the methods associated with Command Handler Module, Command Manager Module, and Lock module. For the documentation associated with the Arduino devices, please see the Command Devices Package page for more details.
Module contents¶
Custom library designed to interface with Arduino hardware, allowing for control of the boards via Python.
Subpackages¶
Command Handler Module¶
-
class
commanduino.commandhandler.
CommandHandler
(delim=', ', term=';', cmd_decimal=2)[source]¶ Bases:
object
Represents the Command Handler which will handle commands to/from the Arduino hardware.
- Args:
delim (chr): Delimiter of the command, default set to DEFAULT_DELIM(‘,’)
term (chr): Terminal character of the command, set to DEFAULT_TERM(‘;’)
cmd_decimal (int): Decimal of the command, default set to DEFAULT_CMD_DECIMAL(2)
-
add_command
(command_id, callback_function)[source]¶ Adds an Arduino command to the Handler.
- Args:
command_id (str): The ID of the command.
callback_function (str): A copy of the command to “callback”.
-
add_default_handler
(callback_function)[source]¶ Adds a default handler to the device.
- Args:
- callback_function (str): A copy of the command to “callback”.
-
add_relay
(command_id, callback_function)[source]¶ Adds a relay to the Handler.
- Args:
command_id (str): The ID of the command.
callback_function (str): A copy of the command to “callback”.
-
build_remaining
(cmd_list)[source]¶ Builds an Arduino command from a list of partial commands.
- Args:
- cmd_list (List): The list of command constituents.
-
forge_command
(command_id, *args)[source]¶ Creates a full Arduino command.
- Args:
command_id (str): The ID of the command.
*args: Variable length argument list.
-
handle
(cmd)[source]¶ Handles a full command to/from the Arduino hardware.
- Args:
- cmd (str): The command to be handled.
-
process_char
(a_char)[source]¶ Processes a single character of a command.
- Args:
- a_char (chr): The character to be processed
-
process_serial
(a_serial)[source]¶ Processes the serial communication to obtain data to be processed.
- Args:
- a_serial (int): The serial to read from.
-
process_string
(a_string)[source]¶ Processes a full string in the command.
- Args:
- a_string (str): The string to be processed.
-
remove_command
(command_id, callback_function)[source]¶ Removes an Arduino command from the Handler.
- Args:
command_id (str): The ID of the command.
callback_function (str): A copy of the command to “callback”.
-
remove_default_handler
(callback_function)[source]¶ Removes a default handler from the device.
- Args:
- callback_function (str): A copy of the command to “callback”.
-
remove_relay
(command_id, callback_function)[source]¶ Removes a relay form the Handler.
- Args:
command_id (str): The ID of the command.
callback_function (str): A copy of the command to “callback”.
-
class
commanduino.commandhandler.
SerialCommandHandler
(port, baudrate=115200, timeout=0.01, delim=', ', term=';', cmd_decimal=2)[source]¶ Bases:
threading.Thread
,commanduino.commandhandler.CommandHandler
Represents the Command Handler which will handle commands to/from the Arduino hardware via Serial Communication.
- Args:
port (str): The port to communicate over.
baudrate (int): The baudrate of the serial communication, default set to DEFAULT_BAUDRATE (115200)
timeout (float): The time to wait for timeout, default set to DEFAULT_TIMEOUT (0.01)
delim (chr): The delimiting character of a command, default set to DEFAULT_DELIM (‘,’)
term (chr): The terminal character of a command, default set to DEFAULT_TERM (‘;’)
cmd_decimal (int): The decimal of the command, default set to DEFAULT_CMD_DECIMAL (2)
-
classmethod
from_config
(serialcommand_config)[source]¶ Obtains the details of the handler from a configuration.
- Args:
cls (Class): THe instantiating class.
serialcommand_config (Dict): Dictionary containing the configuration details.
- Returns:
- SerialCommandHandler: New SerialCommandHandler object with details set from a configuration setup.
-
open
(port, baudrate, timeout)[source]¶ Opens the serial communication between the PC and Arduino board.
- Args:
port (str): The port to communicate over.
baudrate (int): The baudrate of serial communication.
timeout (float): The time to wait for timeout.
-
send
(command_id, *arg)[source]¶ Sends a command over the serial communication.
- Args:
command_id (str): The ID of the command.
*arg: Variable argument.
Command Manager Module¶
-
exception
commanduino.commandmanager.
BonjourError
(command_id)[source]¶ Bases:
exceptions.Exception
Exception for when Bonjour device is not available/not found.
-
class
commanduino.commandmanager.
CommandBonjour
(serialcommandhandlers, timeout=0.1)[source]¶ Bases:
object
Represents a Command Manager for Bonjour devices.
- Args:
serialcommandhandlers: Collection of SerialCommandHandler objects.
timeout (float): Time to wait before timeout, default set to DEFAULT_BONJOUR_TIMEOUT (0.1)
-
detect_device
(command_id)[source]¶ Attempts to detect the Bonjour device.
- Args:
- command_id (str): The ID of the command.
- Returns:
cmdHdl (SerialCommandHandler): The Serial Command Handler.
bonjour_id (str): The Bonjour ID.
elapsed (float): Time elapsed since request.
-
get_bonjour_id
(serialcommandhandler, command_id)[source]¶ Obtains the device’s bonjour ID.
- Args:
serialcommandhandler (SerialCommandHandler): The Serial Command Handler object.
command_id (str): The ID of the command.
- Returns:
self.device_bonjour_id (str): The Bonjour ID.
is_valid (bool): Validity of the ID.
elapsed (float): Time elapsed since request.
-
class
commanduino.commandmanager.
CommandManager
(serialcommand_configs, devices_dict, init_timeout=1, init_n_repeats=5)[source]¶ Bases:
object
Manages variying amounts of Command Handler objects.
- Args:
serialcommand_configs (Tuple): Collection of serial command configurations.
devices_dict (Dict): Dictionary containing the list of devices.
init_timeout (int): Initialisation timeout, default se tto DEFAULT_INIT_TIMEOUT (1).
init_n_repeats (int): Number of times to attempt initialisation, default set to DEFAULT_INIT_N_REPEATS (5).
- Raises:
OSError: Port was not found.
SerialException: Port was not found.
InitError: CommandManager on port was not initialised.
-
classmethod
from_config
(config)[source]¶ Obtains the necessary information from a configuration setup.
- Args:
cls (CommandManager): The instantiating class.
config (Dict): Dictionary contianing the configuration data.
-
classmethod
from_configfile
(configfile)[source]¶ Obtains the configuration data from a configuration file.
- Args:
cls (CommandManager): The instantiating class.
configfile (File): The configuration file.
-
handle_init
(*arg)[source]¶ Handles the initialisation of the Manager, ensuring that the threading locks are released.
- Args:
- *arg: Variable argument.
-
register_all_devices
(devices_dict)[source]¶ Registers all available Arduino devices.
- Args:
- devices_dict (Dict): Dictionary containing all devices.
-
register_device
(device_name, device_info)[source]¶ Registers an individual Arduino device.
- Args:
device_name (str): Name of the device.
device_info (Dict): Dictionary containing the device information.
- Raises:
DeviceRegisterError: Device is not in the device register.
BonjourError: Device has not been found.
-
request_and_wait_for_init
(serialcommandhandler)[source]¶ Requests initialisation and waits until it obtains a threading lock.
- Args:
- serialcommandhandler (SerialCommandHandler): Serial Command Handler object for communication.
Lock module¶
-
class
commanduino.lock.
Lock
(timeout=1, sleep_time=0.001)[source]¶ Bases:
object
Represents the Threading locks to be used throughout this library.
- Args:
timeout (int): Time to wait until timeout, default set to 1.
sleep_time (float): Time to wait, default set to 0.001.