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

Commit

Permalink
Fix sending i2c bytes by sending them as two 7-bit bytes.
Browse files Browse the repository at this point in the history
Mask all bytes with 0x7f to make sure all bytes sent are 7 bit data.
  • Loading branch information
FozzTexx committed Feb 17, 2016
1 parent 61b11b4 commit f355d63
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions PyMata/pymata.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def analog_write(self, pin, value):
"""

if self._command_handler.ANALOG_MESSAGE + pin < 0xf0:
command = [self._command_handler.ANALOG_MESSAGE + pin, value & 0x7f, value >> 7]
command = [self._command_handler.ANALOG_MESSAGE + pin, value & 0x7f, (value >> 7) & 0x7f]
self._command_handler.send_command(command)
else:
self.extended_analog(pin, value)
Expand Down Expand Up @@ -302,7 +302,7 @@ def digital_write(self, pin, value):

# Assemble the command
command = (calculated_command, self.digital_output_port_pins[port] & 0x7f,
self.digital_output_port_pins[port] >> 7)
(self.digital_output_port_pins[port] >> 7) & 0x7f)

self._command_handler.send_command(command)

Expand Down Expand Up @@ -386,7 +386,7 @@ def extended_analog(self, pin, data):
@param pin: 0 - 127
@param data: 0 - 0xfffff
"""
analog_data = [pin, data & 0x7f, (data >> 7) & 0x7f, data >> 14]
analog_data = [pin, data & 0x7f, (data >> 7) & 0x7f, (data >> 14) & 0x3fff]
self._command_handler.send_sysex(self._command_handler.EXTENDED_ANALOG, analog_data)


Expand Down Expand Up @@ -535,7 +535,7 @@ def i2c_config(self, read_delay_time=0, pin_type=None, clk_pin=0, data_pin=0):
@param data_pin: pin number (see comment above).
@return: No Return Value
"""
data = [read_delay_time & 0x7f, read_delay_time >> 7]
data = [read_delay_time & 0x7f, (read_delay_time >> 7) & 0x7f]
self._command_handler.send_sysex(self._command_handler.I2C_CONFIG, data)

# If pin type is set, set pin mode in appropriate response table for these pins
Expand Down Expand Up @@ -563,8 +563,8 @@ def i2c_read(self, address, register, number_of_bytes, read_type, cb=None):
@param read_type: I2C_READ or I2C_READ_CONTINUOUSLY
@param cb: Optional callback function to report i2c data as result of read command
"""
data = [address, read_type, register & 0x7f, register >> 7,
number_of_bytes & 0x7f, number_of_bytes >> 7]
data = [address, read_type, register & 0x7f, (register >> 7) & 0x7f,
number_of_bytes & 0x7f, (number_of_bytes >> 7) & 0x7f]

# add or update entry in i2c_map for reply
self._command_handler.i2c_map[address] = [cb, None]
Expand All @@ -580,7 +580,8 @@ def i2c_write(self, address, *args):
"""
data = [address, self.I2C_WRITE]
for item in args:
data.append(item)
data.append(item & 0x7f)
data.append((item >> 7) & 0x7f)
self._command_handler.send_sysex(self._command_handler.I2C_REQUEST, data)


Expand Down Expand Up @@ -630,10 +631,10 @@ def play_tone(self, pin, tone_command, frequency, duration):
if tone_command == self.TONE_TONE:
# duration is specified
if duration:
data = [tone_command, pin, frequency & 0x7f, frequency >> 7, duration & 0x7f, duration >> 7]
data = [tone_command, pin, frequency & 0x7f, (frequency >> 7) & 0x7f, duration & 0x7f, (duration >> 7) & 0x7f]

else:
data = [tone_command, pin, frequency & 0x7f, frequency >> 7, 0, 0]
data = [tone_command, pin, frequency & 0x7f, (frequency >> 7) & 0x7f, 0, 0]

self._command_handler.digital_response_table[pin][self._command_handler.RESPONSE_TABLE_MODE] = \
self.TONE
Expand Down Expand Up @@ -765,7 +766,7 @@ def set_sampling_interval(self, interval):
@param interval: Integer value for desired sampling interval in milliseconds
@return: No return value.
"""
data = [interval & 0x7f, interval >> 7]
data = [interval & 0x7f, (interval >> 7) & 0x7f]
self._command_handler.send_sysex(self._command_handler.SAMPLING_INTERVAL, data)


Expand All @@ -778,8 +779,8 @@ def servo_config(self, pin, min_pulse=544, max_pulse=2400):
@return: No return value
"""
self.set_pin_mode(pin, self.SERVO, self.OUTPUT)
command = [pin, min_pulse & 0x7f, min_pulse >> 7, max_pulse & 0x7f,
max_pulse >> 7]
command = [pin, min_pulse & 0x7f, (min_pulse >> 7) & 0x7f,
max_pulse & 0x7f, (max_pulse >> 7) & 0x7f]

self._command_handler.send_sysex(self._command_handler.SERVO_CONFIG, command)

Expand All @@ -800,7 +801,7 @@ def sonar_config(self, trigger_pin, echo_pin, cb=None, ping_interval=50, max_dis
if max_distance > 200:
max_distance = 200
max_distance_lsb = max_distance & 0x7f
max_distance_msb = max_distance >> 7
max_distance_msb = (max_distance >> 7) & 0x7f
data = [trigger_pin, echo_pin, ping_interval, max_distance_lsb, max_distance_msb]
self.set_pin_mode(trigger_pin, self.SONAR, self.INPUT)
self.set_pin_mode(echo_pin, self.SONAR, self.INPUT)
Expand All @@ -823,7 +824,7 @@ def stepper_config(self, steps_per_revolution, stepper_pins):
@param steps_per_revolution: number of steps per motor revolution
@param stepper_pins: a list of control pin numbers - either 4 or 2
"""
data = [self.STEPPER_CONFIGURE, steps_per_revolution & 0x7f, steps_per_revolution >> 7]
data = [self.STEPPER_CONFIGURE, steps_per_revolution & 0x7f, (steps_per_revolution >> 7) & 0x7f]
for pin in range(len(stepper_pins)):
data.append(stepper_pins[pin])
self._command_handler.send_sysex(self._command_handler.STEPPER_DATA, data)
Expand All @@ -841,8 +842,8 @@ def stepper_step(self, motor_speed, number_of_steps):
else:
direction = 0
abs_number_of_steps = abs(number_of_steps)
data = [self.STEPPER_STEP, motor_speed & 0x7f, (motor_speed >> 7) & 0x7f, motor_speed >> 14,
abs_number_of_steps & 0x7f, abs_number_of_steps >> 7, direction]
data = [self.STEPPER_STEP, motor_speed & 0x7f, (motor_speed >> 7) & 0x7f, (motor_speed >> 14) & 0x3fff,
abs_number_of_steps & 0x7f, (abs_number_of_steps >> 7) & 0x7f, direction]
self._command_handler.send_sysex(self._command_handler.STEPPER_DATA, data)


Expand Down

0 comments on commit f355d63

Please sign in to comment.