/
fast.py
834 lines (673 loc) · 33.7 KB
/
fast.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
"""FAST hardware platform.
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.
"""
import asyncio
import logging
import os
from copy import deepcopy
from distutils.version import StrictVersion
from typing import Dict, Set
from mpf.platforms.fast.fast_io_board import FastIoBoard
from mpf.platforms.fast.fast_servo import FastServo
from mpf.platforms.fast import fast_defines
from mpf.platforms.fast.fast_dmd import FASTDMD
from mpf.platforms.fast.fast_driver import FASTDriver
from mpf.platforms.fast.fast_gi import FASTGIString
from mpf.platforms.fast.fast_led import FASTDirectLED, FASTDirectLEDChannel
from mpf.platforms.fast.fast_light import FASTMatrixLight
from mpf.platforms.fast.fast_serial_communicator import FastSerialCommunicator
from mpf.platforms.fast.fast_switch import FASTSwitch
from mpf.core.platform import ServoPlatform, DmdPlatform, SwitchPlatform, DriverPlatform, LightsPlatform,\
DriverSettings, SwitchSettings, DriverConfig, SwitchConfig
from mpf.core.utility_functions import Util
# pylint: disable-msg=too-many-instance-attributes
from mpf.platforms.interfaces.light_platform_interface import LightPlatformInterface
class FastHardwarePlatform(ServoPlatform, LightsPlatform, DmdPlatform,
SwitchPlatform, DriverPlatform):
"""Platform class for the FAST hardware controller."""
__slots__ = ["dmd_connection", "net_connection", "rgb_connection", "serial_connections", "fast_leds",
"flag_led_tick_registered", "config", "machine_type", "hw_switch_data", "io_boards", "fast_commands"]
def __init__(self, machine):
"""Initialise fast hardware platform.
Args:
machine: The main ``MachineController`` instance.
"""
super().__init__(machine)
self.log = logging.getLogger('FAST')
self.log.debug("Configuring FAST hardware.")
self.features['tickless'] = True
self.dmd_connection = None
self.net_connection = None
self.rgb_connection = None
self.serial_connections = set() # type: Set[FastSerialCommunicator]
self.fast_leds = {}
self.flag_led_tick_registered = False
self.config = None
self.machine_type = None
self.hw_switch_data = None
self.io_boards = {} # type: Dict[int, FastIoBoard]
self.fast_commands = {'ID': lambda x, y: None, # processor ID
'WX': lambda x, y: None, # watchdog
'NI': lambda x, y: None, # node ID
'RX': lambda x, y: None, # RGB cmd received
'RA': lambda x, y: None, # RGB all cmd received
'RF': lambda x, y: None, # RGB fade cmd received
'DX': lambda x, y: None, # DMD cmd received
'SX': lambda x, y: None, # sw config received
'LX': lambda x, y: None, # lamp cmd received
'PX': lambda x, y: None, # segment cmd received
'WD': lambda x, y: None, # watchdog
'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 cls
'!B': self.receive_bootloader, # nano bootloader message
}
def get_info_string(self):
"""Dump infos about boards."""
infos = ""
if not self.net_connection:
infos += "No connection to the NET CPU.\n"
else:
infos += "NET CPU: {} {} {}\n".format(
self.net_connection.remote_processor,
self.net_connection.remote_model,
self.net_connection.remote_firmware)
if not self.rgb_connection:
infos += "No connection to the RGB CPU.\n"
else:
infos += "RGB CPU: {} {} {}\n".format(
self.rgb_connection.remote_processor,
self.rgb_connection.remote_model,
self.rgb_connection.remote_firmware)
if not self.dmd_connection:
infos += "No connection to the DMD CPU.\n"
else:
infos += "DMD CPU: {} {} {}\n".format(
self.dmd_connection.remote_processor,
self.dmd_connection.remote_model,
self.dmd_connection.remote_firmware)
infos += "\nBoards:\n"
for board in self.io_boards.values():
infos += board.get_description_string() + "\n"
return infos
def _update_net(self) -> str:
"""Update NET CPU."""
infos = ""
if not self.net_connection:
infos += "No NET CPU connected. Cannot update.\n"
return infos
infos += "NET CPU is version {}\n".format(self.net_connection.remote_firmware)
max_firmware = self.net_connection.remote_firmware
update_config = None
for update in self.config['firmware_updates']:
if StrictVersion(update['version']) > StrictVersion(max_firmware) and update['type'] == "net":
update_config = update
if not update_config:
infos += "Firmware is up to date. Will not update.\n"
return infos
infos += "Found an update to version {} for the NET CPU. Will flash file {}\n".format(
update_config['version'], update_config['file'])
firmware_file = os.path.join(self.machine.machine_path, update_config['file'])
try:
with open(firmware_file) as f:
update_string = f.read().replace("\n", "\r")
except FileNotFoundError:
infos += "Could not find update file.\b"
return infos
self.net_connection.writer.write(update_string.encode())
infos += "Update done.\n"
return infos
def update_firmware(self) -> str:
"""Upgrade the firmware of the CPUs."""
return self._update_net()
@asyncio.coroutine
def initialize(self):
"""Initialise platform."""
self.config = self.machine.config['fast']
self.machine.config_validator.validate_config("fast", self.config)
if self.config['debug']:
self.debug = True
self.machine_type = (
self.machine.config['hardware']['driverboards'].lower())
if self.machine_type == 'wpc':
self.debug_log("Configuring the FAST Controller for WPC driver "
"board")
else:
self.debug_log("Configuring FAST Controller for FAST IO boards.")
yield from self._connect_to_hardware()
def stop(self):
"""Stop platform and close connections."""
if self.net_connection:
# set watchdog to expire in 1ms
self.net_connection.writer.write(b'WD:1\r')
self.net_connection.stop()
self.net_connection = None
if self.rgb_connection:
self.rgb_connection.writer.write(b'BL:AA55\r') # reset CPU using bootloader
self.rgb_connection.stop()
self.rgb_connection = None
if self.dmd_connection:
self.dmd_connection.writer.write(b'BL:AA55\r') # reset CPU using bootloader
self.dmd_connection.stop()
self.dmd_connection = None
self.serial_connections = set()
@asyncio.coroutine
def start(self):
"""Start listening for commands and schedule watchdog."""
self.machine.clock.schedule_interval(self._update_watchdog, self.config['watchdog'] / 2000)
for connection in self.serial_connections:
yield from connection.start_read_loop()
def __repr__(self):
"""Return str representation."""
return '<Platform.FAST>'
def register_io_board(self, board):
"""Register an IO board.
Args:
board: 'mpf.platform.fast.fast_io_board.FastIoBoard' to register
"""
if board.node_id in self.io_boards:
raise AssertionError("Duplicate node_id")
self.io_boards[board.node_id] = board
def _update_watchdog(self):
"""Send Watchdog command."""
self.net_connection.send('WD:' + str(hex(self.config['watchdog']))[2:])
def process_received_message(self, msg: str, remote_processor: str):
"""Send an incoming message from the FAST controller to the proper method for servicing.
Args:
msg: messaged which was received
"""
if msg == "!SRE":
# ignore system interrupt
self.log.info("Received system interrupt from %s.", remote_processor)
return
if msg[2:3] == ':':
cmd = msg[0:2]
payload = msg[3:].replace('\r', '')
else: # pragma: no cover
self.log.warning("Received malformed message: %s from %s", msg, remote_processor)
return
# Can't use try since it swallows too many errors for now
if cmd in self.fast_commands:
self.fast_commands[cmd](payload, remote_processor)
else: # pragma: no cover
self.log.warning("Received unknown serial command? %s from %s.", msg, remote_processor)
@asyncio.coroutine
def _connect_to_hardware(self):
"""Connect to each port from the config.
This process will cause the connection threads to figure out which processor they've connected to
and to register themselves.
"""
for port in self.config['ports']:
comm = FastSerialCommunicator(platform=self, port=port,
baud=self.config['baud'])
yield from comm.connect()
self.serial_connections.add(comm)
def register_processor_connection(self, name: str, communicator):
"""Register processor.
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.
Args:
communicator: communicator object
name: name of processor
"""
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('RF:0')
self.rgb_connection.send('RA:000000') # turn off all LEDs
self.rgb_connection.send('RF:{}'.format(
Util.int_to_hex_string(self.config['hardware_led_fade_time'])))
def update_leds(self):
"""Update 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.
"""
dirty_leds = [led for led in self.fast_leds.values() if led.dirty]
if dirty_leds:
msg = 'RS:' + ','.join(["%s%s" % (led.number, led.current_color) for led in dirty_leds])
self.rgb_connection.send(msg)
@asyncio.coroutine
def get_hw_switch_states(self):
"""Return hardware states."""
return self.hw_switch_data
def receive_nw_open(self, msg, remote_processor):
"""Process network switch open.
Args:
msg: switch number
"""
assert remote_processor == "NET"
self.machine.switch_controller.process_switch_by_num(state=0,
num=(msg, 1),
platform=self)
def receive_nw_closed(self, msg, remote_processor):
"""Process network switch closed.
Args:
msg: switch number
"""
assert remote_processor == "NET"
self.machine.switch_controller.process_switch_by_num(state=1,
num=(msg, 1),
platform=self)
def receive_local_open(self, msg, remote_processor):
"""Process local switch open.
Args:
msg: switch number
"""
assert remote_processor == "NET"
self.machine.switch_controller.process_switch_by_num(state=0,
num=(msg, 0),
platform=self)
def receive_local_closed(self, msg, remote_processor):
"""Process local switch closed.
Args:
msg: switch number
"""
assert remote_processor == "NET"
self.machine.switch_controller.process_switch_by_num(state=1,
num=(msg, 0),
platform=self)
def receive_sa(self, msg, remote_processor):
"""Receive all switch states.
Args:
msg: switch states as bytearray
"""
assert remote_processor == "NET"
self.debug_log("Received SA: %s", msg)
hw_states = dict()
_, local_states, _, 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
@staticmethod
def convert_number_from_config(number):
"""Convert a number from config format to hex."""
return Util.int_to_hex_string(number)
def _parse_driver_number(self, number):
try:
board_str, driver_str = number.split("-")
except ValueError:
total_drivers = 0
for board_obj in self.io_boards.values():
total_drivers += board_obj.driver_count
index = self.convert_number_from_config(number)
if int(index, 16) >= total_drivers:
raise AssertionError("Driver {} does not exist. Only {} drivers found. Driver number: {}".format(
int(index, 16), total_drivers, number))
return index
board = int(board_str)
driver = int(driver_str)
if board not in self.io_boards:
raise AssertionError("Board {} does not exist for driver {}".format(board, number))
if self.io_boards[board].driver_count <= driver:
raise AssertionError("Board {} only has {} drivers. Driver: {}".format(
board, self.io_boards[board].driver_count, number))
index = 0
for board_number, board_obj in self.io_boards.items():
if board_number >= board:
continue
index += board_obj.driver_count
return Util.int_to_hex_string(index + driver)
def configure_driver(self, config: DriverConfig, number: str, platform_settings: dict) -> FASTDriver:
"""Configure a driver.
Args:
config: Driver config.
Returns: Driver object
"""
# dont modify the config. make a copy
platform_settings = deepcopy(platform_settings)
if not self.net_connection:
raise AssertionError('A request was made to configure a FAST '
'driver, but no connection to a NET processor'
'is available')
if not number:
raise AssertionError("Driver needs a number")
# If we have WPC driver boards, look up the driver number
if self.machine_type == 'wpc':
number = fast_defines.wpc_driver_map.get(number.upper())
if ('connection' in platform_settings and
platform_settings['connection'].lower() == 'network'):
platform_settings['connection'] = 1
else:
platform_settings['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':
number = self._parse_driver_number(number)
# Now figure out the connection type
if ('connection' in platform_settings and
platform_settings['connection'].lower() == 'local'):
platform_settings['connection'] = 0
else:
platform_settings['connection'] = 1 # network driver (default for FAST)
else:
raise AssertionError("Invalid machine type: {}".format(
self.machine_type))
return FASTDriver(config, self, number, platform_settings)
def _parse_servo_number(self, number):
try:
board_str, servo_str = number.split("-")
except ValueError:
return self.convert_number_from_config(number)
board = int(board_str)
servo = int(servo_str)
if board < 0:
raise AssertionError("Board needs to be positive.")
if servo < 0 or servo > 5:
raise AssertionError("Servo number has to be between 0 and 5.")
# every servo board supports exactly 6 servos
return self.convert_number_from_config(board * 6 + servo)
@asyncio.coroutine
def configure_servo(self, number: str):
"""Configure a servo.
Args:
number: Number of servo
Returns: Servo object.
"""
number_int = self._parse_servo_number(str(number))
return FastServo(number_int, self.net_connection)
def _parse_switch_number(self, number):
try:
board_str, switch_str = number.split("-")
except ValueError:
total_switches = 0
for board_obj in self.io_boards.values():
total_switches += board_obj.switch_count
index = self.convert_number_from_config(number)
if int(index, 16) >= total_switches:
raise AssertionError("Switch {} does not exist. Only {} switches found. Switch number: {}".format(
int(index, 16), total_switches, number))
return index
board = int(board_str)
switch = int(switch_str)
if board not in self.io_boards:
raise AssertionError("Board {} does not exist for switch {}".format(board, number))
if self.io_boards[board].switch_count <= switch:
raise AssertionError("Board {} only has {} switches. Switch: {}".format(
board, self.io_boards[board].switch_count, number))
index = 0
for board_number, board_obj in self.io_boards.items():
if board_number >= board:
continue
index += board_obj.switch_count
return Util.int_to_hex_string(index + switch)
def configure_switch(self, number: str, config: SwitchConfig, platform_config: dict) -> FASTSwitch:
"""Configure 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.
Args:
config: Switch config.
Returns: Switch object.
"""
if not number:
raise AssertionError("Switch needs a number")
if not self.net_connection:
raise AssertionError("A request was made to configure a FAST "
"switch, but no connection to a NET processor"
"is available")
if self.machine_type == 'wpc': # translate switch num to FAST switch
number = fast_defines.wpc_switch_map.get(
str(number).upper())
if 'connection' not in platform_config:
platform_config['connection'] = 0 # local switch (default for WPC)
else:
platform_config['connection'] = 1 # network switch
elif self.machine_type == 'fast':
if 'connection' not in platform_config:
platform_config['connection'] = 1 # network switch (default for FAST)
else:
platform_config['connection'] = 0 # local switch
try:
number = self._parse_switch_number(number)
except ValueError:
raise AssertionError("Could not parse switch number {}. Seems "
"to be not a valid switch number for the"
"FAST platform.".format(number))
# convert the switch number into a tuple which is:
# (switch number, connection)
number_tuple = (number, platform_config['connection'])
self.debug_log("FAST Switch hardware tuple: %s", number)
switch = FASTSwitch(config=config, number_tuple=number_tuple,
platform=self, platform_settings=platform_config)
return switch
def configure_light(self, number, subtype, platform_settings) -> LightPlatformInterface:
"""Configure light in platform."""
if not self.net_connection:
raise AssertionError('A request was made to configure a FAST Light, '
'but no connection to a NET processor is '
'available')
if subtype == "gi":
return FASTGIString(number, self.net_connection.send, self.machine,
int(1 / self.machine.config['mpf']['default_light_hw_update_hz'] * 1000))
elif subtype == "matrix":
return FASTMatrixLight(number, self.net_connection.send, self.machine,
int(1 / self.machine.config['mpf']['default_light_hw_update_hz'] * 1000), self)
elif not subtype or subtype == "led":
if not self.flag_led_tick_registered:
# Update leds every frame
self.machine.clock.schedule_interval(self.update_leds,
1 / self.machine.config['mpf']['default_light_hw_update_hz'])
self.flag_led_tick_registered = True
number_str, channel = number.split("-")
if number_str not in self.fast_leds:
self.fast_leds[number_str] = FASTDirectLED(
number_str, int(self.config['hardware_led_fade_time']))
fast_led_channel = FASTDirectLEDChannel(self.fast_leds[number_str], channel)
return fast_led_channel
else:
raise AssertionError("Unknown subtype {}".format(subtype))
def parse_light_number_to_channels(self, number: str, subtype: str):
"""Parse light channels from number string."""
if subtype == "gi":
if self.machine_type == 'wpc': # translate number to FAST GI number
number = fast_defines.wpc_gi_map.get(str(number).upper())
else:
number = self.convert_number_from_config(number)
return [
{
"number": number
}
]
elif subtype == "matrix":
if self.machine_type == 'wpc': # translate number to FAST light num
number = fast_defines.wpc_light_map.get(str(number).upper())
else:
number = self.convert_number_from_config(number)
return [
{
"number": number
}
]
elif not subtype or subtype == "led":
# if the LED number is in <channel> - <led> format, convert it to a
# FAST hardware number
if '-' in str(number):
num = str(number).split('-')
number = Util.int_to_hex_string((int(num[0]) * 64) + int(num[1]))
else:
number = self.convert_number_from_config(number)
return [
{
"number": number + "-0"
},
{
"number": number + "-1"
},
{
"number": number + "-2"
},
]
else:
raise AssertionError("Unknown subtype {}".format(subtype))
def configure_dmd(self):
"""Configure a hardware DMD connected to a FAST controller."""
if not self.dmd_connection:
raise AssertionError("A request was made to configure a FAST DMD, "
"but no connection to a DMD processor is "
"available.")
return FASTDMD(self.machine, self.dmd_connection.send)
@classmethod
def get_coil_config_section(cls):
"""Return coil config section."""
return "fast_coils"
@classmethod
def get_switch_config_section(cls):
"""Return switch config section."""
return "fast_switches"
def _check_switch_coil_combincation(self, switch, coil):
switch_number = int(switch.hw_switch.number[0], 16)
coil_number = int(coil.hw_driver.number, 16)
# first 8 switches always work
if 0 <= switch_number <= 7:
return
switch_index = 0
coil_index = 0
for board_obj in self.io_boards.values():
# if switch and coil are on the same board we are fine
if switch_index <= switch_number < switch_index + board_obj.switch_count and \
coil_index <= coil_number < coil_index + board_obj.driver_count:
return
coil_index += board_obj.driver_count
switch_index += board_obj.switch_count
raise AssertionError("Driver {} and switch {} are on different boards. Cannot apply rule!".format(
coil.hw_driver.number, switch.hw_switch.number))
def set_pulse_on_hit_and_release_rule(self, enable_switch, coil):
"""Set pulse on hit and release rule to driver."""
self.debug_log("Setting Pulse on hit and release HW Rule. Switch: %s,"
"Driver: %s", enable_switch.hw_switch.number,
coil.hw_driver.number)
self._check_switch_coil_combincation(enable_switch, coil)
driver = coil.hw_driver
cmd = '{}{},{},{},18,{},{},00,{},00'.format(
driver.get_config_cmd(),
coil.hw_driver.number,
driver.get_control_for_cmd(enable_switch),
enable_switch.hw_switch.number[0],
Util.int_to_hex_string(coil.pulse_settings.duration),
driver.get_pwm_for_cmd(coil.pulse_settings.power),
driver.get_recycle_ms_for_cmd(coil.recycle, coil.pulse_settings.duration))
enable_switch.hw_switch.configure_debounce(enable_switch.debounce)
driver.set_autofire(cmd, coil.pulse_settings.duration, coil.pulse_settings.power, 0)
def set_pulse_on_hit_and_enable_and_release_and_disable_rule(self, enable_switch, disable_switch, coil):
"""Set pulse on hit and enable and release and disable rule on driver."""
# Potential command from Dave:
# Command
# [DL/DN]:<DRIVER_ID>,<CONTROL>,<SWITCH_ID_ON>,<75>,<SWITCH_ID_OFF>,<Driver On Time1>,<Driver On Time2 X 100mS>,
# <PWM2><Driver Rest Time><CR>#
# SWITCH_ID_ON would be the flipper switch
# SWITCH_ID_OFF would be the EOS switch.
# So for the flipper, Driver On Time1 will = the maximum time the coil can be held on if the EOS fails.
# Driver On Time2 X 100mS would not be used for a flipper, so set it to 0.
# And PWM2 should be left on full 0xff unless you need less power for some reason.
self.debug_log("Setting Pulse on hit and release with HW Rule. Switch:"
"%s, Driver: %s", enable_switch.hw_switch.number,
coil.hw_driver.number)
# TODO: hold does not work here
self._check_switch_coil_combincation(enable_switch, coil)
self._check_switch_coil_combincation(disable_switch, coil)
driver = coil.hw_driver
cmd = '{}{},{},{},75,{},{},00,{},{}'.format(
driver.get_config_cmd(),
coil.hw_driver.number,
driver.get_control_for_cmd(enable_switch, disable_switch),
enable_switch.hw_switch.number[0],
disable_switch.hw_switch.number[0],
Util.int_to_hex_string(coil.pulse_settings.duration),
driver.get_pwm_for_cmd(coil.pulse_settings.power),
driver.get_recycle_ms_for_cmd(coil.recycle, coil.pulse_settings.duration))
enable_switch.hw_switch.configure_debounce(enable_switch.debounce)
disable_switch.hw_switch.configure_debounce(disable_switch.debounce)
driver.set_autofire(cmd, coil.pulse_settings.duration, coil.pulse_settings.power, 0)
def set_pulse_on_hit_rule(self, enable_switch: SwitchSettings, coil: DriverSettings):
"""Set pulse on hit rule on driver."""
self.debug_log("Setting Pulse on hit and release HW Rule. Switch: %s,"
"Driver: %s", enable_switch.hw_switch.number,
coil.hw_driver.number)
self._check_switch_coil_combincation(enable_switch, coil)
driver = coil.hw_driver
cmd = '{}{},{},{},10,{},{},00,00,{}'.format(
driver.get_config_cmd(),
coil.hw_driver.number,
driver.get_control_for_cmd(enable_switch),
enable_switch.hw_switch.number[0],
Util.int_to_hex_string(coil.pulse_settings.duration),
driver.get_pwm_for_cmd(coil.pulse_settings.power),
driver.get_recycle_ms_for_cmd(coil.recycle, coil.pulse_settings.duration))
enable_switch.hw_switch.configure_debounce(enable_switch.debounce)
driver.set_autofire(cmd, coil.pulse_settings.duration, coil.pulse_settings.power, 0)
def set_pulse_on_hit_and_enable_and_release_rule(self, enable_switch: SwitchSettings, coil: DriverSettings):
"""Set pulse on hit and enable and relase rule on driver."""
self.debug_log("Setting Pulse on hit and enable and release HW Rule. "
"Switch: %s, Driver: %s",
enable_switch.hw_switch.number, coil.hw_driver.number)
self._check_switch_coil_combincation(enable_switch, coil)
driver = coil.hw_driver
cmd = '{}{},{},{},18,{},{},{},{},00'.format(
driver.get_config_cmd(),
driver.number,
driver.get_control_for_cmd(enable_switch),
enable_switch.hw_switch.number[0],
Util.int_to_hex_string(coil.pulse_settings.duration),
driver.get_pwm_for_cmd(coil.pulse_settings.power),
driver.get_pwm_for_cmd(coil.hold_settings.power),
driver.get_recycle_ms_for_cmd(coil.recycle, coil.pulse_settings.duration))
enable_switch.hw_switch.configure_debounce(enable_switch.debounce)
driver.set_autofire(cmd, coil.pulse_settings.duration, coil.pulse_settings.power, coil.hold_settings.power)
def clear_hw_rule(self, switch, coil):
"""Clear 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:
switch: The switch whose rule you want to clear.
coil: The coil whose rule you want to clear.
"""
self.debug_log("Clearing HW Rule for switch: %s, coils: %s",
switch.hw_switch.number, coil.hw_driver.number)
# TODO: check that the rule is switch + coil and not another switch + this coil
driver = coil.hw_driver
driver.clear_autofire(driver.get_config_cmd(), driver.number)
def receive_bootloader(self, msg, remote_processor):
"""Process bootloader message."""
self.debug_log("Got Bootloader message: %s from", msg, remote_processor)
if msg in ('00', '02'):
self.error_log("The FAST %s processor rebooted. Unfortunately, that means that it lost all it's state "
"(such as hardware rules or switch configs). This is likely cause by an unstable power "
"supply but it might as well be a firmware bug. MPF will exit now.", remote_processor)
self.machine.stop("FAST {} rebooted during game".format(remote_processor))