Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
v2.7 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterYsLab@gmail.com committed Nov 9, 2015
1 parent 62b98f7 commit 9fc44ad
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions pymata_aio/pymata_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
"""

import asyncio
import glob
import logging
import sys
import time
import logging
import glob

import serial

from .constants import Constants
from .private_constants import PrivateConstants
from .pin_data import PinData
from .private_constants import PrivateConstants
from .pymata_serial import PymataSerial
from .pymata_socket import PymataSocket

Expand Down Expand Up @@ -239,7 +241,6 @@ def __init__(self, arduino_wait=2, sleep_tune=0.0001, log_output=False,
# set up signal handler for controlC
self.loop = asyncio.get_event_loop()


def start(self):
"""
This method must be called immediately after the class is instantiated.
Expand Down Expand Up @@ -338,7 +339,6 @@ def start(self):
len(self.analog_pins),
'Analog Pins\n\n'))


async def start_aio(self):
"""
This method must be called immediately after the class is instantiated.
Expand Down Expand Up @@ -969,12 +969,15 @@ async def set_pin_mode(self, pin_number, pin_state, callback=None,
For Servo, use servo_config() instead.
:param pin_number: Arduino Pin Number
:param pin_state:INPUT/OUTPUT/ANALOG/PWM/
:param pin_state: INPUT/OUTPUT/ANALOG/PWM - for SERVO use
servo_config()
:param callback: Optional: A reference to a call back function to be
called when pin data value changes
:param callback_type: direct call or asyncio await
:returns: No return value.
called when pin data value changes
:param cb_type: Constants.CB_TYPE_DIRECT = direct call or
Constants.CB_TYPE_ASYNCIO = asyncio coroutine
:returns: No return value
"""

# There is a potential start up race condition when running pymata3.
# This is a workaround for that race condition
#
Expand Down Expand Up @@ -1158,7 +1161,6 @@ async def stepper_step(self, motor_speed, number_of_steps):
abs_number_of_steps & 0x7f, abs_number_of_steps >> 7, direction]
await self._send_sysex(PrivateConstants.STEPPER_DATA, data)


async def pixy_init(self, max_blocks=5, cb=None, cb_type=None):
"""
Initialize Pixy and enable Pixy block reporting.
Expand All @@ -1167,17 +1169,16 @@ async def pixy_init(self, max_blocks=5, cb=None, cb_type=None):
:param cb: callback function to report Pixy blocks
:param cb_type: Constants.CB_TYPE_DIRECT = direct call or
Constants.CB_TYPE_ASYNCIO = asyncio coroutine
:param max_block: Maximum number of Pixy blocks to report when many signatures are found.
:param max_blocks: Maximum number of Pixy blocks to report when many signatures are found.
:returns: No return value.
"""
if cb:
self.digital_pins[PrivateConstants.PIN_PIXY_MOSI].cb = cb # Pixy uses SPI. Pin 11 is MOSI.
self.digital_pins[PrivateConstants.PIN_PIXY_MOSI].cb = cb # Pixy uses SPI. Pin 11 is MOSI.
if cb_type:
self.digital_pins[PrivateConstants.PIN_PIXY_MOSI].cb_type = cb_type
data = [PrivateConstants.PIXY_INIT, max_blocks & 0x7f]
await self._send_sysex(PrivateConstants.PIXY_CONFIG, data)


async def pixy_set_servos(self, s0, s1):
"""
Sends the setServos Pixy command.
Expand All @@ -1190,7 +1191,6 @@ async def pixy_set_servos(self, s0, s1):
data = [PrivateConstants.PIXY_SET_SERVOS, s0 & 0x7f, (s0 >> 7) & 0x7f, s1 & 0x7f, (s1 >> 7) & 0x7f]
await self._send_sysex(PrivateConstants.PIXY_CONFIG, data)


async def pixy_set_brightness(self, brightness):
"""
Sends the setBrightness Pixy command.
Expand All @@ -1202,7 +1202,6 @@ async def pixy_set_brightness(self, brightness):
data = [PrivateConstants.PIXY_SET_BRIGHTNESS, brightness & 0x7f, brightness >> 7]
await self._send_sysex(PrivateConstants.PIXY_CONFIG, data)


async def pixy_set_led(self, r, g, b):
"""
Sends the setLed Pixy command.
Expand All @@ -1216,7 +1215,6 @@ async def pixy_set_led(self, r, g, b):
data = [PrivateConstants.PIXY_SET_LED, r & 0x7f, r >> 7, g & 0x7f, g >> 7, b & 0x7f, b >> 7]
await self._send_sysex(PrivateConstants.PIXY_CONFIG, data)


async def _command_dispatcher(self):
"""
This is a private method.
Expand Down Expand Up @@ -1275,8 +1273,8 @@ async def _command_dispatcher(self):
logging.exception(ex)
else:
print(ex)
print("An exception occured on the asyncio event loop while receiving data. Invalid message.")
#raise # re-raise exception.
print("An exception occurred on the asyncio event loop while receiving data. Invalid message.")
# raise # re-raise exception.

'''
Firmata message handlers
Expand Down Expand Up @@ -1400,7 +1398,7 @@ async def _encoder_data(self, data):
loop = asyncio.get_event_loop()
loop.call_soon(self.digital_pins[pin].cb, hall_data)


# noinspection PyDictCreation
async def _pixy_data(self, data):
"""
This is a private message handler method.
Expand All @@ -1411,14 +1409,15 @@ async def _pixy_data(self, data):
"""
if len(self.digital_pins) < PrivateConstants.PIN_PIXY_MOSI:
# Pixy data sent before board finished pin discovery.
#print("Pixy data sent before board finished pin discovery.")
# print("Pixy data sent before board finished pin discovery.")
return

# strip off sysex start and end
data = data[1:-1]
num_blocks = data[0] # First byte is the number of blocks.
num_blocks = data[0] # First byte is the number of blocks.
# Prepare the new blocks list and then used it to overwrite the pixy_blocks.
blocks = []
# noinspection PyDictCreation
for i in range(num_blocks):
block = {}
block["signature"] = int((data[i * 12 + 2] << 7) + data[i * 12 + 1])
Expand All @@ -1436,7 +1435,6 @@ async def _pixy_data(self, data):
loop = asyncio.get_event_loop()
loop.call_soon(self.digital_pins[PrivateConstants.PIN_PIXY_MOSI].cb, blocks)


async def _i2c_reply(self, data):
"""
This is a private message handler method.
Expand Down

0 comments on commit 9fc44ad

Please sign in to comment.