Are there docs anywhere on integrating platformio into my own tool? I use it for development, but I want to also integrate it in a small GUI application for flashing the microcontroller. The app already uses python and kivy so I am guessing adding platformio tools (basically just pio run
) would probably not be hard. But I can’t find any api docs. Do they exist?
PlatformIO’s API is the command line interface, i.e. the pio
command line tool.
The documentation is here: CLI Guide — PlatformIO latest documentation
Ok, I want to use it from code though, not the cli. I guess that is not an intended use-case maybe, but I managed it anyway
For those looking for examples, this is what I produced quickly by digging through the code. It’s not complete yet but a good start. Notably there is no error handling in there at all.
"""Provides the platformio API"""
import logging
from multiprocessing import cpu_count
from pathlib import Path
from platformio import fs
from platformio.platform.factory import PlatformFactory
from platformio.project.config import ProjectConfig
try:
DEFAULT_JOB_NUMS = cpu_count()
except NotImplementedError:
DEFAULT_JOB_NUMS = 1
BASEPATH = Path(__file__).parent.resolve()
class PlatformIOApi:
"""PlatformIO API"""
def __init__(self, code_path=BASEPATH / 'code'):
self.log = logging.getLogger(self.__class__.__name__)
self.code_path = code_path
def compile_sketch(self, name):
"""Compile a sketch."""
self.log.info("Compiling sketch %s", name)
self.pio_run(name)
def pio_run(self, name, target=None):
"""Run a platformio target."""
if target is None:
target = []
self.log.info("Running platformio with target %s for env %s", target,
name)
with fs.cd(self.code_path):
pio_config = ProjectConfig.get_instance()
pio_config.validate([name])
variables = {'pioenv': name, 'project_config': pio_config.path}
options = pio_config.items(env=name, as_dict=True)
platform = PlatformFactory.new(options['platform'])
platform.run(variables, target, False, False, cpu_count())
def upload_sketch(self, name):
"""Upload a sketch to a device"""
self.log.info("Uploading environment %s", name)
self.pio_run(name, ['upload'])
1 Like