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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. #ifdef DEBUG
  29. #define VERSION "v2 (Debug Build)\nNOT COMPATIBLE WITH CubeControl!\n"
  30. #else
  31. #define VERSION "v2 Release\n"
  32. #endif
  33. #include <avr/io.h>
  34. #include <util/delay.h>
  35. #include <avr/interrupt.h>
  36. #include <avr/pgmspace.h>
  37. #include <stdint.h>
  38. #include <stdlib.h>
  39. #include "serial.h"
  40. #include "cube.h"
  41. #include "time.h"
  42. #include "audio.h"
  43. #include "mem.h"
  44. #include "memLayer.h"
  45. #include "twi.h"
  46. #define NOERROR 0
  47. // Audio does not answer
  48. #define AUDIOERROR 1
  49. // Memory does not answer
  50. #define MEMORYERROR 2
  51. // Memory not writeable
  52. #define MEMORYWRITEERROR 4
  53. // x = errorcode, e = error definition, not NOERROR
  54. #define ISERROR(x, e) ((x) & (e))
  55. void serialHandler(char c);
  56. void sendAudioData(void);
  57. void recieveAnimations(void);
  58. void transmitAnimations(void);
  59. uint8_t audioModeSelected(void);
  60. void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
  61. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf);
  62. void visualizeAudioData(uint8_t *audioData, uint8_t *imageData);
  63. #ifdef DEBUG
  64. void printErrors(uint8_t e);
  65. uint8_t selfTest(void);
  66. #include "snake.c"
  67. #endif
  68. uint8_t refreshAnimationCount = 1;
  69. uint8_t lastButtonState = 0;
  70. char buffer[11];
  71. uint8_t defaultImage[64] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  72. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  73. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  74. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  75. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  76. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  77. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  78. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  79. int main(void) {
  80. uint8_t *audioData = NULL;
  81. uint8_t *imageData = NULL;
  82. uint8_t i, length = 0, lastMode;
  83. uint16_t count;
  84. uint64_t lastChecked;
  85. initCube();
  86. serialInit(25, 8, NONE, 1);
  87. i2c_init();
  88. initSystemTimer();
  89. sei(); // Enable Interrupts
  90. DDRD = 0xFC; // Mosfets as Output
  91. DDRB = 0xFE;
  92. DDRC = 0xFF; // Latch Enable
  93. DDRA = 0xFF; // Latch Data
  94. setImage(defaultImage); // Display something
  95. #ifdef DEBUG
  96. // Kill animation counter in debug mode
  97. // => Don't preserve animations while power down
  98. setAnimationCount(0);
  99. i = selfTest();
  100. if (i) {
  101. serialWriteString("Self-Test Error: 0b");
  102. serialWriteString(itoa(i, buffer, 2));
  103. serialWrite('\n');
  104. printErrors(i);
  105. }
  106. #endif
  107. #ifdef DEBUG
  108. serialWriteString("\n\nInitialized: ");
  109. serialWriteString(VERSION);
  110. serialWriteString("Took ");
  111. serialWriteString(itoa(getSystemTime(), buffer, 10));
  112. serialWriteString(" ms!\n");
  113. #endif
  114. lastMode = audioModeSelected();
  115. lastChecked = getSystemTime();
  116. i = 0;
  117. count = getAnimationCount();
  118. while (1) {
  119. if(lastMode) {
  120. // Get Audio Data and visualize it
  121. if (isFinished()) {
  122. audioData = getAudioData();
  123. if (audioData != NULL) {
  124. imageData = (uint8_t *)malloc(64);
  125. visualizeAudioData(audioData, imageData);
  126. setImage(imageData);
  127. free(imageData);
  128. free(audioData);
  129. }
  130. }
  131. } else {
  132. if (refreshAnimationCount) {
  133. // Get animation count stored in FRAM via TWI, if needed
  134. count = getAnimationCount();
  135. refreshAnimationCount = 0;
  136. i = 0;
  137. }
  138. if (count > 0) {
  139. if (isFinished() > length) {
  140. // Load next image
  141. if (i < (count - 1)) {
  142. i++;
  143. } else {
  144. i = 0;
  145. }
  146. imageData = getFrame(i);
  147. length = imageData[64];
  148. setImage(imageData);
  149. free(imageData);
  150. }
  151. }
  152. }
  153. if (serialHasChar()) {
  154. serialHandler((char)(serialGet()));
  155. }
  156. if ((getSystemTime() - lastChecked) > 150) {
  157. lastMode = audioModeSelected();
  158. lastChecked = getSystemTime();
  159. }
  160. }
  161. close();
  162. return 0;
  163. }
  164. #ifdef DEBUG
  165. uint8_t selfTest(void) {
  166. uint8_t result = NOERROR;
  167. uint8_t *data = getAudioData();
  168. if (data == NULL) {
  169. result |= AUDIOERROR;
  170. } else {
  171. free(data);
  172. }
  173. data = memGetBytes(0, 1);
  174. if (data == NULL) {
  175. result |= MEMORYERROR;
  176. } else {
  177. free(data);
  178. }
  179. setGeneralPurposeByte(0, 0x42);
  180. if (getGeneralPurposeByte(0) != 0x42) {
  181. result |= MEMORYWRITEERROR;
  182. }
  183. return result;
  184. }
  185. void printErrors(uint8_t e) {
  186. if (ISERROR(e, AUDIOERROR)) {
  187. serialWriteString(" => No answer from Audio!\n");
  188. }
  189. if (ISERROR(e, MEMORYERROR)) {
  190. serialWriteString(" => No answer from Memory!\n");
  191. }
  192. if (ISERROR(e, MEMORYWRITEERROR)) {
  193. serialWriteString(" => Can't write to Memory!\n");
  194. }
  195. }
  196. #endif
  197. void serialHandler(char c) {
  198. // Used letters:
  199. // a, c, d, g, s, t, v, x
  200. #ifdef DEBUG
  201. serialWrite(c);
  202. serialWriteString(": ");
  203. #endif
  204. switch(c) {
  205. case OK:
  206. serialWrite(OK);
  207. break;
  208. case 'h': case 'H': case '?':
  209. serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n");
  210. #ifdef DEBUG
  211. serialWriteString("(t)ime, (a)udio, (c)ount, (x)Custom count\n");
  212. serialWriteString("(y)Set fixed animation count\n");
  213. serialWriteString("S(e)lf Test\n");
  214. serialWriteString("Play S(n)ake\n");
  215. serialWriteString("(0): All LEDs Off\n");
  216. serialWriteString("(1): All LEDs On\n");
  217. #endif
  218. break;
  219. case 'd': case 'D':
  220. clearMem();
  221. serialWrite(OK);
  222. break;
  223. case 'g': case 'G':
  224. transmitAnimations();
  225. break;
  226. case 's': case 'S':
  227. recieveAnimations();
  228. break;
  229. case 'v': case 'V':
  230. serialWriteString(VERSION);
  231. break;
  232. #ifdef DEBUG
  233. case 't': case 'T':
  234. serialWriteString("System Time: ");
  235. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  236. serialWriteString("ms");
  237. if (getSystemTime() > 1000) {
  238. serialWriteString(" (");
  239. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  240. itoa(getSystemTime() % 1000, buffer, 10);
  241. if (buffer[0] != '\0')
  242. serialWrite('.');
  243. if (buffer[2] == '\0')
  244. serialWrite('0');
  245. if (buffer[1] == '\0')
  246. serialWrite('0');
  247. if (buffer[0] != '\0')
  248. serialWriteString(buffer);
  249. serialWriteString("s)\n");
  250. } else {
  251. serialWrite('\n');
  252. }
  253. break;
  254. case 'a': case 'A':
  255. sendAudioData();
  256. break;
  257. case 'c': case 'C':
  258. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  259. serialWriteString(" Frames stored\n");
  260. break;
  261. case 'x': case 'X':
  262. // Get byte, store as animation count
  263. serialWriteString("Send a byte... ");
  264. while (!serialHasChar());
  265. c = serialGet();
  266. setAnimationCount(c);
  267. serialWriteString(itoa(c, buffer, 10));
  268. serialWriteString(" written!\n");
  269. break;
  270. case 'y': case 'Y':
  271. setAnimationCount(0x2201);
  272. serialWriteString("Animation count now 8705!\n");
  273. break;
  274. case 'e': case 'E':
  275. c = selfTest();
  276. serialWriteString("Self-Test: 0b");
  277. serialWriteString(itoa(c, buffer, 2));
  278. serialWrite('\n');
  279. printErrors(c);
  280. break;
  281. case 'n': case 'N':
  282. snake();
  283. break;
  284. case '0':
  285. fillBuffer(0x00);
  286. setAnimationCount(0);
  287. refreshAnimationCount = 1;
  288. serialWriteString("Killed Animation Counter!\n");
  289. break;
  290. case '1':
  291. fillBuffer(0xFF);
  292. setAnimationCount(0);
  293. refreshAnimationCount = 1;
  294. serialWriteString("Killed Animation Counter!\n");
  295. break;
  296. #endif
  297. default:
  298. serialWrite(ERROR);
  299. break;
  300. }
  301. // c was used as temp var and does not contain the char anymore...!
  302. }
  303. #ifdef DEBUG
  304. void sendAudioData(void) {
  305. uint8_t i;
  306. uint8_t *audioData = getAudioData();
  307. if (audioData == NULL) {
  308. serialWriteString("Could not access device!\n");
  309. } else {
  310. serialWriteString("Audio Data:\n");
  311. for (i = 0; i < 7; i++) {
  312. serialWrite(i + '0');
  313. serialWriteString(": ");
  314. itoa(audioData[i], buffer, 10);
  315. serialWriteString(buffer);
  316. serialWrite('\n');
  317. }
  318. free(audioData);
  319. }
  320. }
  321. #endif
  322. void recieveAnimations() {
  323. uint8_t animCount, a, frameCount, f, i;
  324. uint16_t completeCount = 0, character;
  325. uint8_t frame[65];
  326. serialWrite(OK); // We are ready...
  327. character = serialGet();
  328. while (character & 0xFF00) { // Wait for answer
  329. character = serialGet();
  330. }
  331. animCount = (uint8_t)(character & 0x00FF); // Got animation count
  332. serialWrite(OK);
  333. for (a = 0; a < animCount; a++) {
  334. character = serialGet();
  335. while (character & 0xFF00) { // Wait for answer
  336. character = serialGet();
  337. }
  338. frameCount = (uint8_t)(character & 0x00FF); // Got frame count
  339. serialWrite(OK);
  340. for (f = 0; f < frameCount; f++) {
  341. character = serialGet();
  342. while (character & 0xFF00) { // Wait for answer
  343. character = serialGet();
  344. }
  345. frame[64] = (uint8_t)(character & 0x00FF); // Got duration
  346. serialWrite(OK);
  347. for (i = 0; i < 64; i++) {
  348. character = serialGet();
  349. while (character & 0xFF00) { // Wait for answer
  350. character = serialGet();
  351. }
  352. frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
  353. }
  354. serialWrite(OK);
  355. setFrame(completeCount++, frame);
  356. }
  357. }
  358. character = serialGet();
  359. while (character & 0xFF00) { // Wait for answer
  360. character = serialGet();
  361. }
  362. character = serialGet();
  363. while (character & 0xFF00) { // Wait for answer
  364. character = serialGet();
  365. }
  366. character = serialGet();
  367. while (character & 0xFF00) { // Wait for answer
  368. character = serialGet();
  369. }
  370. character = serialGet();
  371. while (character & 0xFF00) { // Wait for answer
  372. character = serialGet();
  373. }
  374. serialWrite(OK);
  375. setAnimationCount(completeCount);
  376. refreshAnimationCount = 1;
  377. }
  378. void transmitAnimations() {
  379. // We store no animation information in here
  380. // So we have to place all frames in one or more
  381. // animations... We need 8 animations max...
  382. uint8_t animationsToGo;
  383. uint16_t framesToGo = getAnimationCount();
  384. uint16_t character;
  385. uint8_t a;
  386. uint8_t f, fMax, i;
  387. uint8_t *frame;
  388. if ((framesToGo % 255) == 0) {
  389. animationsToGo = framesToGo / 255;
  390. } else {
  391. animationsToGo = (framesToGo / 255) + 1;
  392. }
  393. serialWrite(OK);
  394. serialWrite(animationsToGo);
  395. while ((character = serialGet()) & 0xFF00); // Wait for answer
  396. if ((character & 0x00FF) != OK) { // Error code recieved
  397. return;
  398. }
  399. for (a = 0; a < animationsToGo; a++) {
  400. if (framesToGo > 255) {
  401. fMax = 255;
  402. } else {
  403. fMax = framesToGo;
  404. }
  405. serialWrite(fMax); // Number of Frames in current animation
  406. while ((character = serialGet()) & 0xFF00); // Wait for answer
  407. if ((character & 0x00FF) != OK) { // Error code recieved
  408. return;
  409. }
  410. for (f = 0; f < fMax; f++) {
  411. frame = getFrame(f + (255 * a));
  412. serialWrite(frame[64]); // frame duration
  413. while ((character = serialGet()) & 0xFF00); // Wait for answer
  414. if ((character & 0x00FF) != OK) { // Error code recieved
  415. free(frame);
  416. return;
  417. }
  418. for (i = 0; i < 64; i++) {
  419. serialWrite(frame[i]);
  420. }
  421. while ((character = serialGet()) & 0xFF00); // Wait for answer
  422. if ((character & 0x00FF) != OK) { // Error code recieved
  423. free(frame);
  424. return;
  425. }
  426. free(frame);
  427. }
  428. framesToGo -= fMax;
  429. }
  430. serialWrite(OK);
  431. serialWrite(OK);
  432. serialWrite(OK);
  433. serialWrite(OK);
  434. while ((character = serialGet()) & 0xFF00); // Wait for answer
  435. // Error code ignored...
  436. }
  437. uint8_t audioModeSelected(void) {
  438. // Pushbutton: PB0, Low active
  439. if (!(PINB & (1 << PB0))) {
  440. // Button pushed
  441. if (lastButtonState == 0) {
  442. lastButtonState = 1;
  443. } else {
  444. lastButtonState = 0;
  445. }
  446. #ifdef DEBUG
  447. serialWriteString("New State (");
  448. serialWriteString(itoa(lastButtonState, buffer, 10));
  449. serialWriteString(")\n");
  450. #endif
  451. }
  452. return lastButtonState;
  453. }
  454. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
  455. uint8_t i = 0;
  456. for (; i < height; i++) {
  457. setPixelBuffer(x, i, z, buf);
  458. }
  459. }
  460. void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  461. buf[(8 * z) + y] |= (1 << x);
  462. }
  463. void visualizeAudioData(uint8_t *audioData, uint8_t *imageData) {
  464. uint8_t i;
  465. for (i = 0; i < 64; i++) {
  466. imageData[i] = 0;
  467. }
  468. // 8 LEDs, Max Val 255:
  469. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  470. // 255 / FACTOR = 8,...
  471. // 127 / FACTOR = 4,...
  472. #define FACTOR 31
  473. // Could not figure out a way to represent this easily in a loop
  474. // without using a shitload of 'if's...
  475. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  476. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  477. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  478. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  479. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  480. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  481. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  482. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  483. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  484. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  485. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  486. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  487. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  488. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  489. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  490. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  491. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  492. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  493. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  494. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  495. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  496. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  497. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  498. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  499. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  500. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  501. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  502. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  503. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  504. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  505. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  506. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  507. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  508. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  509. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  510. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  511. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  512. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  513. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  514. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  515. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  516. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  517. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  518. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  519. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  520. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  521. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  522. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  523. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  524. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  525. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  526. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  527. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  528. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  529. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  530. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  531. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  532. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  533. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  534. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  535. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  536. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  537. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  538. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  539. }