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

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