-
Notifications
You must be signed in to change notification settings - Fork 60
/
build-arduino-library.py
114 lines (92 loc) · 4.1 KB
/
build-arduino-library.py
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
from __future__ import (print_function)
'''
EEZ PSU Firmware
Copyright (C) 2015 Envox d.o.o.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
'''
This script creates scpi-parser library that is compatible with Arduino IDE.
As a convinience, it also copies a newly create library into arduino libraries
directory so any Arduino sketch can see it.
Execute this script if scpi-parser is changed or scpi_user_config.h (from this directory) is changed.
'''
import os
import platform
import sys
import shutil
def rm_then_cp(src, dest, ignoreRootDirs=None):
def copytreeIgnore(d, files):
if ignoreRootDirs is not None:
if d == src:
return ignoreRootDirs
return []
if os.path.exists(dest):
shutil.rmtree(dest)
shutil.copytree(src, dest, ignore=copytreeIgnore)
def build_scpi_parser_lib(libscpi_dir, scpi_parser_dir):
'''
Build scpi-parser as arduino compatible library
'''
# copy *.h files
rm_then_cp(os.path.join(libscpi_dir, 'inc/scpi'), os.path.join(scpi_parser_dir, 'src/scpi'))
# modify config.h
config_h_file_path = os.path.join(scpi_parser_dir, 'src/scpi/config.h')
config_h_file = open(config_h_file_path)
tmp_file_path = config_h_file_path + ".tmp"
tmp_file = open(tmp_file_path, "w")
for line in config_h_file:
if line == '#ifdef SCPI_USER_CONFIG\n':
tmp_file.write('// This is automatically added by the build-arduino-library.py\n')
tmp_file.write('#define SCPI_USER_CONFIG 1\n')
tmp_file.write('\n')
tmp_file.write(line)
config_h_file.close()
tmp_file.close()
os.unlink(config_h_file_path)
os.rename(tmp_file_path, config_h_file_path)
# copy scpi_user_config.h
shutil.copyfile(os.path.join(os.path.dirname(__file__), 'eez_psu_sketch/scpi_user_config.h'), os.path.join(scpi_parser_dir, 'src/scpi/scpi_user_config.h'))
# copy *.c files
rm_then_cp(os.path.join(libscpi_dir, 'src'), os.path.join(scpi_parser_dir, 'src/impl'))
def build_UIPEthernet_lib(arduino_uip_dir, UIPEthernet_dir):
rm_then_cp(os.path.join(arduino_uip_dir, 'UIPEthernet'), UIPEthernet_dir, ['examples'])
def build_Ethernet2_lib(arduino_Ethernet2_dir, Ethernet2_dir):
rm_then_cp(arduino_Ethernet2_dir, Ethernet2_dir, ['.git', '.github', 'examples'])
def copy_lib(src_lib_dir, dst_name):
#
# find arduino libraries directory
#
ARDUINO_LIB_DIR_CANDIDATES = {
'Linux': ['Arduino/libraries/', 'Documents/Arduino/libraries/'],
'Darwin': ['Documents/Arduino/libraries/'],
'Windows': ['Documents\\Arduino\\libraries\\', 'My Documents\\Arduino\\libraries\\']
}
home_dir = os.path.expanduser('~')
arduino_libs_dir = None
candidates = ARDUINO_LIB_DIR_CANDIDATES.get(platform.system())
if candidates:
for candidate_dir in ARDUINO_LIB_DIR_CANDIDATES.get(platform.system()):
arduino_libs_dir = os.path.join(home_dir, candidate_dir)
if os.path.exists(arduino_libs_dir):
break
if arduino_libs_dir:
# copy arduino scpi-parser library to the arduino libraries folder
rm_then_cp(src_lib_dir, os.path.join(arduino_libs_dir, dst_name))
return True
else:
print("Arduino libraries directory not found!")
return False
if __name__ == "__main__":
libscpi_dir = os.path.join(os.path.dirname(__file__), '../../libscpi')
scpi_parser_dir = os.path.join(os.path.dirname(__file__), 'scpi-parser')
build_scpi_parser_lib(libscpi_dir, scpi_parser_dir)
copy_lib(scpi_parser_dir, 'scpi-parser')