Commit 091bd07e authored by Jake Read's avatar Jake Read

add note and bugfix on programmatic rep

parent 860a0c9d
......@@ -139,7 +139,7 @@ Pardon my at-the-moment brief explanation.
# Writing New Modules
Whenever a menu is requested, the system searches ```modules/ * ``` for *anything*. include your .js of a new module there.
Whenever a menu is requested, the system searches ```modules/ * ``` for *anything*. When you write a new module, just include your newmodule.js there, and the program should find it.
These modules are written with objects inherited from ```lib/jsunit.js```
......@@ -217,6 +217,52 @@ module.exports = uiNum
Hardware Modules are identical to other modules, but they inherit a class that can be found and examined at ```lib/atkunit.js``` which subsequently calls hardware-interfacing classes from ```lib/atkroute.js``` and often use tools from ```lib/packets.js``` (packets.js is mostly about packing 64-bit information into byte-size units).
# Programatically Connecting and Loading Modules
We can also write and manipulate modules as software objects. This is sometimes very nice.
I.E. here is 'main.js' as configured to run the small program described.
```javascript
// business
const Reps = require('./reps.js')
const Programs = require('./programs.js')
// the program object: real simple, just has a description, and a 'modules'
var program = Programs.new('new program')
/* example program-like-an-api */
// load some modules
var multiline = Programs.loadModuleFromSource(program, './modules/ui/multiline.js')
var gcode = Programs.loadModuleFromSource(program, './modules/parsing/gcode.js')
// attaching: always like outputs to inputs
multiline.outputs.lineOut.attach(gcode.inputs.lineIn)
// we can move things around here as well
multiline.description.position = {
left: 50,
top: 50
}
gcode.description.position = {
left: 500,
top: 100
}
// if I have a public function in a module, I can also use that
multiline.load('./files/dogbone.gcode')
// UI
const View = require('./views.js')
View.startHttp()
View.startWs()
Programs.assignSocket(View.uiSocket)
View.assignProgram(program)
```
# Development Notes
## For MW
......
......@@ -27,9 +27,31 @@ programs are assemblies of modules
const Reps = require('./reps.js')
const Programs = require('./programs.js')
// the program object: real simple, just has a description, and a 'modules'
var program = Programs.new('new program')
// var step = Programs.loadModuleFromSource(program, './modules/hardware/atkstepper.js')
/* example program-like-an-api
// load some modules
var multiline = Programs.loadModuleFromSource(program, './modules/ui/multiline.js')
var gcode = Programs.loadModuleFromSource(program, './modules/parsing/gcode.js')
// attaching: always like outputs to inputs
multiline.outputs.lineOut.attach(gcode.inputs.lineIn)
// we can move things around here as well
multiline.description.position = {
left: 50,
top: 50
}
gcode.description.position = {
left: 500,
top: 100
}
// if I have a public function in a module, I can also use that
multiline.load('./files/dogbone.gcode')
*/
// UI
const View = require('./views.js')
......
// boilerplate atkapi header
const InOut = require('../../lib/jsunit.js')
let Input = InOut.Input
let Output = InOut.Output
let State = InOut.State
let Button = InOut.Button
const Hardware = require('../../lib/atkunit.js')
const PCKT = require('../../lib/packets.js')
// a constructor, a fn, a javascript mess
function ATKMathRobot() {
// we make the module, starting from this base
// '0,1' is the route to our piece of hardware
// 'onPacket' is the function that will be called
// when data arrives from that port
var atkmr = Hardware()
// change the name ...
atkmr.description.name = 'atk-math-robot-joint'
// inputs and outputs as usual
atkmr.inputs = {
pc_t: Input('number', onPositionTargetInput)
}
atkmr.outputs = {
ok: Output('nothing-yet')
}
// and state as well
var state = atkmr.state
state.message = 'no packet yet'
state.pc_t = 2048
state.onChange('pc_t', onPositionTargetUserChange)
state.walk = 1024
state.tickButton = Button('Walk Value', onPositionTickTock)
// to send things down the well, we can use
// atkmr.route.send(packet)
// where packet is a byte array
function onPositionTargetInput(evt){
console.log("INPUTS NOT YET BOUND", evt)
}
function onPositionTargetUserChange() {
var pc_t = state.pc_t
if (pc_t > 16384) {
pc_t = 16384
} else if (pc_t < 0) {
pc_t = 0
}
var pc_tpack = PCKT.pack32(pc_t)
pc_tpack.unshift(143)
state.message = 'packet out'
atkmr.route.send(pc_tpack)
}
function onPositionTickTock(){
var pc_t = state.pc_t
pc_t += state.walk
pc_t %= 16384
state.pc_t = pc_t
onPositionTargetUserChange()
}
// to get replies to certain packets, we can
// subscribe, where 141 (here) is the 'key'
// we're looking for on the network, and the
// msg is byte array we'll get back
atkmr.route.subscribe(143, function(msg){
state.message = 'packet ok'
})
return atkmr
}
// exports
module.exports = ATKMathRobot
\ No newline at end of file
......@@ -150,7 +150,6 @@ function ATKSerialLink() {
// we'll make sure it's what we think it will be
// console.log("PORT DATA")
var dtArray = new Array()
console.log('sp data', data)
if (Buffer.isBuffer(data)) {
for (var i = 0; i < data.length; i++) {
dtArray[i] = data[i]
......
......@@ -119,7 +119,7 @@ function socketRecv(evt) {
}
function socketSend(type, data) {
if (sckt.readyState === 1) {
if (sckt != null && sckt.readyState === 1) {
var msg = {
type: type,
data: data
......@@ -127,7 +127,7 @@ function socketSend(type, data) {
//console.log('SEND', msg)
sckt.send(JSON.stringify(msg))
} else {
throw 'on socketSend, ws not ready'
console.log('on socketSend, ws not ready')
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment