gtx328.nas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # Garmin GTX-327 Transponder
  2. # Copyright (c) 2019 Joshua Davidson (Octal450)
  3. # Initialize variables
  4. var annuns = ["off", "stby", "off", "stby", "on", "alt"];
  5. var code = "7000";
  6. var mode = 1; # 0 = OFF, 1 = STANDBY, 4 = ON, 5 = ALTITUDE
  7. var modes = ["OFF", "STANDBY", "TEST", "GROUND", "ON", "ALTITUDE"];
  8. var powerUpTime = 0;
  9. var powerUpTestAnnun = 0;
  10. var identTime = 0;
  11. # Initialize all used property nodes
  12. var elapsedSec = props.globals.getNode("/sim/time/elapsed-sec");
  13. var powerSrc = props.globals.getNode("/systems/electrical/outputs/transponder", 1); # Transponder power source
  14. powerSrc.setDoubleValue(0.0);
  15. var serviceable = props.globals.initNode("/instrumentation/it-gtx327/serviceable", 1, "BOOL");
  16. var systemAlive = props.globals.initNode("/instrumentation/it-gtx327/internal/system-alive", 0, "BOOL");
  17. var systemAliveTemp = 0;
  18. var powerUpTest = props.globals.initNode("/instrumentation/it-gtx327/internal/powerup-test", -1, "INT"); # -1 = Powerup test not done, 0 = Powerup test complete, 1 = Powerup test in progress
  19. var powerUpTestTemp = 0;
  20. var idCode = props.globals.getNode("/instrumentation/transponder/id-code", 1);
  21. var modeKnob = props.globals.getNode("/instrumentation/transponder/inputs/knob-mode", 1);
  22. var identBtn = props.globals.getNode("/instrumentation/transponder/inputs/ident-btn", 1);
  23. var modeID = props.globals.getNode("/sim/gui/dialogs/radios/transponder-mode", 1);
  24. var displayMode = props.globals.initNode("/instrumentation/it-gtx327/internal/display-mode", "PA", "STRING");
  25. var displayOn = props.globals.initNode("/instrumentation/it-gtx327/internal/display-on", 0, "BOOL");
  26. var codeEntryActive = props.globals.initNode("/instrumentation/it-gtx327/internal/code-entry-active", 0, "BOOL");
  27. var Annun = {
  28. code: [nil, props.globals.initNode("/instrumentation/it-gtx327/annun/code-1", 7, "INT"), props.globals.initNode("/instrumentation/it-gtx327/annun/code-2", 0, "INT"), props.globals.initNode("/instrumentation/it-gtx327/annun/code-3", 0, "INT"), props.globals.initNode("/instrumentation/it-gtx327/annun/code-4", 0, "INT")],
  29. fail: props.globals.initNode("/instrumentation/it-gtx327/annun/fail", 0, "BOOL"),
  30. ident: props.globals.initNode("/instrumentation/it-gtx327/annun/ident", 0, "BOOL"),
  31. mode: props.globals.initNode("/instrumentation/it-gtx327/annun/mode", "off", "STRING"),
  32. r: props.globals.initNode("/instrumentation/it-gtx327/annun/reply", 0, "BOOL"),
  33. sel: props.globals.initNode("/instrumentation/it-gtx327/annun/sel", 0, "INT"),
  34. selTemp: 0,
  35. test: props.globals.initNode("/instrumentation/it-gtx327/annun/test", 0, "BOOL"),
  36. };
  37. setlistener("/sim/signals/fdm-initialized", func {
  38. system.init();
  39. });
  40. var system = {
  41. init: func() {
  42. mode = 1;
  43. codeEntryActive.setBoolValue(0);
  44. code = idCode.getValue(); # If a code was saved via aircraft-data or other means, import it
  45. me.updateCode();
  46. identBtn.setBoolValue(0);
  47. powerUpTest.setValue(-1);
  48. displayMode.setValue("PA");
  49. displayOn.setBoolValue(0);
  50. Annun.fail.setBoolValue(0);
  51. Annun.ident.setBoolValue(0);
  52. Annun.r.setBoolValue(0);
  53. Annun.sel.setValue(0);
  54. Annun.test.setBoolValue(0);
  55. me.setMode(mode);
  56. update.start();
  57. },
  58. loop: func() {
  59. if (powerSrc.getValue() >= 8) {
  60. systemAlive.setBoolValue(1);
  61. if (powerUpTest.getValue() == -1 and mode != 0) { # Begin power on test
  62. powerUpTest.setValue(1);
  63. powerUpTime = elapsedSec.getValue();
  64. } else if (powerUpTest.getValue() != -1 and mode == 0) {
  65. powerUpTest.setValue(-1);
  66. }
  67. } else {
  68. systemAlive.setBoolValue(0);
  69. if (powerUpTest.getValue() != -1) {
  70. powerUpTest.setValue(-1);
  71. }
  72. }
  73. systemAliveTemp = systemAlive.getBoolValue();
  74. if (systemAliveTemp != 0 and serviceable.getBoolValue()) {
  75. if (powerUpTest.getValue() >= 1 and powerUpTime + 3 < elapsedSec.getValue()) {
  76. powerUpTest.setValue(0);
  77. }
  78. } else if (systemAliveTemp != 0 and !serviceable.getBoolValue()) {
  79. if ((powerUpTest.getValue() == 0 or powerUpTest.getValue() == 1) and powerUpTime + 3 < elapsedSec.getValue()) {
  80. powerUpTest.setValue(2);
  81. }
  82. }
  83. powerUpTestTemp = powerUpTest.getValue();
  84. if (systemAliveTemp and powerUpTestTemp != -1) {
  85. displayOn.setBoolValue(1);
  86. } else {
  87. displayOn.setBoolValue(0);
  88. }
  89. # Annunciators
  90. if (powerUpTestTemp == 1 and systemAliveTemp) {
  91. Annun.test.setBoolValue(1);
  92. } else {
  93. Annun.test.setBoolValue(0);
  94. }
  95. if (powerUpTestTemp == 2 and systemAliveTemp) {
  96. Annun.fail.setBoolValue(1);
  97. } else {
  98. Annun.fail.setBoolValue(0);
  99. }
  100. if (powerUpTestTemp == 0 and systemAliveTemp) {
  101. Annun.mode.setValue(annuns[modeKnob.getValue()]);
  102. } else {
  103. Annun.mode.setValue("off");
  104. }
  105. if (identBtn.getBoolValue() and powerUpTestTemp == 0 and systemAliveTemp) {
  106. Annun.ident.setBoolValue(1);
  107. } else {
  108. Annun.ident.setBoolValue(0);
  109. }
  110. # Update transponder modes
  111. if (powerUpTestTemp == 0 and serviceable.getBoolValue()) {
  112. if (modeKnob.getValue() != mode) {
  113. me.setMode(mode);
  114. }
  115. } else {
  116. me.setMode(0);
  117. }
  118. },
  119. compileCode: func() {
  120. system.setCode(Annun.code[1].getValue() ~ Annun.code[2].getValue() ~ Annun.code[3].getValue() ~ Annun.code[4].getValue());
  121. },
  122. setCode: func(c) {
  123. me.endCodeEntry();
  124. code = c;
  125. idCode.setValue(c);
  126. me.updateCode();
  127. },
  128. updateCode: func() { # ~ "" exists to make sure it is a string
  129. Annun.code[1].setValue(substr(code ~ "", 0, 1));
  130. Annun.code[2].setValue(substr(code ~ "", 1, 1));
  131. Annun.code[3].setValue(substr(code ~ "", 2, 1));
  132. Annun.code[4].setValue(substr(code ~ "", 3, 1));
  133. },
  134. beginCodeEntry: func() {
  135. codeEntryActive.setBoolValue(1);
  136. Annun.sel.setValue(2)
  137. },
  138. endCodeEntry: func() {
  139. Annun.sel.setValue(0);
  140. codeEntryActive.setBoolValue(0);
  141. me.updateCode();
  142. },
  143. setMode: func(m) {
  144. modeKnob.setValue(m);
  145. modeID.setValue(modes[m]);
  146. },
  147. beginIdent: func(t) {
  148. identTime = t;
  149. identBtn.setBoolValue(1);
  150. identChk.start();
  151. },
  152. };
  153. var button = {
  154. OFF: func() {
  155. if (systemAlive.getBoolValue()) {
  156. mode = 0;
  157. }
  158. },
  159. STBY: func() {
  160. if (systemAlive.getBoolValue()) {
  161. mode = 1;
  162. }
  163. },
  164. ON: func() {
  165. if (systemAlive.getBoolValue()) {
  166. mode = 4;
  167. }
  168. },
  169. ALT: func() {
  170. if (systemAlive.getBoolValue()) {
  171. mode = 5;
  172. }
  173. },
  174. IDENT: func() {
  175. if (systemAlive.getBoolValue() and powerUpTest.getValue() == 0 and serviceable.getBoolValue()) {
  176. system.beginIdent(elapsedSec.getValue());
  177. }
  178. },
  179. VFR: func() {
  180. if (systemAlive.getBoolValue() and powerUpTest.getValue() == 0 and serviceable.getBoolValue()) {
  181. system.setCode(7000);
  182. }
  183. },
  184. CLR: func() {
  185. if (systemAlive.getBoolValue() and powerUpTest.getValue() == 0 and serviceable.getBoolValue()) {
  186. if (codeEntryActive.getBoolValue()) {
  187. Annun.selTemp = Annun.sel.getValue();
  188. if (Annun.selTemp > 1) {
  189. Annun.sel.setValue(Annun.selTemp - 1);
  190. Annun.code[Annun.selTemp - 1].setValue(-1);
  191. }
  192. } else {
  193. # TODO: If within 5sec of entry, return cursor to 4th line
  194. }
  195. }
  196. },
  197. CRSR: func() {
  198. if (systemAlive.getBoolValue() and powerUpTest.getValue() == 0 and serviceable.getBoolValue()) {
  199. system.endCodeEntry();
  200. }
  201. },
  202. Number: func(n) {
  203. if (systemAlive.getBoolValue() and powerUpTest.getValue() == 0 and serviceable.getBoolValue()) {
  204. if (n != 8 and n != 9) {
  205. if (codeEntryActive.getBoolValue()) {
  206. if (Annun.code[1].getValue() == -1) {
  207. Annun.code[1].setValue(n);
  208. Annun.sel.setValue(2);
  209. } else if (Annun.code[2].getValue() == -1) {
  210. Annun.code[2].setValue(n);
  211. Annun.sel.setValue(3);
  212. } else if (Annun.code[3].getValue() == -1) {
  213. Annun.code[3].setValue(n);
  214. Annun.sel.setValue(4);
  215. } else if (Annun.code[4].getValue() == -1) {
  216. Annun.code[4].setValue(n);
  217. system.compileCode();
  218. }
  219. } else {
  220. system.beginCodeEntry();
  221. Annun.code[1].setValue(n);
  222. Annun.code[2].setValue(-1);
  223. Annun.code[3].setValue(-1);
  224. Annun.code[4].setValue(-1);
  225. }
  226. }
  227. }
  228. },
  229. };
  230. var identChk = maketimer(0.5, func {
  231. if (identBtn.getBoolValue() and systemAlive.getBoolValue() and mode != 0) {
  232. if (identTime + 18 <= elapsedSec.getValue()) {
  233. identChk.stop();
  234. identBtn.setBoolValue(0);
  235. }
  236. } else {
  237. identChk.stop();
  238. identBtn.setBoolValue(0);
  239. }
  240. });
  241. # Handler for code change from generic dialog
  242. setlistener("/instrumentation/transponder/id-code", func {
  243. if (code != idCode.getValue()) {
  244. system.setCode(sprintf("%04d", idCode.getValue()));
  245. }
  246. }, 0, 0);
  247. var update = maketimer(0.1, system, system.loop);