Simple single-color 8x8x8 LED Cube with AVRs
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

main.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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] = { 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. 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':
  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. #endif
  216. break;
  217. case 'd': case 'D':
  218. clearMem();
  219. serialWrite(OK);
  220. break;
  221. case 'g': case 'G':
  222. transmitAnimations();
  223. break;
  224. case 's': case 'S':
  225. recieveAnimations();
  226. break;
  227. case 'v': case 'V':
  228. serialWriteString(VERSION);
  229. break;
  230. #ifdef DEBUG
  231. case 't': case 'T':
  232. serialWriteString("System Time: ");
  233. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  234. serialWriteString("ms");
  235. if (getSystemTime() > 1000) {
  236. serialWriteString(" (");
  237. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  238. itoa(getSystemTime() % 1000, buffer, 10);
  239. if (buffer[0] != '\0')
  240. serialWrite('.');
  241. if (buffer[2] == '\0')
  242. serialWrite('0');
  243. if (buffer[1] == '\0')
  244. serialWrite('0');
  245. if (buffer[0] != '\0')
  246. serialWriteString(buffer);
  247. serialWriteString("s)\n");
  248. } else {
  249. serialWrite('\n');
  250. }
  251. break;
  252. case 'a': case 'A':
  253. sendAudioData();
  254. break;
  255. case 'c': case 'C':
  256. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  257. serialWriteString(" Frames stored\n");
  258. break;
  259. case 'x': case 'X':
  260. // Get byte, store as animation count
  261. serialWriteString("Send a byte... ");
  262. while (!serialHasChar());
  263. c = serialGet();
  264. setAnimationCount(c);
  265. serialWriteString(itoa(c, buffer, 10));
  266. serialWriteString(" written!\n");
  267. break;
  268. case 'y': case 'Y':
  269. setAnimationCount(0x2201);
  270. serialWriteString("Animation count now 8705!\n");
  271. break;
  272. case 'e': case 'E':
  273. c = selfTest();
  274. serialWriteString("Self-Test: 0b");
  275. serialWriteString(itoa(c, buffer, 2));
  276. serialWrite('\n');
  277. printErrors(c);
  278. break;
  279. case 'n': case 'N':
  280. snake();
  281. break;
  282. #endif
  283. default:
  284. serialWrite(ERROR);
  285. break;
  286. }
  287. // c was used as temp var and does not contain the char anymore...!
  288. }
  289. #ifdef DEBUG
  290. void sendAudioData(void) {
  291. uint8_t i;
  292. uint8_t *audioData = getAudioData();
  293. if (audioData == NULL) {
  294. serialWriteString("Could not access device!\n");
  295. } else {
  296. serialWriteString("Audio Data:\n");
  297. for (i = 0; i < 7; i++) {
  298. serialWrite(i + '0');
  299. serialWriteString(": ");
  300. itoa(audioData[i], buffer, 10);
  301. serialWriteString(buffer);
  302. serialWrite('\n');
  303. }
  304. free(audioData);
  305. }
  306. }
  307. #endif
  308. void recieveAnimations() {
  309. uint8_t animCount, a, frameCount, f, i;
  310. uint16_t completeCount = 0, character;
  311. uint8_t frame[65];
  312. serialWrite(OK); // We are ready...
  313. character = serialGet();
  314. while (character & 0xFF00) { // Wait for answer
  315. character = serialGet();
  316. }
  317. animCount = (uint8_t)(character & 0x00FF); // Got animation count
  318. serialWrite(OK);
  319. for (a = 0; a < animCount; a++) {
  320. character = serialGet();
  321. while (character & 0xFF00) { // Wait for answer
  322. character = serialGet();
  323. }
  324. frameCount = (uint8_t)(character & 0x00FF); // Got frame count
  325. serialWrite(OK);
  326. for (f = 0; f < frameCount; f++) {
  327. character = serialGet();
  328. while (character & 0xFF00) { // Wait for answer
  329. character = serialGet();
  330. }
  331. frame[64] = (uint8_t)(character & 0x00FF); // Got duration
  332. serialWrite(OK);
  333. for (i = 0; i < 64; i++) {
  334. character = serialGet();
  335. while (character & 0xFF00) { // Wait for answer
  336. character = serialGet();
  337. }
  338. frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
  339. }
  340. serialWrite(OK);
  341. setFrame(completeCount++, frame);
  342. }
  343. }
  344. character = serialGet();
  345. while (character & 0xFF00) { // Wait for answer
  346. character = serialGet();
  347. }
  348. character = serialGet();
  349. while (character & 0xFF00) { // Wait for answer
  350. character = serialGet();
  351. }
  352. character = serialGet();
  353. while (character & 0xFF00) { // Wait for answer
  354. character = serialGet();
  355. }
  356. character = serialGet();
  357. while (character & 0xFF00) { // Wait for answer
  358. character = serialGet();
  359. }
  360. serialWrite(OK);
  361. setAnimationCount(completeCount);
  362. refreshAnimationCount = 1;
  363. }
  364. void transmitAnimations() {
  365. // We store no animation information in here
  366. // So we have to place all frames in one or more
  367. // animations... We need 8 animations max...
  368. uint8_t animationsToGo;
  369. uint16_t framesToGo = getAnimationCount();
  370. uint16_t character;
  371. uint8_t a;
  372. uint8_t f, fMax, i;
  373. uint8_t *frame;
  374. if ((framesToGo % 255) == 0) {
  375. animationsToGo = framesToGo / 255;
  376. } else {
  377. animationsToGo = (framesToGo / 255) + 1;
  378. }
  379. serialWrite(OK);
  380. serialWrite(animationsToGo);
  381. while ((character = serialGet()) & 0xFF00); // Wait for answer
  382. if ((character & 0x00FF) != OK) { // Error code recieved
  383. return;
  384. }
  385. for (a = 0; a < animationsToGo; a++) {
  386. if (framesToGo > 255) {
  387. fMax = 255;
  388. } else {
  389. fMax = framesToGo;
  390. }
  391. serialWrite(fMax); // Number of Frames in current animation
  392. while ((character = serialGet()) & 0xFF00); // Wait for answer
  393. if ((character & 0x00FF) != OK) { // Error code recieved
  394. return;
  395. }
  396. for (f = 0; f < fMax; f++) {
  397. frame = getFrame(f + (255 * a));
  398. serialWrite(frame[64]); // frame duration
  399. while ((character = serialGet()) & 0xFF00); // Wait for answer
  400. if ((character & 0x00FF) != OK) { // Error code recieved
  401. free(frame);
  402. return;
  403. }
  404. for (i = 0; i < 64; i++) {
  405. serialWrite(frame[i]);
  406. }
  407. while ((character = serialGet()) & 0xFF00); // Wait for answer
  408. if ((character & 0x00FF) != OK) { // Error code recieved
  409. free(frame);
  410. return;
  411. }
  412. free(frame);
  413. }
  414. framesToGo -= fMax;
  415. }
  416. serialWrite(OK);
  417. serialWrite(OK);
  418. serialWrite(OK);
  419. serialWrite(OK);
  420. while ((character = serialGet()) & 0xFF00); // Wait for answer
  421. // Error code ignored...
  422. }
  423. uint8_t audioModeSelected(void) {
  424. // Pushbutton: PB0, Low active
  425. if (!(PINB & (1 << PB0))) {
  426. // Button pushed
  427. if (lastButtonState == 0) {
  428. lastButtonState = 1;
  429. } else {
  430. lastButtonState = 0;
  431. }
  432. #ifdef DEBUG
  433. serialWriteString("New State (");
  434. serialWriteString(itoa(lastButtonState, buffer, 10));
  435. serialWriteString(")\n");
  436. #endif
  437. }
  438. return lastButtonState;
  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 setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
  447. buf[(8 * z) + y] |= (1 << x);
  448. }
  449. void visualizeAudioData(uint8_t *audioData, uint8_t *imageData) {
  450. uint8_t i;
  451. for (i = 0; i < 64; i++) {
  452. imageData[i] = 0;
  453. }
  454. // 8 LEDs, Max Val 255:
  455. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  456. // 255 / FACTOR = 8,...
  457. // 127 / FACTOR = 4,...
  458. #define FACTOR 31
  459. // Could not figure out a way to represent this easily in a loop
  460. // without using a shitload of 'if's...
  461. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  462. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  463. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  464. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  465. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  466. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  467. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  468. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  469. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  470. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  471. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  472. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  473. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  474. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  475. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  476. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  477. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  478. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  479. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  480. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  481. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  482. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  483. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  484. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  485. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  486. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  487. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  488. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  489. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  490. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  491. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  492. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  493. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  494. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  495. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  496. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  497. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  498. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  499. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  500. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  501. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  502. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  503. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  504. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  505. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  506. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  507. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  508. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  509. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  510. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  511. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  512. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  513. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  514. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  515. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  516. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  517. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  518. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  519. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  520. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  521. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  522. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  523. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  524. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  525. }