Skip to content

Instantly share code, notes, and snippets.

@U-1F992
Created January 17, 2024 16:04
Show Gist options
  • Save U-1F992/128fc0701667d6aebf4655b23e855eb4 to your computer and use it in GitHub Desktop.
Save U-1F992/128fc0701667d6aebf4655b23e855eb4 to your computer and use it in GitHub Desktop.
import math
import time
import typing
import serial
from Commands.PythonCommandBase import ImageProcPythonCommand
import qingpi
from qingpi import * # type: ignore
_Badge = tuple[int, int]
def polish_badge(
hold: HoldFunction,
release: ReleaseFunction,
sleep: typing.Callable[[float], None],
badge: _Badge,
duration: float,
):
r = 7
round = [
TouchScreen(int(badge[0] + math.sin(i) * r), int(badge[1] + math.cos(i) * r))
for i in [math.radians(i) for i in range(360) if i % 2 == 0]
]
release()
start_time = time.perf_counter()
try:
while time.perf_counter() - start_time < duration:
for ts in round:
hold(ts)
sleep(0)
finally:
release()
_Zero = typing.Literal[0]
_One = typing.Literal[1]
_PunchedCard = list[
tuple[_Zero, _Zero, _Zero, _Zero, _Zero, _Zero, _Zero, _Zero]
| tuple[_One, _Zero, _Zero, _Zero, _Zero, _Zero, _Zero, _Zero]
| tuple[_Zero, _One, _Zero, _Zero, _Zero, _Zero, _Zero, _Zero]
| tuple[_Zero, _Zero, _One, _Zero, _Zero, _Zero, _Zero, _Zero]
| tuple[_Zero, _Zero, _Zero, _One, _Zero, _Zero, _Zero, _Zero]
| tuple[_Zero, _Zero, _Zero, _Zero, _Zero, _Zero, _Zero, _Zero]
| tuple[_Zero, _Zero, _Zero, _Zero, _One, _Zero, _Zero, _Zero]
| tuple[_Zero, _Zero, _Zero, _Zero, _Zero, _One, _Zero, _Zero]
| tuple[_Zero, _Zero, _Zero, _Zero, _Zero, _Zero, _One, _Zero]
| tuple[_Zero, _Zero, _Zero, _Zero, _Zero, _Zero, _Zero, _One]
]
_Badges = tuple[_Badge, _Badge, _Badge, _Badge, _Badge, _Badge, _Badge, _Badge]
def play_music_with_badges(
hold: HoldFunction,
release: ReleaseFunction,
sleep: typing.Callable[[float], None],
badges: _Badges,
punched_card: _PunchedCard,
bpm: int,
resolution=1 / 16,
):
for row in punched_card:
start_time = time.perf_counter()
for i, _ in [(i, note) for i, note in enumerate(row) if note == 1]:
hold(TouchScreen(badges[i][0], badges[i][1]))
sleep(0.04)
release(TouchScreen)
if i != len(row) - 1:
sleep(0.03)
elapsed_time = time.perf_counter() - start_time
sleep_time = (60 / bpm * resolution) - elapsed_time
if sleep_time > 0:
sleep(sleep_time)
class PolishAndPlayMusicWithBadges(ImageProcPythonCommand):
NAME = "バッジを磨いて神社"
def __init__(self, cam, gui=None):
super().__init__(cam, gui)
def do(self):
if self.keys is None:
print("keys is None")
self.finish()
return
ser = self.keys.ser.ser
if not isinstance(ser, serial.Serial):
print("ser is Any other than serial.Serial")
self.finish()
return
hold, release = qingpi.init(ser)
badges = (
(55, 79),
(125, 79),
(195, 79),
(265, 75),
(55, 148),
(125, 148),
(195, 148),
(265, 148),
)
try:
for badge in badges:
polish_badge(hold, release, self.wait, badge, 28)
self.wait(5)
play_music_with_badges(
hold,
release,
self.wait,
badges,
[
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
#
(0, 0, 0, 0, 1, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 1, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 1, 0, 0, 0, 0),
(0, 0, 0, 1, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 1, 0, 0),
#
(0, 0, 0, 0, 1, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(1, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 1, 0, 0, 0, 0),
(0, 0, 1, 0, 0, 0, 0, 0),
(0, 1, 0, 0, 0, 0, 0, 0),
(0, 0, 1, 0, 0, 0, 0, 0),
#
(1, 0, 0, 0, 0, 0, 0, 0),
(0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0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
],
96,
1 / 2,
)
finally:
release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment