-
Notifications
You must be signed in to change notification settings - Fork 46
/
LuaLoaderMain.pas
2715 lines (2416 loc) · 75.2 KB
/
LuaLoaderMain.pas
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
unit LuaLoaderMain;
// LuaLoader http://benlo.com/esp8266 https://github.com/GeoNomad/LuaLoader/
// LuaLoader uses the SerialNG component included in the package
// SerialNG is open source GNU licensed as of 2002
// The component needs to be installed before compiling this code.
//
// This is the Delphi 4 version. Changes may be required for porting to other versions of Delphi.
//
// LuaLoaderMain.pas is open source MIT License, but the SerialNG component has its separate license
//
// netstat.pas is not included, comment out lines containing net. or replace with your own net.geturl function
// geturl is not necessary for functionality - just for checking latest versions, etc.
//
// const ThisVersion is set below
// TODO - add tmr.stop on restart automatically, also resume higher baud rate
// 0.90 manually add a COM port that is not detected automatically + user OpenKeyReadOnly to get W10 HARDWARE key
// 0.90 change to ini files instead of registry
// 0.89 removed Hard Restart and Soft Restart messages which appeared out of sequence and confused
// 0.88 allow more baud rates, fix the reset to 9600
// 0.87 set default DTR and RTS on each connect
// 0.86 changed the default value of DTR and RTS to false = HI
// 0.86 updated latest firmware URL to github/releases
// 0.86 fixed upload all .lua files bug (uploading .lua.bak)
// 0.86 added right click to Read - set autoread repeat rate
// 0.85 fix bug in upload all aborting
// 0.85 added download binary
// 0.95 added compile button
// 0.85 added do(lc) button - executes lc version of selected .lua file
// 0.84 fixed bug in < Upload all .lua files (! and < were included)
// 0.83 check for new ports when opening dialog (no need to restart LL)
// 0.83 add Upload all .lua files
// 0.83 add Custom button to execute short lua file from PC
// 0.82 add Upload all changed files command to filename dropdown list
// 0.82 add ! to mark recently changed files in filename dropdown list
// 0.82 tell user if there is a new NodeMCU firmware version available
// 0.82 dofile('LLbin.lua') automatically after upload
// 0.82 right click DTR and RTS to set defaults at startup
// 0.81 add check mark on recently accessed documentation
// 0.81 automatically load LLbin.lua if missing
// 0.81 changed net.ShowURL to ShowURL in several places
// 0.80 Add 'Remove Current Workspace' to Workspace menu
// 0.80 Fixed bug in file list after remove
// 0.80 Added more error handling on failed binary transfers
// 0.79 binary upload option
// 0.79 run file.list if needed when looking at FilenameE
// 0.78 add filter options to file list drop down menu (right click)
// 0.77 Hide Restart Garbage default changed to off
// 0.77 removed dofile() at end of load
// 0.77 added wifi.setmode(wifi.STATIONAP) to [Survey]
// 0.76 added link to buy NodeMCU boards
// 0.75 added read ADC input to [Read] button and autorepeat
// 0.74 Added DTR and RTS output control
// 0.74 Added CTS input monitor
// 0.73 fix crash on illegal com port names
// 0.73 fix changed restart name (NodeMcu became NodeMCU) affecting Hide Garbage setting
// 0.73 added file.format() button [Format]
// 0.72 improve file.list formatting
// 0.72 add [remove all] button
// 0.72 add Help - Local Documentation - opens files or shortcuts in a local folder
// 0.71 reformatted [survey] button
// 0.71 updated link to latest firmware
// 0.70 fixed = node.heap() after upload
// 0.70 fixed version grab with newer format
// 0.69 Revised GPIO pin numbers
// 0.69 Added bit module to Help menu
// 0.68 fixed high comm port numbers to 255
// 0.68 fast uploads automatically uses 921600 if checked
// 0.68 color the baud rate if disconnected
// 0.68 refactored the Help menu
// 0.68 fixed check for Logging turned on in .Log
// 0.67 fixed bug in getting long file lists from the board
// 0.67 color scheme skin
// 0.66 added Workspaces to File menu - added by uploading a file
// 0.66 Try except around log file writing
// 0.65 fixed random characters in entry window
// 0.65 tmr.stop menu - right click to choose 0 to 7 timers
// 0.65 Changed parsing of api document revision date
// 0.64 [Load] file list populated with Working Directory contents (*.lua)
// 0.63 Send history in text edit
// 0.62 reconnect COM port automatically after bin reflash
// 0.61 log time stamps for Hard restart and soft restart
// 0.61 added Wifi status button
// 0.61 automatically revert to 9600 baud on connection errors (usually restart)
// 0.6 upload with no echo
// 0.6 set baud rate for communications
//
// 0.55 date stamp in log file each time [dofile] click
// Tools menu - download latest firmware, flasher
// Added 75000 to baud rate list - search for baudrate if problems...
// Allow non-standard baud rates as text entry
// 0.54 tmr.stop() - timers 0 and 1 on click and upload
//
// 0.53 View Log (file association must be set for .log)
// 0.53 Edit button (file association must be set for .lua)
// 0.52 added PULLUP support
// 0.51
// one line GPIO monitoring
// start with crlf to show prompt
// logging fixed
// 0.5 GPIO read
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,SerialNG,
StdCtrls, ExtCtrls, ComCtrls, ImgList, Buttons, URLLabel, ClipBrd, Registry,
Menus, ShellAPI, SuperTimer, Math, IniFiles;
type
TForm1 = class(TForm)
SerialPortNG1: TSerialPortNG;
OpenDialog1: TOpenDialog;
MainMenu1: TMainMenu;
File1: TMenuItem;
Help1: TMenuItem;
NodeMcuLua1: TMenuItem;
Lua1: TMenuItem;
LuaLoaderUpdates1: TMenuItem;
N2: TMenuItem;
Luainminutes1: TMenuItem;
BugReports1: TMenuItem;
BuyESP8266Boards1: TMenuItem;
Settings1: TMenuItem;
CommPortSettings1: TMenuItem;
SaveSettings1: TMenuItem;
LoadSavedSettings1: TMenuItem;
Panel1: TPanel;
Panel2: TPanel;
SSID: TEdit;
Password: TEdit;
SetAPBtn: TButton;
Panel3: TPanel;
FileListBtn: TButton;
FilenameE: TComboBox;
DoFileBtn: TButton;
RemoveBtn: TButton;
CatBtn: TButton;
UploadBtn: TButton;
UploadFileBtn: TButton;
SendFileBtn: TButton;
SendBtn: TButton;
PasteBtn: TButton;
Terminal: TMemo;
ClearBtn: TButton;
Panel4: TPanel;
NodeHeapBtn: TButton;
ChipIDBtn: TButton;
TmrStop: TButton;
StatusBar: TStatusBar;
APSurveyBtn: TButton;
GetIPBtn: TButton;
DisconnectBtn: TButton;
ConnectM: TMenuItem;
GroupBox1: TGroupBox;
GPIO: TComboBox;
SetMode: TButton;
ReadBtn: TButton;
HighBtn: TButton;
LowBtn: TButton;
LuaBooksM: TMenuItem;
LogTerminalM: TMenuItem;
SaveTerminalM: TMenuItem;
SaveDialog1: TSaveDialog;
N3: TMenuItem;
LuaReferenceManual1: TMenuItem;
N4: TMenuItem;
Connect1: TMenuItem;
ConnectedStatusTimer: TSuperTimer;
Autorepeat: TSpeedButton;
RepeatTimer: TTimer;
LuaUsersWikiiCodeSamples1: TMenuItem;
N5: TMenuItem;
HideRestartGarbageM: TMenuItem;
Mode1: TComboBox;
Mode2: TComboBox;
ViewLog1: TMenuItem;
N6: TMenuItem;
EditBtn: TSpeedButton;
Tools1: TMenuItem;
Downloadlatestfirmware1: TMenuItem;
DownloadESP8266Flasherexe1: TMenuItem;
Runesp8266flasherexe1: TMenuItem;
N8: TMenuItem;
Tutorialonflashing1: TMenuItem;
NurdSpaceLuaReference1: TMenuItem;
GPIOrateM: TMenuItem;
WifiStatusBtn: TSpeedButton;
ResetBaudTimer: TSuperTimer;
SendEdit: TComboBox;
AutoResetBaudRateM: TMenuItem;
tmrM: TPopupMenu;
tmrstop01: TMenuItem;
tmrstop11: TMenuItem;
tmrstop21: TMenuItem;
tmrstop31: TMenuItem;
tmrstop41: TMenuItem;
tmrstop51: TMenuItem;
tmrstop61: TMenuItem;
Checkall1: TMenuItem;
CheckNone1: TMenuItem;
N7: TMenuItem;
Workspace: TMenuItem;
c1: TMenuItem;
DownloadNodeMcuesp8266flasherexe1: TMenuItem;
FixFilename: TSuperTimer;
CommPanel: TGroupBox;
BaudRate: TComboBox;
FastUp: TMenuItem;
NodeMCUFirmware1: TMenuItem;
NodeMCULuaFirmwaregithub1: TMenuItem;
NodeMCUFlasher1: TMenuItem;
N9: TMenuItem;
ESP82661: TMenuItem;
EspressifSystems1: TMenuItem;
ESP8266WikiReference1: TMenuItem;
ESP8266Flasher1: TMenuItem;
ESP8266Forum1: TMenuItem;
LuaLanguage1: TMenuItem;
About1: TMenuItem;
LuaFAQ1: TMenuItem;
LuaGotchas1: TMenuItem;
Node1: TMenuItem;
FileModule1: TMenuItem;
WifiModule1: TMenuItem;
TimerModule1: TMenuItem;
GPIOModule1: TMenuItem;
PWMModule1: TMenuItem;
NetModule1: TMenuItem;
I2CModule1: TMenuItem;
ADCModule1: TMenuItem;
UARTModule1: TMenuItem;
OneWireModule1: TMenuItem;
esp8266ForumLuaLanguage1: TMenuItem;
N1: TMenuItem;
Quit1: TMenuItem;
BitModule: TMenuItem;
APIOverviewandChangeLog1: TMenuItem;
LocalM: TMenuItem;
LocalSM: TMenuItem;
RestartBtn: TSpeedButton;
FormatBtn: TButton;
DTRbtn: TSpeedButton;
RTSbtn: TSpeedButton;
CTSbtn: TSpeedButton;
BuyNodeMCUdevelopmentboards1: TMenuItem;
QuickStartforBeginners1: TMenuItem;
UploadMenu: TPopupMenu;
ESPLua: TMenuItem;
WSLua: TMenuItem;
N10: TMenuItem;
UseBin: TMenuItem;
BinM: TPopupMenu;
UploadBinary1: TMenuItem;
UploadText1: TMenuItem;
OpenMenu: TPopupMenu;
OpenWorkspaceFolderinExplorer1: TMenuItem;
DTRm: TPopupMenu;
Setcurrentvalueasstartupdefault1: TMenuItem;
Custom: TButton;
CustomPopup: TPopupMenu;
Resetfunctionality1: TMenuItem;
Compile: TButton;
DownLoadBtn: TButton;
DoFileCompiled: TButton;
DLDecoder: TSuperTimer;
ListlcM: TMenuItem;
ListallM: TMenuItem;
ReadRateM: TPopupMenu;
Setrepeatrateforread1: TMenuItem;
NodeMCUCustomBuilds1: TMenuItem;
procedure AdvSettingsBtnClick(Sender: TObject);
procedure SerialPortNG1RxClusterEvent(Sender: TObject);
procedure SerialPortNG1ProcessError(Sender: TObject; Place,Code: DWord; Msg: String);
procedure SendBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure SerialPortNG1RxCharEvent(Sender: TObject);
procedure SerialPortNG1TxQueueEmptyEvent(Sender: TObject);
procedure SaveSettingsBtnClick(Sender: TObject);
procedure SerialPortNG1CommStat(Sender: TObject);
procedure SerialPortNG1WriteDone(Sender: TObject);
procedure SendFileBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure GetIPBtnClick(Sender: TObject);
procedure SetAPBtnClick(Sender: TObject);
procedure UploadFileBtnClick(Sender: TObject);
procedure DoFileBtnClick(Sender: TObject);
procedure TmrStopClick(Sender: TObject);
procedure APSurveyBtnClick(Sender: TObject);
procedure NodeHeapBtnClick(Sender: TObject);
procedure RestartBtnClick(Sender: TObject);
procedure RemoveBtnClick(Sender: TObject);
procedure ChipIDBtnClick(Sender: TObject);
procedure FileListBtnClick(Sender: TObject);
procedure ClearBtnClick(Sender: TObject);
procedure TerminalKeyPress(Sender: TObject; var Key: Char);
procedure PasteBtnClick(Sender: TObject);
procedure CatBtnClick(Sender: TObject);
procedure LuaLoaderUpdates1Click(Sender: TObject);
procedure ShowURL( URL : string);
procedure NodeMcuLua1Click(Sender: TObject);
procedure ESP8266Forum1Click(Sender: TObject);
procedure Lua1Click(Sender: TObject);
procedure Espressif1Click(Sender: TObject);
procedure Luainminutes1Click(Sender: TObject);
procedure BugReports1Click(Sender: TObject);
procedure WikiReference1Click(Sender: TObject);
procedure BuyESP8266Boards1Click(Sender: TObject);
procedure UploadBtnClick(Sender: TObject);
procedure Settings1Click(Sender: TObject);
procedure ConnectMClick(Sender: TObject);
procedure SetModeClick(Sender: TObject);
procedure ReadBtnClick(Sender: TObject);
procedure LowBtnClick(Sender: TObject);
procedure HighBtnClick(Sender: TObject);
procedure DisconnectBtnClick(Sender: TObject);
procedure LuaBooksMClick(Sender: TObject);
procedure LogTerminalMClick(Sender: TObject);
procedure SaveTerminalMClick(Sender: TObject);
procedure NodeMCULuaFirmware1Click(Sender: TObject);
procedure LuaReferenceManual1Click(Sender: TObject);
procedure NodeMCUFlasher1Click(Sender: TObject);
procedure Connect1Click(Sender: TObject);
procedure ConnectedStatusTimerTimer(Sender: TObject);
procedure AutorepeatClick(Sender: TObject);
procedure RepeatTimerTimer(Sender: TObject);
procedure LuaUsersWikiiCodeSamples1Click(Sender: TObject);
procedure ESP8266Flasher1Click(Sender: TObject);
procedure HideRestartGarbageMClick(Sender: TObject);
procedure ViewLog1Click(Sender: TObject);
procedure EditBtnClick(Sender: TObject);
procedure Downloadlatestfirmware1Click(Sender: TObject);
procedure DownloadESP8266Flasherexe1Click(Sender: TObject);
procedure Runesp8266flasherexe1Click(Sender: TObject);
procedure BaudRateChange(Sender: TObject);
procedure SaveSettings1Click(Sender: TObject);
procedure NurdSpaceLuaReference1Click(Sender: TObject);
procedure GPIOrateMClick(Sender: TObject);
procedure WifiStatusBtnClick(Sender: TObject);
procedure ResetBaudTimerStop(Sender: TObject);
procedure AutoResetBaudRateMClick(Sender: TObject);
procedure FilenameEDropDown(Sender: TObject);
procedure tmrstop01Click(Sender: TObject);
procedure WSMenuClick(Sender: TObject);
procedure DownloadNodeMcuesp8266flasherexe1Click(Sender: TObject);
procedure FilenameEExit(Sender: TObject);
procedure FilenameEClick(Sender: TObject);
procedure FixFilenameStop(Sender: TObject);
procedure FastUpClick(Sender: TObject);
procedure LuaFAQ1Click(Sender: TObject);
procedure LuaGotchas1Click(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure Node1Click(Sender: TObject);
procedure FileModule1Click(Sender: TObject);
procedure WifiModule1Click(Sender: TObject);
procedure TimerModule1Click(Sender: TObject);
procedure GPIOModule1Click(Sender: TObject);
procedure PWMModule1Click(Sender: TObject);
procedure NetModule1Click(Sender: TObject);
procedure I2CModule1Click(Sender: TObject);
procedure ADCModule1Click(Sender: TObject);
procedure UARTModule1Click(Sender: TObject);
procedure OneWireModule1Click(Sender: TObject);
procedure Quit1Click(Sender: TObject);
procedure BitModuleClick(Sender: TObject);
procedure APIOverviewandChangeLog1Click(Sender: TObject);
procedure ESP8266WikiReference1Click(Sender: TObject);
procedure DeleteAllClick(Sender: TObject);
procedure LocalMClick(Sender: TObject);
procedure LocalSMClick(Sender: TObject);
procedure FormatBtnClick(Sender: TObject);
procedure ShowHint(Sender: TObject);
procedure DTRbtnClick(Sender: TObject);
procedure RTSbtnClick(Sender: TObject);
procedure CTSbtnClick(Sender: TObject);
procedure BuyNodeMCUdevelopmentboards1Click(Sender: TObject);
procedure QuickStartforBeginners1Click(Sender: TObject);
procedure ESPLuaClick(Sender: TObject);
procedure WSLuaClick(Sender: TObject);
procedure UseBinClick(Sender: TObject);
procedure UploadBinary1Click(Sender: TObject);
procedure UploadText1Click(Sender: TObject);
procedure WorkspaceClick(Sender: TObject);
procedure Checkall1Click(Sender: TObject);
procedure CheckNone1Click(Sender: TObject);
procedure OpenWorkspaceFolderinExplorer1Click(Sender: TObject);
procedure Setcurrentvalueasstartupdefault1Click(Sender: TObject);
procedure CustomClick(Sender: TObject);
procedure Resetfunctionality1Click(Sender: TObject);
procedure CompileClick(Sender: TObject);
procedure DoFileCompiledClick(Sender: TObject);
procedure DownLoadBtnClick(Sender: TObject);
procedure DecodeDL(Sender: TObject);
procedure ListlcMClick(Sender: TObject);
procedure ListallMClick(Sender: TObject);
procedure NodeMCUCustomBuilds1Click(Sender: TObject);
private
{ Private declarations }
RxDCharStartTimer : Boolean;
RxDCharResetTimer : Boolean;
SendDataSize : DWord;
procedure CheckLLVersion(Sender: TObject; filename : string; error : word);
procedure Log(line : string);
procedure Send(line : string; mode : integer);
procedure Show(line : string; mode : integer);
function AwaitPrompt(delay : integer) : boolean;
procedure ActivateSerial(Sender: TObject);
public
{ Public declarations }
function Upload(fname : string) : boolean ;
end;
var
Form1: TForm1;
StartTime : double;
GetFileNames : Boolean;
GetFileBuf : string;
WorkingDir : string;
Version : string;
LogFile : string;
LatestNodeMCU : string;
LatestLUA : string;
APIrevision : string;
VersionData : string;
HideRestartGarbage : boolean;
FlasherPath : string;
Prompt : boolean;
Timeoutc : integer;
okc : integer;
SoftRestart : boolean;
CheckWiFiStatus : boolean;
SendHistory : TStringList;
WasConnected : boolean;
LocalDocs : string;
FilesOnESP : TStringList;
StartingBin : Boolean;
LLBinNil : Boolean;
CannotOpen : Boolean;
DoingBin : Boolean;
xxdNil : Boolean;
DoingDL : Boolean;
StartingDL : Boolean;
CannotOpenxxd : Boolean;
AbortUnexpected : Boolean;
DTRdefault : Boolean;
RTSdefault : Boolean;
RecentDocFiles : TStringList;
RecentFiles : TStringList;
DLCapture : string;
CustomLuaFile : string;
const
ThisVersion = '0.90 '; // change with each version
CRLF = #$0d#$0a ;
implementation
uses SerialNGAdv, netstat; // Include Advanced Dialog
{$R *.DFM}
procedure AddHexString(S : String; Lines : TStrings );
var AddS, HexS, CopyS : String;
i : Integer;
const SLen = 8;
begin
while Length(S) > 0 do
begin
AddS := Copy(S,1,SLen);
HexS := '';
Delete(S,1,SLen);
for i := 1 to SLen do
begin
CopyS := Copy(AddS,i,1);
if CopyS <> '' then
HexS := HexS + ' ' + Format('%2.2x',[Byte(CopyS[1])]) //
else
HexS := HexS + ' ';
end;
while Length(AddS) < SLen do
AddS := AddS + ' ';
for i := 1 to SLen do
case AddS[i] of
#0..#31 : AddS[i] := '.';
#127 : AddS[i] := '.';
end;
Lines.Add(HexS+' : '+AddS);
end;
end;
procedure pause( delay : integer );
var
t : double;
begin
t := Now + delay/(24*60*60*1000);
while ( Now < t ) do
begin
Application.ProcessMessages();
end;
end;
function CommName : string;
var
port : string;
begin
port := Form1.SerialPortNG1.CommPort;
try
if port[1] <> '\' then
begin
result := port;
exit;
end;
result := Copy(port,5,99);
except;
result := '';
end;
end;
function FileTime( filename : string) : integer;
var
F : TSearchRec;
begin
if FileExists(filename) then
begin
FindFirst(filename,faAnyFile,F);
Result := F.Time;
FindClose(F);
end
else
Result := 0;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Regx : TIniFile;
timers : string;
i,n : integer;
ws : string;
MenuI : TMenuItem;
Temp : TStringList;
rtime : integer;
rfile : string;
begin
SerialPortNG1.Active := True;
HideRestartGarbage := False;
WasConnected := False;
StartingBin := False;
DoingBin := False;
Application.OnHint := ShowHint;
Application.HintHidePause := 5000;
Application.HintPause := 200;
SendHistory := TStringList.Create;
FilesOnESP := TStringList.Create;
FilesOnESP.Sorted := True;
FilesOnESP.Duplicates := dupIgnore;
FilesOnESP.Add('< Run file.list to add ESP files');
RecentDocFiles := TStringList.Create;
RecentDocFiles.Sorted := True;
RecentDocFiles.Duplicates := dupIgnore;
RecentFiles := TStringList.Create;
RecentFiles.Sorted := True;
RecentFiles.Duplicates := dupIgnore;
Caption := 'ESP8266 LuaLoader '+ThisVersion;
Regx := TIniFile.Create( ChangeFileExt(ParamStr(0),'.ini') );
Top := Regx.ReadInteger('LuaLoader','Top',Top);
Left := Regx.ReadInteger('LuaLoader','Left',Left);
Width := Regx.ReadInteger('LuaLoader','Width',Width);
Height := Regx.ReadInteger('LuaLoader','Height',Height);
WorkingDir := Regx.ReadString('LuaLoader','WorkingDir',ExtractFilePath(ParamStr(0)));
EditBtn.Hint := 'Edit '+workingDir+FilenameE.Text;
UploadBtn.Hint := 'Upload '+ workingDir+FilenameE.Text;
GPIO.Text := Regx.ReadString('LuaLoader','GPIO',' 3 GPIO0');
SSID.Text := Regx.ReadString('LuaLoader','SSID','SSID');
Password.Text := Regx.ReadString('LuaLoader','Password','Password');
LogFile := Regx.ReadString('LuaLoader','LogFile','');
FlasherPath := Regx.ReadString('LuaLoader','FlasherPath','c:\esp8266_flasher.exe');
LocalDocs := Regx.ReadString('LuaLoader','LocalDocumentation','');
LatestLua := Regx.ReadString('LuaLoader','LatestLua','');
HideRestartGarbageM.Checked := Regx.ReadBool('LuaLoader','HideRestartGarbage',False);
LogTerminalM.Checked := Regx.ReadBool('LuaLoader','Logging',False);
AutoResetBaudRateM.Checked := Regx.ReadBool('LuaLoader','AutoResetBaudRate',True);
FastUp.Checked := Regx.ReadBool('LuaLoader','FastUploads',False);
DTRdefault := Regx.ReadBool('LuaLoader','DTRdefault',False);
RTSdefault := Regx.ReadBool('LuaLoader','RTSdefault',False);
ESPLua.Checked := Regx.ReadBool('LuaLoader','ESPLuaOnly',True);
WSLua.Checked := Regx.ReadBool('LuaLoader','WSLuaOnly',True);
UseBin.Checked := Regx.ReadBool('LuaLoader','BinaryUpload',False);
if UseBin.Checked then
begin
UploadBtn.Caption := 'Upload Bin';
UploadBinary1.Checked := True;
UploadText1.Checked := False;
end
else
begin
UploadBtn.Caption := 'Upload Text';
UploadBinary1.Checked := False;
UploadText1.Checked := True;
end;
timers := Regx.ReadString('LuaLoader','Timers','TTFFFFF');
for i := 1 to 7 do
begin
TmrM.Items[i-1].Checked := timers[i] = 'T';
end;
Regx.WriteString('LuaLoader','Timers',timers);
for i := 0 to 25 do
begin
ws := Regx.ReadString('LuaLoader','Workspace'+IntToStr(i),'');
if (ws <> '') and (ws <> 'c:\') then
begin
MenuI := TMenuItem.Create(self);
MenuI.Caption := ws;
MenuI.OnClick := WSMenuClick;
Workspace.Insert(0,MenuI);
end;
end;
Custom.Caption := Regx.ReadString('LuaLoader','CustomButton','Custom');
CustomLuaFile := Regx.ReadString('LuaLoader','CustomLuaFile','');
Regx.Free;
if LogTerminalM.Checked and (LogFile = '') then
LogTerminalM.Checked := False;
BaudRate.ItemIndex := 0;
SoftRestart := False;
CheckWiFiStatus := False;
if FileExists(ExtractFilePath(ParamStr(0))+'RecentFiles.txt') then
begin
Temp := TStringList.Create;
Temp.LoadFromFile(ExtractFilePath(ParamStr(0))+'RecentFiles.txt');
for i := 0 to Temp.Count-1 do
begin
rtime := StrToIntDef(Copy(Temp[i],1,12),0);
rfile := Copy(Temp[i],14,999);
n := RecentFiles.Add(rfile);
RecentFiles.Objects[n] := TObject(rtime);
end;
Temp.Free;
end;
end;
procedure TForm1.ShowHint(Sender : TObject);
begin
if Length(FilenameE.Text) > 1 then
RemoveBtn.Hint := 'Delete '+FilenameE.Text
else
RemoveBtn.Hint := 'Delete file';
LowBtn.Hint := 'Set pin '+GPIO.Text+' Output Low';
HighBtn.Hint := 'Set pin '+GPIO.Text+' Output High';
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
Regx : TIniFile;
i : integer;
timers : string;
Temp : TStringList;
begin
ConnectedStatusTimer.Enabled := False;
net.TimeOutAbort.Enabled := False;
//Action := caFree;
SerialPortNG1.WriteSettings('Software\Benlo.com','LuaLoader');
SerialPortNG1.Active := False;
Regx := TIniFile.Create( ChangeFileExt(ParamStr(0),'.ini') );
Regx.WriteInteger('LuaLoader','Top',Top);
Regx.WriteInteger('LuaLoader','Left',Left);
Regx.WriteInteger('LuaLoader','Width',Width);
Regx.WriteInteger('LuaLoader','Height',Height);
Regx.WriteString('LuaLoader','WorkingDir',WorkingDir);
Regx.WriteString('LuaLoader','GPIO',GPIO.Text);
Regx.WriteString('LuaLoader','SSID',SSID.Text);
Regx.WriteString('LuaLoader','Password',Password.Text);
Regx.WriteString('LuaLoader','LogFile',LogFile);
Regx.WriteString('LuaLoader','FlasherPath',FlasherPath);
Regx.WriteString('LuaLoader','LocalDocumentation',LocalDocs);
Regx.WriteString('LuaLoader','LatestLua',LatestLua);
Regx.WriteString('LuaLoader','CustomButton',Custom.Caption);
Regx.WriteString('LuaLoader','CustomLuaFile',CustomLuaFile);
Regx.WriteBool('LuaLoader','Logging',LogTerminalM.Checked);
Regx.WriteBool('LuaLoader','HideRestartGarbage',HideRestartGarbageM.Checked);
Regx.WriteBool('LuaLoader','AutoResetBaudRate',AutoResetBaudRateM.Checked);
Regx.WriteBool('LuaLoader','FastUploads',FastUp.Checked);
Regx.WriteBool('LuaLoader','DTRdefault',DTRdefault);
Regx.WriteBool('LuaLoader','RTSdefault',RTSdefault);
Regx.WriteBool('LuaLoader','ESPLuaOnly',ESPLua.Checked);
Regx.WriteBool('LuaLoader','WSLuaOnly', WSLua.Checked);
Regx.WriteBool('LuaLoader','BinaryUpload', UseBin.Checked);
// Halt(1);
timers := 'TTFFFFF';
for i := 0 to 6 do
begin
if TmrM.Items[i].Checked then
timers[i+1] := 'T'
else
timers[i+1] := 'F';
end;
Regx.WriteString('LuaLoader','Timers',timers);
for i := 0 to min(25,Workspace.Count-1) do
begin
if Workspace.Items[i].caption <> 'Remove Current Workspace' then
Regx.WriteString('LuaLoader','Workspace'+IntToStr(i),Workspace.Items[i].caption);
end;
Regx.Free;
Temp := TStringList.Create;
for i := 0 to RecentFiles.Count-1 do
begin
Temp.Add( Format('%12.12d %s',[Integer(RecentFiles.Objects[i]),RecentFiles[i]]) );
end;
Temp.SaveToFile(ExtractFilePath(ParamStr(0))+'RecentFiles.txt');
Temp.Free;
end;
procedure TForm1.FormShow(Sender: TObject);
var
i : integer;
begin
Net.StatusBar := False;
SerialPortNG1.ReadSettings('Software\Benlo.com','LuaLoader');
i := BaudRate.Items.IndexOf(IntToStr(SerialPortNG1.BaudRate));
if i >= 0 then
BaudRate.ItemIndex := i
else
BaudRate.Text := IntToStr(SerialPortNG1.BaudRate);
SerialPortNG1.DTRState := DTRdefault;
SerialPortNG1.RTSState := RTSdefault;
Net.GetURL('http://benlo.com/esp8266/LuaLoader.php?LL=1&api=1&version='+ThisVersion,CheckLLVersion);
Send(CRLF,1);
end;
procedure TForm1.ActivateSerial(Sender: TObject);
begin
SerialPortNG1.Active := True;
SerialPortNG1.DTRState := DTRdefault;
SerialPortNG1.RTSState := RTSdefault;
end;
procedure TForm1.ShowURL( URL : string);
begin
ShellExecute( Application.Handle,'open', PChar(URL), PChar(URL), nil, SW_SHOWNORMAL);
end;
procedure TForm1.Log(line : string);
var
f : textfile;
begin
if LogTerminalM.Checked then
begin
try
AssignFile(f, LogFile);
if not FileExists(LogFile) then
begin
Rewrite(f);
end
else
begin
Append(f);
end;
Write(f, line);
Flush(f);
CloseFile(f);
except
ShowMessage('Error writing to log file:'#13#13+LogFile);
end
end;
end;
procedure TForm1.Send(line : string; mode : integer); // sends to comm and logs
begin
if SerialPortNG1.Active then
begin
SerialPortNG1.SendString(line);
Log(line);
if mode = 1 then pause(200);
end
else
begin
Show('Not connected to comm port',1);
end;
end;
procedure TForm1.Show(line : string; mode : integer); // types on terminal and logs
begin
if mode = 1 then Terminal.Lines.Add('');
Terminal.Lines.Add(line);
if mode = 1 then Terminal.Lines.Add('');
Log(line);
end;
procedure TForm1.AdvSettingsBtnClick(Sender: TObject);
begin
try
SerialNGAdvDLG.SetDLGData(SerialPortNG1);
if SerialNGAdvDLG.ShowModal = mrOK then
SerialNGAdvDLG.GetDLGData(SerialPortNG1);
except
ShowMessage('Error loading COM dialog!');
end;
end;
// All receiving is done here
// See how less Lines!
procedure TForm1.SerialPortNG1RxClusterEvent(Sender: TObject);
var
line : string;
x, y : integer;
newf : string;
DL : TStringList;
begin
if SerialPortNG1.NextClusterSize >= 0 then // Data available?
begin
if SerialPortNG1.NextClusterCCError = 0 then // Error during receiveing?
else
begin
Show('Communications Error - Check baud rate',1);
if AutoResetBaudRateM.Checked then
ResetBaudTimer.Enabled := True;
end;
line := SerialPortNG1.ReadNextClusterAsString;
if Pos('',line) > 0 then
begin
Show('Communications Error - Check baud rate',1);
if AutoResetBaudRateM.Checked then
ResetBaudTimer.Enabled := True;
end;
if CheckWifiStatus then
begin
x := Pos('>',line);
if x > 0 then
begin
y := StrToIntDef(Copy(line,x-3,1),6);
case y of
0: line := stringreplace(line,'0','0 IDLE',[]);
1: line := stringreplace(line,'1','1 CONNECTING',[]);
2: line := stringreplace(line,'2','2 INCORRECT PASSWORD',[]);
3: line := stringreplace(line,'3','3 AP NOT FOUND',[]);
4: line := stringreplace(line,'4','4 CONNECT FAILED',[]);
5: line := stringreplace(line,'5','5 GOT IP',[]);
end;
end;
WifiStatusBtn.Down := False;
CheckWiFiStatus := False;
end;
if StartingBin then
begin
if Pos('LLbin'' (a nil',line) > 0 then
LLBinNil := True;
if Pos('cannot open LLbin.lua',line) > 0 then
CannotOpen := True;
end;
if DoingBin then
begin
if Pos('unexpected symbol',line) > 0 then
begin
AbortUnexpected := True;
UploadBtn.Caption := 'Upload Bin'; // aborts bin when caption was Abort
Show(CRLF+'Binary upload requires NodeMCU build 20150105 or newer'+CRLF,1);
end;
end;
if StartingDL then
begin
if Pos('xxd'' (a nil',line) > 0 then
begin
xxdNil := True;
DoingDL := False;
end;
if Pos('cannot open xxd.lua',line) > 0 then
CannotOpenxxd := True;
end;
if DoingDL then
begin
DLCapture := DLCapture + line;
if Pos('EOF',line) > 0 then
begin
DL := TStringList.Create;
DL.Add(DLCapture);
DL.SaveToFile(ExtractFilePath(ParamStr(0))+'xxd.temp');
DL.Free;
DLCapture := '';
DoingDL := False;
DLDecoder.Enabled := True;
end;
end;
if 0 < Pos('>',line) then
begin
Prompt := True;
end;
if RepeatTimer.Enabled then
begin
x := Pos(CRLF+'>', line);
Delete(line,x,4);
end;
if HideRestartGarbage then
begin
x := Pos('MCU',UpperCase(line));
if x > 0 then
begin
Delete(line,1,x-5);
HideRestartGarbage := False;
end
else line := '';
end;
Terminal.Text := Terminal.Text + line;
SendMessage(Terminal.Handle, EM_LINESCROLL, 0,Terminal.Lines.Count); // scroll to end
if GetFileNames then
begin
GetFileBuf := GetFileBuf + line;
if Pos('for k,v',GetFileBuf) > 0 then Delete(GetFileBuf,1,Pos('for k,v',GetFileBuf));
if Pos(') end',GetFileBuf) > 0 then Delete(GetFileBuf,1,Pos(') end',GetFileBuf));
if Pos(')'#$D#$A'>',GetFileBuf) > 0 then Delete(GetFileBuf,1,Pos(')'#$D#$A'>',GetFileBuf)+3);
if Pos('>',GetFileBuf) > 0 then
begin
x := Pos(CRLF+CRLF,GetFileBuf);
if x > 0 then Delete(GetFileBuf,1,x+1);
repeat
begin
x := Pos(CRLF,GetFileBuf);
if x > 0 then Delete(GetFileBuf,1,x+1);
y := Pos(' ',GetFileBuf);