#include "modbus-master.h"
#include "gefran_gtf.h"
#include <stdint.h>

uint16_t gtf_firmware_version(uint8_t address){
    return gtf_read_register(address, GTF_FIRMWARE_VERSION);
}

void gtf_init(uint8_t addr){
    gtf_write_register(addr, 55, 1<<4);
    gtf_write_register(addr, 56, 0);
}

uint16_t gtf_read_register(uint8_t address, uint8_t reg){
    uint16_t val = 0;
    readReg(address, reg, 1);
    int8_t res = wait_receive(1, &val, 50);

    if(!res)
        return val;
    else
        return res;
}

int8_t gtf_write_register(uint8_t address, uint8_t reg, uint16_t value){
    writeReg(address, reg, value);
    return wait_write(50);
}

int8_t gtf_read_coil(uint8_t address, uint8_t coil){
    uint16_t val=0;
    readCoil(address, coil);
    uint8_t res = wait_receive_coil(20);
    if(res)
        return -1;
    return val;
}

int8_t gtf_write_coil(uint8_t address, uint8_t coil, uint8_t val){
    writeCoil(address, coil, val);
    uint8_t res = wait_write(20);
    if(res)
        return -1;
    return val;
}
