gukebox 0.3.0a0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. gukebox-0.3.0a0/.gitignore +14 -0
  2. gukebox-0.3.0a0/LICENSE +21 -0
  3. gukebox-0.3.0a0/PKG-INFO +306 -0
  4. gukebox-0.3.0a0/Pn532-nfc-hat-code/raspberrypi/python/pn532/__init__.py +13 -0
  5. gukebox-0.3.0a0/Pn532-nfc-hat-code/raspberrypi/python/pn532/i2c.py +156 -0
  6. gukebox-0.3.0a0/Pn532-nfc-hat-code/raspberrypi/python/pn532/pn532.py +594 -0
  7. gukebox-0.3.0a0/Pn532-nfc-hat-code/raspberrypi/python/pn532/spi.py +177 -0
  8. gukebox-0.3.0a0/Pn532-nfc-hat-code/raspberrypi/python/pn532/uart.py +112 -0
  9. gukebox-0.3.0a0/README.md +273 -0
  10. gukebox-0.3.0a0/discstore/__init__.py +0 -0
  11. gukebox-0.3.0a0/discstore/adapters/__init__.py +0 -0
  12. gukebox-0.3.0a0/discstore/adapters/inbound/__init__.py +0 -0
  13. gukebox-0.3.0a0/discstore/adapters/inbound/api_controller.py +63 -0
  14. gukebox-0.3.0a0/discstore/adapters/inbound/cli_controller.py +62 -0
  15. gukebox-0.3.0a0/discstore/adapters/inbound/cli_display.py +62 -0
  16. gukebox-0.3.0a0/discstore/adapters/inbound/config.py +151 -0
  17. gukebox-0.3.0a0/discstore/adapters/inbound/interactive_cli_controller.py +92 -0
  18. gukebox-0.3.0a0/discstore/adapters/inbound/logger.py +15 -0
  19. gukebox-0.3.0a0/discstore/adapters/inbound/ui_controller.py +101 -0
  20. gukebox-0.3.0a0/discstore/adapters/outbound/__init__.py +0 -0
  21. gukebox-0.3.0a0/discstore/adapters/outbound/json_library_repository.py +32 -0
  22. gukebox-0.3.0a0/discstore/app.py +34 -0
  23. gukebox-0.3.0a0/discstore/di_container.py +53 -0
  24. gukebox-0.3.0a0/discstore/domain/__init__.py +0 -0
  25. gukebox-0.3.0a0/discstore/domain/entities/__init__.py +0 -0
  26. gukebox-0.3.0a0/discstore/domain/entities/disc.py +21 -0
  27. gukebox-0.3.0a0/discstore/domain/entities/library.py +10 -0
  28. gukebox-0.3.0a0/discstore/domain/repositories/__init__.py +0 -0
  29. gukebox-0.3.0a0/discstore/domain/repositories/library_repository.py +13 -0
  30. gukebox-0.3.0a0/discstore/domain/use_cases/__init__.py +0 -0
  31. gukebox-0.3.0a0/discstore/domain/use_cases/add_disc.py +16 -0
  32. gukebox-0.3.0a0/discstore/domain/use_cases/edit_disc.py +16 -0
  33. gukebox-0.3.0a0/discstore/domain/use_cases/list_discs.py +12 -0
  34. gukebox-0.3.0a0/discstore/domain/use_cases/remove_disc.py +15 -0
  35. gukebox-0.3.0a0/jukebox/__init__.py +0 -0
  36. gukebox-0.3.0a0/jukebox/app.py +144 -0
  37. gukebox-0.3.0a0/jukebox/players/__init__.py +2 -0
  38. gukebox-0.3.0a0/jukebox/players/dryrun.py +22 -0
  39. gukebox-0.3.0a0/jukebox/players/player.py +23 -0
  40. gukebox-0.3.0a0/jukebox/players/sonos.py +47 -0
  41. gukebox-0.3.0a0/jukebox/players/sonos.py.bak +135 -0
  42. gukebox-0.3.0a0/jukebox/players/utils.py +61 -0
  43. gukebox-0.3.0a0/jukebox/providers/local_file.py +54 -0
  44. gukebox-0.3.0a0/jukebox/readers/__init__.py +2 -0
  45. gukebox-0.3.0a0/jukebox/readers/dryrun.py +30 -0
  46. gukebox-0.3.0a0/jukebox/readers/nfc.py +27 -0
  47. gukebox-0.3.0a0/jukebox/readers/reader.py +7 -0
  48. gukebox-0.3.0a0/jukebox/readers/utils.py +44 -0
  49. gukebox-0.3.0a0/pn532/__init__.py +13 -0
  50. gukebox-0.3.0a0/pn532/i2c.py +156 -0
  51. gukebox-0.3.0a0/pn532/pn532.py +594 -0
  52. gukebox-0.3.0a0/pn532/spi.py +177 -0
  53. gukebox-0.3.0a0/pn532/uart.py +112 -0
  54. gukebox-0.3.0a0/pyproject.toml +67 -0
  55. gukebox-0.3.0a0/tests/discstore/adapters/inbound/test_api_controller.py +26 -0
  56. gukebox-0.3.0a0/tests/discstore/adapters/inbound/test_cli_display.py +66 -0
  57. gukebox-0.3.0a0/tests/discstore/adapters/inbound/test_config.py +150 -0
  58. gukebox-0.3.0a0/tests/discstore/adapters/inbound/test_logger.py +36 -0
  59. gukebox-0.3.0a0/tests/discstore/adapters/inbound/test_ui_controller.py +25 -0
  60. gukebox-0.3.0a0/tests/discstore/domain/use_cases/test_add_disc.py +47 -0
  61. gukebox-0.3.0a0/tests/discstore/domain/use_cases/test_edit_disc.py +45 -0
  62. gukebox-0.3.0a0/tests/discstore/domain/use_cases/test_list_discs.py +28 -0
  63. gukebox-0.3.0a0/tests/discstore/domain/use_cases/test_remove_disc.py +40 -0
  64. gukebox-0.3.0a0/tests/discstore/test_app.py +128 -0
  65. gukebox-0.3.0a0/tests/discstore/test_di_container.py +113 -0
@@ -0,0 +1,14 @@
1
+ .env
2
+ .python-version
3
+
4
+ .venv
5
+ venv
6
+ __pycache__/
7
+ .pytest_cache/
8
+ .ruff_cache/
9
+ built-package/
10
+ *.egg-info
11
+ dist/
12
+
13
+ library.json
14
+ library/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Gudsfile
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,306 @@
1
+ Metadata-Version: 2.4
2
+ Name: gukebox
3
+ Version: 0.3.0a0
4
+ Summary: A Jukebox to play music on speakers using 'CD' with NFC tag
5
+ Project-URL: Repository, https://github.com/Gudsfile/jukebox
6
+ Author: Gudsfile
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Keywords: discstore,jukebox,music,nfc
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3 :: Only
13
+ Classifier: Programming Language :: Python :: 3.7
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Requires-Python: <3.13,>=3.7
20
+ Requires-Dist: pydantic==2.10.6; python_version >= '3.10'
21
+ Requires-Dist: pydantic==2.5.3; python_version < '3.10'
22
+ Requires-Dist: soco==0.30.11
23
+ Requires-Dist: typing-extensions; python_version == '3.7.*'
24
+ Provides-Extra: api
25
+ Requires-Dist: fastapi==0.116.1; (python_version >= '3.8') and extra == 'api'
26
+ Requires-Dist: uvicorn==0.33.0; (python_version >= '3.8' and python_version < '3.10') and extra == 'api'
27
+ Requires-Dist: uvicorn==0.35.0; (python_version >= '3.10') and extra == 'api'
28
+ Provides-Extra: ui
29
+ Requires-Dist: fastui==0.7.0; (python_version >= '3.10') and extra == 'ui'
30
+ Requires-Dist: jukebox[api]; extra == 'ui'
31
+ Requires-Dist: python-multipart==0.0.20; (python_version >= '3.10') and extra == 'ui'
32
+ Description-Content-Type: text/markdown
33
+
34
+ # My jukebox
35
+
36
+ 💿 Play music on speakers using NFC tags.
37
+
38
+ 🚧 At the moment:
39
+
40
+ - NFC tags - CDs must be pre-populated in a JSON file (`discstore` included with `jukebox` may be of help to you)
41
+ - supports many music providers (Spotify, Apple Music, etc.), just add the URIs to the JSON file
42
+ - only works with Sonos speakers (there is a "dryrun" player for development), but code is designed to be modified to add new ones
43
+ - **as soon as** the NFC tag is removed, the music pauses, then resumes when the NFC tag is replaced
44
+
45
+ 💡 Inspired by:
46
+
47
+ - https://github.com/hankhank10/vinylemulator
48
+ - https://github.com/zacharycohn/jukebox
49
+
50
+ 📋 Table of contents:
51
+
52
+ - [Install](#install)
53
+ - [Usage](#usage)
54
+ - [First steps](#first-steps)
55
+ - [Avaible players and readers](#avaible-players-and-readers)
56
+ - [Readers](#readers)
57
+ - [Players](#players)
58
+ - [The library file](#the-library-file)
59
+ - [Developer setup](#developer-setup)
60
+
61
+ ## Notes
62
+
63
+ The project remains in Python 3.7 to make it easier to use on hardware like Raspberry Pi.
64
+ However, it works on Python versions 3.7+.
65
+ The `api` and `ui` extras are only available for Python versions 3.8+ and 3.10+.
66
+
67
+ ## Install
68
+
69
+ ### PyPI
70
+
71
+ Install the package from [PyPI](https://pypi.org/project/gukebox/).
72
+
73
+ > [!WARNING]
74
+ > The package name is `gukebox` with `g` instead of a `j` (due to a name already taken).
75
+
76
+ To invoke the tool without installing it you could use `uvx`:
77
+
78
+ ```shell
79
+ uvx --from gukebox jukebox
80
+ ```
81
+
82
+ It is recommended to installing `jukebox` into an isolated environment, e.g., with `uv tool install`:
83
+
84
+ ```shell
85
+ uv tool install gukebox
86
+ ```
87
+
88
+ or with `pipx`
89
+
90
+ ```shell
91
+ pipx install gukebox
92
+ ```
93
+
94
+ However you could install it with `pip`:
95
+
96
+ ```shell
97
+ pip install gukebox
98
+ ```
99
+
100
+ ### GitHub Releases
101
+
102
+ All releases can be downloaded from the [GitHub releases page](https://github.com/Gudsfile/jukebox/releases).
103
+
104
+ ## First steps
105
+
106
+ Set the `SONOS_HOST` environment variable with the IP address of your Sonos Zone Player (see [Available players and readers](#available-players-and-readers)).
107
+
108
+ Create a `~/.jukebox/library.json` file and complete it with the desired artists and albums.
109
+ For this, you can use `discstore` installed with the package or write it manually.
110
+
111
+ ### Using the discstore
112
+
113
+ To associate an URI with an NFC tag:
114
+
115
+ ```shell
116
+ discstore add tag_id uri
117
+ ```
118
+
119
+ Other commands are available, use `--help` to see them.
120
+
121
+ To use the `api` and `ui` commands, additional packages are required. You can install the `package[extra]` syntax regardless of the package manager you use, for example:
122
+
123
+ ```shell
124
+ # Python 3.8+ required
125
+ uv tool install gukebox[api]
126
+
127
+ # Python 3.10+ required, ui includes the api extra
128
+ uv tool install gukebox[ui]
129
+ ```
130
+
131
+ ### Manually
132
+
133
+ Complete your `~/.jukebox/library.json` file with each tag id and the expected media URI.
134
+ Take a look at `sample_library.json` and the [The library file](#the-library-file) section for more information.
135
+
136
+ ## Usage
137
+
138
+ Start the jukebox with the `jukebox` command (show help message with `--help`)
139
+
140
+ ```shell
141
+ jukebox PLAYER_TO_USE READER_TO_USE -l YOUR_LIBRARY_FILE
142
+ ```
143
+
144
+ 🎉 With choosing the `sonos` player and `nfc` reader, by approaching a NFC tag stored in the `library.json` file, you should hear the associated music begins.
145
+
146
+ ## Avaible players and readers
147
+
148
+ ### Readers
149
+
150
+ **Dry run** (`dryrun`)
151
+ Read a text entry.
152
+
153
+ **NFC** (`nfc`)
154
+ Read an NFC tag and get its UID.
155
+ This project works with an NFC reader like the **PN532** and NFC tags like the **NTAG2xx**.
156
+ It is configured according to the [Waveshare PN532 wiki](https://www.waveshare.com/wiki/PN532_NFC_HAT).
157
+
158
+ ### Players
159
+
160
+ **Dry run** (`dryrun`)
161
+ Displays the events that a real speaker would have performed (`playing …`, `pause`, etc.).
162
+
163
+ **Sonos** (`sonos`)
164
+ Play music through a Sonos speaker.
165
+ `SONOS_HOST` environment variable must be set with the IP address of your Sonos Zone Player.
166
+ You could set the environment varible with `export SONOS_HOST=192.168.0.???` to use this speaker through the `jukebox` command.
167
+ Or set it in a `.env` file to use the `uv run --env-file .env <command to run>` version.
168
+
169
+ ## The library file
170
+
171
+ The `library.json` file is a JSON file that contains the artists, albums and tags.
172
+ It is used by the `jukebox` command to find the corresponding metadata for each tag.
173
+ And the `discsstore` command help you to managed this file with a CLI, an interactive CLI, an API or an UI (see `discstore --help`).
174
+
175
+ By default, this file should be placed at `~/.jukebox/library.json`. But you can use another path by creating a `JUKEBOX_LIBRARY_PATH` environment variable or with the `--library` argument.
176
+
177
+ ```json
178
+ {
179
+ "discs": {
180
+ "a:tag:uid": {
181
+ "uri": "URI of a track, an album or a playlist on many providers",
182
+ "option": { "shuffle": true }
183
+ },
184
+ "another:tag:uid": {
185
+ "uri": "uri"
186
+ },
187
+
188
+ }
189
+ }
190
+ ```
191
+
192
+ The `discs` part is a dictionary containing NFC tag UIDs.
193
+ Each UID is associated with an URI.
194
+ URIs are the URIs of the music providers (Spotify, Apple Music, etc.) and relate to tracks, albums, playlists, etc.
195
+
196
+ `metadata` is an optional section where the names of the artist, album, song, or playlist are entered:
197
+
198
+ ```json
199
+ "a:tag:uid": {
200
+ "uri": "uri",
201
+ "metadata": { "artist": "artist" }
202
+ }
203
+ ```
204
+
205
+ It is also possible to use the `shuffle` key to play the album in shuffle mode:
206
+
207
+ ```json
208
+ "a:tag:uid": {
209
+ "uri": "uri",
210
+ "option": { "shuffle": true }
211
+ }
212
+ ```
213
+
214
+ To summarize, for example, if you have the following `~/.jukebox/library.json` file:
215
+
216
+ ```json
217
+ {
218
+ "discs": {
219
+ "ta:g1:id": {
220
+ "uri": "uri1",
221
+ "metadata": { "artist": "a", "album": "a" }
222
+ },
223
+ "ta:g2:id": {
224
+ "uri": "uri2",
225
+ "metadata": { "playlist": "b" },
226
+ "option": { "shuffle": true }
227
+ }
228
+ }
229
+ }
230
+ ```
231
+
232
+ Then, the jukebox will find the metadata for the tag `ta:g2:id` and will send the `uri2` to the speaker so that it plays playlist "b" in random order.
233
+
234
+ ## Developer setup
235
+
236
+ ### Install
237
+
238
+ Clone the project.
239
+
240
+ Installing dependencies with [uv](https://github.com/astral-sh/uv):
241
+
242
+ ```shell
243
+ uv sync
244
+ ```
245
+
246
+ Add `--all-extras` to install dependencies for all extras (`api` and `ui`).
247
+
248
+ Set the `SONOS_HOST` environment variable with the IP address of your Sonos Zone Player (see [Available players and readers](#available-players-and-readers)).
249
+ To do this you can use a `.env` file and `uv run --env-file .env <command to run>`.
250
+
251
+ Create a `library.json` file and complete it with the desired NFC tags and CDs.
252
+ Take a look at `sample_library.json` and the [The library file](#the-library-file) section for more information.
253
+
254
+ ### Usage
255
+
256
+ Start the jukebox with `uv` and use `--help` to show help message
257
+
258
+ ```shell
259
+ uv run jukebox PLAYER_TO_USE READER_TO_USE
260
+ ```
261
+
262
+ #### player (`players/utils.py`)
263
+
264
+ This part allows to play music through a player.
265
+ It is used by `app.py` but can be used separately.
266
+
267
+ Show help message
268
+
269
+ ```shell
270
+ uv run player --help
271
+ ```
272
+
273
+ Play a specific album
274
+
275
+ ```shell
276
+ uv run player sonos play --artist "Your favorite artist" --album "Your favorite album by this artist"
277
+ ```
278
+
279
+ Artist and album must be entered in the library's JSON file. This file can be specified with the `--library` parameter.
280
+
281
+ For the moment, the player can only play music through Sonos speakers.
282
+ A "dryrun" player is also available for testing the script without any speakers configured.
283
+
284
+ #### reader (`readers/utils.py`)
285
+
286
+ This part allows to read an input like a NFC tag.
287
+ It is used by `app.py` but can be used separately, even if it is useless.
288
+
289
+ Show help message
290
+
291
+ ```shell
292
+ uv run reader --help
293
+ ```
294
+
295
+ Read an input
296
+
297
+ ```shell
298
+ uv run reader nfc
299
+ ```
300
+
301
+ For the moment, this part can only works with PN532 NFC reader.
302
+ A "dryrun" reader is also available for testing the script without any NFC reader configured.
303
+
304
+ ## Contributing
305
+
306
+ Contributions are welcome! Feel free to open an issue or a pull request.
@@ -0,0 +1,13 @@
1
+ __all__ = [
2
+ 'pn532',
3
+ 'i2c',
4
+ 'spi',
5
+ 'uart',
6
+ 'PN532_I2C',
7
+ 'PN532_SPI',
8
+ 'PN532_UART'
9
+ ]
10
+ from . import pn532
11
+ from .i2c import PN532_I2C
12
+ from .spi import PN532_SPI
13
+ from .uart import PN532_UART
@@ -0,0 +1,156 @@
1
+ # Waveshare PN532 NFC Hat control library.
2
+ # Author: Yehui from Waveshare
3
+ #
4
+ # The MIT License (MIT)
5
+ #
6
+ # Copyright (c) 2015-2018 Adafruit Industries
7
+ # Copyright (c) 2019 Waveshare
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ # of this software and associated documentation files (the "Software"), to deal
11
+ # in the Software without restriction, including without limitation the rights
12
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ # copies of the Software, and to permit persons to whom the Software is
14
+ # furnished to do so, subject to the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included in
17
+ # all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ # THE SOFTWARE.
26
+
27
+ """
28
+ This module will let you communicate with a PN532 RFID/NFC chip
29
+ using I2C on the Raspberry Pi.
30
+ """
31
+
32
+ import fcntl
33
+ import os
34
+ import time
35
+ import RPi.GPIO as GPIO
36
+ from .pn532 import PN532, BusyError
37
+
38
+ # pylint: disable=bad-whitespace
39
+ # PN532 address without R/W bit, i.e. (0x48 >> 1)
40
+ I2C_ADDRESS = 0x24
41
+ I2C_CHANNEL = 1
42
+
43
+ # ctypes defines for i2c, see <linux/i2c-dev.h>
44
+ I2C_SLAVE = 1795
45
+
46
+
47
+ class I2CDevice:
48
+ """Implements I2C device on ioctl"""
49
+ def __init__(self, channel, addr):
50
+ self.addr = addr
51
+ self.i2c = os.open('/dev/i2c-%d' % channel, os.O_RDWR)
52
+ if self.i2c < 0:
53
+ raise RuntimeError('i2c device does not exist')
54
+ if fcntl.ioctl(self.i2c, I2C_SLAVE, addr) < 0:
55
+ raise RuntimeError('i2c slave does not exist')
56
+
57
+ def write(self, buf):
58
+ """Wrapper method of os.write"""
59
+ return os.write(self.i2c, buf)
60
+
61
+ def read(self, count):
62
+ """Wrapper method of os.read"""
63
+ return os.read(self.i2c, count)
64
+
65
+
66
+ class PN532_I2C(PN532):
67
+ """Driver for the PN532 connected over I2C."""
68
+ def __init__(self, irq=None, reset=None, req=None, debug=False):
69
+ """Create an instance of the PN532 class using I2C. Note that PN532
70
+ uses clock stretching. Optional IRQ pin (not used),
71
+ reset pin and debugging output.
72
+ """
73
+ self.debug = debug
74
+ self._irq = irq
75
+ self._req = req
76
+ GPIO.setmode(GPIO.BCM)
77
+ # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
78
+ # harware reset
79
+ GPIO.setup(reset, GPIO.OUT)
80
+ # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
81
+ # wakeup! this means we don't need to do the I2C clock-stretch thing
82
+ GPIO.setup(req, GPIO.OUT)
83
+ self._gpio_init(irq=irq, req=req, reset=reset)
84
+ self._i2c = I2CDevice(I2C_CHANNEL, I2C_ADDRESS)
85
+ super().__init__(debug=debug, reset=reset)
86
+
87
+ def _gpio_init(self, reset, irq=None, req=None):
88
+ self._irq = irq
89
+ self._req = req
90
+ GPIO.setmode(GPIO.BCM)
91
+ if reset:
92
+ GPIO.setup(reset, GPIO.OUT)
93
+ GPIO.output(reset, True)
94
+ if irq:
95
+ GPIO.setup(irq, GPIO.IN)
96
+ if req:
97
+ GPIO.setup(req, GPIO.OUT)
98
+ GPIO.output(req, True)
99
+
100
+ def _reset(self, pin):
101
+ """Perform a hardware reset toggle"""
102
+ GPIO.output(pin, True)
103
+ time.sleep(0.1)
104
+ GPIO.output(pin, False)
105
+ time.sleep(0.5)
106
+ GPIO.output(pin, True)
107
+ time.sleep(0.1)
108
+
109
+ def _wakeup(self): # pylint: disable=no-self-use
110
+ """Send any special commands/data to wake up PN532"""
111
+ if self._req:
112
+ GPIO.output(self._req, True)
113
+ time.sleep(0.1)
114
+ GPIO.output(self._req, False)
115
+ time.sleep(0.1)
116
+ GPIO.output(self._req, True)
117
+ time.sleep(0.5)
118
+
119
+ def _wait_ready(self, timeout=10):
120
+ """Poll PN532 if status byte is ready, up to `timeout` seconds"""
121
+ time.sleep(0.01) # required after _wait_ready()
122
+ status = bytearray(1)
123
+ timestamp = time.monotonic()
124
+ while (time.monotonic() - timestamp) < timeout:
125
+ try:
126
+ status[0] = self._i2c.read(1)[0]
127
+ except OSError:
128
+ self._wakeup()
129
+ continue
130
+ if status == b'\x01':
131
+ return True # No longer busy
132
+ time.sleep(0.005) # lets ask again soon!
133
+ # Timed out!
134
+ return False
135
+
136
+ def _read_data(self, count):
137
+ """Read a specified count of bytes from the PN532."""
138
+ try:
139
+ status = self._i2c.read(1)[0]
140
+ if status != 0x01: # not ready
141
+ raise BusyError
142
+ frame = bytes(self._i2c.read(count+1))
143
+ except OSError as err:
144
+ if self.debug:
145
+ print(err)
146
+ return
147
+
148
+ if self.debug:
149
+ print("Reading: ", [hex(i) for i in frame[1:]])
150
+ else:
151
+ time.sleep(0.1)
152
+ return frame[1:] # don't return the status byte
153
+
154
+ def _write_data(self, framebytes):
155
+ """Write a specified count of bytes to the PN532"""
156
+ self._i2c.write(framebytes)