Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mem.c 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * mem.c - HardwareEmulator
  3. * frame is represented as 65 bytes.
  4. * 0 - 63: frame data
  5. * 64: duration, 0 => 1/24th second
  6. *
  7. * mem.c
  8. *
  9. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  10. *
  11. * This file is part of LED-Cube.
  12. *
  13. * LED-Cube is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * LED-Cube is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "mem.h"
  30. char *mem = NULL;
  31. int frameCount = 0;
  32. // return != 0 if error
  33. int addFrame(char *frame) {
  34. char *newMem;
  35. if (mem != NULL) {
  36. newMem = (char *)malloc(65 * (frameCount + 1));
  37. if (newMem == NULL) {
  38. return 1;
  39. }
  40. memcpy(newMem, mem, 65 * frameCount); // Copy old frames
  41. free(mem);
  42. } else {
  43. frameCount = 0;
  44. newMem = (char *)malloc(65 * (frameCount + 1));
  45. if (newMem == NULL) {
  46. return 1;
  47. }
  48. }
  49. memcpy((newMem + (65 * frameCount)), frame, 65); // Add new frame
  50. frameCount++;
  51. mem = newMem;
  52. return 0;
  53. }
  54. // returns NULL if error
  55. char *getFrame(int index) {
  56. if (index >= frameCount) {
  57. return NULL;
  58. } else if (mem == NULL) {
  59. return NULL;
  60. } else {
  61. return (mem + (65 * index));
  62. }
  63. }
  64. int framesStored() {
  65. return frameCount;
  66. }
  67. void clearMemory() {
  68. free(mem);
  69. mem = NULL;
  70. frameCount = 0;
  71. }