SPS.ino 20 KB

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