Index: exposurses.c ================================================================== --- exposurses.c +++ exposurses.c @@ -29,11 +29,11 @@ "11", "12", "13", "14", "15", - "16", + "16", NULL }; char *iso_array[] = { "50", @@ -45,10 +45,11 @@ "3200", NULL }; char *shutter_array[] = { + "OVER", "1/1000", "1/500", "1/250", "1/125", "1/60", @@ -56,22 +57,25 @@ "1/15", "1/8", "1/4", "1/2", "1", + "UNDER", NULL }; char *aperture_array[] = { + "UNDER", "f/1.4", "f/2", "f/2.8", "f/4", "f/5.6", "f/8", "f/11", "f/16", + "OVER", NULL }; ITEM **exposure_items; ITEM **iso_items; @@ -92,11 +96,11 @@ MENU *add_menu(char **array, ITEM **items, int n); WINDOW *add_window(int xpos, char *title); int exposure(int iso); double shutter(int exposure, double aperture); double aperture(int exposure, double shutter); -int nearest_match(double x, int menu); +int nearest_match(double x, int menu, int n_array); double fraction_to_double(char *fraction); /* No one will ever need more than 9 bytes! */ char exposure_sel[9] = ""; char iso_sel[9] = ""; char shutter_sel[9] = ""; @@ -230,11 +234,12 @@ /* There is probably a nicer way to format the below */ set_menu_pattern( shutter_menu, shutter_array[nearest_match( shutter(exposure(atoi(iso_sel)), strtod(aperture_sel_, NULL)), - 3 + 3, + n_shutter )] ); menu_driver(shutter_menu, REQ_DOWN_ITEM); menu_driver(shutter_menu, REQ_UP_ITEM); wrefresh(shutter_win); @@ -244,11 +249,12 @@ menu_driver(aperture_menu, REQ_SCR_DPAGE); set_menu_pattern( aperture_menu, aperture_array[nearest_match( aperture(exposure(atoi(iso_sel)), fraction_to_double(shutter_sel)), - 4 + 4, + n_aperture )] ); menu_driver(aperture_menu, REQ_DOWN_ITEM); menu_driver(aperture_menu, REQ_UP_ITEM); wrefresh(aperture_win); @@ -373,49 +379,78 @@ double aperture (int exposure, double shutter) { /* EV = log2 (N^2/t) */ return sqrt(pow(2, exposure) * shutter); } -int nearest_match (double x, int menu) { +int nearest_match (double x, int menu, int n_array) { /* Need to search array for closest match */ int n; - int diff_idx = 0; + int diff_idx = 1; char array_value_str[9]; double array_value_db; double diff; /* Need a starting value for difference */ switch(menu) { case 3: - array_value_db = fraction_to_double(shutter_array[0]); + array_value_db = fraction_to_double(shutter_array[1]); break; case 4: - strncpy(array_value_str, aperture_array[0]+2, 4); + strncpy(array_value_str, aperture_array[1]+2, 4); array_value_db = strtod(array_value_str, NULL); break; } diff = fabs(array_value_db - x); /* lots of repetition here but pointers to arrays seem to be a bad thing */ + /* Could do from n = 3 (2 above) until != under/over + * but also need to do something like if diff is more than twice max/min then under/over */ switch(menu) { case 3: - for ( n = 1; shutter_array[n] != NULL; ++n ) { + for ( n = 2; n < n_array-2; ++n ) { array_value_db = fraction_to_double(shutter_array[n]); if (fabs(array_value_db - x) < diff) { diff_idx = n; diff = fabs(array_value_db - x); } + } + /* Check if at extremities and then if under/over exposed */ + if (diff_idx == 1) { + if (diff >= fraction_to_double(shutter_array[1])/2) { /* diff is greater than diff of next one down minus max/min */ + diff_idx = 0; + } + } + if (diff_idx == n_array-3) { + if (diff >= fraction_to_double(shutter_array[n_array-3])*2) { + diff_idx = n_array-2; + } } break; case 4: - for ( n = 1; aperture_array[n] != NULL; ++n ) { + for ( n = 2; n < n_array-2; ++n ) { strncpy(array_value_str, aperture_array[n]+2, 4); array_value_db = strtod(array_value_str, NULL); if (fabs(array_value_db - x) < diff) { diff_idx = n; diff = fabs(array_value_db - x); } } + /* Apertures similarly. Although progression is fiddlier.*/ + if (diff_idx == 1) { + strncpy(array_value_str, aperture_array[1]+2, 4); + array_value_db = strtod(array_value_str, NULL); + if (diff >= array_value_db/sqrt(2.0)) { /* diff is greater than diff of next one down minus max/min */ + diff_idx = 0; + } + } + if (diff_idx == n_array-3) { + strncpy(array_value_str, aperture_array[n_array-3]+2, 4); + array_value_db = strtod(array_value_str, NULL); + if (diff >= array_value_db*sqrt(2.0)) { + diff_idx = n_array-2; + } + } + break; } return diff_idx; }