28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
-
-
+
-
+
+
-
+
-
+
+
+
-
+
|
"10",
"11",
"12",
"13",
"14",
"15",
"16",
NULL
};
char *iso_array[] = {
"50",
"100",
"200",
"400",
"800",
"1600",
"3200",
NULL
};
char *shutter_array[] = {
"OVER",
"1/1000",
"1/500",
"1/250",
"1/125",
"1/60",
"1/30",
"1/15",
"1/8",
"1/4",
"1/2",
"1",
NULL
"UNDER",
};
char *aperture_array[] = {
"UNDER",
"f/1.4",
"f/2",
"f/2.8",
"f/4",
"f/5.6",
"f/8",
"f/11",
"f/16",
NULL
"OVER",
};
ITEM **exposure_items;
ITEM **iso_items;
ITEM **shutter_items;
ITEM **aperture_items;
MENU *exposure_menu;
MENU *iso_menu;
MENU *shutter_menu;
MENU *aperture_menu;
WINDOW *exposure_win;
WINDOW *iso_win;
WINDOW *shutter_win;
WINDOW *aperture_win;
void selection(char *name);
void add_menu(char **array, ITEM **items, MENU *men, WINDOW *win, int n, int xpos, char *title);
void remove_menu(ITEM **items, MENU *men, int n);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
MENU *add_menu(ITEM **items);
WINDOW *add_window(int xpos, char *title);
ITEM **add_item(char **array, int n);
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] = "";
char aperture_sel[9] = "";
int selection_counter = 1;
|
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
/* Create items */
/* Can't really avoid finding array sizes here */
n_exposure = ARRAY_SIZE(exposure_array);
n_iso = ARRAY_SIZE(iso_array);
n_shutter = ARRAY_SIZE(shutter_array);
n_aperture = ARRAY_SIZE(aperture_array);
/* http://stackoverflow.com/a/2620158/208793 */
exposure_items = add_item(exposure_array, n_exposure);
iso_items = add_item(iso_array, n_iso);
aperture_items = add_item(aperture_array, n_aperture);
shutter_items = add_item(shutter_array, n_shutter);
exposure_menu = add_menu(exposure_items);
iso_menu = add_menu(iso_items);
shutter_menu = add_menu(shutter_items);
aperture_menu = add_menu(aperture_items);
exposure_win = add_window(4, "EV");
iso_win = add_window(45, "ISO");
shutter_win = add_window(86, "Shutter");
aperture_win = add_window(127, "Aperture");
/* Don't know how to avoid the repition below */
add_menu(exposure_array, exposure_items, exposure_menu, exposure_win, n_exposure, 4, "EV");
add_menu(iso_array, iso_items, iso_menu, iso_win, n_iso, 45, "ISO");
add_menu(shutter_array, shutter_items, shutter_menu, shutter_win, n_shutter, 86, "Shutter");
add_menu(aperture_array, aperture_items, aperture_menu, aperture_win, n_aperture, 127, "Aperture");
set_menu_win(exposure_menu, exposure_win);
set_menu_win(iso_menu, iso_win);
set_menu_win(shutter_menu, shutter_win);
set_menu_win(aperture_menu, aperture_win);
set_menu_sub(exposure_menu, derwin(exposure_win, 6, 38, 3, 1));
set_menu_sub(iso_menu, derwin(iso_win, 6, 38, 3, 1));
set_menu_sub(shutter_menu, derwin(shutter_win, 6, 38, 3, 1));
set_menu_sub(aperture_menu, derwin(aperture_win, 6, 38, 3, 1));
post_menu(exposure_menu);
post_menu(iso_menu);
post_menu(shutter_menu);
post_menu(aperture_menu);
wrefresh(exposure_win);
wrefresh(iso_win);
wrefresh(shutter_win);
wrefresh(aperture_win);
attron(COLOR_PAIR(2));
mvprintw(LINES - 2, 0, "Select EV");
/*mvprintw(LINES - 2, 0, "Select ISO and then one of Shutter/Aperture to calculate other of Shutter/Aperture");*/
mvprintw(LINES - 1, 0, "Arrow keys to navigate, Enter to select, Q to exit");
attroff(COLOR_PAIR(2));
refresh();
|
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
+
-
+
-
-
+
+
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
|
void (*p)(char *);
cur = current_item(*menu);
p = item_userptr(cur);
/* Learning notes - Don't understand this bit */
/* Is this a function pointer? */
p((char *)item_name(cur));
switch (selection_counter) {
case 1: { /* Exposure selected */
selection_counter += 1;
menu_counter += 1;
move(LINES - 2, 0);
clrtoeol();
mvprintw(LINES - 2, 0, "Select ISO");
refresh();
menu = &iso_menu;
win = &iso_win;
}
break;
case 2: { /* ISO Selected */
selection_counter += 1;
menu_counter += 1;
move(LINES - 2, 0);
clrtoeol();
mvprintw(LINES - 2, 0, "Select Shutter or Aperture");
refresh();
menu = &shutter_menu;
win = &shutter_win;
}
break;
case 3: { /* Shutter or Aperture selected */
if (strcmp("", shutter_sel) == 0) {
char aperture_sel_[4] = "";
strncpy(aperture_sel_, aperture_sel+2, 3);
/* Using menu_driver to go up/down to force refresh and correct highlighting */
menu_driver(shutter_menu, REQ_SCR_UPAGE);
menu_driver(shutter_menu, REQ_SCR_DPAGE);
/* 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)),
/* need to igore over/under if selected, probably easier than try to prevent selection */
if (!((strcmp("OVER", shutter_sel) == 0)
|| (strcmp("UNDER", shutter_sel) == 0)
|| (strcmp("OVER", aperture_sel) == 0)
|| (strcmp("UNDER", aperture_sel) == 0))) {
switch (selection_counter) {
case 1: { /* Exposure selected */
selection_counter += 1;
menu_counter += 1;
move(LINES - 2, 0);
clrtoeol();
mvprintw(LINES - 2, 0, "Select ISO");
refresh();
menu = &iso_menu;
win = &iso_win;
}
break;
case 2: { /* ISO Selected */
selection_counter += 1;
menu_counter += 1;
move(LINES - 2, 0);
clrtoeol();
mvprintw(LINES - 2, 0, "Select Shutter or Aperture");
refresh();
menu = &shutter_menu;
win = &shutter_win;
}
break;
case 3: { /* Shutter or Aperture selected */
if (strcmp("", shutter_sel) == 0) {
char aperture_sel_[4] = "";
strncpy(aperture_sel_, aperture_sel+2, 3);
/* Using menu_driver to go up/down to force refresh and correct highlighting */
menu_driver(shutter_menu, REQ_SCR_UPAGE);
menu_driver(shutter_menu, REQ_SCR_DPAGE);
/* 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)),
2
)]
);
menu_driver(shutter_menu, REQ_DOWN_ITEM);
menu_driver(shutter_menu, REQ_UP_ITEM);
wrefresh(shutter_win);
}
if (strcmp("", aperture_sel) == 0) {
menu_driver(aperture_menu, REQ_SCR_UPAGE);
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)),
3,
n_shutter
)]
);
menu_driver(shutter_menu, REQ_DOWN_ITEM);
menu_driver(shutter_menu, REQ_UP_ITEM);
wrefresh(shutter_win);
}
if (strcmp("", aperture_sel) == 0) {
menu_driver(aperture_menu, REQ_SCR_UPAGE);
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)),
3
)]
);
menu_driver(aperture_menu, REQ_DOWN_ITEM);
menu_driver(aperture_menu, REQ_UP_ITEM);
wrefresh(aperture_win);
}
/* clear the selections for next time */
strcpy(iso_sel, "");
strcpy(shutter_sel, "");
strcpy(aperture_sel, "");
/* And set defaults back to start */
selection_counter = 1;
menu_counter = 1;
menu = &exposure_menu;
win = &exposure_win;
move(LINES - 2, 0);
clrtoeol();
mvprintw(LINES - 2, 0, "Select EV");
refresh();
}
break;
}
}
4,
n_aperture
)]
);
menu_driver(aperture_menu, REQ_DOWN_ITEM);
menu_driver(aperture_menu, REQ_UP_ITEM);
wrefresh(aperture_win);
}
/* clear the selections for next time */
strcpy(iso_sel, "");
strcpy(shutter_sel, "");
strcpy(aperture_sel, "");
/* And set defaults back to start */
selection_counter = 1;
menu_counter = 1;
menu = &exposure_menu;
win = &exposure_win;
move(LINES - 2, 0);
clrtoeol();
mvprintw(LINES - 2, 0, "Select EV");
refresh();
}
break;
}
}
/* If over/under need to clear selection so know which is blank
* when a proper selection is made */
if ((strcmp("OVER", shutter_sel) == 0) || (strcmp("UNDER", shutter_sel) == 0)) {
strcpy(shutter_sel, "");
}
if ((strcmp("OVER", aperture_sel) == 0) || (strcmp("UNDER", aperture_sel) == 0)) {
strcpy(aperture_sel, "");
}
}
break;
}
wrefresh(win);
wrefresh(*win);
}
/* Unpost and free all the memory taken up */
remove_menu(exposure_items, exposure_menu, n_exposure);
remove_menu(iso_items, iso_menu, n_iso);
remove_menu(shutter_items, shutter_menu, n_shutter);
remove_menu(aperture_items, aperture_menu, n_aperture);
endwin();
}
void add_menu(char **array, ITEM **items, MENU *men, WINDOW *win, int n, int xpos, char *title) {
ITEM **add_item(char **array, int n) {
int i;
ITEM **local_items;
items = (ITEM **)calloc(n, sizeof(ITEM *));
local_items = (ITEM **)calloc(n, sizeof(ITEM *));
for(i = 0; i<n; ++i) {
items[i] = new_item(array[i], array[i]);
set_item_userptr(items[i], selection);
local_items[i] = new_item(array[i], array[i]);
set_item_userptr(local_items[i], selection);
}
men = new_menu((ITEM **)items);
win = newwin(10, 40, 4, xpos);
return local_items;
}
keypad(win, TRUE);
set_menu_win(men, win);
set_menu_sub(men, derwin(win, 6, 38, 3, 1));
set_menu_format(men, 5, 1);
set_menu_mark(men, " * ");
box(win, 0, 0);
print_in_middle(win, 1, 0, 40, title, COLOR_PAIR(1));
mvwaddch(win, 2, 0, ACS_LTEE);
mvwhline(win, 2, 1, ACS_HLINE, 38);
mvwaddch(win, 2, 39, ACS_RTEE);
MENU *add_menu(ITEM **items) {
MENU *local_menu;
local_menu = new_menu((ITEM **)items);
set_menu_format(local_menu, 5, 1);
set_menu_mark(local_menu, " * ");
return local_menu;
}
WINDOW *add_window(int xpos, char *title) {
WINDOW *local_win;
local_win = newwin(10, 40, 4, xpos);
keypad(local_win, TRUE);
box(local_win, 0, 0);
print_in_middle(local_win, 1, 0, 40, title, COLOR_PAIR(1));
mvwaddch(local_win, 2, 0, ACS_LTEE);
mvwhline(local_win, 2, 1, ACS_HLINE, 38);
mvwaddch(local_win, 2, 39, ACS_RTEE);
post_menu(men);
wrefresh(win);
return local_win;
}
void remove_menu(ITEM **items, MENU *men, int n) {
int i;
unpost_menu(men);
free_menu(men);
|
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
-
+
-
+
-
+
-
-
-
-
+
-
-
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
}
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 1:
case 3:
array_value_db = strtod(iso_array[0], NULL);
break;
case 2:
array_value_db = fraction_to_double(shutter_array[0]);
array_value_db = fraction_to_double(shutter_array[1]);
break;
case 3:
strncpy(array_value_str, aperture_array[0]+2, 4);
case 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 1:
for ( n = 1; iso_array[n] != NULL; ++n ) {
case 3:
for ( n = 2; n < n_array-1; ++n ) {
array_value_db = strtod(iso_array[n], NULL);
if (fabs(array_value_db - x) < diff) {
diff_idx = n;
diff = fabs(array_value_db - x);
}
}
break;
case 2:
for ( n = 1; shutter_array[n] != NULL; ++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-2) {
if (diff >= fraction_to_double(shutter_array[n_array-2])*2) {
diff_idx = n_array-1;
}
}
break;
case 3:
for ( n = 1; aperture_array[n] != NULL; ++n ) {
case 4:
for ( n = 2; n < n_array-1; ++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-2) {
strncpy(array_value_str, aperture_array[n_array-2]+2, 4);
array_value_db = strtod(array_value_str, NULL);
if (diff >= array_value_db*sqrt(2.0)) {
diff_idx = n_array-1;
}
}
break;
}
return diff_idx;
}
double fraction_to_double(char *fraction) {
double fraction_as_db;
|