-
Notifications
You must be signed in to change notification settings - Fork 3
/
I2Cx.pas
380 lines (355 loc) · 10.1 KB
/
I2Cx.pas
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
unit I2Cx;
{Support routines for BL233B I2C interface chip
http://www.i2cchip.com
}
interface
function IAscii2Hex(S:string; CompactAscii:boolean):string;
function ConvertDelimitedHexStr2Dec(const S:string; BigEndian:boolean=true; Signed:boolean=false;FormatStr:string=''):string;
procedure ConvertDelimitedHexFile(InputFname,OutputFname:string; BigEndian:boolean=true; Signed:boolean=false;FormatStr:string='');
(* type tHexData = class
private
CurrentString:string; //string that is being processed
CurrentStringPos:integer; //pointer to current char pos
protected
public
property FormatString:string;
property BigEndian:boolean;
property Signed:boolean;
property HexString:string;
function IncrementalConvertString(const HexString:String):string; //r
function ConvertString(const HexString:string)
function ConvertFile(InputFname,OutputFname:string)
function PullQuotedString:string;
function PullInteger:integer;
function PullFloat:double;
function PullChar:char;
end;//class
*)
implementation
uses sysutils,strutils,math;
function IAscii2Hex(S:string; CompactAscii:boolean):string;
var i,n:integer;
C:byte;
begin
i:=1;
n:=length(S);
result:='';
while (i<=N ) do begin
C:=byte(S[i]);
if ( CompactAscii and (C<=127) )
then begin
result:=result+chr(C+128); //BL233 compact ascii mode
end
else begin
result:=result+inttohex(C,2);
end;
inc(i);
end;
end;
Function ReverseEndian (V:longword; NibbleCount:integer) : longword ;
//reverse endianness of an integer
var i, ByteCount:integer;
B:byte;
//Var B0, B1, B2, B3 : Byte ;
Begin
ByteCount:=ceil(NibbleCount / 2);
result:=0;
for i:=1 to ByteCount do begin
B:=(V and $000000FF);
V:=V shr 8;
result:=result shl 8;
result:=result or B;
end; //for
End ;
// Reading routines
function HexN2Int(S:string;N:integer;BigEndian:boolean):longword;
begin
end;
// Convert a delimited string to delimited decimal
// Quoted strings are passed through with quotes
// numbers preceded by "d" (lower case) can be treated as bcd/decimal, and passed through without the "d"
// numbers containing "." can be treated as decimal floats
//4AsFloat
//8AsFloat
//SignedInts
//BigEndian
const QuoteChar:char = '"';
const Delims:string = ', ';
function IsDelimiter(C:char):boolean;
var i:integer;
begin
result:=false;
for i:=1 to length(Delims) do begin
if C=Delims[i] then begin
result:=true;
break;
end;
end; //for
end; //fn
function SignAdjust(V:cardinal;NibbleCount:integer):longword;
//takes a number N nibbles long. Tests sign bit, and if set, fills the other bits
//uses lookups for test and or
const SignBitArray: array[1..8] of cardinal =
($00000008,
$00000080,
$00000800,
$00008000,
$00080000,
$00800000,
$08000000,
$80000000);
const SignExtendArray: array[1..8] of cardinal =
($FFFFFFF0,
$FFFFFF00,
$FFFFF000,
$FFFF0000,
$FFF00000,
$FF000000,
$F0000000,
$00000000);
begin
if (V and SignBitArray[NibbleCount])<>0 //sign bit for this length is set
then result:=V or SignExtendArray[NibbleCount]
else result:=V;
end;
function HexChar2Nibble(C:char):byte;
//must be uppercase hex chars only
var V:byte;
begin
if byte(C)<= byte('9')
then V:=byte(C)-byte('0')
else V:=byte(C)-byte('A')+10;
result:=V;
//result:=7;
end;//fn
function Hex2Dec(HexS:string; BigEndian, Signed:boolean):longword;
//
var Ptr,PtrStep,ExitPtr:integer;
NibbleCount:integer;
procedure InitPtr;
//setup pointer for either forward or reverse loading
//this idea didn't work, as endianness is bytewise not nibblewise
begin
{if not BigEndian
then begin
Ptr:=length(HexS);
PtrStep:=-1;
ExitPtr:=0;
end
else begin }
Ptr:=1;
PtrStep:=1;
ExitPtr:=length(HexS)+1;
// end; //if
end; //fn
var V:longword;
begin //hex2dec
InitPtr;
V:=0;
NibbleCount:=0;
while Ptr<>ExitPtr do begin
V:=V shl 4; //make space for the nibble
V:=V or HexChar2Nibble(HexS[Ptr]);
Ptr:=Ptr+PtrStep; //move the pointer
inc(NibbleCount);
end; // while
//so now we have an unsigned integer
if not BigEndian then V:=ReverseEndian(V,NibbleCount);
if Signed then V:=SignAdjust(V,NibbleCount);
result:=V;
end; //fn hex2dec
function Hex2BinStr(S:string):string;
var i:integer; B:string; V:byte;
begin
result:='';
for i:=1 to length(S) do begin
B:='0000';
V:=HexChar2Nibble(S[i]);
if (V and 8)>0 then B[1]:='1';
if (V and 4)>0 then B[2]:='1';
if (V and 2)>0 then B[3]:='1';
if (V and 1)>0 then B[4]:='1';
result:=result+B;
end; //for
end; //fn
function Hex2DecStr(S:string;BigEndian,Signed:boolean):string;
var V:longword;
begin
V:=Hex2Dec(S,BigEndian,Signed);
if Signed
then result:=inttostr(longint(V))
else result:=inttostr(V);
end; //fn
type PSingle=^Single;
function HexFloat2Str(S:string;BigEndian:boolean):string;
var V:longword;
X:Single; PV,PX:pointer;
begin
V:= Hex2Dec(S,BigEndian,false);
X:=PSingle(@V)^;
case length(S) of
8: result:=Floattostr(X);
else //invalid floating points
result:=S;
end;
end;
function Hex2AsciiStr(S:string):string;
var V:byte;
i:integer;
begin
i:=1;
result:='';
while i<=length(S) do begin
V:=HexChar2Nibble(S[i]);
inc(i);
if i<=length(S) then begin //there is another hex char to be had
V:=V shl 4;
V:=V or HexChar2Nibble(S[i]);
inc(i);
end;//if
result:=result+char(V);
end;//for
end;
//type TGetCharFunction = function(var C:char):boolean;
function ConvertDelimitedHexStr2Dec(const S:string; BigEndian:boolean; Signed:boolean;FormatStr:string):string;
//BigEndian and Signed set the default. Use of the explicit chars overides them
//CommaDelim, TabDelim,
//AllHex
function ProcessSubString(S:string; IsAllHex,BigEndian,Signed:boolean;Float:boolean=false):string;
procedure TrimLeadingCharX(var S:string; IsBigEndian, IsSigned:boolean; IsFloat:boolean=false);
var STrimmed:string; i:integer;
begin
BigEndian:=IsBigEndian;
Signed:=IsSigned;
Float:=IsFloat;
STrimmed:=RightStr(S,Length(S)-1);
IsAllHex:=true;
IsFloat:=false;
for i:=1 to length(STrimmed) do begin
case Strimmed[i] of
'0'..'9','A'..'F' : ;
else begin
IsAllHex:=false;
end; //else
end;//case
end; //for
if IsAllHex then S:=STrimmed;
end; //fn
var LengthS:integer;
C1:char;
V:longword;
//Signed: boolean;
begin
LengthS:=length(S);
if LengthS=0 then begin
result:='';
exit;
end;
C1:=S[1];
{if (C1='d') and (LengthS>=2) then begin //explicit decimals
result:=RightStr(S,LengthS-1); //prune off the leading D
exit;
end; }
if LengthS>=2 then begin
case C1 of
'd': begin
result:=RightStr(S,LengthS-1); //prune off the leading D
exit;
end;
'b': begin TrimLeadingCharX(S,true,false,true);
result:=Hex2BinStr(S);
exit;
end;
'a': begin ;
result:=Hex2AsciiStr(RightStr(S,LengthS-1));
exit;
end;
'f': TrimLeadingCharX(S,true,false,true); //big endian floating point
'g': TrimLeadingCharX(S,false,false,true); //little endian floating point
's': TrimLeadingCharX(S,true,true); //s signed bigendian
't': TrimLeadingCharX(S,false,true); //t signed littleendian
'u': TrimLeadingCharX(S,true,false); //u unsigned bigendian
'v': TrimLeadingCharX(S,false,false); //v unsigned littleendian
end; //case
end; //if
if not IsAllHex then begin //quoted strings, floats, random errors
result:=S;
exit;
end;
//must be a hex string to process....
//Signed:=true;
if Float
then result:=HexFloat2Str(S,BigEndian)
else result:=Hex2DecStr(S,BigEndian,Signed);
end; //fn
var i, iSubString:integer;
InQuotes:boolean;
var IsAllHex:boolean;
c:char;
SS:string; //substring
iFormat:integer; //
begin
InQuotes:=false;
iSubString:=1;
iFormat:=1;
IsAllHex:=true;
SS:='';
result:='';
for i:=1 to length(S) do begin
c:=S[i];
//while GetNextChar(c) do begin
if c=QuoteChar then begin
InQuotes:=not InQuotes;
end;
if InQuotes
then begin
SS:=SS+c; //add to substring, but
end
else begin
if IsDelimiter(c) then begin
//use the format string for Allhex strings, if within format string
if IsAllHex and (length(FormatStr)>=1) and (iFormat<=length(FormatStr)) then begin
SS:=FormatStr[iFormat]+SS;
inc(iFormat);
end; //if
result:=result+ProcessSubString(SS,IsAllHex,BigEndian,Signed)+c;
IsAllHex:=true;
SS:='';
inc(iSubstring);
end
else begin
SS:=SS+c;
case C of
'0'..'9','A'..'F' : ;
else begin
IsAllHex:=false;
end; //else
end;//case
end;//if
end;
end; //for each char
result:=result+ProcessSubString(SS,IsAllHex,BigEndian,Signed);
end; //fn
procedure ConvertDelimitedHexFile(InputFname,OutputFname:string;BigEndian:boolean; Signed:boolean;FormatStr:string);
var Input,Output:text;
S,Sout:string;
LineCount:integer;
begin
LineCount:=0;
try
AssignFile(Input, InputFname);
Reset(Input);
AssignFile(Output, OutputFname);
Rewrite(Output);
while not EOF(Input) do begin
ReadLn(Input,S);
Sout:=ConvertDelimitedHexStr2Dec(S,BigEndian,Signed,FormatStr);
WriteLn(Output,Sout);
inc(LineCount);
end; //while
finally
CloseFile(Input);
CloseFile(Output);
end; //try
end;
end.