Skip to content

Commit 051bd3e

Browse files
Fix pylint issues
1 parent 5af7277 commit 051bd3e

File tree

5 files changed

+63
-68
lines changed

5 files changed

+63
-68
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Usage Example
9999
uart = busio.UART(tx=board.GP16, rx=board.GP17, baudrate=115200)
100100
101101
df_player = DF1201S(uart)
102-
df_player.volume = 0.2
102+
df_player.volume = 0.2
103103
df_player.play_mode = DF1201S.PLAYMODE_PLAY_ONCE
104104
105105
if not df_player.play_next():

docs/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ sound stored on the DFPlayer Pro.
1111
More Features
1212
--------------
1313

14-
Explore more features of the DFPlayer Pro, including volume adjustment, play modes,
14+
Explore more features of the DFPlayer Pro, including volume adjustment, play modes,
1515
and play status.
1616

1717
.. literalInclude:: ../examples/df1201s_morefeatures.py

examples/df1201s_morefeatures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
import board
65
import time
6+
import board
77
import busio
88
from mindwidgets_df1201s import DF1201S
99

examples/df1201s_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
uart = busio.UART(tx=board.GP16, rx=board.GP17, baudrate=115200)
1010

1111
df_player = DF1201S(uart)
12-
df_player.volume = 0.2
12+
df_player.volume = 0.2
1313
df_player.play_mode = DF1201S.PLAYMODE_PLAY_ONCE
1414

1515
if not df_player.play_next():

mindwidgets_df1201s.py

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,33 @@
3838
_OK_RESPONSE = "OK"
3939

4040

41-
class DF1201S(object):
41+
def _unwrap_int(value: str) -> int:
42+
"""
43+
Convert a string to an int if it only contains digits, otherwise return -1.
44+
45+
:param str value: the string to attempt to convert to an int
46+
:return: the int value of the string, or -1 if it could not be converted
47+
"""
48+
if value.isdigit():
49+
return int(value)
50+
return -1
51+
52+
53+
def _map_volume(volume: float) -> int:
54+
"""
55+
Map 0.0 to 1.0 volume value to DFPlayer's volume range.
56+
57+
:param float volume: the volume, from 0 to 1, to convert
58+
:return: the volume mapped to a valid DFPlayer Pro volume value
59+
"""
60+
return max(min(int(volume * _DFPLAYER_VOLUME_MAX), _DFPLAYER_VOLUME_MAX), 0)
61+
62+
63+
class DF1201S:
4264
"""Driver for DFRobot DFPlayer Pro (DF1201S) MP3 player with onboard storage."""
4365

66+
# pylint: disable=too-many-public-methods
67+
4468
PLAYMODE_REPEAT_ONE_SONG = const(1)
4569
"""play_mode: Repeat one sound."""
4670
PLAYMODE_REPEAT_ALL = const(2)
@@ -56,7 +80,7 @@ def __init__(self, uart: busio.UART) -> None:
5680
"""
5781
Initialize the DFPlayer connection.
5882
59-
:param busio.UART uart: the serial connection to the DFPlayer
83+
:param busio.UART uart: the serial connection to the DFPlayer
6084
"""
6185
self._uart = uart
6286
if not self.connected:
@@ -66,14 +90,14 @@ def _send_query(self, command: str, arg=None) -> str:
6690
"""
6791
Send AT command and return the response.
6892
69-
:param str command: the command to send (without AT prefix or CR\LF)
93+
:param str command: the command to send (without AT prefix or CR/LF)
7094
:param arg: optional command argument
71-
:return: the response to the command (CR\LF removed)
95+
:return: the response to the command (CR/LF removed)
7296
"""
7397
# format the AT command, adding the argument if given
7498
at_command = "AT"
7599
if command != "":
76-
if arg != None:
100+
if arg is not None:
77101
at_command = f"AT+{command}={arg}"
78102
else:
79103
at_command = f"AT+{command}"
@@ -86,7 +110,7 @@ def _send_query(self, command: str, arg=None) -> str:
86110
# read the response and strip the \r\n
87111
response = self._uart.readline()
88112

89-
parsed = "" if response == None else str(response[0:-2], 'ascii')
113+
parsed = "" if response is None else str(response[0:-2], "ascii")
90114

91115
print("DFPlayer Response: ", parsed)
92116

@@ -96,42 +120,20 @@ def _send_command(self, command: str, arg=None) -> bool:
96120
"""
97121
Send AT command and return true for an OK response.
98122
99-
:param str command: the command to send (without AT prefix or CR\LF)
123+
:param str command: the command to send (without AT prefix or CR/LF)
100124
:param arg: optional command argument
101125
:return: True if the command was successful
102126
"""
103127
return self._send_query(command, arg) == _OK_RESPONSE
104128

105-
def _unwrap_int(self, value: str) -> int:
106-
"""
107-
Convert a string to an int if it only contains digits, otherwise return -1.
108-
109-
:param str value: the string to attempt to convert to an int
110-
:return: the int value of the string, or -1 if it could not be converted
111-
"""
112-
if value.isdigit():
113-
return int(value)
114-
return -1
115-
116-
def _map_volume(self, volume: float) -> int:
117-
"""
118-
Map 0.0 to 1.0 volume value to DFPlayer's volume range.
119-
120-
:param float volume: the volume, from 0 to 1, to convert
121-
:return: the volume mapped to a valid DFPlayer Pro volume value
122-
"""
123-
return max(min(int(volume * _DFPLAYER_VOLUME_MAX), _DFPLAYER_VOLUME_MAX), 0)
124-
125129
@property
126130
def connected(self) -> bool:
127-
"""
128-
:return: True if the DFPlayer is connected.
129-
"""
131+
"""True if the DFPlayer is connected."""
130132
return self._send_command("")
131133

132134
def set_baud_rate(self, rate: int) -> bool:
133135
"""
134-
Set baud rate for the serial DFPlayer connection.
136+
Set baud rate for the serial DFPlayer connection.
135137
136138
.. note:: Persists after power off.
137139
@@ -143,11 +145,9 @@ def set_baud_rate(self, rate: int) -> bool:
143145

144146
@property
145147
def volume(self) -> float:
146-
"""
147-
:return: Volume level, as a float between 0 and 1.
148-
"""
148+
"""Volume level, as a float between 0 and 1."""
149149
response = self._send_query("VOL", "?")
150-
df_volume = self._unwrap_int(response[7:-1])
150+
df_volume = _unwrap_int(response[7:-1])
151151
if df_volume < 0:
152152
return 0
153153

@@ -163,7 +163,7 @@ def volume(self, volume: float) -> bool:
163163
:param float volume: the volume level, as a float between 0 and 1.
164164
:return: True if the volume was set.
165165
"""
166-
df_volume = self._map_volume(volume)
166+
df_volume = _map_volume(volume)
167167
return self._send_command("VOL", df_volume)
168168

169169
def increase_volume(self, increment: float) -> bool:
@@ -175,7 +175,7 @@ def increase_volume(self, increment: float) -> bool:
175175
:param float increment: the amount to increase the volume, as a float between 0 and 1.
176176
:return: True if the volume was increased.
177177
"""
178-
df_volume = self._map_volume(increment)
178+
df_volume = _map_volume(increment)
179179
return self._send_command("VOL", f"+{df_volume}")
180180

181181
def decrease_volume(self, decrement: float) -> bool:
@@ -187,7 +187,7 @@ def decrease_volume(self, decrement: float) -> bool:
187187
:param float decrement: the amount to decrement the volume, as a float between 0 and 1.
188188
:return: True if the volume was decreased.
189189
"""
190-
df_volume = self._map_volume(decrement)
190+
df_volume = _map_volume(decrement)
191191
return self._send_command("VOL", f"-{df_volume}")
192192

193193
def enable_led(self) -> bool:
@@ -251,20 +251,22 @@ def play_mode(self) -> int:
251251
"""
252252
Returns the current play mode.
253253
254-
:return: One of the play mode constants (`PLAYMODE_REPEAT_ONE_SONG`, `PLAYMODE_REPEAT_ALL`, or `PLAYMODE_PLAY_ONCE`).
254+
:return: One of the play mode constants, `PLAYMODE_REPEAT_ONE_SONG`, `PLAYMODE_REPEAT_ALL`,
255+
or `PLAYMODE_PLAY_ONCE`.
255256
`PLAYMODE_RANDOM` can be set, but this function will return -1. Bug in the device?
256257
`PLAYMODE_REPEAT_FOLDER` can be set, but this function will return 3. Bug in the device?
257258
258259
"""
259260
response = self._send_query("PLAYMODE", "?")
260-
return self._unwrap_int(response[10:])
261+
return _unwrap_int(response[10:])
261262

262263
@play_mode.setter
263264
def play_mode(self, new_mode: int) -> bool:
264265
"""
265266
Set the play mode.
266267
267-
:param int new_mode: One of `PLAYMODE_REPEAT_ONE_SONG`, `PLAYMODE_REPEAT_ALL`, `PLAYMODE_PLAY_ONCE`, `PLAYMODE_RANDOM`, or `PLAYMODE_REPEAT_FOLDER`.
268+
:param int new_mode: One of `PLAYMODE_REPEAT_ONE_SONG`, `PLAYMODE_REPEAT_ALL`,
269+
`PLAYMODE_PLAY_ONCE`, `PLAYMODE_RANDOM`, or `PLAYMODE_REPEAT_FOLDER`.
268270
:return: True if the play mode was set.
269271
"""
270272
return self._send_command("PLAYMODE", new_mode)
@@ -289,7 +291,8 @@ def play_file_number(self, file_number: int) -> bool:
289291
"""
290292
Play the sound at the given file number.
291293
292-
.. note:: Plays the first sound if the file number is invalid. I don't see a way to turn off this behavior.
294+
.. note:: Plays the first sound if the file number is invalid. I don't see a way to turn
295+
off this behavior.
293296
294297
:param int file_number: the file number to play.
295298
:return: True if the given file number is playing, False if the first file is playing.
@@ -300,7 +303,8 @@ def play_file_name(self, file_name: str) -> bool:
300303
"""
301304
Play the sound with the given file name.
302305
303-
.. note:: Plays the first sound if the file name is invalid. I don't see a way to turn off this behavior.
306+
.. note:: Plays the first sound if the file name is invalid. I don't see a way to turn
307+
off this behavior.
304308
305309
:param str file_name: the file name to play.
306310
:return: True if the given file is playing, False if the first file if playing.
@@ -329,49 +333,40 @@ def fast_seek(self, seconds: int) -> bool:
329333
"""
330334
Start playing the current sound at a specific time offset.
331335
332-
.. note:: If the offset exceeds the sound length, this function still returns `True` and the sound stops.
336+
.. note:: If the offset exceeds the sound length, this function still returns `True`
337+
and the sound stops.
333338
334339
:param int seconds: the time offset, in seconds.
335-
:return: True if the current offset was set.
340+
:return: True if the current offset was set.
336341
"""
337342
return self._send_command("TIME", seconds)
338343

339344
@property
340345
def total_files(self) -> int:
341346
"""The total count of sound files available."""
342-
return self._unwrap_int(self._send_query("QUERY", 2))
347+
return _unwrap_int(self._send_query("QUERY", 2))
343348

344349
@property
345350
def file_number(self) -> int:
346-
"""
347-
:return: The file number of the currently playing file.
348-
"""
349-
return self._unwrap_int(self._send_query("QUERY", 1))
351+
"""The file number of the currently playing file."""
352+
return _unwrap_int(self._send_query("QUERY", 1))
350353

351354
@property
352355
def file_name(self) -> str:
353-
"""
354-
:return: The file name of the currently playing file.
355-
"""
356+
"""The file name of the currently playing file."""
356357
return self._send_query("QUERY", 5)
357358

358359
@property
359360
def played_time(self) -> int:
360-
"""
361-
:return: The number of seconds the current sound has played.
362-
"""
363-
return self._unwrap_int(self._send_query("QUERY", 3))
361+
"""The number of seconds the current sound has played."""
362+
return _unwrap_int(self._send_query("QUERY", 3))
364363

365364
@property
366365
def total_time(self) -> int:
367-
"""
368-
:return: The length of the current sound in seconds.
369-
"""
370-
return self._unwrap_int(self._send_query("QUERY", 4))
366+
"""The length of the current sound in seconds."""
367+
return _unwrap_int(self._send_query("QUERY", 4))
371368

372369
@property
373370
def playing(self) -> bool:
374-
"""
375-
:return: True if a sound is currently playing.
376-
"""
371+
"""True if a sound is currently playing."""
377372
return self.played_time < self.total_time

0 commit comments

Comments
 (0)