Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.c 15KB

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