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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. uint8_t selfTest(void);
  56. void serialHandler(char c);
  57. void sendAudioData(void);
  58. void recieveAnimations(void);
  59. void transmitAnimations(void);
  60. uint8_t audioModeSelected(void);
  61. inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
  62. inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf);
  63. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf);
  64. void visualizeAudioData(uint8_t *audioData, uint8_t *imageData);
  65. #ifdef DEBUG
  66. void printErrors(uint8_t e);
  67. #endif
  68. uint8_t refreshAnimationCount = 1;
  69. uint8_t lastButtonState = 0;
  70. char buffer[11];
  71. uint8_t defaultImage[64] = { 0xe7, 0xc3, 0xa5, 0x18, 0x18, 0xa5, 0xc3, 0xe7,
  72. 0x81, 0x81, 0x00, 0x18, 0x18, 0x00, 0x81, 0x81,
  73. 0x81, 0x00, 0x81, 0x18, 0x18, 0x81, 0x00, 0x81,
  74. 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00,
  76. 0x81, 0x00, 0x81, 0x18, 0x18, 0x81, 0x00, 0x81,
  77. 0x81, 0x81, 0x00, 0x18, 0x18, 0x00, 0x81, 0x81,
  78. 0xe7, 0xc3, 0xa5, 0x18, 0x18, 0xa5, 0xc3, 0xe7 };
  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. visualizeAudioData(audioData, imageData);
  125. setImage(imageData);
  126. free(audioData);
  127. }
  128. }
  129. } else {
  130. if (refreshAnimationCount) {
  131. // Get animation count stored in FRAM via TWI, if needed
  132. count = getAnimationCount();
  133. refreshAnimationCount = 0;
  134. i = 0;
  135. }
  136. if (count > 0) {
  137. if (isFinished() > length) {
  138. // Load next image
  139. if (i < (count - 1)) {
  140. i++;
  141. } else {
  142. i = 0;
  143. }
  144. imageData = getFrame(i);
  145. length = imageData[64];
  146. setImage(imageData);
  147. free(imageData);
  148. }
  149. }
  150. }
  151. if (serialHasChar()) {
  152. serialHandler((char)(serialGet()));
  153. }
  154. if ((getSystemTime() - lastChecked) > 150) {
  155. lastMode = audioModeSelected();
  156. lastChecked = getSystemTime();
  157. }
  158. }
  159. close();
  160. return 0;
  161. }
  162. uint8_t selfTest(void) {
  163. uint8_t result = NOERROR;
  164. uint8_t *data = getAudioData();
  165. if (data == NULL) {
  166. result |= AUDIOERROR;
  167. } else {
  168. free(data);
  169. }
  170. data = memGetBytes(0, 1);
  171. if (data == NULL) {
  172. result |= MEMORYERROR;
  173. } else {
  174. free(data);
  175. }
  176. setGeneralPurposeByte(0, 0x42);
  177. if (getGeneralPurposeByte(0) != 0x42) {
  178. result |= MEMORYWRITEERROR;
  179. }
  180. return result;
  181. }
  182. #ifdef DEBUG
  183. void printErrors(uint8_t e) {
  184. if (ISERROR(e, AUDIOERROR)) {
  185. serialWriteString(" => No answer from Audio!\n");
  186. }
  187. if (ISERROR(e, MEMORYERROR)) {
  188. serialWriteString(" => No answer from Memory!\n");
  189. }
  190. if (ISERROR(e, MEMORYWRITEERROR)) {
  191. serialWriteString(" => Can't write to Memory!\n");
  192. }
  193. }
  194. #endif
  195. void serialHandler(char c) {
  196. // Used letters:
  197. // a, c, d, g, s, t, v, x
  198. #ifdef DEBUG
  199. serialWrite(c);
  200. serialWriteString(": ");
  201. #endif
  202. switch(c) {
  203. case OK:
  204. serialWrite(OK);
  205. break;
  206. case 'h': case 'H':
  207. serialWriteString("(d)elete, (g)et anims, (s)et anims, (v)ersion\n");
  208. #ifdef DEBUG
  209. serialWriteString("(t)ime, (a)udio, (c)ount, (x)Custom count\n");
  210. serialWriteString("(y)Set fixed animation count\n");
  211. serialWriteString("S(e)lf Test\n");
  212. #endif
  213. break;
  214. case 'd': case 'D':
  215. clearMem();
  216. serialWrite(OK);
  217. break;
  218. case 'g': case 'G':
  219. transmitAnimations();
  220. break;
  221. case 's': case 'S':
  222. recieveAnimations();
  223. break;
  224. case 'v': case 'V':
  225. serialWriteString(VERSION);
  226. break;
  227. #ifdef DEBUG
  228. case 't': case 'T':
  229. serialWriteString("System Time: ");
  230. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  231. serialWriteString("ms");
  232. if (getSystemTime() > 1000) {
  233. serialWriteString(" (");
  234. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  235. itoa(getSystemTime() % 1000, buffer, 10);
  236. if (buffer[0] != '\0')
  237. serialWrite('.');
  238. if (buffer[2] == '\0')
  239. serialWrite('0');
  240. if (buffer[1] == '\0')
  241. serialWrite('0');
  242. if (buffer[0] != '\0')
  243. serialWriteString(buffer);
  244. serialWriteString("s)\n");
  245. } else {
  246. serialWrite('\n');
  247. }
  248. break;
  249. case 'a': case 'A':
  250. sendAudioData();
  251. break;
  252. case 'c': case 'C':
  253. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  254. serialWriteString(" Frames stored\n");
  255. break;
  256. case 'x': case 'X':
  257. // Get byte, store as animation count
  258. serialWriteString("Send a byte... ");
  259. while (!serialHasChar());
  260. c = serialGet();
  261. setAnimationCount(c);
  262. serialWriteString(itoa(c, buffer, 10));
  263. serialWriteString(" written!\n");
  264. break;
  265. case 'y': case 'Y':
  266. setAnimationCount(0x2201);
  267. serialWriteString("Animation count now 8705!\n");
  268. break;
  269. case 'e': case 'E':
  270. c = selfTest();
  271. serialWriteString("Self-Test: 0b");
  272. serialWriteString(itoa(c, buffer, 2));
  273. serialWrite('\n');
  274. printErrors(c);
  275. break;
  276. #endif
  277. default:
  278. serialWrite(ERROR);
  279. break;
  280. }
  281. // c was used as temp var and does not contain the char anymore...!
  282. }
  283. #ifdef DEBUG
  284. void sendAudioData(void) {
  285. uint8_t i;
  286. uint8_t *audioData = getAudioData();
  287. if (audioData == NULL) {
  288. serialWriteString("Could not access device!\n");
  289. } else {
  290. serialWriteString("Audio Data:\n");
  291. for (i = 0; i < 7; i++) {
  292. serialWrite(i + '0');
  293. serialWriteString(": ");
  294. itoa(audioData[i], buffer, 10);
  295. serialWriteString(buffer);
  296. serialWrite('\n');
  297. }
  298. free(audioData);
  299. }
  300. }
  301. #endif
  302. void recieveAnimations() {
  303. uint8_t animCount, a, frameCount, f, i;
  304. uint16_t completeCount = 0, character;
  305. uint8_t frame[65];
  306. serialWrite(OK); // We are ready...
  307. character = serialGet();
  308. while (character & 0xFF00) { // Wait for answer
  309. character = serialGet();
  310. }
  311. animCount = (uint8_t)(character & 0x00FF); // Got animation count
  312. serialWrite(OK);
  313. for (a = 0; a < animCount; a++) {
  314. character = serialGet();
  315. while (character & 0xFF00) { // Wait for answer
  316. character = serialGet();
  317. }
  318. frameCount = (uint8_t)(character & 0x00FF); // Got frame count
  319. serialWrite(OK);
  320. for (f = 0; f < frameCount; f++) {
  321. character = serialGet();
  322. while (character & 0xFF00) { // Wait for answer
  323. character = serialGet();
  324. }
  325. frame[64] = (uint8_t)(character & 0x00FF); // Got duration
  326. serialWrite(OK);
  327. for (i = 0; i < 64; i++) {
  328. character = serialGet();
  329. while (character & 0xFF00) { // Wait for answer
  330. character = serialGet();
  331. }
  332. frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
  333. }
  334. serialWrite(OK);
  335. setFrame(completeCount++, frame);
  336. }
  337. }
  338. character = serialGet();
  339. while (character & 0xFF00) { // Wait for answer
  340. character = serialGet();
  341. }
  342. character = serialGet();
  343. while (character & 0xFF00) { // Wait for answer
  344. character = serialGet();
  345. }
  346. character = serialGet();
  347. while (character & 0xFF00) { // Wait for answer
  348. character = serialGet();
  349. }
  350. character = serialGet();
  351. while (character & 0xFF00) { // Wait for answer
  352. character = serialGet();
  353. }
  354. serialWrite(OK);
  355. setAnimationCount(completeCount);
  356. refreshAnimationCount = 1;
  357. }
  358. void transmitAnimations() {
  359. // We store no animation information in here
  360. // So we have to place all frames in one or more
  361. // animations... We need 8 animations max...
  362. uint8_t animationsToGo;
  363. uint16_t framesToGo = getAnimationCount();
  364. uint16_t character;
  365. uint8_t a;
  366. uint8_t f, fMax, i;
  367. uint8_t *frame;
  368. if ((framesToGo % 255) == 0) {
  369. animationsToGo = framesToGo / 255;
  370. } else {
  371. animationsToGo = (framesToGo / 255) + 1;
  372. }
  373. serialWrite(OK);
  374. serialWrite(animationsToGo);
  375. while ((character = serialGet()) & 0xFF00); // Wait for answer
  376. if ((character & 0x00FF) != OK) { // Error code recieved
  377. return;
  378. }
  379. for (a = 0; a < animationsToGo; a++) {
  380. if (framesToGo > 255) {
  381. fMax = 255;
  382. } else {
  383. fMax = framesToGo;
  384. }
  385. serialWrite(fMax); // Number of Frames in current animation
  386. while ((character = serialGet()) & 0xFF00); // Wait for answer
  387. if ((character & 0x00FF) != OK) { // Error code recieved
  388. return;
  389. }
  390. for (f = 0; f < fMax; f++) {
  391. frame = getFrame(f + (255 * a));
  392. serialWrite(frame[64]); // frame duration
  393. while ((character = serialGet()) & 0xFF00); // Wait for answer
  394. if ((character & 0x00FF) != OK) { // Error code recieved
  395. free(frame);
  396. return;
  397. }
  398. for (i = 0; i < 64; i++) {
  399. serialWrite(frame[i]);
  400. }
  401. while ((character = serialGet()) & 0xFF00); // Wait for answer
  402. if ((character & 0x00FF) != OK) { // Error code recieved
  403. free(frame);
  404. return;
  405. }
  406. free(frame);
  407. }
  408. framesToGo -= fMax;
  409. }
  410. serialWrite(OK);
  411. serialWrite(OK);
  412. serialWrite(OK);
  413. serialWrite(OK);
  414. while ((character = serialGet()) & 0xFF00); // Wait for answer
  415. // Error code ignored...
  416. }
  417. uint8_t audioModeSelected(void) {
  418. // Pushbutton: PB0, Low active
  419. if (!(PINB & (1 << PB0))) {
  420. // Button pushed
  421. if (lastButtonState == 0) {
  422. lastButtonState = 1;
  423. } else {
  424. lastButtonState = 0;
  425. }
  426. #ifdef DEBUG
  427. serialWriteString("New State (");
  428. serialWriteString(itoa(lastButtonState, buffer, 10));
  429. serialWriteString(")\n");
  430. #endif
  431. }
  432. return lastButtonState;
  433. }
  434. inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  435. buf[(8 * z) + y] |= (1 << x);
  436. }
  437. inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  438. buf[(8 * z) + y] &= ~(1 << x);
  439. }
  440. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
  441. uint8_t i = 0;
  442. for (; i < height; i++) {
  443. setPixelBuffer(x, i, z, buf);
  444. }
  445. }
  446. void visualizeAudioData(uint8_t *audioData, uint8_t *imageData) {
  447. uint8_t i;
  448. for (i = 0; i < 64; i++) {
  449. imageData[i] = 0;
  450. }
  451. // 8 LEDs, Max Val 255:
  452. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  453. // 255 / FACTOR = 8,...
  454. // 127 / FACTOR = 4,...
  455. #define FACTOR 31
  456. // Could not figure out a way to represent this easily in a loop
  457. // without using a shitload of 'if's...
  458. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  459. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  460. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  461. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  462. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  463. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  464. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  465. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  466. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  467. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  468. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  469. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  470. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  471. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  472. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  473. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  474. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  475. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  476. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  477. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  478. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  479. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  480. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  481. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  482. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  483. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  484. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  485. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  486. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  487. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  488. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  489. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  490. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  491. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  492. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  493. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  494. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  495. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  496. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  497. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  498. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  499. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  500. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  501. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  502. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  503. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  504. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  505. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  506. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  507. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  508. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  509. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  510. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  511. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  512. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  513. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  514. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  515. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  516. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  517. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  518. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  519. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  520. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  521. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  522. }