-
Notifications
You must be signed in to change notification settings - Fork 1
/
M2.js
60 lines (48 loc) · 1.58 KB
/
M2.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
/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "CAN_MSG_FLAG_TRANSMIT_UNMODIFIED" }]*/
const CAN_MSG_FLAG_RESET = 0x00
const CAN_MSG_FLAG_TRANSMIT = 0x01
const CAN_MSG_FLAG_TRANSMIT_UNMODIFIED = 0x02
const CMDID_SET_ALL_MSG_FLAGS = 0x01
const CMDID_SET_MSG_FLAGS = 0x02
const CMDID_GET_MSG_LAST_VALUE = 0x03
const CMDID_GET_ALL_MSG_LAST_VALUE = 0x04
/**
* Implements the direct M2 low-level interface. Allows listening for 'm2' events,
* and sending commands to the m2.
*
* TODO: Almost 100% of this code was directly lifted from server code, so it
* Would be really nice to actually share the code, but how?
*/
export default class M2 {
constructor(transport) {
this.transport = transport
}
getAllLastMessageValues() {
const size = 0
this.transport.sendCommand([CMDID_GET_ALL_MSG_LAST_VALUE, size])
}
getLastMessageValue(bus, id) {
const size = 3
this.transport.sendCommand([CMDID_GET_MSG_LAST_VALUE, size, bus, id & 0xff, id >> 8])
}
setAllMessageFlags(flags) {
const size = 1
this.transport.sendCommand([CMDID_SET_ALL_MSG_FLAGS, size, flags & 0xff])
}
setMessageFlags(bus, id, flags) {
const size = 4
this.transport.sendCommand([CMDID_SET_MSG_FLAGS, size, bus, id & 0xff, id >> 8, flags & 0xff])
}
enableAllMessages() {
this.setAllMessageFlags(CAN_MSG_FLAG_TRANSMIT)
}
disableAllMessages() {
this.setAllMessageFlags(CAN_MSG_FLAG_RESET)
}
enableMessage(bus, id) {
this.setMessageFlags(bus, id, CAN_MSG_FLAG_TRANSMIT)
}
disableMessage(bus, id) {
this.setMessageFlags(bus, id, CAN_MSG_FLAG_RESET)
}
}