wangrong
2026-08-24 beef6536e7c8dd3f5cbe8f73585611479910bea7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once
 
#include "../lvgl_image.h"
#include "gifdec.h"
#include <lvgl.h>
#include <memory>
#include <functional>
 
/**
 * C++ implementation of LVGL GIF widget
 * Provides GIF animation functionality using gifdec library
 */
class LvglGif {
public:
    explicit LvglGif(const lv_img_dsc_t* img_dsc);
    virtual ~LvglGif();
 
    // LvglImage interface implementation
    virtual const lv_img_dsc_t* image_dsc() const;
 
    /**
     * Start/restart GIF animation
     */
    void Start();
 
    /**
     * Pause GIF animation
     */
    void Pause();
 
    /**
     * Resume GIF animation
     */
    void Resume();
 
    /**
     * Stop GIF animation and rewind to first frame
     */
    void Stop();
 
    /**
     * Check if GIF is currently playing
     */
    bool IsPlaying() const;
 
    /**
     * Check if GIF was loaded successfully
     */
    bool IsLoaded() const;
 
    /**
     * Get loop count
     */
    int32_t GetLoopCount() const;
 
    /**
     * Set loop count
     */
    void SetLoopCount(int32_t count);
 
    /**
     * Get loop delay in milliseconds (delay between loops)
     */
    uint32_t GetLoopDelay() const;
 
    /**
     * Set loop delay in milliseconds (delay between loops)
     * @param delay_ms Delay in milliseconds before starting next loop. 0 means no delay.
     */
    void SetLoopDelay(uint32_t delay_ms);
 
    /**
     * Get GIF dimensions
     */
    uint16_t width() const;
    uint16_t height() const;
 
    /**
     * Set frame update callback
     */
    void SetFrameCallback(std::function<void()> callback);
 
private:
    // GIF decoder instance
    gd_GIF* gif_;
    
    // LVGL image descriptor
    lv_img_dsc_t img_dsc_;
    
    // Animation timer
    lv_timer_t* timer_;
    
    // Last frame update time
    uint32_t last_call_;
    
    // Animation state
    bool playing_;
    bool loaded_;
    
    // Loop delay configuration
    uint32_t loop_delay_ms_;      // Delay between loops in milliseconds
    bool loop_waiting_;           // Whether we're waiting for the next loop
    uint32_t loop_wait_start_;    // Timestamp when loop wait started
    
    // Frame update callback
    std::function<void()> frame_callback_;
    
    /**
     * Update to next frame
     */
    void NextFrame();
    
    /**
     * Cleanup resources
     */
    void Cleanup();
};