Skip to content

Commit

Permalink
Merge pull request #200 from thedropbears/more-fun-morse
Browse files Browse the repository at this point in the history
Make Morse code fun again
  • Loading branch information
auscompgeek authored Aug 4, 2024
2 parents 2d6231b + 6751df0 commit 223d90f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
32 changes: 22 additions & 10 deletions components/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ def climbing_arm_retracted(self) -> None:
self.high_priority_pattern = None

def morse(self) -> None:
self.pattern = Morse(HsvColour.ORANGE)
match wpilib.DriverStation.getAlliance():
case wpilib.DriverStation.Alliance.kBlue:
colour = HsvColour.BLUE
case wpilib.DriverStation.Alliance.kRed:
colour = HsvColour.RED
case _:
colour = HsvColour.ORANGE
if isinstance(self.pattern, Morse):
self.pattern.colour = colour
else:
self.pattern = Morse(colour)

def rainbow(self) -> None:
self.pattern = Rainbow(HsvColour.RED)
Expand Down Expand Up @@ -173,7 +183,6 @@ class Morse(TimeBasedPattern):
MESSAGES = (
"KILL ALL HUMANS",
"MORSE CODE IS FOR NERDS",
"HONEYBADGER DONT CARE",
"GLHF",
"I HATE MORSE CODE",
)
Expand Down Expand Up @@ -221,16 +230,20 @@ class Morse(TimeBasedPattern):

def __post_init__(self) -> None:
self.pick_new_message()
self.start_clock()

def start_clock(self) -> None:
self.start_time = self.clock()

def elapsed_time(self) -> float:
return self.clock() - self.start_time

def update(self) -> Hsv:
elapsed_time = self.elapsed_time()

# End of the message. Pick new one
if elapsed_time > self.message_time:
self.pick_new_message()
# End of message, repeat the message
self.start_clock()
return HsvColour.OFF.value

# TODO Might be better to store current token index and time?
Expand All @@ -255,7 +268,6 @@ def update(self) -> Hsv:

def pick_new_message(self) -> None:
# QUESTION? Should functions take args or assume previous step already done
self.start_time = self.clock() # Reset the time of the pattern
self.message = self.random_message()
self.morse_message = self.translate_message(self.message)
self.message_length = self.calculate_message_length(self.morse_message)
Expand All @@ -268,16 +280,16 @@ def random_message(self) -> str:
@classmethod
def translate_message(cls, message: str) -> str:
message = message.upper()
morse_message = ""
morse_message = []
for letter in message:
if letter == " ":
morse_message += " "
morse_message.append("")
continue
morse_message += cls.MORSE_TRANSLATION[letter] + " "
morse_message.append(cls.MORSE_TRANSLATION[letter])

# Add some space at end of message
morse_message += " "
return morse_message
morse_message.append(" ")
return " ".join(morse_message)

@classmethod
def calculate_message_length(cls, morse_message: str) -> int:
Expand Down
5 changes: 3 additions & 2 deletions robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ def disabledPeriodic(self) -> None:
self.chassis.update_odometry()

self.intake_component.maybe_reindex_deployment_encoder()
self.status_lights.execute()
self.vision_port.execute()
self.vision_starboard.execute()

Expand All @@ -228,14 +227,16 @@ def disabledPeriodic(self) -> None:
distance_between(intended_start_pose, current_pose)
< self.START_POS_TOLERANCE
):
self.status_lights.rainbow()
self.status_lights.morse()
else:
self.status_lights.invalid_start()
else:
self.status_lights.missing_start_pose()
else:
self.status_lights.missing_start_pose()

self.status_lights.execute()

def autonomousInit(self) -> None:
self.field.getObject("Intended start pos").setPoses([])

Expand Down

0 comments on commit 223d90f

Please sign in to comment.