-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinusgen.py
executable file
·526 lines (394 loc) · 16.5 KB
/
sinusgen.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#!/usr/bin/env python3
"""
sinusgen v2.33 [01.02.2022] *** by WolF
usage: sinusgen.py [-h] [-cfg CONFIG_FILE] [-show] [-previewfile PREVIEWFILE] [-list] [-output OUTPUT_FILE] [-min MINIMUM_VALUE]
[-max MAXIMUM_VALUE] [-steps STEPS] [-type TYPE] [-invert] [-offset OFFSET] [-mod MODULO]
This program writes sinus data as bytes into a binary file. If values are greater than 256, two files are written.
optional arguments:
-h, --help show this help message and exit
-cfg CONFIG_FILE configuration in .json format
-show show preview
-previewfile PREVIEWFILE
preview png filename
-list list all avaiable sinus types and exit
-output OUTPUT_FILE name of binary output file without its suffix
-min MINIMUM_VALUE minimum value (0-65535)
-max MAXIMUM_VALUE maximum value (0-65535)
-steps STEPS amount of bytes to generate, values below 5 cause an error with some sinus-types
-type TYPE sinus type, see output of --list
-invert invert values
-offset OFFSET step offset, where to begin
-mod MODULO modulo value
Note: All values of a config-file can be overwritten by commandline parameters.
Examples:
./sinusgen.py -cfg sinus1.json --show
./sinusgen.py -output datafile -min 0 -max 255 -steps 256 -type 1 -invert -offset 20 -mod 8 -previewfile sinus.png
./sinusgen.py -cfg sinus1.json -max 255 -type 10 --show
"""
import sys
import struct
import math
import json
import argparse
import PIL.ImageDraw as ImageDraw
import PIL.Image as Image
import PIL.ImageShow as ImageShow
PROGNAME = 'sinusgen';
VERSION = '2.33';
DATUM = '01.02.2022';
VIEW_WIDTH = 800
VIEW_HEIGHT = 600
MAXIMUM_VALUE = 65535
TABLE_SINUS_CALC = (
('full sinus',2,1,1,0), #0
('half sinus',1,1,1,0), #1
('shot',1,2,2,1), #2
('boobs',1,3,2,1), #3
('small hill, big hill',1,4,2,1), #4
('high valleys',1,5,2,1), #5
('three hills',1,5,2,2), #6
('three irregulars',1,6,2,1), #7
('four brothers',8,1,2,1), #8
('wave shot',3,2,2,1), #9
('zig zag',8,5,2,1), #10
('double trouble',4,4,2,1), #11
('what the heck',3,8,2,1), #12
('landscape',5,4,2,1), #13
('two hills',5,3,2,1), #14
('whatever',5,2,2,1), #15
('the famous Mr Ed',4,5,2,1), #16
('curly',7,3,2,1), #17
('Oh dear...',3,8,2,1), #18
('When will it end?',4,9,2,1), #19
('ice cream',4,10,2,1), #20
('good grief...',8,2,2,1), #21
('nonsense',9,2,2,1), #22
('windy',10,2,2,1), #23
('one more to go',2,11,2,1), #24
('Thanks God it`s over.',11,6,2,1) #25
)
args = None
buffer=[]
#default values:
user_min = 0
user_max = 0
user_steps = 0
user_invert = False
user_offset = 0
user_type = 0
user_mod = 0
user_show = False
user_outname = 'output'
def _calc_sinus(
factor1,
factor2,
multi1,
multi2
) :
#print "calc_sinus: factor1=%d, factor2=%d, multi1=%d, multi2=%d" %(factor1, factor2, multi1, multi2)
global buffer
global user_steps
global user_offset
global MAXIMUM_VALUE
my_buffer = []
for a in range (0,user_steps+1) :
sinus1 = (
(
(
(MAXIMUM_VALUE/2)*
math.sin( (a*math.pi) / (user_steps/factor1) )
) + (MAXIMUM_VALUE/2)
) *multi1
)
sinus2 = (
(
(
(MAXIMUM_VALUE/2)*
math.sin( (a*math.pi) / (user_steps/factor2) )
) + (MAXIMUM_VALUE/2)
) *multi2
)
buffer.append(int(sinus1+sinus2))
#copy my_buffer to buffer starting at the given offset
buffer = buffer[user_offset:user_steps] + buffer[0:user_offset]
return None
def _scale_values(
) :
global buffer
global user_min
global user_max
global user_invert
global user_mod
# get maximal und minimal values
min_buffer = min(buffer)
# normalize data (set minimal value to 0)
for a in range(0, len(buffer)) :
buffer[a] -= min_buffer
scale_user = (user_max+1) - user_min #hack: +1 to avoid that nagging $fe $fe $ff $fe $fe problem...
scale_buffer = max(buffer) - min(buffer)
# scale values
for a in range(0,len(buffer)) :
my_value = buffer[a] * scale_user / scale_buffer
if (user_invert) :
buffer[a] = (user_max - my_value)
else :
buffer[a] = (my_value + user_min)
#undo the hack... the dirty way
if (buffer[a] > user_max) : buffer[a] = user_max
# modulo operation
buffer[a] = buffer[a] % (user_mod+1)
return None
def _write_data(
filename
) :
global user_steps
global user_max
filename_low = filename+".bin"
filename_high = filename+"-high.bin"
print (" Opening file \"%s\" for writing..." % filename_low)
try:
file_out = open(filename_low , "wb")
except IOError as err:
print("I/O error: {0}".format(err))
return 1
out_buffer = []
for b in range(0,len(buffer)) :
out_buffer.append( int(buffer[b]) & 0b11111111 ) # low
file_out.write(bytearray(out_buffer))
file_out.close()
if ( user_max > 255) :
print (" Opening file \"%s\" for writing high-values..." % filename_high)
try:
file_out = open(filename_high , "wb")
except IOError as err:
print("I/O error: {0}".format(err))
return 1
out_buffer = []
for b in range(0,len(buffer)) :
out_buffer.append( int(buffer[b]) >> 8 ) # high
file_out.write(bytearray(out_buffer))
file_out.close()
print(" done.")
return None
def _draw(
) :
global VIEW_HEIGHT
global VIEW_WIDTH
global TABLE_SINUS_CALC
global user_type
global user_steps
global user_max
global user_min
global user_steps
global user_invert
global user_offset
global user_mod
# local variables:
scale_x = VIEW_WIDTH / len(buffer)
scale_y = VIEW_HEIGHT / max(buffer)
draw_data = []
draw_data.append( (0*scale_x,VIEW_HEIGHT ) )
draw_data.append( (0*scale_x,VIEW_HEIGHT-(buffer[-1]*scale_y ) ) )
for a in range(0,len(buffer)) :
draw_data.append( ((a+1)*scale_x,VIEW_HEIGHT-(buffer[a]*scale_y)) )
draw_data.append( ((len(buffer))*scale_x,VIEW_HEIGHT ) )
im = Image.new("RGB", (VIEW_WIDTH, VIEW_HEIGHT), "#000044")
draw = ImageDraw.Draw(im, 'RGBA')
#y-axis
for a in range(0,user_max+1) :
posx = 0
posy = VIEW_HEIGHT-(a-1)*scale_y
if ( (a % 5) == 0 ) : draw.line((posx,posy,VIEW_WIDTH,posy), fill="#004444"); continue
#x-axis
for a in range(0,len(buffer)+1) :
posx = a*scale_x
posy = VIEW_HEIGHT
if ( (a % 5) == 0 ) : draw.line((posx,posy,posx,0), fill="#004444"); continue
#y-axis
for a in range(0,user_max+1) :
posx = 0
posy = VIEW_HEIGHT-(a-1)*scale_y
if ( (a % 10) == 0 ) : draw.line((posx,posy,VIEW_WIDTH,posy),fill="#008888"); draw.text((0, posy-5), str(a)); continue
#x-axis
for a in range(0,len(buffer)+1) :
posx = a*scale_x
posy = VIEW_HEIGHT
if ( (a % 10) == 0 ) : draw.line((posx,posy,posx,0),fill="#008888"); draw.text((posx-5, posy-10), str(a)); continue
#y-axis
for a in range(0,user_max+1) :
posx = 0
posy = VIEW_HEIGHT-(a-1)*scale_y
if ( (a % 50) == 0 ) : draw.line((posx,posy,VIEW_WIDTH,posy),fill="#00cccc"); draw.text((0, posy-5), str(a)); continue
#x-axis
for a in range(0,len(buffer)+1) :
posx = a*scale_x
posy = VIEW_HEIGHT
if ( (a % 50) == 0 ) : draw.line((posx,posy,posx,0),fill="#00cccc"); draw.text((posx-5, posy-10), str(a)); continue
#y-axis
for a in range(0,user_max+1) :
posx = 0
posy = VIEW_HEIGHT-(a-1)*scale_y
if ( (a % 100) == 0 ) : draw.line((posx,posy,VIEW_WIDTH,posy),fill="#00ffff"); draw.text((0, posy-5), str(a)); continue
#x-axis
for a in range(0,len(buffer)+1) :
posx = a*scale_x
posy = VIEW_HEIGHT
if ( (a % 100) == 0 ) : draw.line((posx,posy,posx,0),fill="#00ffff"); draw.text((posx-5, posy-10), str(a)); continue
draw.polygon((draw_data),fill="#aa88ccdd", outline="#8888ffdd")
#y-axis
for a in range(0,user_max+1) :
posx = 0
posy = VIEW_HEIGHT-(a-1)*scale_y
if ( (a % 100) == 0 ) : draw.line((posx,posy,posx+40,posy),fill="#ffffff"); draw.text((0, posy-5), str(a)); continue
if ( (a % 50) == 0 ) : draw.line((posx,posy,posx+30,posy),fill="#cccccc"); draw.text((0, posy-5), str(a)); continue
if ( (a % 10) == 0 ) : draw.line((posx,posy,posx+20,posy),fill="#888888"); draw.text((0, posy-5), str(a)); continue
if ( (a % 5) == 0 ) : draw.line((posx,posy,posx+10,posy), fill="#444444"); continue
#x-axis
for a in range(0,len(buffer)+1) :
posx = a*scale_x
posy = VIEW_HEIGHT
if ( (a % 100) == 0 ) : draw.line((posx,posy,posx,posy-40),fill="#ffffff"); draw.text((posx-5, posy-10), str(a)); continue
if ( (a % 50) == 0 ) : draw.line((posx,posy,posx,posy-30),fill="#cccccc"); draw.text((posx-5, posy-10), str(a)); continue
if ( (a % 10) == 0 ) : draw.line((posx,posy,posx,posy-20),fill="#888888"); draw.text((posx-5, posy-10), str(a)); continue
if ( (a % 5) == 0 ) : draw.line((posx,posy,posx,posy-10), fill="#444444"); continue
draw.rectangle(((40-3,5-3),(400,50)), fill="#444444aa", outline="#0000aadd")
#text
"%s v%s [%s] *** by WolF"% (PROGNAME, VERSION, DATUM)
text = PROGNAME + " v" + VERSION + " [" + DATUM + "] *** by WolF\n" \
+ "type " +str(user_type)+ " \"" + TABLE_SINUS_CALC[user_type][0] + "\"\n" \
+ str(user_steps) + " values " +str(user_min)+ "-" +str(user_max)+ ", offset=" +str(user_offset)+ ", modulo=" +str(user_mod)
if (user_invert) :
text = text + ", inverted values"
draw.text((40, 5), text)
im.show()
if args.previewfile :
print('Writing preview image "%s"...' % args.previewfile)
im.save(args.previewfile)
return None
def _do_it() :
global user_min, user_max, user_steps, user_invert, user_offset, user_type, user_mod, user_outname
global user_show
global sindata
if (args.list) :
_show_sinus_types()
return None
if (args.config_file) :
print (" Opening config-file \"%s\" for reading..." % args.config_file)
try:
file_in = open(args.config_file , "r")
except IOError as err:
print("I/O error: {0}".format(err))
return 1
config = json.load(file_in)
# json_value = json.loads(f.read().decode())
if (config['min']): user_min = config['min']
if (config['max']): user_max = config['max']
if (config['steps']): user_steps = config['steps']
if (config['type']): user_type = config['type']
if (config['invert']):
if (config['invert'] != 0) : user_invert = True
if (config['offset']): user_offset = config['offset']
if (config['mod']): user_mod = config['mod']
if (config['output']): user_outname = config['output']
if (args.minimum_value): user_min = args.minimum_value
if (args.maximum_value): user_max = args.maximum_value
if (args.steps): user_steps = args.steps
if (args.type): user_type = args.type
if (args.invert): user_invert = args.invert
if (args.offset): user_offset = args.offset
if (args.modulo): user_mod = args.modulo
if (args.output_file): user_output = args.output_file
################################################
# sanity check:
if (user_min < 0) :
print (" **** value-error: min value is %d, but it should be >0." % user_min )
return 1
if (user_min >= user_max) :
print (" **** value-error: min value is %d, but it should be <%d (max)." % (user_min, user_max) )
return 1
if (user_min > MAXIMUM_VALUE) :
print (" **** value-error: min value is %d, but it should be <=%d." %( user_max, MAXIMUM_VALUE) )
return 1
if (user_max > MAXIMUM_VALUE) :
print (" **** value-error: max value is %d, but it should be <=%d." %( user_max, MAXIMUM_VALUE) )
return 1
if (user_max <= user_min) :
print (" **** value-error: max value is %d, but it should be >%d (min)." %( user_max, user_min))
return 1
if (user_steps <= 0) :
print (" **** value-error: steps value is %d, but it should be > 0." % (user_steps))
return 1
if (user_type >= len(TABLE_SINUS_CALC)) :
print (" **** value-error: sinustype value is %d, but it should be <%d." % (user_type, len(TABLE_SINUS_CALC)))
return 1
# if (config['show'] > 1) :
# print (" **** config-error: show value is %d, but it should be either 0 or 1." % config['show'])
# return 1
if (user_offset >= user_steps) :
print (" **** value-error: offset value is %d, but it should be <%d (steps)." % (user_offset, user_steps))
return 1
# user_mod = config['mod']
if (user_mod == 0) :
print (" Notice: mod value is %d, disabling modulo feature." % user_mod )
user_mod = user_max
################################################
print (" Config: sinustype %d \"%s\", %d values %d-%d, offset=%d, modulo=%d" %(user_type, TABLE_SINUS_CALC[user_type][0], user_steps, user_min, user_max, user_offset, user_mod) )
if (user_invert) :
print (" Values will be inverted.")
################################################
# calculate sinus
_calc_sinus(
TABLE_SINUS_CALC[user_type][1],
TABLE_SINUS_CALC[user_type][2],
TABLE_SINUS_CALC[user_type][3],
TABLE_SINUS_CALC[user_type][4]
)
_scale_values()
################################################
# write data to file
_write_data(user_outname)
if (args.show) : _draw()
def _show_sinus_types() :
print ('Available sinus types:')
for a in range(0,len(TABLE_SINUS_CALC)) :
print ('%2d : "%s"' %(a,TABLE_SINUS_CALC[a][0]) )
return None
def _main_procedure() :
global args
print("%s v%s [%s] *** by WolF"% (PROGNAME, VERSION, DATUM))
#https://docs.python.org/3/library/argparse.html
parser = argparse.ArgumentParser(
description='This program writes sinus data as bytes into a binary file. If values are greater than 256, two files are written.',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=''\
'Note: All values of a config-file can be overwritten by commandline parameters.\n\n'\
'Examples:\n'\
' %s -cfg sinus1.json --show\n'\
' %s -output datafile -min 0 -max 255 -steps 256 -type 1 -invert -offset 20 -mod 8 -previewfile sinus.png\n'\
' %s -cfg sinus1.json -max 255 -type 10 --show\n' % (sys.argv[0],sys.argv[0],sys.argv[0])
)
parser.add_argument('-cfg', dest='config_file', help='configuration in .json format')
parser.add_argument('-show', dest='show', help='show preview', action='store_true')
parser.add_argument('-previewfile', dest='previewfile', help='preview png filename')
parser.add_argument('-list', dest='list', help='list all avaiable sinus types and exit', action='store_true')
parser.add_argument('-output', dest='output_file', help='name of binary output file without its suffix')
parser.add_argument('-min', dest='minimum_value', help='minimum value (0-%d)'%MAXIMUM_VALUE, type=int)
parser.add_argument('-max', dest='maximum_value', help='maximum value (0-%d)'%MAXIMUM_VALUE, type=int)
parser.add_argument('-steps', dest='steps', help='amount of bytes to generate, values below 5 cause an error with some sinus-types', type=int)
parser.add_argument('-type', dest='type', help='sinus type, see output of --list', type=int)
parser.add_argument('-invert', dest='invert', help='invert values', action='store_true')
parser.add_argument('-offset', dest='offset', help='step offset, where to begin', type=int)
parser.add_argument('-mod', dest='modulo', help='modulo value', type=int)
args = parser.parse_args()
exitcode = _do_it()
if (exitcode != None) :
print('Exiting with return code %d.' % exitcode)
sys.exit(exitcode)
"""
try:
sys.exit(exitcode)
except SystemExit as e:
print('Exiting with return code "%s".' % e)
"""
if __name__ == '__main__':
_main_procedure()