static void sample(void *arg, long period) { int n; ctrl_shm->watchdog = 0; if (ctrl_shm->state == RESET) { /* sampling interrupted, reset everything */ ctrl_shm->curr = 0; ctrl_shm->start = ctrl_shm->curr; ctrl_shm->samples = 0; ctrl_shm->force_trig = 0; /* reset completed, set new state */ ctrl_shm->state = IDLE; } ctrl_rt->mult_cntr++; if (ctrl_rt->mult_cntr < ctrl_shm->mult) { /* not time to do anything yet */ return; } /* reset counter */ ctrl_rt->mult_cntr = 0; /* run the sampling state machine */ switch (ctrl_shm->state) { case IDLE: /* do nothing while waiting for INIT */ break; case INIT: /* init start pointer, curr pointer, sample count */ ctrl_shm->curr = 0; ctrl_shm->start = ctrl_shm->curr; ctrl_shm->samples = 0; ctrl_shm->force_trig = 0; ctrl_rt->auto_timer = 0; /* get info about channels */ for (n = 0; n < 16; n++) { ctrl_rt->data_addr[n] = SHMPTR(ctrl_shm->data_offset[n]); ctrl_rt->data_type[n] = ctrl_shm->data_type[n]; ctrl_rt->data_len[n] = ctrl_shm->data_len[n]; } /* set next state */ ctrl_shm->state = PRE_TRIG; break; case PRE_TRIG: /* acquire a sample */ capture_sample(); /* increment sample counter */ ctrl_shm->samples++; /* check if all pre-trigger samples captured */ if (ctrl_shm->samples >= ctrl_shm->pre_trig) { /* yes - start waiting for trigger */ ctrl_shm->state = TRIG_WAIT; /* dummy call to preset 'compare_result' */ check_trigger(); } break; case TRIG_WAIT: /* acquire a sample */ capture_sample(); /* increment sample counter */ ctrl_shm->samples++; /* check if trigger condition met */ if (check_trigger()) { /* yes - start acquiring post trigger data */ ctrl_shm->state = POST_TRIG; } else { /* no, discard oldest pre-trig sample */ ctrl_shm->samples--; ctrl_shm->start += ctrl_shm->sample_len; /* is there a valid sample here, or end of buffer? */ if ((ctrl_shm->start + ctrl_shm->sample_len) > ctrl_shm->buf_len) { /* end of buffer, wrap back to beginning */ ctrl_shm->start = 0; } } break; case POST_TRIG: /* acquire a sample */ capture_sample(); /* increment sample counter */ ctrl_shm->samples++; /* check if all post-trigger samples captured */ if (ctrl_shm->samples >= ctrl_shm->rec_len) { /* yes - stop sampling and cleanup */ ctrl_shm->state = DONE; } break; case DONE: /* do nothing while GUI displays waveform */ break; default: /* shouldn't get here - if we do, set a legal state */ ctrl_shm->state = IDLE; break; } /* done */ }
int main(int argc, char **argv) { FILE *file; struct wav_header header; unsigned int card = 0; unsigned int device = 0; unsigned int channels = 2; unsigned int rate = 44100; unsigned int bits = 16; unsigned int frames; unsigned int period_size = 1024; unsigned int period_count = 4; enum pcm_format format; if (argc < 2) { fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] " "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]); return 1; } file = fopen(argv[1], "wb"); if (!file) { fprintf(stderr, "Unable to create file '%s'\n", argv[1]); return 1; } /* parse command line arguments */ argv += 2; while (*argv) { if (strcmp(*argv, "-d") == 0) { argv++; if (*argv) device = atoi(*argv); } else if (strcmp(*argv, "-c") == 0) { argv++; if (*argv) channels = atoi(*argv); } else if (strcmp(*argv, "-r") == 0) { argv++; if (*argv) rate = atoi(*argv); } else if (strcmp(*argv, "-b") == 0) { argv++; if (*argv) bits = atoi(*argv); } else if (strcmp(*argv, "-D") == 0) { argv++; if (*argv) card = atoi(*argv); } else if (strcmp(*argv, "-p") == 0) { argv++; if (*argv) period_size = atoi(*argv); } else if (strcmp(*argv, "-n") == 0) { argv++; if (*argv) period_count = atoi(*argv); } if (*argv) argv++; } header.riff_id = ID_RIFF; header.riff_sz = 0; header.riff_fmt = ID_WAVE; header.fmt_id = ID_FMT; header.fmt_sz = 16; header.audio_format = FORMAT_PCM; header.num_channels = channels; header.sample_rate = rate; switch (bits) { case 32: format = PCM_FORMAT_S32_LE; break; case 24: format = PCM_FORMAT_S24_LE; break; case 16: format = PCM_FORMAT_S16_LE; break; default: fprintf(stderr, "%d bits is not supported.\n", bits); return 1; } header.bits_per_sample = pcm_format_to_bits(format); header.byte_rate = (header.bits_per_sample / 8) * channels * rate; header.block_align = channels * (header.bits_per_sample / 8); header.data_id = ID_DATA; /* leave enough room for header */ fseek(file, sizeof(struct wav_header), SEEK_SET); /* install signal handler and begin capturing */ signal(SIGINT, sigint_handler); frames = capture_sample(file, card, device, header.num_channels, header.sample_rate, format, period_size, period_count); printf("Captured %d frames\n", frames); /* write header now all information is known */ header.data_sz = frames * header.block_align; header.riff_sz = header.data_sz + sizeof(header) - 8; fseek(file, 0, SEEK_SET); fwrite(&header, sizeof(struct wav_header), 1, file); fclose(file); return 0; }
int main(int argc, char **argv) { FILE *file; struct wav_header header; unsigned int card = 0; unsigned int device = 0; unsigned int channels = 2; unsigned int rate = 44100; unsigned int bits = 16; unsigned int frames; if (argc < 2) { fprintf(stderr, "Usage: %s file.wav [-d device] [-c channels] " "[-r rate] [-b bits]\n", argv[0]); return 1; } file = fopen(argv[1], "wb"); if (!file) { fprintf(stderr, "Unable to create file '%s'\n", argv[1]); return 1; } /* parse command line arguments */ argv += 2; while (*argv) { if (strcmp(*argv, "-d") == 0) { argv++; device = atoi(*argv); } else if (strcmp(*argv, "-c") == 0) { argv++; channels = atoi(*argv); } else if (strcmp(*argv, "-r") == 0) { argv++; rate = atoi(*argv); } else if (strcmp(*argv, "-b") == 0) { argv++; bits = atoi(*argv); } argv++; } header.riff_id = ID_RIFF; header.riff_sz = 0; header.riff_fmt = ID_WAVE; header.fmt_id = ID_FMT; header.fmt_sz = 16; header.audio_format = FORMAT_PCM; header.num_channels = channels; header.sample_rate = rate; header.bits_per_sample = bits; header.byte_rate = (header.bits_per_sample / 8) * channels * rate; header.block_align = channels * (header.bits_per_sample / 8); header.data_id = ID_DATA; /* leave enough room for header */ fseek(file, sizeof(struct wav_header), SEEK_SET); /* install signal handler and begin capturing */ signal(SIGINT, sigint_handler); signal(SIGTERM, sigint_handler); fprintf(stdout, "Output channel(%d) sample(%d) bits(%d)\n", header.num_channels, header.sample_rate, header.bits_per_sample); fprintf(stdout, "from card(%d) device(%d)\n", card, device); frames = capture_sample(file, card, device, header.num_channels, header.sample_rate, header.bits_per_sample); printf("Captured %d frames\n", frames); /* write header now all information is known */ header.data_sz = frames * header.block_align; fseek(file, 0, SEEK_SET); fwrite(&header, sizeof(struct wav_header), 1, file); fclose(file); return 0; }
/** * Record sensor data and capture images. * Activity is recorded to DATA.LOG to be picked up by the Skywire task. */ void camera_task(void * pvParameters) { int low_pow = 0; /* Initialize the peripherals and state for this task. */ if (!camera_task_setup()) { trace_printf("camera_task: setup failed\n"); vTaskDelete(NULL); return; } else { trace_printf("camera_task: started\n"); } /* Initialize timing for capture sub-tasks. */ TickType_t lastReading, lastCapture; lastReading = lastCapture = xTaskGetTickCount(); /* Initialize the sample buffer. */ samples.Count = 0; for (;;) { TickType_t currentTicks = xTaskGetTickCount(); if ((currentTicks - lastReading) >= SAMPLE_RATE_MS) { capture_sample(&lastReading); } //point at which image is captured from camera if (arduCamInstalled) { if ((currentTicks - lastCapture) >= IMAGE_RATE_MS) { //if camera is currently in low power mode, exit low power mode before taking image if (low_pow == 1){ if(arducam_low_power_remove() != DEVICES_OK){ trace_printf("Error removing low power mode \n"); } else{ low_pow = 0; } } //only try taking image if low power has been properly disabled if (low_pow == 0){ capture_image(&lastCapture); } //after image has been taken, put camera down into low-power mode if (low_pow == 0){ if(arducam_low_power_set() != DEVICES_OK){ trace_printf("Error setting low power mode \n"); } else{ trace_printf("Low power mode set \n"); low_pow = 1; } } } vTaskDelay(100); } } }