AWS IoT for ESP32 v1.0.0
An ESP-IDF based solution
stdutils.h
1
2/**********************************************************************************************
3 ExploreEmbedded
4****************************************************************************************************
5* File: stdutils.h
6* Version: 15.1
7* Author: ExploreEmbedded
8* Website: http://www.exploreembedded.com/wiki
9* Description: Contains standard util macros, typedefs and constants
10
11The libraries have been tested on ExploreEmbedded development boards. We strongly believe that the
12library works on any of development boards for respective controllers. However, ExploreEmbedded
13disclaims any kind of hardware failure resulting out of usage of libraries, directly or indirectly.
14Files may be subject to change without prior notice. The revision history contains the information
15related to updates.
16
17
18GNU GENERAL PUBLIC LICENSE:
19 This program is free software: you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation, either version 3 of the License, or
22 (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program. If not, see <http://www.gnu.org/licenses/>.
31
32Errors and omissions should be reported to codelibraries@exploreembedded.com
33********************************************************************************************/
34
35/*********************************************************************************************
36 Revision History
37**********************************************************************************************
3815.0: Initial version
3915.1: Changed the prefix of bit masks from M_ to MASK_
40 Added the section others macros
41 Defined the constants for Number system
42*********************************************************************************************/
43
44#ifndef _STD_UTIL_H_
45#define _STD_UTIL_H_
46#include "esp_types.h"
47
48/**********************************************************************************************
49 LOGIC level
50*********************************************************************************************/
51#define LOW 0x00u
52#define HIGH 0x01u
53/***************************************************************************************/
54
55/**********************************************************************************************
56 Commonly used constants
57********************************************************************************************/
58
59#ifndef NULL
60#define NULL 0u
61#endif
62
63#ifndef TRUE
64#define TRUE 1u
65#endif
66
67#ifndef true
68#define true 1u
69#endif
70
71#ifndef FALSE
72#define FALSE 0u
73#endif
74
75#ifndef false
76#define false 0u
77#endif
78
79/***************************************************************************************/
80
81/**********************************************************************************************
82 Standard Enumerations and Constants
83*********************************************************************************************/
84
85typedef enum
86{
87 STS_SUCCESS,
88 STS_BUSY,
89 STS_FAILED,
90 STS_TIMEOUT
91} status_et;
92
93/***************************************************************************************/
94
95/**********************************************************************************************
96 Macros for Bit Manipulation
97**********************************************************************************************/
98#define util_GetBitMask(bit) (1 << (bit))
99#define util_BitSet(x, bit) ((x) |= util_GetBitMask(bit))
100#define util_BitClear(x, bit) ((x) &= ~util_GetBitMask(bit))
101#define util_BitToggle(x, bit) ((x) ^= util_GetBitMask(bit))
102#define util_UpdateBit(x, bit, val) ((val) ? util_BitSet(x, bit) : util_BitClear(x, bit))
103
104#define util_GetBitStatus(x, bit) (((x) & (util_GetBitMask(bit))) != 0u)
105#define util_IsBitSet(x, bit) (((x) & (util_GetBitMask(bit))) != 0u)
106#define util_IsBitCleared(x, bit) (((x) & (util_GetBitMask(bit))) == 0u)
107
108#define util_AreAllBitsSet(x, BitMask) (((x) & (BitMask)) == BitMask)
109#define util_AreAnyBitsSet(x, BitMask) (((x) & (BitMask)) != 0x00u)
110/***************************************************************************************/
111
112/**********************************************************************************************
113 Macros to find the mod of a number
114*********************************************************************************************/
115#define util_GetMod8(dividend, divisor) (uint8_t)(dividend - (divisor * (uint8_t)(dividend / divisor)))
116#define util_GetMod16(dividend, divisor) (uint16_t)(dividend - (divisor * (uint16_t)(dividend / divisor)))
117#define util_GetMod32(dividend, divisor) (uint32_t)(dividend - (divisor * (uint32_t)(dividend / divisor)))
118/****************************************************************************************/
119
120/**********************************************************************************************
121 Macros for Dec2Ascii,Hec2Ascii and Acsii2Hex conversion
122**********************************************************************************************/
123#define util_Dec2Ascii(Dec) ((Dec) + 0x30)
124#define util_Ascii2Dec(Asc) ((Asc)-0x30)
125#define util_Hex2Ascii(Hex) (((Hex) > 0x09) ? ((Hex) + 0x37) : ((Hex) + 0x30))
126#define util_Ascii2Hex(Asc) (((Asc) > 0x39) ? ((Asc)-0x37) : ((Asc)-0x30))
127#define util_IsUpperCase(Asc) (((Asc) >= 'A') && ((Asc) <= 'Z'))
128#define util_IsLowerCase(Asc) (((Asc) >= 'a') && ((Asc) <= 'z'))
129#define util_toLower(Asc) (util_IsUpperCase(Asc) ? ((Asc) + 0x20) : (Asc))
130#define util_toUpper(Asc) (util_IsLowerCase(Asc) ? ((Asc)-0x20) : (Asc))
131/***************************************************************************************************/
132
133/**********************************************************************************************
134 Macros to extract the Nibbles
135*********************************************************************************************/
136#define util_ExtractBits0to4(x) (uint8_t)((x)&0x0Fu)
137#define util_ExtractBits4to8(x) (uint8_t)(((x) >> 4) & 0x0Fu)
138#define util_ExtractBits8to12(x) (uint8_t)(((x) >> 8) & 0x0Fu)
139#define util_ExtractBits12to16(x) (uint8_t)(((x) >> 12) & 0x0Fu)
140/***************************************************************************************/
141
142/**********************************************************************************************
143 Macros to extract the Byte
144*********************************************************************************************/
145#define util_ExtractBits0to8(x) (uint8_t)((x)&0xFFu)
146#define util_ExtractBits8to16(x) (uint8_t)(((x) >> 8) & 0xFFu)
147#define util_ExtractBits16to24(x) (uint8_t)(((x) >> 16) & 0xFFu)
148#define util_ExtractBits24to32(x) (uint8_t)(((x) >> 24) & 0xFFu)
149/***************************************************************************************/
150
151/**********************************************************************************************
152 Other Macros
153*********************************************************************************************/
154#define util_GetMax(num1, num2) (((num1) > (num2)) ? (num1) : (num2))
155#define util_GetMin(num1, num2) (((num1) < (num2)) ? (num1) : (num2))
156
157#define util_swap(x, y) (x ^= y ^= x ^= y)
158#define util_GetAbsolute(x) (((x) < 0) ? -(x) : (x))
159#define util_IsAsciiInt(Asc) (((Asc) > 0x29) && ((Asc) < 0x40))
160#define util_IsAsciiHex(Asc) ((util_IsAsciiInt(Asc)) || \
161 (((Asc) >= 'A') && ((Asc) <= 'F')) || \
162 (((Asc) >= 'a') && ((Asc) <= 'f')))
163
164#define util_ShiftToRight(x, c) ((x) >> c)
165#define util_ShiftToLeft(x, c) ((x) << c)
166#define util_ShiftNibbleToRight(x) ((x) >> 4)
167#define util_ShiftNibbleToLeft(x) ((x) << 4)
168#define util_ShiftBytesToRight(x, c) ((x) >> (8 * (c)))
169#define util_ShiftBytesToLeft(x, c) ((x) << (8 * (c)))
170
171#define util_SecToMs(x) ((x)*1000)
172
173#define TIMEOUT_US(us) (us)
174#define TIMEOUT_MS(ms) (ms)
175#define TIMEOUT_SEC(sec) (sec)
176#define TIMEOUT_MIN(min) (min)
177
178#define util_generateEnum(ENUM) ENUM,
179#define util_generateString(STRING) #STRING,
180/***************************************************************************************/
181
182uint32_t util_getNumFromString(const char *pStr);
183bool util_isValidString(const char *pStr);
184
185#endif