Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

main.c 15KB

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