title: Call Arduino C/C++ library function from Python
body: <p>I am trying to do call Arduino C++ library <a href="https://github.com/adafruit/Adafruit-Motor-Shield-library/blob/master/AFMotor.cpp" rel="nofollow noreferrer">Adafruit-Motor-Shield-library
  </a> from the python3.</p>
  <p>I have tried with several workarounds. For instance, <a href="https://pypi.org/project/arduino-python3/#:%7E:text=The%20Arduino%2DPython3%20Command%20API,custom%20protocol%2C%20similar%20to%20Firmata." rel="nofollow noreferrer">Arduino-Python3 Command API
  </a><strong>,</strong>  <a href="https://pypi.org/project/pyserial/" rel="nofollow noreferrer">pyserial</a> <strong>,</strong> and <a href="https://pypi.org/project/pyFirmata/" rel="nofollow noreferrer">pyFirmata</a> but they have limited support and I want to call <a href="https://github.com/adafruit/Adafruit-Motor-Shield-library/blob/master/AFMotor.cpp" rel="nofollow noreferrer">Adafruit-Motor-Shield-library
  </a> functions from python3.</p>
  <p>Also, I've tried with the python subprocess which works but I don't want to use .ino/C++ file.</p>
  <p>For instance here is the <strong>python script which uses subprocess call for .ino C++ file.</strong></p>
  <pre class="lang-sh prettyprint-override"><code># TODO: Testing
  if __name__ == &quot;__main__&quot;:
      import subprocess
      import sys
  
      # next line must be edited to be appropriate for your PC
      arduinoProg = &quot;/home/arslan/Downloads/arduino-1.8.13/arduino&quot;
  
      # projectFile = sys.argv[1]
      projectFile = &quot;DCM2.ino&quot;
      codeFile = open(projectFile, 'r')
      startLine = codeFile.readline()[3:].strip()
      actionLine = codeFile.readline()[3:].strip()
      boardLine = codeFile.readline()[3:].strip()
      portLine = codeFile.readline()[3:].strip()
      endLine = codeFile.readline()[3:].strip()
      codeFile.close()
  
      if startLine != &quot;python-build-start&quot; or endLine != &quot;python-build-end&quot;:
          print(&quot;Sorry, can't process file&quot;)
          sys.exit()
  
      arduinoCommand = arduinoProg + &quot; --&quot; + actionLine + &quot; --board &quot; + boardLine + &quot; --port &quot; + portLine + &quot; &quot; + projectFile
  
      print(&quot;\n\n -- Arduino Command --&quot;)
      print(arduinoCommand)
  
      print(&quot;-- Starting %s --\n&quot; % (actionLine))
  
      presult = subprocess.call(arduinoCommand, shell=True)
  
      if presult != 0:
          print(&quot;\n Failed - result code = %s --&quot; % (presult))
      else:
          print(&quot;\n-- Success --&quot;)
  
  </code></pre>
  <p>Lastly, will python <code>Boost.Python library</code> can handle this issue? Is there any way to make <code>shared dll</code> for <a href="https://github.com/adafruit/Adafruit-Motor-Shield-library/blob/master/AFMotor.cpp" rel="nofollow noreferrer">Adafruit-Motor-Shield-library
  </a> from the python3 and then call from python3 without overwhelming manual changes in src/include of <code>Adafruit-Motor-Shield-library.</code></p>
  <p>For instance, I want to convert this simple Arduino code to <strong>Python3</strong>.</p>
  <pre class="lang-sh prettyprint-override"><code>// Arduino code
  #include &lt;AFMotor.h&gt;
  #include &lt;SoftwareSerial.h&gt;
  
  void setup() {
    // put your setup code here, to run once:
  AF_DCMotor Leftmotor1(1);  
  AF_DCMotor Rightmotor2(2);  
  AF_DCMotor Rightmotor3(3); 
  AF_DCMotor Leftmotor4(4); 
  
  
  }
  
  void loop() {
    // put your main code here, to run repeatedly:
   Leftmotor1.setSpeed(SpeedL1);
    Leftmotor1.run(RELEASE);
    Leftmotor1.run(FORWARD);
    Rightmotor2.setSpeed(SpeedR2);
   }
  </code></pre>
  <p>I will appreciate for any comments, and thanks in advnace.</p>