diff --git a/HansonServo.ino b/HansonServo.ino index c547de6..e6efebb 100644 --- a/HansonServo.ino +++ b/HansonServo.ino @@ -52,13 +52,15 @@ void setup() { return; } - // sweep.clear(); - // sweep.createSampleSweep(4); - // sweep.saveToFile("/sweep.anim"); + sweep.clear(); + sweep.createSampleSweep(4); + sweep.saveToFile("/sweep.anim"); // sweep.clear(); // sweep.createStaggeredSweep(4); // sweep.saveToFile("/stagger.anim"); - // delay(9999); + //delay(9999); + sweep.printKeyframes(); + // anim.clear(); // if (!sweep.loadFromFile("/sweep.anim")) { diff --git a/animation.cpp b/animation.cpp index 4ecf5dc..b141033 100644 --- a/animation.cpp +++ b/animation.cpp @@ -39,7 +39,21 @@ const std::vector& Animation::getKeyframes() const { return keyframes; } - +void Animation::printKeyframes() { + const std::vector& frames = getKeyframes(); + Serial.println("Keyframes:"); + for (size_t i = 0; i < frames.size(); ++i) { + const Keyframe& kf = frames[i]; + Serial.print(" ["); + Serial.print(i); + Serial.print("] Motor ID: "); + Serial.print(kf.motorId); + Serial.print(", Frame: "); + Serial.print(kf.frame); + Serial.print(", Position: "); + Serial.println(kf.position); + } +} void Animation::clear() { memset(data, 0, sizeof(data)); @@ -77,7 +91,7 @@ bool Animation::saveToFile(const char* filename) { file.write((uint8_t*)&header, sizeof(header)); file.write((uint8_t*)data, sizeof(data)); - // Write keyframe count + // Write keyframe count uint16_t keyframeCount = keyframes.size(); file.write((uint8_t*)&keyframeCount, sizeof(keyframeCount)); @@ -88,6 +102,8 @@ bool Animation::saveToFile(const char* filename) { file.write((uint8_t*)&kf.position, sizeof(kf.position)); } + + file.close(); return true; } @@ -118,6 +134,7 @@ bool Animation::loadFromFile(const char* filename) { return false; } + // Read keyframe count uint16_t keyframeCount; if (file.read((uint8_t*)&keyframeCount, sizeof(keyframeCount)) != sizeof(keyframeCount)) { @@ -138,6 +155,7 @@ bool Animation::loadFromFile(const char* filename) { keyframes.push_back(kf); } + file.close(); return true; } @@ -174,6 +192,7 @@ void Animation::createSampleSweep(uint8_t seconds) { addKeyframe(ch, sweepFrames - 1, 1023); // Peak addKeyframe(ch, totalFrames - 1, 0); // Return to 0 } + } void Animation::createStaggeredSweep(uint8_t seconds) { diff --git a/animation.h b/animation.h index 28fbb16..f9f9c06 100644 --- a/animation.h +++ b/animation.h @@ -6,6 +6,7 @@ #include "FFat.h" #include + #define NUM_CHANNELS 5 #define FRAMES_PER_SECOND 50 #define MAX_DURATION_SECONDS 10 @@ -19,14 +20,14 @@ struct AnimationHeader { uint8_t reserved[8]; // 8–15 }; - -struct Keyframe { +struct __attribute__((packed)) Keyframe { uint8_t motorId; uint16_t frame; uint16_t position; }; + class Animation { public: Animation(); @@ -37,7 +38,7 @@ public: void addKeyframe(uint8_t motorId, uint16_t frame, uint16_t position); const std::vector& getKeyframes() const; - + void printKeyframes(); void clear(); uint16_t* getRawData(); // Optional: for bulk access size_t getSize() const; @@ -50,7 +51,8 @@ public: private: AnimationHeader header; uint16_t data[MAX_FRAMES][NUM_CHANNELS]; - std::vector keyframes; + std::vector keyframes; + }; #endif