Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mem.c 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "mem.h"
  11. char *mem = NULL;
  12. int frameCount = 0;
  13. // return != 0 if error
  14. int addFrame(char *frame) {
  15. char *newMem, *oldMem = mem;
  16. int i;
  17. if (oldMem != NULL) {
  18. newMem = (char *)malloc(65 * frameCount);
  19. if (newMem == NULL) {
  20. return 1;
  21. }
  22. memcpy(newMem, oldMem, 65 * frameCount); // Copy old frames
  23. free(oldMem);
  24. } else {
  25. // oldMem == NULL
  26. frameCount = 0;
  27. newMem = (char *)malloc(65);
  28. if (newMem == NULL) {
  29. return 1;
  30. }
  31. }
  32. memcpy((newMem + (65 * frameCount)), frame, 65); // Add new frame
  33. frameCount++;
  34. mem = newMem;
  35. return 0;
  36. }
  37. // returns NULL if error
  38. char *getFrame(int index) {
  39. if (index >= frameCount) {
  40. return NULL;
  41. } else if (mem == NULL) {
  42. return NULL;
  43. } else {
  44. return (mem + (65 * index));
  45. }
  46. }
  47. int framesStored() {
  48. return frameCount;
  49. }
  50. void clearMemory() {
  51. free(mem);
  52. mem = NULL;
  53. frameCount = 0;
  54. }