/*
  Name:    SauronPlus.h
  Author:  Roman Schönfeld & Daniel Hausherr
*/

/***********************************************************************************************
  This work is licensed under a Creative Commons Attribution 4.0 International (CC BY 4.0) License.

  You are free to:

  Share — copy and redistribute the material in any medium or format
  Adapt — remix, transform, and build upon the material
  for any purpose, even commercially.

  The licensor cannot revoke these freedoms as long as you follow the license terms.

  Under the following terms:

  Attribution — You must give appropriate credit, provide a link to the license, and indicate if
  changes were made. You may do so in any reasonable manner, but not in any way that suggests
  the licensor endorses you or your use.

  No additional restrictions — You may not apply legal terms or technological measures that
  legally restrict others from doing anything the license permits.

  Credits:
  Laboratory for Physics and Illumination Technologies,
  University of Applied Sciences Southern Westfalia
  Germany

  Make Light! Support by the BMBF in the development of this device is gratefully acknowledged
***************************************************************************************************/
#ifndef _SAURONPLUS_h
#define _SAURONPLUS_h

#define SOFT_SDA 6
#define SOFT_SCL 7

#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif

#include "FreqCounter.h"

#include <SoftWire.h>

extern SoftWire Wire;

/***************************************************************************************************
**  Define Constants
**
**  Define directives providing meaningful names for constant values.
***************************************************************************************************/
//Software I2C
#define SCL_PIN                 7
#define SCL_PORT                PORTD
#define SDA_PIN                 6
#define SDA_PORT                PORTD

#define _READY_PIN              0x02     // definiert den ready pin des Saurons zur Abfrage, ob Messwerte verfügbar sind

#define _SW_EN                  A2
#define _SW_A0                  A1
#define _SW_A1                  A0

#define _OUTX                   0x03     // Input Pin 
#define _OSR                    0x00     // Operational State Register 
#define _CMD                    0x08     // Registereintrag = 8b im Register CREGH (Command Register High Byte), versetzt den Sauron in den Kommandmodus und deaktiviert den Hardwareteiler
#define _CREGL                  0x06     // Register CREGL (Command Register Low Byte)
#define _CREGH                  0x07     // Register CREGH (Command Register HIGH Byte)
#define _CONFIG_MODE            0x02     // Registereintrag = 2b, im OSR der Sauron befindet sich im Konfigurationsmodus + Stopmodus + Standby off
#define _MEASUREMENT_MODE       0xC3     // Registereintrag = 0xC3, im OSR der Sauron befindet sich im Messmodus + Startmodus + Standby on
// Für weitere Informationen siehe Datenblatt "Mazet MCDC04"

#define START                   true
#define STOP                    false

typedef enum {
  TINT_MS_1 = 0,
  TINT_MS_2,
  TINT_MS_4,
  TINT_MS_8,
  TINT_MS_16,
  TINT_MS_32,
  TINT_MS_64,

  TINT_COUNT
} integrationTime;

typedef enum {
  IREF_NA_20 = 0,
  IREF_NA_80,
  IREF_NA_320,
  IREF_NA_1280,
  IREF_NA_5120,

  IREF_COUNT
} referenceCurrent;

typedef enum {
  ATTENUATION_1 = 0,
  ATTENUATION_10,
  ATTENUATION_100,
  ATTENUATION_1000,
  ATTENUATION_X,

  ATTENUATION_COUNT
} attenuation;

typedef enum {
  IO_LOW = 0x00,
  IO_HIGH_VFC = 0x03,
  IO_HIGH_MAZET = 0x0C,

  IO_COUNT
} io_expander;

typedef enum {
  IO_OUTPUT = 0x00,
  
  IO_DIRECTION_COUNT
} io_expander_direction;

extern volatile bool ready_pin_state_high; 

extern integrationTime        current_time;
extern io_expander            current_io;
extern attenuation            current_attenuation;

extern void init_device();
extern double get_max_range();
extern uint8_t get_range();
extern int8_t set_settings_device();
extern int8_t define_matching_range();
extern int8_t get_value_in_current(double*);

#endif
