Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added morse related files #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Sending custom msg morse-coded/Makefile
Original file line number Diff line number Diff line change
@@ -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
148 changes: 148 additions & 0 deletions Sending custom msg morse-coded/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <sstream>

// 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<std::mutex> lk{m} ;
cv.wait( lk ) ;

std::atomic<unsigned> 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<rep>( nsec_per_sec / frequency) ) ;

auto start = high_resolution_clock::now() ;
auto const end = start + nanoseconds( static_cast<rep>(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<string> split(string str, char delimiter) {
vector<string> 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<string> 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;

}

18 changes: 18 additions & 0 deletions Sending custom msg morse-coded/readme.txt
Original file line number Diff line number Diff line change
@@ -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"
153 changes: 153 additions & 0 deletions Sending custom msg morse-coded/sock_client.py
Original file line number Diff line number Diff line change
@@ -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()