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

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