(byte: number)
| 92 | } |
| 93 | |
| 94 | public processByte(byte: number) { |
| 95 | let newStatus: Status = this.status; |
| 96 | |
| 97 | if (this.checkSync(byte)) { // check for completed sync |
| 98 | newStatus = Status.IDLE; |
| 99 | this.emit('synchronized'); |
| 100 | } |
| 101 | else { |
| 102 | switch (this.status) { |
| 103 | case Status.IDLE: |
| 104 | if (byte === 0x00) { break; } // Sync Packet |
| 105 | else if (byte === 0b01110000) { this.emit('overflow'); } |
| 106 | else if ((byte & TIMESTAMP_MASK) === 0x00) { |
| 107 | this.timestamp = 0; |
| 108 | this.resetRxPacket(-1, 5, PacketType.TIMESTAMP); |
| 109 | this.rxWriteByte(byte); |
| 110 | |
| 111 | if (byte & 0x80) { |
| 112 | newStatus = Status.TIMESTAMP; |
| 113 | } |
| 114 | else { |
| 115 | this.emit('timestamp', this.getRxPacket()); |
| 116 | } |
| 117 | } |
| 118 | else if ((byte & LENGTH_MASK) !== 0x00) { |
| 119 | let count = byte & 0x03; |
| 120 | if (count === 3) { count = 4; } |
| 121 | |
| 122 | const port = (byte & PORT_MASK) >>> 3; |
| 123 | |
| 124 | if ((byte & HARDWARE_MASK) !== 0) { |
| 125 | this.resetRxPacket(port, count, PacketType.HARDWARE); |
| 126 | newStatus = Status.HARDWARE_EVENT; |
| 127 | } |
| 128 | else { |
| 129 | this.resetRxPacket(port, count, PacketType.SOFTWARE); |
| 130 | newStatus = Status.SOFTWARE_EVENT; |
| 131 | } |
| 132 | } |
| 133 | else { |
| 134 | newStatus = Status.RESERVED; |
| 135 | this.emit('lost-synchronization'); |
| 136 | } |
| 137 | break; |
| 138 | case Status.TIMESTAMP: |
| 139 | const receivedMax = this.rxWriteByte(byte); |
| 140 | // Check if the continuation bit is false. |
| 141 | // This indicates the last byte in a timestamp |
| 142 | if ((byte & 0x80) === 0x00) { |
| 143 | this.emit('timestamp', this.getRxPacket()); |
| 144 | newStatus = Status.IDLE; |
| 145 | } else if (receivedMax) { |
| 146 | // A timestamp is at most 5 packets. If we didn't see the continuation bit false, something has gone wrong |
| 147 | // This often happens with PeMicro because it starts sending garbage after a clock change. |
| 148 | // In theory we should go to UNSYNCED, but that leads to never recovering. |
| 149 | // Going back to IDLE allows recovery when it stops sending garbage |
| 150 | newStatus = Status.IDLE; |
| 151 | } |
no test coverage detected