ugui.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. unit uGUI;
  2. {$mode objfpc}{$H+}
  3. {$define DebugLCLComponents}
  4. interface
  5. uses
  6. Windows, Classes, SysUtils, FileUtil, SdpoSerial, Forms, Controls, Graphics, Dialogs,
  7. Grids, ExtCtrls, Menus, ComCtrls, ActnList, StdActns, LCLProc, StdCtrls, Spin,
  8. XMLPropStorage, uSPS, LCLType, Buttons, types, MCSWINAPI, Math,
  9. fpjson, jsonparser, Midi;
  10. type
  11. { TForm1 }
  12. TForm1 = class(TForm)
  13. acExit: TAction;
  14. acDebug: TAction;
  15. acNextStep: TAction;
  16. acStop: TAction;
  17. acThisStep: TAction;
  18. acShowHexFile: TAction;
  19. acNew: TAction;
  20. acNewRow: TAction;
  21. acDeleteRow: TAction;
  22. acFileSave: TAction;
  23. acHelpAbout: TAction;
  24. acHexFile: TAction;
  25. acNextInsert: TAction;
  26. acUpload: TAction;
  27. ActionList1: TActionList;
  28. acFileOpen: TFileOpen;
  29. acFileSaveAs: TFileSaveAs;
  30. btnTone: TBitBtn;
  31. cbTPSVersion: TComboBox;
  32. cbAdrActual: TCheckBox;
  33. EditDelay: TLabeledEdit;
  34. EditJump: TLabeledEdit;
  35. EditPage: TLabeledEdit;
  36. EditRAdr: TLabeledEdit;
  37. acPresetLoad: TFileOpen;
  38. acPresetSave: TFileSaveAs;
  39. GBControl: TGroupBox;
  40. ImageList2: TImageList;
  41. Label3: TLabel;
  42. Label4: TLabel;
  43. Label5: TLabel;
  44. lbStack: TListBox;
  45. MCSLabel: TLabel;
  46. MenuItem1: TMenuItem;
  47. pmExamples: TPopupMenu;
  48. RC1: TSpinEdit;
  49. RC2: TSpinEdit;
  50. cbCommand: TComboBox;
  51. cbData: TComboBox;
  52. Din1: TCheckBox;
  53. Din2: TCheckBox;
  54. Din3: TCheckBox;
  55. Din4: TCheckBox;
  56. GBOutput: TGroupBox;
  57. GBInput: TGroupBox;
  58. GBInternal: TGroupBox;
  59. ImageList1: TImageList;
  60. Label1: TLabel;
  61. Label10: TLabel;
  62. Label11: TLabel;
  63. Label12: TLabel;
  64. Label13: TLabel;
  65. Label14: TLabel;
  66. Label15: TLabel;
  67. Label16: TLabel;
  68. Label17: TLabel;
  69. Label2: TLabel;
  70. Panel2: TPanel;
  71. Panel3: TPanel;
  72. PWM1: TLabeledEdit;
  73. PWM2: TLabeledEdit;
  74. Panel1: TPanel;
  75. ADC1: TSpinEdit;
  76. ADC2: TSpinEdit;
  77. SaveDialog1: TSaveDialog;
  78. SaveHexFile: TSaveDialog;
  79. SdpoSerial1: TSdpoSerial;
  80. Servo1: TLabeledEdit;
  81. Servo2: TLabeledEdit;
  82. EditA: TLabeledEdit;
  83. EditC: TLabeledEdit;
  84. EditE: TLabeledEdit;
  85. EditB: TLabeledEdit;
  86. EditD: TLabeledEdit;
  87. EditF: TLabeledEdit;
  88. EditAddr: TLabeledEdit;
  89. Shape1: TShape;
  90. Shape2: TShape;
  91. ShapeOut1: TShape;
  92. ShapeOut2: TShape;
  93. ShapeOut3: TShape;
  94. ShapeOut4: TShape;
  95. SpeedButton1: TSpeedButton;
  96. StatusBar1: TStatusBar;
  97. StringGrid1: TStringGrid;
  98. tbPrg: TToggleBox;
  99. tbPreLoad: TToolButton;
  100. tbPreSave: TToolButton;
  101. tbSel: TToggleBox;
  102. Timer1: TTimer;
  103. ToolBar1: TToolBar;
  104. ToolBar2: TToolBar;
  105. ToolButton1: TToolButton;
  106. ToolButton10: TToolButton;
  107. ToolButton11: TToolButton;
  108. ToolButton12: TToolButton;
  109. ToolButton13: TToolButton;
  110. ToolButton14: TToolButton;
  111. ToolButton15: TToolButton;
  112. ToolButton16: TToolButton;
  113. ToolButton17: TToolButton;
  114. ToolButton18: TToolButton;
  115. tbPreset1: TToolButton;
  116. tbReset: TToolButton;
  117. ToolButton19: TToolButton;
  118. ToolButton2: TToolButton;
  119. tbPreset2: TToolButton;
  120. tbPreset3: TToolButton;
  121. tbPreset4: TToolButton;
  122. tbPreset5: TToolButton;
  123. tbPreset6: TToolButton;
  124. tbPreset7: TToolButton;
  125. tbPreset8: TToolButton;
  126. ToolButton20: TToolButton;
  127. ToolButton3: TToolButton;
  128. ToolButton4: TToolButton;
  129. ToolButton5: TToolButton;
  130. ToolButton6: TToolButton;
  131. ToolButton7: TToolButton;
  132. ToolButton8: TToolButton;
  133. ToolButton9: TToolButton;
  134. TOpenDialogPreset: TOpenDialog;
  135. TSaveDialogPreset: TSaveDialog;
  136. XMLPropStorage1: TXMLPropStorage;
  137. procedure acDeleteRowExecute(Sender: TObject);
  138. procedure acExitExecute(Sender: TObject);
  139. procedure acFileOpenAccept(Sender: TObject);
  140. procedure acFileSaveAsAccept(Sender: TObject);
  141. procedure acDebugExecute(Sender: TObject);
  142. procedure acHelpAboutExecute(Sender: TObject);
  143. procedure acHexFileExecute(Sender: TObject);
  144. procedure acNewExecute(Sender: TObject);
  145. procedure acNewRowExecute(Sender: TObject);
  146. procedure acNextInsertExecute(Sender: TObject);
  147. procedure acNextStepExecute(Sender: TObject);
  148. procedure acPresetLoadAccept(Sender: TObject);
  149. procedure acPresetSaveAccept(Sender: TObject);
  150. procedure acShowHexFileExecute(Sender: TObject);
  151. procedure acStopExecute(Sender: TObject);
  152. procedure acThisStepExecute(Sender: TObject);
  153. procedure acFileSaveExecute(Sender: TObject);
  154. procedure acUploadExecute(Sender: TObject);
  155. procedure btnToneClick(Sender: TObject);
  156. procedure cbCommandChange(Sender: TObject);
  157. procedure cbDataChange(Sender: TObject);
  158. procedure cbTPSVersionChange(Sender: TObject);
  159. procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
  160. procedure FormCreate(Sender: TObject);
  161. procedure FormDropFiles(Sender: TObject; const FileNames: array of string);
  162. procedure MCSLabelClick(Sender: TObject);
  163. procedure MenuItem1Click(Sender: TObject);
  164. procedure mnSaveClick(Sender: TObject);
  165. procedure pmExamplesPopup(Sender: TObject);
  166. procedure Shape1Paint(Sender: TObject);
  167. procedure Shape2Paint(Sender: TObject);
  168. procedure StringGrid1EditingDone(Sender: TObject);
  169. procedure StringGrid1Selection(Sender: TObject; aCol, aRow: integer);
  170. procedure tbPreset1Click(Sender: TObject);
  171. procedure tbPreset1ContextPopup(Sender: TObject; MousePos: TPoint;
  172. var Handled: boolean);
  173. procedure tbResetClick(Sender: TObject);
  174. procedure Timer1Timer(Sender: TObject);
  175. private
  176. { private declarations }
  177. sps: TSPS;
  178. stopit: boolean;
  179. activeFile: string;
  180. dirty: boolean;
  181. lastNote: byte;
  182. Examples : TStringList;
  183. procedure initMidi;
  184. procedure playNote(note: byte);
  185. function readString(var line: string): boolean;
  186. procedure saveSection(filename: string; key: string);
  187. procedure loadSection(filename: string; key: string);
  188. procedure loadFile(filename: string);
  189. procedure programSps;
  190. procedure nextStep;
  191. procedure renumberGrid;
  192. procedure saveFile(filename: string);
  193. procedure selectAddress(addr: byte);
  194. procedure inputSps;
  195. procedure outputSps;
  196. procedure uploadFile;
  197. function serialUpload: string;
  198. procedure setDirty(Value: boolean);
  199. function checkDirty: boolean;
  200. procedure checkPresets;
  201. procedure makeHexFile(fileName: string);
  202. procedure setCaption;
  203. procedure addHeaderText;
  204. procedure loadPreset(filename: string);
  205. procedure savePreset(filename: string);
  206. procedure activateSps(enable: boolean);
  207. public
  208. { public declarations }
  209. end;
  210. var
  211. Form1: TForm1;
  212. implementation
  213. uses MCSAbout, uTextUi, uSelectCom, MCSTools, MCSStrings, synaser,
  214. MCSIO, mcsintelhex, MCSLSU, MCSIniFiles, MCSWinHttp, luijsonutils;
  215. {$R *.lfm}
  216. procedure delayCallback(Value: integer);
  217. begin
  218. Form1.EditDelay.Text := IntToStr(Value);
  219. Application.ProcessMessages;
  220. end;
  221. { TForm1 }
  222. var
  223. lsuCode: integer;
  224. procedure TForm1.FormCreate(Sender: TObject);
  225. var
  226. line: string;
  227. begin
  228. Infobox.AppTitel := MCSLSU.GetLSUText('infobox', 'ID_APPTITLE', lsuCode);
  229. Infobox.CopyRight := MCSLSU.GetLSUText('infobox', 'ID_COPYRIGHT', lsuCode);
  230. Infobox.Build := MCSGetVersion(Application.ExeName);
  231. Infobox.AppID := 31;
  232. Infobox.AppURL :=
  233. 'http://www.wk-music.de/pages/mcs/microcontroller/tps-sps-emulator.php';
  234. sps := TSPS.Create();
  235. sps.setDelayCallback(@delayCallback);
  236. addHeaderText;
  237. renumberGrid();
  238. cbTPSVersionChange(Sender);
  239. acStop.Enabled := False;
  240. ToolButton18.Align := alRight;
  241. MCSLabel.Align := alRight;
  242. cbAdrActual.Enabled := False;
  243. checkPresets();
  244. tbResetClick(nil);
  245. if Application.ParamCount >= 1 then
  246. begin
  247. line := Application.Params[1];
  248. loadFile(line);
  249. end;
  250. activateSps(False);
  251. Timer1.Enabled := True;
  252. MCSLSU.MakeForm('form1', 'ID_', form1);
  253. initMidi;
  254. Examples := TStringList.Create;
  255. end;
  256. procedure TForm1.initMidi;
  257. var
  258. Value: integer;
  259. begin
  260. lastNote := 0;
  261. MidiOutput.Open(0);
  262. Value := XMLPropStorage1.ReadInteger('MidiProgram', 30);
  263. MidiOutput.Send(0, 192, Value, 0);
  264. end;
  265. procedure TForm1.playNote(note: byte);
  266. var
  267. command, velocity: byte;
  268. begin
  269. if (lastNote > 0) then
  270. begin
  271. command := $80;
  272. velocity := $7F;
  273. MidiOutput.Send(0, command, lastnote, velocity);
  274. end;
  275. if (note > 0) then
  276. begin
  277. command := $90;
  278. velocity := $7F;
  279. MidiOutput.Send(0, command, note, velocity);
  280. end;
  281. lastNote := note;
  282. end;
  283. procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string);
  284. var
  285. line: string;
  286. begin
  287. if (SizeOF(FileNames) > 0) then
  288. begin
  289. line := FileNames[0];
  290. loadFile(line);
  291. end;
  292. end;
  293. procedure TForm1.MCSLabelClick(Sender: TObject);
  294. begin
  295. ShExec2(self.Handle, InfoBox.AppURL);
  296. end;
  297. procedure TForm1.MenuItem1Click(Sender: TObject);
  298. var i : integer;
  299. begin
  300. // todo
  301. if (Sender is TMenuItem) then begin
  302. i := TMenuItem(Sender).Tag;
  303. end;
  304. end;
  305. procedure TForm1.mnSaveClick(Sender: TObject);
  306. begin
  307. Statusbar1.SimpleText := Sender.ClassName;
  308. end;
  309. procedure TForm1.pmExamplesPopup(Sender: TObject);
  310. var
  311. i: integer;
  312. myName: string;
  313. myMenu: TMenuItem;
  314. jsonString: string;
  315. Data, JArray: TJSONData;
  316. JItem: TJSONData;
  317. begin
  318. for i := pmExamples.Items.Count - 1 downto 0 do
  319. begin
  320. myMenu := pmExamples.Items[i];
  321. myMenu.Free;
  322. end;
  323. pmExamples.Items.Clear;
  324. Examples.Clear;
  325. try
  326. jsonString := DownloadFile('http://wkla.no-ip.biz/down/examples.json');
  327. Data := StringToJSONData(jsonString);
  328. JArray := GetJSONProp(TJSONObject(Data), 'examples');
  329. for i := 1 to TJSONArray(JArray).Count do
  330. begin
  331. JItem := TJSONArray(JArray).Items[i-1];
  332. myName := GetJsonProp(TJSONObject(JItem), 'name', '');
  333. myName := format('%.2d - %s', [i, myName]);
  334. myMenu := TMenuItem.Create(pmExamples);
  335. myMenu.Caption := myName;
  336. myMenu.Tag := i;
  337. myMenu.OnClick := @MenuItem1Click;
  338. pmExamples.Items.Add(myMenu);
  339. Examples.Add(GetJsonProp(TJSONObject(JItem), 'file', ''));
  340. end;
  341. except
  342. end;
  343. end;
  344. procedure TForm1.Shape1Paint(Sender: TObject);
  345. var
  346. radius: integer;
  347. x, y: integer;
  348. begin
  349. radius := round(Shape1.Width / 2);
  350. Shape1.Canvas.Brush.Color := clBlack;
  351. Shape1.canvas.MoveTo(radius, radius);
  352. x := radius - round(radius * cos(degtorad(sps.getServo1())));
  353. y := radius - round(radius * sin(degtorad(sps.getServo1())));
  354. Shape1.Canvas.LineTo(x, y);
  355. end;
  356. procedure TForm1.Shape2Paint(Sender: TObject);
  357. var
  358. radius: integer;
  359. x, y: integer;
  360. begin
  361. radius := round(Shape1.Width / 2);
  362. Shape2.Canvas.Brush.Color := clBlack;
  363. Shape2.canvas.MoveTo(radius, radius);
  364. x := radius - round(radius * cos(degtorad(sps.getServo2())));
  365. y := radius - round(radius * sin(degtorad(sps.getServo2())));
  366. Shape2.Canvas.LineTo(x, y);
  367. end;
  368. procedure TForm1.acExitExecute(Sender: TObject);
  369. begin
  370. Close;
  371. end;
  372. procedure TForm1.acDeleteRowExecute(Sender: TObject);
  373. var
  374. i: integer;
  375. begin
  376. i := StringGrid1.Row;
  377. StringGrid1.DeleteRow(i);
  378. renumberGrid();
  379. end;
  380. procedure TForm1.acFileOpenAccept(Sender: TObject);
  381. var
  382. filename: string;
  383. begin
  384. filename := (Sender as TFileOpen).Dialog.FileName;
  385. loadFile(filename);
  386. end;
  387. procedure TForm1.loadFile(filename: string);
  388. var
  389. i: integer;
  390. f: Text;
  391. line: string;
  392. list: TStringList;
  393. begin
  394. if (checkDirty()) then
  395. begin
  396. if (FileExists(filename)) then
  397. begin
  398. acNew.Execute;
  399. list := TStringList.Create;
  400. i := 1;
  401. AssignFile(f, filename);
  402. Reset(f);
  403. while (not EOF(f)) do
  404. begin
  405. if (i + 1 > StringGrid1.RowCount) then
  406. begin
  407. StringGrid1.RowCount := StringGrid1.RowCount + 1;
  408. end;
  409. readln(f, line);
  410. if (Pos('#', line) = 1) then
  411. begin
  412. line := RightstrPos(line, 2);
  413. if (Pos('TPS:', line) = 1) then
  414. begin
  415. line := RightstrPos(line, 5);
  416. cbTPSVersion.Text := line;
  417. cbTPSVersionChange(nil);
  418. end;
  419. end
  420. else
  421. begin
  422. MCSStrings.DelimSepTextToStringlist(line, '"', ',', list);
  423. if list.Count > 0 then
  424. Stringgrid1.Cells[0, i] := list[0];
  425. if list.Count > 1 then
  426. Stringgrid1.Cells[1, i] := list[1];
  427. if list.Count > 2 then
  428. Stringgrid1.Cells[2, i] := list[2];
  429. if list.Count > 3 then
  430. Stringgrid1.Cells[4, i] := list[3];
  431. list.Clear;
  432. i := i + 1;
  433. end;
  434. end;
  435. CloseFile(f);
  436. list.Free;
  437. activeFile := filename;
  438. renumberGrid();
  439. addHeaderText;
  440. setCaption();
  441. setDirty(False);
  442. end;
  443. end;
  444. end;
  445. procedure TForm1.acFileSaveAsAccept(Sender: TObject);
  446. var
  447. filename: string;
  448. begin
  449. filename := (Sender as TFileSaveAs).Dialog.FileName;
  450. saveFile(filename);
  451. end;
  452. procedure TForm1.saveFile(filename: string);
  453. var
  454. x, i: integer;
  455. f: Text;
  456. line: string;
  457. begin
  458. if (filename = '') then
  459. begin
  460. if SaveDialog1.Execute() then
  461. begin
  462. filename := SaveDialog1.FileName;
  463. end;
  464. end;
  465. if (filename <> '') then
  466. begin
  467. AssignFile(f, filename);
  468. Rewrite(f);
  469. line := '#TPS:' + cbTPSVersion.Text;
  470. Writeln(f, line);
  471. i := StringGrid1.RowCount;
  472. for x := 1 to i - 1 do
  473. begin
  474. if (StringGrid1.Cells[1, x] <> '') then
  475. begin
  476. line := StringGrid1.Cells[0, x] + ',' + StringGrid1.Cells[1, x] +
  477. ',' + StringGrid1.Cells[2, x] + ',"' + StringGrid1.Cells[4, x] + '"';
  478. Writeln(f, line);
  479. end;
  480. end;
  481. CloseFile(f);
  482. setDirty(False);
  483. activeFile := filename;
  484. setCaption();
  485. end;
  486. end;
  487. procedure TForm1.acDebugExecute(Sender: TObject);
  488. begin
  489. if (sps.isActive()) then
  490. begin
  491. acStopExecute(Sender);
  492. end
  493. else
  494. begin
  495. activateSps(True);
  496. acDebug.Enabled := True;
  497. acDebug.ImageIndex := 10;
  498. cbAdrActual.Enabled := True;
  499. acNextStep.Enabled := False;
  500. // sps programmieren
  501. programSps();
  502. // programm starten
  503. Statusbar1.SimpleText := MCSLSU.GetLSUText('statusbar', 'ID_SPS_START', lsuCode);
  504. sps.start();
  505. stopit := False;
  506. Statusbar1.SimpleText := MCSLSU.GetLSUText('statusbar', 'ID_SPS_RUNNING', lsuCode);
  507. while (not stopit) do
  508. begin
  509. nextStep();
  510. if (cbAdrActual.Checked) then
  511. selectAddress(sps.getAddress());
  512. Application.ProcessMessages;
  513. end;
  514. Statusbar1.SimpleText := MCSLSU.GetLSUText('statusbar', 'ID_SPS_STOPPED', lsuCode);
  515. cbAdrActual.Enabled := False;
  516. acNextStep.Enabled := True;
  517. sps.doReset();
  518. end;
  519. end;
  520. procedure TForm1.acHelpAboutExecute(Sender: TObject);
  521. begin
  522. Infobox.Show;
  523. end;
  524. procedure TForm1.acHexFileExecute(Sender: TObject);
  525. var
  526. filename: string;
  527. begin
  528. filename := MCSIO.MCSExtractFileNameExlExt(activeFile) + '.hex';
  529. SaveHexFile.FileName := filename;
  530. if (SaveHexFile.Execute) then
  531. begin
  532. filename := SaveHexFile.FileName;
  533. makeHexFile(filename);
  534. end;
  535. end;
  536. procedure TForm1.acNewExecute(Sender: TObject);
  537. begin
  538. if (checkDirty()) then
  539. begin
  540. StringGrid1.RowCount := 2;
  541. StringGrid1.Clean;
  542. addHeaderText();
  543. renumberGrid();
  544. activeFile := '';
  545. setCaption();
  546. setDirty(False);
  547. end;
  548. end;
  549. procedure TForm1.acNewRowExecute(Sender: TObject);
  550. var
  551. myPos: integer;
  552. i, x: integer;
  553. eot: boolean;
  554. begin
  555. eot := False;
  556. myPos := StringGrid1.Row;
  557. if (myPos = StringGrid1.RowCount - 1) then
  558. eot := True;
  559. StringGrid1.RowCount := StringGrid1.RowCount + 1;
  560. if (not eot) then
  561. begin
  562. for i := StringGrid1.RowCount - 2 downto myPos do
  563. begin
  564. for x := 1 to StringGrid1.ColCount - 1 do
  565. begin
  566. StringGrid1.Cells[x, i + 1] := StringGrid1.Cells[x, i];
  567. end;
  568. end;
  569. for x := 1 to StringGrid1.ColCount - 1 do
  570. begin
  571. StringGrid1.Cells[x, myPos] := '';
  572. end;
  573. end;
  574. renumberGrid();
  575. end;
  576. procedure TForm1.acNextInsertExecute(Sender: TObject);
  577. var
  578. myPos: integer;
  579. begin
  580. myPos := StringGrid1.Row;
  581. if (myPos = StringGrid1.RowCount - 1) then
  582. acNewRow.Execute;
  583. StringGrid1.Row := myPos + 1;
  584. renumberGrid();
  585. end;
  586. procedure TForm1.programSps;
  587. var
  588. x, i: integer;
  589. com, Data: byte;
  590. tmp: string;
  591. begin
  592. Statusbar1.SimpleText := MCSLSU.GetLSUText('statusbar', 'ID_SPS_PROGRAMMING', lsuCode);
  593. i := StringGrid1.RowCount;
  594. for x := 1 to i - 1 do
  595. begin
  596. com := 0;
  597. Data := 0;
  598. tmp := StringGrid1.Cells[1, x];
  599. if (tmp <> '') then
  600. begin
  601. com := HexToInt(tmp);
  602. tmp := StringGrid1.Cells[2, x];
  603. if (tmp <> '') then
  604. Data := HexToInt(tmp);
  605. Data := com * 16 + Data;
  606. end;
  607. sps.writeEEProm(x - 1, Data);
  608. end;
  609. // endekennzeichnung schreiben
  610. sps.writeEEProm(i, $ff);
  611. end;
  612. procedure TForm1.acNextStepExecute(Sender: TObject);
  613. begin
  614. nextStep();
  615. selectAddress(sps.getAddress());
  616. end;
  617. procedure TForm1.acPresetLoadAccept(Sender: TObject);
  618. var
  619. filename: string;
  620. begin
  621. filename := (Sender as TFileOpen).Dialog.FileName;
  622. loadPreset(filename);
  623. end;
  624. procedure TForm1.acPresetSaveAccept(Sender: TObject);
  625. var
  626. filename: string;
  627. begin
  628. filename := (Sender as TFileSaveAs).Dialog.FileName;
  629. savePreset(filename);
  630. end;
  631. procedure TForm1.nextStep;
  632. begin
  633. if (not sps.isActive()) then
  634. begin
  635. activateSps(True);
  636. programSps;
  637. sps.start();
  638. acStop.Enabled := True;
  639. acDebug.Enabled := False;
  640. end
  641. else
  642. begin
  643. acNextStep.Enabled := False;
  644. acStop.Enabled := False;
  645. inputSps();
  646. sps.nextStep();
  647. outputSps();
  648. acNextStep.Enabled := True;
  649. acStop.Enabled := True;
  650. end;
  651. end;
  652. procedure TForm1.acShowHexFileExecute(Sender: TObject);
  653. var
  654. list: TStringList;
  655. i, x: integer;
  656. line, tmp: string;
  657. Value: byte;
  658. begin
  659. Form2 := TForm2.Create(self);
  660. list := TStringList.Create();
  661. line := MCSLSU.GetLSUText('hexfile', 'ID_START_LINE', lsuCode);
  662. list.add(line);
  663. i := StringGrid1.RowCount;
  664. for x := 1 to i - 1 do
  665. begin
  666. if (StringGrid1.Cells[1, x] <> '') then
  667. begin
  668. tmp := '';
  669. if (StringGrid1.Cells[1, x] = '') then
  670. begin
  671. tmp := ' ';
  672. end
  673. else
  674. begin
  675. tmp := StringGrid1.Cells[1, x];
  676. end;
  677. if (StringGrid1.Cells[2, x] = '') then
  678. begin
  679. tmp := tmp + ' ';
  680. end
  681. else
  682. begin
  683. tmp := tmp + StringGrid1.Cells[2, x];
  684. end;
  685. line := StringGrid1.Cells[0, x] + ': ' + tmp + ' ';
  686. Value := HexToInt(StringGrid1.Cells[1, x]);
  687. tmp := '';
  688. if (Value and 8) > 0 then
  689. tmp := tmp + 'X'
  690. else
  691. tmp := tmp + '0';
  692. if (Value and 4) > 0 then
  693. tmp := tmp + 'X'
  694. else
  695. tmp := tmp + '0';
  696. if (Value and 2) > 0 then
  697. tmp := tmp + 'X'
  698. else
  699. tmp := tmp + '0';
  700. if (Value and 1) > 0 then
  701. tmp := tmp + 'X'
  702. else
  703. tmp := tmp + '0';
  704. line := line + tmp + ' ';
  705. Value := HexToInt(StringGrid1.Cells[2, x]);
  706. tmp := '';
  707. if (Value and 8) > 0 then
  708. tmp := tmp + 'X'
  709. else
  710. tmp := tmp + '0';
  711. if (Value and 4) > 0 then
  712. tmp := tmp + 'X'
  713. else
  714. tmp := tmp + '0';
  715. if (Value and 2) > 0 then
  716. tmp := tmp + 'X'
  717. else
  718. tmp := tmp + '0';
  719. if (Value and 1) > 0 then
  720. tmp := tmp + 'X'
  721. else
  722. tmp := tmp + '0';
  723. line := line + tmp;
  724. line := line + ' ' + StringGrid1.Cells[3, x] + ' ,"' +
  725. StringGrid1.Cells[4, x] + '"';
  726. list.add(line);
  727. end;
  728. end;
  729. Form2.addHexFile(list);
  730. Form2.ShowModal;
  731. list.Free;
  732. end;
  733. procedure TForm1.acStopExecute(Sender: TObject);
  734. begin
  735. if (sps.isActive()) then
  736. begin
  737. stopit := True;
  738. sps.break();
  739. acDebug.ImageIndex := 18;
  740. // repeat
  741. // Application.ProcessMessages;
  742. // until (not sps.isDelayActive());
  743. sps.doReset();
  744. acStop.Enabled := False;
  745. acDebug.Enabled := True;
  746. outputSps();
  747. selectAddress(0);
  748. activateSps(False);
  749. end;
  750. end;
  751. procedure TForm1.acThisStepExecute(Sender: TObject);
  752. var
  753. Data, com: byte;
  754. tmp: string;
  755. begin
  756. inputSps();
  757. try
  758. tmp := StringGrid1.Cells[1, StringGrid1.Row];
  759. if (tmp <> '') then
  760. begin
  761. com := HexToInt(tmp);
  762. tmp := StringGrid1.Cells[2, StringGrid1.Row];
  763. if (tmp <> '') then
  764. Data := HexToInt(tmp);
  765. Data := com * 16 + Data;
  766. end;
  767. sps.doSingleCommand(Data);
  768. except
  769. end;
  770. outputSps();
  771. end;
  772. procedure TForm1.acFileSaveExecute(Sender: TObject);
  773. begin
  774. saveFile(activeFile);
  775. end;
  776. procedure TForm1.acUploadExecute(Sender: TObject);
  777. begin
  778. uploadFile();
  779. //serialUpload;
  780. end;
  781. procedure TForm1.btnToneClick(Sender: TObject);
  782. begin
  783. MidiOutput.SendAllSoundOff(0, 0);
  784. end;
  785. function TForm1.serialUpload: string;
  786. var
  787. hexFile: string;
  788. begin
  789. hexFile := MCSIO.CreateUniqueFile(MCSIO.GetTempDir, 'TPS', '.hex');
  790. makeHexFile(hexFile);
  791. Result := hexFile;
  792. end;
  793. procedure TForm1.uploadFile;
  794. var
  795. line: string;
  796. comServices: TStringList;
  797. comService: string;
  798. return: integer;
  799. hexFile: string;
  800. hexFormat: TIntelHexFormat;
  801. prgMem: array of byte;
  802. i, x: integer;
  803. com, Data: byte;
  804. tmp: string;
  805. arduinoPath: string;
  806. cmd, config, mcu: string;
  807. KeyName, StringValue: string;
  808. Res: WideString;
  809. Lines: TStringList;
  810. TimeOut: integer;
  811. error: boolean;
  812. begin
  813. error := False;
  814. line := GetSerialPortNames;
  815. // if (line <> '') then
  816. begin
  817. comServices := TStringList.Create;
  818. MCSStrings.DelimTextToStringlist(line, ',', comServices);
  819. return := mrOk;
  820. line := XMLPropStorage1.ReadString('ComPort', comServices[0]);
  821. frmSelectCom := TfrmSelectCom.Create(self);
  822. frmSelectCom.cbServices.Items.AddStrings(comServices);
  823. frmSelectCom.cbServices.Text := line;
  824. return := frmSelectCom.ShowModal;
  825. comService := frmSelectCom.cbServices.Text;
  826. XMLPropStorage1.WriteString('ComPort', comService);
  827. comServices.Free;
  828. if (return = mrOk) then
  829. begin
  830. hexFile := serialUpload;
  831. SdpoSerial1.Device := comService;
  832. SdpoSerial1.Active := True;
  833. if (not readString(line)) then
  834. begin
  835. error := True;
  836. end;
  837. if (not error) then
  838. begin
  839. SdpoSerial1.WriteData('w');
  840. if (readString(line)) then
  841. begin
  842. if (pos('ready', line) > 0) then
  843. begin
  844. Lines := TStringList.Create;
  845. Lines.LoadFromFile(hexFile);
  846. for x := 0 to Lines.Count - 1 do
  847. begin
  848. SdpoSerial1.WriteData(Lines.Strings[x]);
  849. SdpoSerial1.WriteData(CRLF);
  850. Sleep(250);
  851. end;
  852. SdpoSerial1.WriteData('e');
  853. end
  854. else
  855. begin
  856. error := True;
  857. end;
  858. end;
  859. end;
  860. if (error) then
  861. begin
  862. Application.MessageBox(
  863. 'Arduino antwortet nicht. Evtl. Arduino nicht angeschlossen oder falsche Firmware?',
  864. 'Keine Antwort',
  865. MB_OK + MB_ICONEXCLAMATION);
  866. end;
  867. SdpoSerial1.Active := False;
  868. DeleteFile(hexFile);
  869. end;
  870. end;
  871. end;
  872. function TForm1.readString(var line: string): boolean;
  873. var
  874. TimeOut: integer;
  875. begin
  876. Result := False;
  877. TimeOut := 10;
  878. while ((not SdpoSerial1.DataAvailable) and (TimeOut > 0)) do
  879. begin
  880. Dec(TimeOut);
  881. Sleep(1000);
  882. end;
  883. if (Timeout > 0) then
  884. begin
  885. line := SdpoSerial1.ReadData;
  886. Result := True;
  887. end;
  888. end;
  889. procedure TForm1.setDirty(Value: boolean);
  890. begin
  891. if (dirty <> Value) then
  892. begin
  893. dirty := Value;
  894. if (dirty) then
  895. begin
  896. if (Pos('*', Caption) = 0) then
  897. begin
  898. Caption := Caption + '*';
  899. end;
  900. end
  901. else
  902. begin
  903. if (Pos('*', Caption) > 0) then
  904. begin
  905. Caption := Leftstr(Caption, Pos('*', Caption) - 1);
  906. end;
  907. end;
  908. end;
  909. end;
  910. function TForm1.checkDirty: boolean;
  911. var
  912. i: integer;
  913. begin
  914. if (dirty) then
  915. begin
  916. i := MCSLSU.LSUAutoMsgBox('Messages', 'SAVE_CHANGES', MB_ICONQUESTION or
  917. MB_YESNOCANCEL);
  918. if (i = mrYes) then
  919. begin
  920. saveFile(activeFile);
  921. Result := True;
  922. end
  923. else if (i = mrNo) then
  924. begin
  925. setDirty(False);
  926. Result := True;
  927. end
  928. else
  929. begin
  930. Result := False;
  931. end;
  932. end
  933. else
  934. begin
  935. Result := True;
  936. end;
  937. end;
  938. procedure TForm1.checkPresets;
  939. begin
  940. if XMLPropStorage1.ReadBoolean('preset_1.set', False) then
  941. tbPreset1.Caption := '1*'
  942. else
  943. tbPreset1.Caption := '1';
  944. if XMLPropStorage1.ReadBoolean('preset_2.set', False) then
  945. tbPreset2.Caption := '2*'
  946. else
  947. tbPreset2.Caption := '2';
  948. if XMLPropStorage1.ReadBoolean('preset_3.set', False) then
  949. tbPreset3.Caption := '3*'
  950. else
  951. tbPreset3.Caption := '3';
  952. if XMLPropStorage1.ReadBoolean('preset_4.set', False) then
  953. tbPreset4.Caption := '4*'
  954. else
  955. tbPreset4.Caption := '4';
  956. if XMLPropStorage1.ReadBoolean('preset_5.set', False) then
  957. tbPreset5.Caption := '5*'
  958. else
  959. tbPreset5.Caption := '5';
  960. if XMLPropStorage1.ReadBoolean('preset_6.set', False) then
  961. tbPreset6.Caption := '6*'
  962. else
  963. tbPreset6.Caption := '6';
  964. if XMLPropStorage1.ReadBoolean('preset_7.set', False) then
  965. tbPreset7.Caption := '7*'
  966. else
  967. tbPreset7.Caption := '7';
  968. if XMLPropStorage1.ReadBoolean('preset_8.set', False) then
  969. tbPreset7.Caption := '8*'
  970. else
  971. tbPreset7.Caption := '8';
  972. end;
  973. procedure TForm1.makeHexFile(fileName: string);
  974. var
  975. i, x: integer;
  976. tmp: string;
  977. hexFormat: TIntelHexFormat;
  978. prgMem: array of byte;
  979. com, Data: byte;
  980. begin
  981. i := StringGrid1.RowCount;
  982. SetLength(prgMem, i);
  983. for x := 1 to i - 1 do
  984. begin
  985. com := 0;
  986. Data := 0;
  987. tmp := StringGrid1.Cells[1, x];
  988. if (tmp <> '') then
  989. begin
  990. com := HexToInt(tmp);
  991. tmp := StringGrid1.Cells[2, x];
  992. if (tmp <> '') then
  993. Data := HexToInt(tmp);
  994. Data := com * 16 + Data;
  995. end;
  996. prgMem[x - 1] := Data;
  997. end;
  998. hexFormat := TIntelHexFormat.Create(prgMem, 8);
  999. tmp := hexFormat.Text;
  1000. MCSIO.StrToFile(fileName, tmp);
  1001. hexFormat.Free;
  1002. SetLength(prgMem, 0);
  1003. end;
  1004. procedure TForm1.setCaption;
  1005. begin
  1006. if (activeFile = '') then
  1007. begin
  1008. Caption := MCSLSU.GetLSUText('form1Captions', 'ID_CAPTION', lsuCode);
  1009. end
  1010. else
  1011. begin
  1012. Caption := MCSLSU.GetLSUText('form1Captions', 'ID_CAPTION', lsuCode) +
  1013. ':' + ExtractFileName(activeFile);
  1014. end;
  1015. end;
  1016. procedure TForm1.addHeaderText;
  1017. var
  1018. i: integer;
  1019. begin
  1020. for i := 0 to StringGrid1.Columns.Count - 1 do
  1021. begin
  1022. ;
  1023. StringGrid1.Columns[i].Title.Caption :=
  1024. MCSLSU.GetLSUText('form1Captions', StringGrid1.Columns[i].Title.Caption, lsuCode);
  1025. end;
  1026. StringGrid1.Cells[0, 0] := MCSLSU.GetLSUText('form1Captions',
  1027. 'ID_GRID_STORAGE', lsuCode);
  1028. StringGrid1.Repaint;
  1029. end;
  1030. procedure TForm1.cbCommandChange(Sender: TObject);
  1031. var
  1032. x: integer;
  1033. begin
  1034. cbData.Items.Clear;
  1035. x := cbCommand.ItemIndex;
  1036. sps.getDatas(x, cbData.Items);
  1037. if StringGrid1.Row > 0 then
  1038. begin
  1039. StringGrid1.Cells[1, StringGrid1.Row] := IntToHex(x, 1);
  1040. end;
  1041. end;
  1042. procedure TForm1.cbDataChange(Sender: TObject);
  1043. var
  1044. x: integer;
  1045. begin
  1046. x := cbData.ItemIndex;
  1047. if StringGrid1.Row > 0 then
  1048. begin
  1049. StringGrid1.Cells[2, StringGrid1.Row] := IntToHex(x, 1);
  1050. StringGrid1.Cells[3, StringGrid1.Row] := sps.getCommandText(cbCommand.ItemIndex, x);
  1051. end;
  1052. end;
  1053. procedure TForm1.cbTPSVersionChange(Sender: TObject);
  1054. begin
  1055. if (cbTPSVersion.ItemIndex = 0) then
  1056. begin
  1057. // HOLTEC
  1058. sps.setTPSVersion(Holtek);
  1059. Label2.Visible := True;
  1060. ADC2.Visible := True;
  1061. PWM2.Visible := False;
  1062. Label16.Visible := False;
  1063. RC1.Visible := False;
  1064. Label17.Visible := False;
  1065. RC2.Visible := False;
  1066. Servo1.Visible := False;
  1067. Servo2.Visible := False;
  1068. Shape1.Visible := False;
  1069. Shape2.Visible := False;
  1070. EditE.Visible := False;
  1071. EditF.Visible := False;
  1072. acUpload.Enabled := False;
  1073. Label5.Visible := False;
  1074. btnTone.Visible := False;
  1075. end;
  1076. if (cbTPSVersion.ItemIndex = 1) then
  1077. begin
  1078. // ATMega8
  1079. sps.setTPSVersion(ATMega8);
  1080. Label2.Visible := True;
  1081. ADC2.Visible := True;
  1082. PWM2.Visible := True;
  1083. Label16.Visible := False;
  1084. RC1.Visible := False;
  1085. Label17.Visible := False;
  1086. RC2.Visible := False;
  1087. Servo1.Visible := False;
  1088. Servo2.Visible := False;
  1089. Shape1.Visible := False;
  1090. Shape2.Visible := False;
  1091. EditE.Visible := False;
  1092. EditF.Visible := False;
  1093. acUpload.Enabled := False;
  1094. Label5.Visible := False;
  1095. btnTone.Visible := False;
  1096. end;
  1097. if ((cbTPSVersion.ItemIndex = 2) or (cbTPSVersion.ItemIndex = 3)) then
  1098. begin
  1099. if (cbTPSVersion.ItemIndex = 2) then
  1100. begin
  1101. // ATMega84
  1102. sps.setTPSVersion(ATTiny84);
  1103. acUpload.Enabled := False;
  1104. Label5.Visible := False;
  1105. btnTone.Visible := False;
  1106. end
  1107. else
  1108. begin
  1109. // Arduino 328
  1110. sps.setTPSVersion(Arduino);
  1111. acUpload.Enabled := True;
  1112. Label5.Visible := True;
  1113. btnTone.Visible := True;
  1114. end;
  1115. Label2.Visible := True;
  1116. ADC2.Visible := True;
  1117. PWM2.Visible := True;
  1118. Label16.Visible := True;
  1119. RC1.Visible := True;
  1120. Label17.Visible := True;
  1121. RC2.Visible := True;
  1122. Servo1.Visible := True;
  1123. Servo2.Visible := True;
  1124. Shape1.Visible := True;
  1125. Shape2.Visible := True;
  1126. EditE.Visible := True;
  1127. EditF.Visible := True;
  1128. end;
  1129. cbCommand.Items.Clear;
  1130. sps.getCommands(cbCommand.Items);
  1131. end;
  1132. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  1133. begin
  1134. canClose := checkDirty();
  1135. end;
  1136. procedure TForm1.StringGrid1EditingDone(Sender: TObject);
  1137. var
  1138. Value: string;
  1139. x: integer;
  1140. begin
  1141. // die aktuelle Zeile ist auch die letzte
  1142. if ((StringGrid1.Row + 1) = StringGrid1.RowCount) then
  1143. begin
  1144. // es wurde auch was eingegeben
  1145. Value := StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row];
  1146. Value := trim(Value);
  1147. if (Value <> '') then
  1148. begin
  1149. setDirty(True);
  1150. StringGrid1.RowCount := StringGrid1.RowCount + 1;
  1151. renumberGrid();
  1152. end;
  1153. end
  1154. else
  1155. begin
  1156. x := StringGrid1.Row;
  1157. StringGrid1.Cells[3, x] :=
  1158. sps.getCommandText(HexToInt(StringGrid1.Cells[1, x]),
  1159. HexToInt(StringGrid1.Cells[2, x]));
  1160. setDirty(True);
  1161. end;
  1162. end;
  1163. procedure TForm1.StringGrid1Selection(Sender: TObject; aCol, aRow: integer);
  1164. begin
  1165. try
  1166. cbCommand.ItemIndex := HexToInt(StringGrid1.Cells[1, aRow]);
  1167. cbCommandChange(Sender);
  1168. cbData.ItemIndex := HexToInt(StringGrid1.Cells[2, aRow]);
  1169. except
  1170. end;
  1171. end;
  1172. procedure TForm1.loadPreset(filename: string);
  1173. var
  1174. x: integer;
  1175. key: string;
  1176. begin
  1177. for x := 1 to 8 do
  1178. begin
  1179. key := 'preset_' + IntToStr(x);
  1180. loadSection(filename, key);
  1181. end;
  1182. checkPresets;
  1183. end;
  1184. procedure TForm1.savePreset(filename: string);
  1185. var
  1186. x: integer;
  1187. key: string;
  1188. begin
  1189. for x := 1 to 8 do
  1190. begin
  1191. key := 'preset_' + IntToStr(x);
  1192. saveSection(filename, key);
  1193. end;
  1194. end;
  1195. procedure TForm1.activateSps(enable: boolean);
  1196. begin
  1197. GBControl.Enabled := enable;
  1198. GBInternal.Enabled := enable;
  1199. GBOutput.Enabled := enable;
  1200. ;
  1201. end;
  1202. procedure TForm1.saveSection(filename: string; key: string);
  1203. begin
  1204. WriteIniBool(key, 'set', XMLPropStorage1.ReadBoolean(key + '.set', False), filename);
  1205. WriteIniBool(key, 'prg', XMLPropStorage1.ReadBoolean(key + '.prg', False), filename);
  1206. WriteIniBool(key, 'sel', XMLPropStorage1.ReadBoolean(key + '.sel', False), filename);
  1207. WriteIniBool(key, 'input1', XMLPropStorage1.ReadBoolean(key + '.input1', False),
  1208. filename);
  1209. WriteIniBool(key, 'input2', XMLPropStorage1.ReadBoolean(key + '.input2', False),
  1210. filename);
  1211. WriteIniBool(key, 'input3', XMLPropStorage1.ReadBoolean(key + '.input3', False),
  1212. filename);
  1213. WriteIniBool(key, 'input4', XMLPropStorage1.ReadBoolean(key + '.input4', False),
  1214. filename);
  1215. WriteIniInteger(key, 'adc1', XMLPropStorage1.ReadInteger(key + '.adc1', 0), filename);
  1216. WriteIniInteger(key, 'adc2', XMLPropStorage1.ReadInteger(key + '.adc2', 0), filename);
  1217. WriteIniInteger(key, 'rc1', XMLPropStorage1.ReadInteger(key + '.rc1', 0), filename);
  1218. WriteIniInteger(key, 'rc2', XMLPropStorage1.ReadInteger(key + '.rc2', 0), filename);
  1219. end;
  1220. procedure TForm1.loadSection(filename: string; key: string);
  1221. begin
  1222. XMLPropStorage1.WriteBoolean(key + '.set', ReadIniBool(key, 'set', False, filename));
  1223. XMLPropStorage1.WriteBoolean(key + '.prg', ReadIniBool(key, 'prg', False, filename));
  1224. XMLPropStorage1.WriteBoolean(key + '.sel', ReadIniBool(key, 'sel', False, filename));
  1225. XMLPropStorage1.WriteBoolean(key + '.input1', ReadIniBool(key,
  1226. 'input1', False, filename));
  1227. XMLPropStorage1.WriteBoolean(key + '.input2', ReadIniBool(key,
  1228. 'input2', False, filename));
  1229. XMLPropStorage1.WriteBoolean(key + '.input3', ReadIniBool(key,
  1230. 'input3', False, filename));
  1231. XMLPropStorage1.WriteBoolean(key + '.input4', ReadIniBool(key,
  1232. 'input4', False, filename));
  1233. XMLPropStorage1.WriteInteger(key + '.adc1', ReadIniInteger(key, 'adc1', 0, filename));
  1234. XMLPropStorage1.WriteInteger(key + '.adc2', ReadIniInteger(key, 'adc2', 0, filename));
  1235. XMLPropStorage1.WriteInteger(key + '.rc1', ReadIniInteger(key, 'rc1', 0, filename));
  1236. XMLPropStorage1.WriteInteger(key + '.rc2', ReadIniInteger(key, 'rc2', 0, filename));
  1237. end;
  1238. procedure TForm1.tbPreset1Click(Sender: TObject);
  1239. var
  1240. key: string;
  1241. begin
  1242. if Sender = tbPreset1 then
  1243. key := 'preset_1'
  1244. else
  1245. if Sender = tbPreset2 then
  1246. key := 'preset_2'
  1247. else
  1248. if Sender = tbPreset3 then
  1249. key := 'preset_3'
  1250. else
  1251. if Sender = tbPreset4 then
  1252. key := 'preset_4'
  1253. else
  1254. if Sender = tbPreset5 then
  1255. key := 'preset_5'
  1256. else
  1257. if Sender = tbPreset6 then
  1258. key := 'preset_6'
  1259. else
  1260. if Sender = tbPreset7 then
  1261. key := 'preset_7'
  1262. else
  1263. if Sender = tbPreset8 then
  1264. key := 'preset_8';
  1265. tbSel.Checked := XMLPropStorage1.ReadBoolean(key + '.prg', tbSel.Checked);
  1266. tbPrg.Checked := XMLPropStorage1.ReadBoolean(key + '.sel', tbPrg.Checked);
  1267. Din1.Checked := XMLPropStorage1.ReadBoolean(key + '.input1', Din1.Checked);
  1268. Din2.Checked := XMLPropStorage1.ReadBoolean(key + '.input2', Din2.Checked);
  1269. Din3.Checked := XMLPropStorage1.ReadBoolean(key + '.input3', Din3.Checked);
  1270. Din4.Checked := XMLPropStorage1.ReadBoolean(key + '.input4', Din4.Checked);
  1271. ADC1.Value := XMLPropStorage1.ReadInteger(key + '.adc1', ADC1.Value);
  1272. ADC2.Value := XMLPropStorage1.ReadInteger(key + '.adc2', ADC2.Value);
  1273. RC1.Value := XMLPropStorage1.ReadInteger(key + '.rc1', RC1.Value);
  1274. RC2.Value := XMLPropStorage1.ReadInteger(key + '.rc2', RC2.Value);
  1275. end;
  1276. procedure TForm1.tbPreset1ContextPopup(Sender: TObject; MousePos: TPoint;
  1277. var Handled: boolean);
  1278. var
  1279. key: string;
  1280. begin
  1281. if Sender = tbPreset1 then
  1282. key := 'preset_1'
  1283. else
  1284. if Sender = tbPreset2 then
  1285. key := 'preset_2'
  1286. else
  1287. if Sender = tbPreset3 then
  1288. key := 'preset_3'
  1289. else
  1290. if Sender = tbPreset4 then
  1291. key := 'preset_4'
  1292. else
  1293. if Sender = tbPreset5 then
  1294. key := 'preset_5'
  1295. else
  1296. if Sender = tbPreset6 then
  1297. key := 'preset_6'
  1298. else
  1299. if Sender = tbPreset7 then
  1300. key := 'preset_7'
  1301. else
  1302. if Sender = tbPreset8 then
  1303. key := 'preset_8';
  1304. XMLPropStorage1.WriteBoolean(key + '.set', True);
  1305. XMLPropStorage1.WriteBoolean(key + '.prg', tbPrg.Checked);
  1306. XMLPropStorage1.WriteBoolean(key + '.sel', tbSel.Checked);
  1307. XMLPropStorage1.WriteBoolean(key + '.input1', Din1.Checked);
  1308. XMLPropStorage1.WriteBoolean(key + '.input2', Din2.Checked);
  1309. XMLPropStorage1.WriteBoolean(key + '.input3', Din3.Checked);
  1310. XMLPropStorage1.WriteBoolean(key + '.input4', Din4.Checked);
  1311. XMLPropStorage1.WriteInteger(key + '.adc1', ADC1.Value);
  1312. XMLPropStorage1.WriteInteger(key + '.adc2', ADC2.Value);
  1313. XMLPropStorage1.WriteInteger(key + '.rc1', RC1.Value);
  1314. XMLPropStorage1.WriteInteger(key + '.rc2', RC2.Value);
  1315. checkPresets();
  1316. end;
  1317. procedure TForm1.tbResetClick(Sender: TObject);
  1318. begin
  1319. tbPrg.Checked := False;
  1320. tbSel.Checked := False;
  1321. Din1.Checked := False;
  1322. Din2.Checked := False;
  1323. Din3.Checked := False;
  1324. Din4.Checked := False;
  1325. ADC1.Value := 0;
  1326. ADC2.Value := 0;
  1327. RC1.Value := 8;
  1328. RC2.Value := 8;
  1329. end;
  1330. procedure TForm1.Timer1Timer(Sender: TObject);
  1331. var
  1332. jsonString: string;
  1333. Data: TJSONData;
  1334. iNetVersion, version: string;
  1335. thisVersion, iVersion: TVersionRecord;
  1336. begin
  1337. MCSLabel.AutoSize := True;
  1338. Timer1.Enabled := False;
  1339. MCSLabel.Hint := InfoBox.versionHint;
  1340. if (InfoBox.newVersion) then
  1341. begin
  1342. MCSLabel.Font.Color := clred;
  1343. MCSLabel.Hint := InfoBox.versionHint + chr($0a) + chr($0d) +
  1344. MCSLSU.GetLSUText('form1Captions', 'ID_CLICK_HERE', lsuCode);
  1345. end;
  1346. MCSLabel.Caption := InfoBox.versionText;
  1347. end;
  1348. procedure TForm1.renumberGrid;
  1349. var
  1350. x, i: integer;
  1351. begin
  1352. i := StringGrid1.RowCount;
  1353. for x := 1 to i - 1 do
  1354. begin
  1355. StringGrid1.Cells[0, x] := '0x' + inttohex(x - 1, 2);
  1356. if (StringGrid1.Cells[1, x] = '') then
  1357. StringGrid1.Cells[1, x] := '0';
  1358. if (StringGrid1.Cells[2, x] = '') then
  1359. StringGrid1.Cells[2, x] := '0';
  1360. StringGrid1.Cells[3, x] :=
  1361. sps.getCommandText(HexToInt(StringGrid1.Cells[1, x]),
  1362. HexToInt(StringGrid1.Cells[2, x]));
  1363. end;
  1364. end;
  1365. procedure TForm1.selectAddress(addr: byte);
  1366. begin
  1367. StringGrid1.Row := addr + 1;
  1368. Application.ProcessMessages;
  1369. end;
  1370. procedure TForm1.inputSps;
  1371. begin
  1372. sps.setDin1(Din1.Checked);
  1373. sps.setDin2(Din2.Checked);
  1374. sps.setDin3(Din3.Checked);
  1375. sps.setDin4(Din4.Checked);
  1376. sps.setADC1(ADC1.Value);
  1377. sps.setADC2(ADC2.Value);
  1378. sps.setRC1(RC1.Value);
  1379. sps.setRC2(RC2.Value);
  1380. sps.setSPrg(tbPrg.Checked);
  1381. sps.setSSel(tbSel.Checked);
  1382. end;
  1383. procedure TForm1.outputSps;
  1384. var
  1385. List: TStrings;
  1386. i: integer;
  1387. begin
  1388. if sps.isDout1() then
  1389. ShapeOut1.Brush.Color := clRed
  1390. else
  1391. ShapeOut1.Brush.Color := clWhite;
  1392. if sps.isDout2() then
  1393. ShapeOut2.Brush.Color := clRed
  1394. else
  1395. ShapeOut2.Brush.Color := clWhite;
  1396. if sps.isDout3() then
  1397. ShapeOut3.Brush.Color := clRed
  1398. else
  1399. ShapeOut3.Brush.Color := clWhite;
  1400. if sps.isDout4() then
  1401. ShapeOut4.Brush.Color := clRed
  1402. else
  1403. ShapeOut4.Brush.Color := clWhite;
  1404. PWM1.Text := IntToStr(sps.getPWM1());
  1405. PWM2.Text := IntToStr(sps.getPWM2());
  1406. Servo1.Text := IntToStr(sps.getServo1());
  1407. Shape1.Repaint;
  1408. Servo2.Text := IntToStr(sps.getServo2());
  1409. Shape2.Repaint;
  1410. EditA.Text := IntToStr(sps.getARegister());
  1411. EditB.Text := IntToStr(sps.getBRegister());
  1412. EditC.Text := IntToStr(sps.getCRegister());
  1413. EditD.Text := IntToStr(sps.getDRegister());
  1414. EditE.Text := IntToStr(sps.getERegister());
  1415. EditF.Text := IntToStr(sps.getFRegister());
  1416. EditAddr.Text := '0x' + IntToHex(sps.getAddress(), 2);
  1417. EditRAdr.Text := '0x' + IntToHex(sps.getRAdr(), 2);
  1418. EditPage.Text := '0x' + IntToHex(sps.getPage(), 2);
  1419. if (sps.getJump() > 0) then
  1420. EditJump.Text := '0x' + IntToHex(sps.getJump(), 2)
  1421. else
  1422. EditJump.Text := '';
  1423. if (sps.getTone() > 0) then
  1424. begin
  1425. ImageList2.GetBitmap(23, btnTone.Glyph);
  1426. i := round(440 * power(2.0, ((sps.getTone() - 69) / 12)));
  1427. btnTone.Caption := IntToStr(i);
  1428. if (lastNote <> sps.getTone()) then
  1429. begin
  1430. playNote(sps.getTone());
  1431. end;
  1432. btntone.Enabled := True;
  1433. end
  1434. else
  1435. begin
  1436. ImageList2.GetBitmap(22, btnTone.Glyph);
  1437. btnTone.Caption := '';
  1438. if (lastNote <> sps.getTone()) then
  1439. begin
  1440. playNote(0);
  1441. end;
  1442. btntone.Enabled := False;
  1443. end;
  1444. List := TStringList.Create;
  1445. try
  1446. sps.getStack(List);
  1447. lbStack.Clear;
  1448. for i := 0 to List.Count - 1 do
  1449. begin
  1450. lbStack.Items.Add(IntToStr(i) + ':' + List[i]);
  1451. end;
  1452. finally
  1453. List.Free;
  1454. end;
  1455. end;
  1456. end.