Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
utilities.cpp
00001 /******************************************************************************* 00002 ################################################################################ 00003 # (C) STMicroelectronics 2014 00004 # 00005 # This program is free software; you can redistribute it and/or modify it under 00006 # the terms of the GNU General Public License version 2 and only version 2 as 00007 # published by the Free Software Foundation. 00008 # 00009 # This program is distributed in the hope that it will be useful, but WITHOUT 00010 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00012 # details. 00013 # 00014 # You should have received a copy of the GNU General Public License along with 00015 # this program; if not, write to the Free Software Foundation, Inc., 00016 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 # 00018 #------------------------------------------------------------------------------ 00019 # Imaging Division 00020 ################################################################################ 00021 ********************************************************************************/ 00022 00023 /** 00024 * @file utilities.cpp 00025 * 00026 * Copyright (C) 2014 ST MicroElectronics 00027 * 00028 * Utilities module definition. 00029 * 00030 */ 00031 00032 #include "utilities.h" 00033 #include "platform.h" 00034 #ifndef __KERNEL__ 00035 #include <cstring> 00036 #include <cstdlib> 00037 #include <stdio.h> 00038 #else 00039 #include <linux/kernel.h> 00040 #include <linux/string.h> 00041 #endif 00042 #ifndef __KERNEL__ 00043 int32_t roundFloatToInt(float_t floatVal) 00044 { 00045 floatVal += 0.5f; 00046 return (int32_t)floatVal; 00047 } 00048 #endif 00049 int32_t unPackBytes(int32_t data, uint8_t *byteArray, int32_t size) 00050 { 00051 const int32_t cByteBitShift = 8; 00052 const int32_t cByteMask = 0xFF; 00053 int32_t bitShift = cByteBitShift * (size - 1); 00054 int32_t byteIndex = 0; 00055 00056 for (byteIndex = 0; byteIndex < size; byteIndex++) 00057 { 00058 int32_t mask = cByteMask << bitShift; 00059 int32_t value = (data & mask) >> bitShift; 00060 bitShift -= cByteBitShift; 00061 byteArray[byteIndex] = (uint8_t)(value); 00062 } 00063 return 0; 00064 } 00065 00066 uint32_t packBytes(uint8_t *byteArray, uint32_t size) 00067 { 00068 const int cBitsPerByte = 8; 00069 uint32_t cBytesPerInt = 4; 00070 uint32_t data = 0; 00071 unsigned int index = 0; 00072 00073 if(size > cBytesPerInt) 00074 { 00075 size = cBytesPerInt; 00076 } 00077 00078 for(index = 0; index < size; index++) 00079 { 00080 if(index > 0) 00081 { 00082 data <<= cBitsPerByte; 00083 } 00084 data += byteArray[index]; 00085 } 00086 return data; 00087 } 00088 00089 int32_t array2HexCString(uint8_t *byteArray, char *str, uint32_t count) 00090 { 00091 uint32_t i; 00092 00093 sprintf(str, "0x"); 00094 00095 // int32_t length = count; 00096 // for(int32_t i = 0; i < count; i++) 00097 for(i = 0; i < count; i++) 00098 { 00099 str += 2; 00100 sprintf(str, "%02X", (uint32_t)byteArray[i]); 00101 } 00102 return 0; 00103 } 00104 00105 #ifndef __KERNEL__ 00106 uint32_t encodeTo9_7_Format(float_t value) 00107 { 00108 const float_t cShiftFactor = 128.0f; 00109 const uint32_t cMax = 65535; 00110 uint32_t encodedVal = (uint32_t)roundFloatToInt(value * cShiftFactor); 00111 if (encodedVal > cMax) 00112 { 00113 encodedVal = cMax; 00114 } 00115 return encodedVal; 00116 } 00117 00118 float_t decodeFrom9_7_Format(uint32_t value) 00119 { 00120 const float_t cShiftFactor = 128.0f; 00121 return (float_t)value / cShiftFactor; 00122 } 00123 00124 00125 uint32_t encodeTo4_4_Format(float_t value) 00126 { 00127 const float_t cShiftFactor = 16.0f; 00128 const uint32_t cMax = 255; 00129 uint32_t encodedVal = (uint32_t)roundFloatToInt(value * cShiftFactor); 00130 if (encodedVal > cMax) 00131 { 00132 encodedVal = cMax; 00133 } 00134 return encodedVal; 00135 } 00136 00137 float_t decodeFrom4_4_Format(uint32_t value) 00138 { 00139 const float_t cShiftFactor = 16.0f; 00140 return (float_t)value / cShiftFactor; 00141 } 00142 #endif 00143
Generated on Tue Jul 12 2022 22:19:50 by
1.7.2