Skip to content

Commit fe06bf1

Browse files
committed
Base connection protocol class and VM/Execution stack
0 parents  commit fe06bf1

11 files changed

+1099
-0
lines changed

.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
# Google Cloud files
61+
source-context.json
62+
source-contexts.json
63+
64+
# Mac OSx files
65+
.DS_Store

CursorFile.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class CursorFile {
2+
3+
4+
5+
}

CursorReal.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class CursorReal {
2+
3+
constructor (serialPort) {
4+
5+
}
6+
7+
async next() {
8+
9+
}
10+
11+
async previous() {
12+
13+
}
14+
15+
16+
halt, read, reset...
17+
18+
}

ReconnectableSerialPort.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const _ = require('lodash')
2+
const EventEmitter = require('events')
3+
4+
const SerialPort = require('serialport')
5+
const Ready = SerialPort.parsers.Ready
6+
const Readline = SerialPort.parsers.Readline
7+
8+
module.exports = class ReconnectableSerialPort extends EventEmitter{
9+
10+
constructor(pattern) {
11+
super()
12+
13+
this.port = null
14+
this.pattern = pattern
15+
16+
// Configure readline parser
17+
// this.ready = new Ready({data: 'init'})
18+
19+
this.readLine = new Readline()
20+
this.ready = new Ready({
21+
delimiter: 'init',
22+
})
23+
24+
this.readLine.on('data', (data) => { this.emit('data', data) })
25+
this.ready.on('ready', () => { this.emit('ready') })
26+
27+
// Init attempt to connect
28+
this.attemptConnect()
29+
this.keepTrying()
30+
}
31+
32+
write(data) {
33+
if (!this.connected())
34+
return false
35+
36+
this.port.write(data)
37+
}
38+
39+
async attemptConnect() {
40+
if (this.connected()) {
41+
return true
42+
}
43+
44+
// List all serial ports
45+
let boards = await SerialPort.list()
46+
47+
// Find board
48+
let board = _.find(boards, this.pattern)
49+
50+
if (!board)
51+
return false
52+
53+
// Save connection
54+
this.port = await new SerialPort(board.comName, {baudRate: 115200});
55+
56+
// Listen to close event
57+
this.port.on('close', () => { this.keepTrying() })
58+
59+
// Pipe stream to readline
60+
this.port.pipe(this.ready)
61+
this.port.pipe(this.readLine)
62+
63+
// Rebind events
64+
_.forEach(['close', 'open', 'error'], (event) => {
65+
this.port.on(event, () => { this.emit(event) })
66+
})
67+
68+
return this.port
69+
}
70+
71+
keepTrying() {
72+
clearTimeout(this.timeout)
73+
74+
if (this.connected()) {
75+
return
76+
}
77+
78+
this.timeout = setTimeout(async () => {
79+
if ( await this.attemptConnect() )
80+
return
81+
82+
this.keepTrying()
83+
}, 300)
84+
}
85+
86+
connected() {
87+
if (!this.port)
88+
return false
89+
90+
return this.port.isOpen || this.port.opening
91+
}
92+
}

Robot.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Robot {
2+
3+
constructor (serialPort) {
4+
this.serialPort = serialPort
5+
}
6+
7+
front(){
8+
9+
}
10+
11+
back(){
12+
13+
}
14+
15+
}

SerialCommandProtocol.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const _ = require('lodash')
2+
3+
module.exports = class SerialCommandProtocol {
4+
constructor(serialPort) {
5+
this.promiseQueue = []
6+
this.serialPort = serialPort
7+
serialPort.on('data', (data) => this.dataReceived(data))
8+
serialPort.on('open', () => this.resetQueue())
9+
}
10+
11+
resetQueue() {
12+
this.promiseQueue = []
13+
}
14+
15+
dataReceived(data) {
16+
console.log('data:', data)
17+
let promise = this.promiseQueue.shift()
18+
19+
if (!promise)
20+
return
21+
22+
promise.resolve(data)
23+
}
24+
25+
execute(command) {
26+
this.serialPort.write(command)
27+
let promise = new Promise((resolve, reject) => {
28+
let queued = {resolve, reject}
29+
this.promiseQueue.push(queued)
30+
31+
// Timeout command
32+
setTimeout(() => {
33+
// Remove from queue
34+
_.pull(this.promiseQueue, queued)
35+
36+
// Reject
37+
reject('Timeout action')
38+
}, 5000)
39+
})
40+
41+
return promise
42+
}
43+
44+
}

VM.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class VM {
2+
constructor(cursorDelegate, robotDelegate) {
3+
this.cursor = cursorDelegate
4+
this.robot = robotDelegate
5+
}
6+
7+
8+
async run () {
9+
await this.cursor.reset()
10+
11+
// Read current
12+
try {
13+
while (!await this.cursor.ended()) {
14+
let command = await this.cursor.read()
15+
16+
await executeCommand(command)
17+
}
18+
} catch (e) {
19+
this.cursor.halt()
20+
}
21+
}
22+
23+
async executeCommand(command) {
24+
// TODO
25+
26+
if (command == [ ])
27+
await this.robot.front()
28+
}
29+
}

main.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const VM = require('./VM')
2+
const Robot = require('./Robot')
3+
const CursorReal = require('./CursorReal')
4+
5+
(async () => {
6+
7+
let robot = new Robot(serial...)
8+
let cursor = new CursorReal(serial...)
9+
10+
let vm = new VM(cursor, robot)
11+
12+
})()

0 commit comments

Comments
 (0)