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

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