-
Notifications
You must be signed in to change notification settings - Fork 0
/
hallway_function.py
579 lines (437 loc) · 17.9 KB
/
hallway_function.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import random
from Radio import Radio
from collections import Counter
from copy import copy
start_time = time.time()
# slavePuzzle = "mainPuzzle"
hallwayPuzzles = "hallwayPuzzles"
captainsBridge = "captainsBridge"
class LedsIdTable:
FUSE = 23
ROBOT_BODY_LEFT = 12
ROBOT_BODY_RIGHT = 13
ROBOT_HEAD = 16
ENGINE_LEFT = 21
ENGINE_RIGTH = 20
BOX_1 = 8
BOX_2 = 9
BOX_3 = 10
BOX_4 = 11
class ButtonsIdTable:
WIRE_CONNECTION = 11
FUSE = 6
MECHANICS_CARD = 10
ROBOT_HEAD = 8
ENGINE = 9
COMMUTATOR = 7
class Colors:
WHITE = [0xff, 0xff, 0xff]
RED = [0xff, 0x0, 0x0]
GREEN = [0x0, 0xff, 0x0]
BLUE = [0x0, 0x0, 0xff]
NONE = [0x0, 0x0, 0x0]
class AdcIdTable:
RADIO = 0
BOX_LOCK = 1
def setLedValue(leds, id, color):
leds[id * 3 + 0] = color[0]
leds[id * 3 + 1] = color[1]
leds[id * 3 + 2] = color[2]
def REQ_WIRE_CONNECTED(master, task, game_state):
wiredConnection = master.getButtons(hallwayPuzzles).get()[
ButtonsIdTable.WIRE_CONNECTION]
if wiredConnection:
return True
return False
def REQ_WIRE_DISCONNECTED(master, task, game_state):
wiredConnection = master.getButtons(hallwayPuzzles).get()[
ButtonsIdTable.WIRE_CONNECTION]
if not wiredConnection:
return True
return False
def AC_ENABLE_FUSE_PUZZLE(master, task, game_state):
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.FUSE, Colors.RED)
def AC_DISABLE_FUSE_PUZZLE(master, task, game_state):
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.FUSE, Colors.NONE)
def REQ_FUSE_PUZZLE_SOLVED(master, task, game_state):
buttons = master.getButtons(hallwayPuzzles)
fuseSolved = buttons.get()[ButtonsIdTable.FUSE]
if fuseSolved:
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.FUSE, Colors.GREEN)
return True
return False
def REQ_FUSE_REMOVED(master, task, game_state):
buttons = master.getButtons(hallwayPuzzles)
fuseSolved = buttons.get()[ButtonsIdTable.FUSE]
if not (fuseSolved == 1 ):
return True
return False
radio = 0
def AC_ENABLE_RADIO(master, task, game_state):
global radio
print("Radio puzzle solved")
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.BOX_1, Colors.RED)
# add radio broadcast
# radio = Radio(0.5, 0.001)
# sounds = [('harp.wav',40.0,80.0), ('island_music_x.wav',120.0,160.0), ('1.wav',200.0,240.0)]
# radio.init_sounds(sounds, 'noize.wav')
# radio.start()
# radio.set_target_value(15)
game_state.add_active_task_with_id(12)
def REQ_RADIO_BROADCAST(master, task, game_state):
pass
# radioValue = master.getAdc(hallwayPuzzles).get()[AdcIdTable.RADIO]
# #print("Radio value: {}".format(radioValue))
# radio.set_target_value(radioValue)
#=============== ADDING TASKS =================================================
class TaskIdTable:
WIRE_CONNECTED = 0
WIRE_DISCONNECTED = 1
FUSE_PUZZLE = 2
FUSE_REMOVED = 3
# RADIO_ENABLE =
# RADIO_DISABLE = 3
def AC_ADD_CONNECT_WIRE(master, task, game_state):
game_state.add_active_task_with_id(TaskIdTable.WIRE_CONNECTED)
def AC_ADD_DISCONNECT_WIRE(master, task, game_state):
game_state.add_active_task_with_id(TaskIdTable.WIRE_DISCONNECTED)
def AC_ADD_FUSE_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(TaskIdTable.FUSE_PUZZLE)
def AC_ADD_FUSE_REMOVED(master, task, game_state):
game_state.add_active_task_with_id(TaskIdTable.FUSE_REMOVED)
# Включение и Выкл. радио выполняются в action
# def ADD_DISABLE_RADIO(master, task, game_state):
# game_state.add_active_task_with_id(3)
def AC_ADD_SEQUENCE_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(4)
def AC_ADD_MECHANICS_CARD_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(5)
def AC_ADD_TUMBLER_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(6)
def AC_ADD_HIDDEN_TUMBLER_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(7)
def AC_ADD_COMMUTATOR_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(8)
def AC_ADD_SECRET(master, task, game_state):
game_state.add_active_task_with_id(9)
def AC_ADD_ROBOT_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(10)
def AC_ADD_ENGINE_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(11)
def AC_ADD_BATTARIES_PUZZLE(master, task, game_state):
game_state.add_active_task_with_id(13)
def AC_ADD_ACTIVATE_CAPTAIN_BRIDGE(mastre, task, game_state):
game_state.add_active_task_with_id(13)
#===========================================================
def REQ_SECRET_DOORS(master, task, game_state):
# buttonStartPosition = 17
buttons = master.getButtons(hallwayPuzzles).get()[12:18]
if buttons[3:6] == [0, 1, 0]:
relays = buttons[0:3]
relays.insert(0, 0)
master.setRelays(captainsBridge, relays)
# master.sendGetButtons(slavePuzzle)
return False
# def HIDDEN_TUMBLER_PUZZLE_SOLVED(master, task, game_state):
# smartLeds = master.getSmartLeds(hallwayPuzzles).get()
#
# ledIdStartPosition = 0
# buttonStartPosition = 0
# buttons = master.getButtons(hallwayPuzzles).get()
# # print("---Time before true {:.3} seconds ---".format(time.time() - start_time))
#
# for index in range(6):
# buttonIndex = buttonStartPosition + index
# ledID = ledIdStartPosition + index
# if buttons[buttonIndex]:
# setLedValue(smartLeds, ledID, Colors.BLUE)
# else:
# setLedValue(smartLeds, ledID, Colors.RED)
#
# master.setSmartLeds(hallwayPuzzles, smartLeds)
#
# if buttons[0:6] == [1] * 6:
# return True
# return False
def AC_OPEN_FIRST_BOX(master, task, game_state):
print("First box was open!")
smartLeds = master.getSmartLeds(hallwayPuzzles)
# blue, because blue and green are switched
smartLeds.setOneLed(LedsIdTable.BOX_1, Colors.BLUE)
relays = master.getRelays(hallwayPuzzles).get()
# relays[0] = 1
# master.setRelays(hallwayPuzzles, relays)
def AC_OPEN_SECOND_BOX(master, task, game_state):
print("Second box was open!")
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.BOX_2, Colors.BLUE)
relays = master.getRelays(hallwayPuzzles).get()
# relays[1] = 1
# master.setRelays(hallwayPuzzles, relays)
def AC_OPEN_THIRD_BOX(master, task, game_state):
print("Third box was open!")
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.BOX_3, Colors.BLUE)
relays = master.getRelays(hallwayPuzzles).get()
# relays[2] = 1
# master.setRelays(hallwayPuzzles, relays)
def AC_OPEN_FOURTH_BOX(master, task, game_state):
print("Fourth box was open!")
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.BOX_4, Colors.BLUE)
relays = master.getRelays(hallwayPuzzles).get()
# relays[3] = 1
# master.setRelays(hallwayPuzzles, relays)
def REQ_COMMUTATOR_PUZZLE_SOLVED(master, task, game_state):
buttons = master.getButtons(hallwayPuzzles).get()
commutatorSolved = buttons[ButtonsIdTable.COMMUTATOR]
# if commutatorSolved:
if True:
return True
return False
class pColors:
RED = Colors.RED
GREEN = Colors.GREEN
BLUE = Colors.BLUE
def toggleHiddenColor(color):
if pColors.RED == color:
color = pColors.GREEN
else:
color = pColors.RED
return color
def toggleVisibleColor(color):
if pColors.GREEN == color:
color = pColors.BLUE
elif pColors.BLUE == color:
color = pColors.RED
else:
color = pColors.GREEN
return color
# initialization value
hiddenPanelColors = [pColors.RED] * 6
visiblePanelColors = [pColors.RED] * 6
visiblePanelSwitchers = []
oldVisiblePanelSwitchers = [None] * 6
hiddenPanelSwitchers = []
oldHiddenPanelSwitchers = [None] * 6
def REQ_TUMBLER_PUZZLE_SOLVED(master, task, game_state):
global hiddenPanelColors
global visiblePanelColors
global visiblePanelSwitchers
global oldVisiblePanelSwitchers
global hiddenPanelSwitchers
global oldHiddenPanelSwitchers
ELEMENTS_NUMBER = 6
VISIBLE_SWITCHERS_START_NUM = 12
VISIBLE_SWITCHERS_END_NUM = 17
HIDDEN_SWITCHERS_START_NUM = 0
HIDDEN_SWITCHERS_END_NUM = 5
buttons = master.getButtons(hallwayPuzzles).get()
# get values from visible Panel
visiblePanelSwitchers = buttons[VISIBLE_SWITCHERS_START_NUM:
VISIBLE_SWITCHERS_END_NUM + 1]
# doing reverse because num 12 in buttons is 6 on panel
visiblePanelSwitchers.reverse()
hiddenPanelSwitchers = buttons[HIDDEN_SWITCHERS_START_NUM:
HIDDEN_SWITCHERS_END_NUM + 1]
# print("Visible values: {}".format(visiblePanelSwitchers))
# print("Hidden values: {}".format(hiddenPanelSwitchers))
# checked Visible Panel
for index in range(ELEMENTS_NUMBER):
if oldVisiblePanelSwitchers[index] != visiblePanelSwitchers[index]:
# change color for current tumbler
visiblePanelColors[index] = toggleVisibleColor(
visiblePanelColors[index])
# change color for nex tumbler on the same line
# on hidden panel
colorId = index + 1
# if element before inc was been the last on the line
# next element been first on the same line
if colorId % 3 == 0:
colorId = colorId - 3
hiddenPanelColors[colorId] = toggleHiddenColor(
hiddenPanelColors[colorId])
# checked Hidden Panel
for index in range(ELEMENTS_NUMBER):
if oldHiddenPanelSwitchers[index] != hiddenPanelSwitchers[index]:
hiddenPanelColors[index] = toggleHiddenColor(hiddenPanelColors[index])
colorId = index + 1
if colorId % 3 == 0:
colorId = colorId - 3
visiblePanelColors[colorId] = toggleVisibleColor(
visiblePanelColors[colorId])
# save new values in global lists
oldVisiblePanelSwitchers = visiblePanelSwitchers[:]
oldHiddenPanelSwitchers = hiddenPanelSwitchers[:]
# set new colors on device
# 12 times executed function, whom used Rlock
# It's might be critical
smartLedsObj = master.getSmartLeds(hallwayPuzzles)
for index in range(ELEMENTS_NUMBER):
smartLedsObj.setOneLed(index, hiddenPanelColors[index])
# NUM_LEDS 32, but index from 0 to 31
visibleIndex = smartLedsObj.NUM_LEDS - 1 - index
smartLedsObj.setOneLed(visibleIndex, visiblePanelColors[index])
WINNER_COLOR_LIST= [pColors.GREEN] * 6
visiblePanelState = (WINNER_COLOR_LIST == visiblePanelColors)
hiddenPanelState = (WINNER_COLOR_LIST == hiddenPanelColors)
return visiblePanelState and hiddenPanelState
# def REQ_TUMBLER_PUZZLE_SOLVED(master, task, game_state):
#
# smartLeds = master.getSmartLeds(hallwayPuzzles).get()
# ledIdStartPosition = 31
# buttonStartPosition = 17
# buttons = master.getButtons(hallwayPuzzles).get()
#
# for index in range(6):
# buttonIndex = buttonStartPosition - index
# ledID = ledIdStartPosition - index
# if buttons[buttonIndex]:
# setLedValue(smartLeds, ledID, Colors.BLUE)
# else:
# setLedValue(smartLeds, ledID, Colors.RED)
#
# master.setSmartLeds(hallwayPuzzles, smartLeds)
#
# if buttons[12:18] == [1] * 6:
# return True
# return False
def AC_DISABLE_RADIO(master, task, game_state):
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.BOX_1, Colors.NONE)
# Пока будут глобальными, потом перенесём в стек
# Проблема в следующем. При повороте тумблера возникают шумы, которые
# мы успеваем считывать на нашей скорости. Шумы надо убрать и оставить
# только устоявшиеся значения.
# Чтение с задержкой не подходит - влияет на остальные функции
# Проверим идею.
# На каждом вхождении в функцию будем считывать значение с АЦП
# и складывать в массив. Через определённое количество входений
# выберем то число, которое чаще всех встречается в массиве.
# Проверку можно делать с определённого считывания,
# и продолжать пока какое-нибудь число не повторить больше N раз.
lastLockPosition = 0
state = 0
# массив значений
READ_DATA_STACK = []
# максимальная длина массива, после достижение которой массив сброситься
READ_DATA_STACK_LENGTH = 180*2
def turnLeft(lastValue, newValue):
if lastValue == 255 and newValue == 0:
return True
if newValue > lastValue:
return True
return False
def turnRigth(lastValue, newValue):
if lastValue == 0 and newValue == 255:
return True
if newValue < lastValue:
return True
return False
def turn(lastValue, newValue):
if turnLeft(lastValue, newValue):
return "L"
if turnRigth(lastValue, newValue):
return "R"
# def findValue(stack):
PLAYER_SEQUENCE=[]
def REQ_CORRECT_SEQUENCE_ENTERED(master, task, game_state):
# return True
# Сохраняем последнее выбранное значение
global lastLockPosition, state
global PLAYER_SEQUENCE
global READ_DATA_STACK, READ_DATA_STACK_LENGTH
# Позиция в массиве ADC
LOCK_ID = 1
# погрешность
ERROR_VALUE = 15
# ACTIVATION_SEQUENCE = ['L', 'L', 'R', 'L', 'R', 'R', 'L', 'R']
# Последовательность для открытия
# L - влево; R - вправо
ACTIVATION_SEQUENCE = ['L','L', 'L', 'R', 'L']
# time.sleep(0.6)
value = master.getAdc(hallwayPuzzles).get()[LOCK_ID]
READ_DATA_STACK.append(value)
if len(READ_DATA_STACK) < READ_DATA_STACK_LENGTH:
return
if len(PLAYER_SEQUENCE) > len(ACTIVATION_SEQUENCE) * 2:
PLAYER_SEQUENCE = copy(PLAYER_SEQUENCE[:len(ACTIVATION_SEQUENCE)])
# get most freq value
value = Counter(READ_DATA_STACK).most_common(1)[0][0]
READ_DATA_STACK = []
lockPosition = value
# print("Lock value: {}".format(value))
# if (lockPosition != lastLockPosition):
# print("LastValue: {}, newValue: {}, state: {} {}".format(
# lastLockPosition, lockPosition, state, ACTIVATION_SEQUENCE[state - 1]))
# Смотрим, менялась ли позиция переключателя.
lessDefault = lastLockPosition < (lockPosition + ERROR_VALUE)
largeDefault = lastLockPosition > (lockPosition - ERROR_VALUE)
if lessDefault and largeDefault:
lastLockPosition = lockPosition
return False
print("Time: {}".format(time.time()))
print("LastValue: {}, newValue: {}".format(lastLockPosition, lockPosition))
turnDirection = turn(lastLockPosition, lockPosition)
PLAYER_SEQUENCE.insert(0, turnDirection)
reversePlayerSequence = copy(PLAYER_SEQUENCE[:len(ACTIVATION_SEQUENCE)])
reversePlayerSequence.reverse()
print("Player sequence: {} \n reverse: {}".format(PLAYER_SEQUENCE, reversePlayerSequence))
reversePlayerSequence = copy(PLAYER_SEQUENCE[:len(ACTIVATION_SEQUENCE)])
reversePlayerSequence.reverse()
if ACTIVATION_SEQUENCE == reversePlayerSequence:
print("We opened lock box: sleep!!!")
PLAYER_SEQUENCE = []
return True
lastLockPosition = lockPosition
def REQ_MECHANICS_CARD_USED(master, task, game_state):
buttons = master.getButtons(hallwayPuzzles).get()
cardUsed = buttons[ButtonsIdTable.MECHANICS_CARD]
if cardUsed:
return True
return False
def AC_ENABLE_TUMBLER_PUZZLE(master, task, game_state):
smartLeds = master.getSmartLeds(hallwayPuzzles).get()
ledIdStartPosition = 31
for index in range(6):
ledID = ledIdStartPosition - index
setLedValue(smartLeds, ledID, Colors.RED)
setLedValue(smartLeds, index, Colors.RED)
master.setSmartLeds(hallwayPuzzles, smartLeds)
def REQ_ROBOT_ASSEMBLED(master, task, game_state):
buttons = master.getButtons(hallwayPuzzles)
robbotAssembled = buttons.get()[ButtonsIdTable.ROBOT_HEAD]
if robbotAssembled:
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.ROBOT_BODY_LEFT, Colors.RED)
smartLeds.setOneLed(LedsIdTable.ROBOT_BODY_RIGHT, Colors.BLUE)
smartLeds.setOneLed(LedsIdTable.ROBOT_HEAD, Colors.WHITE)
return True
return False
def AC_ROBOT_SAY_RIDDLE(master, task, game_state):
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.ROBOT_BODY_LEFT, Colors.GREEN)
smartLeds.setOneLed(LedsIdTable.ROBOT_BODY_RIGHT, Colors.GREEN)
smartLeds.setOneLed(LedsIdTable.ROBOT_HEAD, Colors.WHITE)
print("Robot say RIDDLE!")
def REQ_ENGINE_ASSEMBLED(master, task, game_state):
smartLeds = master.getSmartLeds(hallwayPuzzles)
smartLeds.setOneLed(LedsIdTable.ENGINE_RIGTH, Colors.BLUE)
smartLeds.setOneLed(LedsIdTable.ENGINE_LEFT, Colors.RED)
buttons = master.getButtons(hallwayPuzzles)
engineAssembled = buttons.get()[ButtonsIdTable.ENGINE]
if engineAssembled:
smartLeds.setOneLed(LedsIdTable.ENGINE_RIGTH, Colors.GREEN)
smartLeds.setOneLed(LedsIdTable.ENGINE_LEFT, Colors.GREEN)
return True
return False
def AC_ACTIVATE_CAPTAIN_BRIDGE(master, state):
print("Captain bridge activated")