run_platformio.py 2.73 KB
Newer Older
Poseidon's avatar
Poseidon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess
import dialog
import os

TOP_BOARDS = ['uno', 'diecimilaatmega328', 'megaatmega2560', 'nanoatmega328',
              'leonardo']

SOURCE_DIR = "firmware/respirometro"

shexec = lambda command:\
    subprocess.check_output(command, shell=True).decode('ascii')

def get_plataformio_serial_ports():
    '''

    '''
    serial_raw = shexec("platformio serialports list").splitlines()
18
19
20
21
    if serial_raw == []:
        return [("ERRO", "ERRO: nenhuma porta serial encontrada")]
    else:
        serial_ports = [serial_raw[i-1] 
Poseidon's avatar
Poseidon committed
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
            for i,x in enumerate(serial_raw) if '----' in x]
    return [(x,x) for x in serial_ports]


def get_platformio_atmelavr_boards(top_boards=[]):
    ''' Executes platformio board list command and parse results to a
        nice and clean Python list of tuples.
    '''
    boards_lines = shexec("platformio boards atmelavr").splitlines()[5:]

    boards, top_names = [], {}
    for b in boards_lines:
        cols = [x.strip() for x in b.split('  ') if x.strip() != '']
        if cols[0] in top_boards:
            top_names[cols[0]] = cols[-1]
        else:
            boards.append((cols[0], cols[-1]))

    return [(x, top_names[x]) for x in TOP_BOARDS] + boards


def exec_dialog_select(description, choices):
    ''' Launch a dialog with a list of boards for the user to choose.
        Return the platformio board name selected.
    '''

    d = dialog.Dialog(dialog="dialog")
    code, tag = d.menu(description, choices=choices,
                       no_cancel=True, no_ok=True, no_tags=True)
    return tag

def exec_dialog_select_serial_port():
    ''' Launch a dialog with a list of boards for the user to choose.
        Return the platformio board name selected.
    '''
    return exec_dialog_select(
            description = "Select your serialport:", 
            choices = get_plataformio_serial_ports())


def exec_dialog_select_board():
    ''' Launch a dialog with a list of boards for the user to choose.
        Return the platformio board name selected.
    '''
    return exec_dialog_select(
            description = "Select your board:", 
            choices = get_platformio_atmelavr_boards(top_boards=TOP_BOARDS))


def platformio_setup(source_dir, build_dir='.build'):
    board_name = exec_dialog_select_board()
    serial_port = exec_dialog_select_serial_port()
74
75
76
77
78
79
80
81
82
83
84
    if serial_port != "ERRO":
        os.system("""
            rm -rf {build_dir} &&
            mkdir -p {build_dir} &&
            cd {build_dir} &&
            yes | platformio init --board {board_name} &&
            cp -r ../{source_dir}/* src/. &&
            platformio run -t upload --upload-port {serial_port}
            """.format(**locals()).strip())
    else:
        os.system("clear")
Poseidon's avatar
Poseidon committed
85
86
87

platformio_setup(source_dir=SOURCE_DIR)