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

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