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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. #define VERSION "8^3 LED-Cube v1\n"
  29. #include <avr/io.h>
  30. #include <util/delay.h>
  31. #include <avr/interrupt.h>
  32. #include <avr/pgmspace.h>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #include "serial.h"
  36. #include "cube.h"
  37. #include "time.h"
  38. #include "audio.h"
  39. #include "memLayer.h"
  40. #include "twi.h"
  41. void serialHandler(char c);
  42. void sendAudioData(void);
  43. void recieveAnimations(void);
  44. void transmitAnimations(void);
  45. uint8_t audioModeSelected(void);
  46. inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
  47. inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf);
  48. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf);
  49. void visualizeAudioData(uint8_t *audioData, uint8_t **imageData);
  50. uint8_t refreshAnimationCount = 1;
  51. uint8_t lastButtonState = 0;
  52. char buffer[11];
  53. int main(void) {
  54. uint8_t *audioData;
  55. uint8_t **imageData;
  56. uint8_t i, lastMode;
  57. uint16_t count;
  58. uint64_t lastChecked;
  59. initCube();
  60. serialInit(51, 8, NONE, 1);
  61. i2c_init();
  62. initSystemTimer();
  63. sei(); // Enable Interrupts
  64. DDRD = 0xFC; // Mosfets as Output
  65. DDRB = 0xFE;
  66. DDRC = 0xFF; // Latch Enable
  67. DDRA = 0xFF; // Latch Data
  68. imageData = (uint8_t **)malloc(8 * sizeof(uint8_t *));
  69. for (i = 0; i < 8; i++) {
  70. imageData[i] = (uint8_t *)malloc(8 * sizeof(uint8_t));
  71. }
  72. #ifdef DEBUG
  73. refreshAnimationCount = 0; // Don't talk to FRAM yet...
  74. serialWriteString("Initialized: ");
  75. serialWriteString(VERSION);
  76. serialWriteString("Took ");
  77. serialWriteString(itoa(getSystemTime(), buffer, 10));
  78. serialWriteString(" ms!\n");
  79. #endif
  80. lastMode = audioModeSelected();
  81. lastChecked = getSystemTime();
  82. while (1) {
  83. //if(lastMode) {
  84. // Get Audio Data and visualize it
  85. /* audioData = getAudioData();
  86. if (audioData != NULL) {
  87. visualizeAudioData(audioData, imageData);
  88. setImage(imageData);
  89. free(audioData);
  90. }
  91. while(!isFinished()); // Wait for it to display */
  92. //} else {
  93. // Look for commands, play from fram
  94. // We have 128*1024 bytes
  95. // A Frame needs 65 bytes (64 data + duration)
  96. // We place 2016 Frames in mem => 131040
  97. // That gives us 32 bytes at the beginning, 0 -> 31
  98. // The first frame starts at 32
  99. if (serialHasChar()) {
  100. serialHandler((char)(serialGet()));
  101. }
  102. /* if (refreshAnimationCount) {
  103. // Get animation count stored in FRAM via TWI, if needed
  104. count = getAnimationCount();
  105. refreshAnimationCount = 0;
  106. } */
  107. //}
  108. if ((getSystemTime() - lastChecked) > 100) {
  109. lastMode = audioModeSelected();
  110. lastChecked = getSystemTime();
  111. }
  112. }
  113. close();
  114. return 0;
  115. }
  116. void serialHandler(char c) {
  117. // Used letters:
  118. // a, c, d, g, s, t, v, x
  119. switch(c) {
  120. case OK:
  121. serialWrite(OK);
  122. break;
  123. case 'd': case 'D':
  124. clearMem();
  125. serialWrite(OK);
  126. break;
  127. case 'g': case 'G':
  128. transmitAnimations();
  129. break;
  130. case 's': case 'S':
  131. recieveAnimations();
  132. break;
  133. case 'v': case 'V':
  134. serialWriteString(VERSION);
  135. break;
  136. case 't': case 'T':
  137. serialWriteString("System Time: ");
  138. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  139. serialWriteString("ms");
  140. if (getSystemTime() > 1000) {
  141. serialWriteString(" (");
  142. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  143. itoa(getSystemTime() % 1000, buffer, 10);
  144. if (buffer[0] != '\0')
  145. serialWrite('.');
  146. if (buffer[2] == '\0')
  147. serialWrite('0');
  148. if (buffer[1] == '\0')
  149. serialWrite('0');
  150. if (buffer[0] != '\0')
  151. serialWriteString(buffer);
  152. serialWriteString("s)\n");
  153. } else {
  154. serialWrite('\n');
  155. }
  156. break;
  157. case 'a': case 'A':
  158. sendAudioData();
  159. break;
  160. case 'c': case 'C':
  161. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  162. serialWriteString(" Frames stored\n");
  163. break;
  164. case 'x': case 'X':
  165. // Get byte, store as animation count
  166. serialWriteString("Send a byte...");
  167. while (!serialHasChar());
  168. setAnimationCount(serialGet());
  169. serialWriteString(" Byte written!\n");
  170. break;
  171. case '\n':
  172. serialWriteString(VERSION);
  173. serialWriteString("See xythobuz.org for more Infos!\n");
  174. break;
  175. default:
  176. serialWrite(ERROR);
  177. break;
  178. }
  179. }
  180. void sendAudioData(void) {
  181. uint8_t i;
  182. uint8_t *audioData = getAudioData();
  183. if (audioData == NULL) {
  184. serialWriteString("Could not access device!\n");
  185. return;
  186. }
  187. serialWriteString("Audio Data:\n");
  188. for (i = 0; i < 7; i++) {
  189. serialWrite(i + '0');
  190. serialWriteString(": ");
  191. itoa(audioData[i], buffer, 10);
  192. serialWriteString(buffer);
  193. serialWrite('\n');
  194. }
  195. free(audioData);
  196. }
  197. void recieveAnimations() {
  198. uint8_t animCount, a, frameCount, f, i;
  199. uint16_t completeCount = 0, character;
  200. uint8_t frame[65];
  201. serialWrite(OK); // We are ready...
  202. character = serialGet();
  203. while (character & 0xFF00) { // Wait for answer
  204. character = serialGet();
  205. }
  206. animCount = (uint8_t)(character & 0x00FF); // Got animation count
  207. serialWrite(OK);
  208. for (a = 0; a < animCount; a++) {
  209. character = serialGet();
  210. while (character & 0xFF00) { // Wait for answer
  211. character = serialGet();
  212. }
  213. frameCount = (uint8_t)(character & 0x00FF); // Got frame count
  214. serialWrite(OK);
  215. for (f = 0; f < frameCount; f++) {
  216. character = serialGet();
  217. while (character & 0xFF00) { // Wait for answer
  218. character = serialGet();
  219. }
  220. frame[64] = (uint8_t)(character & 0x00FF); // Got duration
  221. serialWrite(OK);
  222. for (i = 0; i < 64; i++) {
  223. character = serialGet();
  224. while (character & 0xFF00) { // Wait for answer
  225. character = serialGet();
  226. }
  227. frame[i] = (uint8_t)(character & 0x00FF); // Got data byte
  228. }
  229. serialWrite(OK);
  230. setFrame(completeCount++, frame);
  231. }
  232. }
  233. character = serialGet();
  234. while (character & 0xFF00) { // Wait for answer
  235. character = serialGet();
  236. }
  237. character = serialGet();
  238. while (character & 0xFF00) { // Wait for answer
  239. character = serialGet();
  240. }
  241. character = serialGet();
  242. while (character & 0xFF00) { // Wait for answer
  243. character = serialGet();
  244. }
  245. character = serialGet();
  246. while (character & 0xFF00) { // Wait for answer
  247. character = serialGet();
  248. }
  249. serialWrite(OK);
  250. setAnimationCount(completeCount);
  251. refreshAnimationCount = 1;
  252. }
  253. void transmitAnimations() {
  254. // We store no animation information in here
  255. // So we have to place all frames in one or more
  256. // animations... We need 8 animations max...
  257. uint8_t animationsToGo;
  258. uint16_t framesToGo = getAnimationCount();
  259. uint16_t character;
  260. uint8_t a;
  261. uint8_t f, fMax, i;
  262. uint8_t *frame;
  263. if ((framesToGo % 255) == 0) {
  264. animationsToGo = framesToGo / 255;
  265. } else {
  266. animationsToGo = (framesToGo / 255) + 1;
  267. }
  268. serialWrite(OK);
  269. serialWrite(animationsToGo);
  270. while ((character = serialGet()) & 0xFF00); // Wait for answer
  271. if ((character & 0x00FF) != OK) { // Error code recieved
  272. return;
  273. }
  274. for (a = 0; a < animationsToGo; a++) {
  275. if (framesToGo > 255) {
  276. fMax = 255;
  277. } else {
  278. fMax = framesToGo;
  279. }
  280. serialWrite(fMax); // Number of Frames in current animation
  281. while ((character = serialGet()) & 0xFF00); // Wait for answer
  282. if ((character & 0x00FF) != OK) { // Error code recieved
  283. return;
  284. }
  285. for (f = 0; f < fMax; f++) {
  286. frame = getFrame(f + (255 * a));
  287. serialWrite(frame[64]); // frame duration
  288. while ((character = serialGet()) & 0xFF00); // Wait for answer
  289. if ((character & 0x00FF) != OK) { // Error code recieved
  290. free(frame);
  291. return;
  292. }
  293. for (i = 0; i < 64; i++) {
  294. serialWrite(frame[i]);
  295. }
  296. while ((character = serialGet()) & 0xFF00); // Wait for answer
  297. if ((character & 0x00FF) != OK) { // Error code recieved
  298. free(frame);
  299. return;
  300. }
  301. free(frame);
  302. }
  303. framesToGo -= fMax;
  304. }
  305. serialWrite(OK);
  306. serialWrite(OK);
  307. serialWrite(OK);
  308. serialWrite(OK);
  309. while ((character = serialGet()) & 0xFF00); // Wait for answer
  310. // Error code ignored...
  311. }
  312. uint8_t audioModeSelected(void) {
  313. // Pushbutton: PB0, Low active
  314. if (!(PINB & (1 << PB0))) {
  315. // Button pushed
  316. if (lastButtonState == 0) {
  317. lastButtonState = 1;
  318. } else {
  319. lastButtonState = 0;
  320. }
  321. #ifdef DEBUG
  322. serialWriteString("New State (");
  323. serialWriteString(itoa(lastButtonState, buffer, 10));
  324. serialWriteString(")\n");
  325. #endif
  326. }
  327. return lastButtonState;
  328. }
  329. inline void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
  330. buf[z][y] |= (1 << x);
  331. }
  332. inline void clearPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t **buf) {
  333. buf[z][y] &= ~(1 << x);
  334. }
  335. void setBuffer(uint8_t d, uint8_t *buf, uint8_t length) {
  336. uint8_t i;
  337. for (i = 0; i < length; i++) {
  338. buf[i] = d;
  339. }
  340. }
  341. void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t **buf) {
  342. uint8_t i = 0;
  343. for (; i < height; i++) {
  344. setPixelBuffer(x, i, z, buf);
  345. }
  346. }
  347. void visualizeAudioData(uint8_t *audioData, uint8_t **imageData) {
  348. uint8_t i;
  349. for (i = 0; i < 8; i++) {
  350. setBuffer(0, imageData[i], 8);
  351. }
  352. // 8 LEDs, Max Val 255:
  353. // 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
  354. // 255 / FACTOR = 8,...
  355. // 127 / FACTOR = 4,...
  356. #define FACTOR 31
  357. // Could not figure out a way to represent this easily in a loop
  358. // without using a shitload of 'if's...
  359. setRow(0, 0, (audioData[0] / FACTOR), imageData);
  360. setRow(0, 1, (audioData[0] / FACTOR), imageData);
  361. setRow(1, 0, (audioData[0] / FACTOR), imageData);
  362. setRow(0, 2, (audioData[1] / FACTOR), imageData);
  363. setRow(0, 3, (audioData[1] / FACTOR), imageData);
  364. setRow(1, 1, (audioData[1] / FACTOR), imageData);
  365. setRow(1, 2, (audioData[1] / FACTOR), imageData);
  366. setRow(2, 0, (audioData[1] / FACTOR), imageData);
  367. setRow(2, 1, (audioData[1] / FACTOR), imageData);
  368. setRow(0, 4, (audioData[2] / FACTOR), imageData);
  369. setRow(0, 5, (audioData[2] / FACTOR), imageData);
  370. setRow(1, 3, (audioData[2] / FACTOR), imageData);
  371. setRow(1, 4, (audioData[2] / FACTOR), imageData);
  372. setRow(2, 2, (audioData[2] / FACTOR), imageData);
  373. setRow(2, 3, (audioData[2] / FACTOR), imageData);
  374. setRow(3, 0, (audioData[2] / FACTOR), imageData);
  375. setRow(3, 1, (audioData[2] / FACTOR), imageData);
  376. setRow(3, 2, (audioData[2] / FACTOR), imageData);
  377. setRow(4, 0, (audioData[2] / FACTOR), imageData);
  378. setRow(4, 1, (audioData[2] / FACTOR), imageData);
  379. setRow(0, 6, (audioData[3] / FACTOR), imageData);
  380. setRow(0, 7, (audioData[3] / FACTOR), imageData);
  381. setRow(1, 5, (audioData[3] / FACTOR), imageData);
  382. setRow(1, 6, (audioData[3] / FACTOR), imageData);
  383. setRow(2, 4, (audioData[3] / FACTOR), imageData);
  384. setRow(2, 5, (audioData[3] / FACTOR), imageData);
  385. setRow(3, 3, (audioData[3] / FACTOR), imageData);
  386. setRow(3, 4, (audioData[3] / FACTOR), imageData);
  387. setRow(4, 2, (audioData[3] / FACTOR), imageData);
  388. setRow(4, 3, (audioData[3] / FACTOR), imageData);
  389. setRow(5, 0, (audioData[3] / FACTOR), imageData);
  390. setRow(5, 1, (audioData[3] / FACTOR), imageData);
  391. setRow(5, 2, (audioData[3] / FACTOR), imageData);
  392. setRow(6, 0, (audioData[3] / FACTOR), imageData);
  393. setRow(6, 1, (audioData[3] / FACTOR), imageData);
  394. setRow(1, 7, (audioData[4] / FACTOR), imageData);
  395. setRow(2, 6, (audioData[4] / FACTOR), imageData);
  396. setRow(2, 7, (audioData[4] / FACTOR), imageData);
  397. setRow(3, 5, (audioData[4] / FACTOR), imageData);
  398. setRow(3, 6, (audioData[4] / FACTOR), imageData);
  399. setRow(4, 4, (audioData[4] / FACTOR), imageData);
  400. setRow(4, 5, (audioData[4] / FACTOR), imageData);
  401. setRow(5, 3, (audioData[4] / FACTOR), imageData);
  402. setRow(5, 4, (audioData[4] / FACTOR), imageData);
  403. setRow(6, 2, (audioData[4] / FACTOR), imageData);
  404. setRow(6, 3, (audioData[4] / FACTOR), imageData);
  405. setRow(7, 0, (audioData[4] / FACTOR), imageData);
  406. setRow(7, 1, (audioData[4] / FACTOR), imageData);
  407. setRow(3, 7, (audioData[5] / FACTOR), imageData);
  408. setRow(4, 6, (audioData[5] / FACTOR), imageData);
  409. setRow(4, 7, (audioData[5] / FACTOR), imageData);
  410. setRow(5, 5, (audioData[5] / FACTOR), imageData);
  411. setRow(5, 6, (audioData[5] / FACTOR), imageData);
  412. setRow(6, 4, (audioData[5] / FACTOR), imageData);
  413. setRow(6, 5, (audioData[5] / FACTOR), imageData);
  414. setRow(7, 2, (audioData[5] / FACTOR), imageData);
  415. setRow(7, 3, (audioData[5] / FACTOR), imageData);
  416. setRow(7, 4, (audioData[5] / FACTOR), imageData);
  417. setRow(5, 7, (audioData[6] / FACTOR), imageData);
  418. setRow(6, 6, (audioData[6] / FACTOR), imageData);
  419. setRow(6, 7, (audioData[6] / FACTOR), imageData);
  420. setRow(7, 5, (audioData[6] / FACTOR), imageData);
  421. setRow(7, 6, (audioData[6] / FACTOR), imageData);
  422. setRow(7, 7, (audioData[6] / FACTOR), imageData);
  423. }