Select Git revision

Jake Read authored
gcode.js 2.01 KiB
// boilerplate atkapi header
const InOut = require('../../lib/inout.js')
let Input = InOut.Input
let Output = InOut.Output
function Gcode() {
// state
this.state = {
mode: 'G0',
speeds: {
G0: 1200,
G1: 400
}
}
// local functions
var getKeyValues = (str) => {
var kv = {}
for (var i = 0; i < str.length; i++) {
if (str[i].match('[A-Za-z]')) { // regex to match upper case letters
var lastIndex = str.indexOf(' ', i)
if (lastIndex < 0) {
lastIndex = str.length
}
var key = str[i].toUpperCase()
kv[key] = parseFloat(str.slice(i + 1, lastIndex))
}
}
return kv
}
var parseGcode = (str) => {
var instruction = {
position: {},
hasMove: false,
speed: 0
}
kv = getKeyValues(str)
// track modality
if(kv.G == 0 | kv.G == 1){
this.state.mode = 'G' + kv.G.toString()
} else if (kv.G != null) {
// no arcs pls
console.log('unfriendly Gcode mode!', kv)
}
for(key in kv){
if(key.match('[A-EX-Z]')){
instruction.position[key] = kv[key]
instruction.hasMove = true
} else if (key.match('[F]')){
this.state.speeds[this.state.mode] = kv.F
}
}
instruction.speed = this.state.speeds[this.state.mode]
// and this for help later?
instruction.kv = kv
return instruction
}
// input functions
var lineIn = (str) => {
var instruction = parseGcode(str)
if (instruction.hasMove){
this.outputs.instructionOut.emit(instruction)
} else {
this.outputs.modeChange.emit(this.state.mode)
}
}
// ins and outs
this.inputs = {
lineIn: new Input('string input', 'string', lineIn)
}
this.outputs = {
instructionOut: new Output('move instruction', 'object'),
modeChange: new Output('mode change', 'string')
}
}
// export the module
module.exports = Gcode