♻️ refactor to path objects, config loading

refactor to use pathlib objects, add config loading
This commit is contained in:
Peter Buchegger 2021-11-07 22:06:10 +00:00
parent 8da951c517
commit 161a9c8185
3 changed files with 49 additions and 22 deletions

View file

@ -2,6 +2,10 @@ import os
import pytest
import serial
import logging
import json
import tempfile
from pathlib import Path
from HIL.common import runProcess
logger = logging.getLogger(__name__)
@ -9,14 +13,15 @@ logger = logging.getLogger(__name__)
class EspFlash:
def __init__(self, port):
self.pio_package_path = "$HOME/.platformio/packages"
self.port = port
logger.info(f"pio package path: {self.pio_package_path}")
logger.info(f"port: {self.port}")
self._pio_package_path = Path('$HOME') / '.platformio' / 'packages'
self._port = port
logger.info(f"pio package path: {self._pio_package_path}")
logger.info(f"port: {self._port}")
def runESPTool(self, cmd):
esp_tool = self._pio_package_path / 'tool-esptoolpy' / 'esptool.py'
runProcess(
f"/usr/bin/python3 {self.pio_package_path}/tool-esptoolpy/esptool.py --chip esp32 --port {self.port} {cmd}")
f"/usr/bin/python3 {esp_tool} --chip esp32 --port {self._port} {cmd}")
def erase(self):
logger.info("erase flash")
@ -34,29 +39,49 @@ class EspFlash:
def make_spiffs(self, fs_path, fs_bin):
logger.info(f"make spiffs, fs_path: {fs_path}, fs_bin: {fs_bin}")
mkspiffs_espressif32_arduino = self._pio_package_path / \
'tool-mkspiffs' / 'mkspiffs_espressif32_arduino'
runProcess(
f"{self.pio_package_path}/tool-mkspiffs/mkspiffs_espressif32_arduino -c {fs_path} -p 256 -b 4096 -s 1507328 {fs_bin}")
f"{mkspiffs_espressif32_arduino} -c {fs_path} -p 256 -b 4096 -s 1507328 {fs_bin}")
class EspConfig:
def __init__(self, path, filename):
self.filename = filename
logger.info(f"reading config from: {path}/{self.filename}")
with open(path / self.filename) as json_file:
self.data = json.load(json_file)
logger.info(f"was reading this data: {self.data}")
def writeFile(self):
tmpdirname = tempfile.TemporaryDirectory()
logger.info(f"writing this data: {self.data}")
with open(Path(tmpdirname.name) / self.filename, 'w') as outfile:
json.dump(self.data, outfile)
return tmpdirname
class EspDut:
def __init__(self, port):
def __init__(self, port, configPath, configFile):
self.port = port
self.baudrate = 115200
self.serial = None
self.flash = EspFlash(self.port)
self.config = EspConfig(configPath, configFile)
def writeFlash(self, bin_dir):
logger.info("write flash")
self.flash.erase()
self.flash.write("0x1000", f"{bin_dir}/bootloader_dio_40m.bin")
self.flash.write("0x8000", f"{bin_dir}/partitions.bin")
self.flash.write("0xe000", f"{bin_dir}/boot_app0.bin")
self.flash.write("0x10000", f"{bin_dir}/firmware.bin")
self.flash.write("0x1000", bin_dir / 'bootloader_dio_40m.bin')
self.flash.write("0x8000", bin_dir / 'partitions.bin')
self.flash.write("0xe000", bin_dir / 'boot_app0.bin')
self.flash.write("0x10000", bin_dir / 'firmware.bin')
def writeConfig(self, fs_path):
def writeConfig(self):
logger.info("write config")
fs_bin = "spiffs.bin"
self.flash.make_spiffs(fs_path, fs_bin)
fs_path = self.config.writeFile()
self.flash.make_spiffs(fs_path.name, fs_bin)
self.flash.write("0x290000", fs_bin)
def openPort(self):
@ -75,4 +100,4 @@ class EspDut:
@pytest.fixture
def ESP():
return EspDut(os.environ["ESP_PORT"])
return EspDut(os.environ["ESP_PORT"], Path(os.environ["ESP_CONFIG_PATH"]), Path(os.environ["ESP_CONFIG_FILE"]))

View file

@ -1 +1 @@
[]
{}

View file

@ -1,5 +1,6 @@
from HIL.esp_dut import ESP
from HIL.aprs_con import APRSIS, AprsIs
from pathlib import Path
def test_basic_port(ESP):
@ -8,17 +9,18 @@ def test_basic_port(ESP):
def test_flash(ESP):
bin_dir = "testbinary"
bin_dir = Path("testbinary")
ESP.writeFlash(bin_dir)
ESP.flash.verify("0x1000", f"{bin_dir}/bootloader_dio_40m.bin")
ESP.flash.verify("0x8000", f"{bin_dir}/partitions.bin")
ESP.flash.verify("0xe000", f"{bin_dir}/boot_app0.bin")
ESP.flash.verify("0x10000", f"{bin_dir}/firmware.bin")
ESP.flash.verify("0x1000", bin_dir / 'bootloader_dio_40m.bin')
ESP.flash.verify("0x8000", bin_dir / 'partitions.bin')
ESP.flash.verify("0xe000", bin_dir / 'boot_app0.bin')
ESP.flash.verify("0x10000", bin_dir / 'firmware.bin')
def test_flash_config(ESP):
config_dir = "testconfig"
ESP.writeConfig(config_dir)
ESP.config.data["bar"] = "foo"
ESP.config.data["number"] = 123
ESP.writeConfig()
def test_erase(ESP):