Support KLE to PCB metadata and nets

This commit is contained in:
Bán Dénes 2021-07-16 14:31:07 +02:00
parent cd0ae6d38b
commit f955aac994
2 changed files with 40 additions and 7 deletions

View file

@ -9,9 +9,15 @@ const pcbs_lib = require('./pcbs')
const process = async (raw, debug=false, logger=()=>{}) => { const process = async (raw, debug=false, logger=()=>{}) => {
const prefix = 'Interpreting format: '
let empty = true let empty = true
let [config, format] = io.interpret(raw, logger) let [config, format] = io.interpret(raw, logger)
logger('Interpreting format: ' + format) let suffix = format
if (format == 'KLE') {
suffix = `${format} (Auto-debug)`
debug = true
}
logger(prefix + suffix)
logger('Preprocessing input...') logger('Preprocessing input...')
config = prepare.unnest(config) config = prepare.unnest(config)

View file

@ -1,14 +1,35 @@
const u = require('./utils')
const kle = require('kle-serial') const kle = require('kle-serial')
const json5 = require('json5')
exports.convert = (config, logger) => { exports.convert = (config, logger) => {
const keyboard = kle.Serial.deserialize(config) const keyboard = kle.Serial.deserialize(config)
const result = {points: {zones: {}}} const result = {points: {zones: {}}, pcbs: {main: {}}}
// if the keyboard notes are valid JSON, they get added to each key as metadata
let meta = {}
try {
meta = json5.parse(keyboard.meta.notes)
} catch (ex) {
// notes were not valid JSON, oh well...
}
let index = 1 let index = 1
for (const key of keyboard.keys) { for (const key of keyboard.keys) {
const id = `key${index++}` const id = `key${index++}`
const colid = `${id}col` const colid = `${id}col`
const rowid = `${id}row` const rowid = `${id}row`
// we try to look at the first non-empty label
const label = key.labels.filter(e => !!e)[0] || ''
// PCB nets can be specified through key labels
let row_net = id
let col_net = 'GND'
if (label.match(/^\d+_\d+$/)) {
const parts = label.split('_')
row_net = `row_${parts[0]}`
col_net = `col_${parts[1]}`
}
// need to account for keycap sizes, as KLE anchors // need to account for keycap sizes, as KLE anchors
// at the corners, while we consider the centers // at the corners, while we consider the centers
@ -21,22 +42,28 @@ exports.convert = (config, logger) => {
const diff_x = key.rotation_x - (key.x + key.width / 2) const diff_x = key.rotation_x - (key.x + key.width / 2)
const diff_y = key.rotation_y - (key.y + key.height / 2) const diff_y = key.rotation_y - (key.y + key.height / 2)
// anchoring the per-key zone to the KLE-computed coords
const converted = { const converted = {
anchor: { anchor: {
shift: [`${x} u`, `${-y} u`], shift: [`${x} u`, `${-y} u`],
}, },
columns: {} columns: {}
} }
// adding a column-level rotation with origin
converted.columns[colid] = { converted.columns[colid] = {
rotate: -key.rotation_angle, rotate: -key.rotation_angle,
origin: [`${diff_x} u`, `${-diff_y} u`], origin: [`${diff_x} u`, `${-diff_y} u`],
rows: {} rows: {}
} }
converted.columns[colid].rows[rowid] = {
width: key.width, // passing along metadata to each key
height: key.height, converted.columns[colid].rows[rowid] = u.deepcopy(meta)
label: key.labels[0] converted.columns[colid].rows[rowid].width = key.width
} converted.columns[colid].rows[rowid].height = key.height
converted.columns[colid].rows[rowid].label = label
converted.columns[colid].rows[rowid].column_net = col_net
converted.columns[colid].rows[rowid].row_net = row_net
result.points.zones[id] = converted result.points.zones[id] = converted
} }