NMEA Web Serial
    Preparing search index...

    Function createNavigationNmeaMachine

    • Convenience function to create a navigation-focused NMEA machine.

      This is a pre-configured machine that computes navigation data (position, time, speed, heading, depth) from NMEA packets. It uses the navigation adapter which automatically handles priority-based fallback between different sentence types.

      Returns StateMachine<
          NmeaContext<NavigationData, StoredPackets>,

              | { type: "CONNECT" }
              | { type: "DISCONNECT" }
              | { type: "SERIAL.DATA"; data: Packet }
              | { type: "SERIAL.ERROR"; error: string }
              | { type: "FATAL_ERROR"; error: string }
              | { type: "SERIAL.DISCONNECTED" }
              | { type: "STOP" }
              | { type: "SET_LOGGING"; enabled: boolean }
              | { type: "SET_BAUD_RATE"; baudRate: number },
          {
              [key: string]:
                  | ActorRefFromLogic<
                      PromiseActorLogic<SerialPort, { baudRate: number }, EventObject>,
                  >
                  | ActorRefFromLogic<
                      CallbackActorLogic<NmeaEvent, { port: SerialPort }, EventObject>,
                  >
                  | ActorRefFromLogic<
                      PromiseActorLogic<void, { port: SerialPort | null }, EventObject>,
                  >
                  | undefined;
          },
          ToProvidedActor<
              {},
              {
                  connectToSerial: PromiseActorLogic<
                      SerialPort,
                      { baudRate: number },
                      EventObject,
                  >;
                  readNmeaStream: CallbackActorLogic<
                      NmeaEvent,
                      { port: SerialPort },
                      EventObject,
                  >;
                  closePort: PromiseActorLogic<
                      void,
                      { port: SerialPort
                      | null },
                      EventObject,
                  >;
              },
          >,
          ToParameterizedObject<
              {
                  setPort: { port: SerialPort };
                  setError: { error: unknown };
                  logParsedData: { packet: Packet };
                  storePacket: { packet: Packet };
                  clearError: { error: string };
                  setFatalError: { error: string };
                  disconnect: NonReducibleUnknown;
                  setLogging: { enabled: boolean };
                  setBaudRate: { baudRate: number };
              },
          >,
          never,
          never,
          "error"
          | "connected"
          | "connecting"
          | "disconnected"
          | "disconnecting",
          string,
          NonReducibleUnknown,
          NonReducibleUnknown,
          EventObject,
          MetaObject,
          ToStateSchema<
              {
                  id: "nmea";
                  initial: "disconnected";
                  context: {
                      port: null;
                      error: null;
                      packets: StoredPackets;
                      data: NavigationData;
                      enableLogging: false;
                      baudRate: 4800;
                  };
                  on: {
                      SET_LOGGING: {
                          actions: {
                              type: "setLogging";
                              params: (
                                  __namedParameters: {
                                      context: NmeaContext;
                                      event: { type: "SET_LOGGING"; enabled: boolean };
                                  },
                              ) => { enabled: boolean };
                          };
                      };
                  };
                  states: {
                      disconnected: {
                          on: {
                              CONNECT: "connecting";
                              SET_BAUD_RATE: {
                                  actions: {
                                      type: "setBaudRate";
                                      params: (
                                          __namedParameters: {
                                              context: NmeaContext;
                                              event: { type: ...; baudRate: ... };
                                          },
                                      ) => { baudRate: number };
                                  };
                              };
                          };
                      };
                      connecting: {
                          invoke: {
                              id: "connectToSerial";
                              src: "connectToSerial";
                              input: (
                                  __namedParameters: {
                                      context: NmeaContext;
                                      event:
                                          | { type: "CONNECT" }
                                          | { type: "DISCONNECT" }
                                          | { type: "SERIAL.DATA"; data: Packet }
                                          | { type: "SERIAL.ERROR"; error: string }
                                          | { type: "FATAL_ERROR"; error: string }
                                          | { type: "SERIAL.DISCONNECTED" }
                                          | { type: "STOP" }
                                          | { type: "SET_LOGGING"; enabled: boolean }
                                          | { type: "SET_BAUD_RATE"; baudRate: number };
                                      self: ActorRef<
                                          MachineSnapshot<
                                              NmeaContext<(...), (...)>,

                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...),
                                              Record<(...), (...)>,
                                              StateValue,
                                              string,
                                              unknown,
                                              any,
                                              any,
                                          >,

                                              | { type: ... }
                                              | { type: ... }
                                              | { type: ...; data: ... }
                                              | { type: ...; error: ... }
                                              | { type: ...; error: ... }
                                              | { type: ... }
                                              | { type: ... }
                                              | { type: ...; enabled: ... }
                                              | { type: ...; baudRate: ... },
                                          AnyEventObject,
                                      >;
                                  },
                              ) => { baudRate: number };
                              onDone: {
                                  target: "connected";
                                  actions: {
                                      type: "setPort";
                                      params: (
                                          __namedParameters: {
                                              context: NmeaContext;
                                              event: DoneActorEvent;
                                          },
                                      ) => { port: SerialPort };
                                  };
                              };
                              onError: {
                                  target: "error";
                                  actions: {
                                      type: "setError";
                                      params: (
                                          __namedParameters: {
                                              context: NmeaContext;
                                              event: ErrorActorEvent;
                                          },
                                      ) => { error: unknown };
                                  };
                              };
                          };
                      };
                      connected: {
                          on: {
                              "SERIAL.DATA": {
                                  actions: readonly [
                                      {
                                          type: "logParsedData";
                                          params: (
                                              __namedParameters: { context: ...; event: ... },
                                          ) => { packet: ... };
                                      },
                                      {
                                          type: "storePacket";
                                          params: (
                                              __namedParameters: { context: ...; event: ... },
                                          ) => { packet: ... };
                                      },
                                  ];
                              };
                              "SERIAL.ERROR": {
                                  actions: {
                                      type: "clearError";
                                      params: (
                                          __namedParameters: {
                                              context: NmeaContext;
                                              event: { type: ...; error: ... };
                                          },
                                      ) => { error: string };
                                  };
                              };
                              FATAL_ERROR: {
                                  target: "error";
                                  actions: {
                                      type: "setFatalError";
                                      params: (
                                          __namedParameters: {
                                              context: NmeaContext;
                                              event: { type: ...; error: ... };
                                          },
                                      ) => { error: string };
                                  };
                              };
                              "SERIAL.DISCONNECTED": { target: "disconnecting" };
                              DISCONNECT: {
                                  actions: ActionFunction<
                                      NmeaContext<NavigationData, StoredPackets>,
                                      { type: "DISCONNECT" },

                                          | { type: "CONNECT" }
                                          | { type: "DISCONNECT" }
                                          | { type: "SERIAL.DATA"; data: Packet }
                                          | { type: "SERIAL.ERROR"; error: string }
                                          | { type: "FATAL_ERROR"; error: string }
                                          | { type: "SERIAL.DISCONNECTED" }
                                          | { type: "STOP" }
                                          | { type: "SET_LOGGING"; enabled: boolean }
                                          | { type: "SET_BAUD_RATE"; baudRate: number },
                                      undefined,
                                      never,
                                      never,
                                      never,
                                      never,
                                      never,
                                  >;
                              };
                          };
                          invoke: {
                              id: "streamReader";
                              src: "readNmeaStream";
                              input: (
                                  __namedParameters: {
                                      context: NmeaContext;
                                      event:
                                          | { type: "CONNECT" }
                                          | { type: "DISCONNECT" }
                                          | { type: "SERIAL.DATA"; data: Packet }
                                          | { type: "SERIAL.ERROR"; error: string }
                                          | { type: "FATAL_ERROR"; error: string }
                                          | { type: "SERIAL.DISCONNECTED" }
                                          | { type: "STOP" }
                                          | { type: "SET_LOGGING"; enabled: boolean }
                                          | { type: "SET_BAUD_RATE"; baudRate: number };
                                      self: ActorRef<
                                          MachineSnapshot<
                                              NmeaContext<(...), (...)>,

                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...),
                                              Record<(...), (...)>,
                                              StateValue,
                                              string,
                                              unknown,
                                              any,
                                              any,
                                          >,

                                              | { type: ... }
                                              | { type: ... }
                                              | { type: ...; data: ... }
                                              | { type: ...; error: ... }
                                              | { type: ...; error: ... }
                                              | { type: ... }
                                              | { type: ... }
                                              | { type: ...; enabled: ... }
                                              | { type: ...; baudRate: ... },
                                          AnyEventObject,
                                      >;
                                  },
                              ) => { port: SerialPort };
                          };
                      };
                      disconnecting: {
                          invoke: {
                              id: "closePort";
                              src: "closePort";
                              input: (
                                  __namedParameters: {
                                      context: NmeaContext;
                                      event:
                                          | { type: "CONNECT" }
                                          | { type: "DISCONNECT" }
                                          | { type: "SERIAL.DATA"; data: Packet }
                                          | { type: "SERIAL.ERROR"; error: string }
                                          | { type: "FATAL_ERROR"; error: string }
                                          | { type: "SERIAL.DISCONNECTED" }
                                          | { type: "STOP" }
                                          | { type: "SET_LOGGING"; enabled: boolean }
                                          | { type: "SET_BAUD_RATE"; baudRate: number };
                                      self: ActorRef<
                                          MachineSnapshot<
                                              NmeaContext<(...), (...)>,

                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...)
                                                  | (...),
                                              Record<(...), (...)>,
                                              StateValue,
                                              string,
                                              unknown,
                                              any,
                                              any,
                                          >,

                                              | { type: ... }
                                              | { type: ... }
                                              | { type: ...; data: ... }
                                              | { type: ...; error: ... }
                                              | { type: ...; error: ... }
                                              | { type: ... }
                                              | { type: ... }
                                              | { type: ...; enabled: ... }
                                              | { type: ...; baudRate: ... },
                                          AnyEventObject,
                                      >;
                                  },
                              ) => { port: SerialPort
                              | null };
                              onDone: { target: "disconnected"; actions: "disconnect" };
                              onError: { target: "disconnected"; actions: "disconnect" };
                          };
                      };
                      error: { on: { CONNECT: "connecting" } };
                  };
              },
          >,
      >

      An XState state machine configured for navigation data computation.

      const machine = createNavigationNmeaMachine();
      const actor = createActor(machine);
      actor.start();
      actor.send({ type: 'CONNECT' });

      The returned machine is an XState state machine. Use it with XState's createActor to create an actor instance, or use the NavigationNmeaClient class for a simpler API that doesn't require direct XState knowledge.