Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * main.c
  3. *
  4. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  5. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  6. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  7. *
  8. * This file is part of LED-Cube.
  9. *
  10. * LED-Cube is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * LED-Cube is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #ifndef F_CPU
  24. #define F_CPU 16000000L
  25. #endif
  26. #define OK 0x42
  27. #define ERROR 0x23
  28. #include <avr/io.h>
  29. #include <util/delay.h>
  30. #include <avr/interrupt.h>
  31. #include <avr/pgmspace.h>
  32. #include <stdint.h>
  33. #include <stdlib.h>
  34. #include <avr/wdt.h>
  35. #include "serial.h"
  36. #include "cube.h"
  37. #include "time.h"
  38. #include "audio.h"
  39. #include "mem.h"
  40. #include "memLayer.h"
  41. #include "twi.h"
  42. #include "strings.h"
  43. #include "visualizer.h"
  44. #define NOERROR 0
  45. // Audio does not answer
  46. #define AUDIOERROR 1
  47. // Memory does not answer
  48. #define MEMORYERROR 2
  49. // Memory not writeable
  50. #define MEMORYWRITEERROR 4
  51. // x = errorcode, e = error definition, not NOERROR
  52. #define ISERROR(x, e) ((x) & (e))
  53. // Length of an idle animation frame, 24 -> 1 second
  54. #define IDLELENGTH 48
  55. void serialHandler(char c);
  56. void sendAudioData(void);
  57. void recieveAnimations(void);
  58. void transmitAnimations(void);
  59. uint8_t audioModeSelected(void);
  60. #ifdef DEBUG
  61. void printErrors(uint8_t e);
  62. uint8_t selfTest(void);
  63. void printTime(void);
  64. #include "snake.c"
  65. #endif
  66. uint8_t shouldRestart = 0;
  67. uint8_t refreshAnimationCount = 1;
  68. uint8_t lastButtonState = 0;
  69. uint8_t mcusr_mirror;
  70. char buffer[11];
  71. uint8_t defaultImageA[64] = { 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff,
  72. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff,
  73. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff,
  74. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff,
  75. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff,
  76. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff,
  77. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff,
  78. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff };
  79. uint8_t defaultImageB[64] = { 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81,
  80. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81,
  81. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81,
  82. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81,
  83. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81,
  84. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81,
  85. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81,
  86. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81 };
  87. uint8_t defaultImageC[64] = { 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02,
  88. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02,
  89. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02,
  90. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02,
  91. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02,
  92. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02,
  93. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02,
  94. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02 };
  95. uint8_t defaultImageCube[64] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  96. 0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF,
  97. 0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF,
  98. 0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF,
  99. 0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF,
  100. 0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF,
  101. 0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF,
  102. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  103. #define IDLEANIMATIONCOUNT 3
  104. uint8_t *idleAnimation[IDLEANIMATIONCOUNT] = { defaultImageA, defaultImageB, defaultImageC };
  105. uint8_t DebugDone = 0; // Bit 0: 10s int. count, Bit 1: unused
  106. // Bit 2: state changed, disable idle
  107. int main(void) {
  108. uint8_t *audioData = NULL;
  109. uint8_t *imageData = NULL;
  110. uint8_t i, length = 0, lastMode;
  111. uint16_t count;
  112. uint64_t lastChecked;
  113. uint8_t idleCounter = 0;
  114. #ifdef DEBUG
  115. uint32_t temp;
  116. #endif
  117. mcusr_mirror = MCUCSR;
  118. MCUCSR = 0;
  119. wdt_disable();
  120. DDRA = 0xFF; // Latch Data Bus as Output
  121. DDRD = 0xFC; DDRB = 24; // Mosfets as Output
  122. DDRC = 0xFC; DDRB |= 6; // Latch Enable as Output
  123. DDRB &= ~(1 << PB0); // Pushbutton as Input
  124. initCube();
  125. serialInit(25, 8, NONE, 1);
  126. i2c_init();
  127. initSystemTimer();
  128. sei(); // Enable Interrupts
  129. #ifndef DEBUG
  130. // wdt_enable(WDTO_500MS); // Enable watchdog reset after 500ms
  131. wdt_enable(WDTO_1S); // Watchdog reset after 1 second
  132. #endif
  133. setImage(defaultImageCube); // Display something
  134. #ifdef DEBUG
  135. // Kill animation counter in debug mode
  136. // => Don't preserve animations while power down
  137. setAnimationCount(0);
  138. i = selfTest();
  139. if (i) {
  140. serialWriteString(getString(1));
  141. serialWriteString(itoa(i, buffer, 2));
  142. serialWrite('\n');
  143. printErrors(i);
  144. }
  145. serialWriteString(getString(2));
  146. serialWriteString(getString(0));
  147. serialWriteString("Took ");
  148. serialWriteString(itoa(getSystemTime(), buffer, 10));
  149. serialWriteString(" ms!\n");
  150. if (mcusr_mirror & WDRF) {
  151. serialWriteString(getString(31));
  152. } else if (mcusr_mirror & BORF) {
  153. serialWriteString(getString(32));
  154. } else if (mcusr_mirror & EXTRF) {
  155. serialWriteString(getString(34));
  156. } else if (mcusr_mirror & JTRF) {
  157. serialWriteString(getString(35));
  158. } else if (mcusr_mirror & PORF) {
  159. serialWriteString(getString(36));
  160. } else {
  161. serialWriteString(getString(33));
  162. }
  163. #endif
  164. lastMode = audioModeSelected();
  165. lastChecked = getSystemTime();
  166. i = 0;
  167. count = getAnimationCount();
  168. while (1) {
  169. // Reset if requested
  170. if (!shouldRestart) {
  171. wdt_reset();
  172. }
  173. if(lastMode) {
  174. // Get Audio Data and visualize it
  175. if (isFinished()) {
  176. audioData = getAudioData(); // Not malloc'ed => Don't free
  177. if (audioData != NULL) {
  178. simpleVisualization(audioData);
  179. }
  180. }
  181. } else {
  182. if (refreshAnimationCount) {
  183. // Get animation count stored in FRAM via TWI, if needed
  184. count = getAnimationCount();
  185. refreshAnimationCount = 0;
  186. i = 0;
  187. }
  188. if (count > 0) {
  189. if (isFinished() > length) {
  190. // Load next image
  191. if (i < (count - 1)) {
  192. i++;
  193. } else {
  194. i = 0;
  195. }
  196. imageData = getFrame(i);
  197. length = imageData[64];
  198. setImage(imageData);
  199. free(imageData);
  200. }
  201. } else {
  202. if (!(DebugDone & 4)) { // Idle animation allowed
  203. if (isFinished() >= IDLELENGTH) {
  204. // Should happen every half second
  205. setImage(idleAnimation[idleCounter]);
  206. if (idleCounter < (IDLEANIMATIONCOUNT - 1)) {
  207. idleCounter++;
  208. } else {
  209. idleCounter = 0;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. if (serialHasChar()) {
  216. serialHandler((char)(serialGet()));
  217. }
  218. #ifdef DEBUG
  219. // Print frames per second
  220. if ((getSystemTime() >= 1000) && ((DebugDone & 1) == 0)) {
  221. temp = getTriggerCount();
  222. serialWriteString(ltoa(temp, buffer, 10));
  223. serialWriteString(getString(27));
  224. serialWriteString(ltoa((temp / 8), buffer, 10));
  225. serialWriteString(getString(28));
  226. DebugDone |= 1;
  227. }
  228. // Show how stable we are running :)
  229. if (((getSystemTime() % 60000) == 0) && (getSystemTime() > 0)) {
  230. serialWriteString(getString(37));
  231. printTime();
  232. }
  233. #endif
  234. if ((getSystemTime() - lastChecked) > 150) {
  235. lastMode = audioModeSelected();
  236. lastChecked = getSystemTime();
  237. }
  238. }
  239. close();
  240. return 0;
  241. }
  242. #ifdef DEBUG
  243. uint8_t selfTest(void) {
  244. uint8_t result = NOERROR;
  245. uint8_t *data = getAudioData();
  246. if (data == NULL) {
  247. result |= AUDIOERROR;
  248. } else {
  249. free(data);
  250. }
  251. data = memGetBytes(0, 1);
  252. if (data == NULL) {
  253. result |= MEMORYERROR;
  254. } else {
  255. free(data);
  256. }
  257. setGeneralPurposeByte(0, 0x23);
  258. if (getGeneralPurposeByte(0) != 0x23) {
  259. result |= MEMORYWRITEERROR;
  260. }
  261. return result;
  262. }
  263. void printErrors(uint8_t e) {
  264. if (ISERROR(e, AUDIOERROR)) {
  265. serialWriteString(getString(3));
  266. }
  267. if (ISERROR(e, MEMORYERROR)) {
  268. serialWriteString(getString(4));
  269. }
  270. if (ISERROR(e, MEMORYWRITEERROR)) {
  271. serialWriteString(getString(5));
  272. }
  273. }
  274. void randomAnimation(void) {
  275. uint8_t *b = (uint8_t *)malloc(64);
  276. uint8_t x, y, z;
  277. if (b == NULL) {
  278. serialWriteString(getString(24));
  279. return;
  280. }
  281. for (x = 0; x < 64; x++) {
  282. b[x] = 0;
  283. }
  284. while(1) {
  285. setImage(b);
  286. while(isFinished() == 0);
  287. x = rand() / 4096;
  288. y = rand() / 4096;
  289. z = rand() / 4096;
  290. b[x + (8 * y)] ^= (1 << z);
  291. if (serialHasChar()) {
  292. serialWriteString(getString(25));
  293. free(b);
  294. serialHandler(serialGet());
  295. return;
  296. }
  297. }
  298. free(b);
  299. }
  300. #endif
  301. void serialHandler(char c) {
  302. // Used letters:
  303. // a, c, d, e, g, i, n, q, r, s, t, v, x, y, 0, 1, 2, 3
  304. #ifdef DEBUG
  305. uint8_t i, y, z;
  306. serialWrite(c);
  307. serialWriteString(": ");
  308. #endif
  309. switch(c) {
  310. case OK:
  311. serialWrite(OK);
  312. break;
  313. case 'h': case 'H': case '?':
  314. serialWriteString(getString(6));
  315. #ifdef DEBUG
  316. serialWriteString(getString(7));
  317. serialWriteString(getString(8));
  318. serialWriteString(getString(9));
  319. serialWriteString(getString(10));
  320. serialWriteString(getString(11));
  321. serialWriteString(getString(12));
  322. serialWriteString(getString(13));
  323. serialWriteString(getString(26));
  324. #endif
  325. break;
  326. case 'd': case 'D':
  327. clearMem();
  328. #ifndef DEBUG
  329. serialWrite(OK);
  330. #endif
  331. #ifdef DEBUG
  332. serialWriteString(getString(29));
  333. #endif
  334. break;
  335. #ifndef DEBUG
  336. case 'g': case 'G':
  337. transmitAnimations();
  338. break;
  339. case 's': case 'S':
  340. recieveAnimations();
  341. break;
  342. #endif
  343. case 'v': case 'V':
  344. serialWriteString(getString(0));
  345. break;
  346. #ifdef DEBUG
  347. case 'm': case 'M':
  348. lastButtonState = !lastButtonState;
  349. serialWriteString(getString(40));
  350. break;
  351. case 'q': case 'Q':
  352. shouldRestart = 1;
  353. serialWriteString(getString(30));
  354. break;
  355. case 'r': case 'R':
  356. randomAnimation();
  357. break;
  358. case 't': case 'T':
  359. printTime();
  360. break;
  361. case 'a': case 'A':
  362. sendAudioData();
  363. break;
  364. case 'c': case 'C':
  365. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  366. serialWriteString(getString(15));
  367. break;
  368. case 'x': case 'X':
  369. // Get byte, store as animation count
  370. serialWriteString(getString(16));
  371. while (!serialHasChar());
  372. c = serialGet();
  373. setAnimationCount(c);
  374. serialWriteString(itoa(c, buffer, 10));
  375. serialWriteString(getString(17));
  376. break;
  377. case 'y': case 'Y':
  378. setAnimationCount(0x2201);
  379. serialWriteString(getString(18));
  380. break;
  381. case 'e': case 'E':
  382. c = selfTest();
  383. serialWriteString(getString(19));
  384. serialWriteString(itoa(c, buffer, 2));
  385. serialWrite('\n');
  386. printErrors(c);
  387. break;
  388. case 'n': case 'N':
  389. snake();
  390. break;
  391. case '0':
  392. fillBuffer(0);
  393. DebugDone |= 4;
  394. break;
  395. case '1':
  396. fillBuffer(0xFF);
  397. DebugDone |= 4;
  398. break;
  399. case '3':
  400. setImage(defaultImageCube);
  401. DebugDone |= 4;
  402. break;
  403. case '2':
  404. DebugDone |= 4;
  405. fillBuffer(0);
  406. for (i = 0; i < 64; i++) {
  407. defaultImageA[i] = 0;
  408. }
  409. while(1) {
  410. for (i = 0; i < 8; i++) {
  411. for (y = 0; y < 8; y++) {
  412. defaultImageA[y + (i * 8)] = 0;
  413. for (z = 0; z < 8; z++) {
  414. defaultImageA[y + (i * 8)] |= (1 << z);
  415. setImage(defaultImageA);
  416. while (isFinished() == 0) {
  417. if (serialHasChar()) {
  418. goto killMeForIt; // Yes I know...
  419. // But I need to break out of 2 while Loops...
  420. }
  421. }
  422. }
  423. defaultImageA[y + (i * 8)] = 0;
  424. }
  425. }
  426. }
  427. break;
  428. killMeForIt:
  429. serialGet();
  430. serialWriteString(getString(25));
  431. break;
  432. case 'I': case 'i':
  433. serialWriteString(ltoa(getTriggerCount(), buffer, 10));
  434. serialWrite('\n');
  435. break;
  436. #endif
  437. default:
  438. serialWrite(ERROR);
  439. break;
  440. }
  441. // c was used as temp var and does not contain the char anymore...!
  442. }
  443. #ifdef DEBUG
  444. void printTime(void) {
  445. serialWriteString(getString(14));
  446. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  447. serialWriteString("ms");
  448. if (getSystemTime() > 60000) {
  449. serialWriteString(" (");
  450. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  451. serialWriteString(" min)");
  452. }
  453. if (getSystemTime() > 1000) {
  454. serialWriteString(" (");
  455. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  456. itoa(getSystemTime() % 1000, buffer, 10);
  457. if (buffer[0] != '\0')
  458. serialWrite('.');
  459. if (buffer[2] == '\0')
  460. serialWrite('0');
  461. if (buffer[1] == '\0')
  462. serialWrite('0');
  463. if (buffer[0] != '\0')
  464. serialWriteString(buffer);
  465. serialWriteString("s)\n");
  466. } else {
  467. serialWrite('\n');
  468. }
  469. }
  470. void sendAudioData(void) {
  471. uint8_t i;
  472. uint8_t *audioData = getAudioData();
  473. if (audioData == NULL) {
  474. serialWriteString(getString(21));
  475. } else {
  476. serialWriteString(getString(22));
  477. for (i = 0; i < 7; i++) {
  478. serialWrite(i + '0');
  479. serialWriteString(": ");
  480. itoa(audioData[i], buffer, 10);
  481. serialWriteString(buffer);
  482. serialWrite('\n');
  483. }
  484. }
  485. }
  486. #endif
  487. void recieveAnimations() {
  488. uint8_t animCount, a, frameCount, f, i;
  489. uint16_t completeCount = 0, character;
  490. uint8_t frame[65];
  491. serialWrite(OK); // We are ready...
  492. character = serialGet();
  493. while (character & 0xFF00) { // Wait for answer
  494. character = serialGet();
  495. }
  496. animCount = (uint8_t)(character & 0x00FF); // Got animation count
  497. serialWrite(OK);
  498. for (a = 0; a < animCount; a++) {
  499. character = serialGet();
  500. while (character & 0xFF00) { // Wait for answer
  501. character = serialGet();
  502. }
  503. frameCount = (uint8_t)(character & 0x00FF); // Got frame count
  504. serialWrite(OK);
  505. for (f = 0; f < frameCount; f++) {
  506. character = serialGet();
  507. while (character & 0xFF00) { // Wait for answer
  508. character = serialGet();
  509. }
  510. frame[64] = (uint8_t)(character & 0x00FF); // Got duration
  511. serialWrite(OK);
  512. for (i = 0; i < 64; i++) {
  513. character = serialGet();
  514. while (character & 0xFF00) { // Wait for answer
  515. character = serialGet();
  516. }
  517. frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
  518. }
  519. serialWrite(OK);
  520. setFrame(completeCount++, frame);
  521. }
  522. }
  523. character = serialGet();
  524. while (character & 0xFF00) { // Wait for answer
  525. character = serialGet();
  526. }
  527. character = serialGet();
  528. while (character & 0xFF00) { // Wait for answer
  529. character = serialGet();
  530. }
  531. character = serialGet();
  532. while (character & 0xFF00) { // Wait for answer
  533. character = serialGet();
  534. }
  535. character = serialGet();
  536. while (character & 0xFF00) { // Wait for answer
  537. character = serialGet();
  538. }
  539. serialWrite(OK);
  540. setAnimationCount(completeCount);
  541. refreshAnimationCount = 1;
  542. }
  543. void transmitAnimations() {
  544. // We store no animation information in here
  545. // So we have to place all frames in one or more
  546. // animations... We need 8 animations max...
  547. uint8_t animationsToGo;
  548. uint16_t framesToGo = getAnimationCount();
  549. uint16_t character;
  550. uint8_t a;
  551. uint8_t f, fMax, i;
  552. uint8_t *frame;
  553. if ((framesToGo % 255) == 0) {
  554. animationsToGo = framesToGo / 255;
  555. } else {
  556. animationsToGo = (framesToGo / 255) + 1;
  557. }
  558. serialWrite(OK);
  559. serialWrite(animationsToGo);
  560. while ((character = serialGet()) & 0xFF00); // Wait for answer
  561. if ((character & 0x00FF) != OK) { // Error code recieved
  562. return;
  563. }
  564. for (a = 0; a < animationsToGo; a++) {
  565. if (framesToGo > 255) {
  566. fMax = 255;
  567. } else {
  568. fMax = framesToGo;
  569. }
  570. serialWrite(fMax); // Number of Frames in current animation
  571. while ((character = serialGet()) & 0xFF00); // Wait for answer
  572. if ((character & 0x00FF) != OK) { // Error code recieved
  573. return;
  574. }
  575. for (f = 0; f < fMax; f++) {
  576. frame = getFrame(f + (255 * a));
  577. serialWrite(frame[64]); // frame duration
  578. while ((character = serialGet()) & 0xFF00); // Wait for answer
  579. if ((character & 0x00FF) != OK) { // Error code recieved
  580. free(frame);
  581. return;
  582. }
  583. for (i = 0; i < 64; i++) {
  584. serialWrite(frame[i]);
  585. }
  586. while ((character = serialGet()) & 0xFF00); // Wait for answer
  587. if ((character & 0x00FF) != OK) { // Error code recieved
  588. free(frame);
  589. return;
  590. }
  591. free(frame);
  592. }
  593. framesToGo -= fMax;
  594. }
  595. serialWrite(OK);
  596. serialWrite(OK);
  597. serialWrite(OK);
  598. serialWrite(OK);
  599. while ((character = serialGet()) & 0xFF00); // Wait for answer
  600. // Error code ignored...
  601. }
  602. uint8_t audioModeSelected(void) {
  603. // Pushbutton: PB0, Low active
  604. if (!(PINB & (1 << PB0))) {
  605. // Button pushed
  606. if (lastButtonState == 0) {
  607. lastButtonState = 1;
  608. } else {
  609. lastButtonState = 0;
  610. }
  611. #ifdef DEBUG
  612. if (lastButtonState) {
  613. serialWriteString(getString(38));
  614. } else {
  615. serialWriteString(getString(39));
  616. }
  617. #endif
  618. }
  619. return lastButtonState;
  620. }