Package pymata_aio :: Module pymata_core :: Class PymataCore
[hide private]
[frames] | no frames]

Class PymataCore

source code

This class exposes and implements the pymata_core asyncio API, It includes the public API methods as well as a set of private methods. If your application is using asyncio, this is the API that you should use.

After instantiating this class, its "start" method MUST be called to perform Arduino pin auto-detection.

Instance Methods [hide private]
 
__init__(self, arduino_wait=2, sleep_tune=.001, com_port=None)
This is the "constructor" method for the PymataCore class.
source code
 
start(self)
This method must be called immediately after the class is instantiated.
source code
 
analog_read(self, pin)
Retrieve the last data update for the specified analog pin.
source code
 
analog_write(self, pin, value)
Set the selected pin to the specified value.
source code
 
digital_read(self, pin)
Retrieve the last data update for the specified digital pin.
source code
 
digital_write(self, pin, value)
Set the specified pin to the specified value.
source code
 
disable_analog_reporting(self, pin)
Disables analog reporting for a single analog pin.
source code
 
disable_digital_reporting(self, pin)
Disables digital reporting.
source code
 
encoder_config(self, pin_a, pin_b, cb=None)
This command enables the rotary encoder (2 pin + ground) and will enable encoder reporting.
source code
 
encoder_read(self, pin)
This method retrieves the latest encoder data value
source code
 
enable_analog_reporting(self, pin)
Enables analog reporting.
source code
 
enable_digital_reporting(self, pin)
Enables digital reporting.
source code
 
extended_analog(self, pin, data)
This method will send an extended-data analog write command to the selected pin.
source code
 
get_analog_latch_data(self, pin)
A list is returned containing the latch state for the pin, the latched value, and the time stamp [latched_state, threshold_type, threshold_value, latched_data, time_stamp]
source code
 
get_analog_map(self)
This method requests a Firmata analog map query and returns the results.
source code
 
get_capability_report(self)
This method requests and returns a Firmata capability query report
source code
 
get_digital_latch_data(self, pin)
A list is returned containing the latch state for the pin, the latched value, and the time stamp [pin_num, latch_state, latched_value, time_stamp]
source code
 
get_firmware_version(self)
This method retrieves the Firmata firmware version
source code
 
get_protocol_version(self)
This method returns the major and minor values for the protocol version, i.e.
source code
 
get_pin_state(self, pin)
This method retrieves a pin state report for the specified pin
source code
 
get_pymata_version(self)
This method retrieves the PyMata version number
source code
 
i2c_config(self, read_delay_time=0)
NOTE: THIS METHOD MUST BE CALLED BEFORE ANY I2C REQUEST IS MADE This method initializes Firmata for I2c operations.
source code
 
i2c_read_data(self, address)
This method retrieves cached i2c data to support a polling mode.
source code
 
i2c_read_request(self, address, register, number_of_bytes, read_type, cb=None)
This method requests the read of an i2c device.
source code
 
i2c_write_request(self, address, args)
Write data to an i2c device.
source code
 
play_tone(self, pin, tone_command, frequency, duration)
This method will call the Tone library for the selected pin.
source code
 
send_reset(self)
Send a Sysex reset command to the arduino
source code
 
servo_config(self, pin, min_pulse=544, max_pulse=2400)
Configure a pin as a servo pin.
source code
 
set_analog_latch(self, pin, threshold_type, threshold_value, cb=None)
This method "arms" an analog pin for its data to be latched and saved in the latching table If a callback method is provided, when latching criteria is achieved, the callback function is called with latching data notification.
source code
 
set_digital_latch(self, pin, threshold_value, cb=None)
This method "arms" a digital pin for its data to be latched and saved in the latching table If a callback method is provided, when latching criteria is achieved, the callback function is called with latching data notification.
source code
 
set_pin_mode(self, pin_number, pin_state, callback=None)
This method sets the pin mode for the specified pin.
source code
 
set_sampling_interval(self, interval)
This method sends the desired sampling interval to Firmata.
source code
 
shutdown(self)
This method attempts an orderly shutdown
source code
 
sleep(self, sleep_time)
This method is a proxy method for asyncio.sleep
source code
 
sonar_config(self, trigger_pin, echo_pin, cb=None, ping_interval=50, max_distance=200)
Configure the pins,ping interval and maximum distance for an HC-SR04 type device.
source code
 
sonar_data_retrieve(self, trigger_pin)
Retrieve Ping (HC-SR04 type) data.
source code
 
stepper_config(self, steps_per_revolution, stepper_pins)
Configure stepper motor prior to operation.
source code
 
stepper_step(self, motor_speed, number_of_steps)
Move a stepper motor for the number of steps at the specified speed This is a FirmataPlus feature.
source code
 
_command_dispatcher(self)
This is a private method.
source code
 
_analog_mapping_response(self, data)
This is a private message handler method.
source code
 
_analog_message(self, data)
This is a private message handler method.
source code
 
_capability_response(self, data)
This is a private message handler method.
source code
 
_digital_message(self, data)
This is a private message handler method.
source code
 
_encoder_data(self, data)
This is a private message handler method.
source code
 
_i2c_reply(self, data)
This is a private message handler method.
source code
 
_pin_state_response(self, data)
This is a private message handler method.
source code
 
_report_firmware(self, sysex_data)
This is a private message handler method.
source code
 
_report_version(self)
This is a private message handler method.
source code
 
_sonar_data(self, data)
This method handles the incoming sonar data message and stores the data in the response table.
source code
 
_string_data(self, data)
This is a private message handler method.
source code
 
_check_latch_data(self, key, data)
This is a private utility method.
source code
 
_discover_port(self)
This is a private utility method.
source code
 
_format_capability_report(self, data)
This is a private utility method.
source code
 
_process_latching(self, key, latching_entry)
This is a private utility method.
source code
 
_send_command(self, command)
This is a private utility method.
source code
 
_send_sysex(self, sysex_command, sysex_data=None)
This is a private utility method.
source code
 
_wait_for_data(self, current_command, number_of_bytes)
This is a private utility method.
source code
Method Details [hide private]

__init__(self, arduino_wait=2, sleep_tune=.001, com_port=None)
(Constructor)

source code 

This is the "constructor" method for the PymataCore class.

Parameters:
  • arduino_wait - Amount of time to wait for Arduino to reset. UNO takes 2 seconds, Leonardo can be zero
  • sleep_tune - This parameter sets the amount of time PyMata core uses to set asyncio.sleep
  • com_port - Manually selected com port - normally it is auto-detected
Returns:
This method never returns

start(self)

source code 

This method must be called immediately after the class is instantiated. It instantiates the serial interface and then performs auto pin discovery.

Returns:
No return value.

analog_read(self, pin)

source code 

Retrieve the last data update for the specified analog pin.

Parameters:
  • pin - Analog pin number (ex. A2 is specified as 2)
Returns:
Last value reported for the analog pin
Decorators:
  • @asyncio.coroutine

analog_write(self, pin, value)

source code 

Set the selected pin to the specified value.

Parameters:
  • pin - PWM pin number
  • value - Pin value (0 - 0x4000)
Returns:
No return value
Decorators:
  • @asyncio.coroutine

digital_read(self, pin)

source code 

Retrieve the last data update for the specified digital pin.

Parameters:
  • pin - Digital pin number
Returns:
Last value reported for the digital pin
Decorators:
  • @asyncio.coroutine

digital_write(self, pin, value)

source code 

Set the specified pin to the specified value.

Parameters:
  • pin - pin number
  • value - pin value
Returns:
No return value
Decorators:
  • @asyncio.coroutine

disable_analog_reporting(self, pin)

source code 

Disables analog reporting for a single analog pin.

Parameters:
  • pin - Analog pin number. For example for A0, the number is 0.
Returns:
No return value
Decorators:
  • @asyncio.coroutine

disable_digital_reporting(self, pin)

source code 

Disables digital reporting. By turning reporting off for this pin, Reporting is disabled for all 8 bits in the "port" -

Parameters:
  • pin - Pin and all pins for this port
Returns:
No return value
Decorators:
  • @asyncio.coroutine

encoder_config(self, pin_a, pin_b, cb=None)

source code 

This command enables the rotary encoder (2 pin + ground) and will enable encoder reporting.

NOTE: This command is not currently part of standard arduino firmata, but is provided for legacy support of CodeShield on an Arduino UNO.

Encoder data is retrieved by performing a digital_read from pin a (encoder pin_a)

Parameters:
  • pin_a - Encoder pin 1.
  • pin_b - Encoder pin 2.
  • cb - callback function to report encoder changes
Returns:
No return value
Decorators:
  • @asyncio.coroutine

encoder_read(self, pin)

source code 

This method retrieves the latest encoder data value

Parameters:
  • pin - Encoder Pin
Returns:
encoder data value
Decorators:
  • @asyncio.coroutine

enable_analog_reporting(self, pin)

source code 

Enables analog reporting. By turning reporting on for a single pin,

Parameters:
  • pin - Analog pin number. For example for A0, the number is 0.
Returns:
No return value
Decorators:
  • @asyncio.coroutine

enable_digital_reporting(self, pin)

source code 

Enables digital reporting. By turning reporting on for all 8 bits in the "port" - this is part of Firmata's protocol specification.

Parameters:
  • pin - Pin and all pins for this port
Returns:
No return value
Decorators:
  • @asyncio.coroutine

extended_analog(self, pin, data)

source code 

This method will send an extended-data analog write command to the selected pin.

Parameters:
  • pin - 0 - 127
  • data - 0 - 0xfffff
Returns:
No return value
Decorators:
  • @asyncio.coroutine

get_analog_latch_data(self, pin)

source code 

A list is returned containing the latch state for the pin, the latched value, and the time stamp [latched_state, threshold_type, threshold_value, latched_data, time_stamp]

Parameters:
  • pin - Pin number.
Returns:
[latched_state, threshold_type, threshold_value, latched_data, time_stamp]
Decorators:
  • @asyncio.coroutine

get_analog_map(self)

source code 

This method requests a Firmata analog map query and returns the results.

Returns:
An analog map response or None if a timeout occurs
Decorators:
  • @asyncio.coroutine

get_capability_report(self)

source code 

This method requests and returns a Firmata capability query report

Returns:
A capability report in the form of a list
Decorators:
  • @asyncio.coroutine

get_digital_latch_data(self, pin)

source code 

A list is returned containing the latch state for the pin, the latched value, and the time stamp [pin_num, latch_state, latched_value, time_stamp]

Parameters:
  • pin - Pin number.
Returns:
[latched_state, threshold_type, threshold_value, latched_data, time_stamp]
Decorators:
  • @asyncio.coroutine

get_firmware_version(self)

source code 

This method retrieves the Firmata firmware version

Returns:
Firmata firmware version
Decorators:
  • @asyncio.coroutine

get_protocol_version(self)

source code 

This method returns the major and minor values for the protocol version, i.e. 2.4

Returns:
Firmata protocol version
Decorators:
  • @asyncio.coroutine

get_pin_state(self, pin)

source code 

This method retrieves a pin state report for the specified pin

Parameters:
  • pin - Pin of interest
Returns:
pin state report
Decorators:
  • @asyncio.coroutine

get_pymata_version(self)

source code 

This method retrieves the PyMata version number

Returns:
PyMata version number.
Decorators:
  • @asyncio.coroutine

i2c_config(self, read_delay_time=0)

source code 

NOTE: THIS METHOD MUST BE CALLED BEFORE ANY I2C REQUEST IS MADE This method initializes Firmata for I2c operations.

Parameters:
  • read_delay_time, (in, microseconds) - an optional parameter, default is 0
Returns:
No Return Value
Decorators:
  • @asyncio.coroutine

i2c_read_data(self, address)

source code 

This method retrieves cached i2c data to support a polling mode.

Parameters:
  • address - I2C device address
Returns:
Last cached value read
Decorators:
  • @asyncio.coroutine

i2c_read_request(self, address, register, number_of_bytes, read_type, cb=None)

source code 

This method requests the read of an i2c device. Results are retrieved by a call to i2c_get_read_data(). or by callback. If a callback method is provided, when data is received from the device it will be sent to the callback method. Some devices require that transmission be restarted (e.g. MMA8452Q acceleromater). Use I2C_READ | I2C_RESTART_TX for those cases.

Parameters:
  • address - i2c device address
  • register - register number (can be set to zero)
  • number_of_bytes - number of bytes expected to be returned
  • read_type - I2C_READ or I2C_READ_CONTINUOUSLY. I2C_RESTART_TX may be OR'ed when required
  • cb - Optional callback function to report i2c data as result of read command
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

i2c_write_request(self, address, args)

source code 

Write data to an i2c device.

Parameters:
  • address - i2c device address
  • args - A variable number of bytes to be sent to the device passed in as a list
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

play_tone(self, pin, tone_command, frequency, duration)

source code 

This method will call the Tone library for the selected pin. It requires FirmataPlus to be loaded onto the arduino If the tone command is set to TONE_TONE, then the specified tone will be played. Else, if the tone command is TONE_NO_TONE, then any currently playing tone will be disabled.

Parameters:
  • pin - Pin number
  • tone_command - Either TONE_TONE, or TONE_NO_TONE
  • frequency - Frequency of tone
  • duration - Duration of tone in milliseconds
Returns:
No return value
Decorators:
  • @asyncio.coroutine

send_reset(self)

source code 

Send a Sysex reset command to the arduino

Returns:
No return value.
Decorators:
  • @asyncio.coroutine

servo_config(self, pin, min_pulse=544, max_pulse=2400)

source code 

Configure a pin as a servo pin. Set pulse min, max in ms. Use this method (not set_pin_mode) to configure a pin for servo operation.

Parameters:
  • pin - Servo Pin.
  • min_pulse - Min pulse width in ms.
  • max_pulse - Max pulse width in ms.
Returns:
No return value
Decorators:
  • @asyncio.coroutine

set_analog_latch(self, pin, threshold_type, threshold_value, cb=None)

source code 

This method "arms" an analog pin for its data to be latched and saved in the latching table If a callback method is provided, when latching criteria is achieved, the callback function is called with latching data notification. Data returned in the callback list has the pin number as the first element,

Parameters:
  • pin - Analog pin number (value following an 'A' designator, i.e. A5 = 5
  • threshold_type - ANALOG_LATCH_GT | ANALOG_LATCH_LT | ANALOG_LATCH_GTE | ANALOG_LATCH_LTE
  • threshold_value - numerical value - between 0 and 1023
  • cb - callback method
Returns:
True if successful, False if parameter data is invalid
Decorators:
  • @asyncio.coroutine

set_digital_latch(self, pin, threshold_value, cb=None)

source code 

This method "arms" a digital pin for its data to be latched and saved in the latching table If a callback method is provided, when latching criteria is achieved, the callback function is called with latching data notification. Data returned in the callback list has the pin number as the first element,

Parameters:
  • pin - Digital pin number
  • threshold_value - 0 or 1
  • cb - callback function
Returns:
True if successful, False if parameter data is invalid
Decorators:
  • @asyncio.coroutine

set_pin_mode(self, pin_number, pin_state, callback=None)

source code 

This method sets the pin mode for the specified pin. For Servo, use servo_config() instead.

Parameters:
  • pin_number - Arduino Pin Number
  • pin_state - INPUT/OUTPUT/ANALOG/PWM/
  • callback - Optional: A reference to a call back function to be called when pin data value changes
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

set_sampling_interval(self, interval)

source code 

This method sends the desired sampling interval to Firmata. Note: Standard Firmata will ignore any interval less than 10 milliseconds

Parameters:
  • interval - Integer value for desired sampling interval in milliseconds
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

shutdown(self)

source code 

This method attempts an orderly shutdown

Returns:
No return value
Decorators:
  • @asyncio.coroutine

sleep(self, sleep_time)

source code 

This method is a proxy method for asyncio.sleep

Parameters:
  • sleep_time - Sleep interval in seconds
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

sonar_config(self, trigger_pin, echo_pin, cb=None, ping_interval=50, max_distance=200)

source code 

Configure the pins,ping interval and maximum distance for an HC-SR04 type device. Single pin configuration may be used. To do so, set both the trigger and echo pins to the same value. Up to a maximum of 6 SONAR devices is supported If the maximum is exceeded a message is sent to the console and the request is ignored. NOTE: data is measured in centimeters

Parameters:
  • trigger_pin - The pin number of for the trigger (transmitter).
  • echo_pin - The pin number for the received echo.
  • cb - optional callback function to report sonar data changes
  • ping_interval - Minimum interval between pings. Lowest number to use is 33 ms.Max is 127
  • max_distance - Maximum distance in cm. Max is 200.
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

sonar_data_retrieve(self, trigger_pin)

source code 

Retrieve Ping (HC-SR04 type) data. The data is presented as a dictionary. The 'key' is the trigger pin specified in sonar_config() and the 'data' is the current measured distance (in centimeters) for that pin. If there is no data, the value is set to None.

Parameters:
  • trigger_pin - key into sonar data map
Returns:
active_sonar_map
Decorators:
  • @asyncio.coroutine

stepper_config(self, steps_per_revolution, stepper_pins)

source code 

Configure stepper motor prior to operation. This is a FirmataPlus feature.

Parameters:
  • steps_per_revolution - number of steps per motor revolution
  • stepper_pins - a list of control pin numbers - either 4 or 2
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

stepper_step(self, motor_speed, number_of_steps)

source code 

Move a stepper motor for the number of steps at the specified speed This is a FirmataPlus feature.

Parameters:
  • motor_speed - 21 bits of data to set motor speed
  • number_of_steps - 14 bits for number of steps & direction positive is forward, negative is reverse
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

_command_dispatcher(self)

source code 

This is a private method. It continually accepts and interprets data coming from Firmata,and then dispatches the correct handler to process the data.

Returns:
This method never returns
Decorators:
  • @asyncio.coroutine

_analog_mapping_response(self, data)

source code 

This is a private message handler method. It is a message handler for the analog mapping response message

Parameters:
  • data - response data
Returns:
none - but saves the response
Decorators:
  • @asyncio.coroutine

_analog_message(self, data)

source code 

This is a private message handler method. It is a message handler for analog messages.

Parameters:
  • data - message data
Returns:
None - but saves the data in the pins structure
Decorators:
  • @asyncio.coroutine

_capability_response(self, data)

source code 

This is a private message handler method. It is a message handler for capability report responses.

Parameters:
  • data - capability report
Returns:
None - but report is saved
Decorators:
  • @asyncio.coroutine

_digital_message(self, data)

source code 

This is a private message handler method. It is a message handler for Digital Messages.

Parameters:
  • data - digital message
Returns:
None - but update is saved in pins structure
Decorators:
  • @asyncio.coroutine

_encoder_data(self, data)

source code 

This is a private message handler method. It handles encoder data messages.

Parameters:
  • data - encoder data
Returns:
None - but update is saved in the digital pins structure
Decorators:
  • @asyncio.coroutine

_i2c_reply(self, data)

source code 

This is a private message handler method. It handles replies to i2c_read requests. It stores the data for each i2c device address in a dictionary called i2c_map. The data may be retrieved via a polling call to i2c_get_read_data(). It a callback was specified in pymata.i2c_read, the raw data is sent through the callback

Parameters:
  • data - raw data returned from i2c device
Decorators:
  • @asyncio.coroutine

_pin_state_response(self, data)

source code 

This is a private message handler method. It handles pin state query response messages.

Parameters:
  • data - Pin state message
Returns:
None - but response is saved
Decorators:
  • @asyncio.coroutine

_report_firmware(self, sysex_data)

source code 

This is a private message handler method. This method handles the sysex 'report firmware' command sent by Firmata (0x79). It assembles the firmware version by concatenating the major and minor version number components and the firmware identifier into a string. e.g. "2.3 StandardFirmata.ino"

Parameters:
  • sysex_data - Sysex data sent from Firmata
Returns:
None
Decorators:
  • @asyncio.coroutine

_report_version(self)

source code 

This is a private message handler method. This method reads the following 2 bytes after the report version command (0xF9 - non sysex). The first byte is the major number and the second byte is the minor number.

Returns:
None
Decorators:
  • @asyncio.coroutine

_sonar_data(self, data)

source code 

This method handles the incoming sonar data message and stores the data in the response table.

Parameters:
  • data - Message data from Firmata
Returns:
No return value.
Decorators:
  • @asyncio.coroutine

_string_data(self, data)

source code 

This is a private message handler method. It is the message handler for String data messages that will be printed to the console.

Parameters:
  • data - message
Returns:
None - message is sent to console
Decorators:
  • @asyncio.coroutine

_check_latch_data(self, key, data)

source code 

This is a private utility method. When a data change message is received this method checks to see if latching needs to be processed

Parameters:
  • key - encoded pin number
  • data - data change
Returns:
None
Decorators:
  • @asyncio.coroutine

_discover_port(self)

source code 

This is a private utility method. This method attempts to discover the com port that the arduino is connected to.

Returns:
Detected Comport

_format_capability_report(self, data)

source code 

This is a private utility method. This method formats a capability report if the user wishes to send it to the console

Parameters:
  • data - Capability report
Returns:
None

_process_latching(self, key, latching_entry)

source code 

This is a private utility method. This method process latching events and either returns them via callback or stores them in the latch map

Parameters:
  • key - Encoded pin
  • latching_entry - a latch table entry
Returns:
Callback or store data in latch map
Decorators:
  • @asyncio.coroutine

_send_command(self, command)

source code 

This is a private utility method. The method sends a non-sysex command to Firmata.

Parameters:
  • command - command data
Returns:
length of data sent
Decorators:
  • @asyncio.coroutine

_send_sysex(self, sysex_command, sysex_data=None)

source code 

This is a private utility method. This method sends a sysex command to Firmata.

Parameters:
  • sysex_command - sysex command
  • sysex_data - data for command @return : No return value.
Decorators:
  • @asyncio.coroutine

_wait_for_data(self, current_command, number_of_bytes)

source code 

This is a private utility method. This method accumulates the requested number of bytes and then returns the full command

Parameters:
  • current_command - command id
  • number_of_bytes - how many bytes to wait for
Returns:
command
Decorators:
  • @asyncio.coroutine