-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBusPirate.js
158 lines (137 loc) · 4.01 KB
/
BusPirate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const util = require('util')
const fs = require('fs')
const EventEmitter = require('events').EventEmitter
const SerialPort = require('serialport')
const async = require('async')
// our InputQueue object
const InputQueue = require('./lib/InputQueue.js')
// add the I2C module
const i2c = require('./lib/i2c.js')
// add the UART module
const uart = require('./lib/uart.js')
/**
* Main BusPirate module
* @module BusPirate
* @author nodebotanist
*/
/**
* Represents a BusPirate object
* @constructor
* @function
* @param {Object} options
* @param {String} options.port -- the absolute path to the serial port of the bus pirate (e.g. /dev/tty.usbserial-xxxx)
*/
function BusPirate(options) {
// throw if no port is given
if (!options || !options.port) {
throw new Error('Port required in options object')
}
// throw if port does not exist
fs.stat(options.port, (err) => {
if (err) {
throw new Error('Port not found')
}
})
EventEmitter.call(this)
// Queue input from the bus pirate for stuff that needs to be synchronus
this.inputQueue = new InputQueue()
// Initial state setup
this._ready = false
this._i2c = false
this.port = new SerialPort(
options.port, {
baudRate: 115200, // to do: make this an option
autoOpen: false
}
)
this.port.on('open', () => { this.emit('open') }) // todo: is this needed?
this.port.on('data', (data) => {
this.inputQueue.add(data)
})
}
util.inherits(BusPirate, EventEmitter)
// Add in the I2C module
Object.assign(BusPirate.prototype, i2c)
// Add in the UART module
Object.assign(BusPirate.prototype, uart)
/**
* Sends a reset code to the bus pirate-- exits the current mode if applicable then performs a hardware reset
* @method reset
*/
BusPirate.prototype.reset = function() {
let exitReady = false
this.port.write([0x00])
async.until(
() => exitReady,
(cb) => {
if (this.inputQueue.length === 0) {
this.port.write([0x00], () => { setTimeout(cb, 10) })
} else {
let message = this.inputQueue.fetchString(5)
if (message && message == 'BBIO1') {
this.port.write([0x0F])
exitReady = true
this.inputQueue.flush()
cb(null)
} else {
setTimeout(cb, 10)
}
}
}
)
}
/**
* Starts the bus pirate.
* @method start
* @fires ready
*/
BusPirate.prototype.start = function() {
this.port.open(() => {
async.until(
() => this._ready,
(cb) => {
let message = this.inputQueue.fetchString(5)
if (message && message == 'BBIO1') {
this._ready = true
/**
* Ready event -- signals the bus pirate is ready to recieve commands
*
* @event ready
*/
this.emit('ready')
this.inputQueue.flush()
cb(null)
} else {
this.port.write([0x00], () => { setTimeout(cb, 10) })
}
}
)
})
}
/**
* Resets the BusPirate to raw BitBang mode
* @method resetMode
* @fires mode_reset
*/
BusPirate.prototype.resetMode = function() {
this._ready = false
async.until(
() => this._ready,
(cb) => {
let message = this.inputQueue.fetchString(5)
if (message && message == 'BBIO1') {
this._ready = true
/**
* Ready event -- signals the bus pirate is ready to recieve commands
*
* @event ready
*/
this.emit('mode_reset')
cb(null)
} else {
this.port.write([0x00], () => { setTimeout(cb, 10) })
}
}
)
}
module.exports = BusPirate