ugui.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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. acSaveAs: TAction;
  27. acUpload: TAction;
  28. ActionList1: TActionList;
  29. acFileOpen: TFileOpen;
  30. btnTone: TBitBtn;
  31. cbIgnDly: TCheckBox;
  32. cbTPSVersion: TComboBox;
  33. cbAdrActual: TCheckBox;
  34. EditDelay: TLabeledEdit;
  35. EditJump: TLabeledEdit;
  36. EditPage: TLabeledEdit;
  37. EditRAdr: TLabeledEdit;
  38. acPresetLoad: TFileOpen;
  39. acPresetSave: TFileSaveAs;
  40. GBControl: TGroupBox;
  41. ImageList2: TImageList;
  42. ImageList3: TImageList;
  43. Label3: TLabel;
  44. Label4: TLabel;
  45. Label5: TLabel;
  46. lbStack: TListBox;
  47. MCSLabel: TLabel;
  48. MenuItem1: TMenuItem;
  49. pmExamples: TPopupMenu;
  50. RC1: TSpinEdit;
  51. RC2: TSpinEdit;
  52. cbCommand: TComboBox;
  53. cbData: TComboBox;
  54. Din1: TCheckBox;
  55. Din2: TCheckBox;
  56. Din3: TCheckBox;
  57. Din4: TCheckBox;
  58. GBOutput: TGroupBox;
  59. GBInput: TGroupBox;
  60. GBInternal: TGroupBox;
  61. Label1: TLabel;
  62. Label10: TLabel;
  63. Label11: TLabel;
  64. Label12: TLabel;
  65. Label13: TLabel;
  66. Label14: TLabel;
  67. Label15: TLabel;
  68. Label16: TLabel;
  69. Label17: TLabel;
  70. Label2: TLabel;
  71. Panel2: TPanel;
  72. Panel3: TPanel;
  73. PWM1: TLabeledEdit;
  74. PWM2: TLabeledEdit;
  75. Panel1: TPanel;
  76. ADC1: TSpinEdit;
  77. ADC2: TSpinEdit;
  78. SaveDialog1: TSaveDialog;
  79. SaveHexFile: TSaveDialog;
  80. SaveBinFile: TSaveDialog;
  81. SdpoSerial1: TSdpoSerial;
  82. Servo1: TLabeledEdit;
  83. Servo2: TLabeledEdit;
  84. EditA: TLabeledEdit;
  85. EditC: TLabeledEdit;
  86. EditE: TLabeledEdit;
  87. EditB: TLabeledEdit;
  88. EditD: TLabeledEdit;
  89. EditF: TLabeledEdit;
  90. EditAddr: TLabeledEdit;
  91. Shape1: TShape;
  92. Shape2: TShape;
  93. ShapeOut1: TShape;
  94. ShapeOut2: TShape;
  95. ShapeOut3: TShape;
  96. ShapeOut4: TShape;
  97. SpeedButton1: TSpeedButton;
  98. StatusBar1: TStatusBar;
  99. StringGrid1: TStringGrid;
  100. tbPrg: TToggleBox;
  101. tbPreLoad: TToolButton;
  102. tbPreSave: TToolButton;
  103. tbSel: TToggleBox;
  104. Timer1: TTimer;
  105. ToolBar1: TToolBar;
  106. ToolBar2: TToolBar;
  107. ToolButton1: TToolButton;
  108. ToolButton10: TToolButton;
  109. ToolButton11: TToolButton;
  110. ToolButton12: TToolButton;
  111. ToolButton13: TToolButton;
  112. ToolButton14: TToolButton;
  113. ToolButton15: TToolButton;
  114. ToolButton16: TToolButton;
  115. ToolButton17: TToolButton;
  116. ToolButton18: TToolButton;
  117. tbPreset1: TToolButton;
  118. tbReset: TToolButton;
  119. ToolButton19: TToolButton;
  120. ToolButton2: TToolButton;
  121. tbPreset2: TToolButton;
  122. tbPreset3: TToolButton;
  123. tbPreset4: TToolButton;
  124. tbPreset5: TToolButton;
  125. tbPreset6: TToolButton;
  126. tbPreset7: TToolButton;
  127. tbPreset8: TToolButton;
  128. ToolButton20: TToolButton;
  129. ToolButton21: TToolButton;
  130. ToolButton3: TToolButton;
  131. ToolButton4: TToolButton;
  132. ToolButton5: TToolButton;
  133. ToolButton6: TToolButton;
  134. ToolButton7: TToolButton;
  135. ToolButton8: TToolButton;
  136. ToolButton9: TToolButton;
  137. TOpenDialogPreset: TOpenDialog;
  138. TSaveDialogPreset: TSaveDialog;
  139. XMLPropStorage1: TXMLPropStorage;
  140. procedure acDeleteRowExecute(Sender: TObject);
  141. procedure acExitExecute(Sender: TObject);
  142. procedure acFileOpenAccept(Sender: TObject);
  143. procedure acDebugExecute(Sender: TObject);
  144. procedure acHelpAboutExecute(Sender: TObject);
  145. procedure acHexFileExecute(Sender: TObject);
  146. procedure acNewExecute(Sender: TObject);
  147. procedure acNewRowExecute(Sender: TObject);
  148. procedure acNextInsertExecute(Sender: TObject);
  149. procedure acNextStepExecute(Sender: TObject);
  150. procedure acPresetLoadAccept(Sender: TObject);
  151. procedure acPresetSaveAccept(Sender: TObject);
  152. procedure acSaveAsExecute(Sender: TObject);
  153. procedure acShowHexFileExecute(Sender: TObject);
  154. procedure acStopExecute(Sender: TObject);
  155. procedure acThisStepExecute(Sender: TObject);
  156. procedure acFileSaveExecute(Sender: TObject);
  157. procedure acUploadExecute(Sender: TObject);
  158. procedure btnToneClick(Sender: TObject);
  159. procedure cbCommandChange(Sender: TObject);
  160. procedure cbDataChange(Sender: TObject);
  161. procedure cbIgnDlyChange(Sender: TObject);
  162. procedure cbTPSVersionChange(Sender: TObject);
  163. procedure FormActivate(Sender: TObject);
  164. procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
  165. procedure FormCreate(Sender: TObject);
  166. procedure FormDropFiles(Sender: TObject; const FileNames: array of string);
  167. procedure MCSLabelClick(Sender: TObject);
  168. procedure MenuItem1Click(Sender: TObject);
  169. procedure mnSaveClick(Sender: TObject);
  170. procedure pmExamplesPopup(Sender: TObject);
  171. procedure Shape1Paint(Sender: TObject);
  172. procedure Shape2Paint(Sender: TObject);
  173. procedure StringGrid1EditingDone(Sender: TObject);
  174. procedure StringGrid1Selection(Sender: TObject; aCol, aRow: integer);
  175. procedure tbPreset1Click(Sender: TObject);
  176. procedure tbPreset1ContextPopup(Sender: TObject; MousePos: TPoint;
  177. var Handled: boolean);
  178. procedure tbPrgChange(Sender: TObject);
  179. procedure tbResetClick(Sender: TObject);
  180. procedure tbSelChange(Sender: TObject);
  181. procedure Timer1Timer(Sender: TObject);
  182. procedure XMLPropStorage1RestoringProperties(Sender: TObject);
  183. procedure XMLPropStorage1SavingProperties(Sender: TObject);
  184. private
  185. { private declarations }
  186. sps: TSPS;
  187. stopit: boolean;
  188. activeFile: string;
  189. dirty: boolean;
  190. lastNote: byte;
  191. Examples: TStringList;
  192. procedure doStopExecute();
  193. procedure initMidi;
  194. procedure playNote(note: byte);
  195. procedure loadFromList(Lines: TStringList; filename: string);
  196. function readString(var line: string; wait: boolean): boolean;
  197. procedure saveSection(filename: string; key: string);
  198. procedure loadSection(filename: string; key: string);
  199. procedure loadFile(filename: string);
  200. procedure programSps;
  201. procedure nextStep(singleStep: boolean);
  202. procedure renumberGrid;
  203. procedure saveFile(filename: string);
  204. procedure selectAddress(addr: word);
  205. procedure inputSps;
  206. procedure outputSps;
  207. procedure uploadFile;
  208. function serialUpload: string;
  209. procedure setDirty(Value: boolean);
  210. function checkDirty: boolean;
  211. procedure checkPresets;
  212. procedure makeBinFile(fileName: string);
  213. procedure makeHexFile(fileName: string);
  214. procedure setCaption;
  215. procedure addHeaderText;
  216. procedure loadPreset(filename: string);
  217. procedure savePreset(filename: string);
  218. procedure activateSps(enable: boolean);
  219. procedure enableMicrobit(enable: boolean);
  220. procedure outputMicrobit();
  221. procedure inputMicrobit();
  222. public
  223. { public declarations }
  224. end;
  225. var
  226. Form1: TForm1;
  227. implementation
  228. uses MCSAbout, uTextUi, uSelectCom, MCSTools, MCSStrings, synaser,
  229. MCSIO, mcsintelhex, MCSLSU, MCSIniFiles, MCSWinHttp, luijsonutils,
  230. uiMicrobit, uMicrobit;
  231. {$R *.lfm}
  232. procedure delayCallback(Value: integer);
  233. begin
  234. Form1.EditDelay.Text := IntToStr(Value);
  235. Application.ProcessMessages;
  236. end;
  237. { TForm1 }
  238. var
  239. lsuCode: integer;
  240. procedure TForm1.FormCreate(Sender: TObject);
  241. var
  242. line: string;
  243. begin
  244. Infobox.AppTitel := MCSLSU.GetLSUText('infobox', 'ID_APPTITLE', lsuCode);
  245. Infobox.CopyRight := MCSLSU.GetLSUText('infobox', 'ID_COPYRIGHT', lsuCode);
  246. Infobox.Build := MCSGetVersion(Application.ExeName);
  247. Infobox.AppID := 31;
  248. Infobox.AppURL :=
  249. 'http://www.wk-music.de/willie/pages/mcs/microcontroller/tps-sps-emulator.php';
  250. sps := TSPS.Create();
  251. sps.setDelayCallback(@delayCallback);
  252. addHeaderText;
  253. renumberGrid();
  254. cbTPSVersion.ItemIndex := 0;
  255. cbTPSVersionChange(Sender);
  256. acStop.Enabled := False;
  257. ToolButton18.Align := alRight;
  258. MCSLabel.Align := alRight;
  259. cbAdrActual.Enabled := False;
  260. cbIgnDly.Enabled := False;
  261. checkPresets();
  262. tbResetClick(nil);
  263. if Application.ParamCount >= 1 then
  264. begin
  265. line := Application.Params[1];
  266. loadFile(line);
  267. end;
  268. activateSps(False);
  269. Timer1.Enabled := True;
  270. MCSLSU.MakeForm('form1', 'ID_', form1);
  271. initMidi;
  272. Examples := TStringList.Create;
  273. end;
  274. procedure TForm1.initMidi;
  275. var
  276. Value: integer;
  277. begin
  278. lastNote := 0;
  279. MidiOutput.Open(0);
  280. Value := XMLPropStorage1.ReadInteger('MidiProgram', 30);
  281. MidiOutput.Send(0, 192, Value, 0);
  282. end;
  283. procedure TForm1.playNote(note: byte);
  284. var
  285. command, velocity: byte;
  286. begin
  287. if (lastNote > 0) then
  288. begin
  289. command := $80;
  290. velocity := $7F;
  291. MidiOutput.Send(0, command, lastnote, velocity);
  292. end;
  293. if (note > 0) then
  294. begin
  295. command := $90;
  296. velocity := $7F;
  297. MidiOutput.Send(0, command, note, velocity);
  298. end;
  299. lastNote := note;
  300. end;
  301. procedure TForm1.loadFromList(Lines: TStringList; filename: string);
  302. var
  303. i, x: integer;
  304. line: string;
  305. list: TStringList;
  306. begin
  307. acNew.Execute;
  308. list := TStringList.Create;
  309. i := 1;
  310. for x := 0 to Lines.Count - 1 do
  311. begin
  312. if (i + 1 > StringGrid1.RowCount) then
  313. begin
  314. StringGrid1.RowCount := StringGrid1.RowCount + 1;
  315. end;
  316. line := Lines[x];
  317. if (Pos('#', line) = 1) then
  318. begin
  319. line := RightstrPos(line, 2);
  320. if (Pos('TPS:', line) = 1) then
  321. begin
  322. line := RightstrPos(line, 5);
  323. cbTPSVersion.Text := line;
  324. cbTPSVersionChange(nil);
  325. end;
  326. end
  327. else
  328. begin
  329. MCSStrings.DelimSepTextToStringlist(line, '"', ',', list);
  330. if list.Count > 0 then
  331. Stringgrid1.Cells[0, i] := list[0];
  332. if list.Count > 1 then
  333. Stringgrid1.Cells[1, i] := list[1];
  334. if list.Count > 2 then
  335. Stringgrid1.Cells[2, i] := list[2];
  336. if list.Count > 3 then
  337. Stringgrid1.Cells[4, i] := list[3];
  338. list.Clear;
  339. Inc(i);
  340. end;
  341. end;
  342. list.Free;
  343. activeFile := filename;
  344. renumberGrid();
  345. addHeaderText;
  346. setCaption();
  347. setDirty(False);
  348. end;
  349. procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string);
  350. var
  351. line: string;
  352. begin
  353. if (SizeOF(FileNames) > 0) then
  354. begin
  355. line := FileNames[0];
  356. loadFile(line);
  357. end;
  358. end;
  359. procedure TForm1.MCSLabelClick(Sender: TObject);
  360. begin
  361. ShExec2(self.Handle, InfoBox.AppURL);
  362. end;
  363. procedure TForm1.MenuItem1Click(Sender: TObject);
  364. var
  365. i: integer;
  366. fileName, Data: string;
  367. Lines: TStringList;
  368. begin
  369. if (Sender is TMenuItem) then
  370. begin
  371. if (checkDirty()) then
  372. begin
  373. i := TMenuItem(Sender).Tag;
  374. if (i >= 0) then
  375. begin
  376. filename := examples[i];
  377. Data := DownloadFile('http://wkla.no-ip.biz/down/tps_examples/' + filename);
  378. Lines := TStringList.Create;
  379. Lines.Text := Data;
  380. loadFromList(Lines, filename);
  381. Lines.Free;
  382. end;
  383. end;
  384. end;
  385. end;
  386. procedure TForm1.mnSaveClick(Sender: TObject);
  387. begin
  388. Statusbar1.SimpleText := Sender.ClassName;
  389. end;
  390. procedure TForm1.pmExamplesPopup(Sender: TObject);
  391. var
  392. x, i, j, pos, index: integer;
  393. myName: string;
  394. myMenu, my2Menu: TMenuItem;
  395. jsonString: string;
  396. JsonObject: TJsonObject;
  397. Data, JArray: TJSONData;
  398. JItem: TJSONData;
  399. subMenuName: string;
  400. subMenu: TMenuItem;
  401. begin
  402. for i := pmExamples.Items.Count - 1 downto 0 do
  403. begin
  404. myMenu := pmExamples.Items[i];
  405. if (myMenu.Count > 0) then
  406. begin
  407. for j := myMenu.Count - 1 downto 0 do
  408. begin
  409. my2Menu := myMenu.Items[j];
  410. my2Menu.Free;
  411. end;
  412. myMenu.Clear;
  413. end;
  414. myMenu.Free;
  415. end;
  416. pmExamples.Items.Clear;
  417. Examples.Clear;
  418. try
  419. jsonString := DownloadFile('http://wkla.no-ip.biz/down/tps_examples/examples.json');
  420. Data := StringToJSONData(jsonString);
  421. JsonObject := TJSONObject(Data);
  422. i := JsonObject.Count;
  423. for x := 0 to JsonObject.Count - 1 do
  424. begin
  425. subMenuName := JsonObject.Names[x];
  426. JArray := GetJSONProp(TJSONObject(Data), subMenuName);
  427. if (JArray <> nil) then
  428. begin
  429. subMenu := TMenuItem.Create(pmExamples);
  430. subMenu.Caption := subMenuName;
  431. pmExamples.Items.Add(subMenu);
  432. for i := 1 to TJSONArray(JArray).Count do
  433. begin
  434. JItem := TJSONArray(JArray).Items[i - 1];
  435. myName := GetJsonProp(TJSONObject(JItem), 'name', '');
  436. pos := -1;
  437. index := GetJsonProp(TJSONObject(JItem), 'index', -1);
  438. if (index > -1) then
  439. begin
  440. pos := Examples.Add(GetJsonProp(TJSONObject(JItem), 'file', ''));
  441. myName := format('%.2d - %s', [index, myName]);
  442. end;
  443. myMenu := TMenuItem.Create(subMenu);
  444. myMenu.Caption := myName;
  445. myMenu.Tag := pos;
  446. if (index > -1) then
  447. begin
  448. myMenu.OnClick := @MenuItem1Click;
  449. end;
  450. subMenu.Add(myMenu);
  451. end;
  452. end;
  453. end;
  454. except
  455. end;
  456. end;
  457. procedure TForm1.Shape1Paint(Sender: TObject);
  458. var
  459. radius: integer;
  460. x, y: integer;
  461. begin
  462. radius := round(Shape1.Width / 2);
  463. Shape1.Canvas.Brush.Color := clBlack;
  464. Shape1.canvas.MoveTo(radius, radius);
  465. x := radius - round(radius * cos(degtorad(sps.getServo1())));
  466. y := radius - round(radius * sin(degtorad(sps.getServo1())));
  467. Shape1.Canvas.LineTo(x, y);
  468. end;
  469. procedure TForm1.Shape2Paint(Sender: TObject);
  470. var
  471. radius: integer;
  472. x, y: integer;
  473. begin
  474. radius := round(Shape1.Width / 2);
  475. Shape2.Canvas.Brush.Color := clBlack;
  476. Shape2.canvas.MoveTo(radius, radius);
  477. x := radius - round(radius * cos(degtorad(sps.getServo2())));
  478. y := radius - round(radius * sin(degtorad(sps.getServo2())));
  479. Shape2.Canvas.LineTo(x, y);
  480. end;
  481. procedure TForm1.acExitExecute(Sender: TObject);
  482. begin
  483. Close;
  484. end;
  485. procedure TForm1.acDeleteRowExecute(Sender: TObject);
  486. var
  487. i: integer;
  488. begin
  489. i := StringGrid1.Row;
  490. StringGrid1.DeleteRow(i);
  491. renumberGrid();
  492. end;
  493. procedure TForm1.acFileOpenAccept(Sender: TObject);
  494. var
  495. filename: string;
  496. begin
  497. filename := (Sender as TFileOpen).Dialog.FileName;
  498. loadFile(filename);
  499. end;
  500. procedure TForm1.loadFile(filename: string);
  501. var
  502. i: integer;
  503. f: Text;
  504. line: string;
  505. list: TStringList;
  506. begin
  507. if (checkDirty()) then
  508. begin
  509. if (FileExists(filename)) then
  510. begin
  511. acNew.Execute;
  512. list := TStringList.Create;
  513. i := 1;
  514. AssignFile(f, filename);
  515. Reset(f);
  516. while (not EOF(f)) do
  517. begin
  518. if (i + 1 > StringGrid1.RowCount) then
  519. begin
  520. StringGrid1.RowCount := StringGrid1.RowCount + 1;
  521. end;
  522. readln(f, line);
  523. if (Pos('#', line) = 1) then
  524. begin
  525. line := RightstrPos(line, 2);
  526. if (Pos('TPS:', line) = 1) then
  527. begin
  528. line := RightstrPos(line, 5);
  529. cbTPSVersion.Text := line;
  530. cbTPSVersionChange(nil);
  531. end;
  532. end
  533. else
  534. begin
  535. MCSStrings.DelimSepTextToStringlist(line, '"', ',', list);
  536. if list.Count > 0 then
  537. Stringgrid1.Cells[0, i] := list[0];
  538. if list.Count > 1 then
  539. Stringgrid1.Cells[1, i] := list[1];
  540. if list.Count > 2 then
  541. Stringgrid1.Cells[2, i] := list[2];
  542. if list.Count > 3 then
  543. Stringgrid1.Cells[4, i] := list[3];
  544. list.Clear;
  545. i := i + 1;
  546. end;
  547. end;
  548. CloseFile(f);
  549. list.Free;
  550. activeFile := filename;
  551. renumberGrid();
  552. addHeaderText;
  553. setCaption();
  554. setDirty(False);
  555. end;
  556. end;
  557. end;
  558. procedure TForm1.acSaveAsExecute(Sender: TObject);
  559. var
  560. filename: string;
  561. begin
  562. if SaveDialog1.Execute() then
  563. begin
  564. filename := SaveDialog1.FileName;
  565. saveFile(filename);
  566. end;
  567. end;
  568. procedure TForm1.saveFile(filename: string);
  569. var
  570. x, i: integer;
  571. f: Text;
  572. line: string;
  573. begin
  574. if (filename = '') then
  575. begin
  576. if SaveDialog1.Execute() then
  577. begin
  578. filename := SaveDialog1.FileName;
  579. end;
  580. end;
  581. if (filename <> '') then
  582. begin
  583. AssignFile(f, filename);
  584. Rewrite(f);
  585. line := '#TPS:' + cbTPSVersion.Text;
  586. Writeln(f, line);
  587. i := StringGrid1.RowCount;
  588. for x := 1 to i - 1 do
  589. begin
  590. if (StringGrid1.Cells[1, x] <> '') then
  591. begin
  592. line := StringGrid1.Cells[0, x] + ',' + StringGrid1.Cells[1, x] +
  593. ',' + StringGrid1.Cells[2, x] + ',"' + StringGrid1.Cells[4, x] + '"';
  594. Writeln(f, line);
  595. end;
  596. end;
  597. CloseFile(f);
  598. setDirty(False);
  599. activeFile := filename;
  600. setCaption();
  601. end;
  602. end;
  603. procedure TForm1.acDebugExecute(Sender: TObject);
  604. begin
  605. if (sps.isActive()) then
  606. begin
  607. acStopExecute(Sender);
  608. end
  609. else
  610. begin
  611. activateSps(True);
  612. acDebug.Enabled := True;
  613. acDebug.ImageIndex := 57;
  614. cbAdrActual.Enabled := True;
  615. cbIgnDly.Enabled := True;
  616. acNextStep.Enabled := False;
  617. // sps programmieren
  618. programSps();
  619. // programm starten
  620. Statusbar1.SimpleText := MCSLSU.GetLSUText('statusbar', 'ID_SPS_START', lsuCode);
  621. sps.start();
  622. stopit := False;
  623. Statusbar1.SimpleText := MCSLSU.GetLSUText('statusbar', 'ID_SPS_RUNNING', lsuCode);
  624. while (not stopit) do
  625. begin
  626. nextStep(False);
  627. if not sps.isActive() then
  628. begin
  629. stopit := True;
  630. doStopExecute(); // error in sps
  631. end;
  632. if (cbAdrActual.Checked) then
  633. selectAddress(sps.getAddress());
  634. Application.ProcessMessages;
  635. end;
  636. if sps.getLastError() <> '' then
  637. MyMsgBox(sps.getLastError(), 'Error', MB_OK + MB_ICONERROR)
  638. else
  639. Statusbar1.SimpleText := MCSLSU.GetLSUText('statusbar', 'ID_SPS_STOPPED', lsuCode);
  640. cbAdrActual.Enabled := False;
  641. acNextStep.Enabled := True;
  642. sps.doReset();
  643. end;
  644. end;
  645. procedure TForm1.acHelpAboutExecute(Sender: TObject);
  646. begin
  647. Infobox.Show;
  648. end;
  649. procedure TForm1.acHexFileExecute(Sender: TObject);
  650. var
  651. filename: string;
  652. begin
  653. filename := MCSIO.MCSExtractFileNameExlExt(activeFile) + '.hex';
  654. SaveHexFile.FileName := filename;
  655. if (SaveHexFile.Execute) then
  656. begin
  657. filename := SaveHexFile.FileName;
  658. makeHexFile(filename);
  659. end;
  660. end;
  661. procedure TForm1.acNewExecute(Sender: TObject);
  662. begin
  663. if (checkDirty()) then
  664. begin
  665. StringGrid1.RowCount := 2;
  666. StringGrid1.Clean;
  667. addHeaderText();
  668. renumberGrid();
  669. activeFile := '';
  670. setCaption();
  671. setDirty(False);
  672. end;
  673. end;
  674. procedure TForm1.acNewRowExecute(Sender: TObject);
  675. var
  676. myPos: integer;
  677. i, x: integer;
  678. eot: boolean;
  679. begin
  680. eot := False;
  681. i := StringGrid1.RowCount - 1;
  682. if (i >= (sps.getE2E())) then
  683. begin
  684. LSUAutoMsgBox('Messages', 'ID_PRG_TO_LONG', MB_OK + MB_ICONERROR);
  685. exit;
  686. end;
  687. myPos := StringGrid1.Row;
  688. if (myPos = StringGrid1.RowCount - 1) then
  689. eot := True;
  690. StringGrid1.RowCount := StringGrid1.RowCount + 1;
  691. if (not eot) then
  692. begin
  693. for i := StringGrid1.RowCount - 2 downto myPos do
  694. begin
  695. for x := 1 to StringGrid1.ColCount - 1 do
  696. begin
  697. StringGrid1.Cells[x, i + 1] := StringGrid1.Cells[x, i];
  698. end;
  699. end;
  700. for x := 1 to StringGrid1.ColCount - 1 do
  701. begin
  702. StringGrid1.Cells[x, myPos] := '';
  703. end;
  704. end;
  705. renumberGrid();
  706. end;
  707. procedure TForm1.acNextInsertExecute(Sender: TObject);
  708. var
  709. myPos: integer;
  710. i: integer;
  711. begin
  712. i := StringGrid1.RowCount - 1;
  713. if (i >= (sps.getE2E())) then
  714. begin
  715. LSUAutoMsgBox('Messages', 'ID_PRG_TO_LONG', MB_OK + MB_ICONERROR);
  716. exit;
  717. end;
  718. myPos := StringGrid1.Row;
  719. if (myPos = StringGrid1.RowCount - 1) then
  720. acNewRow.Execute;
  721. StringGrid1.Row := myPos + 1;
  722. renumberGrid();
  723. end;
  724. procedure TForm1.programSps;
  725. var
  726. x, i: integer;
  727. com, Data: byte;
  728. tmp: string;
  729. begin
  730. Statusbar1.SimpleText := MCSLSU.GetLSUText('statusbar', 'ID_SPS_PROGRAMMING', lsuCode);
  731. i := StringGrid1.RowCount;
  732. for x := 1 to i - 1 do
  733. begin
  734. com := 0;
  735. Data := 0;
  736. tmp := StringGrid1.Cells[1, x];
  737. if (tmp <> '') then
  738. begin
  739. com := HexToInt(tmp);
  740. tmp := StringGrid1.Cells[2, x];
  741. if (tmp <> '') then
  742. Data := HexToInt(tmp);
  743. Data := com * 16 + Data;
  744. end;
  745. sps.writeEEProm(x - 1, Data);
  746. end;
  747. // endekennzeichnung schreiben
  748. sps.writeEEProm(i, $ff);
  749. sps.setIgnoreDelay(cbIgnDly.Checked);
  750. end;
  751. procedure TForm1.acNextStepExecute(Sender: TObject);
  752. begin
  753. nextStep(True);
  754. selectAddress(sps.getAddress());
  755. end;
  756. procedure TForm1.acPresetLoadAccept(Sender: TObject);
  757. var
  758. filename: string;
  759. begin
  760. filename := (Sender as TFileOpen).Dialog.FileName;
  761. loadPreset(filename);
  762. end;
  763. procedure TForm1.acPresetSaveAccept(Sender: TObject);
  764. var
  765. filename: string;
  766. begin
  767. filename := (Sender as TFileSaveAs).Dialog.FileName;
  768. savePreset(filename);
  769. end;
  770. procedure TForm1.nextStep(singleStep: boolean);
  771. begin
  772. if (not sps.isActive()) then
  773. begin
  774. activateSps(True);
  775. programSps;
  776. sps.start();
  777. acStop.Enabled := True;
  778. acDebug.Enabled := False;
  779. end
  780. else
  781. begin
  782. acNextStep.Enabled := False;
  783. acStop.Enabled := True;
  784. inputMicrobit();
  785. inputSps();
  786. sps.nextStep();
  787. outputSps();
  788. outputMicrobit();
  789. acNextStep.Enabled := singleStep;
  790. acStop.Enabled := True;
  791. end;
  792. end;
  793. procedure TForm1.acShowHexFileExecute(Sender: TObject);
  794. var
  795. list: TStringList;
  796. i, x: integer;
  797. line, tmp: string;
  798. Value: byte;
  799. begin
  800. Form2 := TForm2.Create(self);
  801. list := TStringList.Create();
  802. line := MCSLSU.GetLSUText('hexfile', 'ID_START_LINE', lsuCode);
  803. list.add(line);
  804. i := StringGrid1.RowCount;
  805. for x := 1 to i - 1 do
  806. begin
  807. if (StringGrid1.Cells[1, x] <> '') then
  808. begin
  809. tmp := '';
  810. if (StringGrid1.Cells[1, x] = '') then
  811. begin
  812. tmp := ' ';
  813. end
  814. else
  815. begin
  816. tmp := StringGrid1.Cells[1, x];
  817. end;
  818. if (StringGrid1.Cells[2, x] = '') then
  819. begin
  820. tmp := tmp + ' ';
  821. end
  822. else
  823. begin
  824. tmp := tmp + StringGrid1.Cells[2, x];
  825. end;
  826. line := StringGrid1.Cells[0, x] + ': ' + tmp + ' ';
  827. Value := HexToInt(StringGrid1.Cells[1, x]);
  828. tmp := '';
  829. if (Value and 8) > 0 then
  830. tmp := tmp + 'X'
  831. else
  832. tmp := tmp + '0';
  833. if (Value and 4) > 0 then
  834. tmp := tmp + 'X'
  835. else
  836. tmp := tmp + '0';
  837. if (Value and 2) > 0 then
  838. tmp := tmp + 'X'
  839. else
  840. tmp := tmp + '0';
  841. if (Value and 1) > 0 then
  842. tmp := tmp + 'X'
  843. else
  844. tmp := tmp + '0';
  845. line := line + tmp + ' ';
  846. Value := HexToInt(StringGrid1.Cells[2, x]);
  847. tmp := '';
  848. if (Value and 8) > 0 then
  849. tmp := tmp + 'X'
  850. else
  851. tmp := tmp + '0';
  852. if (Value and 4) > 0 then
  853. tmp := tmp + 'X'
  854. else
  855. tmp := tmp + '0';
  856. if (Value and 2) > 0 then
  857. tmp := tmp + 'X'
  858. else
  859. tmp := tmp + '0';
  860. if (Value and 1) > 0 then
  861. tmp := tmp + 'X'
  862. else
  863. tmp := tmp + '0';
  864. line := line + tmp;
  865. line := line + ' ' + StringGrid1.Cells[3, x] + ' ,"' +
  866. StringGrid1.Cells[4, x] + '"';
  867. list.add(line);
  868. end;
  869. end;
  870. Form2.addHexFile(list);
  871. Form2.ShowModal;
  872. Form2.Destroy;
  873. list.Free;
  874. end;
  875. procedure TForm1.acStopExecute(Sender: TObject);
  876. begin
  877. if (sps.isActive()) then
  878. begin
  879. stopit := True;
  880. sps.break();
  881. doStopExecute();
  882. end;
  883. end;
  884. procedure TForm1.doStopExecute();
  885. begin
  886. acDebug.ImageIndex := 40;
  887. // repeat
  888. // Application.ProcessMessages;
  889. // until (not sps.isDelayActive());
  890. sps.doReset();
  891. acStop.Enabled := False;
  892. acDebug.Enabled := True;
  893. outputSps();
  894. selectAddress(0);
  895. activateSps(False);
  896. end;
  897. procedure TForm1.acThisStepExecute(Sender: TObject);
  898. var
  899. Data, com: byte;
  900. tmp: string;
  901. begin
  902. inputSps();
  903. try
  904. tmp := StringGrid1.Cells[1, StringGrid1.Row];
  905. if (tmp <> '') then
  906. begin
  907. com := HexToInt(tmp);
  908. tmp := StringGrid1.Cells[2, StringGrid1.Row];
  909. if (tmp <> '') then
  910. Data := HexToInt(tmp);
  911. Data := com * 16 + Data;
  912. end;
  913. sps.doSingleCommand(Data);
  914. except
  915. end;
  916. outputSps();
  917. end;
  918. procedure TForm1.acFileSaveExecute(Sender: TObject);
  919. begin
  920. saveFile(activeFile);
  921. end;
  922. procedure TForm1.acUploadExecute(Sender: TObject);
  923. begin
  924. uploadFile();
  925. //serialUpload;
  926. end;
  927. procedure TForm1.btnToneClick(Sender: TObject);
  928. begin
  929. MidiOutput.SendAllSoundOff(0, 0);
  930. ImageList2.GetBitmap(22, btnTone.Glyph);
  931. end;
  932. function TForm1.serialUpload: string;
  933. var
  934. hexFile: string;
  935. begin
  936. hexFile := MCSIO.CreateUniqueFile(MCSIO.GetTempDir, 'TPS', '.hex');
  937. makeHexFile(hexFile);
  938. Result := hexFile;
  939. end;
  940. procedure TForm1.uploadFile;
  941. var
  942. line: string;
  943. comServices: TStringList;
  944. comService: string;
  945. return: integer;
  946. hexFile: string;
  947. hexFormat: TIntelHexFormat;
  948. prgMem: array of byte;
  949. i, x: integer;
  950. com, Data: byte;
  951. tmp: string;
  952. arduinoPath: string;
  953. cmd, config, mcu: string;
  954. KeyName, StringValue: string;
  955. Res: WideString;
  956. Lines: TStringList;
  957. TimeOut: integer;
  958. error: boolean;
  959. filename: string;
  960. retries: integer;
  961. begin
  962. error := False;
  963. if cbTPSVersion.ItemIndex = 4 then
  964. begin
  965. // microbit code
  966. SaveBinFile.FileName := 'mycobit';
  967. if (SaveBinFile.Execute) then
  968. begin
  969. filename := SaveBinFile.FileName;
  970. makeBinFile(filename);
  971. end;
  972. end
  973. else
  974. begin
  975. line := GetSerialPortNames;
  976. // if (line <> '') then
  977. begin
  978. comServices := TStringList.Create;
  979. MCSStrings.DelimTextToStringlist(line, ',', comServices);
  980. return := mrOk;
  981. line := XMLPropStorage1.ReadString('ComPort', comServices[0]);
  982. frmSelectCom := TfrmSelectCom.Create(self);
  983. frmSelectCom.cbServices.Items.AddStrings(comServices);
  984. frmSelectCom.cbServices.Text := line;
  985. return := frmSelectCom.ShowModal;
  986. comService := frmSelectCom.cbServices.Text;
  987. XMLPropStorage1.WriteString('ComPort', comService);
  988. comServices.Free;
  989. if (return = mrOk) then
  990. begin
  991. Form2 := TForm2.Create(self);
  992. Form2.Memo1.Clear;
  993. Form2.Show;
  994. hexFile := serialUpload;
  995. SdpoSerial1.BaudRate := br__9600;
  996. if cbTPSVersion.ItemIndex = 5 then
  997. begin
  998. // Micro:bit V2 auto programm
  999. SdpoSerial1.BaudRate := br115200;
  1000. end;
  1001. SdpoSerial1.Device := comService;
  1002. SdpoSerial1.Active := True;
  1003. retries := 10;
  1004. error := True;
  1005. while retries > 0 do
  1006. begin
  1007. Form2.Memo1.Append('>p');
  1008. SdpoSerial1.WriteData('p');
  1009. if (readString(line, True)) then
  1010. begin
  1011. Form2.Memo1.Append(line);
  1012. if (pos('command', line) > 0) then
  1013. begin
  1014. retries := 0;
  1015. error := False;
  1016. end;
  1017. end;
  1018. if error then
  1019. begin
  1020. Form2.Memo1.Append('>h');
  1021. SdpoSerial1.WriteData('h');
  1022. end;
  1023. Dec(retries);
  1024. end;
  1025. if (not error) then
  1026. begin
  1027. Form2.Memo1.Append('>w');
  1028. SdpoSerial1.WriteData('w');
  1029. if (readString(line, True)) then
  1030. begin
  1031. Form2.Memo1.Append(line);
  1032. if (pos('ready', line) > 0) then
  1033. begin
  1034. Lines := TStringList.Create;
  1035. Lines.LoadFromFile(hexFile);
  1036. for x := 0 to Lines.Count - 1 do
  1037. begin
  1038. Form2.Memo1.Append('>' + Lines.Strings[x]);
  1039. SdpoSerial1.WriteData(Lines.Strings[x] + CRLF);
  1040. Sleep(250);
  1041. if (readString(line, False)) then
  1042. begin
  1043. if line <> '' then
  1044. begin
  1045. line := StringReplace(line, '\r', '', []);
  1046. line := StringReplace(line, '\n', '', []);
  1047. Form2.Memo1.Append(line);
  1048. end;
  1049. end;
  1050. end;
  1051. Form2.Memo1.Append('>e');
  1052. SdpoSerial1.WriteData('e');
  1053. Sleep(250);
  1054. if (readString(line, True)) then
  1055. begin
  1056. if line <> '' then
  1057. begin
  1058. line := StringReplace(line, '\r', '', []);
  1059. line := StringReplace(line, '\n', '', []);
  1060. Form2.Memo1.Append(line);
  1061. end;
  1062. end;
  1063. end
  1064. else
  1065. begin
  1066. error := True;
  1067. end;
  1068. end;
  1069. end;
  1070. if (error) then
  1071. begin
  1072. LSUAutoMsgBox('Messages', 'ID_NOT_READY', MB_OK + MB_ICONERROR);
  1073. end
  1074. else
  1075. begin
  1076. LSUAutoMsgBox('Messages', 'ID_UPLOAD_OK', MB_OK + MB_ICONINFORMATION);
  1077. end;
  1078. Form2.Close();
  1079. Form2.Destroy();
  1080. SdpoSerial1.Active := False;
  1081. DeleteFile(hexFile);
  1082. end;
  1083. end;
  1084. end;
  1085. end;
  1086. function TForm1.readString(var line: string; wait: boolean): boolean;
  1087. var
  1088. TimeOut: integer;
  1089. begin
  1090. Result := False;
  1091. TimeOut := 10;
  1092. if (wait) then
  1093. begin
  1094. while ((not SdpoSerial1.DataAvailable) and (TimeOut > 0)) do
  1095. begin
  1096. Dec(TimeOut);
  1097. Sleep(1000);
  1098. end;
  1099. end;
  1100. if (Timeout > 0) then
  1101. begin
  1102. line := SdpoSerial1.ReadData;
  1103. Result := True;
  1104. end;
  1105. end;
  1106. procedure TForm1.setDirty(Value: boolean);
  1107. begin
  1108. if (dirty <> Value) then
  1109. begin
  1110. dirty := Value;
  1111. if (dirty) then
  1112. begin
  1113. if (Pos('*', Caption) = 0) then
  1114. begin
  1115. Caption := Caption + '*';
  1116. end;
  1117. end
  1118. else
  1119. begin
  1120. if (Pos('*', Caption) > 0) then
  1121. begin
  1122. Caption := Leftstr(Caption, Pos('*', Caption) - 1);
  1123. end;
  1124. end;
  1125. end;
  1126. end;
  1127. function TForm1.checkDirty: boolean;
  1128. var
  1129. i: integer;
  1130. begin
  1131. if (dirty) then
  1132. begin
  1133. i := MCSLSU.LSUAutoMsgBox('Messages', 'SAVE_CHANGES', MB_ICONQUESTION or
  1134. MB_YESNOCANCEL);
  1135. if (i = mrYes) then
  1136. begin
  1137. saveFile(activeFile);
  1138. Result := True;
  1139. end
  1140. else if (i = mrNo) then
  1141. begin
  1142. setDirty(False);
  1143. Result := True;
  1144. end
  1145. else
  1146. begin
  1147. Result := False;
  1148. end;
  1149. end
  1150. else
  1151. begin
  1152. Result := True;
  1153. end;
  1154. end;
  1155. procedure TForm1.checkPresets;
  1156. begin
  1157. if XMLPropStorage1.ReadBoolean('preset_1.set', False) then
  1158. tbPreset1.Caption := '1*'
  1159. else
  1160. tbPreset1.Caption := '1';
  1161. if XMLPropStorage1.ReadBoolean('preset_2.set', False) then
  1162. tbPreset2.Caption := '2*'
  1163. else
  1164. tbPreset2.Caption := '2';
  1165. if XMLPropStorage1.ReadBoolean('preset_3.set', False) then
  1166. tbPreset3.Caption := '3*'
  1167. else
  1168. tbPreset3.Caption := '3';
  1169. if XMLPropStorage1.ReadBoolean('preset_4.set', False) then
  1170. tbPreset4.Caption := '4*'
  1171. else
  1172. tbPreset4.Caption := '4';
  1173. if XMLPropStorage1.ReadBoolean('preset_5.set', False) then
  1174. tbPreset5.Caption := '5*'
  1175. else
  1176. tbPreset5.Caption := '5';
  1177. if XMLPropStorage1.ReadBoolean('preset_6.set', False) then
  1178. tbPreset6.Caption := '6*'
  1179. else
  1180. tbPreset6.Caption := '6';
  1181. if XMLPropStorage1.ReadBoolean('preset_7.set', False) then
  1182. tbPreset7.Caption := '7*'
  1183. else
  1184. tbPreset7.Caption := '7';
  1185. if XMLPropStorage1.ReadBoolean('preset_8.set', False) then
  1186. tbPreset7.Caption := '8*'
  1187. else
  1188. tbPreset7.Caption := '8';
  1189. end;
  1190. procedure TForm1.makeHexFile(fileName: string);
  1191. var
  1192. i, x: integer;
  1193. tmp: string;
  1194. hexFormat: TIntelHexFormat;
  1195. prgMem: array of byte;
  1196. com, Data: byte;
  1197. begin
  1198. i := StringGrid1.RowCount;
  1199. SetLength(prgMem, i);
  1200. for x := 1 to i - 1 do
  1201. begin
  1202. com := 0;
  1203. Data := 0;
  1204. tmp := StringGrid1.Cells[1, x];
  1205. if (tmp <> '') then
  1206. begin
  1207. com := HexToInt(tmp);
  1208. tmp := StringGrid1.Cells[2, x];
  1209. if (tmp <> '') then
  1210. Data := HexToInt(tmp);
  1211. Data := com * 16 + Data;
  1212. end;
  1213. prgMem[x - 1] := Data;
  1214. end;
  1215. hexFormat := TIntelHexFormat.Create(prgMem, 8);
  1216. tmp := hexFormat.Text;
  1217. MCSIO.StrToFile(fileName, tmp);
  1218. hexFormat.Free;
  1219. SetLength(prgMem, 0);
  1220. end;
  1221. procedure TForm1.makeBinFile(fileName: string);
  1222. var
  1223. i, x: integer;
  1224. tmp: string;
  1225. hexFormat: TIntelHexFormat;
  1226. com, Data: byte;
  1227. f: file of byte;
  1228. begin
  1229. i := StringGrid1.RowCount;
  1230. AssignFile(f, fileName);
  1231. Rewrite(f, 1);
  1232. for x := 1 to i - 1 do
  1233. begin
  1234. com := 0;
  1235. Data := 0;
  1236. tmp := StringGrid1.Cells[1, x];
  1237. if (tmp <> '') then
  1238. begin
  1239. com := HexToInt(tmp);
  1240. tmp := StringGrid1.Cells[2, x];
  1241. if (tmp <> '') then
  1242. Data := HexToInt(tmp);
  1243. Data := com * 16 + Data;
  1244. end;
  1245. Write(f, Data);
  1246. end;
  1247. CloseFile(f);
  1248. end;
  1249. procedure TForm1.setCaption;
  1250. begin
  1251. if (activeFile = '') then
  1252. begin
  1253. Caption := MCSLSU.GetLSUText('form1Captions', 'ID_CAPTION', lsuCode);
  1254. end
  1255. else
  1256. begin
  1257. Caption := MCSLSU.GetLSUText('form1Captions', 'ID_CAPTION', lsuCode) +
  1258. ':' + ExtractFileName(activeFile);
  1259. end;
  1260. end;
  1261. procedure TForm1.addHeaderText;
  1262. var
  1263. i: integer;
  1264. begin
  1265. for i := 0 to StringGrid1.Columns.Count - 1 do
  1266. begin
  1267. ;
  1268. StringGrid1.Columns[i].Title.Caption :=
  1269. MCSLSU.GetLSUText('form1Captions', StringGrid1.Columns[i].Title.Caption, lsuCode);
  1270. end;
  1271. StringGrid1.Cells[0, 0] := MCSLSU.GetLSUText('form1Captions',
  1272. 'ID_GRID_STORAGE', lsuCode);
  1273. StringGrid1.Repaint;
  1274. end;
  1275. procedure TForm1.cbCommandChange(Sender: TObject);
  1276. var
  1277. x: integer;
  1278. begin
  1279. cbData.Items.Clear;
  1280. x := cbCommand.ItemIndex;
  1281. sps.getDatas(x, cbData.Items);
  1282. if StringGrid1.Row > 0 then
  1283. begin
  1284. StringGrid1.Cells[1, StringGrid1.Row] := IntToHex(x, 1);
  1285. end;
  1286. end;
  1287. procedure TForm1.cbDataChange(Sender: TObject);
  1288. var
  1289. x: integer;
  1290. begin
  1291. x := cbData.ItemIndex;
  1292. if StringGrid1.Row > 0 then
  1293. begin
  1294. StringGrid1.Cells[2, StringGrid1.Row] := IntToHex(x, 1);
  1295. StringGrid1.Cells[3, StringGrid1.Row] := sps.getCommandText(cbCommand.ItemIndex, x);
  1296. end;
  1297. end;
  1298. procedure TForm1.cbIgnDlyChange(Sender: TObject);
  1299. begin
  1300. sps.SetIgnoreDelay(cbIgnDly.Checked);
  1301. end;
  1302. procedure TForm1.cbTPSVersionChange(Sender: TObject);
  1303. begin
  1304. if (cbTPSVersion.ItemIndex = 0) then
  1305. begin
  1306. // HOLTEC
  1307. sps.setTPSVersion(Holtek);
  1308. Label2.Visible := True;
  1309. ADC2.Visible := True;
  1310. PWM2.Visible := False;
  1311. Label16.Visible := False;
  1312. RC1.Visible := False;
  1313. Label17.Visible := False;
  1314. RC2.Visible := False;
  1315. Servo1.Visible := False;
  1316. Servo2.Visible := False;
  1317. Shape1.Visible := False;
  1318. Shape2.Visible := False;
  1319. EditE.Visible := False;
  1320. EditF.Visible := False;
  1321. acUpload.Enabled := False;
  1322. Label5.Visible := False;
  1323. btnTone.Visible := False;
  1324. ADC1.Value := 0;
  1325. ADC1.MaxValue := 15;
  1326. ADC2.Value := 0;
  1327. ADC2.MaxValue := 15;
  1328. enableMicrobit(False);
  1329. end;
  1330. if (cbTPSVersion.ItemIndex = 1) then
  1331. begin
  1332. // ATMega8
  1333. sps.setTPSVersion(ATMega8);
  1334. Label2.Visible := True;
  1335. ADC2.Visible := True;
  1336. PWM2.Visible := True;
  1337. Label16.Visible := False;
  1338. RC1.Visible := False;
  1339. Label17.Visible := False;
  1340. RC2.Visible := False;
  1341. Servo1.Visible := False;
  1342. Servo2.Visible := False;
  1343. Shape1.Visible := False;
  1344. Shape2.Visible := False;
  1345. EditE.Visible := False;
  1346. EditF.Visible := False;
  1347. acUpload.Enabled := False;
  1348. Label5.Visible := False;
  1349. btnTone.Visible := False;
  1350. ADC1.Value := 0;
  1351. ADC1.MaxValue := 15;
  1352. ADC2.Value := 0;
  1353. ADC2.MaxValue := 15;
  1354. enableMicrobit(False);
  1355. end;
  1356. if ((cbTPSVersion.ItemIndex = 2) or (cbTPSVersion.ItemIndex = 3)) then
  1357. begin
  1358. if (cbTPSVersion.ItemIndex = 2) then
  1359. begin
  1360. // ATMega84
  1361. sps.setTPSVersion(ATTiny84);
  1362. acUpload.Enabled := False;
  1363. Label5.Visible := False;
  1364. btnTone.Visible := False;
  1365. end
  1366. else
  1367. begin
  1368. // Arduino 328
  1369. sps.setTPSVersion(Arduino);
  1370. acUpload.Enabled := True;
  1371. Label5.Visible := True;
  1372. btnTone.Visible := True;
  1373. end;
  1374. Label2.Visible := True;
  1375. ADC2.Visible := True;
  1376. PWM2.Visible := True;
  1377. Label16.Visible := True;
  1378. RC1.Visible := True;
  1379. Label17.Visible := True;
  1380. RC2.Visible := True;
  1381. Servo1.Visible := True;
  1382. Servo2.Visible := True;
  1383. Shape1.Visible := True;
  1384. Shape2.Visible := True;
  1385. EditE.Visible := True;
  1386. EditF.Visible := True;
  1387. ADC1.Value := 0;
  1388. ADC1.MaxValue := 255;
  1389. ADC2.Value := 0;
  1390. ADC2.MaxValue := 255;
  1391. enableMicrobit(False);
  1392. end;
  1393. if (cbTPSVersion.ItemIndex = 4) then
  1394. begin
  1395. // micro:bit
  1396. sps.setTPSVersion(microbit);
  1397. Label2.Visible := True;
  1398. ADC2.Visible := True;
  1399. PWM2.Visible := False;
  1400. Label16.Visible := False;
  1401. RC1.Visible := False;
  1402. Label17.Visible := False;
  1403. RC2.Visible := False;
  1404. Servo1.Visible := False;
  1405. Servo2.Visible := False;
  1406. Shape1.Visible := False;
  1407. Shape2.Visible := False;
  1408. EditE.Visible := False;
  1409. EditF.Visible := False;
  1410. acUpload.Enabled := True;
  1411. Label5.Visible := False;
  1412. btnTone.Visible := False;
  1413. ADC1.Value := 0;
  1414. ADC1.MaxValue := 15;
  1415. ADC2.Value := 0;
  1416. ADC2.MaxValue := 15;
  1417. enableMicrobit(False);
  1418. end;
  1419. if (cbTPSVersion.ItemIndex = 5) then
  1420. begin
  1421. // micro:bit v2
  1422. sps.setTPSVersion(MicroBitV2);
  1423. Label2.Visible := True;
  1424. ADC2.Visible := True;
  1425. PWM2.Visible := True;
  1426. Label16.Visible := True;
  1427. RC1.Visible := True;
  1428. Label17.Visible := True;
  1429. RC2.Visible := True;
  1430. Servo1.Visible := True;
  1431. Servo2.Visible := True;
  1432. Shape1.Visible := True;
  1433. Shape2.Visible := True;
  1434. EditE.Visible := True;
  1435. EditF.Visible := True;
  1436. acUpload.Enabled := True;
  1437. Label5.Visible := True;
  1438. btnTone.Visible := True;
  1439. ADC1.Value := 0;
  1440. ADC1.MaxValue := 15;
  1441. ADC2.Value := 0;
  1442. ADC2.MaxValue := 15;
  1443. enableMicrobit(True);
  1444. end;
  1445. cbCommand.Items.Clear;
  1446. sps.getCommands(cbCommand.Items);
  1447. end;
  1448. procedure TForm1.FormActivate(Sender: TObject);
  1449. begin
  1450. enableMicrobit(cbTPSVersion.ItemIndex = 5);
  1451. end;
  1452. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  1453. begin
  1454. canClose := checkDirty();
  1455. if canClose then
  1456. begin
  1457. if (sps.isActive()) then
  1458. begin
  1459. acStopExecute(Sender);
  1460. end;
  1461. end;
  1462. end;
  1463. procedure TForm1.StringGrid1EditingDone(Sender: TObject);
  1464. var
  1465. Value: string;
  1466. x: integer;
  1467. begin
  1468. // die aktuelle Zeile ist auch die letzte
  1469. if ((StringGrid1.Row + 1) = StringGrid1.RowCount) then
  1470. begin
  1471. // es wurde auch was eingegeben
  1472. Value := StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row];
  1473. Value := trim(Value);
  1474. if (Value <> '') then
  1475. begin
  1476. if StringGrid1.RowCount <= sps.getE2E() then
  1477. begin
  1478. setDirty(True);
  1479. StringGrid1.RowCount := StringGrid1.RowCount + 1;
  1480. renumberGrid();
  1481. end;
  1482. end;
  1483. end
  1484. else
  1485. begin
  1486. x := StringGrid1.Row;
  1487. StringGrid1.Cells[3, x] :=
  1488. sps.getCommandText(HexToInt(StringGrid1.Cells[1, x]),
  1489. HexToInt(StringGrid1.Cells[2, x]));
  1490. setDirty(True);
  1491. end;
  1492. end;
  1493. procedure TForm1.StringGrid1Selection(Sender: TObject; aCol, aRow: integer);
  1494. begin
  1495. try
  1496. cbCommand.ItemIndex := HexToInt(StringGrid1.Cells[1, aRow]);
  1497. cbCommandChange(Sender);
  1498. cbData.ItemIndex := HexToInt(StringGrid1.Cells[2, aRow]);
  1499. except
  1500. end;
  1501. end;
  1502. procedure TForm1.loadPreset(filename: string);
  1503. var
  1504. x: integer;
  1505. key: string;
  1506. begin
  1507. for x := 1 to 8 do
  1508. begin
  1509. key := 'preset_' + IntToStr(x);
  1510. loadSection(filename, key);
  1511. end;
  1512. checkPresets;
  1513. end;
  1514. procedure TForm1.savePreset(filename: string);
  1515. var
  1516. x: integer;
  1517. key: string;
  1518. begin
  1519. for x := 1 to 8 do
  1520. begin
  1521. key := 'preset_' + IntToStr(x);
  1522. saveSection(filename, key);
  1523. end;
  1524. end;
  1525. procedure TForm1.activateSps(enable: boolean);
  1526. begin
  1527. GBControl.Enabled := enable;
  1528. GBInternal.Enabled := enable;
  1529. GBOutput.Enabled := enable;
  1530. end;
  1531. procedure TForm1.enableMicrobit(enable: boolean);
  1532. begin
  1533. if fMicrobit <> nil then
  1534. begin
  1535. if enable and not fMicrobit.Visible then
  1536. begin
  1537. fMicrobit.Show();
  1538. fMicrobit.Left := Left + Width + 8;
  1539. fMicrobit.Top := Top;
  1540. end;
  1541. if not enable and fMicrobit.Visible then
  1542. fMicrobit.Hide();
  1543. end;
  1544. end;
  1545. procedure TForm1.inputMicrobit();
  1546. begin
  1547. if (fMicrobit <> nil) and fMicrobit.Visible then
  1548. begin
  1549. sps.setACC(fMicrobit.accx.Value, fMicrobit.accz.Value, fMicrobit.accz.Value);
  1550. sps.setComp(fMicrobit.compass.Value);
  1551. sps.setLight(fMicrobit.light.Value);
  1552. sps.setSnd(fMicrobit.snd.Value);
  1553. sps.setGesture(fMicrobit.cbGesture.ItemIndex);
  1554. sps.setLogo(fMicrobit.tbLogo.Checked);
  1555. end;
  1556. end;
  1557. procedure TForm1.outputMicrobit();
  1558. var
  1559. image: TMBImage;
  1560. begin
  1561. if (fMicrobit <> nil) and fMicrobit.Visible then
  1562. begin
  1563. image := sps.getDisplay();
  1564. fMicrobit.setImage(image);
  1565. end;
  1566. end;
  1567. procedure TForm1.saveSection(filename: string; key: string);
  1568. begin
  1569. WriteIniBool(key, 'set', XMLPropStorage1.ReadBoolean(key + '.set', False), filename);
  1570. WriteIniBool(key, 'prg', XMLPropStorage1.ReadBoolean(key + '.prg', False), filename);
  1571. WriteIniBool(key, 'sel', XMLPropStorage1.ReadBoolean(key + '.sel', False), filename);
  1572. WriteIniBool(key, 'input1', XMLPropStorage1.ReadBoolean(key + '.input1', False),
  1573. filename);
  1574. WriteIniBool(key, 'input2', XMLPropStorage1.ReadBoolean(key + '.input2', False),
  1575. filename);
  1576. WriteIniBool(key, 'input3', XMLPropStorage1.ReadBoolean(key + '.input3', False),
  1577. filename);
  1578. WriteIniBool(key, 'input4', XMLPropStorage1.ReadBoolean(key + '.input4', False),
  1579. filename);
  1580. WriteIniInteger(key, 'adc1', XMLPropStorage1.ReadInteger(key + '.adc1', 0), filename);
  1581. WriteIniInteger(key, 'adc2', XMLPropStorage1.ReadInteger(key + '.adc2', 0), filename);
  1582. WriteIniInteger(key, 'rc1', XMLPropStorage1.ReadInteger(key + '.rc1', 0), filename);
  1583. WriteIniInteger(key, 'rc2', XMLPropStorage1.ReadInteger(key + '.rc2', 0), filename);
  1584. WriteIniBool(key, 'logo', XMLPropStorage1.ReadBoolean(key + '.logo', False), filename);
  1585. WriteIniInteger(key, 'accx', XMLPropStorage1.ReadInteger(key + '.accx', 0), filename);
  1586. WriteIniInteger(key, 'accy', XMLPropStorage1.ReadInteger(key + '.accy', 0), filename);
  1587. WriteIniInteger(key, 'accz', XMLPropStorage1.ReadInteger(key + '.accz', 0), filename);
  1588. WriteIniInteger(key, 'sound', XMLPropStorage1.ReadInteger(key + '.sound', 0),
  1589. filename);
  1590. WriteIniInteger(key, 'light', XMLPropStorage1.ReadInteger(key + '.light', 0),
  1591. filename);
  1592. WriteIniInteger(key, 'comp', XMLPropStorage1.ReadInteger(key + '.comp', 0), filename);
  1593. WriteIniInteger(key, 'gesture', XMLPropStorage1.ReadInteger(key +
  1594. '.gesture', 0), filename);
  1595. end;
  1596. procedure TForm1.loadSection(filename: string; key: string);
  1597. begin
  1598. XMLPropStorage1.WriteBoolean(key + '.set', ReadIniBool(key, 'set', False, filename));
  1599. XMLPropStorage1.WriteBoolean(key + '.prg', ReadIniBool(key, 'prg', False, filename));
  1600. XMLPropStorage1.WriteBoolean(key + '.sel', ReadIniBool(key, 'sel', False, filename));
  1601. XMLPropStorage1.WriteBoolean(key + '.input1', ReadIniBool(key,
  1602. 'input1', False, filename));
  1603. XMLPropStorage1.WriteBoolean(key + '.input2', ReadIniBool(key,
  1604. 'input2', False, filename));
  1605. XMLPropStorage1.WriteBoolean(key + '.input3', ReadIniBool(key,
  1606. 'input3', False, filename));
  1607. XMLPropStorage1.WriteBoolean(key + '.input4', ReadIniBool(key,
  1608. 'input4', False, filename));
  1609. XMLPropStorage1.WriteInteger(key + '.adc1', ReadIniInteger(key, 'adc1', 0, filename));
  1610. XMLPropStorage1.WriteInteger(key + '.adc2', ReadIniInteger(key, 'adc2', 0, filename));
  1611. XMLPropStorage1.WriteInteger(key + '.rc1', ReadIniInteger(key, 'rc1', 0, filename));
  1612. XMLPropStorage1.WriteInteger(key + '.rc2', ReadIniInteger(key, 'rc2', 0, filename));
  1613. XMLPropStorage1.WriteBoolean(key + '.logo', ReadIniBool(key, 'logo', False, filename));
  1614. XMLPropStorage1.WriteInteger(key + '.accx', ReadIniInteger(key, 'accx', 0, filename));
  1615. XMLPropStorage1.WriteInteger(key + '.accy', ReadIniInteger(key, 'accy', 0, filename));
  1616. XMLPropStorage1.WriteInteger(key + '.accz', ReadIniInteger(key, 'accz', 0, filename));
  1617. XMLPropStorage1.WriteInteger(key + '.sound', ReadIniInteger(key,
  1618. 'sound', 0, filename));
  1619. XMLPropStorage1.WriteInteger(key + '.light', ReadIniInteger(key,
  1620. 'light', 0, filename));
  1621. XMLPropStorage1.WriteInteger(key + '.comp', ReadIniInteger(key, 'comp', 0, filename));
  1622. XMLPropStorage1.WriteInteger(key + '.gesture', ReadIniInteger(key,
  1623. 'gesture', 0, filename));
  1624. end;
  1625. procedure TForm1.tbPreset1Click(Sender: TObject);
  1626. var
  1627. key: string;
  1628. begin
  1629. if Sender = tbPreset1 then
  1630. key := 'preset_1'
  1631. else
  1632. if Sender = tbPreset2 then
  1633. key := 'preset_2'
  1634. else
  1635. if Sender = tbPreset3 then
  1636. key := 'preset_3'
  1637. else
  1638. if Sender = tbPreset4 then
  1639. key := 'preset_4'
  1640. else
  1641. if Sender = tbPreset5 then
  1642. key := 'preset_5'
  1643. else
  1644. if Sender = tbPreset6 then
  1645. key := 'preset_6'
  1646. else
  1647. if Sender = tbPreset7 then
  1648. key := 'preset_7'
  1649. else
  1650. if Sender = tbPreset8 then
  1651. key := 'preset_8';
  1652. tbSel.Checked := XMLPropStorage1.ReadBoolean(key + '.prg', tbSel.Checked);
  1653. tbPrg.Checked := XMLPropStorage1.ReadBoolean(key + '.sel', tbPrg.Checked);
  1654. Din1.Checked := XMLPropStorage1.ReadBoolean(key + '.input1', Din1.Checked);
  1655. Din2.Checked := XMLPropStorage1.ReadBoolean(key + '.input2', Din2.Checked);
  1656. Din3.Checked := XMLPropStorage1.ReadBoolean(key + '.input3', Din3.Checked);
  1657. Din4.Checked := XMLPropStorage1.ReadBoolean(key + '.input4', Din4.Checked);
  1658. ADC1.Value := XMLPropStorage1.ReadInteger(key + '.adc1', ADC1.Value);
  1659. ADC2.Value := XMLPropStorage1.ReadInteger(key + '.adc2', ADC2.Value);
  1660. RC1.Value := XMLPropStorage1.ReadInteger(key + '.rc1', RC1.Value);
  1661. RC2.Value := XMLPropStorage1.ReadInteger(key + '.rc2', RC2.Value);
  1662. fMicrobit.tbLogo.Checked := XMLPropStorage1.ReadBoolean(key + '.logo',
  1663. fMicrobit.tbLogo.Checked);
  1664. fMicrobit.accx.Value := XMLPropStorage1.ReadInteger(key + '.accx',
  1665. fMicrobit.accx.Value);
  1666. fMicrobit.accy.Value := XMLPropStorage1.ReadInteger(key + '.accy',
  1667. fMicrobit.accy.Value);
  1668. fMicrobit.accz.Value := XMLPropStorage1.ReadInteger(key + '.accz',
  1669. fMicrobit.accz.Value);
  1670. fMicrobit.snd.Value := XMLPropStorage1.ReadInteger(key + '.sound',
  1671. fMicrobit.snd.Value);
  1672. fMicrobit.light.Value := XMLPropStorage1.ReadInteger(key + '.light',
  1673. fMicrobit.light.Value);
  1674. fMicrobit.compass.Value := XMLPropStorage1.ReadInteger(key + '.comp',
  1675. fMicrobit.compass.Value);
  1676. fMicrobit.cbGesture.ItemIndex :=
  1677. XMLPropStorage1.ReadInteger(key + '.gesture', fMicrobit.cbGesture.ItemIndex);
  1678. end;
  1679. procedure TForm1.tbPreset1ContextPopup(Sender: TObject; MousePos: TPoint;
  1680. var Handled: boolean);
  1681. var
  1682. key: string;
  1683. begin
  1684. if Sender = tbPreset1 then
  1685. key := 'preset_1'
  1686. else
  1687. if Sender = tbPreset2 then
  1688. key := 'preset_2'
  1689. else
  1690. if Sender = tbPreset3 then
  1691. key := 'preset_3'
  1692. else
  1693. if Sender = tbPreset4 then
  1694. key := 'preset_4'
  1695. else
  1696. if Sender = tbPreset5 then
  1697. key := 'preset_5'
  1698. else
  1699. if Sender = tbPreset6 then
  1700. key := 'preset_6'
  1701. else
  1702. if Sender = tbPreset7 then
  1703. key := 'preset_7'
  1704. else
  1705. if Sender = tbPreset8 then
  1706. key := 'preset_8';
  1707. XMLPropStorage1.WriteBoolean(key + '.set', True);
  1708. XMLPropStorage1.WriteBoolean(key + '.prg', tbPrg.Checked);
  1709. XMLPropStorage1.WriteBoolean(key + '.sel', tbSel.Checked);
  1710. XMLPropStorage1.WriteBoolean(key + '.input1', Din1.Checked);
  1711. XMLPropStorage1.WriteBoolean(key + '.input2', Din2.Checked);
  1712. XMLPropStorage1.WriteBoolean(key + '.input3', Din3.Checked);
  1713. XMLPropStorage1.WriteBoolean(key + '.input4', Din4.Checked);
  1714. XMLPropStorage1.WriteInteger(key + '.adc1', ADC1.Value);
  1715. XMLPropStorage1.WriteInteger(key + '.adc2', ADC2.Value);
  1716. XMLPropStorage1.WriteInteger(key + '.rc1', RC1.Value);
  1717. XMLPropStorage1.WriteInteger(key + '.rc2', RC2.Value);
  1718. XMLPropStorage1.WriteBoolean(key + '.logo', fMicrobit.tbLogo.Checked);
  1719. XMLPropStorage1.WriteInteger(key + '.accx', fMicrobit.accx.Value);
  1720. XMLPropStorage1.WriteInteger(key + '.accy', fMicrobit.accy.Value);
  1721. XMLPropStorage1.WriteInteger(key + '.accz', fMicrobit.accz.Value);
  1722. XMLPropStorage1.WriteInteger(key + '.sound', fMicrobit.snd.Value);
  1723. XMLPropStorage1.WriteInteger(key + '.light', fMicrobit.light.Value);
  1724. XMLPropStorage1.WriteInteger(key + '.comp', fMicrobit.compass.Value);
  1725. XMLPropStorage1.WriteInteger(key + '.gesture', fMicrobit.cbGesture.ItemIndex);
  1726. checkPresets();
  1727. end;
  1728. procedure TForm1.tbPrgChange(Sender: TObject);
  1729. begin
  1730. if tbPrg.Checked then
  1731. begin
  1732. tbPrg.Hint := MCSLSU.GetLSUText('form1Hints', 'ID_BTN_PRG_SEL', lsuCode);
  1733. end
  1734. else
  1735. begin
  1736. tbPrg.Hint := MCSLSU.GetLSUText('form1Hints', 'ID_BTN_PRG_NON', lsuCode);
  1737. end;
  1738. end;
  1739. procedure TForm1.tbResetClick(Sender: TObject);
  1740. begin
  1741. tbPrg.Checked := False;
  1742. tbPrg.Hint := MCSLSU.GetLSUText('form1Hints', 'ID_BTN_PRG_NON', lsuCode);
  1743. tbSel.Checked := False;
  1744. tbSel.Hint := MCSLSU.GetLSUText('form1Hints', 'ID_BTN_SEL_NON', lsuCode);
  1745. Din1.Checked := False;
  1746. Din2.Checked := False;
  1747. Din3.Checked := False;
  1748. Din4.Checked := False;
  1749. ADC1.Value := 0;
  1750. ADC2.Value := 0;
  1751. RC1.Value := 8;
  1752. RC2.Value := 8;
  1753. end;
  1754. procedure TForm1.tbSelChange(Sender: TObject);
  1755. begin
  1756. if tbSel.Checked then
  1757. begin
  1758. tbSel.Hint := MCSLSU.GetLSUText('form1Hints', 'ID_BTN_SEL_SEL', lsuCode);
  1759. end
  1760. else
  1761. begin
  1762. tbSel.Hint := MCSLSU.GetLSUText('form1Hints', 'ID_BTN_SEL_NON', lsuCode);
  1763. end;
  1764. end;
  1765. procedure TForm1.Timer1Timer(Sender: TObject);
  1766. var
  1767. jsonString: string;
  1768. Data: TJSONData;
  1769. iNetVersion, version: string;
  1770. thisVersion, iVersion: TVersionRecord;
  1771. begin
  1772. MCSLabel.AutoSize := True;
  1773. Timer1.Enabled := False;
  1774. MCSLabel.Hint := InfoBox.versionHint;
  1775. if (InfoBox.newVersion) then
  1776. begin
  1777. MCSLabel.Font.Color := clred;
  1778. MCSLabel.Hint := InfoBox.versionHint + chr($0a) + chr($0d) +
  1779. MCSLSU.GetLSUText('form1Captions', 'ID_CLICK_HERE', lsuCode);
  1780. end;
  1781. MCSLabel.Caption := InfoBox.versionText;
  1782. end;
  1783. procedure TForm1.XMLPropStorage1RestoringProperties(Sender: TObject);
  1784. var
  1785. Uid: TGuid;
  1786. Result: HResult;
  1787. uuid: string;
  1788. begin
  1789. uuid := XMLPropStorage1.ReadString('AppUUID', '');
  1790. if uuid = '' then
  1791. begin
  1792. Result := CreateGuid(Uid);
  1793. if Result = S_OK then
  1794. begin
  1795. uuid := GuidToString(Uid);
  1796. XMLPropStorage1.WriteString('AppUUID', uuid);
  1797. end;
  1798. end;
  1799. Infobox.AppUUID := uuid;
  1800. end;
  1801. procedure TForm1.XMLPropStorage1SavingProperties(Sender: TObject);
  1802. begin
  1803. end;
  1804. procedure TForm1.renumberGrid;
  1805. var
  1806. x, i: integer;
  1807. begin
  1808. i := StringGrid1.RowCount;
  1809. for x := 1 to i - 1 do
  1810. begin
  1811. StringGrid1.Cells[0, x] := '0x' + inttohex(x - 1, 2);
  1812. if (StringGrid1.Cells[1, x] = '') then
  1813. StringGrid1.Cells[1, x] := '0';
  1814. if (StringGrid1.Cells[2, x] = '') then
  1815. StringGrid1.Cells[2, x] := '0';
  1816. StringGrid1.Cells[3, x] :=
  1817. sps.getCommandText(HexToInt(StringGrid1.Cells[1, x]),
  1818. HexToInt(StringGrid1.Cells[2, x]));
  1819. end;
  1820. end;
  1821. procedure TForm1.selectAddress(addr: word);
  1822. begin
  1823. StringGrid1.Row := addr + 1;
  1824. Application.ProcessMessages;
  1825. end;
  1826. procedure TForm1.inputSps;
  1827. begin
  1828. sps.setDin1(Din1.Checked);
  1829. sps.setDin2(Din2.Checked);
  1830. sps.setDin3(Din3.Checked);
  1831. sps.setDin4(Din4.Checked);
  1832. sps.setADC1(ADC1.Value);
  1833. sps.setADC2(ADC2.Value);
  1834. sps.setRC1(RC1.Value);
  1835. sps.setRC2(RC2.Value);
  1836. sps.setSPrg(tbPrg.Checked);
  1837. sps.setSSel(tbSel.Checked);
  1838. end;
  1839. procedure TForm1.outputSps;
  1840. var
  1841. List: TStrings;
  1842. i: integer;
  1843. begin
  1844. if sps.isDout1() then
  1845. ShapeOut1.Brush.Color := clRed
  1846. else
  1847. ShapeOut1.Brush.Color := clWhite;
  1848. if sps.isDout2() then
  1849. ShapeOut2.Brush.Color := clRed
  1850. else
  1851. ShapeOut2.Brush.Color := clWhite;
  1852. if sps.isDout3() then
  1853. ShapeOut3.Brush.Color := clRed
  1854. else
  1855. ShapeOut3.Brush.Color := clWhite;
  1856. if sps.isDout4() then
  1857. ShapeOut4.Brush.Color := clRed
  1858. else
  1859. ShapeOut4.Brush.Color := clWhite;
  1860. PWM1.Text := IntToStr(sps.getPWM1());
  1861. PWM2.Text := IntToStr(sps.getPWM2());
  1862. Servo1.Text := IntToStr(sps.getServo1());
  1863. Shape1.Repaint;
  1864. Servo2.Text := IntToStr(sps.getServo2());
  1865. Shape2.Repaint;
  1866. EditA.Text := IntToStr(sps.getARegister());
  1867. EditB.Text := IntToStr(sps.getBRegister());
  1868. EditC.Text := IntToStr(sps.getCRegister());
  1869. EditD.Text := IntToStr(sps.getDRegister());
  1870. EditE.Text := IntToStr(sps.getERegister());
  1871. EditF.Text := IntToStr(sps.getFRegister());
  1872. EditAddr.Text := '0x' + IntToHex(sps.getAddress(), 2);
  1873. EditRAdr.Text := '0x' + IntToHex(sps.getRAdr(), 2);
  1874. EditPage.Text := '0x' + IntToHex(sps.getPage(), 2);
  1875. if (sps.getJump() > 0) then
  1876. EditJump.Text := '0x' + IntToHex(sps.getJump(), 2)
  1877. else
  1878. EditJump.Text := '';
  1879. if (sps.getTone() > 0) then
  1880. begin
  1881. ImageList2.GetBitmap(23, btnTone.Glyph);
  1882. i := round(440 * power(2.0, ((sps.getTone() - 69) / 12)));
  1883. btnTone.Caption := IntToStr(i);
  1884. if (lastNote <> sps.getTone()) then
  1885. begin
  1886. playNote(sps.getTone());
  1887. end;
  1888. btntone.Enabled := True;
  1889. end
  1890. else
  1891. begin
  1892. ImageList2.GetBitmap(22, btnTone.Glyph);
  1893. btnTone.Caption := '';
  1894. if (lastNote <> sps.getTone()) then
  1895. begin
  1896. playNote(0);
  1897. end;
  1898. btntone.Enabled := True;
  1899. end;
  1900. List := TStringList.Create;
  1901. try
  1902. sps.getStack(List);
  1903. lbStack.Clear;
  1904. for i := 0 to List.Count - 1 do
  1905. begin
  1906. lbStack.Items.Add(IntToStr(i) + ':' + List[i]);
  1907. end;
  1908. finally
  1909. List.Free;
  1910. end;
  1911. end;
  1912. end.