SPS.ino 19 KB

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