Debian Codegraph @

Nasal/frame_utils.nas

raw
1 #---------------------------------------------------------------------------
2 #
3 # Title : Frame Utils
4 #
5 # File Type : Implementation File
6 #
7 # Description : Objects related to frame processing
8 #
9 # Author : Richard Harrison ([email protected])
10 #
11 # Creation Date : 05-05-2019
12 #
13 # Version : 1.0
14 #
15 # Copyright (C) 2019 Richard Harrison Released under GPL V2
16 #
17 #---------------------------------------------------------------------------*/
18
19
20 #---------------------------------------------------------------------------*/
21 # Partition data and process
22 #
23 # This manages the processing of data in a manner suitable for real time
24 # operations. Given a data array [0..size] this will process a number
25 # of array elements each time it is called This allows for a simple way
26 # to split up intensive processing across multiple frames.
27 #
28 # The limit is the number of elements to process per invocation or
29 # a specific amount of time.
30 #
31 # To limit the amount of time requires a timestamp object to be set using
32 # the set_timestamp method and then to set the maximum amount of
33 # time (in microseconds) by calling set_max_time_usec. A value of 500us is
34 # a good value to use - but it is upto the implementor to choose a value that
35 # is suited to their environment
36 #
37 # Usually one of more instances of this class will be contained within
38 # another object, however this will work equally well in global space.
39 #
40 # example usage (object);
41 #
42 # var VSD_Device =
43 # {
44 # new : func(designation, model_element, target_module_id, root_node)
45 # {
46 # ...
47 # obj.process_targets = PartitionProcessor.new("VSD-targets", 20, nil);
48 # obj.process_targets.set_max_time_usec(500);
49 # ...
50 # me.process_targets.set_timestamp(notification.Timestamp);
51 #
52 # then invoke.
53 # me.process_targets.process(me, awg_9.tgts_list,
54 # func(pp, obj, data){
55 # # initialisation; called before processing element[0]
56 # # params
57 # # pp is the partition processor that called this
58 # # obj is the reference object (first argument in the .process)
59 # # data is the entire data array.
60 # }
61 # ,
62 # func(pp, obj, element){
63 # # proces individual element;
64 # # params
65 # # pp is the partition processor that called this
66 # # obj is the reference object (first argument in the .process)
67 # # element is the element data[pp.data_index]
68 # # return 0 to stop processing any more elements and call the completed method
69 # # return 1 to continue processing.
70 # },
71 # func(pp, obj, data)
72 # {
73 # # completed; called after the last element processed
74 # # params
75 # # pp is the partition processor that called this
76 # # obj is the reference object (first argument in the .process)
77 # # data is the entire data array.
78 # });
79
80 var PartitionProcessor =
81 {
82 debug_output : 0,
83
84 new : func(_name, _size, _timestamp=nil){
85 var obj = {
86 parents : [PartitionProcessor],
87 data_index : 0,
88 ppos : 0,
89 name : _name,
90 end : 0,
91 partition_size : _size,
92 timestamp : _timestamp,
93 max_time_usec : 0,
94 };
95 return obj;
96 },
97 set_max_time_usec : func(_maxTimeUsec){
98 me.max_time_usec = _maxTimeUsec;
99 },
100 set_timestamp : func(_timestamp){
101 me.timestamp = _timestamp;
102 },
103 process : func (object, data, init_method, process_method, complete_method){
104
105 if (me.end != size(data)) {
106 # data changed during processing restart at the beginning.
107 me.data_index = 0;
108 }
109
110 if (me.data_index == 0) {
111 me.end = size(data);
112 init_method(me, object, data);
113 }
114
115 if (me.end == 0)
116 return;
117
118 me.start_pos = me.data_index;
119 if (me.timestamp != nil and me.max_time_usec > 0) {
120 me.start_time = me.timestamp.elapsedUSec();
121 me.end_time = me.start_time + me.max_time_usec;
122 } else {
123 me.start_time = 0;
124 me.end_time = 0;
125 }
126
127 for (me.ppos=0;me.ppos < me.partition_size; me.ppos += 1) {
128 if (me.data_index >= me.end) {
129 complete_method(me, object, data);
130 me.data_index = 0;
131 return;
132 }
133 if (!process_method(me, object, data[me.data_index])) {
134 complete_method(me, object, data);
135 me.data_index = 0;
136 return; # halt processing requested.
137 } else
138 me.data_index += 1;
139
140 if (me.data_index == me.start_pos) {
141 complete_method(me, object, data);
142 return;
143 }
144
145 if (me.end_time > 0 and me.timestamp.elapsedUSec() > me.end_time) {
146 if (PartitionProcessor.debug_output)
147 printf("PartitionProcessor: [%s] out of time %dus (processed# %d)",me.name, me.timestamp.elapsedUSec() - me.start_time, me.ppos);
148 return;
149 }
150 }
151 },
152 };