Skip to content
Snippets Groups Projects
Commit eb903a6e authored by Jake Read's avatar Jake Read
Browse files

refactor with native inout

parent 4ff5e7b9
No related branches found
No related tags found
No related merge requests found
const EventEmitter = require('events')
class Gcode extends EventEmitter {}
// boilerplate atkapi header
const InOut = require('./inout.js')
let Input = InOut.Input
let Output = InOut.Output
function create() {
var gcode = new Gcode()
function Gcode() {
// state
gcode.mode = 'G0'
gcode.speeds = {
this.state = {
mode: 'G0',
speeds: {
G0: 1200,
G1: 400
}
}
// local functions
function getKeyValues(str) {
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
......@@ -27,7 +30,7 @@ function create() {
return kv
}
function parseGcode(str){
var parseGcode = (str) => {
var instruction = {
position: {},
hasMove: false,
......@@ -37,7 +40,7 @@ function create() {
kv = getKeyValues(str)
// track modality
if(kv.G == 0 | kv.G == 1){
gcode.mode = 'G' + kv.G.toString()
this.state.mode = 'G' + kv.G.toString()
} else if (kv.G != null) {
// no arcs pls
console.log('unfriendly Gcode mode!', kv)
......@@ -48,11 +51,11 @@ function create() {
instruction.position[key] = kv[key]
instruction.hasMove = true
} else if (key.match('[F]')){
gcode.speeds[gcode.mode] = kv.F
this.state.speeds[this.state.mode] = kv.F
}
}
instruction.speed = gcode.speeds[gcode.mode]
instruction.speed = this.state.speeds[this.state.mode]
// and this for help later?
instruction.kv = kv
......@@ -60,18 +63,26 @@ function create() {
}
// input functions
gcode.lineIn = function(str) {
var lineIn = (str) => {
var instruction = parseGcode(str)
if (instruction.hasMove){
gcode.emit('newInstruction', instruction)
this.outputs.instructionOut.emit(instruction)
} else {
gcode.emit('modeChange', gcode.mode)
this.outputs.modeChange.emit(this.state.mode)
}
}
return gcode
// ins and outs
this.inputs = {
lineIn: new Input('string input', 'string', lineIn)
}
module.exports = {
create: create
this.outputs = {
instructionOut: new Output('move instruction', 'object'),
modeChange: new Output('mode change', 'string')
}
}
// export the module
module.exports = Gcode
\ No newline at end of file
......@@ -22,7 +22,7 @@ function Output(name, type){
this.emit = (data) => {
// could typecheck here
if(this.calls.length == 0){
console.log('no events bound here', this.name)
console.log('no events bound to this event: ', this.name)
} else {
for(index in this.calls){
this.calls[index](data)
......
const EventEmitter = require('events')
class Terminal extends EventEmitter {}
// boilerplate atkapi header
const InOut = require('./inout.js')
let Input = InOut.Input
let Output = InOut.Output
// bonus reqs
const readline = require('readline')
function create(){
var terminal = new Terminal()
// a constructor, a fn, a javascript mess
function Terminal(){
// object
terminal.state = {
// object state
this.state = {
width: 64,
height: 48
}
// natural inputs
// natural inputs / local functions
var rlInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rlInterface.on('line', function(data){
console.log(data)
terminal.outputs.postline.emit(data)
rlInterface.on('line', (data) => {
this.outputs.lineOut.emit(data)
})
function post(str){
console.log('calling post', str)
var post = (str) => {
console.log(str)
}
var lineIn = new Input('line input', 'string', post)
var lineOut = new Output('line output', 'string')
terminal.inputs = {lineIn}
terminal.outputs = {lineOut}
// ins and outs
this.inputs = {
lineIn: new Input('line input', 'string', post)
}
return terminal
this.outputs = {
lineOut: new Output('line output', 'string')
}
}
// exports
module.exports = {
create: create
}
\ No newline at end of file
module.exports = Terminal
\ No newline at end of file
var Terminal = require('./lib/terminal.js')
var Gcode = require('./lib/gcode.js')
const Terminal = require('./lib/terminal.js')
const Gcode = require('./lib/gcode.js')
var terminal = Terminal.create()
var terminal = new Terminal()
var gcode = new Gcode()
terminal.outputs.lineOut.attach(terminal.inputs.lineIn)
terminal.outputs.lineOut.attach(gcode.inputs.lineIn)
terminal.outputs.lineOut.emit('dada')
gcode.outputs.instructionOut.attach(terminal.inputs.lineIn)
//terminal.outputs.lineout.attach(gcode.inputs.linein)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment