Skip to content

Commit

Permalink
feature-4 Confure combos through YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
zored committed Dec 22, 2019
1 parent 6b07f15 commit 40d0cd1
Show file tree
Hide file tree
Showing 10 changed files with 700 additions and 421 deletions.
119 changes: 111 additions & 8 deletions compiler/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ function main () {
)
const files = new KeymapFiles(keyboard)

compileKeymap(layers, keyboard, files)
compileSettings(keyboard, files)
const { comboCount } = compileKeymap(layers, keyboard, files, keyFactory)
compileSettings(keyboard, files, {
COMBO_COUNT: comboCount
})

console.log(`
Keymap compiled. Files created.
Expand All @@ -48,6 +50,9 @@ class Key {
get upCode () {
return `code_up(${this.codeName});`
}
get tapCode () {
return `tap_code(${this.codeName});`
}
get layerCodeName () {
return this.codeName
}
Expand Down Expand Up @@ -184,6 +189,22 @@ class LayerHoldKey extends Key {
}
}

class DoKey extends Key {
constructor (action) {
action = _.snakeCase(action).toUpperCase()
super(action)
}
get downCode () {
return ''
}
get upCode () {
return this.tapCode
}
get tapCode () {
return `run_advanced(${this.name});`
}
}

class DanceKey extends Key {
constructor (name, tapAction, holdAction) {
super(name)
Expand Down Expand Up @@ -300,7 +321,7 @@ class KeySequence extends Action {
MAX_CODES = Math.max(MAX_CODES, total)

return acc
}, { codes: [], keyCodeNames: [] }).codes.join(''))
}, { codes: [], keyCodeNames: [] }).codes.join(''))
}
onDanceStateCode () {
return `
Expand Down Expand Up @@ -459,16 +480,87 @@ function getLayersTemplateData (layers, keyboard) {
return { keys, names }
}

function compileKeymap (layers, keyboard, files) {
function compileKeymap (layers, keyboard, files, keyFactory) {
const Mustache = require('mustache')

const { combos, comboCount } = getCombos(keyboard.config.combos, keyFactory)
const result = Mustache.render(fs.readFileSync('compiler/template/zored_keymap.c', 'utf8'), _.merge({
dance: getDanceTemplateData(layers),
unicode: getUnicodeTemplateData(layers),
layers: getLayersTemplateData(layers, keyboard),
functions: getFunctions()
functions: getFunctions(),
combos
}, keyboard.getTemplateData(layers)))
files.add('keymap.c', result)

return { comboCount }
}

function getCombos (combosConfig, keyFactory) {
const KEYS = [
['we', 'rt', 'yu', 'io'],
['sd', 'fg', 'hj', 'kl'],
['vb', 'nm']
]

const combos = combosConfig.flatMap(
(row, x) => row
.map((value, y) => [KEYS[x][y], value])
.filter(([,value]) => value !== null)
)
.map(([key, value]) => [
key,
value,
new KeySequence(
key.split('').map(key => keyFactory.create(key)),
null
)
])
.map(([key, value, keys]) => ({
key: keyFactory.create(value),
name: `combo_${keys.id.toLowerCase()}`,
index: `CMB_${keys.id.toUpperCase()}`,
keyButtons: keys.allKeys.map(key => key.codeName).join(', ')
}))

const definitions = (() => {
const combosDefinitions = combos.map(({ name, keyButtons }) =>
`const uint16_t PROGMEM ${name}[] = {${keyButtons}, COMBO_END};`
).join('\n')

const indexDefinitions = glueEnum(combos.map(({ index }) => index), 0)
const comboMap = combos.map(({ index, name }) => `
[${index}] = COMBO_ACTION(${name}),
`).join('')

return `
${combosDefinitions}
enum combo_names {
${indexDefinitions}
};
combo_t key_combos[COMBO_COUNT] = {
${comboMap}
};
`
})()

const declarations = (() => {
return combos.map(({ index, key }) => `
case ${index}:
${key.tapCode}
break;
`).join('')
})()

return {
comboCount: combos.length,
combos: {
definitions,
declarations
}
}
}

function getFunctions () {
Expand Down Expand Up @@ -496,12 +588,20 @@ void ${aliasPrefix}${aliasNumber}(${getArguments(aliasNumber)}) {
`)).join('')
}

function compileSettings (keyboard, files) {
function compileSettings (keyboard, files, override) {
const { config, rules } = keyboard.settings

const getValue = (value, name) => {
if (override[name] !== undefined) {
return override[name]
}

return value
}

files.add('config.h', _.map(config, (value, name) => `
#undef ${name}
#define ${name} ${value}
#define ${name} ${getValue(value, name)}
`).join(''))

files.add('rules.mk', _.map(rules, (value, name) => `
Expand Down Expand Up @@ -682,9 +782,12 @@ class KeyFactory {
}

if (_.isString(value)) {
if (value.match(/^do[A-Z]/)) {
return new DoKey(value, true)
}

value = this.nameMap[value] || value
const noPrefix = value.match(this.noPrefixPattern)

return new Key(value, noPrefix)
}

Expand Down
Loading

0 comments on commit 40d0cd1

Please sign in to comment.