-
Notifications
You must be signed in to change notification settings - Fork 789
New Guide - rock, paper, scissors game for CLUE and CPB. #1185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6f04734
New Guide - rock, player, scissors game for CLUE and CPB.
e926eaa
Cleaning up comments and some docstrings. Dropping debug level from 3…
283c980
Audio wav files and image bmp file (sprite sheet) for advanced RPS game.
0919096
Merge branch 'master' into rockpaperscissors
kevinjwalters 86b7f12
Merge branch 'master' into rockpaperscissors
TheKitty 304aa80
Correcting/improving the error reporting for mulitple missing audio f…
3806b2f
pylint vs whitespace
a295bc4
Merge branch 'master' into rockpaperscissors
TheKitty ea82f2b
Renaming files to a more specific audio_files.
758280b
Changing evaluateRound() to return value to a form of enumerated valu…
e3dd2d2
Converting filter() to list comprehension with trailing if filter syn…
95acc97
Removing the C-esque pad removal in strUnpad() and replacing with rst…
2612984
Switching to the new match_prefixes for Advertisement matching which …
ff6fffe
Rearranging the text setting for the summary Label on round results s…
81fbdd7
Moving points values out to constants.
a157bbc
Changing assignments of None to _ to normal del for clean-up - this w…
77554db
Slowing down the bubble sort in showGameResultScreen() a little bit m…
3db3f47
Merge branch 'master' into rockpaperscissors
TheKitty 2feef1a
Merge branch 'master' into rockpaperscissors
TheKitty da9216b
Replacing lambda with def per request from code review. #1185
60b97cc
Merge branch 'rockpaperscissors' of https://github.com/kevinjwalters/…
ca15fb3
Changing the advertisements used by Advanced RPS Game to have manufac…
8e8b2f9
Merge branch 'master' into rockpaperscissors
kevinjwalters fe80b0e
Merge branch 'master' into rockpaperscissors
TheKitty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
556 changes: 556 additions & 0 deletions
556
CLUE_Rock_Paper_Scissors/advanced/clue-multi-rpsgame.py
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
237 changes: 237 additions & 0 deletions
237
CLUE_Rock_Paper_Scissors/advanced/rps_advertisements.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
# MIT License | ||
|
||
# Copyright (c) 2020 Kevin J. Walters | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
import struct | ||
|
||
from adafruit_ble.advertising import Advertisement, LazyObjectField | ||
from adafruit_ble.advertising.standard import ManufacturerData, ManufacturerDataField | ||
|
||
# These message should really include version numbers for the | ||
# the protocol and a descriptor for the encryption type | ||
|
||
# From adafruit_ble.advertising | ||
MANUFACTURING_DATA_ADT = 0xFF | ||
ADAFRUIT_COMPANY_ID = 0x0822 | ||
|
||
# pylint: disable=line-too-long | ||
# From https://github.com/adafruit/Adafruit_CircuitPython_BLE_BroadcastNet/blob/c6328d5c7edf8a99ff719c3b1798cb4111bab397/adafruit_ble_broadcastnet.py#L84-L85 | ||
ADAFRUIT_SEQ_ID = 0x0003 | ||
|
||
# According to https://github.com/adafruit/Adafruit_CircuitPython_BLE/blob/master/adafruit_ble/advertising/adafruit.py | ||
# 0xf000 (to 0xffff) is for range for Adafruit customers | ||
GM_JOIN_ID = 0xfe30 | ||
RPS_VERSION = 0xff30 | ||
RPS_ROUND_ID = 0xff74 | ||
RPS_ENC_DATA_ID = 0xff34 | ||
RPS_KEY_DATA_ID = 0xff54 | ||
RPS_ACK_ID = 0xff52 | ||
# These ID numbers have all been carefully selected to obtain a particular | ||
# ordering of the fields within the ManufacturerData based on the current | ||
# dict key ordering - this is bad practice and FRAGILE as the prefix | ||
# matching falls apart if this changes | ||
|
||
# Data formats for shared fields | ||
_DATA_FMT_ROUND = "B" | ||
_DATA_FMT_ACK = "B" | ||
_SEQ_FMT = "B" | ||
|
||
|
||
class RpsEncDataAdvertisement(Advertisement): | ||
"""An RPS (broadcast) message. | ||
This sends the encrypted choice of the player. | ||
This is not connectable and does not elicit a scan response | ||
based on defaults in Advertisement parent class. | ||
""" | ||
flags = None | ||
|
||
_PREFIX_FMT = "<B" "BHBH" | ||
_DATA_FMT_ENC_DATA = "8s" | ||
|
||
prefix = struct.pack( | ||
_PREFIX_FMT, | ||
struct.calcsize(_PREFIX_FMT) - 1, | ||
MANUFACTURING_DATA_ADT, | ||
ADAFRUIT_COMPANY_ID, | ||
struct.calcsize("<H" + _DATA_FMT_ENC_DATA), | ||
RPS_ENC_DATA_ID | ||
) | ||
manufacturer_data = LazyObjectField( | ||
ManufacturerData, | ||
"manufacturer_data", | ||
advertising_data_type=MANUFACTURING_DATA_ADT, | ||
company_id=ADAFRUIT_COMPANY_ID, | ||
key_encoding="<H" | ||
) | ||
|
||
sequence_number = ManufacturerDataField(ADAFRUIT_SEQ_ID, "<" + _SEQ_FMT) | ||
"""Sequence number of the data. Used in acknowledgements.""" | ||
|
||
enc_data = ManufacturerDataField(RPS_ENC_DATA_ID, "<" + _DATA_FMT_ENC_DATA) | ||
round_no = ManufacturerDataField(RPS_ROUND_ID, "<" + _DATA_FMT_ROUND) | ||
ack = ManufacturerDataField(RPS_ACK_ID, "<" + _DATA_FMT_ACK) | ||
"""Round number starting at 1.""" | ||
|
||
def __init__(self, *, enc_data=None, round_no=None, ack=None, sequence_number=None): | ||
"""ack must be set to () to send this optional, data-less field.""" | ||
super().__init__() | ||
if enc_data is not None: | ||
self.enc_data = enc_data | ||
if round_no is not None: | ||
self.round_no = round_no | ||
if ack is not None: | ||
self.ack = ack | ||
if sequence_number is not None: | ||
self.sequence_number = sequence_number | ||
|
||
|
||
class RpsKeyDataAdvertisement(Advertisement): | ||
"""An RPS (broadcast) message. | ||
This sends the key to decrypt the previous encrypted choice of the player. | ||
This is not connectable and does not elicit a scan response | ||
based on defaults in Advertisement parent class. | ||
""" | ||
flags = None | ||
|
||
_PREFIX_FMT = "<B" "BHBH" | ||
_DATA_FMT_KEY_DATA = "8s" | ||
|
||
# prefix appears to be used to determine whether an incoming | ||
# packet matches this class | ||
# The second struct.calcsize needs to include the _DATA_FMT for some | ||
# reason I either don't know or can't remember | ||
prefix = struct.pack( | ||
_PREFIX_FMT, | ||
struct.calcsize(_PREFIX_FMT) - 1, | ||
MANUFACTURING_DATA_ADT, | ||
ADAFRUIT_COMPANY_ID, | ||
struct.calcsize("<H" + _DATA_FMT_KEY_DATA), | ||
RPS_KEY_DATA_ID | ||
) | ||
kevinjwalters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
manufacturer_data = LazyObjectField( | ||
ManufacturerData, | ||
"manufacturer_data", | ||
advertising_data_type=MANUFACTURING_DATA_ADT, | ||
company_id=ADAFRUIT_COMPANY_ID, | ||
key_encoding="<H" | ||
) | ||
|
||
sequence_number = ManufacturerDataField(ADAFRUIT_SEQ_ID, "<" + _SEQ_FMT) | ||
"""Sequence number of the data. Used in acknowledgements.""" | ||
|
||
key_data = ManufacturerDataField(RPS_KEY_DATA_ID, "<" + _DATA_FMT_KEY_DATA) | ||
round_no = ManufacturerDataField(RPS_ROUND_ID, "<" + _DATA_FMT_ROUND) | ||
ack = ManufacturerDataField(RPS_ACK_ID, "<" + _DATA_FMT_ACK) | ||
"""Round number starting at 1.""" | ||
|
||
def __init__(self, *, key_data=None, round_no=None, ack=None, sequence_number=None): | ||
"""ack must be set to () to send this optional, data-less field.""" | ||
super().__init__() | ||
if key_data is not None: | ||
self.key_data = key_data | ||
if round_no is not None: | ||
self.round_no = round_no | ||
if ack is not None: | ||
self.ack = ack | ||
if sequence_number is not None: | ||
self.sequence_number = sequence_number | ||
|
||
|
||
class RpsRoundEndAdvertisement(Advertisement): | ||
"""An RPS (broadcast) message. | ||
This informs other players the round_no is complete. | ||
An important side-effect is acknowledgement of previous message. | ||
This is not connectable and does not elicit a scan response | ||
based on defaults in Advertisement parent class. | ||
""" | ||
flags = None | ||
|
||
_PREFIX_FMT = "<B" "BHBH" | ||
|
||
prefix = struct.pack( | ||
_PREFIX_FMT, | ||
struct.calcsize(_PREFIX_FMT) - 1, | ||
MANUFACTURING_DATA_ADT, | ||
ADAFRUIT_COMPANY_ID, | ||
struct.calcsize("<H" + _DATA_FMT_ROUND), | ||
RPS_ROUND_ID | ||
) | ||
kevinjwalters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
manufacturer_data = LazyObjectField( | ||
ManufacturerData, | ||
"manufacturer_data", | ||
advertising_data_type=MANUFACTURING_DATA_ADT, | ||
company_id=ADAFRUIT_COMPANY_ID, | ||
key_encoding="<H" | ||
) | ||
|
||
sequence_number = ManufacturerDataField(ADAFRUIT_SEQ_ID, "<" + _SEQ_FMT) | ||
"""Sequence number of the data. Used in acknowledgements.""" | ||
|
||
round_no = ManufacturerDataField(RPS_ROUND_ID, "<" + _DATA_FMT_ROUND) | ||
ack = ManufacturerDataField(RPS_ACK_ID, "<" + _DATA_FMT_ACK) | ||
"""Round number starting at 1.""" | ||
|
||
def __init__(self, *, round_no=None, ack=None, sequence_number=None): | ||
"""ack must be set to () to send this optional, data-less field.""" | ||
super().__init__() | ||
if round_no is not None: | ||
self.round_no = round_no | ||
if ack is not None: | ||
self.ack = ack | ||
if sequence_number is not None: | ||
self.sequence_number = sequence_number | ||
|
||
|
||
class JoinGameAdvertisement(Advertisement): | ||
"""A join game (broadcast) message used as the first message to work out who is playing. | ||
This is not connectable and does not elicit a scan response | ||
based on defaults in Advertisement parent class. | ||
""" | ||
flags = None | ||
|
||
_PREFIX_FMT = "<B" "BHBH" | ||
_DATA_FMT = "8s" # this NUL pads for 8s if necessary | ||
|
||
prefix = struct.pack( | ||
_PREFIX_FMT, | ||
struct.calcsize(_PREFIX_FMT) - 1, | ||
MANUFACTURING_DATA_ADT, | ||
ADAFRUIT_COMPANY_ID, | ||
##struct.calcsize("<H" + _SEQ_FMT + _DATA_FMT), | ||
struct.calcsize("<H" + _DATA_FMT), | ||
##struct.calcsize("<H"), | ||
GM_JOIN_ID | ||
) | ||
kevinjwalters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
manufacturer_data = LazyObjectField( | ||
ManufacturerData, | ||
"manufacturer_data", | ||
advertising_data_type=MANUFACTURING_DATA_ADT, | ||
company_id=ADAFRUIT_COMPANY_ID, | ||
key_encoding="<H" | ||
) | ||
|
||
game = ManufacturerDataField(GM_JOIN_ID, "<" + _DATA_FMT) | ||
"""RPS choice.""" | ||
|
||
def __init__(self, *, game=None): | ||
super().__init__() | ||
if game is not None: | ||
self.game = game |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# MIT License | ||
|
||
# Copyright (c) 2020 Kevin J. Walters | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from audiocore import WaveFile | ||
|
||
|
||
class SampleJukeboxError(OSError): | ||
"""Exception raised for any missing audio files. | ||
""" | ||
|
||
def __init__(self, files): | ||
self.files = files | ||
super().__init__("Missing audio files: " + ", ".join(files)) | ||
|
||
|
||
class SampleJukebox(): | ||
"""This plays wav files and tries to control the timing of memory | ||
allocations within the nRF52840 PWMAudioOut library to minimise | ||
the chance of MemoryError exceptions (2048 bytes).""" | ||
|
||
_file_buf = None # Use for WaveFile objects | ||
|
||
def _init_wave_files(self, files, directory): | ||
"""Open files from AUDIO_DIR and return a dict with FileIO objects | ||
or None if file not present.""" | ||
|
||
# 2048 triggers bug in https://github.com/adafruit/circuitpython/issues/3030 | ||
self._file_buf = bytearray(512) # DO NOT CHANGE size til #3030 is fixed | ||
|
||
missing = [] | ||
fhs = {} | ||
for file in files: | ||
wav_file = None | ||
filename = directory + "/" + file + ".wav" | ||
try: | ||
wav_file = open(filename, "rb") | ||
fhs[file] = WaveFile(wav_file, self._file_buf) | ||
except OSError: | ||
# OSError: [Errno 2] No such file/directory: 'filename.ext' | ||
missing.append(filename) | ||
|
||
# Raises an exception at the end to allow it to report ALL | ||
# of the missing files in one go to help out the user | ||
if missing: | ||
raise SampleJukeboxError(missing) | ||
self._wave_files = fhs | ||
|
||
|
||
def __init__(self, audio_device, files, | ||
directory="", error_output=None): | ||
self._audio_device = audio_device | ||
self._error_output = error_output | ||
self._wave_files = None # keep pylint happy | ||
self._init_wave_files(files, directory=directory) | ||
|
||
# play a file that exists to get m_alloc called now | ||
# but immediately stop it with pause() | ||
for wave_file in self._wave_files.values(): | ||
if wave_file is not None: | ||
self._audio_device.play(wave_file, loop=True) | ||
self._audio_device.pause() | ||
break | ||
|
||
|
||
def play(self, name, loop=False): | ||
wave_file = self._wave_files.get(name) | ||
if wave_file is None: | ||
return | ||
# This pairing of stop() and play() will cause an m_free | ||
# and immediate m_malloc() which reduces considerably the risk | ||
# of losing the 2048 contiguous bytes needed for this | ||
self._audio_device.stop() | ||
self._audio_device.play(wave_file, loop=loop) | ||
# https://github.com/adafruit/circuitpython/issues/2036 | ||
# is a general ticket about efficient audio buffering | ||
|
||
|
||
def playing(self): | ||
return self._audio_device.playing | ||
|
||
|
||
def wait(self): | ||
while self._audio_device.playing: | ||
pass | ||
|
||
|
||
def stop(self): | ||
self._audio_device.pause() # This avoid m_free |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.