Skip to content

Commit

Permalink
QMK info json exporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
karliss committed Aug 1, 2023
1 parent 0f051ec commit 8bdde6f
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/kbe/FormatManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FormatManager {
new CSVFormat.CSVExporter(),
new TKBEditorFormat.TKBEExporter(),
new QMKLayoutMacroExporter(true),
new QMKInfoJson.QMKInfoJsonExporter(),
new TestExporter()
];
}
Expand Down
111 changes: 111 additions & 0 deletions src/kbe/QMKInfoJson.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import haxe.Json;
import kbe.KeyBoard;
import kbe.Exporter;

typedef ExportQMKInfoConfig = {
prettyPrint:Bool
};

// https://beta.docs.qmk.fm/reference/reference_info_json
class QMKInfoJsonImporter implements Exporter.Importer {
public var text(default, null):String = "QMK info.json importer";
Expand Down Expand Up @@ -109,3 +113,110 @@ class QMKInfoJsonImporter implements Exporter.Importer {
return keyboard;
}
}

class PrettyJsonPrinter extends haxe.format.JsonPrinter {}

class QMKInfoJsonExporter implements Exporter.Exporter {
public var value(default, null):String = "QMK Info.json exporter";

static public var DEFAULT_CONFIG:ExportQMKInfoConfig = {
prettyPrint: false
}

public function new() {}

public function fileName():String {
return "info.json";
}

public function mimeType():String {
return "text/json";
}

function tryAddPins(keyboard:KeyBoard, data:Dynamic) {
// if (kkeyboard.row)
// if (keyboard.)
if (!(keyboard.rowMapping.columnNames.contains("pin") && keyboard.columnMapping.columnNames.contains("pin"))) {
return;
}
var columnIndex = keyboard.rowMapping.getColumnIndex("pin");
var rowPins = new Array<String>();
var hasPin = false;
for (i in 0...keyboard.rowMapping.rows) {
var pin:String = keyboard.rowMapping.getColumnValue(i, columnIndex);
if (pin != null && pin.length > 0) {
hasPin = true;
}
rowPins.push(pin);
}
columnIndex = keyboard.columnMapping.getColumnIndex("pin");
var columnPins = new Array<String>();
for (i in 0...keyboard.columnMapping.rows) {
var pin:String = keyboard.columnMapping.getColumnValue(i, columnIndex);
if (pin != null && pin.length > 0) {
hasPin = true;
}
columnPins.push(pin);
}
if (!hasPin) {
return;
}
data.matrix_pins = {
cols: columnPins,
rows: rowPins
};
}

function convertLayout(keyboard:KeyBoard, layout:KeyboardLayout, out:Dynamic) {
var content:Dynamic = {};
var keys = [];
for (layoutKey in layout.keys) {
var keyboardId = layout.mappingToGrid(layoutKey.id);
var keyboardKey = keyboardId.length > 0 ? keyboard.getKeyById(keyboardId[0]) : null;
var key:Dynamic = {};
key.x = layoutKey.x;
key.y = layoutKey.y;
var EPS = 0.01;
// TODO: odd shape handling
if (Math.abs(layoutKey.width - 1.0) > EPS) {
key.w = layoutKey.width;
}
if (Math.abs(layoutKey.height - 1.0) > EPS) {
key.h = layoutKey.height;
}
if (layoutKey.name != null && layoutKey.name.length > 0) {
key.label = layoutKey.name;
}
if (keyboardKey != null) {
var pos = keyboard.getMatrixPos(keyboardKey);
key.matrix = [pos.row, pos.col];
}
keys.push(key);
}
content.layout = keys;

Reflect.setField(out, layout.name, content);
}

public function exportWithConfig(keyboard:KeyBoard, config:ExportQMKInfoConfig):Bytes {
var data:Dynamic = {};
tryAddPins(keyboard, data);
var layoutG:Dynamic = {}
for (layout in keyboard.layouts) {
convertLayout(keyboard, layout, layoutG);
}
data.layouts = layoutG;

// Reflect.setField(data, "description", keyboard.description);
var jsonText = if (config.prettyPrint) {
Json.stringify(data, " ");
} else {
Json.stringify(data);
};
return Bytes.ofString(jsonText);
}

public function convert(keyboard:KeyBoard):Bytes {
return exportWithConfig(keyboard, DEFAULT_CONFIG);
}
}

0 comments on commit 8bdde6f

Please sign in to comment.