HansonServo/animation.h

89 lines
2.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma once
#include <Arduino.h>
#include "FS.h"
#include "FFat.h"
#include <unordered_map>
#include <vector>
#include "nodegraph.h"
#define NUM_CHANNELS 5
#define FRAMES_PER_SECOND 50
#define MAX_DURATION_SECONDS 10
#define MAX_FRAMES (FRAMES_PER_SECOND * MAX_DURATION_SECONDS)
struct AnimationHeader {
char magic[4]; // 03 ANIM tag
uint16_t frameCount; // 45 Number of total frames
uint8_t version; // 6
uint8_t frameRate; // 7 Frames per second
uint8_t reserved[8]; // 815
};
struct __attribute__((packed)) CurveSegment {
uint8_t motorID;
uint16_t startTime;// centiseconds (0.01s) MAX 655.35 seconds
uint16_t endTime;// centiseconds (0.01s) MAX 655.35 seconds
// remapped from -1 to 1 → 065535
int16_t startPointY;
uint16_t startHandleX;
int16_t startHandleY;
uint16_t endHandleX;
int16_t endHandleY;
int16_t endPointY;
};
// Version 2 frame data: motor ID + position pair
struct __attribute__((packed)) MotorPosition {
uint8_t motorID;
uint16_t position; // 0-4095
};
class Animation {
public:
Animation();
bool isActive() const { return active; }
void setActive(bool state);
void setFrame(uint16_t frameIndex, uint16_t channel, uint16_t value);
uint16_t getFrame(uint16_t frameIndex, uint16_t channel) const;
void addCurveSegment(const CurveSegment& segment);
void clearCurves(uint8_t motorID);
void clearAllCurves();
String printCurves();
String printAnim();
uint16_t getMotorPosition(uint8_t motorID, uint16_t timeCS);
// Version 2 frame data methods
void setFrameData(uint16_t frameIndex, const std::vector<MotorPosition>& motors);
const std::vector<MotorPosition>* getFrameData(uint16_t frameIndex) const;
void clearFrameData();
void clear();
//uint16_t* getRawData(); // Optional: for bulk access
//size_t getSize() const;
bool saveToFile(const char* filename);
bool loadFromFile(const char* filename);
uint16_t getFrameCount() const;
void setFrameCount(uint16_t count);
void createBasicSCurve();
void createEaseOutCurve();
AnimationHeader header;
NodeGraph nodeGraph;
private:
//uint16_t data[MAX_FRAMES][NUM_CHANNELS];
std::unordered_map<uint8_t, std::vector<CurveSegment>> curves; // Version 1: curves
std::vector<std::vector<MotorPosition>> frameData; // Version 2: raw frame data
bool active = false;
};