Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ueokande
Created April 1, 2016 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ueokande/aa8127f052532de8d613d91867618fb0 to your computer and use it in GitHub Desktop.
Save ueokande/aa8127f052532de8d613d91867618fb0 to your computer and use it in GitHub Desktop.
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