forked from missionpinball/mpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast2.py
1391 lines (1080 loc) · 49.5 KB
/
fast2.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Contains the hardware interface and drivers for the FAST Pinball platform
hardware, including the FAST Core and WPC controllers as well as FAST I/O
boards.
"""
# fast.py
# Mission Pinball Framework
# Written by Brian Madden & Gabe Knuth
# Released under the MIT License. (See license info at the end of this file.)
# Documentation and more info at http://missionpinball.com/mpf
import logging
import time
import sys
import threading
import Queue
import traceback
import io
from distutils.version import StrictVersion
from copy import deepcopy
from mpf.system.platform import Platform
from mpf.system.config import Config
from mpf.system.utility_functions import Util
try:
import serial
serial_imported = True
except:
serial_imported = False
# Minimum firmware versions needed for this module
DMD_MIN_FW = '0.88'
NET_MIN_FW = '0.88'
RGB_MIN_FW = '0.87'
IO_MIN_FW = '0.87'
DMD_LATEST_FW = '0.88'
NET_LATEST_FW = '0.90'
RGB_LATEST_FW = '0.87'
IO_LATEST_FW = '0.89'
class HardwarePlatform(Platform):
"""Platform class for the FAST hardware controller.
Args:
machine: The main ``MachineController`` instance.
"""
def __init__(self, machine):
super(HardwarePlatform, self).__init__(machine)
self.log = logging.getLogger('FAST')
self.log.info("Configuring FAST hardware.")
if not serial_imported:
self.log.error('Could not import "pySerial". This is required for '
'the FAST platform interface')
sys.exit()
# ----------------------------------------------------------------------
# Platform-specific hardware features. WARNING: Do not edit these. They
# are based on what the FAST hardware can and cannot do.
self.features['max_pulse'] = 255 # todo
self.features['hw_timer'] = False
self.features['hw_rule_coil_delay'] = True # todo
self.features['variable_recycle_time'] = True # todo
self.features['variable_debounce_time'] = True # todo
# Make the platform features available to everyone
self.machine.config['platform'] = self.features
# ----------------------------------------------------------------------
self.hw_rules = dict()
self.dmd_connection = None
self.net_connection = None
self.rgb_connection = None
self.fast_nodes = list()
self.connection_threads = set()
self.receive_queue = Queue.Queue()
self.fast_leds = set()
self.flag_led_tick_registered = False
self.fast_io_boards = list()
self.waiting_for_switch_data = False
config_spec = '''
ports: list
baud: int|921600
config_number_format: string|hex
watchdog: ms|1000
default_debounce_open: ms|30
default_debounce_close: ms|30
debug: boolean|False
'''
self.config = Config.process_config(config_spec=config_spec,
source=self.machine.config['fast'])
self.watchdog_command = 'WD:' + str(hex(self.config['watchdog']))[2:]
self.machine_type = (
self.machine.config['hardware']['driverboards'].lower())
if self.machine_type == 'wpc':
self.log.info("Configuring the FAST Controller for WPC driver "
"board")
else:
self.log.info("Configuring FAST Controller for FAST IO boards.")
self._connect_to_hardware()
if 'config_number_format' not in self.machine.config['fast']:
self.machine.config['fast']['config_number_format'] = 'int'
self.wpc_switch_map = {
# WPC HEX DEC
'S11': '00', #00
'S12': '01', #01
'S13': '02', #02
'S14': '03', #03
'S15': '04', #04
'S16': '05', #05
'S17': '06', #06
'S18': '07', #07
'S21': '08', #08
'S22': '09', #09
'S23': '0A', #10
'S24': '0B', #11
'S25': '0C', #12
'S26': '0D', #13
'S27': '0E', #14
'S28': '0F', #15
'S31': '10', #16
'S32': '11', #17
'S33': '12', #18
'S34': '13', #19
'S35': '14', #20
'S36': '15', #21
'S37': '16', #22
'S38': '17', #23
'S41': '18', #24
'S42': '19', #25
'S43': '1A', #26
'S44': '1B', #27
'S45': '1C', #28
'S46': '1D', #29
'S47': '1E', #30
'S48': '1F', #31
'S51': '20', #32
'S52': '21', #33
'S53': '22', #34
'S54': '23', #35
'S55': '24', #36
'S56': '25', #37
'S57': '26', #38
'S58': '27', #39
'S61': '28', #40
'S62': '29', #41
'S63': '2A', #42
'S64': '2B', #43
'S65': '2C', #44
'S66': '2D', #45
'S67': '2E', #46
'S68': '2F', #47
'S71': '30', #48
'S72': '31', #49
'S73': '32', #50
'S74': '33', #51
'S75': '34', #52
'S76': '35', #53
'S77': '36', #54
'S78': '37', #55
'S81': '38', #56
'S82': '39', #57
'S83': '3A', #58
'S84': '3B', #59
'S85': '3C', #60
'S86': '3D', #61
'S87': '3E', #62
'S88': '3F', #63
'S91': '40', #64
'S92': '41', #65
'S93': '42', #66
'S94': '43', #67
'S95': '44', #68
'S96': '45', #69
'S97': '46', #70
'S98': '47', #71
'S101': '48', #72
'S102': '49', #73
'S103': '4A', #74
'S104': '4B', #75
'S105': '4C', #76
'S106': '4D', #77
'S107': '4E', #78
'S108': '4F', #79
# Directs
'SD1': '50', #80
'SD2': '51', #81
'SD3': '52', #82
'SD4': '53', #83
'SD5': '54', #84
'SD6': '55', #85
'SD7': '56', #86
'SD8': '57', #87
# DIP switches
'DIP1': '58', #88
'DIP2': '59', #89
'DIP3': '5A', #90
'DIP4': '5B', #91
'DIP5': '5C', #92
'DIP6': '5D', #93
'DIP7': '5E', #94
'DIP8': '5F', #95
# Fliptronics
'SF1': '60', #96
'SF2': '61', #97
'SF3': '62', #98
'SF4': '63', #99
'SF5': '64', #100
'SF6': '65', #101
'SF7': '66', #102
'SF8': '67', #103
}
self.wpc_light_map = {
'L11': '00', 'L12': '01', 'L13': '02', 'L14': '03',
'L15': '04', 'L16': '05', 'L17': '06', 'L18': '07',
'L21': '08', 'L22': '09', 'L23': '0A', 'L24': '0B',
'L25': '0C', 'L26': '0D', 'L27': '0E', 'L28': '0F',
'L31': '10', 'L32': '11', 'L33': '12', 'L34': '13',
'L35': '14', 'L36': '15', 'L37': '16', 'L38': '17',
'L41': '18', 'L42': '19', 'L43': '1A', 'L44': '1B',
'L45': '1C', 'L46': '1D', 'L47': '1E', 'L48': '1F',
'L51': '20', 'L52': '21', 'L53': '22', 'L54': '23',
'L55': '24', 'L56': '25', 'L57': '26', 'L58': '27',
'L61': '28', 'L62': '29', 'L63': '2A', 'L64': '2B',
'L65': '2C', 'L66': '2D', 'L67': '2E', 'L68': '2F',
'L71': '30', 'L72': '31', 'L73': '32', 'L74': '33',
'L75': '34', 'L76': '35', 'L77': '36', 'L78': '37',
'L81': '38', 'L82': '39', 'L83': '3A', 'L84': '3B',
'L85': '3C', 'L86': '3D', 'L87': '3E', 'L88': '3F',
}
self.wpc_driver_map = {
'C01': '00', 'C02': '01', 'C03': '02', 'C04': '03',
'C05': '04', 'C06': '05', 'C07': '06', 'C08': '07',
'C09': '08', 'C10': '09', 'C11': '0A', 'C12': '0B',
'C13': '0C', 'C14': '0D', 'C15': '0E', 'C16': '0F',
'C17': '10', 'C18': '11', 'C19': '12', 'C20': '13',
'C21': '14', 'C22': '15', 'C23': '16', 'C24': '17',
'C25': '18', 'C26': '19', 'C27': '1A', 'C28': '1B',
'C29': '1C', 'C30': '1D', 'C31': '1E', 'C32': '1F',
'C33': '24', 'C34': '25', 'C35': '26', 'C36': '27',
'FLRM': '20', 'FLRH': '21', 'FLLM': '22', 'FLLH': '23',
'FURM': '24', 'FURH': '25', 'FULM': '26', 'FULH': '27',
'C37': '28', 'C38': '29', 'C39': '2A', 'C40': '2B',
'C41': '2C', 'C42': '2D', 'C43': '2E', 'C44': '2F',
}
self.wpc_gi_map = {
'G01': '00', 'G02': '01', 'G03': '02', 'G04': '03',
'G05': '04', 'G06': '05', 'G07': '06', 'G08': '07',
}
# todo verify this list
self.fast_commands = {'ID': self.receive_id, # processor ID
'WX': self.receive_wx, # watchdog
'NI': self.receive_ni, # node ID
'RX': self.receive_rx, # RGB cmd received
'DX': self.receive_dx, # DMD cmd received
'SX': self.receive_sx, # sw config received
'LX': self.receive_lx, # lamp cmd received
'PX': self.receive_px, # segment cmd received
'SA': self.receive_sa, # all switch states
'/N': self.receive_nw_open, # nw switch open
'-N': self.receive_nw_closed, # nw switch closed
'/L': self.receive_local_open, # local sw open
'-L': self.receive_local_closed, # local sw close
'WD': self.receive_wd, # watchdog
}
def __repr__(self):
return '<Platform.FAST>'
def process_received_message(self, msg):
"""Sends an incoming message from the FAST controller to the proper
method for servicing.
"""
if msg[2:3] == ':':
cmd = msg[0:2]
payload = msg[3:].replace('\r','')
else:
self.log.warning("Received maformed message: %s", msg)
return
# Can't use try since it swallows too many errors for now
if cmd in self.fast_commands:
self.fast_commands[cmd](payload)
else:
self.log.warning("Received unknown serial command? %s. (This is ok "
"to ignore for now while the FAST platform is in "
"development)", msg)
def _connect_to_hardware(self):
# Connect to each port from the config. This procuess will cause the
# connection threads to figure out which processor they've connected to
# and to register themselves.
for port in self.config['ports']:
self.connection_threads.add(SerialCommunicator(machine=self.machine,
platform=self, port=port, baud=self.config['baud'],
send_queue=Queue.Queue(), receive_queue=self.receive_queue))
for i in self.connection_threads:
if i.serial_connection.port == 'com4':
self.machine.dmd_port = i
def register_processor_connection(self, name, communicator):
"""Once a communication link has been established with one of the
processors on the FAST board, this method lets the communicator let MPF
know which processor it's talking to.
This is a separate method since we don't know which processor is on
which serial port ahead of time.
"""
if name == 'DMD':
self.dmd_connection = communicator
elif name == 'NET':
self.net_connection = communicator
elif name == 'RGB':
self.rgb_connection = communicator
self.rgb_connection.send('RA:000000') # turn off all LEDs
def update_leds(self):
"""Updates all the LEDs connected to a FAST controller. This is done
once per game loop for efficiency (i.e. all LEDs are sent as a single
update rather than lots of individual ones).
Also, every LED is updated every loop, even if it doesn't change. This
is in case some interference causes a LED to change color. Since we
update every loop, it will only be the wrong color for one tick.
"""
msg = 'RS:'
for led in self.fast_leds:
msg += (led.number + led.current_color + ',') # todo change to join
msg = msg[:-1] # trim the final comma
self.rgb_connection.send(msg)
def get_hw_switch_states(self):
self.hw_switch_data = None
self.net_connection.send('SA:')
while not self.hw_switch_data:
time.sleep(.01)
self.tick()
return self.hw_switch_data
def receive_id(self, msg):
pass
def receive_wx(self, msg):
pass
def receive_ni(self, msg):
pass
def receive_rx(self, msg):
pass
def receive_dx(self, msg):
pass
def receive_sx(self, msg):
pass
def receive_lx(self, msg):
pass
def receive_px(self, msg):
pass
def receive_wd(self, msg):
pass
def receive_nw_open(self, msg):
self.machine.switch_controller.process_switch(state=0,
num=(msg, 1))
def receive_nw_closed(self, msg):
self.machine.switch_controller.process_switch(state=1,
num=(msg, 1))
def receive_local_open(self, msg):
self.machine.switch_controller.process_switch(state=0,
num=(msg, 0))
def receive_local_closed(self, msg):
self.machine.switch_controller.process_switch(state=1,
num=(msg, 0))
def receive_sa(self, msg):
self.log.info("Received SA: %s", msg)
hw_states = dict()
num_local, local_states, num_nw, nw_states = msg.split(',')
for offset, byte in enumerate(bytearray.fromhex(nw_states)):
for i in range(8):
num = Util.int_to_hex_string((offset * 8) + i)
if byte & (2**i):
hw_states[(num, 1)] = 1
else:
hw_states[(num, 1)] = 0
for offset, byte in enumerate(bytearray.fromhex(local_states)):
for i in range(8):
num = Util.int_to_hex_string((offset * 8) + i)
if byte & (2**i):
hw_states[(num, 0)] = 1
else:
hw_states[(num, 0)] = 0
self.hw_switch_data = hw_states
def configure_driver(self, config, device_type='coil'):
if not self.net_connection:
self.log.critical("A request was made to configure a FAST driver, "
"but no connection to a NET processor is "
"available")
sys.exit()
# If we have WPC driver boards, look up the driver number
if self.machine_type == 'wpc':
config['number'] = self.wpc_driver_map.get(
config['number_str'].upper())
if ('connection' in config and
config['connection'].lower() == 'network'):
config['connection'] = 1
else:
config['connection'] = 0 # local driver (default for WPC)
# If we have FAST IO boards, we need to make sure we have hex strings
elif self.machine_type == 'fast':
if self.config['config_number_format'] == 'int':
config['number'] = Util.int_to_hex_string(config['number'])
else:
config['number'] = Util.normalize_hex_string(config['number'])
# Now figure out the connection type
if ('connection' in config and
config['connection'].lower() == 'local'):
config['connection'] = 0
else:
config['connection'] = 1 # network driver (default for FAST)
else:
self.log.critical("Invalid machine type: {0{}}".format(
self.machine_type))
sys.exit()
return (FASTDriver(config, self.net_connection.send, self.machine),
(config['number'], config['connection']))
def configure_switch(self, config):
"""Configures the switch object for a FAST Pinball controller.
FAST Controllers support two types of switches: `local` and `network`.
Local switches are switches that are connected to the FAST controller
board itself, and network switches are those connected to a FAST I/O
board.
MPF needs to know which type of switch is this is. You can specify the
switch's connection type in the config file via the ``connection:``
setting (either ``local`` or ``network``).
If a connection type is not specified, this method will use some
intelligence to try to figure out which default should be used.
If the DriverBoard type is ``fast``, then it assumes the default is
``network``. If it's anything else (``wpc``, ``system11``, ``bally``,
etc.) then it assumes the connection type is ``local``. Connection types
can be mixed and matched in the same machine.
"""
if not self.net_connection:
self.log.critical("A request was made to configure a FAST switch, "
"but no connection to a NET processor is "
"available")
sys.exit()
if self.machine_type == 'wpc': # translate switch number to FAST switch
config['number'] = self.wpc_switch_map.get(
config['number_str'].upper())
if 'connection' not in config:
config['connection'] = 0 # local switch (default for WPC)
else:
config['connection'] = 1 # network switch
elif self.machine_type == 'fast':
if 'connection' not in config:
config['connection'] = 1 # network switch (default for FAST)
else:
config['connection'] = 0 # local switch
if self.config['config_number_format'] == 'int':
config['number'] = Util.int_to_hex_string(config['number'])
else:
config['number'] = Util.normalize_hex_string(config['number'])
# convert the switch number into a tuple which is:
# (switch number, connection)
config['number'] = (config['number'], config['connection'])
if not config['debounce_open']:
config['debounce_open'] = self.config['default_debounce_open']
if not config['debounce_close']:
config['debounce_close'] = self.config['default_debounce_close']
self.log.debug("FAST Switch hardware tuple: %s", config['number'])
switch = FASTSwitch(number=config['number'],
debounce_open=config['debounce_open'],
debounce_close=config['debounce_close'],
sender=self.net_connection.send)
return switch, config['number']
def configure_led(self, config):
if not self.rgb_connection:
self.log.critical("A request was made to configure a FAST LED, "
"but no connection to an LED processor is "
"available")
sys.exit()
if not self.flag_led_tick_registered:
self.machine.events.add_handler('timer_tick', self.update_leds)
self.flag_led_tick_registered = True
# if the LED number is in <channel> - <led> format, convert it to a
# FAST hardware number
if '-' in config['number_str']:
num = config['number_str'].split('-')
config['number'] = (int(num[0]) * 64) + int(num[1])
self.config['config_number_format'] = 'int'
else:
config['number'] = str(config['number'])
if self.config['config_number_format'] == 'int':
config['number'] = Util.int_to_hex_string(config['number'])
else:
config['number'] = Util.normalize_hex_string(config['number'])
this_fast_led = FASTDirectLED(config['number'])
self.fast_leds.add(this_fast_led)
return this_fast_led
def configure_gi(self, config):
if not self.net_connection:
self.log.critical("A request was made to configure a FAST GI, "
"but no connection to a NET processor is "
"available")
sys.exit()
if self.machine_type == 'wpc': # translate switch number to FAST switch
config['number'] = self.wpc_gi_map.get(config['number_str'].upper())
return (FASTGIString(config['number'], self.net_connection.send),
config['number'])
def configure_matrixlight(self, config):
if not self.net_connection:
self.log.critical("A request was made to configure a FAST matrix "
"light, but no connection to a NET processor is "
"available")
sys.exit()
if self.machine_type == 'wpc': # translate number to FAST light num
config['number'] = self.wpc_light_map.get(
config['number_str'].upper())
elif self.config['config_number_format'] == 'int':
config['number'] = Util.int_to_hex_string(config['number'])
else:
config['number'] = Util.normalize_hex_string(config['number'])
return (FASTMatrixLight(config['number'], self.net_connection.send),
config['number'])
def configure_dmd(self):
"""Configures a hardware DMD connected to a FAST controller."""
if not self.dmd_connection:
self.log.warning("A request was made to configure a FAST DMD, "
"but no connection to a DMD processor is "
"available. No hardware DMD will be used.")
return FASTDMD(self.machine, self.null_dmd_sender)
return FASTDMD(self.machine, self.dmd_connection.send)
def null_dmd_sender(self, *args, **kwargs):
pass
def tick(self):
while not self.receive_queue.empty():
self.process_received_message(self.receive_queue.get(False))
self.net_connection.send(self.watchdog_command)
def write_hw_rule(self, switch_obj, sw_activity, driver_obj, driver_action,
disable_on_release=True, drive_now=False,
**driver_settings_overrides):
"""Used to write (or update) a hardware rule to the FAST controller.
*Hardware Rules* are used to configure the hardware controller to
automatically change driver states based on switch changes. These rules
are completely handled by the hardware (i.e. with no interaction from
the Python game code). They're used for things that you want to happen
fast, like firing coils when flipper buttons are pushed, slingshots, pop
bumpers, etc.
You can overwrite existing hardware rules at any time to change or
remove them.
Args:
switch_obj: Which switch you're creating this rule for. The
parameter is a reference to the switch object itself.
sw_activity: Int which specifies whether this coil should fire when
the switch becomes active (1) or inactive (0)
driver_obj: Driver object this rule is being set for.
driver_action: String 'pulse' or 'hold' which describe what action
will be applied to this driver
drive_now: Should the hardware check the state of the switches when
this rule is first applied, and fire the coils if they should
be? Typically this is True, especially with flippers because you
want them to fire if the player is holding in the buttons when
the machine enables the flippers (which is done via several
calls to this method.)
"""
driver_settings = deepcopy(driver_obj.hw_driver.driver_settings)
driver_settings.update(driver_obj.hw_driver.merge_driver_settings(
**driver_settings_overrides))
self.log.debug("Setting HW Rule. Switch: %s, Switch_action: %s, Driver:"
" %s, Driver settings: %s", switch_obj.name, sw_activity,
driver_obj.name, driver_settings)
control = 0x01 # Driver enabled
if drive_now:
control += 0x08
if sw_activity == 0:
control += 0x10
control = Util.int_to_hex_string(int(control))
# todo need to implement disable_on_release
if driver_action == 'pulse':
mode = '10' # Mode 10 settings
param1 = driver_settings['pulse_ms'] # initial pulse ms
param2 = driver_settings['pwm1'] # intial pwm
param3 = '00' # pulse 2 time
param4 = '00' # pulse 2 pwm
param5 = driver_settings['recycle_ms'] # recycle ms
elif driver_action == 'hold':
mode = '18' # Mode 18 settings
param1 = driver_settings['pulse_ms'] # intiial pulse ms
param2 = driver_settings['pwm1'] # intial pwm
param3 = driver_settings['pwm2'] # hold pwm
param4 = driver_settings['recycle_ms'] # recycle ms
param5 = '00' # not used with Mode 18
elif driver_action == 'timed_hold':
# fast hold time is ms*100
hold_value = driver_settings['activation_time']
mode = '70' # Mode 70 settings
param1 = driver_settings['pulse_ms'] # intiial pulse ms
param2 = driver_settings['pwm1'] # intial pwm
param3 = hold_value # hold time
param4 = driver_settings['pwm2'] # hold pwm
param5 = driver_settings['recycle_ms'] # recycle ms
else:
raise ValueError("Invalid driver action: '%s'. Expected 'hold', "
"'timed_hold', or 'pulse'" % (driver_action))
self.hw_rules[driver_obj] = {'mode': mode,
'param1': param1,
'param2': param2,
'param3': param3,
'param4': param4,
'param5': param5,
'switch': switch_obj.number}
cmd = (driver_settings['config_cmd'] +
driver_obj.number[0] + ',' +
control + ',' +
switch_obj.number[0] + ',' +
mode + ',' +
param1 + ',' +
param2 + ',' +
param3 + ',' +
param4 + ',' +
param5)
driver_obj.autofire = cmd
self.log.debug("Writing hardware rule: %s", cmd)
self.net_connection.send(cmd)
def clear_hw_rule(self, sw_name):
"""Clears a hardware rule.
This is used if you want to remove the linkage between a switch and
some driver activity. For example, if you wanted to disable your
flippers (so that a player pushing the flipper buttons wouldn't cause
the flippers to flip), you'd call this method with your flipper button
as the *sw_num*.
Args:
sw_name: The string name of the switch whose rule you want to clear.
"""
sw_num = self.machine.switches[sw_name].number
# find the rule(s) based on this switch
coils = [k for k, v in self.hw_rules.iteritems() if v['switch'] == sw_num]
self.log.debug("Clearing HW Rule for switch: %s %s, coils: %s", sw_name,
sw_num, coils)
for coil in coils:
del self.hw_rules[coil]
driver_settings = coil.hw_driver.driver_settings
cmd = (driver_settings['config_cmd'] +
driver_settings['number'] + ',' +
'81')
coil.autofire = None
self.log.debug("Clearing hardware rule: %s", cmd)
self.net_connection.send(cmd)
class FASTSwitch(object):
def __init__(self, number, debounce_open, debounce_close, sender):
self.log = logging.getLogger('FASTSwitch')
self.number = number[0]
self.connection = number[1]
self.send = sender
if self.connection:
cmd = 'SN:'
else:
cmd = 'SL:'
debounce_open = str(hex(debounce_open))[2:]
debounce_close = str(hex(debounce_close))[2:]
cmd += str(self.number) + ',01,' + debounce_open + ',' + debounce_close
self.send(cmd)
class FASTDriver(object):
"""Base class for drivers connected to a FAST Controller.
"""
def __init__(self, config, sender, machine):
"""
"""
self.autofire = None
self.config = dict()
self.driver_settings = self.create_driver_settings(machine, **config)
self.driver_settings['mode'] = '10' # pulsed
self.log = logging.getLogger('FASTDriver')
# Number is already normalized FAST hex string at this point
self.driver_settings['number'] = config['number']
self.send = sender
if config['connection'] == 1:
self.driver_settings['config_cmd'] = 'DN:'
self.driver_settings['trigger_cmd'] = 'TN:'
else:
self.driver_settings['config_cmd'] = 'DL:'
self.driver_settings['trigger_cmd'] = 'TL:'
self.driver_settings.update(self.merge_driver_settings(**config))
self.log.debug("Driver Settings: %s", self.driver_settings)
def create_driver_settings(self, machine, pulse_ms=None, **kwargs):
return_dict = dict()
if pulse_ms is None:
pulse_ms = machine.config['mpf']['default_pulse_ms']
return_dict['pulse_ms'] = Util.int_to_hex_string(pulse_ms)
return_dict['pwm1'] = 'ff'
return_dict['pwm2'] = 'ff'
return_dict['recycle_ms'] = '00'
return return_dict
def merge_driver_settings(self,
pulse_ms=None,
pwm_on_ms=None,
pwm_off_ms=None,
pulse_power=None,
hold_power=None,
pulse_power32=None,
hold_power32=None,
pulse_pwm_mask=None,
hold_pwm_mask=None,
recycle_ms=None,
activation_time=None,
**kwargs
):
if pwm_on_ms:
raise ValueError("The setting 'pwm_on_ms' is not valid with the "
"FAST platform. Use a hold_power or hold_pwm_mask"
" instead.")
if pwm_off_ms:
raise ValueError("The setting 'pwm_off_ms' is not valid with the "
"FAST platform. Use a hold_power or hold_pwm_mask"
" instead.")
if pulse_power32:
raise NotImplementedError('"pulse_power32" has not been '
'implemented yet')
if hold_power32:
raise NotImplementedError('"hold_power32" has not been '
'implemented yet')
return_dict = dict()
return_dict['pwm32'] = None
if activation_time is not None:
if activation_time > 25500:
raise ValueError('Max FAST timed_hold time is 25.5s')
# FAST activation times are ms * 100
return_dict['activation_time'] = str(activation_time / 100)
if recycle_ms is not None:
return_dict['recycle_ms'] = (Util.int_to_hex_string(recycle_ms))
if pulse_ms is not None:
return_dict['pulse_ms'] = Util.int_to_hex_string(pulse_ms)
if pulse_pwm_mask:
pulse_pwm_mask = str(pulse_pwm_mask)
if len(pulse_pwm_mask) == 32:
return_dict['pwm1'] = Util.bin_str_to_hex_str(pulse_pwm_mask, 8)
elif len(pulse_pwm_mask) == 8:
return_dict['pwm1'] = Util.bin_str_to_hex_str(pulse_pwm_mask, 2)
else:
raise ValueError("pulse_pwm_mask must either be 8 or 32 bits")
elif pulse_power32 is not None:
return_dict['pwm32'] = Util.pwm32_to_hex_string(pulse_power32)
elif pulse_power is not None:
return_dict['pwm1'] = Util.pwm8_to_hex_string(pulse_power)
if hold_pwm_mask:
hold_pwm_mask = str(hold_pwm_mask)
if len(hold_pwm_mask) == 32:
return_dict['pwm2'] = Util.bin_str_to_hex_str(hold_pwm_mask, 8)
elif len(hold_pwm_mask) == 8:
return_dict['pwm2'] = Util.bin_str_to_hex_str(hold_pwm_mask, 2)
else:
raise ValueError("hold_pwm_mask must either be 8 or 32 bits")
elif hold_power32 is not None:
return_dict['pwm32'] = Util.pwm32_to_hex_string(hold_power32)
elif hold_power is not None:
return_dict['pwm2'] = Util.pwm8_to_hex_string(hold_power)
return return_dict
def disable(self):
"""Disables (turns off) this driver. """
cmd = (self.driver_settings['trigger_cmd'] +
self.driver_settings['number'] + ',' + '02')
self.log.debug("Sending Disable Command: %s", cmd)
self.send(cmd)
self.check_auto()
def enable(self):
"""Enables (turns on) this driver. """
if self.autofire:
# If this driver is also configured for an autofire rule, we just
# manually trigger it with the trigger_cmd and manual on ('03')
cmd = (self.driver_settings['trigger_cmd'] +
self.driver_settings['number'] + ',' +
'03')
self.log.warning("Recived a command to enable this driver, but "
"this driver is configured with an autofire rule,"
" so this enable will reset that rule. We need to"
" change this behavior...")
else:
# Otherwise we send a full config command, trigger C1 (logic triggered
# and drive now) switch ID 00, mode 18 (latched)
cmd = (self.driver_settings['config_cmd'] +
self.driver_settings['number'] +
',C1,00,18,' +
self.driver_settings['pulse_ms'] + ',' +
self.driver_settings['pwm1'] + ',' +
self.driver_settings['pwm2'] + ',' +
self.driver_settings['recycle_ms'])
# todo pwm32
self.log.debug("Sending Enable Command: %s", cmd)
self.send(cmd)
# todo change hold to pulse with re-ups
#self.check_auto()
def pulse(self, milliseconds=None):
"""Pulses this driver. """
if not milliseconds:
hex_ms_string = self.driver_settings['pulse_ms']
else:
hex_ms_string = Util.int_to_hex_string(milliseconds)
if self.autofire:
cmd = (self.driver_settings['trigger_cmd'] +
self.driver_settings['number'] + ',' +
'01')
if milliseconds:
self.log.debug("Received command to pulse driver for %sms, but"
"this driver is configured with an autofire rule"
", so that pulse value will be used instead.")
else:
cmd = (self.driver_settings['config_cmd'] +
self.driver_settings['number'] +
',89,00,10,' +
hex_ms_string + ',' +
self.driver_settings['pwm1'] + ',00,00,' +
self.driver_settings['recycle_ms'])