// // Written for Computer Networks and Systems lab classes // AUTHOR : Sergiusz Warga #include #include #include #include #include // #include #include #include #include "CircBuffer.h" #include "funs.h" #define PI 3.14 #define SIG_FREQ 100 void handle_arguments(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s SAMPLING_FREQUENCY\n", argv[0]); exit(EXIT_FAILURE); } if (strtol(argv[1], NULL, 10) < 400) { fprintf(stderr, "Sampling frequency should be at least 400Hz!\n"); exit(EXIT_FAILURE); } } double get_sample(int signal_frequeuncy, int sampling_frequency, unsigned int sample_no) { // TODO: Just a sketch, gotta rewrite double step = 2.*PI/((double)sampling_frequency/signal_frequeuncy); // Step in radians return sin(sample_no*step); } _Noreturn void producer(int fd, int sampling_frequency) { // That's something new I've learned and I think that // function specifiers are cute. printf("The producer has been called\n"); //----- Initialize the buffer ----- struct CircBuffer *buffer; buffer = mmap(NULL, sizeof(struct CircBuffer), PROT_WRITE, MAP_SHARED, fd, 0); if (buffer == MAP_FAILED) { fprintf(stderr, "mmap: "); exit(EXIT_FAILURE); } unsigned int i = 0; int free_idx; struct timespec delay, tic, toc; delay.tv_sec = 0; delay.tv_nsec = (int) (1./sampling_frequency * NSEC_PER_SEC); // 1/f in nanoseconds clock_gettime(CLOCK_REALTIME, &tic); clock_gettime(CLOCK_REALTIME, &toc); printf("Starting to sampling the signal at %dHz with t_s = %ldns\n", sampling_frequency, delay.tv_nsec); //----- Fill the buffer ----- while (1) { clock_gettime(CLOCK_REALTIME, &toc); free_idx = buffer->info.free_idx; buffer->data[free_idx] = get_sample(SIG_FREQ, sampling_frequency, i); // printf("CircBuffer[%d] = \t %lf\n", free_idx, buffer->data[free_idx]); buffer->info.free_idx = (free_idx + 1 ) % buffer->info.size; ++i; while (timespec_diff_nano(&toc, &tic) < delay.tv_nsec) { // printf("%lld\n", timespec_diff_nano(&toc, &tic)); clock_gettime(CLOCK_REALTIME, &toc); } clock_gettime(CLOCK_REALTIME, &tic); } } int main(int argc, char *argv[]) { //----- Arguments handling ----- handle_arguments(argc, argv); int sampling_frequency = strtol(argv[1], NULL, 10) ; //----- Create a shared memory ----- const char *memory_name = "/dsp"; int fd = shm_open(memory_name, O_CREAT | O_RDWR, 0777); if (fd < 0) { fprintf(stderr, "shm_open: "); exit(EXIT_FAILURE); } // printf("Shared memory file descriptor: %d\n", fd); //----- Truncate memory to the buffer's size if (ftruncate(fd, sizeof(struct CircBuffer)) < 0) { fprintf(stderr, "ftruncate: \n"); exit(EXIT_FAILURE); } //----- Initialize the buffer ----- struct CircBuffer *buffer; buffer = (struct CircBuffer *)mmap(NULL, sizeof(struct CircBuffer), PROT_WRITE, MAP_SHARED, fd, 0); if (buffer == MAP_FAILED) { fprintf(stderr, "mmap: "); exit(EXIT_FAILURE); } buffer->info.size = sizeof(buffer->data)/sizeof(double); buffer->info.free_idx = 0; //----- Sample the producer ----- producer(fd, sampling_frequency); return 0; }