/
record_mode.py
279 lines (230 loc) · 8.12 KB
/
record_mode.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
"""
Provides functions for
1) recording outputs to file
2) replaying outputs from files
"""
import global_data
import mctransmitter
import datetime
import os
import errno
import time
import threading
import ui_display
playback_file_tag = None
save_filename_prefix = 'botwurst_command_record_'
default_save_directory = 'botwurst_command_recordings'
save_file_extension = '.dat'
# TODO set recording limit]
def make_directory(directory_name):
try:
os.makedirs(directory_name + '/')
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def set_default_save_directory(directory_name):
record_save_directory = directory_name
# HELPER FUNCTION FOR LOOKING AT GLOBAL VARIABLES
def print_global_record_variables():
print "RECORDING VARIABLE SETTINGS"
print "===================="
print "Recording: ", global_data.record
print "Will store in file numbered: ", global_data.record_file_number, " in directory: ", default_save_directory
print "Initial time: ", global_data.record_start_time
print "Recording array is empty: ", (len(global_data.record_array) == 0)
print "===================="
# RECORDING FUNCTIONS
def initialize_record_mode(file_number):
"""
Sets all the global_data variables to reflect that we are now recording
Creates the specified directory in which the recording file is to be saved, if directory does not exist
:param file_number: Tag for file where recording will be stored
"""
# if record_array is not empty back it up to file
if global_data.record_array:
file_tag = global_data.record_file_number + "_backup"
create_record_file(file_tag)
global_data.record = True
global_data.record_file_number = file_number
global_data.record_start_time = datetime.datetime.now()
# if save_directory already exists as subdirectory, nothing will happen
make_directory(default_save_directory)
def append_instruction(instruction):
"""
Appends the instruction to record array in global data with time step from 0
:param instruction: triple (PIN TYPE, PIN INDEX, VAL)
"""
time_stamp = datetime.datetime.now()
# TODO: look into note about datetime subtraction (is exact but may overflow)
time_diff = time_stamp - global_data.record_start_time
pin_type = instruction[0]
pin_index = instruction[1]
value = instruction[2]
record_instruction = (pin_type, pin_index, value, time_diff.total_seconds())
global_data.record_array.append(record_instruction)
# 2) CREATE A FILE FROM RECORD ARRAY
def create_record_file(file_tag=None, save_directory=None):
"""
Creates a file with the list of instructions in record_array
:param file_tag: defaults to file_number in global data
"""
if file_tag is None:
file_tag = global_data.record_file_number
if save_directory is None:
save_directory = default_save_directory
record_filename = save_directory + '/' + save_filename_prefix + str(file_tag) + save_file_extension
# Create new file, or overwrite file if it exists
with open(record_filename, 'w') as recording_file:
# Copy all commands to the file
for command in global_data.record_array:
recording_file.write(str(command) + '\n')
# Reinitialize all record variables
global_data.record = False
global_data.record_file_number = None
global_data.record_start_time = None
global_data.record_array = []
# 2) PLAYBACK FUNCTIONS
def clear_playback_array():
global_data.playback_array = []
def populate_playback_array_from_file(filename, is_file_tag=False, save_directory=None):
"""
Appends instructions from current file to playback array
:param filename: name of file containing recording information
:param is_file_tag: True if only using number to identify file (default False)
:param save_directory: default directory specified in global data
"""
if save_directory is None:
save_directory = default_save_directory
if is_file_tag:
filename = save_filename_prefix + str(filename)
playback_file = open(save_directory + '/' + str(filename) + save_file_extension, 'r')
playback_file_lines = playback_file.readlines()
for line in playback_file_lines:
global_data.playback_array.append((eval(line.rstrip())))
def playback_instruction(pin_type, pin_index, value):
if pin_type == 'd':
# print "DIGITAL, PIN_INDEX: ", pin_index, "VALUE: ", value
mctransmitter.tx_digital(pin_index, value)
elif pin_type == 'a':
# print "ANALOG, PIN_INDEX: ", pin_index, "VALUE: ", value
mctransmitter.tx_analog(pin_index, value)
class Playback_From_Array(threading.Thread):
def __init__(self, parent, queue):
threading.Thread.__init__(self)
self._queue = queue
self._parent = parent
self.start()
def run(self):
curr_time_stamp = 0
for instruction in self._queue:
while global_data.playback_paused:
if global_data.playback_cancel:
break
time.sleep(.1)
if global_data.playback_cancel:
break
temp_time_stamp = instruction[3]
time_diff = (temp_time_stamp - curr_time_stamp)
time.sleep(time_diff)
playback_instruction(instruction[0], instruction[1], instruction[2])
curr_time_stamp = temp_time_stamp
ui_display.update()
clear_playback_array()
global_data.playback = False
global_data.playback_file_number = None
ui_display.update()
def playback_from_file(filename, is_file_tag=False, save_directory=None):
clear_playback_array()
global_data.playback = True
global_data.playback_file_number = filename
populate_playback_array_from_file(filename, is_file_tag, save_directory)
playback_thread = Playback_From_Array(None, global_data.playback_array)
return playback_thread
# TESTING FUNCTIONS: TO REMOVE
# class Print_Hello_Every_Sec(threading.Thread):
# def __init__(self, parent, queue):
# threading.Thread.__init__(self)
# self._queue = queue
# self._parent = parent
# self.start()
#
# def run(self):
# for i in range(15):
# print "**********HELLO THERE**************"
# time.sleep(1)
#
# class Pause_Unpause(threading.Thread):
# def __init__(self, parent, queue):
# threading.Thread.__init__(self)
# self._queue = queue
# self._parent = parent
# self.start()
#
# def run(self):
# time.sleep(2)
# global_data.playback_paused = True
# print "PAUSING"
# time.sleep(5)
# global_data.playback_cancel = True
# print "CANCELLING"
# time.sleep(5)
# print "UNPAUSING"
# global_data.playback_paused = False
#
#
# def create_dummy_instruction_file(file_tag):
# short_delay = 0.1
# long_delay = 1
#
# initialize_record_mode(file_tag)
# print_global_record_variables()
#
# i = 1
# j = 0
#
# for iterator in range(10):
# i_is_even = (1 == i%2)
#
# digital_instruction = ('d', 0, i_is_even)
# append_instruction(digital_instruction)
#
# time.sleep(short_delay)
#
# digital_instruction = ('d', 1, not i_is_even)
# append_instruction(digital_instruction)
#
# time.sleep(short_delay)
#
# val = abs((j % 510) - 255)
#
# analog_instruction = ('a', 0, val)
# append_instruction(analog_instruction)
#
# time.sleep(short_delay)
#
# analog_instruction = ('a', 1, 255 - val)
# append_instruction(analog_instruction)
#
# time.sleep(long_delay)
#
# i = i + 1
# j = j + 20
#
# create_record_file()
#
# def main():
# test_file_tag = 5
# # create_dummy_instruction_file(test_file_tag)
#
# pause_thread = Pause_Unpause(None, None)
# playback_thread = playback_from_file(test_file_tag, True)
# print_hello_thread = Print_Hello_Every_Sec(None, None)
#
# print_hello_thread.join()
# playback_thread.join()
# pause_thread.join()
#
# print_global_record_variables()
#
#
# main()