int main(int argc, char *argv[]) {
 	int flag;
 	int error;
 	char type[16];
 	int device_count = 0;
 	int i;

    ////////////////////////////////////////////////////////////////////////////
    // parse & validate arguments
    ////////////////////////////////////////////////////////////////////////////
 	parseArgs(argc, argv);

 	if (dtg.year == 0)
 	{
 		printf("ERROR: invalid dtg provided\n");
 		usage();
 	}

 	printf("sampleNum is: %i\n", sampleNum);

 	if (sampleNum==0){
 		printf("Setting up the header file for the csv\n");
 	}else if (sampleNum >= 1){
 		printf("performing normal sampling\n");
 	}else {
 		printf("ERROR: incorrect scanNum option provided\n");
 		usage();
 	}

    ////////////////////////////////////////////////////////////////////////////

 	for(i = 0; i < SEABREEZE_MAX_DEVICES; i++) {
 		printf("\nOpening spectrometer %d.\n", i);
 		flag = seabreeze_open_spectrometer(i, &error);
 		printf("...Result is (%d) [%s]\n", flag, get_error_string(error));
 		if(0 == flag) {
 			device_count++;
 		} else {
 			break;
 		}
 	}
    

 	for(i = 0; i < device_count; i++) {
 		printf("\nGetting device %d name.\n", i);
 		seabreeze_get_model(i, &error, type, sizeof(type));
 		printf("...Result is (%s) [%s]\n", type, get_error_string(error));
 	}

 	printf("Number of devices found is: %d\n", device_count);

 	for(i = 0; i < device_count; i++) {
 		printf("\nSetting device %d integration time to 100ms.\n", i);
 		seabreeze_set_integration_time_microsec(i, &error, 100000);
 		//printf("...Result is [%s]\n", get_error_string(error));
 	}

 	if (deviceIndex < device_count){
 		printf("\n\nBegin sampling using device: %d\n", deviceIndex);

 		if (sampleNum == 0){
 			get_wavelengths(deviceIndex);
 		}else if (sampleNum>=1){
 			get_spectrum(deviceIndex);
 		}
 	}else{
 		printf("deviceIndex provided in arguments is out of range of the number of devices found" );
 		usage();
 	}
 	
 	//get_raw_spectrum_test(i);

 	////////////////////////////////////////////////////////////////////////////////
 	/////close the spectrometers
 	///////////////////////////////////////////////////////////////////////////////
 	for(i = 0; i < device_count; i++) {
 		printf("\nClosing spectrometer %d.\n", i);
 		flag = seabreeze_close_spectrometer(i, &error);
 		//printf("...Result is (%d) [%s]\n", flag, get_error_string(error));
 	}

 	seabreeze_shutdown();

 	return 0;
 }
Пример #2
0
int main(int argc, char **argv) {
    int error = 0;

    parseArgs(argc, argv);

    if (gArgs.debug)
        seabreeze_set_verbose(1);

    sleep(1);

    ////////////////////////////////////////////////////////////////////////////
    // count connected devices
    ////////////////////////////////////////////////////////////////////////////

    for (int i = 0; i < SEABREEZE_MAX_DEVICES; i++) {
        if (!seabreeze_open_spectrometer(i, &error)) {
            connected_device_count++;
        } else {
            break;
        }
    }

    LOG_DEBUG(("%d devices found", connected_device_count));
    if (!connected_device_count) {
        puts("ERROR: no spectrometers found");
        exit(1);
    }

    ////////////////////////////////////////////////////////////////////////////
    // look for desired spectrometer
    ////////////////////////////////////////////////////////////////////////////

    int matching_device_count = 0;
    int matching_devices[MAX_DEVICES];

    // iterate over connected devices, attempting to match on model & serial
    char thisType[MAX_LABEL_SIZE + 1];
    char thisSerialNumber[MAX_LABEL_SIZE + 1];
    memset(thisSerialNumber, 0, sizeof(thisSerialNumber));

    // header if requested
    if (gArgs.list) {
        puts("## Type            Serial Number");
        puts("-- --------------- ---------------");
    }

    for(int i = 0; i < connected_device_count; i++) {
        int potential_match = 1;

        // match on index
        if (gArgs.index >= 0) {
            if (i == gArgs.index) {
                LOG_DEBUG(("[%02d] matches desired index"));
            } else {
                LOG_DEBUG(("[%02d] does not match desired index"));
                potential_match = 0;
            }
        }

        // match on spectrometer type
        if (potential_match) {
            seabreeze_get_model(i, &error, thisType, sizeof(thisType));
            if (!error) {
                if (gArgs.type) {
                    if (!strcmp(thisType, gArgs.type)) {
                        LOG_DEBUG(("[%02d] matches desired spectrometer type '%s'", i, thisType));
                    } else {
                        LOG_DEBUG(("[%02d] does not match desired spectrometer type '%s' (%s)", i, gArgs.type, thisType));
                        potential_match = 0;
                    }
                } else {
                    LOG_DEBUG(("[%02d] has spectrometer type '%s' (no match criteria provided)", i, thisType));
                }
            } else {
                printf("[%02d] Error getting type: %s\n", i, get_error_string(error));
                potential_match = 0;
            }
        }

        // match on serial_number
        if (potential_match) {
            seabreeze_get_serial_number(i, &error, thisSerialNumber, sizeof(thisSerialNumber));
            if (!error) {
                if (gArgs.serialNumber) {
                    if (!strncmp(thisSerialNumber, gArgs.serialNumber, sizeof(thisSerialNumber))) {
                        LOG_DEBUG(("[%02d] matches desired serial number '%s'", i, thisSerialNumber));
                    } else {
                        LOG_DEBUG(("[%02d] does not match desired serial number '%s' (%s)", i, gArgs.serialNumber, thisSerialNumber));
                        potential_match = 0;
                    }
                } else {
                    LOG_DEBUG(("[%02d] has serial number '%s' (no match criteria provided)", i, thisSerialNumber));
                }
            } else {
                printf("[%02d] Error getting serial number: %s\n", i, get_error_string(error));
                potential_match = 0;
            }
        }

        // collect matching indices
        if (potential_match) {
            LOG_DEBUG(("[%02d] matched all search criteria", i));
            matching_devices[matching_device_count++] = i;
            if (gArgs.list) {
                printf("%02d %-*s %-*s\n", i, MAX_LABEL_SIZE, thisType, MAX_LABEL_SIZE, thisSerialNumber);
            }
        } else {
            LOG_DEBUG(("[%02d] failed to match search criteria", i));
        }
    }

    // give up if no matches
    if (!matching_device_count) {
        if (gArgs.list)
            puts("(no matching devices found)");
        cleanUpAndClose(!gArgs.list);
    }

    // reject invalid unary command combinations
    if (matching_device_count != 1) {
        if (gArgs.serialNumberNew) {
            printf("Error: can only update serial number on exactly one spectrometer (%d matched)\n",
                matching_device_count);
            cleanUpAndClose(1);
        }
    }

    ////////////////////////////////////////////////////////////////////////////
    // Process matching devices
    ////////////////////////////////////////////////////////////////////////////

    // process each matching device
    for (int i = 0; i < matching_device_count; i++) {
        int index = matching_devices[i];
        int error = 0;

        // get pixel count
        int pixels = seabreeze_get_formatted_spectrum_length(index, &error);
        LOG_DEBUG(("[%02d] has %d pixels", index, pixels));

        // --list-eeprom
        if (gArgs.listEEPROMs) {
            printf("[%02d] EEPROM slots 0..%d:\n", index, gArgs.listEEPROMs - 1);
            for (int j = 0; j < gArgs.listEEPROMs; j++) {
                char* errorMessage = NULL;
                unsigned char buf[MAX_LABEL_SIZE + 1];
                memset(buf, 0, sizeof(buf));

                // read value (both raw and as double)
                seabreeze_read_eeprom_slot(index, &error, j, buf, sizeof(buf));
                if (error) {
                    errorMessage = (char*) get_error_string(error);
                }

                double d = atof((const char*) buf);

                // report
                printf("[%02d]   %02d:", index, j);
                for (int k = 0; k < MAX_LABEL_SIZE; k++)
                    printf(" %02x", buf[k]);
                printf(" %+le [", d);
                for (int k = 0; k < MAX_LABEL_SIZE; k++)
                    printf("%c", buf[k] > 31 && buf[k] < 127 ? buf[k] : '.');
                printf("]");
                if (errorMessage)
                    printf(" (returned error: %s)", errorMessage);
                printf("\n");
                fflush(stdout);
            }
        }

        // --list-descriptors
        if (gArgs.listDescriptors) {
            printf("[%02d] USB Descriptors:\n", index);

            // Ocean Optics only uses descriptor strings 1 and 2, sometimes 3
            for (int j = 1; j < 3; j++)
            {
                int bytesCopied = 0;
                char* errorMessage = NULL;
                unsigned char buf[80];
                memset(buf, 0, sizeof(buf));

                bytesCopied = seabreeze_get_usb_descriptor_string(index, &error, j, buf, sizeof(buf));
                if (error) {
                    errorMessage = (char*) get_error_string(error);
                }

                // report
                printf("[%02d]   %02d:", index, j);
                if (errorMessage) {
                    printf(" (returned error %s)", errorMessage);
                } else if (bytesCopied) {
                    printf(" [%s]", (char*) buf);
                }
                printf("\n");
                fflush(stdout);
            }
        }

        // --list-irrad
        if (gArgs.listIrrad) {
            float *irrad = (float*) malloc(pixels * sizeof(float));
            int count = seabreeze_read_irrad_calibration(index, &error, irrad, pixels);
            fflush(stdout);
            if (!check_error(index, &error, "seabreeze_read_irrad_calibration")) {
                printf("[%02d] loaded %d irradiance calibration coefficients:\n", index, count);
                for (int j = 0; j < count; j++) {
                    printf("[%02d]   Coeff #%04d: %+le\n", index, j, irrad[j]);
                }
            }
            fflush(stdout);
            free(irrad);
        }

        // --list-edc-pixels
        if (gArgs.listEDCPixels) {
            #define MAX_INDICES 100
            int indices[MAX_INDICES];
            int count = seabreeze_get_electric_dark_pixel_indices(index, &error, indices, MAX_INDICES);
            fflush(stdout);
            if (!check_error(index, &error, "seabreeze_get_electric_dark_pixel_indices")) {
                printf("[%02d] found %d electrical dark pixel indices:\n", index, count);
                for (unsigned j = 0; j < count; j++) {
                    printf("[%02d]    EDC index #%04d: %d\n", index, j, indices[j]);
                }
            }
        }

        // --set-irrad
        if (gArgs.irradFilename) {
            FILE *infile = NULL;
            if ((infile = fopen(gArgs.irradFilename, "r")) != NULL) {
                float *irrad = (float*) malloc(pixels * sizeof(float));
                int count = 0;
                while(count < pixels && !feof(infile)) {
                    float f;
                    int ok = fscanf(infile, "%f", &f);
                    if (ok) {
                        irrad[count++] = f;
                    }
                }
                fclose(infile);
                printf("[%02d] read %d floats from %s\n", index, count, gArgs.irradFilename);
                if (count != pixels)
                    printf("[%02d] WARNING: irradiance count %d != device pixel count %d\n", index, count, pixels);
                printf("[%02d] writing %d floats to spectrometer (may take awhile)\n", index, count);
                fflush(stdout);
                int ok = seabreeze_write_irrad_calibration(index, &error, irrad, count);
                if (ok && !check_error(index, &error, "seabreeze_read_irrad_calibration")) {
                    printf("[%02d] wrote %d irradiance calibration coefficients to spectrometer\n",
                        index, count);
                }
                free(irrad);
            } else {
                printf("Error: can't read %s\n", gArgs.irradFilename);
            }
        }

        // --set-serial-number
        if (gArgs.serialNumberNew) {
            printf("[%02d] updating serial_number => [%s]\n", index, gArgs.serialNumberNew);
            int len = strlen(gArgs.serialNumberNew);
            unsigned char *buffer = malloc(len + 1);
            strncpy((char*) buffer, gArgs.serialNumberNew, len + 1);
            seabreeze_write_eeprom_slot(index, &error, 0, buffer, len + 1);
            free(buffer);
        }

        // --set-eeprom-value
        if (gArgs.eepromValue) {
            unsigned char *buffer = (unsigned char*) malloc(MAX_LABEL_SIZE);
            printf("[%02d] setting eeprom slot %02d (0x%02x) ==> [%s] (bufferSize %d)\n", index, 
                gArgs.eepromIndex, gArgs.eepromIndex, gArgs.eepromValue, MAX_LABEL_SIZE);
            renderHex(buffer, MAX_LABEL_SIZE, gArgs.eepromValue);
            for (int byteIndex = 0; byteIndex < MAX_LABEL_SIZE; byteIndex++)
                printf("[%02d]   byte %02d: 0x%02x\n", index, byteIndex, buffer[byteIndex]);
            seabreeze_write_eeprom_slot(index, &error, gArgs.eepromIndex, buffer, MAX_LABEL_SIZE);
            free(buffer);
        }

        // other future commands here...
    }

    // cleanup
    cleanUpAndClose(0);
}