Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wstd2daisy #4

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.
11 changes: 7 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = json2daisy
version = 0.4.2
name = wstd2daisy
version = 0.5.3
license = MIT
author = Electrosmith
author_email = [email protected]
Expand All @@ -14,17 +14,20 @@ classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Operating System :: OS Independent
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12

[options]
include_package_data = True
package_dir =
= src
packages = find:
python_requires = >=3.7
python_requires = >=3.8
install_requires =
Jinja2>=2.11
importlib-resources; python_version=="3.8"
[options.packages.find]
where = src
33 changes: 19 additions & 14 deletions src/json2daisy/json2daisy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import jinja2
import json
import os
import pkg_resources

try:
from importlib_resources import files as resource_files # type: ignore
except ImportError:
from importlib.resources import files as resource_files

from typing import Optional

Expand All @@ -11,7 +15,7 @@
# helper for loading and processing the definitions, component list, etc
def map_load(pair: list):
# load the default components
comp_string = pkg_resources.resource_string(__name__, json_defs_file)
comp_string = resource_files('json2daisy').joinpath(json_defs_file).read_text()
component_defs = json.loads(comp_string)

pair[1]['name'] = pair[0]
Expand Down Expand Up @@ -193,17 +197,17 @@ def generate_header(board_description_dict: dict) -> 'tuple[str, dict]':
target['aliases'] = {}

if 'display' in target:
# apply defaults
target['display'] = {
'driver': "daisy::SSD130x4WireSpi128x64Driver",
'config': [],
'dim': [128, 64]
}
# apply defaults if not present in config
target['display']['driver'] = target['display'].get('driver', "daisy::SSD130x4WireSpi128x64Driver")
target['display']['config'] = target['display'].get('config', [])
target['display']['dim'] = target['display'].get('dim', [128, 64])

target['defines']['OOPSY_TARGET_HAS_OLED'] = 1
target['defines']['OOPSY_OLED_DISPLAY_WIDTH'] = target['display']['dim'][0]
target['defines']['OOPSY_OLED_DISPLAY_HEIGHT'] = target['display']['dim'][1]

target['displayprocess'] = target['display'].get('process', '')

replacements = {}
replacements['name'] = target['name']
replacements['som'] = som
Expand Down Expand Up @@ -232,7 +236,7 @@ def generate_header(board_description_dict: dict) -> 'tuple[str, dict]':
components, 'component', ['AnalogControl', 'AnalogControlBipolar'],
'map_init', key_exclude='default', match_exclude=True)

comp_string = pkg_resources.resource_string(__name__, json_defs_file)
comp_string = resource_files('json2daisy').joinpath(json_defs_file).read_text()
definitions_dict = json.loads(comp_string)

for name in definitions_dict:
Expand Down Expand Up @@ -270,7 +274,7 @@ def generate_header(board_description_dict: dict) -> 'tuple[str, dict]':
replacements['hidupdaterates'] = filter_map_template(
components, 'updaterate', key_exclude='default', match_exclude=True)

license_string = pkg_resources.resource_string(__package__, 'resources/LICENSE').decode('utf-8')
license_string = resource_files('json2daisy').joinpath('resources/LICENSE').read_text()
replacements['license'] = '/*\n * ' + '\n * '.join([line for line in license_string.split('\n')]) + '\n */'

component_declarations = list(filter(lambda x: not x.get('default', False), components))
Expand All @@ -296,12 +300,12 @@ def generate_header(board_description_dict: dict) -> 'tuple[str, dict]':
# rendered_header = env.get_template('daisy.h').render(replacements)

# This following works, but is really annoying
header_str = pkg_resources.resource_string(__name__, os.path.join('templates', 'daisy.h'))
header_str = resource_files('json2daisy').joinpath(os.path.join('templates', 'daisy.h')).read_text()
header_env = jinja2.Environment(
loader=jinja2.BaseLoader(),
trim_blocks=True,
lstrip_blocks=True
).from_string(header_str.decode('utf-8'))
).from_string(header_str)

rendered_header = header_env.render(replacements)

Expand All @@ -322,7 +326,8 @@ def generate_header(board_description_dict: dict) -> 'tuple[str, dict]':
'components': components,
'aliases': target['aliases'],
'channels': audio_channels,
'has_midi': target.get('has_midi', False)
'has_midi': target.get('has_midi', False),
'displayprocess': target.get('displayprocess', '')
}

return rendered_header, board_info
Expand Down Expand Up @@ -360,7 +365,7 @@ def generate_header_from_name(board_name: str) -> 'tuple[str, dict]':

try:
description_file = os.path.join('resources', f'{board_name}.json')
daisy_description = pkg_resources.resource_string(__name__, description_file)
daisy_description = resource_files('json2daisy').joinpath(description_file).read_text()
daisy_description_dict = json.loads(daisy_description)
except FileNotFoundError:
raise FileNotFoundError(f'Unknown Daisy board "{board_name}"')
Expand Down
4 changes: 2 additions & 2 deletions src/json2daisy/resources/component_defs.json
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@
]
},
"Mpr121": {
"map_init": "daisy::Mpr121I2CTransport::Config {name}_config;\n {name}_config.periph = {periph};\n {name}_config.speed = {speed};\n {name}_config.scl = som.GetPin({pin_scl});\n {name}_config.sda = som.GetPin({pin_sda});\n {name}_config.dev_addr = {address};\n daisy::Mpr121I2C::Config {name}_main_conf;\n {name}_main_conf.transport_config = {name}_config;\n {name}_main_conf.touch_threshold = {touch_threshold};\n {name}_main_conf.release_threshold = {release_threshold};\n {name}.Init({name}_main_conf);",
"map_init": "daisy::Mpr121I2CTransport::Config {name}_config;\n {name}_config.periph = {periph};\n {name}_config.speed = {speed};\n {name}_config.dev_addr = {address};\n daisy::Mpr121I2C::Config {name}_main_conf;\n {name}_main_conf.transport_config = {name}_config;\n {name}_main_conf.touch_threshold = {touch_threshold};\n {name}_main_conf.release_threshold = {release_threshold};\n {name}.Init({name}_main_conf);",
"typename": "daisy::Mpr121I2C",
"direction": "in",
"pin": "scl,sda",
Expand Down Expand Up @@ -1389,4 +1389,4 @@
}
]
}
}
}
4 changes: 2 additions & 2 deletions src/json2daisy/resources/component_defs_patchsm.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
]
},
"Mpr121": {
"map_init": "daisy::Mpr121I2CTransport::Config {name}_config;\n {name}_config.periph = {periph};\n {name}_config.speed = {speed};\n {name}_config.scl = som.GetPin({pin_scl});\n {name}_config.sda = som.GetPin({pin_sda});\n {name}_config.dev_addr = {address};\n daisy::Mpr121I2C::Config {name}_main_conf;\n {name}_main_conf.transport_config = {name}_config;\n {name}_main_conf.touch_threshold = {touch_threshold};\n {name}_main_conf.release_threshold = {release_threshold};\n {name}.Init({name}_main_conf);",
"map_init": "daisy::Mpr121I2CTransport::Config {name}_config;\n {name}_config.periph = {periph};\n {name}_config.speed = {speed};\n {name}_config.dev_addr = {address};\n daisy::Mpr121I2C::Config {name}_main_conf;\n {name}_main_conf.transport_config = {name}_config;\n {name}_main_conf.touch_threshold = {touch_threshold};\n {name}_main_conf.release_threshold = {release_threshold};\n {name}.Init({name}_main_conf);",
"typename": "daisy::Mpr121I2C",
"direction": "in",
"pin": "scl,sda",
Expand Down Expand Up @@ -1155,4 +1155,4 @@
}
]
}
}
}
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ python =
; Test config
[testenv]
deps =
jinja2
-rrequirements.txt
pytest-cov
commands =
python -m pytest --cov-config=tox.ini --cov=json2daisy tests/
Expand All @@ -29,13 +29,13 @@ commands =

[testenv:mypy]
deps =
jinja2
-rrequirements.txt
types-setuptools
mypy
basepython =
python3
commands =
mypy .
mypy src tests

[run]
ignore = examples/*
Expand Down
Loading