1
0

SPS.ino 19 KB

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