diff --git a/main.c b/main.c index a85071f..472f1da 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,14 @@ +/* abandon all hope, this shit is a mess */ + #include #include #include +#include +#include #include +#include -/* define ANSI escape chars */ +/* define ANSI escape chars and math */ #define RESET "\033[0m" #define BOLD "\033[1m" #define UNDERLINE "\033[4m" @@ -26,8 +31,16 @@ #define BG_CYAN "\033[46m" #define BG_WHITE "\033[47m" -/* declare global constants */ +#define M_PI 3.14159265358979323846 +/* declare global variables */ + +rtlsdr_dev_t *dev = NULL; +uint32_t device_index = 0; +uint32_t MHz = 1000000; +uint32_t kHz = 1000; +uint32_t bandwidth; +uint32_t center_freq; /* declare reusable functions */ @@ -78,6 +91,23 @@ void util_check() { } } +void rtl_sdr_check() { + rtlsdr_open; + int index; + const char* device_name; + char serial[256], product[256], manufact[256]; + device_name = rtlsdr_get_device_name(device_index); + rtlsdr_get_device_usb_strings(index, manufact, product, serial); + printf("device name: " GREEN); + printf(device_name); + printf("\n" RESET); + printf(YELLOW"serial: %s\n", serial); + printf("product: %s\n", product); + printf("manufacturer: %s\n", manufact); + printf(RESET); + rtlsdr_close; +} + /* making sure program has everything it needs to run, more checks will be introduced as capabilities are added */ void startup_checks() { @@ -102,23 +132,113 @@ void startup_checks() { printf(MAGENTA"UTILITIES\n\n"RESET); util_check(); printf("----------------------------------------------------------------------\n"); + printf(MAGENTA"SOFTWARE-DEFINED RADIO\n\n"RESET); + rtl_sdr_check(); + printf("----------------------------------------------------------------------\n"); printf(MAGENTA"all checks finished!\n"RESET); printf("######################################################################\n"); + printf(GREEN"initializing RTL-SDR...\n"RESET); + rtlsdr_open(&dev, device_index); +} + +/* signal analysis function */ +void fourier_transform(unsigned char *buf, double *out, int n) { + for (int k = 0; k < n; k++) { + double real = 0.0; + double imag = 0.0; + for (int t = 0; t < n; t++) { + double angle = 2 * M_PI * t * k / n; + real += buf[t] * cos(angle); + imag -= buf[t] * sin(angle); + } + out[k] = sqrt(real * real + imag * imag); + } } /* define functions for dashboard - WIP */ -/* +void init_colors() { + start_color(); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_YELLOW, COLOR_BLACK); + init_pair(4, COLOR_BLUE, COLOR_BLACK); + init_pair(5, COLOR_MAGENTA, COLOR_BLACK); + init_pair(6, COLOR_CYAN, COLOR_BLACK); + init_pair(7, COLOR_WHITE, COLOR_BLACK); +} + +void draw_bar_graph(double *data, int size) { + clear(); + double max_value = 0.0; + attron(COLOR_PAIR(6)); + printw("center frequency: %.3f MHz gain: %d bandwidth: %.3f MHz", center_freq / (double)MHz, rtlsdr_get_tuner_gain(dev), bandwidth / (double)MHz); + attroff(COLOR_PAIR(6)); + double bottom = center_freq - bandwidth / 2.0; + double step = bandwidth / (double)size; + for (int i = 0; i < size; i++) { + if (data[i] > max_value) { + max_value = data[i]; + } + } + attron(COLOR_PAIR(4)); + for (int i = 0; i < size; i++) { + mvprintw(i + 1, 0, "%.3f MHz: ", bottom / (double)MHz + i * step / (double)MHz); + for (int j = 0; j < data[i] * 50 / max_value; j++) { + printw(">"); + } + } + attroff(COLOR_PAIR(4)); + refresh(); +} + + +/* define rtl-sdr specific functions and general SDR workflow - WIP */ + +void rtl_sdr_processing(unsigned char *buf, int buffer_length) { + int n = buffer_length; + double *out = (double *)malloc(n * sizeof(double)); + fourier_transform(buf, out, n); + /* debug + for (int i = 0; i < n; i++) { + printf("%f\n", out[i]); + } */ + draw_bar_graph(out, n); + free(out); +} + +void rtl_sdr_waterfall() { + bandwidth = 10*MHz; + rtlsdr_set_tuner_bandwidth(dev, bandwidth); + uint32_t buffer_length = 40, n_read = 10; // example buffer length + unsigned char *buffer = (unsigned char *)malloc(buffer_length); + rtlsdr_set_center_freq(dev, 104*MHz); //104MHz for testing purposes + center_freq = rtlsdr_get_center_freq(dev); + rtlsdr_read_sync(dev, buffer, buffer_length, &n_read); + rtl_sdr_processing(buffer, buffer_length); + usleep(500000); +} + +/* main dashboard function */ + void start_dashboard() { initscr(); cbreak(); noecho(); + init_colors(); + do + { + rtl_sdr_waterfall(); + } while (true); + rtlsdr_close(dev); + endwin(); } -*/ int main() { startup_screen(); startup_checks(); + start_dashboard(); + return 0; } \ No newline at end of file