/*
 * THIS FILE IS AUTOMATICALLY GENERATED
 *
 * Generator:     sensirion-driver-generator 1.6.1
 * Product:       scd30
 * Model-Version: 1.1.1
 */
/*
 * 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 "SensirionI2cScd30.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[48] = {0};

SensirionI2cScd30::SensirionI2cScd30() {
}

int16_t SensirionI2cScd30::awaitDataReady(void) {
    uint16_t dataReady = 0;
    int16_t localError = 0;

    localError = getDataReady(dataReady);
    if (localError != NO_ERROR) {
        return localError;
    }
    while (dataReady == 0) {
        delay(100);
        localError = getDataReady(dataReady);
        if (localError != NO_ERROR) {
            return localError;
        }
    }
    return localError;
}

int16_t SensirionI2cScd30::blockingReadMeasurementData(float& co2Concentration,
                                                       float& temperature,
                                                       float& humidity) {
    int16_t localError = 0;

    localError = awaitDataReady();
    if (localError != NO_ERROR) {
        return localError;
    }
    localError = readMeasurementData(co2Concentration, temperature, humidity);
    return localError;
}

int16_t SensirionI2cScd30::startPeriodicMeasurement(uint16_t ambientPressure) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

int16_t SensirionI2cScd30::stopPeriodicMeasurement(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

int16_t SensirionI2cScd30::setMeasurementInterval(uint16_t interval) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

int16_t SensirionI2cScd30::getMeasurementInterval(uint16_t& interval) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        SCD30_GET_MEASUREMENT_INTERVAL_CMD_ID, buffer_ptr, 3);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(10);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt16(interval);
    return localError;
}

int16_t SensirionI2cScd30::getDataReady(uint16_t& dataReadyFlag) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        SCD30_GET_DATA_READY_CMD_ID, buffer_ptr, 3);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(10);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt16(dataReadyFlag);
    return localError;
}

int16_t SensirionI2cScd30::readMeasurementData(float& co2Concentration,
                                               float& temperature,
                                               float& humidity) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        SCD30_READ_MEASUREMENT_DATA_CMD_ID, buffer_ptr, 18);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(10);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 18);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 18,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getFloat(co2Concentration);
    localError |= rxFrame.getFloat(temperature);
    localError |= rxFrame.getFloat(humidity);
    return localError;
}

int16_t SensirionI2cScd30::activateAutoCalibration(uint16_t doActivate) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

int16_t SensirionI2cScd30::getAutoCalibrationStatus(uint16_t& isActive) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        SCD30_GET_AUTO_CALIBRATION_STATUS_CMD_ID, buffer_ptr, 3);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(10);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt16(isActive);
    return localError;
}

int16_t SensirionI2cScd30::forceRecalibration(uint16_t co2RefConcentration) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

int16_t
SensirionI2cScd30::getForceRecalibrationStatus(uint16_t& co2RefConcentration) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        SCD30_GET_FORCE_RECALIBRATION_STATUS_CMD_ID, buffer_ptr, 3);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(10);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt16(co2RefConcentration);
    return localError;
}

int16_t SensirionI2cScd30::setTemperatureOffset(uint16_t temperatureOffset) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

int16_t SensirionI2cScd30::getTemperatureOffset(uint16_t& temperatureOffset) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        SCD30_GET_TEMPERATURE_OFFSET_CMD_ID, buffer_ptr, 3);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(10);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt16(temperatureOffset);
    return localError;
}

int16_t SensirionI2cScd30::getAltitudeCompensation(uint16_t& altitude) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        SCD30_GET_ALTITUDE_COMPENSATION_CMD_ID, buffer_ptr, 3);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(10);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt16(altitude);
    return localError;
}

int16_t SensirionI2cScd30::setAltitudeCompensation(uint16_t altitude) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

int16_t SensirionI2cScd30::readFirmwareVersion(uint8_t& major, uint8_t& minor) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

    SensirionI2CTxFrame txFrame = SensirionI2CTxFrame::createWithUInt16Command(
        SCD30_READ_FIRMWARE_VERSION_CMD_ID, buffer_ptr, 3);
    localError =
        SensirionI2CCommunication::sendFrame(_i2cAddress, txFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    delay(10);
    SensirionI2CRxFrame rxFrame(buffer_ptr, 3);
    localError = SensirionI2CCommunication::receiveFrame(_i2cAddress, 3,
                                                         rxFrame, *_i2cBus);
    if (localError != NO_ERROR) {
        return localError;
    }
    localError |= rxFrame.getUInt8(major);
    localError |= rxFrame.getUInt8(minor);
    return localError;
}

int16_t SensirionI2cScd30::softReset(void) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

int16_t SensirionI2cScd30::readSerialNumber(int8_t serialNumber[],
                                            uint16_t serialNumberSize) {
    int16_t localError = NO_ERROR;
    uint8_t* buffer_ptr = communication_buffer;

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

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