-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
1074 lines (1003 loc) · 36.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
170
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
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
345
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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2018 Anton Vakulenko
// e-mail: anton.vakulenko@gmail.com
//
// This file is part of TinyShooter.
//
// TinyShooter is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TinyShooter is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TinyShooter. If not, see <http://www.gnu.org/licenses/>.
#include <exception>
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <winsock.h>
#include <commctrl.h>
#include <stdio.h>
#include <ctime>
#include <shlobj.h> // for choose folder dialog
#include "resource.h"
#include "EDSDK.h"
#include "EDSDKErrors.h"
#include "EDSDKTypes.h"
using namespace std;
// application name
const char AppName [16] = "TinyShooter 1.0";
// variables for settings
int exposure = 300; // default single frame exposure, seconds
int repeat = 1; // default number of frames to shoot
int wait = 0; // default pause before first frame, MINUTES!
int comport = 1; // default serial port number
int mirror = 5; // default time for mirror to settle (0 is not allowed)
int pause = 30; // default pause between frames, seconds
int dither_distance = 3; // default dither distance for PHD (see comments in Dither() function)
int eos = 0; // we don't want to save images to PC by default
bool dither_bool = false; // we don't want to dither by default
char download_to[256] = "C:"; // default path to download images from camera
// varialbles for timer procedure
int TimeElapsed = 0; // number of seconds, passed for each status (should reset to 0 at the end of each shooting stage)
int TimeLeft; // time left till the end of shooting session (initially it equals total session's time) See #define TIMELEFT
int Frame = 0; // number of frames taken (should reset to 0 at the end of each shooting session)
string status = "Mirror"; // status (should reset to "Mirror" at the end of each shooting session)
// misc handles
HANDLE Port; // Serial port
SOCKET phd; // Socket handle
HINSTANCE hInst; // for winapi
// Calculates total session time in seconds
#define TIMELEFT (exposure + mirror + pause)*repeat - pause + wait*60
EdsError EDSCALLBACK handleObjectEvent( EdsObjectEvent event, EdsBaseRef object, EdsVoid * context)
{
// this function downloads images from camera to PC
// getting file info to download
EdsDirectoryItemInfo dirItemInfo;
EdsGetDirectoryItemInfo(object, &dirItemInfo);
// create file stream to download image
// image filename would be IMG_yyyy-MM-dd_HHmmss.CR2
// otherwise images from new session will overwrite old ones
EdsStreamRef stream = NULL;
// get and format current time
time_t rawtime;
struct tm * timeinfo;
char buffer[25] = ""; // will contain filename only
char full_path[256] = ""; // will contain full path (with filename)
time (&rawtime);
timeinfo = localtime(&rawtime); // current time in unreadable format
// format filename
strftime(buffer,sizeof(buffer),"\\IMG_%Y%m%d_%H%M%S.CR2",timeinfo);
// append download path
strcat(full_path,download_to);
// append filename
strcat(full_path,buffer);
EdsCreateFileStream(full_path, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
// download image
EdsDownload(object, dirItemInfo.size, stream);
// done!
EdsDownloadComplete(object);
// free resourses
EdsRelease(stream);
EdsRelease(object);
return EDS_ERR_OK;
}
EdsError CameraConnect()
{
// function to connect to camera
// Canon EOS only supported! And only ONE camera. Needs EDSDK.dll also!
// if everything is OK, returns EDS_ERR_OK
EdsError err = EDS_ERR_OK;
EdsCameraListRef cameraList = NULL;
EdsCameraRef camera = NULL;
// checking if we have EDSDK.dll
ifstream dll("EDSDK.dll");
if (dll.fail())
{
// dll missing!
MessageBox(NULL,"EDSDK.dll not found. Place it in application's folder.",AppName,MB_ICONWARNING);
err = EDS_ERR_MISSING_SUBCOMPONENT;
exit;
}
else
{
//dll in place, let's connect to camera
dll.close();
// Initialize SDK
EdsInitializeSDK();
// get list of connected cameras
EdsGetCameraList(&cameraList);
// get first connected camera
EdsGetChildAtIndex(cameraList, 0, &camera);
// start session with selected camera
err = EdsOpenSession(camera);
// save images to pc
EdsUInt32 intSaveTo = kEdsSaveTo_Host;
EdsSetPropertyData(camera, kEdsPropID_SaveTo, 0, sizeof(intSaveTo), &intSaveTo);
// tell camera that we have a plenty of free space on hdd
EdsCapacity capacity;
capacity.numberOfFreeClusters = 0x7FFFFFFF;
capacity.bytesPerSector = 0x1000;
capacity.reset = 1;
EdsSetCapacity(camera, capacity);
// set event handler to download images from camera's memory to pc
EdsSetObjectEventHandler(camera, kEdsObjectEvent_DirItemRequestTransfer, handleObjectEvent, NULL);
// checking if we got any errors
if (err != EDS_ERR_OK)
{
MessageBox(NULL,"Can't connect to camera",AppName,MB_ICONWARNING);
}
return err;
}
}
bool PHD(const string& s)
{
// connect and disconnect to/from PHD
// check firewall setting in case of error
if (s == "Connect")
{
//Start up Winsock v.2
WSADATA wsadata;
WSAStartup(0x0202, &wsadata);
//Fill out the information needed to initialize a socket
SOCKADDR_IN target; //Socket address information
target.sin_family = AF_INET; // address family Internet
target.sin_port = htons (4300); //Port to connect on
target.sin_addr.s_addr = inet_addr ("127.0.0.1"); //Target IP
//Create socket
phd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
//Try connecting...
if (connect(phd, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
MessageBox(GetActiveWindow(),"Can't connect to PHD",AppName,MB_ICONWARNING);
return false; //Couldn't connect
}
else
{
return true; //Success
}
}
if (s == "Disconnect")
{
//Close the socket if it exists
if (phd)
closesocket(phd);
//Clean up Winsock
WSACleanup();
return true;
}
}
void Dither ()
{
// say dither to PHD. more details here:
// https://github.com/OpenPHDGuiding/phd2/wiki/SocketServerInterface
//MSG_MOVE1 3 Dither a random amount, up to +/- 0.5 x dither_scale
//MSG_MOVE2 4 Dither a random amount, up to +/- 1.0 x dither_scale
//MSG_MOVE3 5 Dither a random amount, up to +/- 2.0 x dither_scale
//MSG_MOVE4 12 Dither a random amount, up to +/- 3.0 x dither_scale
//MSG_MOVE5 13 Dither a random amount, up to +/- 5.0 x dither_scale
char cmd; // contains command
// select appropriate PHD code to command
switch (dither_distance)
{
case 0:
{
cmd = 3;
break;
}
case 1:
{
cmd = 4;
break;
}
case 2:
{
cmd = 5;
break;
}
case 3:
{
cmd = 12;
break;
}
case 5:
{
cmd = 13;
break;
}
}
send(phd,&cmd,strlen(&cmd),0); // send command to PHD
}
bool ComPort (const string& s)
{
// open-close serial port routines
if (s == "Open")
{
//open serial port
char com [13];
sprintf(com,"\\\\.\\COM%d",comport);
Port = CreateFile(com, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (Port == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Can't open serial port. Check settings.",AppName,MB_ICONWARNING);
// no serial port
return false;
}
else
{
// serial port opened successfully, we can continue
DCB dcb;
FillMemory(&dcb, sizeof(dcb), 0);
// get current DCB
GetCommState(Port, &dcb);
// Update DCB
dcb.BaudRate = CBR_9600 ; // unsure is dcb.BaudRate is nessessary?
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
// Set new state
SetCommState(Port, &dcb);
return true;
}
}
if (s == "Close")
{
//close serial port
CloseHandle(Port);
return true;
}
}
void EmptyCheck (HWND hwnd)
{
// check if all fields in settings window are filled and not equal to 0
// if not disables Save button
// obviously not a perfect solution, but it works ok
BOOL bSuccess1, bSuccess2, bSuccess3;
GetDlgItemInt(hwnd,ID_COM,&bSuccess1,false);
if ( GetDlgItemInt(hwnd,ID_MIRROR,&bSuccess2,false) == 0)
{
SetDlgItemInt(hwnd,ID_MIRROR,1,false);
}
if ( GetDlgItemInt(hwnd,ID_PAUSE,&bSuccess3,false) == 0)
{
SetDlgItemInt(hwnd,ID_PAUSE,1,false);
}
if (bSuccess1 & bSuccess2 & bSuccess3)
{
EnableWindow( GetDlgItem( hwnd, IDOK ), TRUE);
}
else
{
EnableWindow( GetDlgItem( hwnd, IDOK ), FALSE);
}
}
string FormatTime (int seconds)
{
// prepare text for timing information
char ft[55] = "";
int te, h1, h2, m1, m2, s1, s2;
te = exposure * repeat; // total exposure
h1 = te / 3600;
m1 = (te % 3600) / 60;
s1 = (te % 3600) % 60;
h2 = seconds / 3600;
m2 = (seconds % 3600) / 60;
s2 = (seconds % 3600) % 60;
sprintf(ft,"Total exposure: %02d:%02d:%02d Time left: %02d:%02d:%02d", h1, m1, s1, h2, m2, s2);
string str(ft);
return str;
}
void Statistics(const string& t)
{
// put text at the bottom of the main window
char line1 [55];
char line2 [39];
int te, h1, h2, m1, m2, s1, s2;
te = exposure * repeat; // total exposure (this is not a total session time!)
// convert seconds in human readable format
h1 = te / 3600;
m1 = (te % 3600) / 60;
s1 = (te % 3600) % 60;
h2 = TimeLeft / 3600;
m2 = (TimeLeft % 3600) / 60;
s2 = (TimeLeft % 3600) % 60;
sprintf(line1,"Total exposure: %02d:%02d:%02d Time left: %02d:%02d:%02d", h1, m1, s1, h2, m2, s2);
if (t == "Wait")
{
sprintf(line2, "Wait to start session... %d sec", TimeElapsed);
SendMessage(GetDlgItem(GetActiveWindow(),ID_PROGRESSBAR), PBM_STEPIT, 0, 0);
}
if (t == "Mirror")
{
sprintf(line2, "[Frame %d] Mirror settling... %d sec", Frame + 1, TimeElapsed);
SendMessage(GetDlgItem(GetActiveWindow(),ID_PROGRESSBAR), PBM_STEPIT, 0, 0);
}
if (t == "Capture")
{
sprintf(line2, "[Frame %d] Capturing... %d sec", Frame + 1, TimeElapsed);
SendMessage(GetDlgItem(GetActiveWindow(),ID_PROGRESSBAR), PBM_STEPIT, 0, 0);
}
if (t == "Pause")
{
sprintf(line2, "[Frame %d] Pause... %d sec", Frame + 1, TimeElapsed);
SendMessage(GetDlgItem(GetActiveWindow(),ID_PROGRESSBAR), PBM_STEPIT, 0, 0);
}
if (t == "Idle")
{
strcpy(line2, "Idle");
}
if (t == "Empty")
{
strcpy(line1, "Fill in all data!");
strcpy(line2, "Idle");
}
if (t == "Aborted")
{
strcpy(line2, "Session aborted");
SendMessage(GetDlgItem(GetActiveWindow(),ID_PROGRESSBAR), PBM_SETPOS, 0, 0);
}
if (t == "Complete")
{
strcpy(line2, "Session complete");
SendMessage(GetDlgItem(GetActiveWindow(),ID_PROGRESSBAR), PBM_SETPOS, 0, 0);
}
// update text lines
SetDlgItemText(GetActiveWindow(), ID_TIME, line1);
SetDlgItemText(GetActiveWindow(), ID_STATUS, line2);
}
void CleanUp()
{
// clean up things at the and of shooting session
// stop main timer
KillTimer (GetActiveWindow(), 1);
// drops all variables for main timer procedure to defaults
status = "Mirror";
TimeElapsed = 0;
Frame = 0;
// disconnect from PHD
PHD("Disconnect");
// close serial port
EscapeCommFunction(Port, CLRRTS);
EscapeCommFunction(Port, CLRDTR);
ComPort("Close");
// do cosmetics
SetDlgItemText(GetActiveWindow(),ID_START_BUTTON, "Start" );
EnableWindow( GetDlgItem( GetActiveWindow(), ID_EXP ), TRUE);
EnableWindow( GetDlgItem( GetActiveWindow(), ID_COUNT ), TRUE);
EnableWindow( GetDlgItem( GetActiveWindow(), ID_PHD ), TRUE);
EnableWindow( GetDlgItem( GetActiveWindow(), ID_WAIT ), TRUE);
EnableWindow( GetDlgItem( GetActiveWindow(), ID_SETTINGS_BUTTON ), TRUE);
}
void TimerProcKillESDK(HWND Arg1, UINT Arg2, UINT_PTR Arg3, DWORD Arg4)
{
// if we terminate edsdk just after last frame is done, we abort downloading of the last image
// so we need to wait for some time (5 sec should be enough I think)
// also not a perfect solution but it works ok
// 5 seconds left, lets kill edsdk
EdsTerminateSDK();
// stop timer
KillTimer(GetActiveWindow(),2);
}
void TimerProc(HWND Arg1, UINT Arg2, UINT_PTR Arg3, DWORD Arg4)
{
// main shooting procedure
// here we open and close shutter, dither and update statistics
// at first we need to wait till initial pause time (wait) is left
if (wait != 0)
{
TimeLeft -= 1;
TimeElapsed += 1;
// update statistics
Statistics("Wait");
// checking if time to wait before session is elapsed
if (TimeElapsed == wait*60)
{
// reset
wait = 0;
TimeElapsed = 0;
}
// we exit now from ticking cycle or we skip one second and get to next "if"
goto end_tick_cycle;
}
// now it's time to check our status
// it could be mirror, capture and pause
if (status == "Mirror")
{
if (TimeElapsed == 0)
{
// shutter button down
EscapeCommFunction(Port, SETRTS);
EscapeCommFunction(Port, SETDTR);
// wait a bit
Sleep(50);
// shutter button up
EscapeCommFunction(Port, CLRRTS);
EscapeCommFunction(Port, CLRDTR);
// now we just made mirror lock-up
}
TimeLeft -= 1;
TimeElapsed += 1;
// update statistics
Statistics("Mirror");
// checking if mirror settle time elapsed
if (TimeElapsed == mirror)
{
// change status
status = "Capture";
TimeElapsed = 0;
}
// we exit now from ticking cycle or we skip one second and get to next "if"
goto end_tick_cycle;
}
if (status == "Capture")
{
if (TimeElapsed == 0)
{
// shutter button down (we open shutter)
EscapeCommFunction(Port, SETRTS);
EscapeCommFunction(Port, SETDTR);
}
TimeLeft -= 1;
TimeElapsed += 1;
// update statistics
Statistics("Capture");
// checking if exposure time elapsed
if (TimeElapsed == exposure)
{
// change status
status = "Pause";
TimeElapsed = 0;
}
// we exit now from ticking cycle or we skip one second and get to next "if"
goto end_tick_cycle;
}
if (status == "Pause")
{
if (TimeElapsed == 0)
{
// shutter button up (close shutter, mirror down)
EscapeCommFunction(Port, CLRRTS);
EscapeCommFunction(Port, CLRDTR);
// this is the end of the single frame shooting
// now we say "dither" to PHD (if needed)
if ((IsDlgButtonChecked(GetActiveWindow(),ID_PHD) == BST_CHECKED) && (Frame + 1 != repeat)) // we don't dither after last frame!
{
Dither();
}
}
if (Frame + 1 == repeat)
{
// all frames are done
Frame = 0;
// cosmetics
TimeLeft = TIMELEFT;
Statistics("Complete");
// call this to stop our shooting session
CleanUp();
// wait 5 seconds to download last image from camera to pc
// more detail in TimerProcKillEDSDK() function
SetTimer(GetActiveWindow(), 2, 5000, (TIMERPROC) TimerProcKillESDK);
}
else
{
// not all frames are done
TimeLeft -= 1;
TimeElapsed += 1;
// cosmetics
Statistics("Pause");
}
// checking if pause time elapsed
if (TimeElapsed == pause)
{
TimeElapsed = 0;
// change status
status = "Mirror";
Frame += 1;
}
}
// we get here when status changes
end_tick_cycle:
;
}
string BrowseFolder()
{
// show browse folder dialog and get selected path
BROWSEINFO bi = { 0 };
bi.hwndOwner = GetActiveWindow();
bi.lpszTitle = ("Choose folder to download images from camera:");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
//get the name of the folder and put it in path
TCHAR path[MAX_PATH];
SHGetPathFromIDList ( pidl, path );
//free memory used
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
return path;
}
return "";
}
void SaveSettings()
{
// save settings to ts.cfg - configuration file
// note, we don't save values from EDITBOXES, RADIOBUTTONS and so on. We save values of the VARIABLES!
// so one should always update variable's values before run this function
// ts.cfg has fixed format:
// 1st line: "don't edit..."
// 2nd line: comport
// 3nd line: mirror
// 4th line: pause
// 5th line: dither_distance
// 6th line: eos
// 7th line: download_to
ofstream config_file ("ts.cfg", ios::out | ios::trunc);
config_file << "DO NOT EDIT THIS FILE! USE SETTINGS BUTTON IN APPLICATION" << endl;
config_file << comport << endl;
config_file << mirror << endl;
config_file << pause << endl;
config_file << dither_distance << endl;
config_file << eos << endl;
config_file << download_to << endl;
config_file.close();
}
void LoadSettings()
{
// check ts.cfg and read from it
ifstream config_file("ts.cfg");
try
{
string s;
getline(config_file,s);
getline(config_file,s);
comport = stoi(s);
getline(config_file,s);
mirror = stoi(s);
getline(config_file,s);
pause = stoi(s);
getline(config_file,s);
dither_distance = stoi(s);
getline(config_file,s);
eos = stoi(s);
// in case of error with stoi(), exception will be thrown automatically
// but with the next line we have to throw it manually:
if (getline(config_file,s))
{
strcpy (download_to,s.c_str());
}
else
{
throw(1);
}
config_file.close();
}
catch (...)
// we don't find ts.cfg or it has errors
{
MessageBox(GetActiveWindow(),"Configuration file is missing or corrupted. Defaults will be used. Check settings.",AppName,MB_ICONWARNING);
// the next line creates new ts.cfg with default settings
SaveSettings();
}
}
BOOL CALLBACK DlgSettings(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
// here all messages from settings window are processed
switch (message)
{
case WM_INITDIALOG:
{
// set icon
HICON hIcon;
hIcon = (HICON)LoadImageW(GetModuleHandleW(NULL),MAKEINTRESOURCEW(IDI_ICON1),IMAGE_ICON,GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),0);
if (hIcon)
{
SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
}
// load settings from ts.cfg
LoadSettings();
// set values to dialog from ts.cfg
SetDlgItemInt(hwndDlg,ID_COM,comport,false);
SetDlgItemInt(hwndDlg,ID_MIRROR,mirror,false);
SetDlgItemInt(hwndDlg,ID_PAUSE,pause,false);
SetDlgItemText(hwndDlg,ID_PATH,download_to);
switch (dither_distance) // details on values in Dither() function
{
case 0:
{
SendDlgItemMessage(hwndDlg,ID_05,BM_SETCHECK,BST_CHECKED,NULL);
break;
}
case 1:
{
SendDlgItemMessage(hwndDlg,ID_1,BM_SETCHECK,BST_CHECKED,NULL);
break;
}
case 2:
{
SendDlgItemMessage(hwndDlg,ID_2,BM_SETCHECK,BST_CHECKED,NULL);
break;
}
case 3:
{
SendDlgItemMessage(hwndDlg,ID_3,BM_SETCHECK,BST_CHECKED,NULL);
break;
}
case 5:
{
SendDlgItemMessage(hwndDlg,ID_5,BM_SETCHECK,BST_CHECKED,NULL);
break;
}
}
if (eos == 1)
{
CheckDlgButton(hwndDlg,ID_DOWNLOAD,BST_CHECKED);
}
else
{
CheckDlgButton(hwndDlg,ID_DOWNLOAD,BST_UNCHECKED);
}
}
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_PATH_BUTTON:
{
// choose path to download images here
strcpy (download_to,BrowseFolder().c_str());
SetDlgItemText(hwndDlg,ID_PATH,download_to);
break;
}
case IDOK:
{
// save entered values to variables
BOOL bSuccess;
comport = GetDlgItemInt(hwndDlg,ID_COM,&bSuccess,false);
mirror = GetDlgItemInt(hwndDlg,ID_MIRROR,&bSuccess,false);
pause = GetDlgItemInt(hwndDlg,ID_PAUSE,&bSuccess,false);
if (IsDlgButtonChecked(hwndDlg,ID_05) == BST_CHECKED)
{
dither_distance = 0;
}
if (IsDlgButtonChecked(hwndDlg,ID_1) == BST_CHECKED)
{
dither_distance = 1;
}
if (IsDlgButtonChecked(hwndDlg,ID_2) == BST_CHECKED)
{
dither_distance = 2;
}
if (IsDlgButtonChecked(hwndDlg,ID_3) == BST_CHECKED)
{
dither_distance = 3;
}
if (IsDlgButtonChecked(hwndDlg,ID_5) == BST_CHECKED)
{
dither_distance = 5;
}
if (IsDlgButtonChecked(hwndDlg,ID_DOWNLOAD) == BST_CHECKED)
{
eos = 1;
}
else
{
eos = 0;
}
GetDlgItemText(hwndDlg, ID_PATH, download_to, 256);
// write variables to ts.cfg
SaveSettings();
EndDialog(hwndDlg, wParam);
break;
}
case ID_COM:
{
if (HIWORD(wParam) == EN_CHANGE)
// checking if field is not empty
{
EmptyCheck(hwndDlg);
}
break;
}
case ID_MIRROR:
{
if (HIWORD(wParam) == EN_CHANGE)
// checking if field is not empty
{
EmptyCheck(hwndDlg);
}
break;
}
case ID_PAUSE:
{
if (HIWORD(wParam) == EN_CHANGE)
// checking if field is not empty
{
EmptyCheck(hwndDlg);
}
break;
}
case ID_ABOUT:
{
char msg [290] = "";
strcat(msg,AppName);
strcat(msg,"\n\nAstrophoto utility to capture series of images with dithering support");
strcat(msg,"\n\nCopyright (C) 2018 Anton Vakulenko\nanton.vakulenko@gmail.com\nhttps://github.com/anton-vakulenko/tinyshooter");
strcat(msg,"\n\nThis program is licensed under the terms of the GNU General Public License version 3");
MessageBox(hwndDlg,msg,AppName,MB_ICONINFORMATION);
break;
}
case IDCANCEL:
EndDialog(hwndDlg, wParam);
return TRUE;
}
}
return FALSE;
}
BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// here we process messages from main window
switch(uMsg)
{
case WM_INITDIALOG:
{
// parse config
LoadSettings();
// set icon
HICON hIcon;
hIcon = (HICON)LoadImageW(GetModuleHandleW(NULL),MAKEINTRESOURCEW(IDI_ICON1),IMAGE_ICON,GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),0);
if (hIcon)
{
SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
}
// set title of main window
SetWindowText(hwndDlg,AppName);
// set default values to exporuse, frames and initial pause (wait)
SetDlgItemInt(hwndDlg,ID_EXP,exposure,false);
SetDlgItemInt(hwndDlg,ID_COUNT,repeat,false);
SetDlgItemInt(hwndDlg,ID_WAIT,wait,false);
// count session duration
TimeLeft = TIMELEFT;
}
return TRUE;
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
// open settings dialog
case ID_SETTINGS_BUTTON:
{
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_SETTINGS), hwndDlg, (DLGPROC)DlgSettings);
// recal total time (maybe we changed some values in settings window)
TimeLeft = TIMELEFT;
Statistics("Idle");
break;
}
// starts or stops shooting process
case ID_START_BUTTON:
{
char s[5];
GetDlgItemText(hwndDlg, ID_START_BUTTON, s, 6);
if ( strcmp(s, "Start") == 0 )
{
// we want to shoot
// do cosmetics
SetDlgItemText(hwndDlg,ID_START_BUTTON, "Stop" );
EnableWindow( GetDlgItem( hwndDlg, ID_EXP ), FALSE);
EnableWindow( GetDlgItem( hwndDlg, ID_COUNT ), FALSE);
EnableWindow( GetDlgItem( hwndDlg, ID_PHD ), FALSE);
EnableWindow( GetDlgItem( hwndDlg, ID_WAIT ), FALSE);
EnableWindow( GetDlgItem( hwndDlg, ID_SETTINGS_BUTTON ), FALSE);
// Set the range and increment of the progress bar (1 step = 1 second)
SendMessage(GetDlgItem(hwndDlg,ID_PROGRESSBAR), PBM_SETRANGE, 0, MAKELPARAM(0, TimeLeft));
SendMessage(GetDlgItem(hwndDlg,ID_PROGRESSBAR), PBM_SETSTEP, (WPARAM) 1, 0);
// try to connect to camera
if (eos == 1)
{
if (CameraConnect() != EDS_ERR_OK)
{
// no camera - no shooting
Statistics("Idle");
goto no_luck;
}
}
// try to connect to PHD
if (IsDlgButtonChecked(GetActiveWindow(),ID_PHD) == BST_CHECKED)
{
if (!PHD("Connect"))
{
// no PHD - no shooting
Statistics("Idle");
goto no_luck;
}
}
//open serial port
if (ComPort("Open"))
{
// everything is ok, we can start ticking every 1 second
SetTimer(hwndDlg, 1, 1000, (TIMERPROC) TimerProc);
}
else
{
// no port - no shooting
Statistics("Idle");
goto no_luck;
}
}
else
{
no_luck: // we go here if we can't open serial port or connect to camera of connect to phd
// also we are here if we press stop button
// we want to stop shooting
// close serial port
// we need to know, on which stage of shooting we are (or we can't close com port correctly)
if (status == "Mirror")
{
// shutter button down
EscapeCommFunction(Port, SETRTS);
EscapeCommFunction(Port, SETDTR);
// wait a bit
Sleep(100);
// shutter button up
EscapeCommFunction(Port, CLRRTS);
EscapeCommFunction(Port, CLRDTR);
}
if (status == "Capture")
{
// close shutter, mirror down
EscapeCommFunction(Port, CLRRTS);
EscapeCommFunction(Port, CLRDTR);
}
ComPort("Close"); // now serial port is closed correctly
// disconnect from PHD
PHD("Disconnect");
// disconect from edsdk
EdsTerminateSDK();
// stop ticking
KillTimer (GetActiveWindow(), 1);
// drops all variables for timer procedure to defaults
status = "Mirror";
TimeElapsed = 0;
Frame = 0;
// do cosmetics
TimeLeft = TIMELEFT;
Statistics("Aborted");
SetDlgItemText(hwndDlg,ID_START_BUTTON, "Start" );
EnableWindow( GetDlgItem( hwndDlg, ID_EXP ), TRUE);
EnableWindow( GetDlgItem( hwndDlg, ID_COUNT ), TRUE);
EnableWindow( GetDlgItem( hwndDlg, ID_PHD ), TRUE);
EnableWindow( GetDlgItem( hwndDlg, ID_WAIT ), TRUE);
EnableWindow( GetDlgItem( hwndDlg, ID_SETTINGS_BUTTON ), TRUE);
}
break;
}
// code below checks if all fields in main window are filled with correct values
// if not it disables buttons and updates statistics
case ID_EXP:
{
if (HIWORD(wParam) == EN_CHANGE)
// exposure changed, need to recalculate total time
{
BOOL bSuccess1, bSuccess2, bSuccess3;
// update exposure
exposure = GetDlgItemInt(hwndDlg,ID_EXP,&bSuccess1,false);
// if value is "0" changes it to "1"
if (exposure == 0)
{
SetDlgItemInt(hwndDlg,ID_EXP,1,false);
}
GetDlgItemInt(hwndDlg,ID_COUNT,&bSuccess2,false);
GetDlgItemInt(hwndDlg,ID_WAIT,&bSuccess3,false);
// count session duration
if (bSuccess1 && bSuccess2 && bSuccess3)
{
// we here because all fields are not empty
// recal total time
TimeLeft = TIMELEFT;
Statistics("Idle");
EnableWindow( GetDlgItem( hwndDlg, ID_START_BUTTON ), TRUE);
EnableWindow( GetDlgItem( hwndDlg, ID_SETTINGS_BUTTON ), TRUE);
}
else
{
// ups, at least one field is empty
Statistics("Empty");
EnableWindow( GetDlgItem( hwndDlg, ID_START_BUTTON ), FALSE);
EnableWindow( GetDlgItem( hwndDlg, ID_SETTINGS_BUTTON ), FALSE);
}
}
break;
}
case ID_COUNT:
{
if (HIWORD(wParam) == EN_CHANGE)
// number of frames changed, recalculate total time
{
BOOL bSuccess1 = true, bSuccess2 = true, bSuccess3 = true;
// update repeat
repeat = GetDlgItemInt(hwndDlg,ID_COUNT,&bSuccess1,false);
// if value is "0" changes it to "1"
if (repeat == 0)
{