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 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. #define NOERROR 0
  44. // Audio does not answer
  45. #define AUDIOERROR 1
  46. // Memory does not answer
  47. #define MEMORYERROR 2
  48. // Memory not writeable
  49. #define MEMORYWRITEERROR 4
  50. // x = errorcode, e = error definition, not NOERROR
  51. #define ISERROR(x, e) ((x) & (e))
  52. // Length of an idle animation frame, 24 -> 1 second
  53. #define IDLELENGTH 48
  54. void serialHandler(char c);
  55. void sendAudioData(void);
  56. void recieveAnimations(void);
  57. void transmitAnimations(void);
  58. uint8_t audioModeSelected(void);
  59. void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
  60. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf);
  61. void visualizeAudioData(uint8_t *audioData, uint8_t *imageData);
  62. #ifdef DEBUG
  63. void printErrors(uint8_t e);
  64. uint8_t selfTest(void);
  65. void printTime(void);
  66. #include "snake.c"
  67. #endif
  68. uint8_t shouldRestart = 0;
  69. uint8_t refreshAnimationCount = 1;
  70. uint8_t lastButtonState = 0;
  71. uint8_t mcusr_mirror;
  72. char buffer[11];
  73. uint8_t defaultImageA[64] = { 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. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff,
  80. 0xff, 0x01, 0x01, 0x01, 0xff, 0x80, 0x80, 0xff };
  81. uint8_t defaultImageB[64] = { 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. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81,
  88. 0x7e, 0x81, 0x81, 0x81, 0xff, 0x81, 0x81, 0x81 };
  89. uint8_t defaultImageC[64] = { 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. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02,
  96. 0x1e, 0x22, 0x22, 0x22, 0x1e, 0x02, 0x02, 0x02 };
  97. uint8_t defaultImageCube[64] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 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, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF,
  103. 0xFF, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xFF,
  104. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  105. #define IDLEANIMATIONCOUNT 3
  106. uint8_t *idleAnimation[IDLEANIMATIONCOUNT] = { defaultImageA, defaultImageB, defaultImageC };
  107. uint8_t DebugDone = 0; // Bit 0: 10s int. count, Bit 1: unused
  108. // Bit 2: state changed, disable idle
  109. int main(void) {
  110. uint8_t *audioData = NULL;
  111. uint8_t *imageData = NULL;
  112. uint8_t i, length = 0, lastMode;
  113. uint16_t count;
  114. uint64_t lastChecked;
  115. uint8_t idleCounter = 0;
  116. #ifdef DEBUG
  117. uint32_t temp;
  118. #endif
  119. mcusr_mirror = MCUCSR;
  120. MCUCSR = 0;
  121. wdt_disable();
  122. DDRA = 0xFF; // Latch Data Bus as Output
  123. DDRD = 0xFC; DDRB = 24; // Mosfets as Output
  124. DDRC = 0xFC; DDRB |= 6; // Latch Enable as Output
  125. DDRB &= ~(1 << PB0); // Pushbutton as Input
  126. initCube();
  127. serialInit(25, 8, NONE, 1);
  128. i2c_init();
  129. initSystemTimer();
  130. sei(); // Enable Interrupts
  131. #ifndef DEBUG
  132. // wdt_enable(WDTO_500MS); // Enable watchdog reset after 500ms
  133. wdt_enable(WDTO_1S); // Watchdog reset after 1 second
  134. #endif
  135. setImage(defaultImageCube); // Display something
  136. #ifdef DEBUG
  137. // Kill animation counter in debug mode
  138. // => Don't preserve animations while power down
  139. setAnimationCount(0);
  140. i = selfTest();
  141. if (i) {
  142. serialWriteString(getString(1));
  143. serialWriteString(itoa(i, buffer, 2));
  144. serialWrite('\n');
  145. printErrors(i);
  146. }
  147. serialWriteString(getString(2));
  148. serialWriteString(getString(0));
  149. serialWriteString("Took ");
  150. serialWriteString(itoa(getSystemTime(), buffer, 10));
  151. serialWriteString(" ms!\n");
  152. if (mcusr_mirror & WDRF) {
  153. serialWriteString(getString(31));
  154. } else if (mcusr_mirror & BORF) {
  155. serialWriteString(getString(32));
  156. } else if (mcusr_mirror & EXTRF) {
  157. serialWriteString(getString(34));
  158. } else if (mcusr_mirror & JTRF) {
  159. serialWriteString(getString(35));
  160. } else if (mcusr_mirror & PORF) {
  161. serialWriteString(getString(36));
  162. } else {
  163. serialWriteString(getString(33));
  164. }
  165. #endif
  166. lastMode = audioModeSelected();
  167. lastChecked = getSystemTime();
  168. i = 0;
  169. count = getAnimationCount();
  170. while (1) {
  171. // Reset if requested
  172. if (!shouldRestart) {
  173. wdt_reset();
  174. }
  175. if(lastMode) {
  176. // Get Audio Data and visualize it
  177. if (isFinished()) {
  178. audioData = getAudioData(); // Not malloc'ed => Don't free
  179. if (audioData != NULL) {
  180. imageData = (uint8_t *)malloc(64);
  181. if (imageData == NULL) {
  182. #ifdef DEBUG
  183. serialWriteString(getString(24));
  184. #endif
  185. while(1); // Ran out of heap => Watchdog Reset
  186. }
  187. visualizeAudioData(audioData, imageData);
  188. setImage(imageData);
  189. free(imageData);
  190. }
  191. }
  192. } else {
  193. if (refreshAnimationCount) {
  194. // Get animation count stored in FRAM via TWI, if needed
  195. count = getAnimationCount();
  196. refreshAnimationCount = 0;
  197. i = 0;
  198. }
  199. if (count > 0) {
  200. if (isFinished() > length) {
  201. // Load next image
  202. if (i < (count - 1)) {
  203. i++;
  204. } else {
  205. i = 0;
  206. }
  207. imageData = getFrame(i);
  208. length = imageData[64];
  209. setImage(imageData);
  210. free(imageData);
  211. }
  212. } else {
  213. if (!(DebugDone & 4)) { // Idle animation allowed
  214. if (isFinished() >= IDLELENGTH) {
  215. // Should happen every half second
  216. setImage(idleAnimation[idleCounter]);
  217. if (idleCounter < (IDLEANIMATIONCOUNT - 1)) {
  218. idleCounter++;
  219. } else {
  220. idleCounter = 0;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. if (serialHasChar()) {
  227. serialHandler((char)(serialGet()));
  228. }
  229. #ifdef DEBUG
  230. // Print frames per second
  231. if ((getSystemTime() >= 1000) && ((DebugDone & 1) == 0)) {
  232. temp = getTriggerCount();
  233. serialWriteString(ltoa(temp, buffer, 10));
  234. serialWriteString(getString(27));
  235. serialWriteString(ltoa((temp / 8), buffer, 10));
  236. serialWriteString(getString(28));
  237. DebugDone |= 1;
  238. }
  239. // Show how stable we are running :)
  240. if (((getSystemTime() % 60000) == 0) && (getSystemTime() > 0)) {
  241. serialWriteString(getString(37));
  242. printTime();
  243. }
  244. #endif
  245. if ((getSystemTime() - lastChecked) > 150) {
  246. lastMode = audioModeSelected();
  247. lastChecked = getSystemTime();
  248. }
  249. }
  250. close();
  251. return 0;
  252. }
  253. #ifdef DEBUG
  254. uint8_t selfTest(void) {
  255. uint8_t result = NOERROR;
  256. uint8_t *data = getAudioData();
  257. if (data == NULL) {
  258. result |= AUDIOERROR;
  259. } else {
  260. free(data);
  261. }
  262. data = memGetBytes(0, 1);
  263. if (data == NULL) {
  264. result |= MEMORYERROR;
  265. } else {
  266. free(data);
  267. }
  268. setGeneralPurposeByte(0, 0x23);
  269. if (getGeneralPurposeByte(0) != 0x23) {
  270. result |= MEMORYWRITEERROR;
  271. }
  272. return result;
  273. }
  274. void printErrors(uint8_t e) {
  275. if (ISERROR(e, AUDIOERROR)) {
  276. serialWriteString(getString(3));
  277. }
  278. if (ISERROR(e, MEMORYERROR)) {
  279. serialWriteString(getString(4));
  280. }
  281. if (ISERROR(e, MEMORYWRITEERROR)) {
  282. serialWriteString(getString(5));
  283. }
  284. }
  285. void randomAnimation(void) {
  286. uint8_t *b = (uint8_t *)malloc(64);
  287. uint8_t x, y, z;
  288. if (b == NULL) {
  289. serialWriteString(getString(24));
  290. return;
  291. }
  292. for (x = 0; x < 64; x++) {
  293. b[x] = 0;
  294. }
  295. while(1) {
  296. setImage(b);
  297. while(isFinished() == 0);
  298. x = rand() / 4096;
  299. y = rand() / 4096;
  300. z = rand() / 4096;
  301. b[x + (8 * y)] ^= (1 << z);
  302. if (serialHasChar()) {
  303. serialWriteString(getString(25));
  304. free(b);
  305. serialHandler(serialGet());
  306. return;
  307. }
  308. }
  309. free(b);
  310. }
  311. #endif
  312. void serialHandler(char c) {
  313. // Used letters:
  314. // a, c, d, e, g, i, n, q, r, s, t, v, x, y, 0, 1, 2, 3
  315. #ifdef DEBUG
  316. uint8_t i, y, z;
  317. serialWrite(c);
  318. serialWriteString(": ");
  319. #endif
  320. switch(c) {
  321. case OK:
  322. serialWrite(OK);
  323. break;
  324. case 'h': case 'H': case '?':
  325. serialWriteString(getString(6));
  326. #ifdef DEBUG
  327. serialWriteString(getString(7));
  328. serialWriteString(getString(8));
  329. serialWriteString(getString(9));
  330. serialWriteString(getString(10));
  331. serialWriteString(getString(11));
  332. serialWriteString(getString(12));
  333. serialWriteString(getString(13));
  334. serialWriteString(getString(26));
  335. #endif
  336. break;
  337. case 'd': case 'D':
  338. clearMem();
  339. #ifndef DEBUG
  340. serialWrite(OK);
  341. #endif
  342. #ifdef DEBUG
  343. serialWriteString(getString(29));
  344. #endif
  345. break;
  346. #ifndef DEBUG
  347. case 'g': case 'G':
  348. transmitAnimations();
  349. break;
  350. case 's': case 'S':
  351. recieveAnimations();
  352. break;
  353. #endif
  354. case 'v': case 'V':
  355. serialWriteString(getString(0));
  356. break;
  357. #ifdef DEBUG
  358. case 'm': case 'M':
  359. lastButtonState = !lastButtonState;
  360. serialWriteString(getString(40));
  361. break;
  362. case 'q': case 'Q':
  363. shouldRestart = 1;
  364. serialWriteString(getString(30));
  365. break;
  366. case 'r': case 'R':
  367. randomAnimation();
  368. break;
  369. case 't': case 'T':
  370. printTime();
  371. break;
  372. case 'a': case 'A':
  373. sendAudioData();
  374. break;
  375. case 'c': case 'C':
  376. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  377. serialWriteString(getString(15));
  378. break;
  379. case 'x': case 'X':
  380. // Get byte, store as animation count
  381. serialWriteString(getString(16));
  382. while (!serialHasChar());
  383. c = serialGet();
  384. setAnimationCount(c);
  385. serialWriteString(itoa(c, buffer, 10));
  386. serialWriteString(getString(17));
  387. break;
  388. case 'y': case 'Y':
  389. setAnimationCount(0x2201);
  390. serialWriteString(getString(18));
  391. break;
  392. case 'e': case 'E':
  393. c = selfTest();
  394. serialWriteString(getString(19));
  395. serialWriteString(itoa(c, buffer, 2));
  396. serialWrite('\n');
  397. printErrors(c);
  398. break;
  399. case 'n': case 'N':
  400. snake();
  401. break;
  402. case '0':
  403. fillBuffer(0);
  404. DebugDone |= 4;
  405. break;
  406. case '1':
  407. fillBuffer(0xFF);
  408. DebugDone |= 4;
  409. break;
  410. case '3':
  411. setImage(defaultImageCube);
  412. DebugDone |= 4;
  413. break;
  414. case '2':
  415. DebugDone |= 4;
  416. fillBuffer(0);
  417. for (i = 0; i < 64; i++) {
  418. defaultImageA[i] = 0;
  419. }
  420. while(1) {
  421. for (i = 0; i < 8; i++) {
  422. for (y = 0; y < 8; y++) {
  423. defaultImageA[y + (i * 8)] = 0;
  424. for (z = 0; z < 8; z++) {
  425. defaultImageA[y + (i * 8)] |= (1 << z);
  426. setImage(defaultImageA);
  427. while (isFinished() == 0) {
  428. if (serialHasChar()) {
  429. goto killMeForIt; // Yes I know...
  430. // But I need to break out of 2 while Loops...
  431. }
  432. }
  433. }
  434. defaultImageA[y + (i * 8)] = 0;
  435. }
  436. }
  437. }
  438. break;
  439. killMeForIt:
  440. serialGet();
  441. serialWriteString(getString(25));
  442. break;
  443. case 'I': case 'i':
  444. serialWriteString(ltoa(getTriggerCount(), buffer, 10));
  445. serialWrite('\n');
  446. break;
  447. #endif
  448. default:
  449. serialWrite(ERROR);
  450. break;
  451. }
  452. // c was used as temp var and does not contain the char anymore...!
  453. }
  454. #ifdef DEBUG
  455. void printTime(void) {
  456. serialWriteString(getString(14));
  457. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  458. serialWriteString("ms");
  459. if (getSystemTime() > 60000) {
  460. serialWriteString(" (");
  461. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  462. serialWriteString(" min)");
  463. }
  464. if (getSystemTime() > 1000) {
  465. serialWriteString(" (");
  466. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  467. itoa(getSystemTime() % 1000, buffer, 10);
  468. if (buffer[0] != '\0')
  469. serialWrite('.');
  470. if (buffer[2] == '\0')
  471. serialWrite('0');
  472. if (buffer[1] == '\0')
  473. serialWrite('0');
  474. if (buffer[0] != '\0')
  475. serialWriteString(buffer);
  476. serialWriteString("s)\n");
  477. } else {
  478. serialWrite('\n');
  479. }
  480. }
  481. void sendAudioData(void) {
  482. uint8_t i;
  483. uint8_t *audioData = getAudioData();
  484. if (audioData == NULL) {
  485. serialWriteString(getString(21));
  486. } else {
  487. serialWriteString(getString(22));
  488. for (i = 0; i < 7; i++) {
  489. serialWrite(i + '0');
  490. serialWriteString(": ");
  491. itoa(audioData[i], buffer, 10);
  492. serialWriteString(buffer);
  493. serialWrite('\n');
  494. }
  495. }
  496. }
  497. #endif
  498. void recieveAnimations() {
  499. uint8_t animCount, a, frameCount, f, i;
  500. uint16_t completeCount = 0, character;
  501. uint8_t frame[65];
  502. serialWrite(OK); // We are ready...
  503. character = serialGet();
  504. while (character & 0xFF00) { // Wait for answer
  505. character = serialGet();
  506. }
  507. animCount = (uint8_t)(character & 0x00FF); // Got animation count
  508. serialWrite(OK);
  509. for (a = 0; a < animCount; a++) {
  510. character = serialGet();
  511. while (character & 0xFF00) { // Wait for answer
  512. character = serialGet();
  513. }
  514. frameCount = (uint8_t)(character & 0x00FF); // Got frame count
  515. serialWrite(OK);
  516. for (f = 0; f < frameCount; f++) {
  517. character = serialGet();
  518. while (character & 0xFF00) { // Wait for answer
  519. character = serialGet();
  520. }
  521. frame[64] = (uint8_t)(character & 0x00FF); // Got duration
  522. serialWrite(OK);
  523. for (i = 0; i < 64; i++) {
  524. character = serialGet();
  525. while (character & 0xFF00) { // Wait for answer
  526. character = serialGet();
  527. }
  528. frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
  529. }
  530. serialWrite(OK);
  531. setFrame(completeCount++, frame);
  532. }
  533. }
  534. character = serialGet();
  535. while (character & 0xFF00) { // Wait for answer
  536. character = serialGet();
  537. }
  538. character = serialGet();
  539. while (character & 0xFF00) { // Wait for answer
  540. character = serialGet();
  541. }
  542. character = serialGet();
  543. while (character & 0xFF00) { // Wait for answer
  544. character = serialGet();
  545. }
  546. character = serialGet();
  547. while (character & 0xFF00) { // Wait for answer
  548. character = serialGet();
  549. }
  550. serialWrite(OK);
  551. setAnimationCount(completeCount);
  552. refreshAnimationCount = 1;
  553. }
  554. void transmitAnimations() {
  555. // We store no animation information in here
  556. // So we have to place all frames in one or more
  557. // animations... We need 8 animations max...
  558. uint8_t animationsToGo;
  559. uint16_t framesToGo = getAnimationCount();
  560. uint16_t character;
  561. uint8_t a;
  562. uint8_t f, fMax, i;
  563. uint8_t *frame;
  564. if ((framesToGo % 255) == 0) {
  565. animationsToGo = framesToGo / 255;
  566. } else {
  567. animationsToGo = (framesToGo / 255) + 1;
  568. }
  569. serialWrite(OK);
  570. serialWrite(animationsToGo);
  571. while ((character = serialGet()) & 0xFF00); // Wait for answer
  572. if ((character & 0x00FF) != OK) { // Error code recieved
  573. return;
  574. }
  575. for (a = 0; a < animationsToGo; a++) {
  576. if (framesToGo > 255) {
  577. fMax = 255;
  578. } else {
  579. fMax = framesToGo;
  580. }
  581. serialWrite(fMax); // Number of Frames in current animation
  582. while ((character = serialGet()) & 0xFF00); // Wait for answer
  583. if ((character & 0x00FF) != OK) { // Error code recieved
  584. return;
  585. }
  586. for (f = 0; f < fMax; f++) {
  587. frame = getFrame(f + (255 * a));
  588. serialWrite(frame[64]); // frame duration
  589. while ((character = serialGet()) & 0xFF00); // Wait for answer
  590. if ((character & 0x00FF) != OK) { // Error code recieved
  591. free(frame);
  592. return;
  593. }
  594. for (i = 0; i < 64; i++) {
  595. serialWrite(frame[i]);
  596. }
  597. while ((character = serialGet()) & 0xFF00); // Wait for answer
  598. if ((character & 0x00FF) != OK) { // Error code recieved
  599. free(frame);
  600. return;
  601. }
  602. free(frame);
  603. }
  604. framesToGo -= fMax;
  605. }
  606. serialWrite(OK);
  607. serialWrite(OK);
  608. serialWrite(OK);
  609. serialWrite(OK);
  610. while ((character = serialGet()) & 0xFF00); // Wait for answer
  611. // Error code ignored...
  612. }
  613. uint8_t audioModeSelected(void) {
  614. // Pushbutton: PB0, Low active
  615. if (!(PINB & (1 << PB0))) {
  616. // Button pushed
  617. if (lastButtonState == 0) {
  618. lastButtonState = 1;
  619. } else {
  620. lastButtonState = 0;
  621. }
  622. #ifdef DEBUG
  623. if (lastButtonState) {
  624. serialWriteString(getString(38));
  625. } else {
  626. serialWriteString(getString(39));
  627. }
  628. #endif
  629. }
  630. return lastButtonState;
  631. }
  632. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
  633. uint8_t i = 0;
  634. for (; i < height; i++) {
  635. setPixelBuffer(x, i, z, buf);
  636. }
  637. }
  638. void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  639. buf[(8 * z) + (7 - y)] |= (1 << x);
  640. }
  641. void visualizeAudioData(uint8_t *audioData, uint8_t *imageData) {
  642. uint8_t i;
  643. for (i = 0; i < 64; i++) {
  644. imageData[i] = 0;
  645. }
  646. // 8 LEDs, Max Val 255:
  647. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  648. // 255 / FACTOR = 8,...
  649. // 127 / FACTOR = 4,...
  650. #define FACTOR 31
  651. // Could not figure out a way to represent this easily in a loop
  652. // without using a shitload of 'if's...
  653. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  654. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  655. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  656. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  657. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  658. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  659. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  660. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  661. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  662. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  663. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  664. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  665. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  666. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  667. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  668. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  669. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  670. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  671. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  672. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  673. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  674. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  675. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  676. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  677. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  678. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  679. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  680. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  681. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  682. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  683. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  684. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  685. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  686. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  687. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  688. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  689. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  690. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  691. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  692. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  693. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  694. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  695. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  696. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  697. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  698. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  699. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  700. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  701. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  702. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  703. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  704. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  705. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  706. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  707. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  708. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  709. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  710. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  711. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  712. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  713. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  714. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  715. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  716. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  717. }