aboutsummaryrefslogtreecommitdiff
path: root/src/misc/lv_utils.c
blob: 868692170c8f37dc4473a0c1d4fbc50170eb431c (plain)
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
/**
 * @file lv_utils.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_utils.h"
#include "lv_fs.h"
#include "lv_types.h"
#include "cache/lv_image_cache.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void * lv_utils_bsearch(const void * key, const void * base, size_t n, size_t size,
                        int (*cmp)(const void * pRef, const void * pElement))
{
    const char * middle;
    int32_t c;

    for(middle = base; n != 0;) {
        middle += (n / 2) * size;
        if((c = (*cmp)(key, middle)) > 0) {
            n    = (n / 2) - ((n & 1) == 0);
            base = (middle += size);
        }
        else if(c < 0) {
            n /= 2;
            middle = base;
        }
        else {
            return (char *)middle;
        }
    }
    return NULL;
}

lv_result_t lv_draw_buf_save_to_file(const lv_draw_buf_t * draw_buf, const char * path)
{
    lv_fs_file_t file;
    lv_fs_res_t res = lv_fs_open(&file, path, LV_FS_MODE_WR);
    if(res != LV_FS_RES_OK) {
        LV_LOG_ERROR("create file %s failed", path);
        return LV_RESULT_INVALID;
    }

    /*Image content modified, invalidate image cache.*/
    lv_image_cache_drop(path);

    uint32_t bw;
    res = lv_fs_write(&file, &draw_buf->header, sizeof(draw_buf->header), &bw);
    if(res != LV_FS_RES_OK || bw != sizeof(draw_buf->header)) {
        LV_LOG_ERROR("write draw_buf->header failed");
        lv_fs_close(&file);
        return LV_RESULT_INVALID;
    }

    res = lv_fs_write(&file, draw_buf->data, draw_buf->data_size, &bw);
    if(res != LV_FS_RES_OK || bw != draw_buf->data_size) {
        LV_LOG_ERROR("write draw_buf->data failed");
        lv_fs_close(&file);
        return LV_RESULT_INVALID;
    }

    lv_fs_close(&file);
    LV_LOG_TRACE("saved draw_buf to %s", path);
    return LV_RESULT_OK;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/