<commit_before>#!/usr/bin/env python
# encoding: utf-8
"""
Control All Arduino Functions
"""

from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants

class Arduino():
  # Define Pin Constants
  _MOTOR1 = 3
  _MOTOR1_DIR_A = 2
  _MOTOR1_DIR_B = 4

  _MOTOR2 = 6
  _MOTOR2_DIR_A = 7
  _MOTOR2_DIR_B = 8

  # Note: ping sensor shouldn't have to be PWM
  _PING = 12

  _LED = 13


  def __init__(self):
    # Instantiate the pymata_core API
    self.board = PyMata3()

    # Set the pin modes
    self.board.set_pin_mode(self._MOTOR1, Constants.PWM)
    self.board.set_pin_mode(self._MOTOR1_DIR_A, Constants.OUTPUT)
    self.board.set_pin_mode(self._MOTOR1_DIR_B, Constants.OUTPUT)

    self.board.set_pin_mode(self._MOTOR2, Constants.PWM)
    self.board.set_pin_mode(self._MOTOR2_DIR_A, Constants.OUTPUT)
    self.board.set_pin_mode(self._MOTOR2_DIR_B, Constants.OUTPUT)

    self.board.sonar_config(self._PING, self._PING)

    self.board.set_pin_mode(self._LED, Constants.OUTPUT)

  def set_motors(self, motor1, motor2):
    if (motor1 < -1 or motor1 > 1 or motor2 < -1 or motor2 > 1):
      raise ValueError("set_motor called with (motor1=" + str(motor1) + ") and (motor2=" + str(motor2) + ")")

    print("Setting Motor 1 to: " + str(motor1))
    print("Setting Motor 2 to: " + str(motor2))

    # Set motor directions
    self.board.digital_write(self._MOTOR1_DIR_A, 0 if (motor1 < 0) else 1)
    self.board.digital_write(self._MOTOR1_DIR_B, 1 if (motor1 < 0) else 0)

    self.board.digital_write(self._MOTOR2_DIR_A, 1 if (motor2 < 0) else 0)
    self.board.digital_write(self._MOTOR2_DIR_B, 0 if (motor2 < 0) else 1)

    # Set motor speeds
    self.board.analog_write(self._MOTOR1, int(abs(motor1) * 255))
    self.board.analog_write(self._MOTOR2, int(abs(motor2) * 255))

  # Get the ping sensor's distance in cm
  # TODO: Consider using callbacks?
  def get_ping(self):
    return self.board.sonar_data_retrieve(self._PING)
    # data = self.board.sonar_data_retrieve(self._PING)
    # return None if (data == None) else data[self._PING]
<commit_msg>Add a guess for how to use the pixy. This is untested.<commit_after>#!/usr/bin/env python
# encoding: utf-8
"""
Control All Arduino Functions
"""

from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants

class Arduino():
  # Define Pin Constants
  _MOTOR1 = 3
  _MOTOR1_DIR_A = 2
  _MOTOR1_DIR_B = 4

  _MOTOR2 = 6
  _MOTOR2_DIR_A = 7
  _MOTOR2_DIR_B = 8

  # Note: ping sensor shouldn't have to be PWM
  _PING = 12

  _LED = 13


  def __init__(self):
    # Instantiate the pymata_core API
    self.board = PyMata3()

    # Set the pin modes
    self.board.set_pin_mode(self._MOTOR1, Constants.PWM)
    self.board.set_pin_mode(self._MOTOR1_DIR_A, Constants.OUTPUT)
    self.board.set_pin_mode(self._MOTOR1_DIR_B, Constants.OUTPUT)

    self.board.set_pin_mode(self._MOTOR2, Constants.PWM)
    self.board.set_pin_mode(self._MOTOR2_DIR_A, Constants.OUTPUT)
    self.board.set_pin_mode(self._MOTOR2_DIR_B, Constants.OUTPUT)

    self.board.sonar_config(self._PING, self._PING)

    self.board.pixy_init()

    self.board.set_pin_mode(self._LED, Constants.OUTPUT)

  def set_motors(self, motor1, motor2):
    if (motor1 < -1 or motor1 > 1 or motor2 < -1 or motor2 > 1):
      raise ValueError("set_motor called with (motor1=" + str(motor1) + ") and (motor2=" + str(motor2) + ")")

    print("Setting Motor 1 to: " + str(motor1))
    print("Setting Motor 2 to: " + str(motor2))

    # Set motor directions
    self.board.digital_write(self._MOTOR1_DIR_A, 0 if (motor1 < 0) else 1)
    self.board.digital_write(self._MOTOR1_DIR_B, 1 if (motor1 < 0) else 0)

    self.board.digital_write(self._MOTOR2_DIR_A, 1 if (motor2 < 0) else 0)
    self.board.digital_write(self._MOTOR2_DIR_B, 0 if (motor2 < 0) else 1)

    # Set motor speeds
    self.board.analog_write(self._MOTOR1, int(abs(motor1) * 255))
    self.board.analog_write(self._MOTOR2, int(abs(motor2) * 255))

  # Get the ping sensor's distance in cm
  # TODO: Consider using callbacks?
  def get_ping(self):
    return self.board.sonar_data_retrieve(self._PING)
    # data = self.board.sonar_data_retrieve(self._PING)
    # return None if (data == None) else data[self._PING]

  # TODO: Returns the value from the pixy camera
  def get_pixy(self):
    return self.board.pixy_get_blocks()

{'fraction_non_alphanumeric': 0.11335939296389974, 'fraction_numerical': 0.03403081168084617, 'mean_word_length': 3.7489082969432315, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3490629', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1684, 'n_words': 368}