databits: split out the databits implementations

rename framebits to databits
added databits_binary decoder
This commit is contained in:
Kamal Mostafa 2012-08-31 21:39:08 -07:00
parent a7d1602c9d
commit e2a51f093a
7 changed files with 216 additions and 70 deletions

View File

@ -42,8 +42,14 @@ FSK_SRC = fsk.h fsk.c
BAUDOT_SRC = baudot.h baudot.c
DATABITS_SRC = \
databits.h \
databits_ascii.c \
databits_binary.c \
databits_baudot.c $(BAUDOT_SRC)
minimodem_LDADD = $(DEPS_LIBS)
minimodem_SOURCES = minimodem.c $(BAUDOT_SRC) $(FSK_SRC) $(SIMPLEAUDIO_SRC)
minimodem_SOURCES = minimodem.c $(DATABITS_SRC) $(FSK_SRC) $(SIMPLEAUDIO_SRC)
minimodem.1.html: minimodem.1 Makefile

View File

@ -66,12 +66,14 @@ CONFIG_CLEAN_VPATH_FILES =
am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)"
PROGRAMS = $(bin_PROGRAMS)
am__objects_1 = baudot.$(OBJEXT)
am__objects_2 = fsk.$(OBJEXT)
am__objects_3 = simpleaudio.$(OBJEXT) simple-tone-generator.$(OBJEXT) \
am__objects_2 = databits_ascii.$(OBJEXT) databits_binary.$(OBJEXT) \
databits_baudot.$(OBJEXT) $(am__objects_1)
am__objects_3 = fsk.$(OBJEXT)
am__objects_4 = simpleaudio.$(OBJEXT) simple-tone-generator.$(OBJEXT) \
simpleaudio-pulse.$(OBJEXT) simpleaudio-alsa.$(OBJEXT) \
simpleaudio-benchmark.$(OBJEXT) simpleaudio-sndfile.$(OBJEXT)
am_minimodem_OBJECTS = minimodem.$(OBJEXT) $(am__objects_1) \
$(am__objects_2) $(am__objects_3)
am_minimodem_OBJECTS = minimodem.$(OBJEXT) $(am__objects_2) \
$(am__objects_3) $(am__objects_4)
minimodem_OBJECTS = $(am_minimodem_OBJECTS)
am__DEPENDENCIES_1 =
minimodem_DEPENDENCIES = $(am__DEPENDENCIES_1)
@ -222,8 +224,14 @@ SIMPLEAUDIO_SRC = \
FSK_SRC = fsk.h fsk.c
BAUDOT_SRC = baudot.h baudot.c
DATABITS_SRC = \
databits.h \
databits_ascii.c \
databits_binary.c \
databits_baudot.c $(BAUDOT_SRC)
minimodem_LDADD = $(DEPS_LIBS)
minimodem_SOURCES = minimodem.c $(BAUDOT_SRC) $(FSK_SRC) $(SIMPLEAUDIO_SRC)
minimodem_SOURCES = minimodem.c $(DATABITS_SRC) $(FSK_SRC) $(SIMPLEAUDIO_SRC)
all: all-am
.SUFFIXES:
@ -308,6 +316,9 @@ distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/baudot.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/databits_ascii.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/databits_baudot.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/databits_binary.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fsk.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/minimodem.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/simple-tone-generator.Po@am__quote@

53
src/databits.h Normal file
View File

@ -0,0 +1,53 @@
/*
* databits.h
*
* Copyright (C) 2012 Kamal Mostafa <kamal@whence.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
typedef int (databits_encoder)(
unsigned int *databits_outp, char char_out );
typedef unsigned int (databits_decoder)(
char *dataout_p, unsigned int dataout_size,
unsigned int bits, unsigned int n_databits );
int
databits_encode_ascii8( unsigned int *databits_outp, char char_out );
unsigned int
databits_decode_ascii8( char *dataout_p, unsigned int dataout_size,
unsigned int bits, unsigned int n_databits );
#include "baudot.h"
#define databits_encode_baudot baudot_encode // from baudot.h
//int
//databits_encode_baudot( unsigned int *databits_outp, char char_out );
unsigned int
databits_decode_baudot( char *dataout_p, unsigned int dataout_size,
unsigned int bits, unsigned int n_databits );
int
databits_encode_binary( unsigned int *databits_outp, char char_out );
unsigned int
databits_decode_binary( char *dataout_p, unsigned int dataout_size,
unsigned int bits, unsigned int n_databits );

45
src/databits_ascii.c Normal file
View File

@ -0,0 +1,45 @@
/*
* databits_ascii.c
*
* Copyright (C) 2012 Kamal Mostafa <kamal@whence.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "databits.h"
/*
* ASCII 8-bit data databits decoder/encoder (passthrough)
*/
/* returns the number of datawords stuffed into *databits_outp */
int
databits_encode_ascii8( unsigned int *databits_outp, char char_out )
{
*databits_outp = char_out;
return 1;
}
/* returns nbytes decoded */
unsigned int
databits_decode_ascii8( char *dataout_p, unsigned int dataout_size,
unsigned int bits, unsigned int n_databits )
{
if ( ! dataout_p ) // databits processor reset: noop
return 0;
bits &= 0xFF;
*dataout_p = bits;
return 1;
}

40
src/databits_baudot.c Normal file
View File

@ -0,0 +1,40 @@
/*
* databits_baudot.c
*
* Copyright (C) 2012 Kamal Mostafa <kamal@whence.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "databits.h"
/*
* Baudot 5-bit data databits decoder/encoder
*/
#include "baudot.h"
/* returns nbytes decoded */
unsigned int
databits_decode_baudot( char *dataout_p, unsigned int dataout_size,
unsigned int bits, unsigned int n_databits )
{
if ( ! dataout_p ) { // databits processor reset: reset Baudot state
baudot_reset();
return 0;
}
bits &= 0x1F;
return baudot_decode(dataout_p, bits);
}

42
src/databits_binary.c Normal file
View File

@ -0,0 +1,42 @@
/*
* databits_binary.c
*
* Copyright (C) 2012 Kamal Mostafa <kamal@whence.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "databits.h"
/*
* Rawbits N-bit binary data decoder/encoder
*/
#include <assert.h>
// returns nbytes decoded
unsigned int
databits_decode_binary( char *dataout_p, unsigned int dataout_size,
unsigned int bits, unsigned int n_databits )
{
if ( ! dataout_p ) // databits processor reset: noop
return 0;
assert( dataout_size >= n_databits + 1 );
int j;
for ( j=0; j<n_databits; j++ )
dataout_p[j] = (bits>>j & 1) + '0';
dataout_p[j] = '\n';
return n_databits + 1;
}

View File

@ -39,59 +39,10 @@
#include "simpleaudio.h"
#include "fsk.h"
#include "baudot.h"
#include "databits.h"
char *program_name = "";
/*
* ASCII 8-bit data framebits decoder/encoder (passthrough)
*/
/* returns the number of datawords stuffed into *databits_outp */
int
framebits_encode_ascii8( unsigned int *databits_outp, char char_out )
{
*databits_outp = char_out;
return 1;
}
/* returns nbytes decoded */
static unsigned int
framebits_decode_ascii8( char *dataout_p, unsigned int dataout_size,
unsigned int bits )
{
if ( dataout_p == NULL ) // frame processor reset: noop
return 0;
assert( (bits & ~0xFF) == 0 );
assert( dataout_size >= 1);
*dataout_p = bits;
return 1;
}
/*
* Baudot 5-bit data framebits decoder/encoder
*/
#define framebits_encode_baudot baudot_encode
/* returns nbytes decoded */
static unsigned int
framebits_decode_baudot( char *dataout_p, unsigned int dataout_size,
unsigned int bits )
{
if ( dataout_p == NULL ) { // frame processor reset: reset Baudot state
baudot_reset();
return 0;
}
assert( (bits & ~0x1F) == 0 );
assert( dataout_size >= 1);
return baudot_decode(dataout_p, bits);
}
int tx_transmitting = 0;
int tx_leader_bits_len = 2;
int tx_trailer_bits_len = 2;
@ -128,7 +79,7 @@ static void fsk_transmit_stdin(
float bfsk_space_f,
int n_data_bits,
float bfsk_nstopbits,
int (*framebits_encoder)( unsigned int *databits_outp, char char_out )
databits_encoder encode
)
{
size_t sample_rate = simpleaudio_get_rate(sa_out);
@ -157,7 +108,7 @@ static void fsk_transmit_stdin(
// fprintf(stderr, "<c=%d>", c);
unsigned int nwords;
unsigned int bits[2];
nwords = framebits_encoder(bits, c);
nwords = encode(bits, c);
if ( !tx_transmitting )
{
@ -586,10 +537,8 @@ main( int argc, char*argv[] )
float bfsk_data_rate = 0.0;
int (*bfsk_framebits_encode)( unsigned int *databits_outp, char char_out );
unsigned int (*bfsk_framebits_decode)( char *dataout_p, unsigned int dataout_size,
unsigned int bits );
databits_encoder *bfsk_databits_encode;
databits_decoder *bfsk_databits_decode;
if ( strncasecmp(modem_mode, "rtty",5)==0 ) {
bfsk_data_rate = 45.45;
@ -606,11 +555,11 @@ main( int argc, char*argv[] )
usage();
if ( bfsk_n_data_bits == 8 ) {
bfsk_framebits_decode = framebits_decode_ascii8;
bfsk_framebits_encode = framebits_encode_ascii8;
bfsk_databits_decode = databits_decode_ascii8;
bfsk_databits_encode = databits_encode_ascii8;
} else if ( bfsk_n_data_bits == 5 ) {
bfsk_framebits_decode = framebits_decode_baudot;
bfsk_framebits_encode = framebits_encode_baudot;
bfsk_databits_decode = databits_decode_baudot;
bfsk_databits_encode = databits_encode_baudot;
} else {
assert( 0 && bfsk_n_data_bits );
}
@ -695,7 +644,7 @@ main( int argc, char*argv[] )
bfsk_mark_f, bfsk_space_f,
bfsk_n_data_bits,
bfsk_nstopbits,
bfsk_framebits_encode
bfsk_databits_encode
);
simpleaudio_close(sa_out);
@ -1015,7 +964,7 @@ main( int argc, char*argv[] )
fskp->b_mark * fskp->band_width);
}
carrier = 1;
bfsk_framebits_decode(0, 0, 0); /* reset the frame processor */
bfsk_databits_decode(0, 0, 0, 0); // reset the frame processor
}
confidence_total += confidence;
@ -1054,9 +1003,9 @@ main( int argc, char*argv[] )
char dataoutbuf[4096];
unsigned int dataout_nbytes = 0;
dataout_nbytes += bfsk_framebits_decode(dataoutbuf + dataout_nbytes,
dataout_nbytes += bfsk_databits_decode(dataoutbuf + dataout_nbytes,
dataout_size - dataout_nbytes,
bits);
bits, (int)bfsk_n_data_bits);
/*
* Print the output buffer to stdout