Add support to sendook to read bit and duration from file

pull/265/head
Alberto Morato 2021-08-01 15:59:55 +02:00
rodzic 5fce7fc94a
commit aaefebc8ee
1 zmienionych plików z 137 dodań i 70 usunięć

Wyświetl plik

@ -10,6 +10,13 @@
bool running = true;
typedef struct
{
double active;
uint32_t duration; //nano seconds
uint32_t padding;
} Bitdata;
void print_usage(void)
{
/** Future options :
@ -30,6 +37,7 @@ Options:\n\
-r nb : repeat nb times the message (default : 3)\n\
-p nb : pause between each message (default : 1000us=1ms)\n\
-m nb : modulation type 0=OOK, 1=OOK_PWM, 2=OOK_PPM (default : 0=OOK)\n\
-i : filemode : read from file\n\
\n\
\"binary code\":\n\
a serie of 0 or 1 char (space allowed and ignored)\n\
@ -75,6 +83,10 @@ int main(int argc, char *argv[])
int dryrun = 0; // if 1 : hte message is not really transmitted
int modulation = 0; // 0=OOK, 1=OOK_PWM, 2=OOK_PPM
char *bits = NULL;
int filemode = 0;
char *filename = NULL;
uint8_t *data = NULL;
int size;
for (int i = 0; i < 64; i++)
{
@ -85,7 +97,7 @@ int main(int argc, char *argv[])
}
while(1)
{
a = getopt(argc, argv, "f:0:1:g:r:p:m:hvd");
a = getopt(argc, argv, "f:0:1:g:r:p:m:hvdi");
if(a == -1)
{
if(anyargs) break;
@ -125,6 +137,9 @@ int main(int argc, char *argv[])
case 'd': // Dry run
dryrun = 1;
break;
case 'i': // filemode
filemode = 1;
break;
case -1:
break;
default:
@ -136,12 +151,26 @@ int main(int argc, char *argv[])
if (optind >= argc) {
FATAL_ERROR(-2, "Missing bit message.\n");
}
bits = argv[optind];
if (filemode)
{
filename = argv[optind];
}
else
{
bits = argv[optind];
}
printf("Frequency set to : %" PRIu64 "Hz \n", Freq);
printf("Modulation: %d \n", modulation);
printf("Bit duration 0 : %" PRIu64 "us ; 1 : %" PRIu64 "us\n",
bit0duration, bit1duration);
printf("Bit gap = %" PRIu64 "us \n", bitgap);
if(!filemode)
{
printf("Modulation: %d \n", modulation);
printf("Bit duration 0 : %" PRIu64 "us ; 1 : %" PRIu64 "us\n",
bit0duration, bit1duration);
printf("Bit gap = %" PRIu64 "us \n", bitgap);
}
else
{
printf("Reading data from file %s.\n", filename);
}
printf("Send message %d times with a pause of %dus\n", nbrepeat, pause);
if (dryrun)
printf("Dry run mode enabled : no message will be sent\n");
@ -149,28 +178,54 @@ int main(int argc, char *argv[])
// Simplify the message to send
int computed_duration = 0; // in us
int nbbits = 0;
for(size_t i = 0; i < strlen(bits); i++)
if(!filemode)
{
char c = bits[i];
if (c == '0')
for(size_t i = 0; i < strlen(bits); i++)
{
nbbits ++;
computed_duration += bit0duration;
} else if (c == '1')
char c = bits[i];
if (c == '0')
{
nbbits ++;
computed_duration += bit0duration;
} else if (c == '1')
{
nbbits ++;
computed_duration += bit1duration;
}
/* OOK_PWM and OOK_PPM requires extra bit */
if((modulation == 1) || (modulation == 2))
{
computed_duration += bitgap;
nbbits ++;
}
// any other char is ignored (it allows to speparate nibble with a space for example)
// improvement : allow "." and "-" or "i" and "a" to create a MORSE sender
}
}
else
{
FILE *p_file = NULL;
p_file = fopen(filename,"rb");
if(p_file == NULL)
{
nbbits ++;
computed_duration += bit1duration;
FATAL_ERROR(-2, "Can't open file %s.\n", filename);
}
fseek(p_file,0,SEEK_END);
size = ftell(p_file);
data = (uint8_t *)malloc(size);
rewind(p_file);
fread(data,sizeof(uint8_t),size,p_file);
Bitdata *p_bitdata = (Bitdata *)data;
nbbits = size/sizeof(Bitdata);
for(int i = 0; i < nbbits; i++)
{
computed_duration += p_bitdata[i].duration/1000; //nano to us
}
/* OOK_PWM and OOK_PPM requires extra bit */
if((modulation == 1) || (modulation == 2))
{
computed_duration += bitgap;
nbbits ++;
}
// any other char is ignored (it allows to speparate nibble with a space for example)
// improvement : allow "." and "-" or "i" and "a" to create a MORSE sender
}
dbg_printf(1, "Send %d bits, with a total duration of %d us.\n", nbbits, computed_duration);
if (computed_duration == 0 || nbbits == 0)
{
@ -184,60 +239,72 @@ int main(int argc, char *argv[])
// Prepare the message
ookbursttiming ooksender(Freq, computed_duration);
ookbursttiming::SampleOOKTiming Message[nbbits];
for(size_t i = 0; i < strlen(bits); i++)
if(!filemode)
{
char c = bits[i];
switch (modulation)
for(size_t i = 0; i < strlen(bits); i++)
{
case 0: // OOK:
if (c == '0')
{
Message[i].value = 0;
Message[i].duration = bit0duration;
} else if (c == '1')
{
Message[i].value = 1;
Message[i].duration = bit1duration;
}
break;
case 1: // OOK_PWM:
if (c == '0')
{
Message[i*2].value = 1;
Message[i*2].duration = bit0duration;
} else if (c == '1')
{
Message[i*2].value = 1;
Message[i*2].duration = bit1duration;
}
Message[(i*2)+1].value = 0;
Message[(i*2)+1].duration = bitgap;
break;
case 2: // OOK_PPM:
Message[i*2].value = 1;
Message[i*2].duration = bitgap;
if (c == '0')
char c = bits[i];
switch (modulation)
{
case 0: // OOK:
if (c == '0')
{
Message[i].value = 0;
Message[i].duration = bit0duration;
} else if (c == '1')
{
Message[i].value = 1;
Message[i].duration = bit1duration;
}
break;
case 1: // OOK_PWM:
if (c == '0')
{
Message[i*2].value = 1;
Message[i*2].duration = bit0duration;
} else if (c == '1')
{
Message[i*2].value = 1;
Message[i*2].duration = bit1duration;
}
Message[(i*2)+1].value = 0;
Message[(i*2)+1].duration = bit0duration;
} else if (c == '1')
{
Message[(i*2)+1].value = 0;
Message[(i*2)+1].duration = bit1duration;
Message[(i*2)+1].duration = bitgap;
break;
case 2: // OOK_PPM:
Message[i*2].value = 1;
Message[i*2].duration = bitgap;
if (c == '0')
{
Message[(i*2)+1].value = 0;
Message[(i*2)+1].duration = bit0duration;
} else if (c == '1')
{
Message[(i*2)+1].value = 0;
Message[(i*2)+1].duration = bit1duration;
}
break;
default:
break;
}
break;
default:
break;
}
}
else
{
Bitdata *p_bitdata = (Bitdata *)data;
for(int i = 0; i < nbbits; i++)
{
Message[i].value = p_bitdata[i].active;
Message[i].duration = p_bitdata[i].duration/1000; //nano to us
}
}
// Send the message