Skip to content

Commit ee0cb78

Browse files
author
Frank Hunleth
committed
Add an option to loop Morse code sequences
This provides an easy way to workaround the end of the Morse code message being clipped when the `:hz` setting is incorrect. What happens in this situation is that Linux's rounds its timers up, Delux doesn't know about it, and then Delux calculates the entire sequences duration incorrectly and cuts it off early. Some people may also want their Morse code messages to loop anyway.
1 parent e78f1a3 commit ee0cb78

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

lib/delux/morse.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ defmodule Delux.Morse do
6363
?@ => ".--.-."
6464
}
6565

66-
@type options() :: [words_per_minute: number()]
66+
@type options() :: [words_per_minute: number(), loop?: boolean()]
6767

6868
@doc """
6969
Convert a text string to Morse code
@@ -76,12 +76,14 @@ defmodule Delux.Morse do
7676
Options:
7777
7878
* `:words_per_minute` - the rate at which to send the message
79+
* `:loop?` - set to `true` to repeat the message (defaults to `false`)
7980
"""
8081
@spec encode(RGB.color(), String.t(), options()) :: Program.t()
8182
def encode(c, string, options \\ []) do
8283
{r, g, b} = RGB.new(c)
8384

8485
words_per_minute = options[:words_per_minute] || @default_words_per_minute
86+
loop? = options[:loop?] || false
8587

8688
if words_per_minute < 1 or words_per_minute > 100 do
8789
raise ArgumentError, "Invalid :words_per_minute"
@@ -90,13 +92,14 @@ defmodule Delux.Morse do
9092
up_string = String.upcase(string, :ascii)
9193
base_elements = morse(up_string, words_per_minute)
9294
duration = duration_elements(base_elements)
95+
mode = if loop?, do: :simple_loop, else: :one_shot
9396

9497
%Program{
9598
red: elements(r, base_elements, duration),
9699
green: elements(g, base_elements, duration),
97100
blue: elements(b, base_elements, duration),
98101
description: "Morse code: #{up_string}",
99-
mode: :one_shot
102+
mode: mode
100103
}
101104
end
102105

test/delux/morse_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,22 @@ defmodule Delux.MorseTest do
7070
assert p.mode == :one_shot
7171
assert Program.text_description(p) == "Morse code: E E E"
7272
end
73+
74+
test "looping a message" do
75+
p = Morse.encode(:red, "T", words_per_minute: 20, loop?: true)
76+
77+
# -
78+
assert p.red == [
79+
{1, 180},
80+
{1, 0},
81+
{0, 420},
82+
{0, 0}
83+
]
84+
85+
assert p.green == [{0, 600}, {0, 0}]
86+
assert p.blue == [{0, 600}, {0, 0}]
87+
88+
assert p.mode == :simple_loop
89+
assert Program.text_description(p) == "Morse code: T"
90+
end
7391
end

0 commit comments

Comments
 (0)