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

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