exposurses

Check-in [6513b427e7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Implement functionality to calc req'd and then sel nearest match in menu So far only implemented on Shutter menu, i.e. have to select ISO and Aperture, it will then use the equations to calculate the exact required shutter speed, but then select the nearest value in the menu. In order to do that added these two functions: - nearest_match - fraction_to_double The latter needed, because need to convert "1/1000", etc into float. For Aperture menu items can just strip off the "f/". Seems to work for Sunny 16.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/master | trunk
Files: files | file ages | folders
SHA3-256: 6513b427e7cb2d6191054029b8a73724a11be100601d6317c34b2a9d480a3ce3
User & Date: base@atomicules.co.uk 2013-08-29 23:24:17
Context
2013-08-31
07:47
Remove extraneous line that came from menu_scroll.c check-in: ed161d4179 user: base@atomicules.co.uk tags: origin/master, trunk
2013-08-29
23:24
Implement functionality to calc req'd and then sel nearest match in menu So far only implemented on Shutter menu, i.e. have to select ISO and Aperture, it will then use the equations to calculate the exact required shutter speed, but then select the nearest value in the menu. In order to do that added these two functions: - nearest_match - fraction_to_double The latter needed, because need to convert "1/1000", etc into float. For Aperture menu items can just strip off the "f/". Seems to work for Sunny 16. check-in: 6513b427e7 user: base@atomicules.co.uk tags: origin/master, trunk
2013-08-27
12:00
Fix order of arguements in exposure functions check-in: c663824d67 user: base@atomicules.co.uk tags: origin/master, trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to exposurses.c.

    10     10   char *iso_array[] = {
    11     11   	"50",
    12     12   	"100",
    13     13   	"200",
    14     14   	"400",
    15     15   	"800",
    16     16   	"1600",
    17         -	"3200"
           17  +	"3200",
           18  +	NULL
    18     19   };
    19     20   
    20     21   char *shutter_array[] = {
    21     22   	"1/1000",
    22     23   	"1/500",
    23     24   	"1/250",
    24     25   	"1/125",
    25     26   	"1/60",
    26     27   	"1/30",
    27     28   	"1/15",
    28     29   	"1/8",
    29     30   	"1/4",
    30     31   	"1/2",
    31         -	"1"
           32  +	"1",
           33  +	NULL
    32     34   };
    33     35   
    34     36   char *aperture_array[] = {
    35     37   	"f/1.4",
    36     38   	"f/2",
    37     39   	"f/2.8",
    38     40   	"f/4",
    39     41   	"f/5.6",
    40     42   	"f/8",
    41     43   	"f/11",
    42         -	"f/16"
           44  +	"f/16",
           45  +	NULL
    43     46   };
    44     47   
    45     48   ITEM **iso_items;
    46     49   ITEM **shutter_items;
    47     50   ITEM **aperture_items;
    48     51   MENU *iso_menu;
    49     52   MENU *shutter_menu;
................................................................................
    54     57   
    55     58   void selection(char *name);
    56     59   void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
    57     60   int exposure(int iso);
    58     61   int selection_counter;
    59     62   double shutter(int exposure, double aperture);
    60     63   double aperture(int exposure, int shutter);
           64  +int nearest_match(double x, int menu);
           65  +double fraction_to_double(char *fraction);
    61     66   
    62     67   char iso_sel[5] = "";
    63     68   char shutter_sel[7] = "";
    64     69   char aperture_sel[6] = "";
    65     70   int menu_counter = 1;
    66     71   
    67     72   int main() {
................................................................................
   226    231   				}
   227    232   				if (menu_counter != menu_sel_last)
   228    233   					 selection_counter += 1;
   229    234   				if (selection_counter == 2) { 
   230    235   					/* calculate the other menu */
   231    236   					/* how to get missing menu? */
   232    237   					if (strcmp("", iso_sel) == 0) {
          238  +						/* Test searching for item in menu */
   233    239   						set_menu_pattern(iso_menu, "200");
   234         -
   235         -						/* Test setting item in menu
   236         -							set_top_row(iso_menu, 3);
   237         -						/* This works, but plain refreshing doesn't work properly */
   238         -						/* But cheating and using menu_driver to go up/down does */
          240  +						/* Using menu_driver to go up/down to force refresh */
   239    241   						menu_driver(iso_menu, REQ_DOWN_ITEM);
   240    242   						menu_driver(iso_menu, REQ_UP_ITEM);
   241    243   						wrefresh(iso_win);
   242    244   					}
   243    245   					if (strcmp("", shutter_sel) == 0) {
          246  +						/* Test finding nearest matching value in array? */
          247  +						char aperture_sel_[4] = "";
          248  +						strncpy(aperture_sel_, aperture_sel+2, 3);
          249  +						/* There is probably a nicer way to format the below */
          250  +						set_menu_pattern(
          251  +							shutter_menu,
          252  +							shutter_array[nearest_match(
          253  +								shutter(aperture_sel_, exposure(iso_sel)),
          254  +								2
          255  +							)]
          256  +						);
   244    257   						set_top_row(shutter_menu, 3);
   245    258   						menu_driver(shutter_menu, REQ_DOWN_ITEM);
   246    259   						menu_driver(shutter_menu, REQ_UP_ITEM);
   247    260   						wrefresh(shutter_win);
   248    261   					}
   249    262   					if (strcmp("", aperture_sel) == 0) {
          263  +						/* Tests setting to row number */
   250    264   						set_top_row(aperture_menu, 3);
   251    265   						menu_driver(aperture_menu, REQ_DOWN_ITEM);
   252    266   						menu_driver(aperture_menu, REQ_UP_ITEM);
   253    267   						wrefresh(aperture_win);
   254    268   					}
   255    269   					/* clear the selections for next time */
   256    270   					selection_counter = 0;
................................................................................
   326    340   	return pow(aperture, 2) / pow(2, exposure);
   327    341   }
   328    342   
   329    343   double aperture (int exposure, int shutter) {
   330    344   	/* EV = log2 (N^2/t) */
   331    345   	return sqrt(pow(2, exposure) * shutter);
   332    346   }
          347  +
          348  +int nearest_match (double x, int menu) {
          349  +	/* Need to search array for closest match */
          350  +	int n;
          351  +	int diff_idx = 0;
          352  +	char array_value_str[7];
          353  +	double array_value_db;
          354  +	double diff;
          355  +
          356  +	/* Need a starting value for difference */
          357  +	switch(menu) {
          358  +		case 1:
          359  +			array_value_db = strtod(iso_array[0], NULL);
          360  +			break;
          361  +		case 2:
          362  +			array_value_db = fraction_to_double(shutter_array[0]);
          363  +			break;
          364  +		case 3:
          365  +			strncpy(array_value_str, aperture_array[0]+2, 3);
          366  +			array_value_db = strtod(array_value_str, NULL);
          367  +			break;
          368  +	}
          369  +	diff = abs(array_value_db - x);
          370  +	/* lots of repetition here but pointers to arrays seem to be a bad thing */
          371  +	switch(menu) {
          372  +		case 1:
          373  +			for ( n = 1; iso_array[n] != NULL; ++n ) {
          374  +				array_value_db = strtod(iso_array[n], NULL);
          375  +				if (abs(array_value_db - x) < diff) { 
          376  +					diff_idx = n;
          377  +					diff = abs(array_value_db - x);
          378  +				}
          379  +			}
          380  +			break;
          381  +		case 2:
          382  +			for ( n = 1; iso_array[n] != NULL; ++n ) {
          383  +				array_value_db = fraction_to_double(shutter_array[n]);
          384  +				if (abs(array_value_db - x) < diff) {
          385  +					diff_idx = n;
          386  +					diff = abs(array_value_db - x);
          387  +				}
          388  +			}
          389  +			break;
          390  +		case 3:
          391  +			for ( n = 1; iso_array[n] != NULL; ++n ) {
          392  +				strncpy(array_value_str, aperture_array[0]+2, 3);
          393  +				array_value_db = strtod(array_value_str, NULL);
          394  +				if (abs(array_value_db - x) < diff) { 
          395  +					diff_idx = n;
          396  +					diff = abs(array_value_db - x);
          397  +				}
          398  +			}
          399  +			break;
          400  +	}
          401  +	return diff_idx;
          402  +}
          403  +
          404  +double fraction_to_double(char *fraction) {
          405  +	double fraction_as_db;
          406  +	char denominator[5];
          407  +	char *ptr = strchr(fraction, "/");
          408  +
          409  +	if (ptr) {
          410  +		/*then split*/
          411  +		strncpy(denominator, fraction+2, 5);
          412  +		fraction_as_db = 1/strtod(denominator, NULL);
          413  +	}
          414  +	else {
          415  +		fraction_as_db = strtod(fraction, NULL);
          416  +	}
          417  +return fraction_as_db;
          418  +}