Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Morse Code for LED in Linux
#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