summaryrefslogtreecommitdiff
path: root/include/py/homekit/pio/products.py
blob: a0e7a1f41ac7378c3b461cc0274452c42d7b025b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import logging

from io import StringIO
from collections import OrderedDict


_logger = logging.getLogger(__name__)
_products_dir = os.path.join(
    os.path.dirname(__file__),
    '..', '..', '..', '..',
    'pio'
)


def get_products():
    products = []
    for f in os.listdir(_products_dir):
        if f in ('common',):
            continue

        if os.path.isdir(os.path.join(_products_dir, f)):
            products.append(f)

    return products


def platformio_ini(product_config: dict,
                   target: str,
                   # node_id: str,
                   build_specific_defines: dict,
                   build_specific_defines_enums: list[str],
                   platform: str,
                   framework: str = 'arduino',
                   upload_port: str = '/dev/ttyUSB0',
                   monitor_speed: int = 115200,
                   debug=False,
                   debug_network=False) -> str:
    node_id = build_specific_defines['CONFIG_NODE_ID']

    # defines
    defines = {
        **product_config['common_defines'],
        'CONFIG_NODE_ID': node_id,
        'CONFIG_WIFI_AP_SSID': ('HK_'+node_id)[:31]
    }
    try:
        defines.update(product_config['target_defines'][target])
    except KeyError:
        pass
    defines['CONFIG_NODE_SECRET_SIZE'] = len(defines['CONFIG_NODE_SECRET'])
    defines['CONFIG_MQTT_CLIENT_ID'] = node_id

    build_type = 'release'
    if debug:
        defines['DEBUG'] = True
        build_type = 'debug'
    if debug_network:
        defines['DEBUG'] = True
        defines['DEBUG_ESP_SSL'] = True
        defines['DEBUG_ESP_PORT'] = 'Serial'
        build_type = 'debug'
    if build_specific_defines:
        for k, v in build_specific_defines.items():
            defines[k] = v
    defines = OrderedDict(sorted(defines.items(), key=lambda t: t[0]))

    # libs
    libs = []
    if 'common_libs' in product_config:
        libs.extend(product_config['common_libs'])
    if 'target_libs' in product_config and target in product_config['target_libs']:
        libs.extend(product_config['target_libs'][target])
    libs = list(set(libs))
    libs.sort()

    try:
        target_real_name = product_config['target_board_names'][target]
    except KeyError:
        target_real_name = target

    buf = StringIO()

    buf.write('; Generated by pio_ini.py\n\n')
    buf.write(f'[env:{target_real_name}]\n')
    buf.write(f'platform = {platform}\n')
    buf.write(f'board = {target_real_name}\n')
    buf.write(f'framework = {framework}\n')
    buf.write(f'upload_port = {upload_port}\n')
    buf.write(f'monitor_speed = {monitor_speed}\n')
    if libs:
        buf.write(f'lib_deps =\n')
        for lib in libs:
            if lib.startswith('homekit_'):
                lib = 'file://../../include/pio/libs/'+lib[8:]
            buf.write(f'    {lib}\n')
    buf.write(f'build_flags =\n')
    if defines:
        for name, value in defines.items():
            buf.write(f'    -D{name}')
            is_enum = name in build_specific_defines_enums
            if type(value) is not bool:
                buf.write('=')
                if type(value) is str:
                    if not is_enum:
                        buf.write('"\\"')
                    value = value.replace('"', '\\"')
                buf.write(f'{value}')
                if type(value) is str and not is_enum:
                    buf.write('"\\"')
            buf.write('\n')
        buf.write(f'    -I../../include/pio/include')
    buf.write(f'\nbuild_type = {build_type}')

    return buf.getvalue()