diff --git a/src/Makefile b/src/Makefile index 4444023..28f1bac 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ -SRC = util.c irmc.c cwprotocol.c beep.c +SRC = irmc.c cwprotocol.c beep.c OBJ = ${SRC:.c=.o} LDFLAGS = -lportaudio -lpthread -lm -CFLAGS = -Wall -Wno-format-zero-length -Wno-unused-function +CFLAGS = -Wall INSTALLDIR = ${HOME}/bin all: options irmc @@ -19,7 +19,7 @@ options: irmc: ${OBJ} @echo CC -o $@ - @${CC} -o $@ ${OBJ} ${LDFLAGS} + @${CC} -o $@ ${OBJ} ${LDFLAGS} java: java -jar test/MorseKOB.jar @@ -27,12 +27,5 @@ java: clean: @echo cleaning @rm -f irmc irmc.core ${OBJ} - -linux: ${OBJ} - @echo CC -o irmc - @${CC} -o irmc ${OBJ} ${LDFLAGS} -lasound - - - install: irmc cp irmc ${INSTALLDIR} diff --git a/src/beep.c b/src/beep.c index 82ba80f..a98fe24 100644 --- a/src/beep.c +++ b/src/beep.c @@ -2,15 +2,16 @@ #include #include #include // for usleep() - +#include #include "beep.h" -#ifdef PORTAUDIO + // 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(). @@ -87,7 +88,7 @@ int buzzer_start(void) outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ outputParameters.channelCount = 1; /* stereo output */ outputParameters.sampleFormat = paUInt8; /* 32 bit floating point output */ - outputParameters.suggestedLatency = 30; //Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; + outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; err = Pa_OpenStream( @@ -133,18 +134,16 @@ error: 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) { - if (freq_hz > 0.0) { - buzzer_set_freq(freq_hz); - msleep(duration_sec*1000.); - buzzer_set_freq(0.); - } - else - { - msleep(duration_sec*1000.); - } + buzzer_set_freq(freq_hz); + msleep(duration_sec*1000.); + buzzer_set_freq(0.); return 0; } int beep_init() @@ -153,102 +152,31 @@ int beep_init() return 0; } -int beep_close() -{ - buzzer_stop(); - return 0; -} int beep_test(void) { + + // notes frequency chart: http://www.phy.mtu.edu/~suits/notefreqs.html + buzzer_start(); buzzer_set_freq(261); + msleep(250); + buzzer_set_freq(0); + msleep(250); + buzzer_set_freq(329); + msleep(250); + buzzer_set_freq(349); + msleep(250); + buzzer_set_freq(392); + msleep(250); + buzzer_set_freq(440); + msleep(250); + buzzer_set_freq(494); + msleep(250); + buzzer_beep(523, 200); + msleep(250); + buzzer_stop(); + return 0; } - -#endif - - - -// Raspi does not work with portaudio?! -#ifdef ALSA -// https://www.raspberrypi.org/forums/viewtopic.php?t=84485&p=603451 - -static char *device = "hw:0,0"; /* playback device */ -snd_output_t *output = NULL; - snd_pcm_t *handle; - -int beep(double freq_hz, double duration_sec) -{ -if (freq_hz <= 0.0) -{ - usleep(duration_sec*1000.*1000); -return 0; -} - double p1,p2,f1,f2; - unsigned int i,j; - snd_pcm_sframes_t frames; - - unsigned int up_count = (unsigned int)(SAMPLE_RATE*1.0 * duration_sec); - -//printf(" %d %f ", up_count, duration_sec); - p1 = p2 = f1 = f2 = 0.02;//SAMPLE_RATE/freq_hz;//0.02; -i=0; - for (i = 0; i < up_count; ) { - for (j = 0; j < FRAMES*2 ; j+=2) - //for (j = 0; j < up_count*2; j+=2) - { -i += 1; -if (i 0 && frames < FRAMES) - printf("Short write (expected %li, wrote %li)\n", FRAMES, (long)frames); - } - return 0; - -} -int beep_init() -{ - int err; - - if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { - printf("Playback open error: %s\n", snd_strerror(err)); - exit(EXIT_FAILURE); - } - if ((err = snd_pcm_set_params(handle, - SND_PCM_FORMAT_S16_LE, - SND_PCM_ACCESS_RW_INTERLEAVED, - 2, - SAMPLE_RATE, - 1, - 500000)) < 0) { /* 0.5sec */ - printf("Playback open error: %s\n", snd_strerror(err)); - exit(EXIT_FAILURE); - } - return 0; -} -int beep_close() -{ - snd_pcm_close(handle); - return 0; -} - -#endif diff --git a/src/beep.h b/src/beep.h index 4d1b04c..e3ebf83 100644 --- a/src/beep.h +++ b/src/beep.h @@ -1,27 +1,15 @@ +#include #include - -#ifdef __MACH__ -#define PORTAUDIO -#endif - -#ifdef __arm__ -//#define ALSA -#define PORTAUDIO -#endif - -#ifdef PORTAUDIO -#include -#endif +#include "portaudio.h" #define NUM_SECONDS (5) -#define SAMPLE_RATE (48000) +#define SAMPLE_RATE (44100) #define FRAMES_PER_BUFFER (64) #ifndef M_PI #define M_PI (3.14159265) #endif -#ifdef PORTAUDIO typedef struct { uint32_t total_count; @@ -32,32 +20,13 @@ typedef struct uint32_t freq; } paTestData; -static int patestCallback( const void *inputBuffer, void *outputBuffer, - unsigned long framesPerBuffer, - const PaStreamCallbackTimeInfo* timeInfo, - PaStreamCallbackFlags statusFlags, - void *userData ); - 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); -#endif - -#ifdef ALSA -#include -#include -#include - -#define FRAMES 32L //1024L //16384L - -int16_t buffer[FRAMES*2]; /* 16bit stereo sound samples */ - -#endif // compatibility to old interface int beep(double freq_hz, double duration_sec); int beep_init(); -int beep_close(); diff --git a/src/irmc.c b/src/irmc.c index d2d49d2..4f47daa 100644 --- a/src/irmc.c +++ b/src/irmc.c @@ -15,9 +15,18 @@ #include #include #include +#include +#include +#include - -// raspi wiring: http://raspberrypiguide.de/howtos/raspberry-pi-gpio-how-to/ +#ifdef __MACH__ + #include + #include +#else + #include + #include + #include +#endif //#define DEBUG 1 @@ -25,7 +34,6 @@ #include "cwprotocol.h" #include "beep.h" -#include "util.h" int serial_status = 0, fd_serial, numbytes; @@ -49,6 +57,59 @@ 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); +} + // disconnect from the server void inthandler(int sig) @@ -60,7 +121,6 @@ void inthandler(int sig) exit(1); } -// main tx loop void txloop (void) { key_press_t1 = fastclock(); @@ -93,7 +153,7 @@ void txloop (void) } } -// screen output + void message(int msg) { switch(msg){ @@ -229,9 +289,7 @@ 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; @@ -342,7 +400,7 @@ int main(int argc, char *argv[]) send(fd_socket, &disconnect_packet, SIZE_COMMAND_PACKET, 0); close(fd_socket); close(fd_serial); - beep_close(); + buzzer_stop(); exit(0); } diff --git a/src/util.c b/src/util.c deleted file mode 100644 index a4a9cbd..0000000 --- a/src/util.c +++ /dev/null @@ -1,103 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include // for usleep() - - -// Detect OSX -#ifdef __MACH__ - #include - #include -#endif - -// Detect Raspi -#ifdef __ARM - #include - #include - #include -#endif - - - - -#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); -} - -void msleep(int d) -{ - usleep(d*1000); -} - diff --git a/src/util.h b/src/util.h deleted file mode 100644 index da857f3..0000000 --- a/src/util.h +++ /dev/null @@ -1,5 +0,0 @@ -void current_utc_time(struct timespec *ts); -long fastclock(void); -int kbhit (void); -void msleep(int d); -void *get_in_addr(struct sockaddr *sa);