/
rpi.py
295 lines (224 loc) · 11.1 KB
/
rpi.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
"""Platform to control the hardware of a Raspberry Pi."""
import asyncio
from typing import Optional
from mpf.platforms.interfaces.i2c_platform_interface import I2cPlatformInterface
from mpf.core.delays import DelayManager
from mpf.core.utility_functions import Util
from mpf.platforms.interfaces.servo_platform_interface import ServoPlatformInterface
from mpf.platforms.interfaces.switch_platform_interface import SwitchPlatformInterface
from mpf.platforms.interfaces.driver_platform_interface import DriverPlatformInterface, PulseSettings, HoldSettings
from mpf.core.platform import SwitchPlatform, DriverPlatform, ServoPlatform, SwitchSettings, \
DriverSettings, DriverConfig, SwitchConfig, I2cPlatform, RepulseSettings
# apiogpio is not a requirement for MPF so we fail with a nice error when loading
try:
import apigpio
except ImportError:
apigpio = None
BOARD_NAME = "Raspberry Pi"
class RpiSwitch(SwitchPlatformInterface):
"""A switch on a RPI."""
def get_board_name(self):
"""Return name."""
return BOARD_NAME
class RpiDriver(DriverPlatformInterface):
"""An output on a Rasoberry Pi."""
def __init__(self, number, config, platform):
"""Initialize output."""
super().__init__(config, number)
self.platform = platform # type: RaspberryPiHardwarePlatform
self.gpio = int(self.number)
self.delay = DelayManager(self.platform.machine)
def get_board_name(self):
"""Return name."""
return BOARD_NAME
def pulse(self, pulse_settings: PulseSettings):
"""Pulse output."""
self.enable(pulse_settings, None)
def timed_enable(self, pulse_settings: PulseSettings, hold_settings: HoldSettings):
"""Pulse and enable the coil for an explicit duration."""
raise NotImplementedError
def enable(self, pulse_settings: PulseSettings, hold_settings: Optional[HoldSettings]):
"""Enable output."""
self.platform.send_command(self.platform.pi.write(self.gpio, 1))
if hold_settings and hold_settings.power == 1:
# do nothing. just keep driver enabled
pass
elif hold_settings and hold_settings.power > 0:
# schedule pwm
self.delay.add(pulse_settings.duration, self._pwm, hold_power=hold_settings.power)
else:
# no hold. disable after pulse
self.delay.add(pulse_settings.duration, self.disable)
def _pwm(self, hold_power):
"""Set up pwm."""
self.platform.send_command(self.platform.pi.set_PWM_dutycycle(self.gpio, hold_power * 255))
def disable(self):
"""Disable output."""
self.platform.send_command(self.platform.pi.write(self.gpio, 0))
# clear all delays
self.delay.clear()
class RpiServo(ServoPlatformInterface):
"""A servo connected to a RPI."""
def __init__(self, number, platform):
"""Initialize servo."""
self.gpio = int(number)
self.platform = platform # type: RaspberryPiHardwarePlatform
def go_to_position(self, position):
"""Move servo to position."""
# can be between 1000 and 2000us
position_translated = 1000 + position * 1000
self.platform.send_command(self.platform.pi.set_servo_pulsewidth(self.gpio, position_translated))
def stop(self):
"""Disable servo."""
self.platform.send_command(self.platform.pi.set_servo_pulsewidth(self.gpio, 0))
def set_speed_limit(self, speed_limit):
"""Not implemented."""
def set_acceleration_limit(self, acceleration_limit):
"""Not implemented."""
class RpiI2cDevice(I2cPlatformInterface):
"""A I2c device on a Rpi."""
def __init__(self, number: str, loop, platform) -> None:
"""Initialize i2c device on rpi."""
super().__init__(number)
self.loop = loop
self.pi = platform.pi
self.platform = platform
self.number = number
self.handle = None
async def open(self):
"""Open I2c port."""
self.handle = await self._get_i2c_handle(self.number)
@staticmethod
def _get_i2c_bus_address(address):
"""Split and return bus + address."""
if isinstance(address, int):
return 0, address
bus, address = address.split("-")
return int(bus), int(address)
async def _get_i2c_handle(self, address):
"""Get or open handle for i2c device via pigpio."""
bus_address, device_address = self._get_i2c_bus_address(address)
handle = await self.pi.i2c_open(bus_address, device_address)
return handle
def i2c_write8(self, register, value):
"""Write to i2c via pigpio."""
self.platform.send_command(self._i2c_write8_async(register, value))
async def _i2c_write8_async(self, register, value):
await self.pi.i2c_write_byte_data(self.handle, register, value)
async def i2c_read8(self, register):
"""Read from i2c via pigpio."""
return await self.pi.i2c_read_byte_data(self.handle, register)
async def i2c_read_block(self, register, count):
"""Read block via I2C."""
return await self.pi.i2c_read_i2c_block_data(self.handle, register, count)
class RaspberryPiHardwarePlatform(SwitchPlatform, DriverPlatform, ServoPlatform, I2cPlatform):
"""Control the hardware of a Raspberry Pi.
Works locally and remotely via network.
"""
def __init__(self, machine):
"""Initialize Raspberry Pi platform."""
super().__init__(machine)
if not apigpio:
raise AssertionError("To use the Raspberry Pi platform you need to install the apigpio extension. "
"Run: pip3 install apigpio-mpf.")
self.pi = None # type: Optional[apigpio.Pi]
# load config
self.config = self.machine.config_validator.validate_config("raspberry_pi", self.machine.config['raspberry_pi'])
self._switches = None # type: Optional[int]
self._cmd_queue = None # type: Optional[asyncio.Queue]
self._cmd_task = None # type: Optional[asyncio.Task]
self._configure_device_logging_and_debug("Raspberry Pi", self.config)
async def initialize(self):
"""Initialize platform."""
# create pi object and connect
self.pi = apigpio.Pi(self.machine.clock.loop)
await self.pi.connect((self.config['ip'], self.config['port']))
self._switches = await self.pi.read_bank_1()
self._cmd_queue = asyncio.Queue()
self._cmd_task = asyncio.create_task(self._run())
self._cmd_task.add_done_callback(Util.raise_exceptions)
def send_command(self, cmd):
"""Add a command to the command queue."""
self._cmd_queue.put_nowait(cmd)
async def _run(self):
"""Handle the command queue."""
while True:
# get next command
cmd = await self._cmd_queue.get()
# run command
await cmd
self._cmd_queue.task_done()
def stop(self):
"""Stop platform."""
if self._cmd_task:
self.machine.clock.loop.run_until_complete(self._cmd_queue.join())
self._cmd_task.cancel()
self._cmd_task = None
if self.pi:
self.machine.clock.loop.run_until_complete(self.pi.stop())
self.pi = None
async def configure_servo(self, number: str, config: dict) -> ServoPlatformInterface:
"""Configure a servo."""
del config
return RpiServo(number, self)
async def get_hw_switch_states(self):
"""Return current switch states."""
hw_states = dict()
curr_bit = 1
for index in range(32):
hw_states[str(index)] = not bool(curr_bit & self._switches)
curr_bit <<= 1
return hw_states
def configure_switch(self, number: str, config: SwitchConfig, platform_config: dict) -> "SwitchPlatformInterface":
"""Configure a switch with pull up."""
# set input
self.send_command(self.pi.set_mode(int(number), apigpio.INPUT))
# configure pull up
self.send_command(self.pi.set_pull_up_down(int(number), apigpio.PUD_UP))
if config.debounce:
# configure debounce to 2ms
self.send_command(self.pi.set_glitch_filter(int(number), 2000))
else:
# configure debounce to 100us
self.send_command(self.pi.set_glitch_filter(int(number), 100))
# add callback
self.send_command(self.pi.add_callback(int(number), apigpio.EITHER_EDGE, self._switch_changed))
return RpiSwitch(config, number, self)
def _switch_changed(self, gpio, level, tick):
"""Process switch change."""
del tick
self.machine.switch_controller.process_switch_by_num(str(gpio), not level, self)
def set_pulse_on_hit_and_release_rule(self, enable_switch: SwitchSettings, coil: DriverSettings):
"""Raise exception."""
raise AssertionError("Not supported on the RPi currently. Write in the forum if you need it.")
def set_pulse_on_hit_and_enable_and_release_rule(self, enable_switch: SwitchSettings, coil: DriverSettings):
"""Raise exception."""
raise AssertionError("Not supported on the RPi currently. Write in the forum if you need it.")
def set_pulse_on_hit_and_release_and_disable_rule(self, enable_switch: SwitchSettings,
eos_switch: SwitchSettings, coil: DriverSettings,
repulse_settings: Optional[RepulseSettings]):
"""Raise exception."""
raise AssertionError("Not supported on the RPi currently. Write in the forum if you need it.")
def set_pulse_on_hit_and_enable_and_release_and_disable_rule(self, enable_switch: SwitchSettings,
eos_switch: SwitchSettings, coil: DriverSettings,
repulse_settings: Optional[RepulseSettings]):
"""Raise exception."""
raise AssertionError("Not supported on the RPi currently. Write in the forum if you need it.")
def set_pulse_on_hit_rule(self, enable_switch: SwitchSettings, coil: DriverSettings):
"""Raise exception."""
raise AssertionError("Not supported on the RPi currently. Write in the forum if you need it.")
def clear_hw_rule(self, switch: SwitchSettings, coil: DriverSettings):
"""Raise exception."""
raise AssertionError("Not supported on the RPi currently. Write in the forum if you need it.")
def configure_driver(self, config: DriverConfig, number: str, platform_settings: dict) -> "DriverPlatformInterface":
"""Configure an output on the Raspberry Pi."""
# disable pull up/down
self.send_command(self.pi.set_pull_up_down(int(number), apigpio.PUD_OFF))
# set output
self.send_command(self.pi.set_mode(int(number), apigpio.OUTPUT))
return RpiDriver(number, config, self)
async def configure_i2c(self, number: str):
"""Configure I2c device."""
device = RpiI2cDevice(number, self.machine.clock.loop, self)
await device.open()
return device