// Copyright (C) 2026 Pierre Barbier <pierrebarbier741@gmail.com>
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <ring_buffer.h>
#include <stdbool.h>
#include <stdint.h>

#define MAX_CAM_MISS 2
#define SPARK_ADVANCE 40
#define INJECTION_PHASE 0

typedef enum { SYNC_OK = 0, SYNC_PENDING = 1, SYNC_NOT_OK = 2 } sync_state_t;

typedef enum {
  CYCLE_COMBUSTION = 0,
  CYCLE_EXHAUST = 1,
  CYCLE_INTAKE = 2,
  CYCLE_COMPRESSION = 3,
  CYCLE_UNKNOWN = 4
} crank_state_t;

typedef enum { CAM_IDLE = 0, CAM_TRIGD = 1 } cam_state_t;

typedef enum {
  SPARK_IDLE = 0,
  SPARK_CHARGING = 1,
  SPARK_NONE = 2
} spark_state_t;

typedef struct {
  crank_state_t crank_state;
  cam_state_t cam_state;
  uint8_t cam_miss_ctr;
  sync_state_t sync_state;
  ring_buffer_t crank_RB;
  ring_buffer_t cam_RB;
  spark_state_t next_spark_state;
  spark_state_t current_spark_state;
} global_state_t;
