Skip to content

Instantly share code, notes, and snippets.

@samcamwilliams
Last active January 1, 2017 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samcamwilliams/19339ea2d8431f27728ec446a7b4583a to your computer and use it in GitHub Desktop.
Save samcamwilliams/19339ea2d8431f27728ec446a7b4583a to your computer and use it in GitHub Desktop.
An Erlang script that uses morse code to send messages to @lelandbatey's keyboard.
#!/usr/bin/env escript
%%% USAGE: ./keyboard_morse STRING
%%% Uses morse code to send messages to a keyboard light that flashes
%%% when a web server request is made. See here[1] for details.
%%% This script could be modified to communicate with other kinds of
%%% devices by changing the emit_flash/0 function.
%%%
%%% [1] http://lelandbatey.com/posts/2016/12/Making-lights-blink-for-each-HTTP-request/
main([Str]) ->
inets:start(),
send(list_to_morse(Str)),
ok.
send([]) -> ok;
send([X|R]) ->
emit_flash(),
receive
stop -> ok
after (case X of long -> 1000; short -> 500 end) -> send(R)
end.
emit_flash() ->
httpc:request("http://lelandbatey.com/").
list_to_morse(Str) ->
lists:flatten(
lists:map(fun(X) -> char_to_morse(X) end, string:to_upper(Str))
).
char_to_morse($A) -> [short, long];
char_to_morse($B) -> [long, short, short, short];
char_to_morse($C) -> [long, short, long, short];
char_to_morse($D) -> [long, short, short];
char_to_morse($E) -> [short];
char_to_morse($F) -> [short, short, long, short];
char_to_morse($G) -> [long, long, short];
char_to_morse($H) -> [short, short, short, short];
char_to_morse($I) -> [short, short];
char_to_morse($J) -> [short, long, long, long];
char_to_morse($K) -> [long, short, long];
char_to_morse($L) -> [short, long, short, short];
char_to_morse($M) -> [long, long];
char_to_morse($N) -> [long, short];
char_to_morse($O) -> [long, long, long];
char_to_morse($P) -> [short, long, long, short];
char_to_morse($Q) -> [long, long, short, long];
char_to_morse($R) -> [short, long, short];
char_to_morse($S) -> [short, short, short];
char_to_morse($T) -> [long];
char_to_morse($U) -> [short, short, long];
char_to_morse($V) -> [short, short, short, long];
char_to_morse($W) -> [short, long, long];
char_to_morse($X) -> [long, short, short, long];
char_to_morse($Y) -> [long, short, long, long];
char_to_morse($Z) -> [long, long, short, short];
char_to_morse($,) -> [long, long, short, short, long, long];
char_to_morse($.) -> [short, long, short, long, short, long].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment