#include <stdlib.h>
#include <stdint.h>
#include <stm32f4xx.h>

#ifndef PIN_H
#define PIN_H

typedef struct {
  GPIO_TypeDef* gpio;
  uint8_t pin;
} pin_t;

typedef enum {
  NONE = 0,
  PULLUP = 1,
  PULLDOWN = 2,
  PULL_RESERVED = 3
} pin_pullmode_t;

typedef enum {
  INPUT,
  OUTPUT,
  ALTERNATE,
  ANALOG
} pin_mode_t;

typedef enum {
  LOW_SPEED,
  MEDIUM_SPEED,
  HIGH_SPEED,
  VERY_HIGH_SPEED
} pin_speedmode_t;

void pin_init(pin_t* pin, GPIO_TypeDef* gpio, uint8_t pin_index);

void pin_mode(pin_t* pin, pin_mode_t mode);
void pin_pull(pin_t* pin, pin_pullmode_t mode);
void pin_speed(pin_t* pin, pin_speedmode_t mode);

// Reads input, not output!
uint8_t pin_read(pin_t* pin);
uint8_t pin_write(pin_t* pin, uint8_t val);
void pin_toggle(pin_t* pin);
void pin_set(pin_t* pin);
void pin_reset(pin_t* pin);

void pin_into_pulldown(pin_t* pin);
void pin_into_pullup(pin_t* pin);
void pin_into_no_pull(pin_t* pin);

void pin_into_output(pin_t* pin);
void pin_into_output_pushpull(pin_t* pin);
void pin_into_output_opendrain(pin_t* pin);
void pin_into_pushpull(pin_t* pin);
void pin_into_opendrain(pin_t* pin);

void pin_into_input(pin_t* pin);
void pin_into_input_highspeed(pin_t* pin);

void pin_into_alternate(pin_t* pin, uint8_t alternate);
void pin_into_alternate_highspeed(pin_t* pin, uint8_t alternate);

void pin_into_highspeed(pin_t* pin);

#endif // PIN_H
