Factor out argument processing

Separate the argument processing so that main only processes the args
and calls another function. This way, the Python extension can call the
other function.
This commit is contained in:
Brandon Skari 2016-01-17 11:21:09 +07:00
parent 6b9cdd8fdc
commit a1b9330c41
2 changed files with 120 additions and 64 deletions

View File

@ -83,7 +83,6 @@ Optimize CPU on PWMFrequency
#define HEADER_SIZE 44
typedef unsigned char uchar ; // 8 bit
typedef unsigned short uint16 ; // 16 bit
typedef unsigned int uint ; // 32 bits
@ -109,6 +108,7 @@ uint32_t GlobalTabPwmFrequency[50];
char EndOfApp=0;
unsigned char loop_mode_flag=0;
char *FileName = 0;
void *rawFileMemory = NULL;
int FileInHandle; //Handle in Transport Stream File
static void
udelay(int us)
@ -956,7 +956,7 @@ int pitx_init(int SampleRate,double TuningFrequency)
}
void print_usage()
void print_usage(void)
{
fprintf(stderr,\
@ -1022,47 +1022,47 @@ int pitx_SetTuneFrequency(double Frequency)
return 1;
}
/** Wrapper around read. */
static ssize_t readFile(void *buffer, const size_t count) {
return read(FileInHandle, buffer, count);
}
static void resetFile(void) {
lseek(FileInHandle, 0, SEEK_SET);
}
static void* arrayBaseAddress;
static int arrayLength;
static int arrayPosition;
/** Wrapper around reading from memory with an interface similar to read. */
ssize_t readArray(void *buffer, const size_t count) {
if (arrayPosition >= arrayLength) {
return 0;
}
const int left = arrayLength - arrayPosition;
const int numBytesToCopy = left > count ? left : count;
memcpy(buffer, arrayBaseAddress + arrayPosition, numBytesToCopy);
arrayPosition += numBytesToCopy;
return numBytesToCopy;
}
void setUpReadArray(void* baseAddress, size_t length) {
arrayBaseAddress = baseAddress;
arrayLength = length;
arrayPosition = 0;
}
int
main(int argc, char **argv)
main(int argc, char* argv[])
{
int a;
int i;
//char pagemap_fn[64];
int OffsetModulation=1000;//TBR
int MicGain=100;
char NoUsePwmFrequency=0;
//unsigned char *data;
#define MODE_IQ 0
#define MODE_RF 1
#define MODE_RFA 2
#define MODE_IQ_FLOAT 3
#define MODE_VFO 4
char Mode=0;
int anyargs = 0;
char Mode = MODE_IQ; // By default
int SampleRate=48000;
float SetFrequency=1e6;//1MHZ
float ppmpll=0.0;
//Specific to ModeIQ
static signed short *IQArray=NULL;
//Specific to ModeIQ_FLOAT
static float *IQFloatArray=NULL;
//Specific to Mode RF
typedef struct {
double Frequency;
uint32_t WaitForThisSample;
} samplerf_t;
samplerf_t *TabRfSample;
fprintf(stdout,"rpitx Version %s compiled %s (F5OEO Evariste) running on ",PROGRAM_VERSION,__DATE__);
Mode=MODE_IQ; //By default
int anyargs = 0;
char NoUsePwmFrequency=0;
while(1)
{
@ -1077,7 +1077,7 @@ main(int argc, char **argv)
switch(a)
{
case 'i': // Frequency
case 'i': // File name
FileName = optarg;
break;
case 'f': // Frequency
@ -1138,20 +1138,6 @@ main(int argc, char **argv)
}/* end switch a */
}/* end while getopt() */
// Init Plls Frequency using ppm (or default)
PllFreq500MHZ=PLL_FREQ_500MHZ;
PllFreq500MHZ+=PllFreq500MHZ * (ppmpll / 1000000.0);
PllFreq1GHZ=PLL_FREQ_1GHZ;
PllFreq1GHZ+=PllFreq1GHZ * (ppmpll / 1000000.0);
PllFreq19MHZ=PLLFREQ_192;
PllFreq19MHZ+=PllFreq19MHZ * (ppmpll / 1000000.0);
//End of Init Plls
//Open File Input for modes which need it
if((Mode==MODE_IQ)||(Mode==MODE_IQ_FLOAT)||(Mode==MODE_RF)||(Mode==MODE_RFA))
{
@ -1166,6 +1152,53 @@ main(int argc, char **argv)
}
}
return pitx_run(Mode, SampleRate, SetFrequency, ppmpll, NoUsePwmFrequency, readFile, resetFile);
}
int pitx_run(
const char Mode,
int SampleRate,
const float SetFrequency,
const float ppmpll,
const char NoUsePwmFrequency,
ssize_t (*readWrapper)(void *buffer, size_t count),
void (*reset)(void)
) {
int i;
//char pagemap_fn[64];
int OffsetModulation=1000;//TBR
int MicGain=100;
//unsigned char *data;
//Specific to ModeIQ
static signed short *IQArray=NULL;
//Specific to ModeIQ_FLOAT
static float *IQFloatArray=NULL;
//Specific to Mode RF
typedef struct {
double Frequency;
uint32_t WaitForThisSample;
} samplerf_t;
samplerf_t *TabRfSample=NULL;
fprintf(stdout,"rpitx Version %s compiled %s (F5OEO Evariste) running on ",PROGRAM_VERSION,__DATE__);
// Init Plls Frequency using ppm (or default)
PllFreq500MHZ=PLL_FREQ_500MHZ;
PllFreq500MHZ+=PllFreq500MHZ * (ppmpll / 1000000.0);
PllFreq1GHZ=PLL_FREQ_1GHZ;
PllFreq1GHZ+=PllFreq1GHZ * (ppmpll / 1000000.0);
PllFreq19MHZ=PLLFREQ_192;
PllFreq19MHZ+=PllFreq19MHZ * (ppmpll / 1000000.0);
//End of Init Plls
if(Mode==MODE_IQ)
{
IQArray=malloc(DmaSampleBurstSize*2*sizeof(signed short)); // TODO A FREE AT THE END OF SOFTWARE
@ -1343,7 +1376,7 @@ for (;;)
static int Min=32767;
static int CompteSample=0;
CompteSample++;
NbRead=read(FileInHandle,IQArray,DmaSampleBurstSize*2*2/*SHORT I,SHORT Q*/);
NbRead=readWrapper(IQArray,DmaSampleBurstSize*2*2/*SHORT I,SHORT Q*/);
if(NbRead!=DmaSampleBurstSize*2*2)
@ -1351,13 +1384,12 @@ for (;;)
if(loop_mode_flag==1)
{
printf("Looping FileIn\n");
close(FileInHandle);
FileInHandle = open(FileName, O_RDONLY);
reset();
char dummyheader[HEADER_SIZE];
if (read(FileInHandle,dummyheader,HEADER_SIZE) != HEADER_SIZE) {
if (readWrapper(dummyheader,HEADER_SIZE) != HEADER_SIZE) {
fatal("Unable to read header\n");
}
NbRead=read(FileInHandle,IQArray,DmaSampleBurstSize*2*2);
NbRead=readWrapper(IQArray,DmaSampleBurstSize*2*2);
}
else
terminate(0);
@ -1425,15 +1457,14 @@ for (;;)
static int Min=32767;
static int CompteSample=0;
CompteSample++;
NbRead=read(FileInHandle,IQFloatArray,DmaSampleBurstSize*2*sizeof(float));
NbRead=readWrapper(IQFloatArray,DmaSampleBurstSize*2*sizeof(float));
if(NbRead!=DmaSampleBurstSize*2*sizeof(float))
{
if(loop_mode_flag==1)
{
printf("Looping FileIn\n");
close(FileInHandle);
FileInHandle = open(FileName, O_RDONLY);
reset();
}
else if (FileInHandle != STDIN_FILENO)
terminate(0);
@ -1500,15 +1531,14 @@ for (;;)
if(TimeRemaining==0)
{
NbRead=read(FileInHandle,&SampleRf,sizeof(samplerf_t));
NbRead=readWrapper(&SampleRf,sizeof(samplerf_t));
if(NbRead!=sizeof(samplerf_t))
{
if(loop_mode_flag==1)
{
//printf("Looping FileIn\n");
close(FileInHandle);
FileInHandle = open(FileName, O_RDONLY);
NbRead=read(FileInHandle,&SampleRf,sizeof(samplerf_t));
reset();
NbRead=readWrapper(&SampleRf,sizeof(samplerf_t));
}
else if (FileInHandle != STDIN_FILENO)
{

View File

@ -1,5 +1,31 @@
#ifndef RPITX_H
#define RPITX_H
#include <ctype.h>
int pitx_init(int SampleRate,double TuningFrequency);
int pitx_SetTuneFrequencyu(uint32_t Frequency);
#define MODE_IQ 0
#define MODE_RF 1
#define MODE_RFA 2
#define MODE_IQ_FLOAT 3
#define MODE_VFO 4
int pitx_run(
char Mode,
int SampleRate,
float SetFrequency,
float ppmpll,
char NoUsePwmFrequency,
// Wrapper around read to read wav file bytes
ssize_t (*readWrapper)(void *buffer, size_t count),
// Wrapper to reset file for looping
void (*reset)(void)
);
/** Wrapper around reading from memory with an interface similar to read. */
ssize_t readArray(void *buffer, const size_t count);
void setUpReadArray(void* baseAddress, size_t length);
#endif