Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
1.9 kB
3
Indexable
Never
#include <stdint.h>

typedef enum {
    STATUS_SUCCESS,
    STATUS_BAD_POINTER,
    STATUS_BAD_ARRAY_LENGTH,
    STATUS_NO_SENSOR_DATA
} FuncStatus_t;

FuncStatus_t TransformData(
    const size_t inputDataLength,
    const uint8_t inputDataValues[],
    const size_t outputDataLength,
    uint8_t outputDataValues[]
) {
    // Check for null pointers
    if (inputDataValues == NULL || outputDataValues == NULL) {
        return STATUS_BAD_POINTER;
    }

    // Check for correct input and output array lengths
    if (inputDataLength != 9 || outputDataLength != 33) {
        return STATUS_BAD_ARRAY_LENGTH;
    }

    // Extract the sensor bitmask from the first byte
    uint8_t sensorBitMask = inputDataValues[0];
    uint8_t validSensorCount = 0;

    // Initialize output array to zero
    for (size_t i = 0; i < outputDataLength; i++) {
        outputDataValues[i] = 0;
    }

    // Iterate over the sensor bitmask to count valid sensors and transform data
    for (uint8_t i = 0; i < 8; i++) {
        if (sensorBitMask & (1 << i)) {
            validSensorCount++;
            // Apply the transformation and store it in the correct place in output
            uint32_t transformedValue = inputDataValues[i + 1] * (1 << i);
            size_t byteIndex = 1 + (i * 4);
            outputDataValues[byteIndex] = (transformedValue >> 0) & 0xFF;
            outputDataValues[byteIndex + 1] = (transformedValue >> 8) & 0xFF;
            outputDataValues[byteIndex + 2] = (transformedValue >> 16) & 0xFF;
            outputDataValues[byteIndex + 3] = (transformedValue >> 24) & 0xFF;
        }
    }

    // Set the number of valid sensors in the first byte of the output array
    outputDataValues[0] = validSensorCount;

    // Check if no sensors were valid
    if (validSensorCount == 0) {
        return STATUS_NO_SENSOR_DATA;
    }

    return STATUS_SUCCESS;
}
Leave a Comment