handleSerial method

void handleSerial(
  1. List<int> bytes
)

Handles incoming serial bytes

Implementation

void handleSerial(List<int> bytes) {
  for (final packet in bytes.splitAfter((element) => element == end)) {
    final message = parseOsc(slip.decode(packet));
    if (message == null) {
      continue;
    }
    if (message.address == "/button") {
      handleCommand(SubsystemsCommand(zeroIMU: true));
    }
    if (message.address == "/ahrs/zero") {
      // signal that the zero was received and processed
      if (serial.isOpen) {
        final command = OSCMessage("/identify", arguments: []);
        serial.write(slip.encode(command.toBytes()).toUint8List());
      }
      // send a duplicate of a subsystems command as a "handshake"
      collection.server.sendMessage(SubsystemsCommand(zeroIMU: true));
    }
    if (message.address == "/euler") {
      final orientation = Orientation(
        x: message.arguments[0] as double,
        y: message.arguments[1] as double,
        z: message.arguments[2] as double,
      );
      final position = RoverPosition(orientation: orientation, version: positionVersion);
      collection.server.sendMessage(position);
      collection.server.sendMessage(position, destination: autonomySocket);
    }
  }
}