#include <stdint.h>
#include <xc.h>

#ifndef STRUCTURES
#define STRUCTURES

#define PI 3.141592654



#define SW1 (ui_state.keys & 0x01)
#define SW2 (ui_state.keys & 0x02)
#define SW3 (ui_state.keys & 0x04)
#define SW4 (ui_state.keys & 0x08)
#define SW5 (ui_state.keys & 0x10)
#define SW6 (ui_state.keys & 0x20)
#define SW7 (ui_state.keys & 0x40)
#define SW8 (ui_state.keys & 0x80)
#define D1(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 0) :  (ui_state.leds &= ~((uint32_t)1 << 0))
#define D2(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 1) :  (ui_state.leds &= ~((uint32_t)1 << 1))
#define D3(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 2) :  (ui_state.leds &= ~((uint32_t)1 << 2))
#define D4(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 3) :  (ui_state.leds &= ~((uint32_t)1 << 3))
#define D5(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 4) :  (ui_state.leds &= ~((uint32_t)1 << 4))
#define D6(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 5) :  (ui_state.leds &= ~((uint32_t)1 << 5))
#define D7(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 6) :  (ui_state.leds &= ~((uint32_t)1 << 6))
#define D8(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 7) :  (ui_state.leds &= ~((uint32_t)1 << 7))
#define D9(x)  (x == 1) ? (ui_state.leds |= (uint32_t)1 << 8) :  (ui_state.leds &= ~((uint32_t)1 << 8))
#define D10(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 9) :  (ui_state.leds &= ~((uint32_t)1 << 9))
#define D11(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 10) : (ui_state.leds &= ~((uint32_t)1 << 10))
#define D12(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 11) : (ui_state.leds &= ~((uint32_t)1 << 11))
#define D13(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 12) : (ui_state.leds &= ~((uint32_t)1 << 12))
#define D14(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 13) : (ui_state.leds &= ~((uint32_t)1 << 13))
#define D15(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 14) : (ui_state.leds &= ~((uint32_t)1 << 14))
#define D16(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 15) : (ui_state.leds &= ~((uint32_t)1 << 15))
#define D17(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 16) : (ui_state.leds &= ~((uint32_t)1 << 16))
#define D18(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 17) : (ui_state.leds &= ~((uint32_t)1 << 17))
#define D19(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 18) : (ui_state.leds &= ~((uint32_t)1 << 18))
#define D20(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 19) : (ui_state.leds &= ~((uint32_t)1 << 19))
#define D21(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 20) : (ui_state.leds &= ~((uint32_t)1 << 20))
#define D22(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 21) : (ui_state.leds &= ~((uint32_t)1 << 21))
#define D23(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 22) : (ui_state.leds &= ~((uint32_t)1 << 22))
#define D24(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 23) : (ui_state.leds &= ~((uint32_t)1 << 23))
#define D25(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 24) : (ui_state.leds &= ~((uint32_t)1 << 24))
#define D26(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 25) : (ui_state.leds &= ~((uint32_t)1 << 25))
#define D27(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 26) : (ui_state.leds &= ~((uint32_t)1 << 26))
#define D28(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 27) : (ui_state.leds &= ~((uint32_t)1 << 27))
#define D29(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 28) : (ui_state.leds &= ~((uint32_t)1 << 28))
#define D30(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 29) : (ui_state.leds &= ~((uint32_t)1 << 29))
#define D31(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 30) : (ui_state.leds &= ~((uint32_t)1 << 30))
#define D32(x) (x == 1) ? (ui_state.leds |= (uint32_t)1 << 31) : (ui_state.leds &= ~((uint32_t)1 << 31))


struct results
{
    double z_real;
    double z_imag;
    double y_real;
    double y_imag;
    double z_mod;
    double z_arg;
    double y_mod;
    double y_arg;
};

struct ui_state
{
    uint8_t disp1_arr[6];
    uint8_t disp2_arr[6];
    char suffix1;
    char suffix2;
    int8_t disp_format;
    uint32_t leds;
    uint8_t keys;
};

struct config
{
    uint8_t fq_sel;
    uint8_t amp_sel;
    uint8_t do_the_cal;         //is set when ui requests calibration
    uint8_t comp_open;          //is set when ui requests open circuit compensation
    uint8_t comp_short;         //is set when ui requests short circuit compensation
    uint8_t turn_off;        //is set when ui requests turn on/off
};

struct hys_buf_struct
{
    uint8_t hys;
    uint8_t ext_last;        
};

#endif
