SPS.ino 20 KB

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