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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 <avr/wdt.h>
  35. #include "serial.h"
  36. #include "cube.h"
  37. #include "time.h"
  38. #include "audio.h"
  39. #include "mem.h"
  40. #include "memLayer.h"
  41. #include "twi.h"
  42. #include "strings.h"
  43. #include "visualizer.h"
  44. #include "animations.h"
  45. #include "transmit.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. // Length of an idle animation frame, 24 -> 1 second
  56. #define IDLELENGTH 48
  57. void serialHandler(char c);
  58. uint8_t audioModeSelected(void);
  59. void printErrors(uint8_t e);
  60. uint8_t selfTest(void);
  61. void printTime(void);
  62. // #include "snake.c"
  63. uint8_t shouldRestart = 0;
  64. uint8_t refreshAnimationCount = 1;
  65. uint8_t lastButtonState = 0;
  66. uint8_t maxButtonState = 0;
  67. char buffer[11];
  68. #include "builtInFrames.c"
  69. uint8_t DebugDone = 0; // Bit 0: 10s int. count, Bit 1: idle switch
  70. // Bit 2: state changed, disable idle
  71. int main(void) {
  72. uint8_t *audioData = NULL;
  73. uint8_t *imageData = NULL;
  74. uint8_t i, length = 0;
  75. uint16_t count;
  76. uint64_t lastChecked;
  77. uint8_t idleCounter = 0;
  78. uint32_t temp;
  79. MCUCSR = 0;
  80. wdt_disable();
  81. DDRA = 0xFF; // Latch Data Bus as Output
  82. DDRD = 0xFC; DDRB = 24; // Mosfets as Output
  83. DDRC = 0xFC; DDRB |= 6; // Latch Enable as Output
  84. DDRB &= ~(1 << PB0); // Pushbutton as Input
  85. initCube();
  86. serialInit(25, 8, NONE, 1);
  87. i2c_init();
  88. initSystemTimer();
  89. sei(); // Enable Interrupts
  90. setImage(defaultImageCube); // Display something
  91. wdt_enable(WDTO_1S); // Watchdog reset after 1 second
  92. serialWriteString(getString(2)); // "Initialized: "
  93. i = selfTest();
  94. if (i) {
  95. serialWriteString(getString(1)); // Selftest error
  96. serialWriteString(itoa(i, buffer, 2));
  97. serialWrite('\n');
  98. printErrors(i);
  99. }
  100. serialWriteString(getString(0)); // Version
  101. maxButtonState = numberOfVisualizations() + 1; // All visualizations and anim mode
  102. audioModeSelected();
  103. lastChecked = getSystemTime();
  104. i = 0; // Image count
  105. count = getAnimationCount();
  106. while (1) {
  107. // Reset if requested
  108. if (!shouldRestart) {
  109. wdt_reset();
  110. }
  111. if(lastButtonState >= 1) {
  112. // Get Audio Data and visualize it.
  113. // Visualization id is in (lastButtonState - 1)
  114. if (isFinished()) {
  115. audioData = getAudioData(); // Not malloc'ed => Don't free
  116. if (audioData != NULL) {
  117. runVisualization(audioData, lastButtonState - 1);
  118. }
  119. }
  120. } else {
  121. if (refreshAnimationCount) {
  122. // Get animation count stored in FRAM via TWI, if needed
  123. count = getAnimationCount();
  124. refreshAnimationCount = 0;
  125. i = 0;
  126. }
  127. if (count > 0) { // We have frames stored
  128. if (isFinished() > length) {
  129. // Load next image
  130. if (i < (count - 1)) {
  131. i++;
  132. } else {
  133. i = 0;
  134. }
  135. imageData = getFrame(i);
  136. length = imageData[64];
  137. setImage(imageData);
  138. free(imageData);
  139. }
  140. } else { // No frames available
  141. if (!(DebugDone & 4)) { // Idle animation allowed
  142. if (DebugDone & 2) {
  143. if (idleCounter < numOfAnimations()) {
  144. executeAnimation(idleCounter++);
  145. } else {
  146. idleCounter = 0;
  147. DebugDone &= ~(2); // Show frames again
  148. }
  149. } else {
  150. // Show idle frames
  151. if (isFinished() >= IDLELENGTH) {
  152. setImage(idleAnimation[idleCounter]);
  153. if (idleCounter < IDLEANIMATIONCOUNT) {
  154. idleCounter++;
  155. } else {
  156. idleCounter = 0;
  157. DebugDone |= 2; // Show animation
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. if (serialHasChar()) {
  165. serialHandler((char)(serialGet()));
  166. }
  167. // Print fps after one second
  168. if ((getSystemTime() >= 1000) && ((DebugDone & 1) == 0)) {
  169. temp = getTriggerCount();
  170. serialWriteString(ltoa(temp, buffer, 10));
  171. serialWriteString(getString(27));
  172. serialWriteString(ltoa((temp / 8), buffer, 10));
  173. serialWriteString(getString(28));
  174. DebugDone |= 1;
  175. }
  176. if ((getSystemTime() - lastChecked) > 150) { // Check button state every 150ms
  177. audioModeSelected();
  178. lastChecked = getSystemTime();
  179. }
  180. }
  181. close();
  182. return 0;
  183. }
  184. uint8_t audioModeSelected(void) {
  185. // Pushbutton: PB0, Low active
  186. if (!(PINB & (1 << PB0))) {
  187. // Button pushed
  188. if (lastButtonState < (maxButtonState - 1)) {
  189. lastButtonState++;
  190. } else {
  191. lastButtonState = 0;
  192. }
  193. }
  194. return lastButtonState;
  195. }
  196. uint8_t selfTest(void) {
  197. uint8_t result = NOERROR;
  198. uint8_t *data = getAudioData();
  199. if (data == NULL) {
  200. result |= AUDIOERROR;
  201. } else {
  202. free(data);
  203. }
  204. data = memGetBytes(0, 1);
  205. if (data == NULL) {
  206. result |= MEMORYERROR;
  207. } else {
  208. free(data);
  209. }
  210. setGeneralPurposeByte(0, 0x23);
  211. if (getGeneralPurposeByte(0) != 0x23) {
  212. result |= MEMORYWRITEERROR;
  213. }
  214. return result;
  215. }
  216. void printErrors(uint8_t e) {
  217. if (ISERROR(e, AUDIOERROR)) {
  218. serialWriteString(getString(3));
  219. }
  220. if (ISERROR(e, MEMORYERROR)) {
  221. serialWriteString(getString(4));
  222. }
  223. if (ISERROR(e, MEMORYWRITEERROR)) {
  224. serialWriteString(getString(5));
  225. }
  226. }
  227. void randomAnimation(void) {
  228. uint8_t *b = (uint8_t *)malloc(64);
  229. uint8_t x, y, z;
  230. if (b == NULL) {
  231. serialWriteString(getString(24));
  232. return;
  233. }
  234. for (x = 0; x < 64; x++) {
  235. b[x] = 0;
  236. }
  237. while(1) {
  238. setImage(b);
  239. while(isFinished() == 0);
  240. x = rand() / 4096;
  241. y = rand() / 4096;
  242. z = rand() / 4096;
  243. b[x + (8 * y)] ^= (1 << z);
  244. if (serialHasChar()) {
  245. serialWriteString(getString(25));
  246. free(b);
  247. serialHandler(serialGet());
  248. return;
  249. }
  250. }
  251. free(b);
  252. }
  253. void serialHandler(char c) {
  254. // Used letters:
  255. // a, b, c, d, e, g, i, n, q, r, s, t, v, x, y, 0, 1, 2, 3, #
  256. uint8_t i, y, z;
  257. switch(c) {
  258. case OK:
  259. serialWrite(OK);
  260. break;
  261. case 'h': case 'H': case '?':
  262. serialWriteString(getString(6));
  263. serialWriteString(getString(7));
  264. serialWriteString(getString(8));
  265. serialWriteString(getString(9));
  266. serialWriteString(getString(10));
  267. serialWriteString(getString(11));
  268. serialWriteString(getString(12));
  269. serialWriteString(getString(13));
  270. serialWriteString(getString(26));
  271. break;
  272. case 'd': case 'D':
  273. clearMem();
  274. serialWrite(OK);
  275. break;
  276. case 'g': case 'G':
  277. transmitAnimations();
  278. break;
  279. case 's': case 'S':
  280. recieveAnimations();
  281. break;
  282. case 'v': case 'V':
  283. serialWriteString(getString(0));
  284. break;
  285. case 'm': case 'M':
  286. lastButtonState = !lastButtonState;
  287. if (lastButtonState) {
  288. serialWriteString(getString(41));
  289. } else {
  290. serialWriteString(getString(40));
  291. }
  292. break;
  293. case 'q': case 'Q':
  294. shouldRestart = 1;
  295. serialWriteString(getString(30));
  296. break;
  297. case 'r': case 'R':
  298. randomAnimation();
  299. break;
  300. case 't': case 'T':
  301. printTime();
  302. break;
  303. case 'a': case 'A':
  304. sendAudioData();
  305. break;
  306. case 'c': case 'C':
  307. serialWriteString(itoa(getAnimationCount(), buffer, 10));
  308. serialWriteString(getString(15));
  309. break;
  310. case 'x': case 'X':
  311. // Get byte, store as animation count
  312. serialWriteString(getString(16));
  313. while (!serialHasChar());
  314. c = serialGet();
  315. setAnimationCount(c);
  316. serialWriteString(itoa(c, buffer, 10));
  317. serialWriteString(getString(17));
  318. break;
  319. case 'y': case 'Y':
  320. setAnimationCount(0x2201);
  321. serialWriteString(getString(18));
  322. break;
  323. case 'e': case 'E':
  324. c = selfTest();
  325. serialWriteString(getString(19));
  326. serialWriteString(itoa(c, buffer, 2));
  327. serialWrite('\n');
  328. printErrors(c);
  329. break;
  330. case 'n': case 'N':
  331. // snake();
  332. break;
  333. case '0':
  334. fillBuffer(0);
  335. DebugDone |= 4;
  336. break;
  337. case '1':
  338. fillBuffer(0xFF);
  339. DebugDone |= 4;
  340. break;
  341. case '3':
  342. setImage(defaultImageCube);
  343. DebugDone |= 4;
  344. break;
  345. case '2':
  346. DebugDone |= 4;
  347. fillBuffer(0);
  348. for (i = 0; i < 64; i++) {
  349. defaultImageA[i] = 0;
  350. }
  351. while(1) {
  352. for (i = 0; i < 8; i++) {
  353. for (y = 0; y < 8; y++) {
  354. defaultImageA[y + (i * 8)] = 0;
  355. for (z = 0; z < 8; z++) {
  356. defaultImageA[y + (i * 8)] |= (1 << z);
  357. setImage(defaultImageA);
  358. while (isFinished() == 0) {
  359. wdt_reset();
  360. if (serialHasChar()) {
  361. goto killMeForIt; // Yes I know...
  362. // But I need to break out of 2 while Loops...
  363. }
  364. }
  365. }
  366. defaultImageA[y + (i * 8)] = 0;
  367. }
  368. }
  369. }
  370. break;
  371. killMeForIt:
  372. serialGet();
  373. serialWriteString(getString(25));
  374. break;
  375. case 'I': case 'i':
  376. serialWriteString(ltoa(getTriggerCount(), buffer, 10));
  377. serialWrite('\n');
  378. break;
  379. default:
  380. serialWrite(ERROR);
  381. break;
  382. }
  383. // c was used as temp var and does not contain the char anymore...!
  384. }
  385. void printTime(void) {
  386. serialWriteString(getString(14));
  387. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  388. serialWriteString("ms");
  389. if (getSystemTime() > 60000) {
  390. serialWriteString(" (");
  391. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  392. serialWriteString(" min)");
  393. }
  394. if (getSystemTime() > 1000) {
  395. serialWriteString(" (");
  396. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  397. itoa(getSystemTime() % 1000, buffer, 10);
  398. if (buffer[0] != '\0')
  399. serialWrite('.');
  400. if (buffer[2] == '\0')
  401. serialWrite('0');
  402. if (buffer[1] == '\0')
  403. serialWrite('0');
  404. if (buffer[0] != '\0')
  405. serialWriteString(buffer);
  406. serialWriteString("s)\n");
  407. } else {
  408. serialWrite('\n');
  409. }
  410. }