Skip to content

Commit b608a8b

Browse files
committed
preparing version 1
1 parent 83a6f71 commit b608a8b

File tree

14 files changed

+293
-199
lines changed

14 files changed

+293
-199
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ then just run it with dev to start
2222

2323
# Tasks
2424
## urgent
25+
- [x] check if a keyboard is connected (usb drive) in the keyboard selector preview
26+
- [ ] show serial output in the gui
2527
- [ ] guides etc for setup + split workflow
2628
- [ ] share pog.json files
2729
- [ ] check if the controller you use even has the pin you specified (controller lookup and serial to get pins )

src/main/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { app, shell, BrowserWindow, ipcMain, Menu } from 'electron'
22
import { join } from 'path'
33
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
44
import icon from '../../resources/icon.png?asset'
5-
import { handleSelectDrive, selectKeyboard } from './selectKeyboard'
5+
import {checkForUSBKeyboards, handleSelectDrive, selectKeyboard} from './selectKeyboard'
66
import { updateFirmware } from './kmkUpdater'
77
import { handleKeymapSave, saveConfiguration } from './saveConfig'
88
import { autoUpdater } from 'electron-updater'
@@ -168,7 +168,6 @@ app.whenReady().then(async () => {
168168
// dock icon is clicked and there are no other windows open.
169169
if (BrowserWindow.getAllWindows().length === 0) createWindow()
170170
})
171-
scanForKeyboards().then((boards) => console.log(boards))
172171
})
173172

174173
// Quit when all windows are closed, except on macOS. There, it's common
@@ -192,6 +191,7 @@ ipcMain.handle('deselectKeyboard', () => deselectKeyboard())
192191
ipcMain.handle('rescanKeyboards', () => scanForKeyboards())
193192
ipcMain.handle('updateFirmware', () => updateFirmware())
194193
ipcMain.on('saveConfiguration', (_event, data) => saveConfiguration(data))
194+
ipcMain.handle('checkForUSBKeyboards', (_event, data) => checkForUSBKeyboards(data))
195195
ipcMain.handle('selectKeyboard', (_event, data) => selectKeyboard(data))
196196

197197
autoUpdater.on('update-available', () => {
@@ -236,7 +236,7 @@ export const serialBoards: { value: any[] } = { value: [] }
236236
// TODO: resolve callbacks properly
237237
// https://stackoverflow.com/questions/69608234/get-promise-resolve-from-separate-callback
238238
const scanForKeyboards = async () => {
239-
console.log('checking for connected keyboards')
239+
console.log('checking for connected keyboards via serial')
240240
if (connectedKeyboardPort && connectedKeyboardPort.isOpen) connectedKeyboardPort.close()
241241
const ports = await serialPort.SerialPort.list()
242242
const circuitPythonPorts = ports //.filter(port => {
@@ -252,7 +252,7 @@ const scanForKeyboards = async () => {
252252
.filter((a) => a.value !== undefined)
253253
.map((a) => a.value)
254254

255-
console.log('boards ready')
255+
console.log('found the following boards:', filteredBoards)
256256
filteredBoards.map((a) => console.log(`${a.name} - ${a.id} | ${a.path}`))
257257
mainWindow?.webContents.send('keyboardScan', {
258258
keyboards: filteredBoards

src/main/pythontemplates/boot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import supervisor
44
import storage
55
import microcontroller
66
7-
supervisor.set_next_stack_limit(4096 + 4096)
7+
# optional
8+
# supervisor.set_next_stack_limit(4096 + 4096)
89
usb_cdc.enable(console=True, data=True)
910
1011
# index configs

src/main/pythontemplates/kb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class KMKKeyboard(_KMKKeyboard):
4141
side = SplitSide.RIGHT if str(getmount('/').label)[-1] == 'R' else SplitSide.LEFT
4242
if pog.splitPinB:
4343
self.split = Split(split_side=side, data_pin=pog.splitPinA, data_pin2=pog.splitPinB, use_pio=True)
44-
else:
44+
else:
4545
self.split = Split(split_side=side, data_pin=pog.splitPinA, use_pio=True)
4646
self.modules.append(self.split)
4747

src/main/saveConfig.ts

Lines changed: 117 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { kbpy } from './pythontemplates/kb'
77
import { codepy } from './pythontemplates/code'
88
import { bootpy } from './pythontemplates/boot'
99
import { pog_serialpy } from './pythontemplates/pog_serial'
10-
import {connectedKeyboardPort, pogconfigbuffer, writeKeymapViaSerial, writePogConfViaSerial} from './index'
10+
import {
11+
connectedKeyboardPort,
12+
pogconfigbuffer,
13+
writeKeymapViaSerial,
14+
writePogConfViaSerial
15+
} from './index'
1116
// save code.py based on pog.json
1217

1318
export const saveConfiguration = (data: string) => {
@@ -23,65 +28,70 @@ export const saveConfiguration = (data: string) => {
2328
console.log('pog File written successfully\n')
2429
})
2530

26-
saveKeyboardConfig(pogConfig) // initialize files if they dont exist
31+
saveKeyboardConfig(pogConfig) // initialize files if they don't exist
2732
handleKeymapSave({ pogConfig, serial: false })
2833
}
2934
}
3035
export const saveKeyboardConfig = (pogConfig) => {
31-
const coordmapstring = () => {
32-
if (pogConfig.coordMap.length === 0) return ''
33-
let str = ' coord_mapping = [\n'
34-
pogConfig.coordMap.forEach((row) => {
35-
str += ' ' + row.join(',') + ',\n'
36-
})
37-
str += ' ]'
38-
return str.replaceAll(/spc,/gi, ' ')
39-
}
36+
// TODO: remove at some point from reference
37+
// this is now all moved to the pog.json file
38+
39+
// const coordmapstring = () => {
40+
// if (pogConfig.coordMap.length === 0) return ''
41+
// let str = ' coord_mapping = [\n'
42+
// pogConfig.coordMap.forEach((row) => {
43+
// str += ' ' + row.join(',') + ',\n'
44+
// })
45+
// str += ' ]'
46+
// return str.replaceAll(/spc,/gi, ' ')
47+
// }
4048

4149
// write kb.py for basic config
42-
let pinSetup = ``
43-
const renderPin = (pin) => {
44-
if (pogConfig.pinPefix === 'board') return `board.${pin}`
45-
if (pogConfig.pinPefix === 'gp') return `board.GP${pin}`
46-
if (pogConfig.pinPefix === 'quickpin') return `pins[${pin}]`
47-
return pin
48-
}
49-
if (pogConfig.wiringMethod === 'matrix') {
50-
pinSetup = `
51-
col_pins = (${pogConfig.colPins.map((a) => renderPin(a)).join(', ')})
52-
row_pins = (${pogConfig.rowPins.map((a) => renderPin(a)).join(', ')})
53-
diode_orientation = DiodeOrientation.${pogConfig.diodeDirection}
54-
${coordmapstring()}
55-
`
56-
} else {
57-
pinSetup = `
58-
def __init__(self):
59-
# create and register the scanner
60-
self.matrix = KeysScanner(
61-
# require argument:
62-
pins=[${pogConfig.directPins.map((a) => 'board.GP' + a).join(', ')}],
63-
# optional arguments with defaults:
64-
value_when_pressed=False,
65-
pull=True,
66-
interval=0.02, # Debounce time in floating point seconds
67-
max_events=64
68-
)
69-
`
70-
}
71-
const quickpinSupport =
72-
pogConfig.pinPrefix === 'quickpin'
73-
? `from kmk.quickpin.pro_micro.nice_nano import pinout as pins `
74-
: ''
75-
const kbConfig = `# KB base config
76-
import board
77-
from kmk.kmk_keyboard import KMKKeyboard as _KMKKeyboard
78-
from kmk.scanners import DiodeOrientation
79-
from kmk.scanners.keypad import KeysScanner
80-
81-
class KMKKeyboard(_KMKKeyboard):
82-
${pinSetup}
83-
`
84-
console.log(quickpinSupport)
50+
// let pinSetup = ``
51+
// const renderPin = (pin) => {
52+
// if (pogConfig.pinPefix === 'board') return `board.${pin}`
53+
// if (pogConfig.pinPefix === 'gp') return `board.GP${pin}`
54+
// if (pogConfig.pinPefix === 'quickpin') return `pins[${pin}]`
55+
// return pin
56+
// }
57+
// if (pogConfig.wiringMethod === 'matrix') {
58+
// pinSetup = `
59+
// col_pins = (${pogConfig.colPins.map((a) => renderPin(a)).join(', ')})
60+
// row_pins = (${pogConfig.rowPins.map((a) => renderPin(a)).join(', ')})
61+
// diode_orientation = DiodeOrientation.${pogConfig.diodeDirection}
62+
// ${coordmapstring()}
63+
// `
64+
// } else {
65+
// pinSetup = `
66+
// def __init__(self):
67+
// # create and register the scanner
68+
// self.matrix = KeysScanner(
69+
// # require argument:
70+
// pins=[${pogConfig.directPins.map((a) => 'board.GP' + a).join(', ')}],
71+
// # optional arguments with defaults:
72+
// value_when_pressed=False,
73+
// pull=True,
74+
// interval=0.02, # Debounce time in floating point seconds
75+
// max_events=64
76+
// )
77+
// `
78+
// }
79+
// const quickpinSupport =
80+
// pogConfig.pinPrefix === 'quickpin'
81+
// ? `from kmk.quickpin.pro_micro.nice_nano import pinout as pins `
82+
// : ''
83+
// const kbConfig = `# KB base config
84+
// import board
85+
// from kmk.kmk_keyboard import KMKKeyboard as _KMKKeyboard
86+
// from kmk.scanners import DiodeOrientation
87+
// from kmk.scanners.keypad import KeysScanner
88+
//
89+
// class KMKKeyboard(_KMKKeyboard):
90+
// ${pinSetup}
91+
// `
92+
// console.log(quickpinSupport)
93+
94+
// add option to overwrite files eg. force flash
8595
// add pog helper and keyboard setup
8696

8797
if (!fs.existsSync(currentKeyboard.path + '/kb.py')) {
@@ -124,11 +134,12 @@ class KMKKeyboard(_KMKKeyboard):
124134
}
125135
}
126136

127-
export const saveCoordMapHelper = () => {
128-
fs.writeFile(currentKeyboard.path + '/coordmaphelper.py', coordmaphelperpy, () => {
129-
console.log('coord map helper File created successfully')
130-
})
131-
}
137+
// is now handled with a flag in pog.json
138+
// export const saveCoordMapHelper = () => {
139+
// fs.writeFile(currentKeyboard.path + '/coordmaphelper.py', coordmaphelperpy, () => {
140+
// console.log('coord map helper File created successfully')
141+
// })
142+
// }
132143

133144
export const handleKeymapSave = ({ pogConfig, serial }) => {
134145
// const codeblockraw = fs.readFileSync(`${keyboardPath}/code.py`, {encoding:'utf8', flag:'r'})
@@ -137,30 +148,18 @@ export const handleKeymapSave = ({ pogConfig, serial }) => {
137148
// console.log(codeblock)
138149
// create basic keymap file
139150
// grab old codeblocks
140-
let pythonImports = ''
141-
let kmkAddons = ''
142-
let codeblock = ''
143-
//layers
144-
if (true) {
145-
pythonImports += '\nfrom kmk.modules.layers import Layers\n'
146-
kmkAddons += '\nkeyboard.modules.append(Layers())\n'
147-
}
148151

149-
//media keys
150-
if (true) {
151-
pythonImports += '\nfrom kmk.extensions.media_keys import MediaKeys\n'
152-
kmkAddons += '\nkeyboard.extensions.append(MediaKeys())\n'
153-
}
154152
// testing encoder enable
155153
if (pogConfig.encoders && pogConfig.encoders.length !== 0) {
156-
pythonImports +=
157-
'\nfrom kmk.modules.layers import Layers\n' +
158-
'from kmk.modules.encoder import EncoderHandler\n'
159-
kmkAddons +=
160-
'\nlayers = Layers()\n' +
161-
'encoder_handler = EncoderHandler()\n' +
162-
'keyboard.modules = [layers, encoder_handler]\n'
163-
154+
// pythonImports +=
155+
// '\nfrom kmk.modules.layers import Layers\n' +
156+
// 'from kmk.modules.encoder import EncoderHandler\n'
157+
// kmkAddons +=
158+
// '\nlayers = Layers()\n' +
159+
// 'encoder_handler = EncoderHandler()\n' +
160+
// 'keyboard.modules = [layers, encoder_handler]\n'
161+
162+
// still keeping the option to save the encoder keymap as raw code instead of the lookup
164163
let encoderPins = ''
165164
pogConfig.encoders.forEach((encoder) => {
166165
encoderPins += `(board.GP${encoder.pad_a}, board.GP${encoder.pad_b}, None,),`
@@ -175,42 +174,44 @@ export const handleKeymapSave = ({ pogConfig, serial }) => {
175174
})
176175
encoderKeymap += '),\n'
177176
})
178-
179-
codeblock +=
180-
'# Encoder\n' +
181-
'encoder_handler.pins = (\n' +
182-
encoderPins +
183-
'\n' +
184-
')\n' +
185-
'encoder_handler.map = [ \n' +
186-
encoderKeymap + // layers
187-
']\n'
188177
}
189-
const mainConfig = `# Main Keyboard Configuration
190-
print("Starting ${pogConfig.name || 'Keyboard'}")
191-
192-
import board
193-
from kb import KMKKeyboard
194-
from kmk.modules.tapdance import TapDance
195-
${pythonImports}
196-
197-
keyboard = KMKKeyboard()
198-
${kmkAddons}
199-
200-
tapdance = TapDance()
201-
tapdance.tap_time = 200
202-
keyboard.modules.append(tapdance)
203-
204-
# Keymap
205-
from keymap import keymap
206-
keyboard.keymap = keymap
207-
208-
${codeblock}
209-
210-
if __name__ == '__main__':
211-
keyboard.go()
212-
`
213178

179+
// codeblock +=
180+
// '# Encoder\n' +
181+
// 'encoder_handler.pins = (\n' +
182+
// encoderPins +
183+
// '\n' +
184+
// ')\n' +
185+
// 'encoder_handler.map = [ \n' +
186+
// encoderKeymap + // layers
187+
// ']\n'
188+
// }
189+
// const mainConfig = `# Main Keyboard Configuration
190+
// print("Starting ${pogConfig.name || 'Keyboard'}")
191+
//
192+
// import board
193+
// from kb import KMKKeyboard
194+
// from kmk.modules.tapdance import TapDance
195+
// ${pythonImports}
196+
//
197+
// keyboard = KMKKeyboard()
198+
// ${kmkAddons}
199+
//
200+
// tapdance = TapDance()
201+
// tapdance.tap_time = 200
202+
// keyboard.modules.append(tapdance)
203+
//
204+
// # Keymap
205+
// from keymap import keymap
206+
// keyboard.keymap = keymap
207+
//
208+
// ${codeblock}
209+
//
210+
// if __name__ == '__main__':
211+
// keyboard.go()
212+
// `
213+
214+
// we are still writing the keymap file by hand
214215
const keymap = `# Keymap Autogenerated by Pog do not edit
215216
from kmk.keys import KC
216217
from kmk.handlers.sequences import send_string, simple_key_sequence
@@ -230,13 +231,11 @@ for l, layer in enumerate(pog.config['encoderKeymap']):
230231
layerEncoders.append(tuple(map(eval, encoder)))
231232
encoderKeymap.append(tuple(layerEncoders))
232233
`
233-
if(serial){
234+
if (serial) {
234235
writeKeymapViaSerial(keymap)
235-
236-
}else{
236+
} else {
237237
fs.writeFile(currentKeyboard.path + '/keymap.py', keymap, () => {
238238
console.log('keymap File written successfully')
239239
})
240240
}
241-
242241
}

src/main/selectKeyboard.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,18 @@ export const selectKeyboard = async ({ path, id }: { path: string; id: string })
5858
}
5959
return { error: 'not all args provided' }
6060
}
61+
62+
export const checkForUSBKeyboards = async (keyboardPaths: string[]) => {
63+
console.log('checking for usb keyboards', keyboardPaths)
64+
// check for each path in the filesystem if it exists
65+
const connectedKeyboards : {path:string,connected:boolean}[] = []
66+
for (const path of keyboardPaths) {
67+
if (fs.existsSync(path)) {
68+
connectedKeyboards.push({
69+
path,
70+
connected: true
71+
})
72+
}
73+
}
74+
return connectedKeyboards
75+
}

src/preload/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const API = {
1616
ipcRenderer.on('serialKeyboardPogConfig', callback)
1717
},
1818
rescanKeyboards: () => ipcRenderer.invoke('rescanKeyboards'),
19+
checkForUSBKeyboards: (data) => ipcRenderer.invoke('checkForUSBKeyboards', data),
1920
deselectKeyboard: () => ipcRenderer.invoke('deselectKeyboard')
2021

2122
}

0 commit comments

Comments
 (0)