Created
March 7, 2020 22:39
-
-
Save bettio/824dd62fd93e3e7c48076e5a89194f2e to your computer and use it in GitHub Desktop.
Elixir morse encode example - step 3
This file contains 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
def handle_req('GET', [], conn) do | |
body = """ | |
<html> | |
<body> | |
<h1>Morse Encoder</h1> | |
<form method=\"post\"> | |
<p>Text: <input type=\"text\" name=\"text\"></p> | |
<p>GPIO: <input type=\"text\" name=\"gpio\"></p> | |
<input type=\"submit\" value=\"Submit\"> | |
</form> | |
</body> | |
</html> | |
""" | |
:http_server.reply(200, body, conn) | |
end | |
def handle_req('POST', [], conn) do | |
params_body = :proplists.get_value(:body_chunk, conn) | |
params = :http_server.parse_query_string(params_body) | |
gpio_string = :proplists.get_value('gpio', params) | |
gpio_num = safe_list_to_integer(gpio_string) | |
text = :proplists.get_value('text', params, 'off') | |
morse_text = Encoder.morse_encode(text) | |
spawn(fn -> blink_led(morse_text) end) | |
body = [ | |
""" | |
<html> | |
<body> | |
<h1>Text Encoded</h1> | |
<p> | |
""", | |
morse_text, | |
""" | |
</p> | |
</body> | |
</html> | |
""" | |
] | |
:http_server.reply(200, body, conn) | |
end | |
defp safe_list_to_integer(l) do | |
try do | |
:erlang.list_to_integer(l) | |
catch | |
_, _ -> nil | |
end | |
end | |
defp blink_led(_morse_text) do | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment