Nucleo Lora / Mbed 2 deprecated STM32_I2C_Sensiron_SCD41

Dependencies:   mbed

Committer:
Picard22
Date:
Mon Aug 16 13:35:58 2021 +0000
Revision:
1:7d24c69d9a0e
Parent:
0:c8ecd653066c
revision_with_med_lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Picard22 0:c8ecd653066c 1 /*
Picard22 0:c8ecd653066c 2 * Copyright (c) 2018, Sensirion AG
Picard22 0:c8ecd653066c 3 * All rights reserved.
Picard22 0:c8ecd653066c 4 *
Picard22 0:c8ecd653066c 5 * Redistribution and use in source and binary forms, with or without
Picard22 0:c8ecd653066c 6 * modification, are permitted provided that the following conditions are met:
Picard22 0:c8ecd653066c 7 *
Picard22 0:c8ecd653066c 8 * * Redistributions of source code must retain the above copyright notice, this
Picard22 0:c8ecd653066c 9 * list of conditions and the following disclaimer.
Picard22 0:c8ecd653066c 10 *
Picard22 0:c8ecd653066c 11 * * Redistributions in binary form must reproduce the above copyright notice,
Picard22 0:c8ecd653066c 12 * this list of conditions and the following disclaimer in the documentation
Picard22 0:c8ecd653066c 13 * and/or other materials provided with the distribution.
Picard22 0:c8ecd653066c 14 *
Picard22 0:c8ecd653066c 15 * * Neither the name of Sensirion AG nor the names of its
Picard22 0:c8ecd653066c 16 * contributors may be used to endorse or promote products derived from
Picard22 0:c8ecd653066c 17 * this software without specific prior written permission.
Picard22 0:c8ecd653066c 18 *
Picard22 0:c8ecd653066c 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Picard22 0:c8ecd653066c 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Picard22 0:c8ecd653066c 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Picard22 0:c8ecd653066c 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
Picard22 0:c8ecd653066c 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Picard22 0:c8ecd653066c 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Picard22 0:c8ecd653066c 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Picard22 0:c8ecd653066c 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Picard22 0:c8ecd653066c 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Picard22 0:c8ecd653066c 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Picard22 0:c8ecd653066c 29 * POSSIBILITY OF SUCH DAMAGE.
Picard22 0:c8ecd653066c 30 */
Picard22 0:c8ecd653066c 31
Picard22 0:c8ecd653066c 32 #include "sensirion_common.h"
Picard22 0:c8ecd653066c 33 #include "sensirion_config.h"
Picard22 0:c8ecd653066c 34
Picard22 0:c8ecd653066c 35 uint16_t sensirion_common_bytes_to_uint16_t(const uint8_t* bytes) {
Picard22 0:c8ecd653066c 36 return (uint16_t)bytes[0] << 8 | (uint16_t)bytes[1];
Picard22 0:c8ecd653066c 37 }
Picard22 0:c8ecd653066c 38
Picard22 0:c8ecd653066c 39 uint32_t sensirion_common_bytes_to_uint32_t(const uint8_t* bytes) {
Picard22 0:c8ecd653066c 40 return (uint32_t)bytes[0] << 24 | (uint32_t)bytes[1] << 16 |
Picard22 0:c8ecd653066c 41 (uint32_t)bytes[2] << 8 | (uint32_t)bytes[3];
Picard22 0:c8ecd653066c 42 }
Picard22 0:c8ecd653066c 43
Picard22 0:c8ecd653066c 44 int16_t sensirion_common_bytes_to_int16_t(const uint8_t* bytes) {
Picard22 0:c8ecd653066c 45 return (int16_t)sensirion_common_bytes_to_uint16_t(bytes);
Picard22 0:c8ecd653066c 46 }
Picard22 0:c8ecd653066c 47
Picard22 0:c8ecd653066c 48 int32_t sensirion_common_bytes_to_int32_t(const uint8_t* bytes) {
Picard22 0:c8ecd653066c 49 return (int32_t)sensirion_common_bytes_to_uint32_t(bytes);
Picard22 0:c8ecd653066c 50 }
Picard22 0:c8ecd653066c 51
Picard22 0:c8ecd653066c 52 float sensirion_common_bytes_to_float(const uint8_t* bytes) {
Picard22 0:c8ecd653066c 53 union {
Picard22 0:c8ecd653066c 54 uint32_t u32_value;
Picard22 0:c8ecd653066c 55 float float32;
Picard22 0:c8ecd653066c 56 } tmp;
Picard22 0:c8ecd653066c 57
Picard22 0:c8ecd653066c 58 tmp.u32_value = sensirion_common_bytes_to_uint32_t(bytes);
Picard22 0:c8ecd653066c 59 return tmp.float32;
Picard22 0:c8ecd653066c 60 }
Picard22 0:c8ecd653066c 61
Picard22 0:c8ecd653066c 62 void sensirion_common_uint32_t_to_bytes(const uint32_t value, uint8_t* bytes) {
Picard22 0:c8ecd653066c 63 bytes[0] = value >> 24;
Picard22 0:c8ecd653066c 64 bytes[1] = value >> 16;
Picard22 0:c8ecd653066c 65 bytes[2] = value >> 8;
Picard22 0:c8ecd653066c 66 bytes[3] = value;
Picard22 0:c8ecd653066c 67 }
Picard22 0:c8ecd653066c 68
Picard22 0:c8ecd653066c 69 void sensirion_common_uint16_t_to_bytes(const uint16_t value, uint8_t* bytes) {
Picard22 0:c8ecd653066c 70 bytes[0] = value >> 8;
Picard22 0:c8ecd653066c 71 bytes[1] = value;
Picard22 0:c8ecd653066c 72 }
Picard22 0:c8ecd653066c 73
Picard22 0:c8ecd653066c 74 void sensirion_common_int32_t_to_bytes(const int32_t value, uint8_t* bytes) {
Picard22 0:c8ecd653066c 75 bytes[0] = value >> 24;
Picard22 0:c8ecd653066c 76 bytes[1] = value >> 16;
Picard22 0:c8ecd653066c 77 bytes[2] = value >> 8;
Picard22 0:c8ecd653066c 78 bytes[3] = value;
Picard22 0:c8ecd653066c 79 }
Picard22 0:c8ecd653066c 80
Picard22 0:c8ecd653066c 81 void sensirion_common_int16_t_to_bytes(const int16_t value, uint8_t* bytes) {
Picard22 0:c8ecd653066c 82 bytes[0] = value >> 8;
Picard22 0:c8ecd653066c 83 bytes[1] = value;
Picard22 0:c8ecd653066c 84 }
Picard22 0:c8ecd653066c 85
Picard22 0:c8ecd653066c 86 void sensirion_common_float_to_bytes(const float value, uint8_t* bytes) {
Picard22 0:c8ecd653066c 87 union {
Picard22 0:c8ecd653066c 88 uint32_t u32_value;
Picard22 0:c8ecd653066c 89 float float32;
Picard22 0:c8ecd653066c 90 } tmp;
Picard22 0:c8ecd653066c 91 tmp.float32 = value;
Picard22 0:c8ecd653066c 92 sensirion_common_uint32_t_to_bytes(tmp.u32_value, bytes);
Picard22 0:c8ecd653066c 93 }
Picard22 0:c8ecd653066c 94
Picard22 0:c8ecd653066c 95 void sensirion_common_copy_bytes(const uint8_t* source, uint8_t* destination,
Picard22 0:c8ecd653066c 96 uint16_t data_length) {
Picard22 0:c8ecd653066c 97 uint16_t i;
Picard22 0:c8ecd653066c 98 for (i = 0; i < data_length; i++) {
Picard22 0:c8ecd653066c 99 destination[i] = source[i];
Picard22 0:c8ecd653066c 100 }
Picard22 0:c8ecd653066c 101 }