/*
 * THIS FILE IS AUTOMATICALLY GENERATED
 *
 * Generator:     sensirion-driver-generator 1.8.0
 * Product:       stc42
 * Model-Version: 1.1.0
 */
/*
 * Copyright (c) 2026, Sensirion AG
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * * Neither the name of Sensirion AG nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "SensirionI2cStc42.h"
#include <Arduino.h>

// make sure that we use the proper definition of NO_ERROR
#ifdef NO_ERROR
#undef NO_ERROR
#endif
#define NO_ERROR 0

static uint8_t communication_buffer[18] = {0};

SensirionI2cStc42::SensirionI2cStc42() {
}

float SensirionI2cStc42::signalHydrogen(uint16_t hydrogenTicks,
                                        uint16_t scaleFactor) {
    float fullScale = 0.0;
    float hydrogen = 0.0;

    fullScale = (float)(scaleFactor & 4095);
    hydrogen = (fullScale * ((float)(hydrogenTicks) - (1 << 14))) / (1 << 15);
    return hydrogen;
}

float SensirionI2cStc42::signalTemperature(uint16_t temperatureTicks) {
    float temperature = 0.0;

    temperature = ((175.0 * (float)(temperatureTicks)) / 65535.0) - 45.0;
    return temperature;
}

float SensirionI2cStc42::signalRelativeHumidity(
    uint16_t relativeHumidityTicks) {
    float relativeHumidity = 0.0;

    relativeHumidity =
        ((125.0 * (float)(relativeHumidityTicks)) / 65535.0) - 6.0;
    return relativeHumidity;
}

int16_t SensirionI2cStc42::readMeasurements(float& hydrogen, float& temperature,
                                            float& humidity,
                                            uint16_t& ambientPressure,
                                            STC42ReadErrorFlags& errorFlags) {
    uint16_t h2Raw = 0;
    uint16_t tRaw = 0;
    uint16_t rhRaw = 0;
    uint16_t p = 0;
    STC42ReadErrorFlags flags = {flags.value = 0u};
    STC42ScaleFactorType scaling = {scaling.value = 0u};
    int16_t localError = 0;

    localError = readMeasurementTicks(h2Raw, tRaw, rhRaw, p, flags, scaling);
    if (localError != NO_ERROR) {
        return localError;
    }
    hydrogen = SensirionI2cStc42::signalHydrogen(h2Raw, scaling.scaleFactor);
    temperature = SensirionI2cStc42::signalTemperature(tRaw);
    humidity = SensirionI2cStc42::signalRelativeHumidity(rhRaw);
    ambientPressure = p;
    errorFlags = flags;
    return localError;
}

int16_t SensirionI2cStc42::performForcedRecalibration(float h2,
                                                      uint16_t scaleFactor) {
    float fullScale = 0.0;
    int32_t hydrogenTicks = 0;
    int16_t localError = 0;

    fullScale = (float)(scaleFactor & 4095);
    hydrogenTicks = (int)(((h2 * 32768.0) / fullScale) + 16384.0);
    localError = performForcedRecalibrationTicks(hydrogenTicks);
    return localError;
}

int16_t SensirionI2cStc42::setCompensationTemperature(float t) {
    int32_t temperatureTicks = 0;
    int16_t localError = 0;

    temperatureTicks = (int)(((t + 45.0) * 65535.0) / 175);
    localError = setCompensationTemperatureTicks(temperatureTicks);
    return localError;
}

int16_t SensirionI2cStc42::setCompensationHumidity(float rh) {
    int32_t humidityTicks = 0;
    int16_t localError = 0;

    humidityTicks = (int)(((rh + 6.0) * 65535.0) / 125);
    localError = setCompensationHumidityTicks(humidityTicks);
    return localError;
}

int16_t SensirionI2cStc42::readProductInfo(uint32_t& productNumber,
                                           int8_t serialNumber[],
                                           uint16_t serialNumberSize) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_READ_PRODUCT_INFO_CMD_ID, buffer_ptr, 18);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(5);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 18);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 18,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt32(productNumber);
    localError |= rxFrame.getBytes((uint8_t*)serialNumber, serialNumberSize);
    return localError;
}

int16_t
SensirionI2cStc42::startConcentrationMeasurement(uint16_t measurementPeriod) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_START_CONCENTRATION_MEASUREMENT_CMD_ID, buffer_ptr, 5);
    localError |= txFrame.addUInt16(measurementPeriod);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(75);
    return localError;
}

int16_t SensirionI2cStc42::readMeasurementTicks(
    uint16_t& gasConcentration, uint16_t& temperature,
    uint16_t& relativeHumidity, uint16_t& ambientPressure,
    STC42ReadErrorFlags& errorFlags, STC42ScaleFactorType& scaleFactor) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_READ_MEASUREMENT_TICKS_CMD_ID, buffer_ptr, 18);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 18);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 18,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt16(gasConcentration);
    localError |= rxFrame.getUInt16(temperature);
    localError |= rxFrame.getUInt16(relativeHumidity);
    localError |= rxFrame.getUInt16(ambientPressure);
    localError |= rxFrame.getUInt16(errorFlags.value);
    localError |= rxFrame.getUInt16(scaleFactor.value);
    return localError;
}

int16_t SensirionI2cStc42::stopMeasurement(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_STOP_MEASUREMENT_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::compensateTemperatureWithSht41(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_COMPENSATE_TEMPERATURE_WITH_SHT41_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::enableManualTemperatureCompensation(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_ENABLE_MANUAL_TEMPERATURE_COMPENSATION_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t
SensirionI2cStc42::setCompensationTemperatureTicks(uint16_t temperatureTicks) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_SET_COMPENSATION_TEMPERATURE_TICKS_CMD_ID, buffer_ptr, 5);
    localError |= txFrame.addUInt16(temperatureTicks);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::compensateHumidityWithSht41(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_COMPENSATE_HUMIDITY_WITH_SHT41_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::enableManualHumidityCompensation(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_ENABLE_MANUAL_HUMIDITY_COMPENSATION_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::setCompensationHumidityTicks(
    uint16_t relativeHumidityTicks) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_SET_COMPENSATION_HUMIDITY_TICKS_CMD_ID, buffer_ptr, 5);
    localError |= txFrame.addUInt16(relativeHumidityTicks);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::enableManualPressureCompensation(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_ENABLE_MANUAL_PRESSURE_COMPENSATION_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::setCompensationPressureMbar(uint16_t pressureMbar) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_SET_COMPENSATION_PRESSURE_MBAR_CMD_ID, buffer_ptr, 5);
    localError |= txFrame.addUInt16(pressureMbar);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t
SensirionI2cStc42::performForcedRecalibrationTicks(uint16_t gasConcentration) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_PERFORM_FORCED_RECALIBRATION_TICKS_CMD_ID, buffer_ptr, 5);
    localError |= txFrame.addUInt16(gasConcentration);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::enableAutomaticSelfCalibration(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_ENABLE_AUTOMATIC_SELF_CALIBRATION_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::disableAutomaticSelfCalibration(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_DISABLE_AUTOMATIC_SELF_CALIBRATION_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::enterSleepMode(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_ENTER_SLEEP_MODE_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::enableLowPowerMode(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_ENABLE_LOW_POWER_MODE_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t SensirionI2cStc42::exitLowPowerMode(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_EXIT_LOW_POWER_MODE_CMD_ID, buffer_ptr, 2);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t
SensirionI2cStc42::setAlertUpperThreshold(uint16_t uppperThresholdTicks) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_SET_ALERT_UPPER_THRESHOLD_CMD_ID, buffer_ptr, 5);
    localError |= txFrame.addUInt16(uppperThresholdTicks);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

int16_t
SensirionI2cStc42::setAlertLowerThreshold(uint16_t lowerThresholdTicks) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        STC42_SET_ALERT_LOWER_THRESHOLD_CMD_ID, buffer_ptr, 5);
    localError |= txFrame.addUInt16(lowerThresholdTicks);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(1);
    return localError;
}

void SensirionI2cStc42::begin(TwoWire& i2cBus, uint8_t i2cAddress) {
    _i2cBus = &i2cBus;
    _i2cAddress = i2cAddress;
}
