vlx lib
utilities.cpp@0:bc9f26b5dadf, 2015-02-08 (annotated)
- Committer:
- vijaynvr
- Date:
- Sun Feb 08 14:26:51 2015 +0000
- Revision:
- 0:bc9f26b5dadf
working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vijaynvr | 0:bc9f26b5dadf | 1 | /******************************************************************************* |
vijaynvr | 0:bc9f26b5dadf | 2 | ################################################################################ |
vijaynvr | 0:bc9f26b5dadf | 3 | # (C) STMicroelectronics 2014 |
vijaynvr | 0:bc9f26b5dadf | 4 | # |
vijaynvr | 0:bc9f26b5dadf | 5 | # This program is free software; you can redistribute it and/or modify it under |
vijaynvr | 0:bc9f26b5dadf | 6 | # the terms of the GNU General Public License version 2 and only version 2 as |
vijaynvr | 0:bc9f26b5dadf | 7 | # published by the Free Software Foundation. |
vijaynvr | 0:bc9f26b5dadf | 8 | # |
vijaynvr | 0:bc9f26b5dadf | 9 | # This program is distributed in the hope that it will be useful, but WITHOUT |
vijaynvr | 0:bc9f26b5dadf | 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
vijaynvr | 0:bc9f26b5dadf | 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
vijaynvr | 0:bc9f26b5dadf | 12 | # details. |
vijaynvr | 0:bc9f26b5dadf | 13 | # |
vijaynvr | 0:bc9f26b5dadf | 14 | # You should have received a copy of the GNU General Public License along with |
vijaynvr | 0:bc9f26b5dadf | 15 | # this program; if not, write to the Free Software Foundation, Inc., |
vijaynvr | 0:bc9f26b5dadf | 16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
vijaynvr | 0:bc9f26b5dadf | 17 | # |
vijaynvr | 0:bc9f26b5dadf | 18 | #------------------------------------------------------------------------------ |
vijaynvr | 0:bc9f26b5dadf | 19 | # Imaging Division |
vijaynvr | 0:bc9f26b5dadf | 20 | ################################################################################ |
vijaynvr | 0:bc9f26b5dadf | 21 | ********************************************************************************/ |
vijaynvr | 0:bc9f26b5dadf | 22 | |
vijaynvr | 0:bc9f26b5dadf | 23 | /** |
vijaynvr | 0:bc9f26b5dadf | 24 | * @file utilities.cpp |
vijaynvr | 0:bc9f26b5dadf | 25 | * |
vijaynvr | 0:bc9f26b5dadf | 26 | * Copyright (C) 2014 ST MicroElectronics |
vijaynvr | 0:bc9f26b5dadf | 27 | * |
vijaynvr | 0:bc9f26b5dadf | 28 | * Utilities module definition. |
vijaynvr | 0:bc9f26b5dadf | 29 | * |
vijaynvr | 0:bc9f26b5dadf | 30 | */ |
vijaynvr | 0:bc9f26b5dadf | 31 | |
vijaynvr | 0:bc9f26b5dadf | 32 | #include "utilities.h" |
vijaynvr | 0:bc9f26b5dadf | 33 | #include "platform.h" |
vijaynvr | 0:bc9f26b5dadf | 34 | #ifndef __KERNEL__ |
vijaynvr | 0:bc9f26b5dadf | 35 | #include <cstring> |
vijaynvr | 0:bc9f26b5dadf | 36 | #include <cstdlib> |
vijaynvr | 0:bc9f26b5dadf | 37 | #include <stdio.h> |
vijaynvr | 0:bc9f26b5dadf | 38 | #else |
vijaynvr | 0:bc9f26b5dadf | 39 | #include <linux/kernel.h> |
vijaynvr | 0:bc9f26b5dadf | 40 | #include <linux/string.h> |
vijaynvr | 0:bc9f26b5dadf | 41 | #endif |
vijaynvr | 0:bc9f26b5dadf | 42 | #ifndef __KERNEL__ |
vijaynvr | 0:bc9f26b5dadf | 43 | int32_t roundFloatToInt(float_t floatVal) |
vijaynvr | 0:bc9f26b5dadf | 44 | { |
vijaynvr | 0:bc9f26b5dadf | 45 | floatVal += 0.5f; |
vijaynvr | 0:bc9f26b5dadf | 46 | return (int32_t)floatVal; |
vijaynvr | 0:bc9f26b5dadf | 47 | } |
vijaynvr | 0:bc9f26b5dadf | 48 | #endif |
vijaynvr | 0:bc9f26b5dadf | 49 | int32_t unPackBytes(int32_t data, uint8_t *byteArray, int32_t size) |
vijaynvr | 0:bc9f26b5dadf | 50 | { |
vijaynvr | 0:bc9f26b5dadf | 51 | const int32_t cByteBitShift = 8; |
vijaynvr | 0:bc9f26b5dadf | 52 | const int32_t cByteMask = 0xFF; |
vijaynvr | 0:bc9f26b5dadf | 53 | int32_t bitShift = cByteBitShift * (size - 1); |
vijaynvr | 0:bc9f26b5dadf | 54 | int32_t byteIndex = 0; |
vijaynvr | 0:bc9f26b5dadf | 55 | |
vijaynvr | 0:bc9f26b5dadf | 56 | for (byteIndex = 0; byteIndex < size; byteIndex++) |
vijaynvr | 0:bc9f26b5dadf | 57 | { |
vijaynvr | 0:bc9f26b5dadf | 58 | int32_t mask = cByteMask << bitShift; |
vijaynvr | 0:bc9f26b5dadf | 59 | int32_t value = (data & mask) >> bitShift; |
vijaynvr | 0:bc9f26b5dadf | 60 | bitShift -= cByteBitShift; |
vijaynvr | 0:bc9f26b5dadf | 61 | byteArray[byteIndex] = (uint8_t)(value); |
vijaynvr | 0:bc9f26b5dadf | 62 | } |
vijaynvr | 0:bc9f26b5dadf | 63 | return 0; |
vijaynvr | 0:bc9f26b5dadf | 64 | } |
vijaynvr | 0:bc9f26b5dadf | 65 | |
vijaynvr | 0:bc9f26b5dadf | 66 | uint32_t packBytes(uint8_t *byteArray, uint32_t size) |
vijaynvr | 0:bc9f26b5dadf | 67 | { |
vijaynvr | 0:bc9f26b5dadf | 68 | const int cBitsPerByte = 8; |
vijaynvr | 0:bc9f26b5dadf | 69 | uint32_t cBytesPerInt = 4; |
vijaynvr | 0:bc9f26b5dadf | 70 | uint32_t data = 0; |
vijaynvr | 0:bc9f26b5dadf | 71 | unsigned int index = 0; |
vijaynvr | 0:bc9f26b5dadf | 72 | |
vijaynvr | 0:bc9f26b5dadf | 73 | if(size > cBytesPerInt) |
vijaynvr | 0:bc9f26b5dadf | 74 | { |
vijaynvr | 0:bc9f26b5dadf | 75 | size = cBytesPerInt; |
vijaynvr | 0:bc9f26b5dadf | 76 | } |
vijaynvr | 0:bc9f26b5dadf | 77 | |
vijaynvr | 0:bc9f26b5dadf | 78 | for(index = 0; index < size; index++) |
vijaynvr | 0:bc9f26b5dadf | 79 | { |
vijaynvr | 0:bc9f26b5dadf | 80 | if(index > 0) |
vijaynvr | 0:bc9f26b5dadf | 81 | { |
vijaynvr | 0:bc9f26b5dadf | 82 | data <<= cBitsPerByte; |
vijaynvr | 0:bc9f26b5dadf | 83 | } |
vijaynvr | 0:bc9f26b5dadf | 84 | data += byteArray[index]; |
vijaynvr | 0:bc9f26b5dadf | 85 | } |
vijaynvr | 0:bc9f26b5dadf | 86 | return data; |
vijaynvr | 0:bc9f26b5dadf | 87 | } |
vijaynvr | 0:bc9f26b5dadf | 88 | |
vijaynvr | 0:bc9f26b5dadf | 89 | int32_t array2HexCString(uint8_t *byteArray, char *str, uint32_t count) |
vijaynvr | 0:bc9f26b5dadf | 90 | { |
vijaynvr | 0:bc9f26b5dadf | 91 | uint32_t i; |
vijaynvr | 0:bc9f26b5dadf | 92 | |
vijaynvr | 0:bc9f26b5dadf | 93 | sprintf(str, "0x"); |
vijaynvr | 0:bc9f26b5dadf | 94 | |
vijaynvr | 0:bc9f26b5dadf | 95 | // int32_t length = count; |
vijaynvr | 0:bc9f26b5dadf | 96 | // for(int32_t i = 0; i < count; i++) |
vijaynvr | 0:bc9f26b5dadf | 97 | for(i = 0; i < count; i++) |
vijaynvr | 0:bc9f26b5dadf | 98 | { |
vijaynvr | 0:bc9f26b5dadf | 99 | str += 2; |
vijaynvr | 0:bc9f26b5dadf | 100 | sprintf(str, "%02X", (uint32_t)byteArray[i]); |
vijaynvr | 0:bc9f26b5dadf | 101 | } |
vijaynvr | 0:bc9f26b5dadf | 102 | return 0; |
vijaynvr | 0:bc9f26b5dadf | 103 | } |
vijaynvr | 0:bc9f26b5dadf | 104 | |
vijaynvr | 0:bc9f26b5dadf | 105 | #ifndef __KERNEL__ |
vijaynvr | 0:bc9f26b5dadf | 106 | uint32_t encodeTo9_7_Format(float_t value) |
vijaynvr | 0:bc9f26b5dadf | 107 | { |
vijaynvr | 0:bc9f26b5dadf | 108 | const float_t cShiftFactor = 128.0f; |
vijaynvr | 0:bc9f26b5dadf | 109 | const uint32_t cMax = 65535; |
vijaynvr | 0:bc9f26b5dadf | 110 | uint32_t encodedVal = (uint32_t)roundFloatToInt(value * cShiftFactor); |
vijaynvr | 0:bc9f26b5dadf | 111 | if (encodedVal > cMax) |
vijaynvr | 0:bc9f26b5dadf | 112 | { |
vijaynvr | 0:bc9f26b5dadf | 113 | encodedVal = cMax; |
vijaynvr | 0:bc9f26b5dadf | 114 | } |
vijaynvr | 0:bc9f26b5dadf | 115 | return encodedVal; |
vijaynvr | 0:bc9f26b5dadf | 116 | } |
vijaynvr | 0:bc9f26b5dadf | 117 | |
vijaynvr | 0:bc9f26b5dadf | 118 | float_t decodeFrom9_7_Format(uint32_t value) |
vijaynvr | 0:bc9f26b5dadf | 119 | { |
vijaynvr | 0:bc9f26b5dadf | 120 | const float_t cShiftFactor = 128.0f; |
vijaynvr | 0:bc9f26b5dadf | 121 | return (float_t)value / cShiftFactor; |
vijaynvr | 0:bc9f26b5dadf | 122 | } |
vijaynvr | 0:bc9f26b5dadf | 123 | |
vijaynvr | 0:bc9f26b5dadf | 124 | |
vijaynvr | 0:bc9f26b5dadf | 125 | uint32_t encodeTo4_4_Format(float_t value) |
vijaynvr | 0:bc9f26b5dadf | 126 | { |
vijaynvr | 0:bc9f26b5dadf | 127 | const float_t cShiftFactor = 16.0f; |
vijaynvr | 0:bc9f26b5dadf | 128 | const uint32_t cMax = 255; |
vijaynvr | 0:bc9f26b5dadf | 129 | uint32_t encodedVal = (uint32_t)roundFloatToInt(value * cShiftFactor); |
vijaynvr | 0:bc9f26b5dadf | 130 | if (encodedVal > cMax) |
vijaynvr | 0:bc9f26b5dadf | 131 | { |
vijaynvr | 0:bc9f26b5dadf | 132 | encodedVal = cMax; |
vijaynvr | 0:bc9f26b5dadf | 133 | } |
vijaynvr | 0:bc9f26b5dadf | 134 | return encodedVal; |
vijaynvr | 0:bc9f26b5dadf | 135 | } |
vijaynvr | 0:bc9f26b5dadf | 136 | |
vijaynvr | 0:bc9f26b5dadf | 137 | float_t decodeFrom4_4_Format(uint32_t value) |
vijaynvr | 0:bc9f26b5dadf | 138 | { |
vijaynvr | 0:bc9f26b5dadf | 139 | const float_t cShiftFactor = 16.0f; |
vijaynvr | 0:bc9f26b5dadf | 140 | return (float_t)value / cShiftFactor; |
vijaynvr | 0:bc9f26b5dadf | 141 | } |
vijaynvr | 0:bc9f26b5dadf | 142 | #endif |
vijaynvr | 0:bc9f26b5dadf | 143 |