Exemplo n.º 1
0
int main(int argc, char const *argv[])
{
	FILE *f = fopen(argv[1], "r");
	if (!f) {
		printf("Could not open file %s\n", argv[1]);
		exit(EXIT_FAILURE);
	}

	/* read entire file into memory */
	char *contents = read_entire_file(f);
	fclose(f);

	/* solve all sudokus */
	struct timeval before;
	gettimeofday (&before, NULL);

	int result_size = 1024;
	char *result = malloc(sizeof(char) * result_size);
	strcpy(result, "");

	char *ptr = contents;
	int total_sudokus = strlen(contents) / (BOARD_SIZE*BOARD_SIZE);
	int sudokus_solved = 0;
	char solved[256];
	do {
		int sudoku[BOARD_SIZE][BOARD_SIZE];
		ptr = from_str(ptr, sudoku);
		solve(sudoku);
		to_str(sudoku, solved);
		result_size = append_str(&result, result_size, solved);
		load_bar(++sudokus_solved, total_sudokus, 100, 100);
	} while (*ptr);
	free(contents);

	struct timeval after;
	gettimeofday (&after, NULL);
	long duration = ((after.tv_sec * 1000000 + after.tv_usec) -
		(before.tv_sec * 1000000 + before.tv_usec)) / 1000;

	/* Write to file the solved sudokus */
	char output_name[64];
	sprintf(output_name, "solved_%s", argv[1]);
	FILE *output = fopen(output_name, "w");
	if (!output) {
		printf("Could not open file %s\n", output_name);
		exit(EXIT_FAILURE);
	}
	fprintf(output, "%s", result);
	free(result);
	fclose(output);

	/* Present time result */
	printf("-- Elapsed time: %d ms\n", duration);
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {

    int arg=1;
    int sbig_type = NO_CAMERA;
    int info_mode = 0;
    int verbose = 0;
    char name[MAX_STRING] = "";
    char ra[MAX_STRING] = "";
    char dec[MAX_STRING] = "";
    char alt[MAX_STRING] = "";
    char az[MAX_STRING] = "";
    char imtype[8];
    int phase;
    float exptime;
    int err;

    /* Set an interrupt handler to trap Ctr-C nicely */
    if(signal(SIGINT, SIG_IGN) != SIG_IGN)
		signal(SIGINT, InterruptHandler);

    /*  set lockfile from the outset */
    get_lock();
    store_pid_in_lockfile();


    /* parse args */
    if (argc < 3) {
        error_exit(usage);
    };
    while (arg < argc - 2) 
    {
        switch (argv[arg++][1]) {
            case 'v':
                verbose = 1;
                SetVerbosity(verbose);
                break;
            case 'n':
                sscanf(argv[arg++], "%s", name);
                break;
             case 'r':
                sscanf(argv[arg++], "%s", ra);
                break;
             case 'd':
                sscanf(argv[arg++], "%s", dec);
                break;
             case 'a':
                sscanf(argv[arg++], "%s", alt);
                break;
             case 'z':
                sscanf(argv[arg++], "%s", az);
                break;
             default:
                error_exit(usage);
                break;
        }
    }
    sscanf(argv[arg++],"%s",imtype);
    sscanf(argv[arg++],"%f",&exptime);

    // Determine what kind of image to take 
    switch(value_from_imagetype_key(imtype)) {
        case DARK:   ccd_type=DARK;  if (verbose) printf("Taking dark frame.\n");  break;
        case LIGHT:  ccd_type=LIGHT; if (verbose) printf("Taking light frame.\n"); break;
        case BIAS:   ccd_type=BIAS;  if (verbose) printf("Taking bias frame.\n");  break;
        case FLAT:   ccd_type=FLAT;  if (verbose) printf("Taking flat frame.\n");  break;
        case BADKEY: fprintf(stderr,"Unknown image type %s\n",imtype); return(1); break;
    }
    fflush(stdout);

    CountCameras();
    if (ccd_ncam < 1){
	fprintf(stderr,"Found 0 cameras\n");
        return(1);
    }

    InitializeAllCameras();
    store_timestamped_note_in_lockfile("Started");
    store_directory_in_lockfile();
    char myline[128];
    sprintf(myline,"Exptime: %5.1f\n",exptime);
    store_note_in_lockfile(myline);


    // Start integrations going on each of the cameras one by one.
    for (int cam_num = 0; cam_num <ccd_ncam; cam_num++)
    {
        err = SetActiveCamera(cam_num);
        ccd_image_data[cam_num] = 
            (unsigned short *) malloc(ccd_image_width*ccd_image_height*sizeof(unsigned short));
        phase = 0;
        err = CaptureImage(&phase,ccd_image_data[cam_num],ccd_type,exptime,FALSE,0,0,0,0);
    }

    // Wait until the data is ready. I'm intentionally playing this safe by
    // making sure I wait at least 1s before trying to read out the data.
    // This is something I will have to look into if I ever use this for
    // fast focusing.
    int nsec = (int) exptime + 1;
    int count = 0;
    for (int i=0; i<=nsec;i++)
    {
        if(nsec > 3 && nsec < 10){ 
            load_bar(count++,nsec,3,30);
        }
        else if (nsec >  10 && nsec < 100){
            load_bar(count++,nsec,(int)(nsec/2),30);
        }
        else if (nsec >  100){
            load_bar(count++,nsec,(int)(nsec/3),30);
        }
        sleep(1);
    }

    // The cameras are ready to be read out. We once again cycle over each camera and 
    // save the data.
    
    for (int cam_num = 0; cam_num <ccd_ncam; cam_num++)
    {
        char infoline[128];
        phase = 1;
        err = SetActiveCamera(cam_num); 
        fflush(stderr);
        err = CaptureImage(&phase,ccd_image_data[cam_num],ccd_type,exptime,FALSE,0,0,0,0);

        // Print out some pixel values to let the user check data integrity
        if (verbose)
            printf("Some pixel values: %u %u %d\n",
                    *(ccd_image_data[cam_num] + 10000), 
                    *(ccd_image_data[cam_num] + 15000), 
                    *(ccd_image_data[cam_num]+20000)); 

        // Figure out what to call the new file
        char *newname;
        newname = (char *)malloc(MAX_STRING*sizeof(char));
        new_filename(ccd_serial_number,imtype,newname);    

        // Save as a FITS file
        GetCameraTemperature();
        double temperature = ccd_camera_info[ActiveCamera()].temperature;
        int filterNumber = 0;
        if (IsCameraAnST402ME())
            filterNumber = FilterWheelPosition();
        write_fits(newname, ccd_image_width, ccd_image_height, ccd_image_data[cam_num], 
                   exptime,imtype,temperature,filterNumber,ccd_serial_number, name,
                   ra,dec,alt,az);
        fprintf(stderr,"Saved %s \n",newname);
        sprintf(infoline,"Camera %d wrote: %s\n",cam_num,newname);
        store_note_in_lockfile(infoline);
        free(ccd_image_data[cam_num]);
        free(newname);

    }

    DisconnectAllCameras();
    release_lock();

    store_timestamped_note_in_lockfile("Completed");

    // Release the lock file
    release_lock();

    // Ring bell to wake up the astronomer
    if (verbose)
    	printf("Camera(s) opened and closed successfully.\n");
    putchar('\a'); putchar('\a'); putchar('\a');

    return(0);

}
Exemplo n.º 3
0
/* Launches one of those fancy effects. */
void launch_effect(int effect)
{
    switch (effect) {
    case 0:
	/* Lights all the layers one by one. */
	load_bar(1000);
	break;
    case 1:
	/* A pixel bouncing randomly around. */
	boing_boing(150, 500, 0x03, 0x01);
	break;
    case 2:
	/* Randomly fill and empty the cube. */
	fill(0x00);
	random_filler(100, 1, 500, 1);
	random_filler(100, 1, 500, 0);
	break;
    case 3:
	/* Send voxels randomly back and forth the z-axis. */
	send_voxels_rand_z(150, 500, 2000);
	break;
    case 4:
	/* Spinning spiral */
	spiral(1, 75, 1000);
	break;
    case 5:
	/* A coordinate bounces randomly around the cube. For every position
	 * the status of that voxel is toggled. */
	boing_boing(150, 500, 0x03, 0x02);
	break;
    case 6:
	/* Random raindrops */
	rain(40, 1000, 500, 500);
	break;
		
    case 7:
	/* Snake: a snake randomly bounce around the cube. */
	boing_boing(150, 500, 0x03, 0x03);
	break;
    case 8:
	/* Spinning plane */
	spinning_plane(1, 50, 1000);
	break;
    case 9:
	/* Set x number of random voxels, delay, unset them. x increases
	 * from 1 to 32 and back to 1. */
	random_2(48);
	break;
    case 10:
	/* Set then unset all 64 voxels in a random order. */
	random_filler2(200, 1);
	delay_ms(2000);
	random_filler2(200, 0);
	delay_ms(1000);
	break;
    case 11:
	/* Bounce a plane up and down all the directions. */
	fly_plane('z', 1, 1000);
	delay_ms(2000);
	fly_plane('y', 1, 1000);
	delay_ms(2000);
	fly_plane('x', 1, 1000);
	delay_ms(2000);
	fly_plane('z', 0, 1000);
	delay_ms(2000);
	fly_plane('y', 0, 1000);
	delay_ms(2000);
	fly_plane('x', 0, 1000);
	delay_ms(2000);
	break;
    case 12:
	/* Fade in and out at low framerate. */
	blinky2();
	break;
    case 13:
	/* Random walk */
	random_walk(4, 250, 1000);
	break;
    case 14:
	/* Spinning square */
	spinning_square(1, 50, 1000);
	break;
    }
}