Added UART sound device
Added UART sound device

file:a/makefile -> file:b/makefile
--- a/makefile
+++ b/makefile
@@ -45,127 +45,140 @@
 ###############################################################################
 
 bin/mixer: build/obj/mixer.o
-	$(CC) $(CFLAGS) -o bin/mixer build/obj/mixer.o
+	$(CC) $(CFLAGS) -o $@ build/obj/mixer.o
 
 build/obj/mixer.o: src/mixer/mixer.c
-	$(CC) $(CFLAGS) -o build/obj/mixer.o -c src/mixer/mixer.c
+	$(CC) $(CFLAGS) -o $@ -c src/mixer/mixer.c
+
+###############################################################################
+# Record audio data from a Serial Device                                      #
+###############################################################################
+
+bin/UARTRecord: build/obj/UARTRecord.o build/obj/UART.o
+	$(CC) $(CFLAGS) -o $@ build/obj/UARTRecord.o build/obj/UART.o
+
+build/obj/UARTRecord.o: src/sound_device/UARTRecord.c
+	$(CC) $(CFLAGS) -o $@ -c src/sound_device/UARTRecord.c
+
+build/obj/UART.o: src/io/uart/UART.c
+	$(CC) $(CFLAGS) -o $@ -c src/io/uart/UART.c
 
 ###############################################################################
 # PSK31 Demodulator                                                           #
 ###############################################################################
 
 bin/psk31: build/obj/main.o build/obj/psk31.o build/obj/Util.o build/obj/psk31_tdtl.o build/obj/psk31_wcpd.o build/obj/psk31_wpdd.o
-	g++ -O3 -o bin/psk31 build/obj/main.o build/obj/psk31.o build/obj/Util.o build/obj/psk31_tdtl.o build/obj/psk31_wcpd.o build/obj/psk31_wpdd.o
+	g++ -O3 -o $@ build/obj/main.o build/obj/psk31.o build/obj/Util.o build/obj/psk31_tdtl.o build/obj/psk31_wcpd.o build/obj/psk31_wpdd.o
 
 build/obj/main.o: src/psk31/main.cpp
-	g++ -O3 -o build/obj/main.o -c src/psk31/main.cpp
+	g++ -O3 -o $@ -c src/psk31/main.cpp
 
 build/obj/psk31.o: src/psk31/psk31.cpp
-	g++ -O3 -o build/obj/psk31.o -c src/psk31/psk31.cpp
+	g++ -O3 -o $@ -c src/psk31/psk31.cpp
 
 build/obj/psk31_tdtl.o: src/psk31/psk31_tdtl.cpp
-	g++ -O3 -o build/obj/psk31_tdtl.o -c src/psk31/psk31_tdtl.cpp
+	g++ -O3 -o $@ -c src/psk31/psk31_tdtl.cpp
 
 build/obj/psk31_wcpd.o: src/psk31/psk31_wcpd.cpp
-	g++ -O3 -o build/obj/psk31_wcpd.o -c src/psk31/psk31_wcpd.cpp
+	g++ -O3 -o $@ -c src/psk31/psk31_wcpd.cpp
 
 build/obj/psk31_wpdd.o: src/psk31/psk31_wpdd.cpp
-	g++ -O3 -o build/obj/psk31_wpdd.o -c src/psk31/psk31_wpdd.cpp
+	g++ -O3 -o $@ -c src/psk31/psk31_wpdd.cpp
 
 ###############################################################################
 # Finite Impulse Response filter                                              #
 ###############################################################################
 
 bin/filter_test: build/obj/filter_test.o build/obj/filter.o build/obj/FIR_filter.o build/obj/Util.o
-	g++ -O3 -o bin/filter_test build/obj/filter_test.o build/obj/filter.o build/obj/FIR_filter.o build/obj/Util.o
+	g++ -O3 -o $@ build/obj/filter_test.o build/obj/filter.o build/obj/FIR_filter.o build/obj/Util.o
 
 build/obj/filter_test.o: src/filter/filter_test.cpp
-	g++ -O3 -o build/obj/filter_test.o -c src/filter/filter_test.cpp
+	g++ -O3 -o $@ -c src/filter/filter_test.cpp
 
 build/obj/filter.o: src/filter/filter.cpp
-	g++ -O3 -o build/obj/filter.o -c src/filter/filter.cpp
+	g++ -O3 -o $@ -c src/filter/filter.cpp
 
 build/obj/FIR_filter.o: src/filter/FIR_filter.cpp
-	g++ -O3 -o build/obj/FIR_filter.o -c src/filter/FIR_filter.cpp
+	g++ -O3 -o $@ -c src/filter/FIR_filter.cpp
 
 ###############################################################################
 # Gets Microphone and pipes to stdout or file                                 #
 ###############################################################################
 
 bin/sound_device: build/obj/sound_device.o build/obj/Util.o
-	g++ -O3 -o bin/sound_device build/obj/sound_device.o build/obj/Util.o -lpulse -lpulse-simple
+	g++ -O3 -o $@ build/obj/sound_device.o build/obj/Util.o -lpulse -lpulse-simple
 
 build/obj/sound_device.o: src/sound_device/sound_device.cpp
-	g++ -O3 -o build/obj/sound_device.o -c src/sound_device/sound_device.cpp
+	g++ -O3 -o $@ -c src/sound_device/sound_device.cpp
 
 ###############################################################################
 # NOAA ATP decoder                                                            #
 ###############################################################################
 
 bin/NOAA_ATP: build/obj/NOAA_ATP_main.o build/obj/NOAA_ATP.o build/obj/Util.o
-	g++ -O3 -o bin/NOAA_ATP build/obj/NOAA_ATP_main.o build/obj/NOAA_ATP.o build/obj/Util.o
+	g++ -O3 -o $@ build/obj/NOAA_ATP_main.o build/obj/NOAA_ATP.o build/obj/Util.o
 
 build/obj/NOAA_ATP_main.o: src/NOAA_ATP/NOAA_ATP_main.cpp
-	g++ -O3 -o build/obj/NOAA_ATP_main.o -c src/NOAA_ATP/NOAA_ATP_main.cpp
+	g++ -O3 -o $@ -c src/NOAA_ATP/NOAA_ATP_main.cpp
 
 build/obj/NOAA_ATP.o: src/NOAA_ATP/NOAA_ATP.cpp
-	g++ -O3 -o  build/obj/NOAA_ATP.o -c src/NOAA_ATP/NOAA_ATP.cpp
+	g++ -O3 -o $@ -c src/NOAA_ATP/NOAA_ATP.cpp
 
 ###############################################################################
 # Network fowarder for data                                                   #
 ###############################################################################
 
 bin/forward: build/obj/forward.o
-	$(CC) -O3 -o bin/forward build/obj/forward.o
+	$(CC) -O3 -o $@ build/obj/forward.o
 
 build/obj/forward.o: src/net/forward.c
-	$(CC) -O3 -o build/obj/forward.o -c src/net/forward.c
+	$(CC) -O3 -o $@ -c src/net/forward.c
 
 ###############################################################################
 # AFSK Demodulator / APRSPacket Decoder                                       #
 ###############################################################################
 
 bin/packet: build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o
-	$(CC) $(CFLAGS) -lm $(DEFINES) -o bin/packet build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o
+	$(CC) $(CFLAGS) -lm $(DEFINES) -o $@ build/obj/char_array_expandable.o build/obj/float_ring_buffer.o build/obj/char_ring_buffer.o build/obj/APRSPacket.o build/obj/AFSK_Demodulator.o build/obj/packet_main.o build/obj/crcccitt.o
 
 build/obj/packet_main.o: src/packet/packet_main.c
-	$(CC) $(CFLAGS) $(DEFINES) -o build/obj/packet_main.o -c src/packet/packet_main.c
+	$(CC) $(CFLAGS) $(DEFINES) -o $@ -c src/packet/packet_main.c
 
 build/obj/AFSK_Demodulator.o: src/packet/AFSK_Demodulator.c
-	$(CC) $(CFLAGS) $(DEFINES) -o build/obj/AFSK_Demodulator.o -c src/packet/AFSK_Demodulator.c
+	$(CC) $(CFLAGS) $(DEFINES) -o $@ -c src/packet/AFSK_Demodulator.c
 
 build/obj/APRSPacket.o: src/packet/APRSPacket.c
-	$(CC) $(CFLAGS) $(DEFINES) -o build/obj/APRSPacket.o -c src/packet/APRSPacket.c
+	$(CC) $(CFLAGS) $(DEFINES) -o $@ -c src/packet/APRSPacket.c
 
 build/obj/char_ring_buffer.o: src/packet/char_ring_buffer.c
-	$(CC) $(CFLAGS) $(DEFINES) -o build/obj/char_ring_buffer.o -c src/packet/char_ring_buffer.c
+	$(CC) $(CFLAGS) $(DEFINES) -o $@ -c src/packet/char_ring_buffer.c
 
 build/obj/float_ring_buffer.o: src/packet/float_ring_buffer.c
-	$(CC) $(CFLAGS) $(DEFINES) -o build/obj/float_ring_buffer.o -c src/packet/float_ring_buffer.c
+	$(CC) $(CFLAGS) $(DEFINES) -o $@ -c src/packet/float_ring_buffer.c
 
 build/obj/char_array_expandable.o: src/packet/char_array_expandable.c
-	$(CC) $(CFLAGS) $(DEFINES) -o build/obj/char_array_expandable.o -c src/packet/char_array_expandable.c
+	$(CC) $(CFLAGS) $(DEFINES) -o $@ -c src/packet/char_array_expandable.c
 
 ###############################################################################
 # CRC                                                                         #
 ###############################################################################
 
 build/obj/crcccitt.o: src/crc/crcccitt.c
-	$(CC) $(CFLAGS) -o build/obj/crcccitt.o -c src/crc/crcccitt.c
+	$(CC) $(CFLAGS) -o $@ -c src/crc/crcccitt.c
 
 ###############################################################################
 # Math                                                                        #
 ###############################################################################
 
 build/obj/trig_table.o: src/math/trig_table.c
-	$(CC) $(CFLAGS) -o build/obj/trig_table.o -c src/math/trig_table.c
+	$(CC) $(CFLAGS) -o $@ -c src/math/trig_table.c
 
 ###############################################################################
 # Utility functions                                                           #
 ###############################################################################
 
 build/obj/Util.o: src/util/Util.cpp
-	g++ -O3 -o build/obj/Util.o -c src/util/Util.cpp
+	g++ -O3 -o $@ -c src/util/Util.cpp
 
 ###############################################################################
 # Cleans built objects                                                        #

--- /dev/null
+++ b/src/io/uart/UART.c
@@ -1,1 +1,170 @@
+#include "UART.h"
 
+int UART_open(UART *self, char *device_name, unsigned int baud_rate){
+
+	self->fd = open(device_name, O_RDWR | O_NOCTTY);
+	if(self->fd < 0){
+		perror("Unable to open UART");
+		return -1;
+	}
+
+	tcgetattr(self->fd, &(self->tio));
+
+	self->tio.c_cflag = CS8 | CLOCAL | CREAD;
+	self->tio.c_iflag = IGNPAR;
+	self->tio.c_oflag = 0;
+	self->tio.c_lflag = 0;
+
+	/* Set baud rate
+	 */
+	speed_t standard_baud_rate = B0;
+	switch(baud_rate){
+		case 50:
+			standard_baud_rate = B50;
+			break;
+		case 75:
+			standard_baud_rate = B75;
+			break;
+		case 110:
+			standard_baud_rate = B110;
+			break;
+		case 134:
+			standard_baud_rate = B134;
+			break;
+		case 150:
+			standard_baud_rate = B150;
+			break;
+		case 200:
+			standard_baud_rate = B200;
+			break;
+		case 300:
+			standard_baud_rate = B300;
+			break;
+		case 600:
+			standard_baud_rate = B600;
+			break;
+		case 1200:
+			standard_baud_rate = B1200;
+			break;
+		case 1800:
+			standard_baud_rate = B1800;
+			break;
+		case 2400:
+			standard_baud_rate = B2400;
+			break;
+		case 4800:
+			standard_baud_rate = B4800;
+			break;
+		case 9600:
+			standard_baud_rate = B9600;
+			break;
+		case 19200:
+			standard_baud_rate = B19200;
+			break;
+		case 38400:
+			standard_baud_rate = B38400;
+			break;
+		case 57600:
+			standard_baud_rate = B57600;
+			break;
+		case 115200:
+			standard_baud_rate = B115200;
+			break;
+		case 230400:
+			standard_baud_rate = B230400;
+			break;
+	}
+
+	if(standard_baud_rate == B38400){
+
+		// Ensure 38400 is actually 38400
+		struct serial_struct ss;
+		ioctl(self->fd, TIOCGSERIAL, &ss);
+		ss.flags &= ~ASYNC_SPD_MASK;
+		ioctl(self->fd, TIOCSSERIAL, &ss);
+
+	} else if(standard_baud_rate == B0){
+
+		standard_baud_rate = B38400;
+
+		struct serial_struct ss;
+
+		ioctl(self->fd, TIOCGSERIAL, &ss);
+
+		ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
+
+		ss.custom_divisor = ((float)(ss.baud_base + (baud_rate / 2)) / baud_rate) + 0.5;
+
+		unsigned int actual_rate = ss.baud_base / ss.custom_divisor;
+
+		fprintf(stderr, "Actual baud rate set to %d\n", actual_rate);
+		fprintf(stderr, "Error of %f%%\n", fabs((float)(baud_rate - actual_rate) / ((float)(baud_rate + actual_rate) / 2.0F)) * 100.0F);
+
+		ioctl(self->fd, TIOCSSERIAL, &ss);
+
+	}
+
+	cfsetispeed(&(self->tio), standard_baud_rate);
+	cfsetispeed(&(self->tio), standard_baud_rate);
+
+	/* Wait until there is at least 1 byte and
+	 * have an indefinite timeout
+	 */
+	self->tio.c_cc[VMIN] = 1;
+	self->tio.c_cc[VTIME] = 0;
+
+	tcflush(self->fd, TCIFLUSH);
+	tcsetattr(self->fd, TCSANOW, &(self->tio));
+
+	return 0;
+
+}
+
+bool UART_is_open(UART *self){
+
+	if(self->fd < 0)
+		return false;
+
+	return true;
+
+}
+
+int UART_close(UART *self){
+
+	// Ensure any custom baud rate is reset to the normal speed
+	struct serial_struct ss;
+	ioctl(self->fd, TIOCGSERIAL, &ss);
+	ss.flags &= ~ASYNC_SPD_MASK;
+	ioctl(self->fd, TIOCSSERIAL, &ss);
+
+	int status = close(self->fd);
+	if(status == -1){
+		perror("Unable to close socket");
+		return -1;
+	}
+
+	self->fd = -1;
+
+	return 0;
+
+}
+
+int UART_read(UART *self, char *buffer, int length){
+
+	int bytes_read = read(self->fd, buffer, length);
+	return bytes_read;
+
+}
+
+int UART_write(UART *self, char *buffer, int length){
+
+	int bytes_written = write(self->fd, buffer, length);
+	if(bytes_written != length){
+		perror("Did not write length bytes");
+		return -1;
+	}
+
+	return 0;
+
+}
+

--- /dev/null
+++ b/src/io/uart/UART.h
@@ -1,1 +1,48 @@
+/**
+ * @file	UART.h
+ * @author	Michael Marques <dryerzinia@gmail.com>
+ * @brief	Object for controlling serial ports
+ * @defgroup UART UART Control
+ *
+ * Contains struct and functions to easily manipulate serial ports
+ */
 
+#ifndef UART_H
+#define UART_H
+
+#include <errno.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <stdbool.h>
+
+#include <unistd.h>
+
+// fabs
+#include <math.h>
+
+#include <linux/serial.h>
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <fcntl.h>
+#include <termios.h>
+
+typedef struct {
+
+	int fd;
+	struct termios tio;
+
+} UART;
+
+int UART_open(UART *self, char *device_name, unsigned int baud_rate);
+bool UART_is_open(UART *self);
+int UART_close(UART *self);
+int UART_read(UART *self, char *buffer, int length);
+int UART_write(UART *self, char *buffer, int length);
+
+#endif
+

--- a/src/mixer/mixer.c
+++ b/src/mixer/mixer.c
@@ -4,12 +4,12 @@
 #include <stdio.h>

 #include <argp.h>

 

-const signed char *program_version = "Mixer 1.0";

-const signed char *program_bug_address = "<dryerzinia@gmail.com>";

+const char *program_version = "Mixer 1.0";

+const char *program_bug_address = "<dryerzinia@gmail.com>";

 

-static signed char doc[] = "Mixer -- A program for mixing 2 raw audio files together";

+static char doc[] = "Mixer -- A program for mixing 2 raw audio files together";

 

-static signed char args_doc[] = "";

+static char args_doc[] = "";

 

 static struct argp_option options[] = {

 		{"input-file-1", 'i', "FILE", 0, "First raw audio file to mix"},

@@ -20,10 +20,10 @@
 

 struct arguments {

 

-	signed char *in_1_filename;

-	signed char *in_2_filename;

+	char *in_1_filename;

+	char *in_2_filename;

 

-	signed char *out_filename;

+	char *out_filename;

 

 };

 


--- a/src/packet/APRSPacket.h
+++ b/src/packet/APRSPacket.h
@@ -43,21 +43,21 @@
 	 * Destination address of packet with SSID
 	 * May contain Mic-E data
 	 */
-	char		destination_address[8];
+	signed char		destination_address[8];
 	/**
 	 * Source of the packet with SSID
 	 */
-	char		source_address[8];
+	signed char		source_address[8];
 
 	/**
 	 * Number of repeaters
 	 * A maximum of 8 repeaters is allowed
 	 */
-	uint8_t		repeaters;
+	uint8_t			repeaters;
 	/**
 	 * List of repeaters
 	 */
-	char		**repeater_addresses;
+	signed char		**repeater_addresses;
 
 } APRSPacket;
 

--- /dev/null
+++ b/src/sound_device/UARTRecord.c
@@ -1,1 +1,107 @@
+#include "UARTRecord.h"
 
+UART uart;
+
+void int_handler(){
+
+	fprintf(stderr, "Interrupt received\n");
+
+	if(UART_is_open(&uart))
+		UART_close(&uart);
+
+	exit(0);
+
+}
+
+static char doc[] = "UARTRecord -- Record audio data from a UART interface";
+
+static char args_doc[] = "";
+
+static struct argp_option options[] = {
+		{"serial-port", 's', "FILE", OPTION_ARG_OPTIONAL, "Serial Port to read from"},
+		{"baud-rate", 'b', "INT", OPTION_ARG_OPTIONAL, "Baud rate to set serial port to"},
+		{0}
+};
+
+/**
+ * Array for arguments to packet executable
+ */
+struct arguments {
+
+	char *device_name;
+
+	unsigned int baud_rate;
+
+};
+
+static error_t parse_opt(int key, char *arg, struct argp_state *state){
+
+	// remove cast in full C
+	struct arguments *arguments = ((struct arguments*)(state->input));
+
+	switch(key){
+		case 's':
+			arguments->device_name = arg;
+			break;
+		case 'b':
+			arguments->baud_rate = atoi(arg);
+			break;
+		default:
+			return ARGP_ERR_UNKNOWN;
+	}
+	return 0;
+}
+
+static struct argp argp = {options, parse_opt, args_doc, doc};
+
+int main(int argc, char **argv){
+
+	signal(SIGINT, int_handler);
+
+	struct arguments arguments;
+
+	arguments.device_name = NULL;
+	arguments.baud_rate = 230400;
+
+	argp_parse(&argp, argc, argv, 0, 0, &arguments);
+
+	if(arguments.device_name == NULL){
+		fprintf(stderr, "Please specify a serial port to read from\n");
+		return -1;
+	}
+
+	int status = UART_open(&uart, arguments.device_name, arguments.baud_rate);
+
+	if(status != 0){
+		fprintf(stderr, "Could not open serial port, terminating\n");
+		return -1;
+	}
+
+	char buffer[BUFFER_SIZE];
+
+	while(true){
+
+		int bytes_read = UART_read(&uart, buffer, BUFFER_SIZE);
+		if(bytes_read <= 0){
+			fprintf(stderr, "Error reading! %d\n", bytes_read);
+			break;
+		}
+
+		int bytes_written = fwrite(buffer, sizeof(char), bytes_read, stdout);
+		if(bytes_written != bytes_read){
+			fprintf(stderr, "Error writing!\n");
+			break;
+		}
+
+		fflush(stdout);
+
+	}
+
+	fprintf(stderr, "Loop broken, closing...\n");
+
+	UART_close(&uart);
+
+	return 0;
+
+}
+

--- /dev/null
+++ b/src/sound_device/UARTRecord.h
@@ -1,1 +1,25 @@
+/**
+ * @file	UARTRecord.h
+ * @author	Michael Marques <dryerzinia@gmail.com>
+ * @brief	Pipes audio from UART to stdout
+ * @ingroup sound_device
+ *
+ * Program to take audio data from a serial port and pipe it to stdout
+ */
 
+#ifndef UARTRECORD_H
+#define UARTRECORD_H
+
+#include "../io/uart/UART.h"
+
+#include <stdio.h>
+#include <stdbool.h>
+
+#include <signal.h>
+
+#include <argp.h>
+
+#define BUFFER_SIZE 25
+
+#endif
+

--- a/src/sound_device/sound_device.cpp
+++ b/src/sound_device/sound_device.cpp
@@ -1,4 +1,7 @@
-// sound_device.cpp

+/**

+ * @file sound_device.cpp

+ * @defgroup sound_device Audio Device Controllers

+ */

 

 #include <iostream>

 #include <string>