New script for generating defined pinouts for scripts
This commit is contained in:
parent
9dfc716624
commit
5e0b459b45
143
generate-defines.py
Executable file
143
generate-defines.py
Executable file
@ -0,0 +1,143 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import json
|
||||||
|
import markdown
|
||||||
|
import unicodedata
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
|
pins = None
|
||||||
|
|
||||||
|
key_template = {
|
||||||
|
'python': '{board_name}_{name}',
|
||||||
|
'ruby': '{board_name}_{name}',
|
||||||
|
'spin': ' {board_name}_{name}',
|
||||||
|
'c': '#define {board_name}_{name}'
|
||||||
|
}
|
||||||
|
|
||||||
|
value_template = {
|
||||||
|
'python': ' = {value}',
|
||||||
|
'ruby': ' = {value}',
|
||||||
|
'spin': ' = {value}',
|
||||||
|
'c': ' {value}'
|
||||||
|
}
|
||||||
|
|
||||||
|
comment_prefix = {
|
||||||
|
'python': '#',
|
||||||
|
'ruby': '',
|
||||||
|
'spin': '\'',
|
||||||
|
'c': '//'
|
||||||
|
}
|
||||||
|
|
||||||
|
def slugify(value):
|
||||||
|
"""
|
||||||
|
Normalizes string, converts to lowercase, removes non-alpha characters,
|
||||||
|
and converts spaces to hyphens.
|
||||||
|
"""
|
||||||
|
value = unicode(value)
|
||||||
|
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
|
||||||
|
value = value.replace('+','PLUS')
|
||||||
|
value = value.replace('-','MINUS')
|
||||||
|
value = re.sub('[^\w\s-]', '', value).strip().lower()
|
||||||
|
return re.sub('[-\s]+', '_', value)
|
||||||
|
|
||||||
|
def bcm_to_physical(pin):
|
||||||
|
pin = pin[3:]
|
||||||
|
for idx in pins:
|
||||||
|
compare_pin = pins[idx]
|
||||||
|
if 'scheme' in compare_pin:
|
||||||
|
if 'bcm' in compare_pin['scheme']:
|
||||||
|
if compare_pin['scheme']['bcm'] == int(pin):
|
||||||
|
print("Mapping BCM{} to {}".format(pin, str(idx)))
|
||||||
|
return str(idx)
|
||||||
|
|
||||||
|
def physical_to_bcm(pin):
|
||||||
|
pin = pins[pin]
|
||||||
|
if 'scheme' in pin:
|
||||||
|
if 'bcm' in pin['scheme']:
|
||||||
|
return str(pin['scheme']['bcm'])
|
||||||
|
return None
|
||||||
|
|
||||||
|
def physical_to_wiringpi(pin):
|
||||||
|
pin = pins[pin]
|
||||||
|
if 'scheme' in pin:
|
||||||
|
if 'wiringpi' in pin['scheme']:
|
||||||
|
return str(pin['scheme']['wiringpi'])
|
||||||
|
return None
|
||||||
|
|
||||||
|
def physical_to(pin, scheme='bcm'):
|
||||||
|
if scheme in ['bcm','wiringpi']:
|
||||||
|
pin = pins[pin]
|
||||||
|
if 'scheme' in pin:
|
||||||
|
if scheme in pin['scheme']:
|
||||||
|
return str(pin['scheme'][scheme])
|
||||||
|
elif scheme == 'physical':
|
||||||
|
return pin
|
||||||
|
return None
|
||||||
|
|
||||||
|
db = json.load(open('pi-pinout.json'))
|
||||||
|
pins = db['pins']
|
||||||
|
|
||||||
|
define = {}
|
||||||
|
keys = []
|
||||||
|
|
||||||
|
def add_define(key, value):
|
||||||
|
global keys, define
|
||||||
|
keys.append(key)
|
||||||
|
define[key] = value
|
||||||
|
|
||||||
|
if len(sys.argv) >= 3:
|
||||||
|
|
||||||
|
overlay_file = sys.argv[1]
|
||||||
|
pin_scheme = sys.argv[2]
|
||||||
|
output_lang = sys.argv[3]
|
||||||
|
|
||||||
|
overlay = json.load(open('overlay/{}.json'.format(overlay_file)))
|
||||||
|
|
||||||
|
if 'i2c' in overlay:
|
||||||
|
for addr in overlay['i2c']:
|
||||||
|
info = overlay['i2c'][addr]
|
||||||
|
|
||||||
|
add_define('ADDR_' + slugify(info['name']).upper(), addr)
|
||||||
|
|
||||||
|
if 'pin' in overlay:
|
||||||
|
for pin in overlay['pin']:
|
||||||
|
info = overlay['pin'][pin]
|
||||||
|
if str(pin).startswith('bcm'):
|
||||||
|
pin = bcm_to_physical(pin)
|
||||||
|
|
||||||
|
|
||||||
|
if 'name' in info:
|
||||||
|
name = slugify(info['name']).upper()
|
||||||
|
else:
|
||||||
|
name = slugify(pins[pin]['name']).upper()
|
||||||
|
|
||||||
|
add_define(name, physical_to(pin, pin_scheme))
|
||||||
|
|
||||||
|
board_name = slugify(overlay['name']).upper()
|
||||||
|
|
||||||
|
print(comment_prefix[output_lang] + ' Pin definitions for ' + overlay['name'])
|
||||||
|
print(comment_prefix[output_lang] + ' Using the {} pin numbering scheme'.format(pin_scheme))
|
||||||
|
|
||||||
|
row_length = 0
|
||||||
|
|
||||||
|
for name in define:
|
||||||
|
key = key_template[output_lang].format(
|
||||||
|
board_name = board_name,
|
||||||
|
name = name
|
||||||
|
)
|
||||||
|
row_length = max(len(key),row_length)
|
||||||
|
|
||||||
|
|
||||||
|
for name in keys:
|
||||||
|
key = key_template[output_lang].format(
|
||||||
|
board_name = board_name,
|
||||||
|
name = name
|
||||||
|
)
|
||||||
|
|
||||||
|
value = value_template[output_lang].format(value = define[name])
|
||||||
|
|
||||||
|
value = value.rjust(row_length - len(key) + len(value),' ')
|
||||||
|
|
||||||
|
print(key+value)
|
@ -6,7 +6,26 @@
|
|||||||
"buy": "http://shop.pimoroni.com/products/explorer-hat",
|
"buy": "http://shop.pimoroni.com/products/explorer-hat",
|
||||||
"description": "An all-in-one light, input, motor, touch and output add-on board.",
|
"description": "An all-in-one light, input, motor, touch and output add-on board.",
|
||||||
"pincount": 40,
|
"pincount": 40,
|
||||||
|
"i2c": {
|
||||||
|
"0x28": {
|
||||||
|
"name": "Cap Touch",
|
||||||
|
"device": "cap1208"
|
||||||
|
},
|
||||||
|
"0x48": {
|
||||||
|
"name": "Analog Input",
|
||||||
|
"device": "ads1015"
|
||||||
|
}
|
||||||
|
},
|
||||||
"pin": {
|
"pin": {
|
||||||
|
"3": {},
|
||||||
|
"5": {},
|
||||||
|
"8": {},
|
||||||
|
"10": {},
|
||||||
|
"12": {},
|
||||||
|
"19": {},
|
||||||
|
"21": {},
|
||||||
|
"23": {},
|
||||||
|
"24": {},
|
||||||
"7": {
|
"7": {
|
||||||
"name": "LED 1",
|
"name": "LED 1",
|
||||||
"mode": "output",
|
"mode": "output",
|
||||||
|
Loading…
Reference in New Issue
Block a user