revert changes - raspi identified

This commit is contained in:
Gerolf Ziegenhain 2016-10-04 18:20:09 +02:00
parent 86e7cc039b
commit 46842ab42b
6 changed files with 103 additions and 263 deletions

View File

@ -1,7 +1,7 @@
SRC = util.c irmc.c cwprotocol.c beep.c SRC = irmc.c cwprotocol.c beep.c
OBJ = ${SRC:.c=.o} OBJ = ${SRC:.c=.o}
LDFLAGS = -lportaudio -lpthread -lm LDFLAGS = -lportaudio -lpthread -lm
CFLAGS = -Wall -Wno-format-zero-length -Wno-unused-function CFLAGS = -Wall
INSTALLDIR = ${HOME}/bin INSTALLDIR = ${HOME}/bin
all: options irmc all: options irmc
@ -19,7 +19,7 @@ options:
irmc: ${OBJ} irmc: ${OBJ}
@echo CC -o $@ @echo CC -o $@
@${CC} -o $@ ${OBJ} ${LDFLAGS} @${CC} -o $@ ${OBJ} ${LDFLAGS}
java: java:
java -jar test/MorseKOB.jar java -jar test/MorseKOB.jar
@ -27,12 +27,5 @@ java:
clean: clean:
@echo cleaning @echo cleaning
@rm -f irmc irmc.core ${OBJ} @rm -f irmc irmc.core ${OBJ}
linux: ${OBJ}
@echo CC -o irmc
@${CC} -o irmc ${OBJ} ${LDFLAGS} -lasound
install: irmc install: irmc
cp irmc ${INSTALLDIR} cp irmc ${INSTALLDIR}

View File

@ -2,15 +2,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <unistd.h> // for usleep() #include <unistd.h> // for usleep()
#include <portaudio.h>
#include "beep.h" #include "beep.h"
#ifdef PORTAUDIO
// http://stackoverflow.com/questions/7678470/generating-sound-of-a-particular-frequency-using-gcc-in-ubuntu // http://stackoverflow.com/questions/7678470/generating-sound-of-a-particular-frequency-using-gcc-in-ubuntu
static PaStream *stream; static PaStream *stream;
static paTestData data; static paTestData data;
/* This routine will be called by the PortAudio engine when audio is needed. /* 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 ** 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(). ** 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.device = Pa_GetDefaultOutputDevice(); /* default output device */
outputParameters.channelCount = 1; /* stereo output */ outputParameters.channelCount = 1; /* stereo output */
outputParameters.sampleFormat = paUInt8; /* 32 bit floating point 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; outputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream( err = Pa_OpenStream(
@ -133,18 +134,16 @@ error:
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return err; return err;
} }
void msleep(int d){
usleep(d*1000);
}
int beep(double freq_hz, double duration_sec) int beep(double freq_hz, double duration_sec)
{ {
if (freq_hz > 0.0) { buzzer_set_freq(freq_hz);
buzzer_set_freq(freq_hz); msleep(duration_sec*1000.);
msleep(duration_sec*1000.); buzzer_set_freq(0.);
buzzer_set_freq(0.);
}
else
{
msleep(duration_sec*1000.);
}
return 0; return 0;
} }
int beep_init() int beep_init()
@ -153,102 +152,31 @@ int beep_init()
return 0; return 0;
} }
int beep_close()
{
buzzer_stop();
return 0;
}
int beep_test(void) int beep_test(void)
{ {
// notes frequency chart: http://www.phy.mtu.edu/~suits/notefreqs.html
buzzer_start(); buzzer_start();
buzzer_set_freq(261); 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(); buzzer_stop();
return 0; 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<up_count)
{
buffer[j] = 1000.0 * sin(p1);
buffer[j+1] = 1000.0 * sin(p2);
p1 += f1;
p2 += f2;
}
//else
//{buffer[j]=buffer[j+1]=0.;}
}
frames = snd_pcm_writei(handle, buffer, FRAMES);
if (frames < 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0) {
//break;
}
if (frames > 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

View File

@ -1,27 +1,15 @@
#include <stdio.h>
#include <math.h> #include <math.h>
#include "portaudio.h"
#ifdef __MACH__
#define PORTAUDIO
#endif
#ifdef __arm__
//#define ALSA
#define PORTAUDIO
#endif
#ifdef PORTAUDIO
#include <portaudio.h>
#endif
#define NUM_SECONDS (5) #define NUM_SECONDS (5)
#define SAMPLE_RATE (48000) #define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (64) #define FRAMES_PER_BUFFER (64)
#ifndef M_PI #ifndef M_PI
#define M_PI (3.14159265) #define M_PI (3.14159265)
#endif #endif
#ifdef PORTAUDIO
typedef struct typedef struct
{ {
uint32_t total_count; uint32_t total_count;
@ -32,32 +20,13 @@ typedef struct
uint32_t freq; uint32_t freq;
} paTestData; } 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_set_freq(int frequency);
void buzzer_beep(int frequency, int msecs); void buzzer_beep(int frequency, int msecs);
int buzzer_start(void); int buzzer_start(void);
int buzzer_stop(); int buzzer_stop();
void msleep(int d); void msleep(int d);
int beep_test(void); int beep_test(void);
#endif
#ifdef ALSA
#include <stdint.h>
#include <alsa/asoundlib.h>
#include <math.h>
#define FRAMES 32L //1024L //16384L
int16_t buffer[FRAMES*2]; /* 16bit stereo sound samples */
#endif
// compatibility to old interface // compatibility to old interface
int beep(double freq_hz, double duration_sec); int beep(double freq_hz, double duration_sec);
int beep_init(); int beep_init();
int beep_close();

View File

@ -15,9 +15,18 @@
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#ifdef __MACH__
// raspi wiring: http://raspberrypiguide.de/howtos/raspberry-pi-gpio-how-to/ #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 DEBUG 1
@ -25,7 +34,6 @@
#include "cwprotocol.h" #include "cwprotocol.h"
#include "beep.h" #include "beep.h"
#include "util.h"
int serial_status = 0, fd_serial, numbytes; int serial_status = 0, fd_serial, numbytes;
@ -49,6 +57,59 @@ char last_sender[16];
int translate = 1; int translate = 1;
int audio_status = 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 // disconnect from the server
void inthandler(int sig) void inthandler(int sig)
@ -60,7 +121,6 @@ void inthandler(int sig)
exit(1); exit(1);
} }
// main tx loop
void txloop (void) void txloop (void)
{ {
key_press_t1 = fastclock(); key_press_t1 = fastclock();
@ -93,7 +153,7 @@ void txloop (void)
} }
} }
// screen output
void message(int msg) void message(int msg)
{ {
switch(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), inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof s); s, sizeof s);
fprintf(stderr, "Connected to %s.\n", s); fprintf(stderr, "Connected to %s.\n", s);
beep_init(); beep_init();
if ((strcmp (serialport, "")) != 0) if ((strcmp (serialport, "")) != 0)
tx_method = TX_SERIAL; tx_method = TX_SERIAL;
@ -342,7 +400,7 @@ int main(int argc, char *argv[])
send(fd_socket, &disconnect_packet, SIZE_COMMAND_PACKET, 0); send(fd_socket, &disconnect_packet, SIZE_COMMAND_PACKET, 0);
close(fd_socket); close(fd_socket);
close(fd_serial); close(fd_serial);
beep_close(); buzzer_stop();
exit(0); exit(0);
} }

View File

@ -1,103 +0,0 @@
#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>
#include <time.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <unistd.h> // for usleep()
// Detect OSX
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
// Detect Raspi
#ifdef __ARM
#include <linux/ioctl.h>
#include <asm-generic/ioctl.h>
#include <asm-generic/termios.h>
#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);
}

View File

@ -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);