-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlsutils.c
227 lines (203 loc) · 6.69 KB
/
lsutils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//*****************************************************************************
//
// Application Name - LightServer
// File Name - lsutils.c
// Application Version - 2.5.0
// Application Modify Date - 26th of December 2014
// Application Developer - Glenn Vassallo
// Application Contact - [email protected]
// Application Repository - https://github.com/remixed123/LightServer
//
// Application Overview - LightServer is a Wifi enabled embedded device which
// controls RGB Intelligent lighting in various ways.
// You can control the lights via iOS Apps or via a
// webpages that are on the device.This example project
// provides a starting
//
// File Overview - lsutils.c contains utility functions to manipulate
// text and numbers and bits
//
// Application Details - https://github.com/remixed123/LightServer/readme.txt
//
//*****************************************************************************
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "lsutils.h"
//*****************************************************************************
// Lookup Tables used by Utilises
//*****************************************************************************
static const long hextable[] = {
[0 ... 255] = -1, // bit aligned access into this table is considerably
['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // faster for most modern processors,
['A'] = 10, 11, 12, 13, 14, 15, // for the space conscious, reduce to
['a'] = 10, 11, 12, 13, 14, 15 // signed char.
};
uint8_t lookup[16] = {
0x0, 0x8, 0x4, 0xC,
0x2, 0xA, 0x6, 0xE,
0x1, 0x9, 0x5, 0xD,
0x3, 0xB, 0x7, 0xF };
//*****************************************************************************
//! randRange
//!
//! Generates a random between a range
//!
//! returns: integer
//*****************************************************************************
int randRange(int min_n, int max_n)
{
return rand() % (max_n - min_n + 1) + min_n;
}
//*****************************************************************************
//! flip
//!
//! reverses the bits
//!
//! returns: reversed bits
//*****************************************************************************
uint8_t flip( uint8_t n )
{
//This should be just as fast and it is easier to understand.
//return (lookup[n%16] << 4) | lookup[n/16];
return (lookup[n&0x0F] << 4) | lookup[n>>4];
}
//*****************************************************************************
//! arrayPosition
//!
//! Returns the position in an array .....
//!
//!
//*****************************************************************************
int arrayPosition(int arrayCounter)
{
int arrayPos = abs(arrayCounter/8);
return arrayPos;
}
//*****************************************************************************
//! bitPosition
//!
//! Returns the bit position .....
//!
//!
//*****************************************************************************
int bitPosition(int shiftCounter)
{
int bitPos = 8-(shiftCounter++%8);
if (bitPos == 8)
{
return 0; // This is for the last bit in the byte, position 0
}
else
{
return bitPos;
}
}
//*****************************************************************************
//! hex2int
//!
//! @brief Convert a hexidecimal string to a signed long. will not produce or process
//! negative numbers except to signal error.
//!
//! @param hex without decoration, case insensative.
//!
//! @return -1 on error, or result (max sizeof(long)-1 bits)
//*****************************************************************************
unsigned int hex2int(unsigned const char *hex)
{
long ret = 0;
while (*hex && ret >= 0) {
ret = (ret << 4) | hextable[*hex++];
}
return ret;
}
//unsigned int hex2intMultiByte(unsigned const char * s)
//{
// unsigned int result = 0;
// int c ;
// if ('0' == *s && 'x' == *(s+1)) { s+=2;
// while (*s) {
// result = result << 4;
// if (c=(*s-'0'),(c>=0 && c <=9)) result|=c;
// else if (c=(*s-'A'),(c>=0 && c <=5)) result|=(c+10);
// else if (c=(*s-'a'),(c>=0 && c <=5)) result|=(c+10);
// else break;
// ++s;
// }
// }
// return result;
//}
//*****************************************************************************
//! hex2intMultiByte
//!
//! Converts a hex values to ....
//!
//!
//*****************************************************************************
unsigned int hex2intMultiByte(char const* hexstring) //(char const* hexstring)
{
unsigned int result = 0;
char const *c = hexstring;
char thisC;
while( (thisC = *c) != NULL )
{
unsigned int add;
//thisC = toupper(thisC);
result <<= 4;
if( thisC >= ASCII_0_VALU && thisC <= ASCII_9_VALU )
add = thisC - ASCII_0_VALU;
else if( thisC >= ASCII_A_VALU && thisC <= ASCII_F_VALU)
add = thisC - ASCII_A_VALU + 10;
else
{
//System_printf("Unrecognised hex character \"%c\"\n", thisC);
exit(-1);
}
result += add;
++c;
}
return result;
}
//*****************************************************************************
//! hexStringToBytes
//!
//! Converts a string of hexadecimal numbers to an array of bytes.s
//!
//!
//*****************************************************************************
unsigned char * hexStringToBytes(unsigned char * hexstring, int byteCount)
{
unsigned char *b;
char work[2]; // = {0x00, 0x00, 0x00};
unsigned char *bytes = NULL;
int i;
//bytes = calloc(strlen(hexstring) / 2, sizeof *bytes);
bytes = calloc(byteCount, sizeof *bytes);
b = bytes;
//for (i = 0; i < (int)strlen(hexstring); i += 2)
for (i = 0; i < (byteCount * 2); i += 2)
{
work[0] = (char)hexstring[i];
work[1] = (char)hexstring[i + 1];
*(b++) = (unsigned char)strtoul(work, (char **)NULL, 16);
}
return bytes;
}
//*****************************************************************************
//! asciiToHex
//!
//! This function converts an ASCII string into its HEX Values, represented as a string
//!
//!
//*****************************************************************************
char * asciiToHex(char * asciiString, int maxLength) {
int i = 0;
char * hex = (char *) malloc(sizeof (char) * ((maxLength*2)+1));
for (i = 0; i < maxLength; i++)
{
//sprintf((hex+(i*2)), "%%02x", (unsigned char)(*(asciiString+i)));
sprintf(hex+i*2, "%02X", asciiString[i]);
}
hex[(maxLength*2)] = '\0';
return hex;
}