From 9a488f14d2efb1c4f1aae71d21248b2c733dd7c8 Mon Sep 17 00:00:00 2001 From: Alberto J Sanchez Date: Mon, 14 Mar 2016 17:57:35 +0100 Subject: [PATCH] Added morse related files --- Sending custom msg morse-coded/Makefile | 21 +++ Sending custom msg morse-coded/main.cpp | 148 +++++++++++++++++ Sending custom msg morse-coded/readme.txt | 18 +++ Sending custom msg morse-coded/sock_client.py | 153 ++++++++++++++++++ 4 files changed, 340 insertions(+) create mode 100644 Sending custom msg morse-coded/Makefile create mode 100644 Sending custom msg morse-coded/main.cpp create mode 100644 Sending custom msg morse-coded/readme.txt create mode 100644 Sending custom msg morse-coded/sock_client.py diff --git a/Sending custom msg morse-coded/Makefile b/Sending custom msg morse-coded/Makefile new file mode 100644 index 0000000..6f343cc --- /dev/null +++ b/Sending custom msg morse-coded/Makefile @@ -0,0 +1,21 @@ +all : gmain cmain + +clean : + rm -f gmain + rm -f cmain + +grun : gmain + ./gmain + +crun : cmain + ./cmain + + +gmain : main.cpp + g++ -Wall -O2 -std=c++11 -pthread -lrt -o gmain main.cpp + +cmain : main.cpp + clang++ -Wall -O2 -std=c++11 -stdlib=libc++ -pthread -lrt -o cmain main.cpp + + +.PHONY : all clean run diff --git a/Sending custom msg morse-coded/main.cpp b/Sending custom msg morse-coded/main.cpp new file mode 100644 index 0000000..2f339c5 --- /dev/null +++ b/Sending custom msg morse-coded/main.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// SYSTEM BUS RADIO +// https://github.com/fulldecent/system-bus-radio +// Copyright 2016 William Entriken +// C++11 port by Ryou Ezoe +// Wrapper C++ Server by Alberto Sanchez, Pedro J. Martinez, Jorge Rivera + +using namespace std; + +#define SERVER_PORT htons(50007) + +std::mutex m ; +std::condition_variable cv ; +std::chrono::high_resolution_clock::time_point mid ; +std::chrono::high_resolution_clock::time_point reset ; + + +void boost_song() +{ + using namespace std::chrono ; + + while( true ) + { + std::unique_lock lk{m} ; + cv.wait( lk ) ; + + std::atomic x{0} ; + while( high_resolution_clock::now() < mid ) + { + ++x ; + } + std::this_thread::sleep_until( reset ) ; + } +} + +void square_am_signal(float time, float frequency) +{ + using namespace std::chrono ; + + std::cout << "Playing / " << time << " seconds / " << frequency << " Hz\n" ; + + seconds const sec{1} ; + nanoseconds const nsec{ sec } ; + using rep = nanoseconds::rep ; + auto nsec_per_sec = nsec.count() ; + + nanoseconds const period( static_cast( nsec_per_sec / frequency) ) ; + + auto start = high_resolution_clock::now() ; + auto const end = start + nanoseconds( static_cast(time * nsec_per_sec) ) ; + + while (high_resolution_clock::now() < end) + { + mid = start + period / 2 ; + reset = start + period ; + + cv.notify_all() ; + std::this_thread::sleep_until( reset ) ; + start = reset; + } +} + +vector split(string str, char delimiter) { + vector internal; + stringstream ss(str); // Turn the string into a stream. + string tok; + + while(getline(ss, tok, delimiter)) { + internal.push_back(tok); + } + + return internal; +} + +int main(int argc, char* args[]){ + + for ( unsigned i = 0 ; i < std::thread::hardware_concurrency() ; ++i ) + { + std::thread t( boost_song ) ; + t.detach() ; + } + + vector v; + int dur; + int freq; + float duration; + + char buffer[1000]; + int n; + + int serverSock=socket(AF_INET, SOCK_STREAM, 0); + + sockaddr_in serverAddr; + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = SERVER_PORT; + serverAddr.sin_addr.s_addr = INADDR_ANY; + + /* bind (this socket, local address, address length) + bind server socket (serverSock) to server address (serverAddr). + Necessary so that server can use a specific port */ + bind(serverSock, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)); + + // wait for a client + /* listen (this socket, request queue length) */ + listen(serverSock,1); + sockaddr_in clientAddr; + socklen_t sin_size=sizeof(struct sockaddr_in); + int clientSock=accept(serverSock,(struct sockaddr*)&clientAddr, &sin_size); + + while (1 == 1) { + bzero(buffer, 1000); + + //receive a message from a client + n = read(clientSock, buffer, 500); + cout << "Confirmation code " << n << endl; + cout << "Server received: " << buffer << endl; + + v = split(buffer, '-'); + dur = std::stoi(v[0]); + freq = std::stoi(v[1]); + duration = (float)dur / 1000; + + square_am_signal(duration, freq); + } + + close(serverSock); + return 0; + +} + diff --git a/Sending custom msg morse-coded/readme.txt b/Sending custom msg morse-coded/readme.txt new file mode 100644 index 0000000..efd1780 --- /dev/null +++ b/Sending custom msg morse-coded/readme.txt @@ -0,0 +1,18 @@ +// SYSTEM BUS RADIO +// https://github.com/fulldecent/system-bus-radio +// Copyright 2016 William Entriken +// Wrapper C++ Server and Python Client Morse messages by Alberto Sanchez, Pedro J. Martinez, Jorge Rivera + +This code uses "Using counter and threads". + +It opens a socket listening at a specified port, 50007, but you can change it. + +When it receives a message with format "duration-frequency" i.e.: "500-2500" (duration in milliseconds and frequency in hertz), calls "square_am_signal" function to get emitted the radiofrequency. + +We have developed a Python client (sock_client.py). This code encodes custom string in morse code and send it to specified socket port (50007). + +How to use? + +1. Compile main.cpp +2. Run ./gmain. This process keeps listening at 50007 port (you can change it). +3. $ python sock_client.py "Message to be sent" diff --git a/Sending custom msg morse-coded/sock_client.py b/Sending custom msg morse-coded/sock_client.py new file mode 100644 index 0000000..bfe7228 --- /dev/null +++ b/Sending custom msg morse-coded/sock_client.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# SYSTEM BUS RADIO +# https://github.com/fulldecent/system-bus-radio +# Copyright 2016 William Entriken +# Python Client Morse messages by Alberto Sanchez, Pedro J. Martinez, Jorge Rivera + +import socket +import sys +import time + +HOST, PORT = "localhost", 50007 +# frequency in hertz +freq = 2000 +try: + st = sys.argv[1] +except: + st = "test" + +def chartomorse(character): + if character == 'a' or character == 'A': + morsecode = '.-' + if character == 'b' or character == 'B': + morsecode = '-...' + if character == 'c' or character == 'C': + morsecode = '-.-.' + if character == 'd' or character == 'D': + morsecode = '-..' + if character == 'e' or character == 'E': + morsecode = '.' + if character == 'f' or character == 'F': + morsecode = '..-.' + if character == 'g' or character == 'G': + morsecode = '--.' + if character == 'h' or character == 'H': + morsecode = '....' + if character == 'i' or character == 'I': + morsecode = '..' + if character == 'j' or character == 'J': + morsecode = '.---' + if character == 'k' or character == 'K': + morsecode = '-.-' + if character == 'l' or character == 'L': + morsecode = '.-..' + if character == 'm' or character == 'M': + morsecode = '--' + if character == 'n' or character == 'N': + morsecode = '-.' + if character == 'ñ' or character == 'Ñ': + morsecode = '--.--' + if character == 'o' or character == 'O': + morsecode = '---' + if character == 'p' or character == 'P': + morsecode = '.--.' + if character == 'q' or character == 'Q': + morsecode = '--.-' + if character == 'r' or character == 'R': + morsecode = '.-.' + if character == 's' or character == 'S': + morsecode = '...' + if character == 't' or character == 'T': + morsecode = '_' + if character == 'u' or character == 'U': + morsecode = '..-' + if character == 'v' or character == 'V': + morsecode = '...-' + if character == 'w' or character == 'W': + morsecode = '.--' + if character == 'x' or character == 'X': + morsecode = '-..-' + if character == 'y' or character == 'Y': + morsecode = '-.--' + if character == 'z' or character == 'Z': + morsecode = '--..' + if character == '0': + morsecode = '-----' + if character == '1': + morsecode = '.----' + if character == '2': + morsecode = '..---' + if character == '3': + morsecode = '...--' + if character == '4': + morsecode = '....-' + if character == '5': + morsecode = '.....' + if character == '6': + morsecode = '-....' + if character == '7': + morsecode = '--...' + if character == '8': + morsecode = '---..' + if character == '9': + morsecode = '----.' + if character == '.': + morsecode = '.-.-.-' + if character == ',': + morsecode = '--..--' + if character == '?': + morsecode = '..--..' + if character == '!': + morsecode = '-.-.--' + if character == ' ': + morsecode = ' ' + return morsecode + +def morsetoduration(char): + + duration = 1 + if char == "-": + duration = 0.500 + if char == ".": + duration = 1 + return duration + +def sendMess(duration, frequency): + + s.send(b''+str(duration)+'-'+str(frequency)) + print str(duration)+'-'+str(frequency) + + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.connect((HOST, PORT)) + +result = "" +for letter in st: + result += chartomorse(letter)+" " +print result + +# Dot 50ms, Dash 150ms, Space between symbols (sbs) 50ms, Space between characters (sbc) 150ms, Space between words (sbw) 350ms + +dot = 50 +dash = 3 * dot +sbs = dot +sbc = 3 * dot +sbw = 7 * dot + +for chmorse in result: + print chmorse + if chmorse == ".": + sendMess(dot, freq) + time.sleep(float(dot+sbs)/1000) + elif chmorse == "-": + sendMess(dash, freq) + time.sleep(float(dot+sbs)/1000) + elif chmorse == " ": + time.sleep(float(sbc)/1000) + elif chmorse == " ": + time.sleep(float(sbw)/1000) + +s.shutdown(socket.SHUT_RDWR) +s.close()