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.

transmit.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * transmit.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. #include <avr/io.h>
  24. #include <util/delay.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <avr/wdt.h>
  28. #include "memLayer.h"
  29. #include "serial.h"
  30. #include "time.h"
  31. #include "strings.h"
  32. #include "audio.h"
  33. #define OK 0x42
  34. #define ERROR 0x23
  35. #define TIMEOUTPERFRAME 500
  36. // These are global variables from main.c
  37. extern char buffer[11];
  38. void recieveAnimations(void) {
  39. uint8_t animCount, a, frameCount, f, i, c;
  40. uint16_t completeCount = 0;
  41. uint8_t *frame = (uint8_t *)malloc(65);
  42. uint64_t timestamp = getSystemTime();
  43. uint64_t timeout = TIMEOUTPERFRAME;
  44. serialWrite(OK); // We are ready...
  45. while (!serialHasChar()) { // Wait for answer
  46. if ((getSystemTime() - timestamp) <= timeout) {
  47. wdt_reset();
  48. } else {
  49. setAnimationCount(0);
  50. return;
  51. }
  52. }
  53. c = serialGet();
  54. animCount = c; // Got animation count
  55. serialWrite(OK);
  56. for (a = 0; a < animCount; a++) {
  57. while (!serialHasChar()) { // Wait for answer
  58. if ((getSystemTime() - timestamp) <= timeout) {
  59. wdt_reset();
  60. } else {
  61. setAnimationCount(0);
  62. return;
  63. }
  64. }
  65. c = serialGet();
  66. frameCount = c; // Got frame count
  67. serialWrite(OK);
  68. for (f = 0; f < frameCount; f++) {
  69. timeout += TIMEOUTPERFRAME;
  70. while (!serialHasChar()) { // Wait for answer
  71. if ((getSystemTime() - timestamp) <= timeout) {
  72. wdt_reset();
  73. } else {
  74. setAnimationCount(0);
  75. return;
  76. }
  77. }
  78. c = serialGet();
  79. frame[64] = c; // Got duration
  80. serialWrite(OK);
  81. for (i = 0; i < 64; i++) {
  82. while (!serialHasChar()) { // Wait for answer
  83. if ((getSystemTime() - timestamp) <= timeout) {
  84. wdt_reset();
  85. } else {
  86. setAnimationCount(0);
  87. return;
  88. }
  89. }
  90. c = serialGet();
  91. frame[i] = c; // Got data byte
  92. }
  93. serialWrite(OK);
  94. setFrame(completeCount++, frame);
  95. }
  96. }
  97. free(frame);
  98. while (!serialHasChar()) { // Wait for answer
  99. if ((getSystemTime() - timestamp) <= timeout) {
  100. wdt_reset();
  101. } else {
  102. setAnimationCount(0);
  103. return;
  104. }
  105. }
  106. c = serialGet();
  107. while (!serialHasChar()) { // Wait for answer
  108. if ((getSystemTime() - timestamp) <= timeout) {
  109. wdt_reset();
  110. } else {
  111. setAnimationCount(0);
  112. return;
  113. }
  114. }
  115. c = serialGet();
  116. while (!serialHasChar()) { // Wait for answer
  117. if ((getSystemTime() - timestamp) <= timeout) {
  118. wdt_reset();
  119. } else {
  120. setAnimationCount(0);
  121. return;
  122. }
  123. }
  124. c = serialGet();
  125. while (!serialHasChar()) { // Wait for answer
  126. if ((getSystemTime() - timestamp) <= timeout) {
  127. wdt_reset();
  128. } else {
  129. setAnimationCount(0);
  130. return;
  131. }
  132. }
  133. c = serialGet();
  134. serialWrite(OK);
  135. setAnimationCount(completeCount);
  136. }
  137. void transmitAnimations(void) {
  138. serialWrite(ERROR);
  139. }
  140. void sendAudioData(void) {
  141. uint8_t i;
  142. uint8_t *audioData = getAudioData();
  143. if (audioData == NULL) {
  144. serialWriteString(getString(21));
  145. } else {
  146. serialWriteString(getString(22));
  147. for (i = 0; i < 7; i++) {
  148. serialWrite(i + '0');
  149. serialWriteString(": ");
  150. itoa(audioData[i], buffer, 10);
  151. serialWriteString(buffer);
  152. serialWrite('\n');
  153. }
  154. }
  155. }
  156. void printTime(void) {
  157. serialWriteString(getString(14));
  158. serialWriteString(ltoa(getSystemTime(), buffer, 10));
  159. serialWriteString("ms");
  160. if (getSystemTime() > 60000) {
  161. serialWriteString(" (");
  162. serialWriteString(itoa(getSystemTime() / 60000, buffer, 10));
  163. serialWriteString(" min)");
  164. }
  165. if (getSystemTime() > 1000) {
  166. serialWriteString(" (");
  167. serialWriteString(itoa(getSystemTime() / 1000, buffer, 10));
  168. itoa(getSystemTime() % 1000, buffer, 10);
  169. if (buffer[0] != '\0')
  170. serialWrite('.');
  171. if (buffer[2] == '\0')
  172. serialWrite('0');
  173. if (buffer[1] == '\0')
  174. serialWrite('0');
  175. if (buffer[0] != '\0')
  176. serialWriteString(buffer);
  177. serialWriteString("s)\n");
  178. } else {
  179. serialWrite('\n');
  180. }
  181. }
  182. void dumpFrame(uint8_t *f) {
  183. uint8_t zeile, spalte;
  184. for (zeile = 0; zeile < 8; zeile++) {
  185. serialWrite(zeile + '0');
  186. serialWriteString(": ");
  187. for (spalte = 0; spalte < 8; spalte++) {
  188. itoa(f[(8 * zeile) + spalte], buffer, 16);
  189. serialWriteString(buffer);
  190. serialWrite(' ');
  191. }
  192. serialWrite('\n');
  193. }
  194. serialWriteString(getString(35));
  195. itoa(f[64], buffer, 10);
  196. serialWriteString(buffer);
  197. serialWrite('\n');
  198. }
  199. uint8_t *readLine(void) {
  200. uint8_t ptr = 0;
  201. while(1) {
  202. wdt_reset();
  203. if (serialHasChar()) {
  204. buffer[ptr] = serialGet();
  205. serialWrite(buffer[ptr]);
  206. if ((buffer[ptr] == '\n') || (ptr == sizeof(buffer) - 1)) {
  207. buffer[ptr] = '\0';
  208. return (uint8_t *)buffer;
  209. }
  210. ptr++;
  211. }
  212. }
  213. }
  214. uint8_t readNumber(uint8_t base) {
  215. uint8_t *s = readLine();
  216. uint8_t val = (uint8_t)strtoul((char *)s, NULL, base);
  217. return val;
  218. }
  219. void writeNumber(uint8_t num, uint8_t base) {
  220. itoa(num, buffer, base);
  221. serialWriteString(buffer);
  222. }
  223. uint8_t *readAFrame(void) {
  224. uint8_t *frame = (uint8_t *)malloc(65);
  225. uint8_t byte;
  226. serialWriteString(getString(35)); // "Duration: "
  227. frame[64] = readNumber(10);
  228. for (byte = 0; byte < 64; byte++) {
  229. writeNumber(byte, 10);
  230. serialWriteString(": "); // "xx: "
  231. frame[byte] = readNumber(16);
  232. }
  233. serialWriteString(getString(33));
  234. return frame;
  235. }
  236. void simpleAnimationInput(void) {
  237. uint8_t start, j, d;
  238. int8_t i;
  239. uint8_t data[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  240. uint8_t *frame = (uint8_t *)malloc(65);
  241. frame[64] = 2;
  242. serialWriteString(getString(36));
  243. start = readNumber(10);
  244. serialWriteString(getString(37));
  245. for (i = 0; i < 8; i++) {
  246. writeNumber(i, 10);
  247. serialWriteString(": ");
  248. data[i] = readNumber(16);
  249. }
  250. serialWriteString(getString(38));
  251. for (i = 0; i < 8; i++) {
  252. d = 0;
  253. for (j = 0; j < 64; j++) {
  254. if ((j < (8 * (i + 1))) && (j >= (8 * i))) {
  255. frame[j] = data[d++];
  256. } else {
  257. frame[j] = 0;
  258. }
  259. }
  260. setFrame(i + start, frame);
  261. }
  262. serialWriteString(getString(33)); // Done
  263. free(frame);
  264. }