Skip to content

Commit da9216b

Browse files
author
Kevin J Walters
committed
Replacing lambda with def per request from code review. adafruit#1185
1 parent 77554db commit da9216b

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

CLUE_Rock_Paper_Scissors/advanced/clue-multi-rpsgame.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# clue-multi-rpsgame v1.19
1+
# clue-multi-rpsgame v1.20
22
# CircuitPython massively multiplayer rock paper scissors game over Bluetooth LE
33

44
# Tested with CLUE and Circuit Playground Bluefruit Alpha with TFT Gizmo
@@ -138,16 +138,15 @@ def tftGizmoPresent():
138138
_button_b = digitalio.DigitalInOut(board.BUTTON_B)
139139
_button_b.switch_to_input(pull=digitalio.Pull.DOWN)
140140
if display is None:
141-
button_left = lambda: _button_a.value
142-
button_right = lambda: _button_b.value
143-
##button_left = lambda: cp.button_a
144-
##button_right = lambda: cp.button_b
145-
141+
def button_left():
142+
return _button_a.value
143+
def button_right():
144+
return _button_b.value
146145
else:
147-
button_left = lambda: _button_b.value
148-
button_right = lambda: _button_a.value
149-
##button_left = lambda: cp.button_b
150-
##button_right = lambda: cp.button_a
146+
def button_left():
147+
return _button_b.value
148+
def button_right():
149+
return _button_a.value
151150

152151
else:
153152
# CLUE with builtin screen (240x240)
@@ -166,10 +165,11 @@ def tftGizmoPresent():
166165
_button_a.switch_to_input(pull=digitalio.Pull.UP)
167166
_button_b = digitalio.DigitalInOut(board.BUTTON_B)
168167
_button_b.switch_to_input(pull=digitalio.Pull.UP)
169-
button_left = lambda: not _button_a.value
170-
button_right = lambda: not _button_b.value
171-
##button_left = lambda: clue.button_a
172-
##button_right = lambda: clue.button_b
168+
def button_left():
169+
return not _button_a.value
170+
def button_right():
171+
return not _button_b.value
172+
173173

174174
blankScreen(display, pixels)
175175

@@ -296,6 +296,20 @@ def addPlayer(name, addr_text, address, ad):
296296
rps_display.fadeUpDown("down")
297297
addPlayer(my_name, addrToText(ble.address_bytes), None, None)
298298

299+
300+
# These two functions mainly serve to adapt the call back arguments
301+
# to the called functions which do not use them
302+
def jgAdCallbackFlashBLE(_a, _b, _c):
303+
"""Used in broadcastAndReceive to flash the NeoPixels
304+
when advertising messages are received."""
305+
return rps_display.flashBLE()
306+
307+
def jgEndscanCallback(_a, _b, _c):
308+
"""Used in broadcastAndReceive to allow early termination of the scanning
309+
when the left button is pressed.
310+
Button may need to be held down for a second."""
311+
return button_left()
312+
299313
# Join Game
300314
gc.collect()
301315
d_print(2, "GC before JG", gc.mem_free())
@@ -307,11 +321,10 @@ def addPlayer(name, addr_text, address, ad):
307321
jg_msg,
308322
scan_time=JG_MSG_TIME_S,
309323
scan_response_request=True,
310-
ad_cb=((lambda _a, _b, _c:
311-
rps_display.flashBLE())
324+
ad_cb=(jgAdCallbackFlashBLE
312325
if JG_FLASH
313326
else None),
314-
endscan_cb=lambda _a, _b, _c: button_left(),
327+
endscan_cb=jgEndscanCallback,
315328
name_cb=addPlayer)
316329
del _ # To clean-up with GC below
317330
sample.stop()

CLUE_Rock_Paper_Scissors/advanced/rps_display.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,14 @@ def showGameResultNeoPixels(self, pla, sco, rounds_tot=None):
578578
gradually lighting up pixels in a circle using rainbow
579579
colours for each revolution of the NeoPixels starting at orange.
580580
"""
581+
def selectSecondElem(seq):
582+
"""Used in a sort() method to sort my second element in a list."""
583+
return seq[1]
584+
585+
# Sort the scores into highest first order
581586
idx_n_score = [(s, sco[s]) for s in range(len(sco))]
582-
idx_n_score.sort(key=lambda s: s[1], reverse=True)
587+
idx_n_score.sort(key=selectSecondElem, reverse=True)
588+
##idx_n_score.sort(key=lambda s: s[1], reverse=True)
583589

584590
bg_col = BLACK
585591
for idx, score in idx_n_score:

CLUE_Rock_Paper_Scissors/simple/clue-simple-rpsgame.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# clue-simple-rpsgame v1.2
1+
# clue-simple-rpsgame v1.3
22
# CircuitPython rock paper scissors game over Bluetooth LE
33

44
# Tested with CLUE and Circuit Playground Bluefruit Alpha with TFT Gizmo
@@ -96,11 +96,15 @@ def tftGizmoPresent():
9696
_button_b = digitalio.DigitalInOut(board.BUTTON_B)
9797
_button_b.switch_to_input(pull=digitalio.Pull.DOWN)
9898
if display is None:
99-
button_left = lambda: _button_a.value
100-
button_right = lambda: _button_b.value
99+
def button_left():
100+
return _button_a.value
101+
def button_right():
102+
return _button_b.value
101103
else:
102-
button_left = lambda: _button_b.value
103-
button_right = lambda: _button_a.value
104+
def button_left():
105+
return _button_b.value
106+
def button_right():
107+
return _button_a.value
104108

105109
else:
106110
# CLUE with builtin screen (240x240)
@@ -113,8 +117,10 @@ def tftGizmoPresent():
113117
_button_a.switch_to_input(pull=digitalio.Pull.UP)
114118
_button_b = digitalio.DigitalInOut(board.BUTTON_B)
115119
_button_b.switch_to_input(pull=digitalio.Pull.UP)
116-
button_left = lambda: not _button_a.value
117-
button_right = lambda: not _button_b.value
120+
def button_left():
121+
return not _button_a.value
122+
def button_right():
123+
return not _button_b.value
118124

119125
if display is None:
120126
print("FATAL:", "This version of program only works with a display")

0 commit comments

Comments
 (0)