//Set the camera's quality to a certain choice
//Current Choices are:
// 0: Large Fine JPEG
// 2: Medium Fine JPEG
int QualityControl(int quality){
	char qualString[20];

	if(quality == currentQuality){ //We don't want to take up cycles changing nothing
		return -1; //Could define set error codes, but we don't look at them anyway
	}

	strncpy(qualString, "", sizeof qualString); //Debatably necessary

	if(quality == 0){
		strncpy(qualString, "Large Fine JPEG", sizeof qualString);
	}
	else if(quality == 2){
		strncpy(qualString, "Medium Fine JPEG", sizeof qualString);
	}
	else{	
		return -1;
	}

	CameraWidget* widget,* child;

	if(gp_camera_get_config(getMyCamera(), &widget, getMyContext()) != 0){ //Get our current config
		free(widget);
		free(child);
		return -1; //If it fails, we have failed.
	}

	//Widgets in libgphoto act sort of as trees of settings/data
	//We need to find the right child of the top level, and the right child of that, etc.
	//until we get down to the appropriate quality setting widget
	//I already parsed through the tree and found the right child, and so
	//have hard-coded the values to get to this child here
	//Check out gphoto2-widget.c for more info

	gp_widget_get_child(widget, 3, &child);
	widget = child;
	gp_widget_get_child(widget, 0, &child);
	widget = child;

	//Here we change the quality value to whatever we want
	//For some reason, it is saved as a string, not a choice
	//Don't ask me why.
	gp_widget_set_value(widget, qualString);
	gp_widget_get_root(widget, &child);

	if(gp_camera_set_config(getMyCamera(), child, getMyContext()) != 0){ //Set the camera's config to our new, modified config
		free(widget);
		free(child);
		return -1;
	}

	free(widget);
	free(child);

	currentQuality = quality; //Remember what quality we currently have
	return 0;
}
//GET THREAD
//Gets Files over USB. Camera -> Memory
void * GetEvents(void*aaa){
    CameraFile* my_File;
    CameraEventType my_Type;
    void* retevent;
    struct timespec time;
    char filename[40];
    while(1){
        CameraControl();
        do{
            //Wait for camera to fire an event. Most are useless/not used by us
            //When a picture is taken (triggered on camera), FILE_ADDED event fired
            //retevent will contain a CameraFilePath object with name/loc on camera of file
            //Timeout is 100 <units, probably ms>
            gp_camera_wait_for_event(getMyCamera(), 100, &my_Type, &retevent, getMyContext());
            if(my_Type == GP_EVENT_FILE_ADDED){
                CameraFilePath* my_FP = (CameraFilePath*)retevent;

                gp_file_new(&my_File);

                //Get the file from the given location to memory
                gp_camera_file_get(getMyCamera(), my_FP->folder, my_FP->name, GP_FILE_TYPE_NORMAL, my_File, getMyContext());

                //Get time in ms since Epoch -> used for filename for Skynet purposes
                clock_gettime(CLOCK_REALTIME, &time);
                long long time_millis = time.tv_sec * 1000 + time.tv_nsec / 1000000; 
                sprintf(filename, "%s/%llu.jpg", ImagesFolder.c_str(), time_millis);
                
                AddFile(my_File, filename);

                //Save the last obtained Telemetry/GPS to disk, with the same filename we just used
                saveLast(time_millis);

                //Tell the Save Thread that there is a new CameraFile enqueued
                sem_post(&NewFileSem);
                free(my_FP);
            }
        } while(decrementWaitCounter() > 0); 
        //WaitCounter is a variable used to cycle through images possibly still on the camera
        //Used to cycle out images before switching quality, see CameraControl.c
        setWaitCounter(0); 
    }
    return NULL;
}