Nucleo Lora / Mbed 2 deprecated STM32_I2C_Sensiron_SCD41

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sensirion_common.c Source File

sensirion_common.c

00001 /*
00002  * Copyright (c) 2018, Sensirion AG
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * * Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  * * Redistributions in binary form must reproduce the above copyright notice,
00012  *   this list of conditions and the following disclaimer in the documentation
00013  *   and/or other materials provided with the distribution.
00014  *
00015  * * Neither the name of Sensirion AG nor the names of its
00016  *   contributors may be used to endorse or promote products derived from
00017  *   this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include "sensirion_common.h"
00033 #include "sensirion_config.h"
00034 
00035 uint16_t sensirion_common_bytes_to_uint16_t(const uint8_t* bytes) {
00036     return (uint16_t)bytes[0] << 8 | (uint16_t)bytes[1];
00037 }
00038 
00039 uint32_t sensirion_common_bytes_to_uint32_t(const uint8_t* bytes) {
00040     return (uint32_t)bytes[0] << 24 | (uint32_t)bytes[1] << 16 |
00041            (uint32_t)bytes[2] << 8 | (uint32_t)bytes[3];
00042 }
00043 
00044 int16_t sensirion_common_bytes_to_int16_t(const uint8_t* bytes) {
00045     return (int16_t)sensirion_common_bytes_to_uint16_t(bytes);
00046 }
00047 
00048 int32_t sensirion_common_bytes_to_int32_t(const uint8_t* bytes) {
00049     return (int32_t)sensirion_common_bytes_to_uint32_t(bytes);
00050 }
00051 
00052 float sensirion_common_bytes_to_float(const uint8_t* bytes) {
00053     union {
00054         uint32_t u32_value;
00055         float float32;
00056     } tmp;
00057 
00058     tmp.u32_value = sensirion_common_bytes_to_uint32_t(bytes);
00059     return tmp.float32;
00060 }
00061 
00062 void sensirion_common_uint32_t_to_bytes(const uint32_t value, uint8_t* bytes) {
00063     bytes[0] = value >> 24;
00064     bytes[1] = value >> 16;
00065     bytes[2] = value >> 8;
00066     bytes[3] = value;
00067 }
00068 
00069 void sensirion_common_uint16_t_to_bytes(const uint16_t value, uint8_t* bytes) {
00070     bytes[0] = value >> 8;
00071     bytes[1] = value;
00072 }
00073 
00074 void sensirion_common_int32_t_to_bytes(const int32_t value, uint8_t* bytes) {
00075     bytes[0] = value >> 24;
00076     bytes[1] = value >> 16;
00077     bytes[2] = value >> 8;
00078     bytes[3] = value;
00079 }
00080 
00081 void sensirion_common_int16_t_to_bytes(const int16_t value, uint8_t* bytes) {
00082     bytes[0] = value >> 8;
00083     bytes[1] = value;
00084 }
00085 
00086 void sensirion_common_float_to_bytes(const float value, uint8_t* bytes) {
00087     union {
00088         uint32_t u32_value;
00089         float float32;
00090     } tmp;
00091     tmp.float32 = value;
00092     sensirion_common_uint32_t_to_bytes(tmp.u32_value, bytes);
00093 }
00094 
00095 void sensirion_common_copy_bytes(const uint8_t* source, uint8_t* destination,
00096                                  uint16_t data_length) {
00097     uint16_t i;
00098     for (i = 0; i < data_length; i++) {
00099         destination[i] = source[i];
00100     }
00101 }