SPS.ino 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. SPS System mit dem Arduino.
  3. Version 0.12.3
  4. 10.06.2021
  5. - adding auto programming feature for the SPS Emulator
  6. Version 0.12.2
  7. 07.06.2021
  8. - bug with servo in 4-bit mode, evaluate the full 8 bit.
  9. Version 0.12.1
  10. 03.09.2019
  11. - changing the variable names in debug mode
  12. Version 0.12
  13. 27.01.2019
  14. - adding demo program,
  15. 11.01.2018
  16. - some refactoring
  17. 07.01.2018
  18. - programming: 1/2 duty cycle for 0 values in address display
  19. Version 0.11
  20. 17.12.2018
  21. - adding Shift left and shift right to register A
  22. Version 0.10
  23. 7.12.2018
  24. - new define for serial programming
  25. 18.11.2018 WKLA
  26. - new standard programming mode
  27. I added a new programming mode for the default programming, because i thing the old one was a little bit clumsy.
  28. the new one has a nicer interface, as you now always know where you are.
  29. Starting with PRG pushed after Reset.
  30. as a result, all LEDs will shortly blink
  31. now you are in programming mode.
  32. the D1 LED will blink
  33. the higher nibble of the address will be shown
  34. the D2 LED will blink
  35. the lower nibble of the address will be shown
  36. the D3 LED will blink
  37. the command part (high nibble) will be shown
  38. with SEL you can step thru all commands
  39. PRG will save the command
  40. the D4 LED will blink
  41. the data part (low nibble) will be shown
  42. with SEL you can step thru all datas
  43. PRG will save the data
  44. if the new value has been changed, all LEDs will flash as the byte will be written to the EEPROM
  45. address will be increased and now it will start with blinking of the D1 LED
  46. To leave the programming simply push reset.
  47. Version 0.9
  48. 18.11.2018 WKLA
  49. - BUGs entfernt. Release.
  50. 10.11.2018 WKLA
  51. - Implementierung Tone Befehl
  52. Version 0.8
  53. 06.11.2018 WKLA
  54. - Umstellung auf dbgOut
  55. - Display TM1637 Anbindung
  56. Version 0.7
  57. 24.09.2012 WKLA
  58. - neue Berechnung A = B - A und Swap A,B...
  59. - Stack auf 16 Bytes berschränkt, wird zu oft gepusht, werden die alten Werte rausgeschoben.
  60. Basierd auf dem TPS System vom elektronik-labor.
  61. Erweiterungen:
  62. - es können bis zu 6 Unterroutinen definiert werden und diese direkt angesprungen werden.
  63. - neben return gibt's auch einen restart
  64. - 2 Servoausgänge für übliche RC Servos. (10° Auflösung in Nibble Modus, <1° Auflösung im Bytemodus)
  65. ACHTUNG: Servo und PWM Ausgänge sind nicht mischbar und können auch nicht gleichzeitig benutzt werden.
  66. - 2 RC Eingänge (16 Schritte auflösung im nibble Modus, Mitte 8, 255 Schritte im Byte Modus)
  67. - fkt. auch mit einem ATTiny84 (44 ist leider auf GRund der Programmgröße nicht mehr für den erweiterten Befehlssatz möglich)
  68. - call stack von bis zu 16 Unterfunktionen
  69. - neue Register e,f
  70. */
  71. /*
  72. Here are the defines used in this software to control special parts of the implementation
  73. #define SPS_USE_DISPLAY: using a external TM1637 Display for displaying address and data at one time
  74. #define SPS_RECEIVER: using a RC receiver input
  75. #define SPS_ENHANCEMENT: all of the other enhancments
  76. #define SPS_SERVO: using servo outputs
  77. #define SPS_TONE: using a tone output
  78. #define SPS_SERIAL_PRG: activates the serial programming feature
  79. */
  80. // Program im Debugmodus kompilieren, dann werden zus. Ausgaben auf die serielle Schnittstelle geschrieben.
  81. #define debug
  82. // defining different hardware platforms
  83. #ifdef __AVR_ATmega328P__
  84. //#define SPS_USE_DISPLAY
  85. //#define SPS_RECEIVER
  86. //#define SPS_ENHANCEMENT
  87. //#define SPS_SERIAL_PRG
  88. //#define SPS_SERVO
  89. //#define SPS_TONE
  90. #endif
  91. #ifdef __AVR_ATtiny84__
  92. #define SPS_ENHANCEMENT
  93. #define SPS_SERIAL_PRG
  94. #define SPS_SERVO
  95. //#define SPS_TONE
  96. #endif
  97. #ifdef __AVR_ATtiny861__
  98. #define SPS_RCRECEIVER
  99. #define SPS_ENHANCEMENT
  100. #define SPS_SERIAL_PRG
  101. //#define SPS_SERVO
  102. #define SPS_TONE
  103. #endif
  104. #ifdef __AVR_ATtiny4313__
  105. #define SPS_RCRECEIVER
  106. #endif
  107. // libraries
  108. #include <debug.h>
  109. #include <makros.h>
  110. #include "EEPROM_mbv2.h"
  111. #ifdef SPS_SERVO
  112. #include <Servo.h>
  113. #endif
  114. #ifdef SPS_ENHANCEMENT
  115. #include <avdweb_Switch.h>
  116. #endif
  117. #ifdef SPS_TONE
  118. #include "notes.h"
  119. #endif
  120. #include "hardware.h"
  121. // Commands
  122. const byte PORT = 0x10;
  123. const byte DELAY = 0x20;
  124. const byte JUMP_BACK = 0x30;
  125. const byte SET_A = 0x40;
  126. const byte IS_A = 0x50;
  127. const byte A_IS = 0x60;
  128. const byte CALC = 0x70;
  129. const byte PAGE = 0x80;
  130. const byte JUMP = 0x90;
  131. const byte C_COUNT = 0xA0;
  132. const byte D_COUNT = 0xB0;
  133. const byte SKIP_IF = 0xC0;
  134. const byte CALL = 0xD0;
  135. const byte CALL_SUB = 0xE0;
  136. const byte CMD_BYTE = 0xF0;
  137. // debouncing with 100ms
  138. const byte DEBOUNCE = 100;
  139. // sub routines
  140. const byte subCnt = 7;
  141. word subs[subCnt];
  142. // the actual address of the program
  143. word addr;
  144. // page register
  145. word page;
  146. // defining register
  147. byte a, b, c, d;
  148. #ifdef SPS_ENHANCEMENT
  149. byte e, f;
  150. #endif
  151. #ifdef SPS_ENHANCEMENT
  152. const byte SAVE_CNT = 16;
  153. #else
  154. const byte SAVE_CNT = 1;
  155. #endif
  156. word saveaddr[SAVE_CNT];
  157. byte saveCnt;
  158. #ifdef SPS_ENHANCEMENT
  159. byte stack[SAVE_CNT];
  160. byte stackCnt;
  161. #endif
  162. unsigned long tmpValue;
  163. #ifdef SPS_SERVO
  164. Servo servo1;
  165. Servo servo2;
  166. #endif
  167. byte prog = 0;
  168. byte data = 0;
  169. byte com = 0;
  170. void setup() {
  171. pinMode(Dout_0, OUTPUT);
  172. pinMode(Dout_1, OUTPUT);
  173. pinMode(Dout_2, OUTPUT);
  174. pinMode(Dout_3, OUTPUT);
  175. pinMode(PWM_1, OUTPUT);
  176. pinMode(PWM_2, OUTPUT);
  177. pinMode(Din_0, INPUT_PULLUP);
  178. pinMode(Din_1, INPUT_PULLUP);
  179. pinMode(Din_2, INPUT_PULLUP);
  180. pinMode(Din_3, INPUT_PULLUP);
  181. pinMode(SW_PRG, INPUT_PULLUP);
  182. pinMode(SW_SEL, INPUT_PULLUP);
  183. digitalWrite(Dout_0, 1);
  184. delay(1000);
  185. digitalWrite(Dout_0, 0);
  186. #ifdef SPS_USE_DISPLAY
  187. initDisplay();
  188. #endif
  189. // Serielle Schnittstelle einstellen
  190. #ifndef __AVR_ATtiny84__
  191. initDebug();
  192. #endif
  193. prgDemoPrg();
  194. doReset();
  195. if (digitalRead(SW_PRG) == 0) {
  196. programMode();
  197. }
  198. #ifdef SPS_ENHANCEMENT
  199. pinMode(LED_BUILTIN, OUTPUT);
  200. #endif
  201. #ifdef SPS_SERIAL_PRG
  202. initSerialPrg();
  203. if (digitalRead(SW_SEL) == 0) {
  204. serialPrg();
  205. }
  206. #endif
  207. }
  208. void doReset() {
  209. dbgOutLn("Reset");
  210. #ifdef SPS_SERVO
  211. servo1.detach();
  212. servo2.detach();
  213. #endif
  214. for (int i = 0; i < subCnt; i++) {
  215. subs[i] = 0;
  216. }
  217. readProgram();
  218. addr = 0;
  219. page = 0;
  220. saveCnt = 0;
  221. a = 0;
  222. b = 0;
  223. c = 0;
  224. d = 0;
  225. #ifdef SPS_ENHANCEMENT
  226. e = 0;
  227. f = 0;
  228. #endif
  229. }
  230. /*
  231. getting all addresses of sub programms
  232. */
  233. void readProgram() {
  234. dbgOutLn("Read program");
  235. word addr = 0;
  236. for ( addr = 0; addr <= STORESIZE; addr++) {
  237. byte value = readbyte(addr);
  238. #ifdef debug
  239. dbgOutLn();
  240. dbgOut2(addr, HEX);
  241. dbgOut(": ");
  242. #endif
  243. if (value == 0xFF) {
  244. // ende des Programms
  245. break;
  246. }
  247. byte cmd = (value & 0xF0);
  248. byte data = (value & 0x0F);
  249. dbgOut2(cmd>>4, HEX);
  250. dbgOut2(data, HEX);
  251. if (cmd == CALL_SUB) {
  252. if (data >= 8) {
  253. data = data - 8;
  254. subs[data] = addr + 1;
  255. }
  256. }
  257. #ifdef SPS_SERVO
  258. if ((cmd == IS_A) && (data == 0x0B)) {
  259. if (!servo1.attached()) {
  260. dbgOutLn(": attach Srv1");
  261. servo1.attach(SERVO_1);
  262. }
  263. } else if ((cmd == CMD_BYTE) && (data == 0x06)) {
  264. if (!servo1.attached()) {
  265. dbgOutLn(": attach Srv1");
  266. servo1.attach(SERVO_1);
  267. }
  268. } else if ((cmd == IS_A) && (data == 0x0C)) {
  269. if (!servo2.attached()) {
  270. dbgOutLn(": attach Srv2");
  271. servo2.attach(SERVO_2);
  272. }
  273. } else if ((cmd == CMD_BYTE) && (data == 0x07)) {
  274. if (!servo2.attached()) {
  275. dbgOutLn(": attach Srv2");
  276. servo2.attach(SERVO_2);
  277. }
  278. }
  279. #endif
  280. }
  281. dbgOutLn();
  282. }
  283. /*
  284. main loop
  285. */
  286. void loop() {
  287. #ifdef SPS_SERIAL_PRG
  288. if (Serial.available() > 0) {
  289. while (Serial.available() > 0) {
  290. char myChar = Serial.read();
  291. if (myChar == 'p') {
  292. serialPrg();
  293. Serial.println("end of inline programming");
  294. doReset();
  295. }
  296. }
  297. }
  298. #endif
  299. byte value = readbyte(addr);
  300. byte cmd = (value & 0xF0);
  301. byte data = (value & 0x0F);
  302. debugOutputRegister();
  303. addr = addr + 1;
  304. switch (cmd) {
  305. case PORT:
  306. doPort(data);
  307. break;
  308. case DELAY:
  309. doDelay(data);
  310. break;
  311. case JUMP_BACK:
  312. doJumpBack(data);
  313. break;
  314. case SET_A:
  315. doSetA(data);
  316. break;
  317. case A_IS:
  318. doAIs(data);
  319. break;
  320. case IS_A:
  321. doIsA(data);
  322. break;
  323. case CALC:
  324. doCalc(data);
  325. break;
  326. case PAGE:
  327. doPage(data);
  328. break;
  329. case JUMP:
  330. doJump(data);
  331. break;
  332. case C_COUNT:
  333. doCCount(data);
  334. break;
  335. case D_COUNT:
  336. doDCount(data);
  337. break;
  338. case SKIP_IF:
  339. doSkipIf(data);
  340. break;
  341. case CALL:
  342. doCall(data);
  343. break;
  344. case CALL_SUB:
  345. doCallSub(data);
  346. break;
  347. case CMD_BYTE:
  348. doByte(data);
  349. break;
  350. default:
  351. ;
  352. }
  353. if (addr > STORESIZE) {
  354. doReset();
  355. }
  356. }
  357. void debugOutputRegister() {
  358. dbgOut2(addr, HEX); dbgOut(":"); dbgOut2(prog, HEX); dbgOut(",");
  359. dbgOut2(com, HEX); dbgOut(","); dbgOut2(data, HEX); dbgOut(",a:");
  360. dbgOut2(a, HEX); dbgOut(","); dbgOut2(b, HEX); dbgOut(",");
  361. dbgOut2(c, HEX); dbgOut(","); dbgOut2(d, HEX); dbgOut(",");
  362. dbgOut2(e, HEX); dbgOut(","); dbgOut2(f, HEX); dbgOutLn();
  363. }
  364. /*
  365. output to port
  366. */
  367. void doPort(byte data) {
  368. digitalWrite(Dout_0, (data & 0x01) > 0);
  369. digitalWrite(Dout_1, (data & 0x02) > 0);
  370. digitalWrite(Dout_2, (data & 0x04) > 0);
  371. digitalWrite(Dout_3, (data & 0x08) > 0);
  372. }
  373. /*
  374. delay in ms
  375. */
  376. void doDelay(byte data) {
  377. dbgOut("dly: ");
  378. dbgOutLn2(data, HEX);
  379. switch (data) {
  380. case 0:
  381. delay(1);
  382. break;
  383. case 1:
  384. delay(2);
  385. break;
  386. case 2:
  387. delay(5);
  388. break;
  389. case 3:
  390. delay(10);
  391. break;
  392. case 4:
  393. delay(20);
  394. break;
  395. case 5:
  396. delay(50);
  397. break;
  398. case 6:
  399. delay(100);
  400. break;
  401. case 7:
  402. delay(200);
  403. break;
  404. case 8:
  405. delay(500);
  406. break;
  407. case 9:
  408. delay(1000);
  409. break;
  410. case 10:
  411. delay(2000);
  412. break;
  413. case 11:
  414. delay(5000);
  415. break;
  416. case 12:
  417. delay(10000);
  418. break;
  419. case 13:
  420. delay(20000);
  421. break;
  422. case 14:
  423. delay(30000);
  424. break;
  425. case 15:
  426. delay(60000);
  427. break;
  428. default:
  429. break;
  430. }
  431. }
  432. /*
  433. jump relative back
  434. */
  435. void doJumpBack(byte data) {
  436. addr = addr - data - 1;
  437. }
  438. /*
  439. a = data
  440. */
  441. void doSetA(byte data) {
  442. a = data;
  443. }
  444. /*
  445. a = somthing;
  446. */
  447. void doAIs(byte data) {
  448. switch (data) {
  449. case 1:
  450. a = b;
  451. break;
  452. case 2:
  453. a = c;
  454. break;
  455. case 3:
  456. a = d;
  457. break;
  458. case 4:
  459. a = digitalRead(Din_0) + (digitalRead(Din_1) << 1) + (digitalRead(Din_2) << 2) + (digitalRead(Din_3) << 3);
  460. break;
  461. case 5:
  462. a = digitalRead(Din_0);
  463. break;
  464. case 6:
  465. a = digitalRead(Din_1);
  466. break;
  467. case 7:
  468. a = digitalRead(Din_2);
  469. break;
  470. case 8:
  471. a = digitalRead(Din_3);
  472. break;
  473. #ifndef __AVR_ATtiny4313__
  474. case 9:
  475. tmpValue = analogRead(ADC_0);
  476. a = tmpValue / 64; //(Umrechnen auf 4 bit)
  477. break;
  478. case 10:
  479. tmpValue = analogRead(ADC_1);
  480. a = tmpValue / 64; //(Umrechnen auf 4 bit)
  481. break;
  482. #else
  483. case 9:
  484. a = digitalRead(ADC_0);
  485. break;
  486. case 10:
  487. a = digitalRead(ADC_1);
  488. break;
  489. #endif
  490. #ifdef SPS_RCRECEIVER
  491. case 11:
  492. tmpValue = pulseIn(RC_0, HIGH, 100000);
  493. if (tmpValue < 1000) {
  494. tmpValue = 1000;
  495. }
  496. if (tmpValue > 2000) {
  497. tmpValue = 2000;
  498. }
  499. a = (tmpValue - 1000) / 64; //(Umrechnen auf 4 bit)
  500. dbgOut("RC1:");
  501. dbgOut(tmpValue);
  502. dbgOut("=");
  503. dbgOutLn(a);
  504. break;
  505. case 12:
  506. tmpValue = pulseIn(RC_1, HIGH, 100000);
  507. if (tmpValue < 1000) {
  508. tmpValue = 1000;
  509. }
  510. if (tmpValue > 2000) {
  511. tmpValue = 2000;
  512. }
  513. a = (tmpValue - 1000) / 64; //(Umrechnen auf 4 bit)
  514. dbgOut("RC2:");
  515. dbgOut(tmpValue);
  516. dbgOut("=");
  517. dbgOutLn(a);
  518. break;
  519. #endif
  520. #ifdef SPS_ENHANCMENT
  521. case 13:
  522. a = e;
  523. break;
  524. case 14:
  525. a = f;
  526. break;
  527. case 15:
  528. if (stackCnt > 0) {
  529. stackCnt -= 1;
  530. a = stack[stackCnt];
  531. } else {
  532. a = 0;
  533. }
  534. break;
  535. #endif
  536. default:
  537. break;
  538. }
  539. }
  540. /*
  541. somthing = a;
  542. */
  543. void doIsA(byte data) {
  544. switch (data) {
  545. #ifdef SPS_ENHANCEMENT
  546. case 0:
  547. swap(a, b, byte);
  548. break;
  549. #endif
  550. case 1:
  551. b = a;
  552. break;
  553. case 2:
  554. c = a;
  555. break;
  556. case 3:
  557. d = a;
  558. break;
  559. case 4:
  560. doPort(a);
  561. break;
  562. case 5:
  563. digitalWrite(Dout_0, (a & 0x01) > 0);
  564. break;
  565. case 6:
  566. digitalWrite(Dout_1, (a & 0x01) > 0);
  567. break;
  568. case 7:
  569. digitalWrite(Dout_2, (a & 0x01) > 0);
  570. break;
  571. case 8:
  572. digitalWrite(Dout_3, (a & 0x01) > 0);
  573. break;
  574. case 9:
  575. tmpValue = a * 16;
  576. dbgOut("PWM1:");
  577. dbgOutLn(tmpValue);
  578. analogWrite(PWM_1, tmpValue);
  579. break;
  580. case 10:
  581. tmpValue = a * 16;
  582. dbgOut("PWM2:");
  583. dbgOutLn(tmpValue);
  584. analogWrite(PWM_2, tmpValue);
  585. break;
  586. #ifdef SPS_SERVO
  587. case 11:
  588. if (servo1.attached()) {
  589. tmpValue = ((a & 0x0f) * 10) + 10;
  590. dbgOut("Srv1:");
  591. dbgOutLn(tmpValue);
  592. servo1.write(tmpValue);
  593. }
  594. break;
  595. case 12:
  596. if (servo2.attached()) {
  597. tmpValue = ((a & 0x0f) * 10) + 10;
  598. dbgOut("Srv2:");
  599. dbgOutLn(tmpValue);
  600. servo2.write(tmpValue);
  601. }
  602. break;
  603. #endif
  604. #ifdef SPS_ENHANCEMENT
  605. case 13:
  606. e = a;
  607. break;
  608. case 14:
  609. f = a;
  610. break;
  611. case 15:
  612. if (stackCnt < SAVE_CNT) {
  613. stack[stackCnt] = a;
  614. stackCnt += 1;
  615. }
  616. else {
  617. for (int i = 1; i <= SAVE_CNT; i++) {
  618. stack[i - 1] = stack[i];
  619. }
  620. stack[stackCnt] = a;
  621. }
  622. break;
  623. #endif
  624. default:
  625. break;
  626. }
  627. }
  628. /*
  629. calculations
  630. */
  631. void doCalc(byte data) {
  632. switch (data) {
  633. case 1:
  634. a = a + 1;
  635. break;
  636. case 2:
  637. a = a - 1;
  638. break;
  639. case 3:
  640. a = a + b;
  641. break;
  642. case 4:
  643. a = a - b;
  644. break;
  645. case 5:
  646. a = a * b;
  647. break;
  648. case 6:
  649. a = a / b;
  650. break;
  651. case 7:
  652. a = a & b;
  653. break;
  654. case 8:
  655. a = a | b;
  656. break;
  657. case 9:
  658. a = a ^ b;
  659. break;
  660. case 10:
  661. a = !a;
  662. break;
  663. #ifdef SPS_ENHANCEMENT
  664. case 11:
  665. a = a % b;
  666. break;
  667. case 12:
  668. a = a + 16 * b;
  669. break;
  670. case 13:
  671. a = b - a;
  672. break;
  673. case 14:
  674. a = a >> 1;
  675. break;
  676. case 15:
  677. a = a << 1;
  678. break;
  679. #endif
  680. default:
  681. break;
  682. }
  683. #ifndef SPS_ENHANCEMENT
  684. a = a & 15;
  685. #endif
  686. }
  687. /*
  688. setting page
  689. */
  690. void doPage(byte data) {
  691. page = data * 16;
  692. }
  693. /*
  694. jump absolute
  695. */
  696. void doJump(byte data) {
  697. #ifdef debug
  698. dbgOut("J");
  699. dbgOut2(page, HEX);
  700. dbgOutLn2(data, HEX);
  701. #endif
  702. addr = page + data;
  703. }
  704. /*
  705. counting with c register
  706. */
  707. void doCCount(byte data) {
  708. if (c > 0) {
  709. c -= 1;
  710. c = c & 0x0F;
  711. doJump(data);
  712. }
  713. }
  714. /*
  715. counting with d register
  716. */
  717. void doDCount(byte data) {
  718. if (d > 0) {
  719. d -= 1;
  720. d = d & 0x0F;
  721. doJump(data);
  722. }
  723. }
  724. /*
  725. simple condition = true, skip next command
  726. */
  727. void doSkipIf(byte data) {
  728. bool skip = false;
  729. switch (data) {
  730. #ifdef SPS_ENHANCEMENT
  731. case 0:
  732. skip = (a == 0);
  733. break;
  734. #endif
  735. case 1:
  736. skip = (a > b);
  737. break;
  738. case 2:
  739. skip = (a < b);
  740. break;
  741. case 3:
  742. skip = (a == b);
  743. break;
  744. case 4:
  745. skip = digitalRead(Din_0);
  746. break;
  747. case 5:
  748. skip = digitalRead(Din_1);
  749. break;
  750. case 6:
  751. skip = digitalRead(Din_2);
  752. break;
  753. case 7:
  754. skip = digitalRead(Din_3);
  755. break;
  756. case 8:
  757. skip = !digitalRead(Din_0);
  758. break;
  759. case 9:
  760. skip = !digitalRead(Din_1);
  761. break;
  762. case 10:
  763. skip = !digitalRead(Din_2);
  764. break;
  765. case 11:
  766. skip = !digitalRead(Din_3);
  767. break;
  768. case 12:
  769. skip = !digitalRead(SW_PRG);
  770. break;
  771. case 13:
  772. skip = !digitalRead(SW_SEL);
  773. break;
  774. case 14:
  775. skip = digitalRead(SW_PRG);
  776. break;
  777. case 15:
  778. skip = digitalRead(SW_SEL);
  779. break;
  780. default:
  781. break;
  782. }
  783. if (skip) {
  784. addr += 1;
  785. }
  786. }
  787. /*
  788. calling a subroutine
  789. */
  790. void doCall(byte data) {
  791. saveaddr[saveCnt] = addr;
  792. saveCnt++;
  793. addr = page + data;
  794. }
  795. /*
  796. calling a subroutine, calling return and restart
  797. */
  798. void doCallSub(byte data) {
  799. if (data == 0) {
  800. if (saveCnt < 0) {
  801. doReset();
  802. return;
  803. }
  804. saveCnt -= 1;
  805. addr = saveaddr[saveCnt];
  806. dbgOut("r:");
  807. dbgOutLn(addr);
  808. return;
  809. }
  810. #ifdef SPS_ENHANCEMENT
  811. if (data <= 7) {
  812. // call subroutine number
  813. doCall(addr);
  814. addr = subs[data - 1];
  815. dbgOut("c:");
  816. dbgOutLn(addr);
  817. return;
  818. }
  819. if (data == 0x0f) {
  820. doReset();
  821. }
  822. #endif
  823. }
  824. /*
  825. calling a byte methods
  826. */
  827. void doByte(byte data) {
  828. #ifdef SPS_ENHANCEMENT
  829. dbgOut("B ");
  830. switch (data) {
  831. case 0:
  832. tmpValue = analogRead(ADC_0);
  833. a = tmpValue >> 2; //(Umrechnen auf 8 bit)
  834. break;
  835. case 1:
  836. tmpValue = analogRead(ADC_1);
  837. a = tmpValue >> 2; //(Umrechnen auf 8 bit)
  838. break;
  839. #ifdef SPS_RCRECEIVER
  840. case 2:
  841. tmpValue = pulseIn(RC_0, HIGH, 100000);
  842. if (tmpValue < 1000) {
  843. tmpValue = 1000;
  844. }
  845. if (tmpValue > 2000) {
  846. tmpValue = 2000;
  847. }
  848. a = (tmpValue - 1000) / 4; //(Umrechnen auf 4 bit)
  849. dbgOut("RC1:");
  850. dbgOut(tmpValue);
  851. dbgOut("=");
  852. dbgOutLn(a);
  853. break;
  854. case 3:
  855. tmpValue = pulseIn(RC_1, HIGH, 100000);
  856. if (tmpValue < 1000) {
  857. tmpValue = 1000;
  858. }
  859. if (tmpValue > 2000) {
  860. tmpValue = 2000;
  861. }
  862. a = (tmpValue - 1000) / 4; //(Umrechnen auf 4 bit)
  863. dbgOut("RC2:");
  864. dbgOut(tmpValue);
  865. dbgOut("=");
  866. dbgOutLn(a);
  867. break;
  868. #endif
  869. case 4:
  870. tmpValue = a;
  871. dbgOut("PWM1:");
  872. dbgOutLn(a);
  873. analogWrite(PWM_1, a);
  874. break;
  875. case 5:
  876. tmpValue = a;
  877. dbgOut("PWM2:");
  878. dbgOutLn(a);
  879. analogWrite(PWM_2, a);
  880. break;
  881. #ifdef SPS_SERVO
  882. case 6:
  883. if (servo1.attached()) {
  884. dbgOut("Srv1:");
  885. tmpValue = map(a, 0, 255, 0, 180);
  886. dbgOutLn(tmpValue);
  887. servo1.write(tmpValue);
  888. }
  889. break;
  890. case 7:
  891. if (servo2.attached()) {
  892. dbgOut("Srv2:");
  893. tmpValue = map(a, 0, 255, 0, 180);
  894. dbgOutLn(tmpValue);
  895. servo2.write(tmpValue);
  896. }
  897. break;
  898. #endif
  899. #ifdef SPS_TONE
  900. case 8:
  901. if (a == 0) {
  902. dbgOutLn("Tone off");
  903. noTone(PWM_2);
  904. } else {
  905. if (between(a, MIDI_START, MIDI_START + MIDI_NOTES)) {
  906. word frequenz = pgm_read_word(a - MIDI_START + midiNoteToFreq);
  907. dbgOut("Tone on: midi ");
  908. dbgOut2(a, DEC);
  909. dbgOut(", ");
  910. dbgOut2(frequenz, DEC);
  911. dbgOutLn("Hz");
  912. tone(PWM_2, frequenz);
  913. }
  914. }
  915. break;
  916. #endif
  917. #ifdef __AVR_ATmega328P__
  918. case 13:
  919. digitalWrite(LED_BUILTIN, 0);
  920. break;
  921. case 14:
  922. digitalWrite(LED_BUILTIN, 1);
  923. break;
  924. #endif
  925. }
  926. #endif
  927. }