Created
April 1, 2016 23:38
-
-
Save ueokande/aa8127f052532de8d613d91867618fb0 to your computer and use it in GitHub Desktop.
Morse Code for LED in Linux
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
#include <iostream> | |
#include <chrono> | |
#include <thread> | |
#include <vector> | |
#include <cstdio> | |
constexpr std::chrono::milliseconds operator""_tick(unsigned long long int tick) { | |
return std::chrono::milliseconds(tick * 200); | |
} | |
int main () { | |
using namespace std::chrono_literals; | |
const std::vector<std::vector<int>> codes = { | |
// A -- Z | |
{0, 1}, {1, 0, 0, 0}, {1, 0, 1, 0}, {1, 0, 0}, {0}, | |
{0, 0, 1, 0}, {1, 1, 0}, {0, 0, 0, 0}, {0, 0}, {0, 1, 1, 1}, | |
{1, 0, 1}, {0, 1, 0, 0}, {1, 1}, {1, 0}, {1, 1, 1}, | |
{0, 1, 1, 0}, {1, 1, 0, 1}, {0, 1, 0}, {0, 0, 0}, {1}, | |
{0, 0, 1}, {0, 0, 0, 1}, {0, 1, 1}, {1, 0, 0, 1}, {1, 0, 1, 1}, | |
{1, 1, 0, 0}, | |
// 0 -- 9 | |
{0, 1, 1, 1, 1}, {0, 0, 1, 1, 1}, {0, 0, 0, 1, 1}, {0, 0, 0, 0, 1}, | |
{0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 1, 0, 0, 0}, {1, 1, 1, 0, 0}, | |
{1, 1, 1, 1, 0}, {1, 1, 1, 1, 1}, | |
}; | |
int c; | |
while((c = std::getchar()) != EOF) { | |
c = std::tolower(c); | |
int index = -1; | |
if ('a' <= c && c <= 'z') { | |
index = c - 'a'; | |
} else if ('0' <= c && c <= '9') { | |
index = c - '0' + 26; | |
} else if (' ' == c) { | |
std::this_thread::sleep_for(7_tick); | |
} | |
if (index == -1) continue; | |
std::this_thread::sleep_for(3_tick); | |
for (const auto key : codes[index]) { | |
std::cout << '1'; | |
std::cout.flush(); | |
if (key) { | |
std::this_thread::sleep_for(3_tick); | |
} else { | |
std::this_thread::sleep_for(1_tick); | |
} | |
std::cout << '0'; | |
std::cout.flush(); | |
std::this_thread::sleep_for(1_tick); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment