Loco Parser - cloudthrottle/dcc-ex--commands GitHub Wiki

Usage

const message = `<l 10 1 130 43>` // would come from the Command Station
const result = locoParser(message)

console.log(result);

// {
//   key: 'l',
//   parser: 'locoParser',
//   params: {
//     cabId: 10,
//     register: 1,
//     speed: 1, // See parseSpeedAndDirection()
//     direction: 1, // See parseSpeedAndDirection()
//     functionButtons: { // See parseFunctionButtons()
//       '0': {
//         value: 1
//       },
//       '1': {
//         value: 1
//       },
//       '2': {
//         value: 0
//       },
//       '3': {
//         value: 1
//       },
//       '4': {
//         value: 0
//       },
//       '5': {
//         value: 1
//       },
//       '6': {
//         value: 0
//       },
//       '7': {
//         value: 0
//       },
//       '8': {
//         value: 0
//       },
//       '9': {
//         value: 0
//       },
//       '10': {
//         value: 0
//       },
//       '11': {
//         value: 0
//       },
//       '12': {
//         value: 0
//       },
//       '13': {
//         value: 0
//       },
//       '14': {
//         value: 0
//       },
//       '15': {
//         value: 0
//       },
//       '16': {
//         value: 0
//       },
//       '17': {
//         value: 0
//       },
//       '18': {
//         value: 0
//       },
//       '19': {
//         value: 0
//       },
//       '20': {
//         value: 0
//       },
//       '21': {
//         value: 0
//       },
//       '22': {
//         value: 0
//       },
//       '23': {
//         value: 0
//       },
//       '24': {
//         value: 0
//       },
//       '25': {
//         value: 0
//       },
//       '26': {
//         value: 0
//       },
//       '27': {
//         value: 0
//       },
//       '28': {
//         value: 0
//       }
//     }
//   },
//   status: 'success'
// }

parseSpeedAndDirection()

Speed value from the Command Station(130) includes the direction of travel.

Values 0 -> 127 (inclusive) result in a "Reverse" direction

Values 128 -> 255 (inclusive) result in a "Forward" direction

A normalised speed is calculated by subtracting 128 from the speed value of the Command Station if the value is greater than or equal to 128

Normalised speed value 0 equates to a stop.

Normalised speed value 1 equates to an emergency stop. In this library emergency stop is represented by a -1 value

All other speed values are calculated as the normalised speed - 1

parseFunctionButtons()

There are 29 function buttons.

The value of the function buttons from the Command Station(43) is converted into a binary string (101011) and then the string is reversed (110101).

This binary representation (in this example) maps to the values for functions '0' -> '5' (inclusive).

All other function values are assumed to be 0

⚠️ **GitHub.com Fallback** ⚠️