communication Module

This module is used to communicate with the shield.

Requirement: Make objects from binary stuff.

class AYABInterface.communication.Communication(file, get_needle_positions, machine, on_message_received=(), left_end_needle=None, right_end_needle=None)[source]

Bases: object

This class comunicates with the AYAB shield.

__init__(file, get_needle_positions, machine, on_message_received=(), left_end_needle=None, right_end_needle=None)[source]

Create a new Communication object.

Parameters:
  • file – a file-like object with read and write methods for the communication with the Arduino. This could be a serial.Serial or a socket.socket.makefile().
  • get_needle_positions – a callable that takes an index and returns None or an iterable over needle positions.
  • machine (AYABInterface.machines.Machine) – the machine to use for knitting
  • on_message_received (list) – an iterable over callables that takes a received message whenever it comes in. Since state changes only take place when a message is received, this can be used as an state observer.
  • left_end_needle – A needle number on the machine. Other needles that are on the left side of this needle are not used for knitting. Their needle positions are not be set.
  • right_end_needle – A needle number on the machine. Other needles that are on the right side of this needle are not used for knitting. Their needle positions are not be set.
__weakref__

list of weak references to the object (if defined)

api_version_is_supported(api_version)[source]

Return whether an api version is supported by this class.

Return type:bool
Returns:if the api version is supported
Parameters:api_version (int) – the api version

Currently supported api versions: 4

can_receive_messages()[source]

Whether tihs communication is ready to receive messages.]

Return type:bool
assert not communication.can_receive_messages()
communication.start()
assert communication.can_receive_messages()
communication.stop()
assert not communication.can_receive_messages()
controller

Information about the controller.

If no information about the controller is received, the return value is None.

If information about the controller is known after The cnfInfo Message was received, you can access these values:

>>> communication.controller.firmware_version
(5, 2)
>>> communication.controller.firmware_version.major
5
>>> communication.controller.firmware_version.minor
2
>>> communication.controller.api_version
4
last_requested_line_number

The number of the last line that was requested.

Return type:int
Returns:the last requested line number or 0
left_end_needle

The left end needle of the needle positions.

Return type:int
Returns:the left end needle of the machine
lock

The lock of the communication.

In case you parallelize() the communication, you may want to use this lock to make shure the parallelization does not break your code.

needle_positions

A cache for the needle positions.

Return type:AYABInterface.communication.cache.NeedlePositionCache
on_message(callable)[source]

Add an observer to received messages.

Parameters:callable – a callable that is called every time a AYABInterface.communication.host_messages.Message is sent or a AYABInterface.communication.controller_messages.Message is received
parallelize(seconds_to_wait=2)[source]

Start a parallel thread for receiving messages.

If start() was no called before, start will be called in the thread. The thread calls receive_message() until the state is_connection_closed().

Parameters:seconds_to_wait (float) – A time in seconds to wait with the parallel execution. This is useful to allow the controller time to initialize.
receive_message()[source]

Receive a message from the file.

right_end_needle

The left end needle of the needle positions.

Return type:int
Returns:the right end needle of the machine
runs_in_parallel()[source]

Whether the communication runs in parallel.

Return type:bool
Returns:whether parallelize() was called and the communication still receives messages and is not stopped
send(host_message_class, *args)[source]

Send a host message.

Parameters:
  • host_message_class (type) – a subclass of AYABImterface.communication.host_messages.Message
  • args – additional arguments that shall be passed to the host_message_class as arguments
start()[source]

Start the communication about a content.

Parameters:content (Content) – the content of the communication.
state

The state this object is in.

Returns:the state this communication object is in.
Return type:AYABInterface.communication.states.State

Note

When calling parallelize() the state can change while you check it.

stop()[source]

Stop the communication with the shield.