Skip to content
Snippets Groups Projects
Select Git revision
  • 28b3843c28784af70b3c20042a86f32030727df0
  • master default protected
  • at_palomagr
3 results

gcode.js

Blame
  • gcode.js 2.71 KiB
    // boilerplate atkapi header
    const InOut = require('../../lib/inout.js')
    let Input = InOut.Input
    let Output = InOut.Output
    let UIElement = InOut.UIElement
    
    function Gcode() {
    
        var gcode = {
            description: {
                name: 'Gcode Parser',
                alt: 'line of gcode -> planner recognized move'
            },
            ui: {
                mode: UIElement('string', 'G0', changeMode),
                G0: UIElement('number', 1200, null),
                G1: UIElement('number', 400, null)
            },
            inputs: {
                lineIn: Input('string', parseGcode)
            },
            outputs: {
                instructionOut: Output('move instruction'),
                modeChange: Output('string')
            }
        }
    
        function changeMode() {
            console.log('gcode mode changed to', gcode.ui.mode.value)
        }
    
        // local functions
        function 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
        }
    
        // TODO: test, can we link global vars to ui objects ... 
        // gcode.ui.mode.value = var ? no bc set / get etc 
        // more like var = gcode.ui.mode.value ? is this referential?
    
        function parseGcode(str){
            var instruction = {
                position: {},
                hasMove: false,
                speed: 0
            }
    
            kv = getKeyValues(str)
            // track modality
            if (kv.G == 0 | kv.G == 1) {
                gcode.ui.mode.set('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]')) {
                    gcode.ui[gcode.ui.mode.value].set(kv.F)
                }
            }
    
            instruction.speed = gcode.ui[gcode.ui.mode.value].get()
            // and this for help later?
            instruction.kv = kv
    
            return instruction
        }
    
        // input functions
        var lineIn = (str) => {
            var instruction = parseGcode(str)
            if (instruction.hasMove) {
                console.log('gcode emit', instruction)
                gcode.outputs.instructionOut.emit(instruction)
            } else {
                outputs.modeChange.emit(gcode.ui.mode.get())
            }
        }
        
    
        return gcode
    
    }
    
    
    // export the module 
    module.exports = Gcode