#pragma once

#include "spi_cs_if.h"

#define MCP41X_C1 5
#define MCP41X_C0 4
#define MCP41X_P1 1
#define MCP41X_P0 0

#define MCP41X_SHUTDOWN_BIT (1 << MCP41X_C1)
#define MCP41X_WRITE_BIT (1 << MCP41X_C0)
#define MCP41X_POT0_BIT (1 << MCP41X_P0)
#define MCP41X_POT1_BIT (1 << MCP41X_P1)

#define MCP41X_SLEEP (MCP41X_SHUTDOWN_BIT | MCP41X_POT0_BIT | MCP41X_POT0_BIT)
#define MCP41X_WRITE0 (MCP41X_WRITE_BIT | MCP41X_POT0_BIT)
#define MCP41X_WRITE1 (MCP41X_WRITE_BIT | MCP41X_POT1_BIT)

typedef enum
{
    MCP41X_ATOB,
    MCP41X_BTOA
} mcp41x_dir_t;

typedef enum
{
    MCP41X_10K,
    MCP41X_50K,
    MCP41X_100K,
    MCP41X_RES_MAX
} mcp41x_res_t;

typedef struct
{
    mcp41x_res_t max_res;
    mcp41x_dir_t dir;

    uint8_t value;

    SPI_HandleTypeDef *hspi;
    cs_handle_t *hcs;
} mcp41x_handle_t;

void mcp41x_init(mcp41x_handle_t *hpot, SPI_HandleTypeDef *hspi, cs_handle_t *hcs, mcp41x_res_t res);
void mcp41x_setValue(mcp41x_handle_t *hpot, uint8_t value);
void mcp41x_setVolume(mcp41x_handle_t *hpot, uint8_t volume);
uint8_t mcp41x_getVolume(mcp41x_handle_t *hpot);
void mcp41x_setResistance(mcp41x_handle_t *hpot, uint32_t resistance);
uint32_t mcp41x_getResistance(mcp41x_handle_t *hpot);
void mcp41x_setWiperDir(mcp41x_handle_t *hpot, mcp41x_dir_t dir);
void mcp41x_sleep(mcp41x_handle_t *hpot);