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

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