-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stimuli_export.py
145 lines (115 loc) · 5.92 KB
/
Stimuli_export.py
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
#! python
# -*-coding:Latin-1 -*
import sys;
import os;
import argparse;
import struct;
import io;
def get_stringFromValue(val, args):
if args.b:
if args.e:
return ("{:#010b}".format(val))[2:];
else:
return ["{:#010b}".format(val)];
else:
if not args.x:
return ["{:>#3}".format(val)];
else:
return ["{:=#04x}".format(val)];
#sys.argv = ['sd_copy.py', 'H:', '-o test.raw'];
parser = argparse.ArgumentParser(description='Reformat a stimul file.');
parser.add_argument('input', help='the input stimuli file');
parser.add_argument('-f', action="append", metavar='field', help='the field name to export');
parser.add_argument('-m', action="append", metavar='mask', help='the bit to export', default=[]);
parser.add_argument('-i', action="append", metavar='init', type=int, help='init value of the byte', default=[]);
parser.add_argument('-e', action='store_true', help='explode the value is bits');
parser.add_argument('-x', action='store_true', help='hexadecimal output');
parser.add_argument('-b', action='store_true', help='binary output');
parser.add_argument('-s', type=int, help='start time', default=0);
args = parser.parse_args();
if len(args.f) != len(args.m):
if len(args.m) == 0:
args.m = ["0xFF"];
if len(args.m) > 1:
raise ValueError("Invalid number of mask occurence {}".format(args.m));
else:
args.m = args.m * len(args.f);
if len(args.f) != len(args.i):
if len(args.m) == 0:
args.i = [None];
if len(args.i) > 1:
raise ValueError("Invalid number of initial value occurence");
else:
args.i = args.i * len(args.f);
print("{}".format(args.f));
print("{}".format(args.m));
print("{}".format(args.i));
try:
for i in range(len(args.f)):
lField = args.f[i];
lInit = args.i[i];
lMask = args.m[i];
print("{} {} {}".format(lField, lInit, lMask));
inputFile = args.input.strip();
lIndex = inputFile.rfind('.');
if lIndex != -1 and lIndex > 0:
outputFile = inputFile[0:lIndex] + '_' + lField + inputFile[lIndex:];
else:
outputFile = inputFile + '_' + lField;
lTimeArray = list();
lValueArray = list();
lCurrentTime = None;
lCurrentValue = None;
lLastValue = None;
lIndex = lMask.find('x');
if lIndex != -1:
lMask = int(lMask[lIndex+1:], 16);
else:
lMask = int(lMask);
if lMask < 8:
lMask = pow(2, lMask);
else:
raise ValueError("Invalid bit number");
print (outputFile);
with open(inputFile, 'r') as f, open(outputFile, 'w') as fout:
for lLine in f:
lLine = lLine.strip();
if lLine.startswith('#'):
if lCurrentTime is not None:
lCurrentTime = lCurrentTime + int(lLine[1:]);
else:
lCurrentTime = 0;
if lCurrentTime is not None and (len(lField) > 0) and lLine.startswith(lField):
lIndex = lLine.find('x');
if (lIndex != -1):
lNewValue = (int(lLine[lIndex+1:], 16) & lMask);
lBoolValue = int(lNewValue > 0);
if lLastValue is None:
if lInit is not None and lCurrentValue is None:
lCurrentValue = lInit;
if args.s is None:
if lCurrentTime > 0:
print("{:>#06}".format(0) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lCurrentValue, args)]), file=fout);
print("{:>#06}".format(lCurrentTime) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lCurrentValue, args)]), file=fout);
print("{:>#06}".format(lCurrentTime) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lNewValue, args)]), file=fout);
lLastValue = lNewValue;
elif lCurrentTime == args.s:
print("{:>#06}".format(lCurrentTime) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lNewValue, args)]), file=fout);
lLastValue = lNewValue;
elif lCurrentTime > args.s:
print("{:>#06}".format(args.s) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lCurrentValue, args)]), file=fout);
print("{:>#06}".format(lCurrentTime) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lCurrentValue, args)]), file=fout);
print("{:>#06}".format(lCurrentTime) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lNewValue, args)]), file=fout);
lLastValue = lNewValue;
elif lNewValue != lLastValue:
print("{:>#06}".format(lCurrentTime) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lLastValue, args)]), file=fout);
print("{:>#06}".format(lCurrentTime) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lNewValue, args)]), file=fout);
lLastValue = lNewValue;
lCurrentValue = lNewValue;
if lLastValue is not None:
print("{:>#06}".format(lCurrentTime) + "".join(["{: >12}".format(v) for v in get_stringFromValue(lLastValue, args)]), file=fout);
print("# Export terminé.");
except (FileNotFoundError, IOError) as e:
print(str(e));
except StopIteration:
print("# Fin du fichier");