Simple single-color 8x8x8 LED Cube with AVRs
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * transmit.c
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <avr/io.h>
  22. #include <util/delay.h>
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <avr/wdt.h>
  26. #include <string.h>
  27. #include "memLayer.h"
  28. #include "serial.h"
  29. #include "time.h"
  30. #include "strings.h"
  31. #include "audio.h"
  32. #include "generator.h"
  33. #include "transmit.h"
  34. #define OK 0x42
  35. #define ERROR 0x23
  36. #define TIMEOUTPERFRAME 500
  37. char buffer[LINEBUFFSIZE];
  38. extern uint8_t refreshAnimationCount;
  39. void recieveAnimations(void) {
  40. uint8_t animCount, a, frameCount, f, i, c;
  41. uint16_t completeCount = 0;
  42. uint8_t *frame = (uint8_t *)malloc(65);
  43. uint64_t timestamp = getSystemTime();
  44. uint64_t timeout = TIMEOUTPERFRAME;
  45. serialWrite(OK); // We are ready...
  46. while (!serialHasChar()) { // Wait for answer
  47. if ((getSystemTime() - timestamp) <= timeout) {
  48. wdt_reset();
  49. } else {
  50. setAnimationCount(0);
  51. return;
  52. }
  53. }
  54. c = serialGet();
  55. animCount = c; // Got animation count
  56. serialWrite(OK);
  57. for (a = 0; a < animCount; a++) {
  58. while (!serialHasChar()) { // Wait for answer
  59. if ((getSystemTime() - timestamp) <= timeout) {
  60. wdt_reset();
  61. } else {
  62. setAnimationCount(0);
  63. return;
  64. }
  65. }
  66. c = serialGet();
  67. frameCount = c; // Got frame count
  68. serialWrite(OK);
  69. for (f = 0; f < frameCount; f++) {
  70. timeout += TIMEOUTPERFRAME;
  71. while (!serialHasChar()) { // Wait for answer
  72. if ((getSystemTime() - timestamp) <= timeout) {
  73. wdt_reset();
  74. } else {
  75. setAnimationCount(0);
  76. return;
  77. }
  78. }
  79. c = serialGet();
  80. frame[64] = c; // Got duration
  81. serialWrite(OK);
  82. for (i = 0; i < 64; i++) {
  83. while (!serialHasChar()) { // Wait for answer
  84. if ((getSystemTime() - timestamp) <= timeout) {
  85. wdt_reset();
  86. } else {
  87. setAnimationCount(0);
  88. return;
  89. }
  90. }
  91. c = serialGet();
  92. frame[i] = c; // Got data byte
  93. }
  94. serialWrite(OK);
  95. setFrame(completeCount++, frame);
  96. }
  97. }
  98. free(frame);
  99. while (!serialHasChar()) { // Wait for answer
  100. if ((getSystemTime() - timestamp) <= timeout) {
  101. wdt_reset();
  102. } else {
  103. setAnimationCount(0);
  104. return;
  105. }
  106. }
  107. c = serialGet();
  108. while (!serialHasChar()) { // Wait for answer
  109. if ((getSystemTime() - timestamp) <= timeout) {
  110. wdt_reset();
  111. } else {
  112. setAnimationCount(0);
  113. return;
  114. }
  115. }
  116. c = serialGet();
  117. while (!serialHasChar()) { // Wait for answer
  118. if ((getSystemTime() - timestamp) <= timeout) {
  119. wdt_reset();
  120. } else {
  121. setAnimationCount(0);
  122. return;
  123. }
  124. }
  125. c = serialGet();
  126. while (!serialHasChar()) { // Wait for answer
  127. if ((getSystemTime() - timestamp) <= timeout) {
  128. wdt_reset();
  129. } else {
  130. setAnimationCount(0);
  131. return;
  132. }
  133. }
  134. c = serialGet();
  135. serialWrite(OK);
  136. setAnimationCount(completeCount);
  137. }
  138. void transmitAnimations(void) {
  139. serialWrite(ERROR);
  140. }
  141. void sendAudioData(void) {
  142. uint8_t i;
  143. uint8_t *audioData = getAudioData();
  144. if (audioData == NULL) {
  145. serialWriteString(getString(21));
  146. } else {
  147. serialWriteString(getString(22));
  148. for (i = 0; i < 7; i++) {
  149. serialWrite(i + '0');
  150. serialWriteString(": ");
  151. itoa(audioData[i], buffer, 10);
  152. serialWriteString(buffer);
  153. serialWrite('\n');
  154. }
  155. }
  156. }
  157. void printTime(void) {
  158. serialWriteString(getString(14));
  159. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  160. serialWriteString("ms");
  161. if (getSystemTime() > 60000) {
  162. serialWriteString(" (");
  163. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  164. serialWriteString(" min)");
  165. }
  166. if (getSystemTime() > 1000) {
  167. serialWriteString(" (");
  168. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  169. itoa(getSystemTime() % 1000, buffer, 10);
  170. if (buffer[0] != '\0')
  171. serialWrite('.');
  172. if (buffer[2] == '\0')
  173. serialWrite('0');
  174. if (buffer[1] == '\0')
  175. serialWrite('0');
  176. if (buffer[0] != '\0')
  177. serialWriteString(buffer);
  178. serialWriteString("s)\n");
  179. } else {
  180. serialWrite('\n');
  181. }
  182. }
  183. void dumpFrame(uint8_t *f) {
  184. uint8_t zeile, spalte;
  185. for (zeile = 0; zeile < 8; zeile++) {
  186. serialWrite(zeile + '0');
  187. serialWriteString(": ");
  188. for (spalte = 0; spalte < 8; spalte++) {
  189. itoa(f[(8 * zeile) + spalte], buffer, 16);
  190. serialWriteString(buffer);
  191. serialWrite(' ');
  192. }
  193. serialWrite('\n');
  194. }
  195. serialWriteString(getString(35));
  196. itoa(f[64], buffer, 10);
  197. serialWriteString(buffer);
  198. serialWrite('\n');
  199. }
  200. uint8_t *readLine(void) {
  201. uint8_t ptr = 0;
  202. while(1) {
  203. wdt_reset();
  204. if (serialHasChar()) {
  205. buffer[ptr] = serialGet();
  206. serialWrite(buffer[ptr]);
  207. if ((buffer[ptr] == '\n') || (ptr == sizeof(buffer) - 1)) {
  208. buffer[ptr] = '\0';
  209. return (uint8_t *)buffer;
  210. }
  211. if (buffer[ptr] != '\r') {
  212. // ignore carriage return (\r)
  213. if (buffer[ptr] == 8) {
  214. // handle backspace
  215. ptr--;
  216. } else {
  217. ptr++;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. uint16_t readNumber(uint8_t base) {
  224. uint8_t *s = readLine();
  225. uint16_t val = (uint16_t)strtoul((char *)s, NULL, base);
  226. return val;
  227. }
  228. void writeNumber(uint8_t num, uint8_t base) {
  229. itoa(num, buffer, base);
  230. serialWriteString(buffer);
  231. }
  232. uint8_t *readAFrame(void) {
  233. uint8_t *frame = (uint8_t *)malloc(65);
  234. uint8_t byte;
  235. serialWriteString(getString(35)); // "Duration: "
  236. frame[64] = readNumber(10);
  237. for (byte = 0; byte < 64; byte++) {
  238. writeNumber(byte, 10);
  239. serialWriteString(": "); // "xx: "
  240. frame[byte] = readNumber(16);
  241. }
  242. serialWriteString(getString(33));
  243. return frame;
  244. }
  245. void simpleAnimationInput(void) {
  246. uint8_t start, i;
  247. uint8_t data[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  248. serialWriteString(getString(36));
  249. start = readNumber(10);
  250. serialWriteString(getString(37));
  251. for (i = 0; i < 8; i++) {
  252. writeNumber(i, 10);
  253. serialWriteString(": ");
  254. data[i] = readNumber(16);
  255. }
  256. serialWriteString(getString(38));
  257. generateMovingAnimation(data, start, 2);
  258. serialWriteString(getString(33)); // Done
  259. }
  260. void textRenderInput(void) {
  261. readLine();
  262. renderText(buffer, 0);
  263. setAnimationCount((strlen(buffer) * 8) + 1); // Theres an empty frame at the end.
  264. refreshAnimationCount = 1;
  265. serialWriteString(getString(33)); // Done
  266. }