Compare commits

...

136 Commits

Author SHA1 Message Date
Gerolf Ziegenhain 2c2ff2d2ed raspi 2016-10-04 21:29:16 +02:00
Gerolf Ziegenhain 4c7442e3ea wip 2016-10-04 21:23:07 +02:00
Gerolf Ziegenhain 2c7de4792d wip 2016-10-04 21:21:59 +02:00
Gerolf Ziegenhain 496aa90bfe wip 2016-10-04 21:19:18 +02:00
Gerolf Ziegenhain 827798b86b wip 2016-10-04 21:19:06 +02:00
Gerolf Ziegenhain 5f9609ce0c wip 2016-10-04 21:18:09 +02:00
Gerolf Ziegenhain 75c77b0cac wip 2016-10-04 21:13:07 +02:00
Gerolf Ziegenhain 7bf9183401 wip 2016-10-04 21:10:34 +02:00
Gerolf Ziegenhain 746b1dcfe5 wip 2016-10-04 21:10:13 +02:00
Gerolf Ziegenhain 63300fa955 wip 2016-10-04 21:07:05 +02:00
Gerolf Ziegenhain a43019e565 wip 2016-10-04 21:05:21 +02:00
Gerolf Ziegenhain 370858ce8e wip 2016-10-04 21:02:06 +02:00
Gerolf Ziegenhain a62c28dcdf rm tx_method 2016-10-04 20:59:40 +02:00
Gerolf Ziegenhain d8815db878 serial_sep 2016-10-04 20:58:30 +02:00
Gerolf Ziegenhain d6d0679fb5 wip 2016-10-04 20:54:13 +02:00
Gerolf Ziegenhain 9d28c29a51 wip 2016-10-04 20:48:42 +02:00
Gerolf Ziegenhain 5d1ea8138b wip 2016-10-04 20:48:17 +02:00
Gerolf Ziegenhain af81f9a084 raspi 2016-10-04 20:45:48 +02:00
Gerolf Ziegenhain 1145e2769e wip 2016-10-04 19:07:03 +02:00
Gerolf Ziegenhain 516c1b4a57 wip 2016-10-04 19:06:37 +02:00
Gerolf Ziegenhain 1db2782667 wiring 2016-10-04 19:00:49 +02:00
Gerolf Ziegenhain ba6ece9ddd wiring 2016-10-04 18:53:27 +02:00
Gerolf Ziegenhain 4f9169d026 wiring 2016-10-04 18:52:52 +02:00
Gerolf Ziegenhain e36b01ce41 wiring 2016-10-04 18:48:43 +02:00
Gerolf Ziegenhain efeee5b0ee done 2016-10-04 18:37:28 +02:00
Gerolf Ziegenhain e01c86a248 add util files 2016-10-04 18:36:58 +02:00
Gerolf Ziegenhain 6163a138ce add util files 2016-10-04 18:35:56 +02:00
Gerolf Ziegenhain 5dad7c3f64 add util files 2016-10-04 18:34:50 +02:00
Gerolf Ziegenhain 8622552f86 add util files 2016-10-04 18:34:04 +02:00
Gerolf Ziegenhain 1d3b674ecc rm old linux code 2016-10-04 18:32:07 +02:00
Gerolf Ziegenhain 81316638fc adjust raspi latency 2016-10-04 18:27:45 +02:00
Gerolf Ziegenhain 83d152c2bc rm zero string warning 2016-10-04 18:26:05 +02:00
Gerolf Ziegenhain fddeaab8ed raspi clean header 2016-10-04 18:24:09 +02:00
Gerolf Ziegenhain 197ac3224e add raspi audio fix 2016-10-04 18:22:50 +02:00
Gerolf Ziegenhain 7ead5be229 add raspi audio fix 2016-10-04 18:22:16 +02:00
Gerolf Ziegenhain 46842ab42b revert changes - raspi identified 2016-10-04 18:20:09 +02:00
Gerolf Ziegenhain 86e7cc039b wip 2016-10-04 18:12:10 +02:00
Gerolf Ziegenhain 5e586d03d9 wip 2016-10-04 18:11:29 +02:00
Gerolf Ziegenhain bf0ebc9f6e wip 2016-10-04 18:10:23 +02:00
Gerolf Ziegenhain 8da5cc22d6 wip 2016-10-04 18:02:49 +02:00
Gerolf Ziegenhain ff773f1f4e wip 2016-10-04 18:01:29 +02:00
Gerolf Ziegenhain 05ad58cb09 wip 2016-10-04 17:59:33 +02:00
Gerolf Ziegenhain 08423de98c wip 2016-10-04 17:58:12 +02:00
Gerolf Ziegenhain a9371e16d4 wip 2016-10-04 17:56:07 +02:00
Gerolf Ziegenhain 57f6c84f88 wip 2016-10-04 17:54:36 +02:00
Gerolf Ziegenhain 15ab7fbbcc wip 2016-10-04 17:53:18 +02:00
Gerolf Ziegenhain c60842a89f wip 2016-10-04 17:52:49 +02:00
Gerolf Ziegenhain a5b7aed310 wip 2016-10-04 17:52:12 +02:00
Gerolf Ziegenhain 49d87c5cc8 wip 2016-10-04 17:50:37 +02:00
Gerolf Ziegenhain 1c1ef6ca4d wip 2016-10-04 17:49:09 +02:00
Gerolf Ziegenhain bd54a9cec4 wip 2016-10-04 17:48:46 +02:00
Gerolf Ziegenhain 5b51a3459a wip 2016-10-04 17:47:51 +02:00
Gerolf Ziegenhain ab06957571 wip 2016-10-04 17:47:18 +02:00
Gerolf Ziegenhain 108d04efd2 wip 2016-10-04 17:46:36 +02:00
Gerolf Ziegenhain aaf24cf35d wip 2016-10-04 17:34:24 +02:00
Gerolf Ziegenhain d77de7e47c wip 2016-10-04 17:34:17 +02:00
Gerolf Ziegenhain b2418e4aa6 wip 2016-10-04 17:32:56 +02:00
Gerolf Ziegenhain d4531a3f73 wip 2016-10-04 17:31:46 +02:00
Gerolf Ziegenhain 13a1af9ed4 wip 2016-10-04 17:31:03 +02:00
Gerolf Ziegenhain 43ac116425 wip 2016-10-04 17:30:33 +02:00
Gerolf Ziegenhain 08f5c20420 wip 2016-10-04 17:28:06 +02:00
Gerolf Ziegenhain e78a722025 wip 2016-10-04 17:27:38 +02:00
Gerolf Ziegenhain 2e975376dc wip 2016-10-04 17:24:05 +02:00
Gerolf Ziegenhain b227bd5d90 wip 2016-10-04 17:23:47 +02:00
Gerolf Ziegenhain 5ac4d0f63f wip 2016-10-04 17:22:11 +02:00
Gerolf Ziegenhain 9be4962d53 wip 2016-10-04 17:21:24 +02:00
Gerolf Ziegenhain 9fdfc71ab3 wip 2016-10-04 17:19:14 +02:00
Gerolf Ziegenhain 3c469edf69 wip 2016-10-04 17:14:43 +02:00
Gerolf Ziegenhain 4a2e3ca88f wip 2016-10-04 17:14:08 +02:00
Gerolf Ziegenhain 4f08a0c6c7 wip 2016-10-04 17:11:18 +02:00
Gerolf Ziegenhain eee931feb1 wip 2016-10-04 17:08:47 +02:00
Gerolf Ziegenhain 96264ece63 wip 2016-10-04 17:08:16 +02:00
Gerolf Ziegenhain 6e24a9e0ba wip 2016-10-04 17:02:16 +02:00
Gerolf Ziegenhain 1262a6abfd wip 2016-10-04 17:01:56 +02:00
Gerolf Ziegenhain 9dd1055495 wip 2016-10-04 17:00:43 +02:00
Gerolf Ziegenhain 96c1a5c030 wip 2016-10-04 16:59:59 +02:00
Gerolf Ziegenhain ae5dffa383 wip 2016-10-04 16:59:41 +02:00
Gerolf Ziegenhain 0d9924f9e6 wip 2016-10-04 16:56:31 +02:00
Gerolf Ziegenhain b398e6df30 wip 2016-10-04 16:55:30 +02:00
Gerolf Ziegenhain a56a475361 wip 2016-10-04 16:54:30 +02:00
Gerolf Ziegenhain b3938f5a5e wip 2016-10-04 16:53:37 +02:00
Gerolf Ziegenhain 28767e86a6 wip 2016-10-04 16:53:16 +02:00
Gerolf Ziegenhain 82611ac618 wip 2016-10-04 16:52:23 +02:00
Gerolf Ziegenhain 2c236c0b25 wip 2016-10-04 16:52:16 +02:00
Gerolf Ziegenhain 55f4a137b2 wip 2016-10-04 16:51:52 +02:00
Gerolf Ziegenhain 609a9e2b3c wip 2016-10-04 16:51:01 +02:00
Gerolf Ziegenhain bc66ce5855 wip 2016-10-04 16:50:10 +02:00
Gerolf Ziegenhain 592de3d5a9 wip 2016-10-04 16:49:43 +02:00
Gerolf Ziegenhain 5995b5e619 wip 2016-10-04 16:46:47 +02:00
Gerolf Ziegenhain d62992fdf1 wip 2016-10-04 16:45:58 +02:00
Gerolf Ziegenhain 0417c67854 wip 2016-10-04 16:44:37 +02:00
Gerolf Ziegenhain 6142bab18b wip 2016-10-04 16:41:45 +02:00
Gerolf Ziegenhain 8142e10343 wip 2016-10-04 16:40:57 +02:00
Gerolf Ziegenhain fc605357df wip 2016-10-04 16:39:46 +02:00
Gerolf Ziegenhain a61c528315 wip 2016-10-04 16:38:51 +02:00
Gerolf Ziegenhain b24c2d5bb3 wip 2016-10-04 16:38:04 +02:00
Gerolf Ziegenhain 2a72f535a7 wip 2016-10-04 16:37:06 +02:00
Gerolf Ziegenhain f3b0df241d wip 2016-10-04 16:35:21 +02:00
Gerolf Ziegenhain 06a2dec810 wip 2016-10-04 16:33:14 +02:00
Gerolf Ziegenhain 4c62e4f573 wip 2016-10-04 16:31:36 +02:00
Gerolf Ziegenhain e340b9f9be wip 2016-10-04 16:31:19 +02:00
Gerolf Ziegenhain 72ab6292fd wip 2016-10-04 16:29:58 +02:00
Gerolf Ziegenhain 8da3e55d8b wip 2016-10-04 16:27:25 +02:00
Gerolf Ziegenhain 0dbe192f7a wip 2016-10-04 16:26:49 +02:00
Gerolf Ziegenhain b3e2c0fa72 wip 2016-10-04 16:26:12 +02:00
Gerolf Ziegenhain 98ec981c98 wip 2016-10-04 16:25:50 +02:00
Gerolf Ziegenhain 934b28aa1f wip 2016-10-04 16:24:34 +02:00
Gerolf Ziegenhain 5d045fa8b4 beep close 2016-10-03 21:24:44 +02:00
Gerolf Ziegenhain e8b8e526f6 rm restart 2016-10-03 21:09:26 +02:00
Gerolf Ziegenhain a056678346 restarting beep 2016-10-03 21:08:29 +02:00
Gerolf Ziegenhain 40853a0d4c add close function wrapper 2016-10-03 21:05:44 +02:00
Gerolf Ziegenhain 2cfb08aa18 . 2016-10-03 17:59:43 +02:00
Gerolf Ziegenhain ab83ca4738 . 2016-10-03 17:58:42 +02:00
Gerolf Ziegenhain 436d30d5e1 raspi hack begin 2016-10-03 17:47:10 +02:00
Gerolf Ziegenhain c7e095b145 hack 2016-10-03 17:16:47 +02:00
Gerolf Ziegenhain c4a2a45805 get rid of known warnings 2016-10-03 17:08:09 +02:00
Gerolf Ziegenhain a76682e1ab raspi 2016-10-03 17:06:40 +02:00
Gerolf Ziegenhain ef4c54eeb9 cleanup 2016-10-03 17:06:22 +02:00
Gerolf Ziegenhain 84691e75bf osx dependencies 2016-10-03 17:04:04 +02:00
Gerolf Ziegenhain f77a4c5821 linux dependencies 2016-10-03 17:03:25 +02:00
Gerolf Ziegenhain 6d5f7173a3 reduce known warnings 2016-10-03 16:57:46 +02:00
Gerolf Ziegenhain 736ce016ad raspi build instructions 2016-10-03 16:56:38 +02:00
Gerolf Ziegenhain 19842349d9 add fct to header 2016-10-03 16:54:54 +02:00
Gerolf Ziegenhain a447eed37c arm 2016-10-03 16:53:11 +02:00
Gerolf Ziegenhain 183e60aa16 adjust interface number 2016-10-03 16:30:35 +02:00
Gerolf Ziegenhain fde22697d8 cleanup beep buzzer 2016-10-03 16:26:58 +02:00
Gerolf Ziegenhain 5286a77c70 reduced docu 2016-10-03 16:25:37 +02:00
Gerolf Ziegenhain b29dc7281a cleanup beep 2016-10-03 16:23:41 +02:00
Gerolf Ziegenhain ac49de555a new beep without jitter 2016-10-03 16:18:56 +02:00
Gerolf Ziegenhain 07d5b97912 wip 2016-10-03 16:15:34 +02:00
Gerolf Ziegenhain b8603fd9d0 wip 2016-10-03 16:13:59 +02:00
Gerolf Ziegenhain 6d8fee68f4 example 2016-10-03 16:02:17 +02:00
Gerolf Ziegenhain 9838719a50 protocol 2016-09-24 17:03:30 +02:00
Gerolf Ziegenhain c12478241a rename 2016-09-24 16:56:31 +02:00
Gerolf Ziegenhain 5842d754da wip 2016-09-24 16:55:57 +02:00
Gerolf Ziegenhain 54cb98634e sort functions 2016-09-24 16:55:36 +02:00
9 changed files with 427 additions and 334 deletions

View File

@ -2,35 +2,19 @@ irmc - Internet Relay Morse Code
================================
IRMC stands for Internet Relay Morse Code and is an implementation of [MOIP](http://8ch9azbsfifz.github.io/moip/).
# How to build?
## Install dependency: morse keyer library
```
wget https://github.com/8cH9azbsFifZ/morse/archive/v0.1.tar.gz
tar xzf v0.1.tar.gz
cd morse-0.1
libtoolize
./autogen.sh
./configure --with-portaudio
# Building
## On Linux
sudo apt-get install -y alsa-oss oss-compat build-essential autoconf libao-dev libtool libportaudio-dev portaudio19-dev
make
### On Raspi (GPIO Interface)
Follow: http://wiringpi.com/download-and-install/
make raspi
## On OSX
brew install portaudio
make
sudo make install
```
## Debian (Wheezy)
Some dependencies have to be installed:
```
apt-get install -y alsa-oss oss-compat build-essential autoconf libao-dev libtool
```
Afterwards compilation with `make` should work. If something went wrong, you may have
to adjust your `LD_LIBRARY_PATH`. Alternatively try:
```
LD_LIBRARY_PATH=/usr/local/lib ./irmc mtc-kob.dyndns.org 7890 33 123
```
## OSX (Yosemite)
Compilation with make :)
For the USB serial devices you need a PL2303 driver
(i.e. [PL2303_Serial-USB_on_OSX_Lion.pkg](http://changux.co/osx-installer-to-pl2303-serial-usb-on-osx-lio/)).
# Hardware interface options
A good description on how to build different interfaces (telegraph key, sounder or both)

View File

@ -1,7 +1,7 @@
SRC = irmc.c cwprotocol.c
SRC = irmc.c cwprotocol.c beep.c util.c
OBJ = ${SRC:.c=.o}
LDFLAGS = -L/usr/local/lib -L/opt/local/lib -lportaudio -lpthread -lm -lmorse
CFLAGS = -I/usr/local/include -I/opt/local/include -Wall
LDFLAGS = -lportaudio -lpthread -lm
CFLAGS = -Wall -Wno-format-zero-length
INSTALLDIR = ${HOME}/bin
all: options irmc
@ -21,6 +21,10 @@ irmc: ${OBJ}
@echo CC -o $@
@${CC} -o $@ ${OBJ} ${LDFLAGS}
raspi:
@${CC} -c -DRASPI ${CFLAGS} ${SRC}
@${CC} -o irmc ${OBJ} ${LDFLAGS} -lwiringPi
java:
java -jar test/MorseKOB.jar

170
src/beep.c Normal file
View File

@ -0,0 +1,170 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h> // for usleep()
#include <portaudio.h>
#include "beep.h"
#ifdef RASPI
#define RASPI_AUDIO_LATENCY_FIX (30./5.) // https://app.assembla.com/spaces/portaudio/tickets/246-paex_sine-choppy-on-raspberry-pi---defaultlowoutputlatency-too-low/details
#else
#define RASPI_AUDIO_LATENCY_FIX (1.)
#endif
// http://stackoverflow.com/questions/7678470/generating-sound-of-a-particular-frequency-using-gcc-in-ubuntu
static PaStream *stream;
static paTestData data;
/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int patestCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
uint8_t *out = (uint8_t*)outputBuffer;
unsigned long i;
uint32_t freq = data->freq;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
for( i=0; i<framesPerBuffer; i++ )
{
if(data->up_count > 0 && data->total_count == data->up_count) {
*out++ = 0x00;
continue;
}
data->total_count++;
if(freq != data->prev_freq) {
data->counter = 0;
}
if(freq) {
int overflow_max = SAMPLE_RATE / freq;
uint32_t data_cnt = data->counter % overflow_max;
if(data_cnt > overflow_max/2)
*out++ = 0xff;
else {
*out++ = 0x00;
}
data->counter++;
}
else {
data->counter = 0;
*out++ = 0;
}
data->prev_freq = freq;
}
return paContinue;
}
void buzzer_set_freq(int frequency)
{
data.up_count = 0; // do not stop!
data.freq = frequency;
}
void buzzer_beep(int frequency, int msecs)
{
data.total_count = 0;
data.up_count = SAMPLE_RATE * msecs / 1000;
data.freq = frequency;
}
int buzzer_start(void)
{
PaStreamParameters outputParameters;
PaError err;
err = Pa_Initialize();
if( err != paNoError ) goto error;
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
outputParameters.channelCount = 1; /* stereo output */
outputParameters.sampleFormat = paUInt8; /* 32 bit floating point output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency * RASPI_AUDIO_LATENCY_FIX;
outputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream(
&stream,
NULL, /* no input */
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
patestCallback,
&data );
if( err != paNoError ) goto error;
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
return err;
error:
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return err;
}
int buzzer_stop()
{
PaError err = 0;
err = Pa_StopStream( stream );
if( err != paNoError ) goto error;
err = Pa_CloseStream( stream );
if( err != paNoError ) goto error;
Pa_Terminate();
return err;
error:
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return err;
}
void msleep(int d){
usleep(d*1000);
}
int beep(double freq_hz, double duration_sec)
{
buzzer_set_freq(freq_hz);
msleep(duration_sec*1000.);
buzzer_set_freq(0.);
return 0;
}
int beep_init()
{
buzzer_start();
return 0;
}
int beep_test(void)
{
buzzer_start();
buzzer_set_freq(261);
msleep(250);
buzzer_set_freq(0);
buzzer_stop();
return 0;
}

View File

@ -1,46 +1,3 @@
/** @file paex_sine.c
@ingroup examples_src
@brief Play a sine wave for several seconds.
@author Ross Bencina <rossb@audiomulch.com>
@author Phil Burk <philburk@softsynth.com>
*/
/*
* $Id$
*
* This program uses the PortAudio Portable Audio Library.
* For more information see: http://www.portaudio.com/
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* The text above constitutes the entire PortAudio license; however,
* the PortAudio community also makes the following non-binding requests:
*
* Any person wishing to distribute modifications to the Software is
* requested to send the modifications to the original developer so that
* they can be incorporated into the canonical version. It is also
* requested that these non-binding requests be included along with the
* license above.
*/
#include <stdio.h>
#include <math.h>
#include "portaudio.h"
@ -53,126 +10,23 @@
#define M_PI (3.14159265)
#endif
#define TABLE_SIZE (200)
typedef struct
{
float sine[TABLE_SIZE];
int left_phase;
int right_phase;
char message[20];
}
paTestData;
uint32_t total_count;
uint32_t up_count;
/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int patestCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
float *out = (float*)outputBuffer;
unsigned long i;
uint32_t counter;
uint32_t prev_freq;
uint32_t freq;
} paTestData;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
for( i=0; i<framesPerBuffer; i++ )
{
*out++ = data->sine[data->left_phase]; /* left */
*out++ = data->sine[data->right_phase]; /* right */
data->left_phase += 1;
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
}
return paContinue;
}
void buzzer_set_freq(int frequency);
void buzzer_beep(int frequency, int msecs);
int buzzer_start(void);
int buzzer_stop();
void msleep(int d);
int beep_test(void);
/*
* This routine is called by portaudio when playback is done.
*/
static void StreamFinished( void* userData )
{
paTestData *data = (paTestData *) userData;
printf( "Stream Completed: %s\n", data->message );
}
/*******************************************************************/
void beep_init(void);
int beep(double freq_hz, double duration_sec)
{
PaStreamParameters outputParameters;
PaStream *stream;
PaError err;
paTestData data;
int i;
printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
/* initialise sinusoidal wavetable */
for( i=0; i<TABLE_SIZE; i++ )
{
data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
}
data.left_phase = data.right_phase = 0;
err = Pa_Initialize();
if( err != paNoError ) goto error;
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
if (outputParameters.device == paNoDevice) {
fprintf(stderr,"Error: No default output device.\n");
goto error;
}
outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream(
&stream,
NULL, /* no input */
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
patestCallback,
&data );
if( err != paNoError ) goto error;
sprintf( data.message, "No Message" );
err = Pa_SetStreamFinishedCallback( stream, &StreamFinished );
if( err != paNoError ) goto error;
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
printf("Play for %d seconds.\n", NUM_SECONDS );
Pa_Sleep( duration_sec * 1000 );
err = Pa_StopStream( stream );
if( err != paNoError ) goto error;
err = Pa_CloseStream( stream );
if( err != paNoError ) goto error;
Pa_Terminate();
printf("Test finished.\n");
return err;
error:
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return err;
}
// compatibility to old interface
int beep(double freq_hz, double duration_sec);
int beep_init();

View File

@ -1,6 +1,18 @@
#include <stdio.h>
#include <sys/socket.h>
#include "cwprotocol.h"
/* Global variables */
struct command_packet_format connect_packet = {CON, DEFAULT_CHANNEL};
struct command_packet_format disconnect_packet = {DIS, 0};
struct data_packet_format id_packet;
struct data_packet_format rx_data_packet;
struct data_packet_format tx_data_packet;
int tx_sequence = 0, rx_sequence;
int fd_socket;
int prepare_id (struct data_packet_format *id_packet, char *id)
{
id_packet->command = DAT;
@ -30,7 +42,44 @@ int prepare_tx (struct data_packet_format *tx_packet, char *id)
tx_packet->a21 = 0; /* These magic numbers was provided by Les Kerr */
tx_packet->a22 = 755;
tx_packet->a23 = 16777215;
snprintf(tx_packet->status, SIZE_STATUS, "?");
snprintf(tx_packet->status, SIZE_STATUS, "?"); // this shall include the sent character
return 0;
}
// connect to server and send my id.
void identifyclient(void)
{
tx_sequence++;
id_packet.sequence = tx_sequence;
send(fd_socket, &connect_packet, SIZE_COMMAND_PACKET, 0);
send(fd_socket, &id_packet, SIZE_DATA_PACKET, 0);
}
int send_latch (void)
{
int i;
tx_sequence++;
tx_data_packet.sequence = tx_sequence;
tx_data_packet.code[0] = -1;
tx_data_packet.code[1] = 1;
tx_data_packet.n = 2;
for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0);
tx_data_packet.n = 0;
return 0;
}
int send_unlatch (void)
{
int i;
tx_sequence++;
tx_data_packet.sequence = tx_sequence;
tx_data_packet.code[0] = -1;
tx_data_packet.code[1] = 2;
tx_data_packet.n = 2;
for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0);
tx_data_packet.n = 0;
return 0;
}

View File

@ -1,4 +1,4 @@
#define INTERFACE_VERSION "irmc v0.02"
#define INTERFACE_VERSION "irmc v0.3.3"
// Structures for the packets: unsigned short command
#define DIS 0x0002 // disconnect
@ -32,7 +32,7 @@ struct data_packet_format{
unsigned int a23;
signed int code[SIZE_CODE];
unsigned int n;
char status[SIZE_STATUS]; /* This is called version in MorseKob */
char status[SIZE_STATUS]; /* This is called version in MorseKob, in cwcom this transmits the sent character?! */
char a4[8];
};
@ -40,6 +40,23 @@ struct data_packet_format{
// Define the packets used
#define DEFAULT_CHANNEL 103
/* Define functions provided by cwprotocol */
int prepare_id (struct data_packet_format *id_packet, char *id);
int prepare_tx (struct data_packet_format *tx_packet, char *id);
void identifyclient (void);
int send_latch (void);
int send_unlatch (void);
/* Define external struct for global variables */
extern struct command_packet_format connect_packet;
extern struct command_packet_format disconnect_packet;
extern struct data_packet_format id_packet;
extern struct data_packet_format rx_data_packet;
extern struct data_packet_format tx_data_packet;
extern int tx_sequence, rx_sequence;
extern int fd_socket;

View File

@ -15,41 +15,21 @@
#include <pthread.h>
#include <signal.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#define BEEP_MORSE
#ifdef BEEP_MORSE
#include <morse/beep.h>
#else
#include "portaudio.h"
#endif
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#else
#include <linux/ioctl.h>
#include <asm-generic/ioctl.h>
#include <asm-generic/termios.h>
#endif
//#define DEBUG 1
#define MAXDATASIZE 1024 // max number of bytes we can get at once
#include "cwprotocol.h"
#include "beep.h"
#include "util.h"
struct command_packet_format connect_packet = {CON, DEFAULT_CHANNEL};
struct command_packet_format disconnect_packet = {DIS, 0};
struct data_packet_format id_packet;
struct data_packet_format rx_data_packet;
struct data_packet_format tx_data_packet;
// http://raspberrypiguide.de/howtos/raspberry-pi-gpio-how-to/
#ifdef RASPI
#include <wiringPi.h>
#define TX_RASPI_PIN 5
#endif
int serial_status = 0, fd_serial, fd_socket, numbytes;
int tx_sequence = 0, rx_sequence;
int serial_status = 0, fd_serial, numbytes;
double tx_timeout = 0;
long tx_timer = 0;
@ -59,8 +39,9 @@ long tx_timer = 0;
/* TX Methods */
#define TX_NONE 0
#define TX_SERIAL 1
#define TX_KEYBOARD 2
//#define TX_SERIAL 1
//#define TX_KEYBOARD 2 // not implemented yet
#define TX_RASPI 3
long key_press_t1;
long key_release_t1;
@ -71,76 +52,15 @@ char last_sender[16];
int translate = 1;
int audio_status = 1;
/* portable time, as listed in https://gist.github.com/jbenet/1087739 */
void current_utc_time(struct timespec *ts) {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, ts);
#endif
}
/* a better clock() in milliseconds */
long fastclock(void)
{
struct timespec t;
long r;
current_utc_time (&t);
r = t.tv_sec * 1000;
r = r + t.tv_nsec / 1000000;
return r;
}
int kbhit (void)
{
struct timeval tv;
fd_set rdfs;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rdfs);
FD_SET (STDIN_FILENO, &rdfs);
select (STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &rdfs);
}
/* get sockaddr, IPv4 or IPv6: */
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
// connect to server and send my id.
void identifyclient(void)
{
tx_sequence++;
id_packet.sequence = tx_sequence;
send(fd_socket, &connect_packet, SIZE_COMMAND_PACKET, 0);
send(fd_socket, &id_packet, SIZE_DATA_PACKET, 0);
}
// disconnect from the server
void inthandler(int sig)
{
signal(sig, SIG_IGN);
send(fd_socket, &disconnect_packet, SIZE_COMMAND_PACKET, 0);
close(fd_socket);
#ifdef TX_SERIAL
close(fd_serial);
#endif
exit(1);
}
@ -153,18 +73,32 @@ void txloop (void)
tx_data_packet.code[tx_data_packet.n - 1] =
(int) ((key_press_t1 - key_release_t1) * -1);
//printf("space: %i\n", tx_data_packet.code[tx_data_packet.n -1]);
#ifdef DEBUG
printf("space: %i\n", tx_data_packet.code[tx_data_packet.n -1]);
#endif
#ifdef TX_SERIAL
while(serial_status & TIOCM_DSR) ioctl(fd_serial, TIOCMGET, &serial_status);
#endif
#ifdef RASPI
while(digitalRead(TX_RASPI_PIN)==1) { }
#endif
key_release_t1 = fastclock();
tx_data_packet.n++;
tx_data_packet.code[tx_data_packet.n - 1] =
(int) ((key_release_t1 - key_press_t1) * 1);
//printf("mark: %i\n", tx_data_packet.code[tx_data_packet.n -1]);
#ifdef DEBUG
printf("mark: %i\n", tx_data_packet.code[tx_data_packet.n -1]);
#endif
while(1){
#ifdef TX_SERIAL
ioctl(fd_serial, TIOCMGET, &serial_status);
if(serial_status & TIOCM_DSR) break;
#endif
#ifdef RASPI
if(digitalRead(TX_RASPI_PIN)==1) break;
#endif
tx_timeout = fastclock() - key_release_t1;
if(tx_timeout > TX_TIMEOUT) return;
}
@ -176,31 +110,6 @@ void txloop (void)
}
}
int send_latch (void)
{
int i;
tx_sequence++;
tx_data_packet.sequence = tx_sequence;
tx_data_packet.code[0] = -1;
tx_data_packet.code[1] = 1;
tx_data_packet.n = 2;
for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0);
tx_data_packet.n = 0;
return 0;
}
int send_unlatch (void)
{
int i;
tx_sequence++;
tx_data_packet.sequence = tx_sequence;
tx_data_packet.code[0] = -1;
tx_data_packet.code[1] = 2;
tx_data_packet.n = 2;
for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0);
tx_data_packet.n = 0;
return 0;
}
void message(int msg)
{
@ -246,7 +155,6 @@ int main(int argc, char *argv[])
int channel;
char id[SIZE_ID];
char serialport[64];
int tx_method = TX_NONE;
// Set default values
snprintf(hostname, 64, "mtc-kob.dyndns.org");
@ -337,21 +245,30 @@ int main(int argc, char *argv[])
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof s);
fprintf(stderr, "Connected to %s.\n", s);
beep_init();
if ((strcmp (serialport, "")) != 0)
tx_method = TX_SERIAL;
if (tx_method == TX_SERIAL) {
fd_serial = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd_serial == -1) {
fprintf(stderr,"Unable to open serial port %s.\n", serialport);
}
#ifdef TX_SERIAL
fd_serial = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd_serial == -1) {
fprintf(stderr,"Unable to open serial port %s.\n", serialport);
}
#endif
#ifdef RASPI
if (wiringPiSetup() == -1)
{
fprintf(stderr,"Unable to setup wiringPi for PIN %d\n", TX_RASPI_PIN);
exit (1);
}
pinMode(TX_RASPI_PIN, INPUT);
#endif
freeaddrinfo(servinfo); /* all done with this structure */
key_release_t1 = fastclock();
identifyclient();
beep_init();
/* Main Loop */
for(;;) {
if(tx_timer == 0)
@ -419,14 +336,22 @@ int main(int argc, char *argv[])
#endif
tx_data_packet.n = 0;
}
if (tx_method == TX_SERIAL) {
ioctl(fd_serial,TIOCMGET, &serial_status);
if(serial_status & TIOCM_DSR){
txloop();
tx_timer = TX_WAIT;
message(1);
}
#ifdef TX_SERIAL
ioctl(fd_serial,TIOCMGET, &serial_status);
if(serial_status & TIOCM_DSR){
txloop();
tx_timer = TX_WAIT;
message(1);
}
#endif
#ifdef RASPI
if(digitalRead(5)==1){
txloop();
tx_timer = TX_WAIT;
message(1);
}
#endif
if(keepalive_t < 0 && tx_timer == 0){
#if DEBUG
@ -447,7 +372,10 @@ int main(int argc, char *argv[])
send(fd_socket, &disconnect_packet, SIZE_COMMAND_PACKET, 0);
close(fd_socket);
#ifdef TX_SERIAL
close(fd_serial);
#endif
buzzer_stop();
exit(0);
}

57
src/util.c Normal file
View File

@ -0,0 +1,57 @@
#include "util.h"
/* portable time, as listed in https://gist.github.com/jbenet/1087739 */
void current_utc_time(struct timespec *ts) {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, ts);
#endif
}
/* a better clock() in milliseconds */
long fastclock(void)
{
struct timespec t;
long r;
current_utc_time (&t);
r = t.tv_sec * 1000;
r = r + t.tv_nsec / 1000000;
return r;
}
int kbhit (void)
{
struct timeval tv;
fd_set rdfs;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rdfs);
FD_SET (STDIN_FILENO, &rdfs);
select (STDIN_FILENO+1, &rdfs, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &rdfs);
}
/* get sockaddr, IPv4 or IPv6: */
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

30
src/util.h Normal file
View File

@ -0,0 +1,30 @@
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <math.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <arpa/inet.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
void current_utc_time(struct timespec *ts);
long fastclock(void);
int kbhit (void);
void *get_in_addr(struct sockaddr *sa);