This commit is contained in:
2026-02-26 19:56:43 -08:00
commit f000ee7c19
1137 changed files with 603879 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
Copyright (c) 2013-2015, LKC Technologies, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. Redistributions in binary form
must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.

View File

@@ -0,0 +1,582 @@
// Copyright (c) 2013, LKC Technologies, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer. Redistributions in binary
// form must reproduce the above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or other materials
// provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
// HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../../../lvgl.h"
#if LV_USE_BARCODE
#include "code128.h"
#include <string.h>
#define CODE128_MALLOC lv_malloc
#define CODE128_REALLOC lv_realloc
#define CODE128_FREE lv_free
#define CODE128_MEMSET lv_memset
#define CODE128_STRLEN lv_strlen
#define CODE128_ASSERT LV_ASSERT
#define CODE128_QUIET_ZONE_LEN 10
#define CODE128_CHAR_LEN 11
#define CODE128_STOP_CODE_LEN 13
#define CODE128_START_CODE_A 103
#define CODE128_START_CODE_B 104
#define CODE128_START_CODE_C 105
#define CODE128_MODE_A 'a'
#define CODE128_MODE_B 'b'
#define CODE128_MODE_C 'c'
#define CODE128_MIN_ENCODE_LEN (CODE128_QUIET_ZONE_LEN * 2 + CODE128_CHAR_LEN * 2 + CODE128_STOP_CODE_LEN)
static const int code128_pattern[] = {
// value: pattern, bar/space widths
1740, // 0: 11011001100, 212222
1644, // 1: 11001101100, 222122
1638, // 2: 11001100110, 222221
1176, // 3: 10010011000, 121223
1164, // 4: 10010001100, 121322
1100, // 5: 10001001100, 131222
1224, // 6: 10011001000, 122213
1220, // 7: 10011000100, 122312
1124, // 8: 10001100100, 132212
1608, // 9: 11001001000, 221213
1604, // 10: 11001000100, 221312
1572, // 11: 11000100100, 231212
1436, // 12: 10110011100, 112232
1244, // 13: 10011011100, 122132
1230, // 14: 10011001110, 122231
1484, // 15: 10111001100, 113222
1260, // 16: 10011101100, 123122
1254, // 17: 10011100110, 123221
1650, // 18: 11001110010, 223211
1628, // 19: 11001011100, 221132
1614, // 20: 11001001110, 221231
1764, // 21: 11011100100, 213212
1652, // 22: 11001110100, 223112
1902, // 23: 11101101110, 312131
1868, // 24: 11101001100, 311222
1836, // 25: 11100101100, 321122
1830, // 26: 11100100110, 321221
1892, // 27: 11101100100, 312212
1844, // 28: 11100110100, 322112
1842, // 29: 11100110010, 322211
1752, // 30: 11011011000, 212123
1734, // 31: 11011000110, 212321
1590, // 32: 11000110110, 232121
1304, // 33: 10100011000, 111323
1112, // 34: 10001011000, 131123
1094, // 35: 10001000110, 131321
1416, // 36: 10110001000, 112313
1128, // 37: 10001101000, 132113
1122, // 38: 10001100010, 132311
1672, // 39: 11010001000, 211313
1576, // 40: 11000101000, 231113
1570, // 41: 11000100010, 231311
1464, // 42: 10110111000, 112133
1422, // 43: 10110001110
1134, // 44: 10001101110
1496, // 45: 10111011000, 113123
1478, // 46: 10111000110, 113321
1142, // 47: 10001110110, 133121
1910, // 48: 11101110110, 313121
1678, // 49: 11010001110, 211331
1582, // 50: 11000101110, 231131
1768, // 51: 11011101000, 213113
1762, // 52: 11011100010, 213311
1774, // 53: 11011101110, 213131
1880, // 54: 11101011000, 311123
1862, // 55: 11101000110, 311321
1814, // 56: 11100010110, 331121
1896, // 57: 11101101000, 312113
1890, // 58: 11101100010, 312311
1818, // 59: 11100011010, 332111
1914, // 60: 11101111010, 314111
1602, // 61: 11001000010, 221411
1930, // 62: 11110001010, 431111
1328, // 63: 10100110000, 111224
1292, // 64: 10100001100, 111422
1200, // 65: 10010110000, 121124
1158, // 66: 10010000110, 121421
1068, // 67: 10000101100, 141122
1062, // 68: 10000100110, 141221
1424, // 69: 10110010000, 112214
1412, // 70: 10110000100, 112412
1232, // 71: 10011010000, 122114
1218, // 72: 10011000010, 122411
1076, // 73: 10000110100, 142112
1074, // 74: 10000110010, 142211
1554, // 75: 11000010010, 241211
1616, // 76: 11001010000, 221114
1978, // 77: 11110111010, 413111
1556, // 78: 11000010100, 241112
1146, // 79: 10001111010, 134111
1340, // 80: 10100111100, 111242
1212, // 81: 10010111100, 121142
1182, // 82: 10010011110, 121241
1508, // 83: 10111100100, 114212
1268, // 84: 10011110100, 124112
1266, // 85: 10011110010, 124211
1956, // 86: 11110100100, 411212
1940, // 87: 11110010100, 421112
1938, // 88: 11110010010, 421211
1758, // 89: 11011011110, 212141
1782, // 90: 11011110110, 214121
1974, // 91: 11110110110, 412121
1400, // 92: 10101111000, 111143
1310, // 93: 10100011110, 111341
1118, // 94: 10001011110, 131141
1512, // 95: 10111101000, 114113
1506, // 96: 10111100010, 114311
1960, // 97: 11110101000, 411113
1954, // 98: 11110100010, 411311
1502, // 99: 10111011110, 113141
1518, // 100: 10111101110, 114131
1886, // 101: 11101011110, 311141
1966, // 102: 11110101110, 411131
1668, // 103: 11010000100, 211412
1680, // 104: 11010010000, 211214
1692 // 105: 11010011100, 211232
};
static const int code128_stop_pattern = 6379; // 1100011101011, 2331112
struct code128_step {
int prev_ix; // Index of previous step, if any
const char * next_input; // Remaining input
unsigned short len; // The length of the pattern so far (includes this step)
char mode; // State for the current encoding
signed char code; // What code should be written for this step
};
struct code128_state {
struct code128_step * steps;
int allocated_steps;
int current_ix;
int todo_ix;
int best_ix;
size_t maxlength;
};
size_t code128_estimate_len(const char * s)
{
return CODE128_QUIET_ZONE_LEN
+ CODE128_CHAR_LEN // start code
+ CODE128_CHAR_LEN * (CODE128_STRLEN(s) * 11 / 10) // contents + 10% padding
+ CODE128_CHAR_LEN // checksum
+ CODE128_STOP_CODE_LEN
+ CODE128_QUIET_ZONE_LEN;
}
static void code128_append_pattern(int pattern, int pattern_length, char * out)
{
// All patterns have their first bit set by design
CODE128_ASSERT(pattern & (1 << (pattern_length - 1)));
int i;
for(i = pattern_length - 1; i >= 0; i--) {
// cast avoids warning: implicit conversion from 'int' to 'char' changes value from 255 to -1 [-Wconstant-conversion]
*out++ = (unsigned char)((pattern & (1 << i)) ? 255 : 0);
}
}
static int code128_append_code(int code, char * out)
{
CODE128_ASSERT(code >= 0 && code < (int)(sizeof(code128_pattern) / sizeof(code128_pattern[0])));
code128_append_pattern(code128_pattern[code], CODE128_CHAR_LEN, out);
return CODE128_CHAR_LEN;
}
static int code128_append_stop_code(char * out)
{
code128_append_pattern(code128_stop_pattern, CODE128_STOP_CODE_LEN, out);
return CODE128_STOP_CODE_LEN;
}
static signed char code128_switch_code(char from_mode, char to_mode)
{
switch(from_mode) {
case CODE128_MODE_A:
switch(to_mode) {
case CODE128_MODE_B:
return 100;
case CODE128_MODE_C:
return 99;
}
break;
case CODE128_MODE_B:
switch(to_mode) {
case CODE128_MODE_A:
return 101;
case CODE128_MODE_C:
return 99;
}
break;
case CODE128_MODE_C:
switch(to_mode) {
case CODE128_MODE_B:
return 100;
case CODE128_MODE_A:
return 101;
}
break;
default:
break;
}
CODE128_ASSERT(0); // Invalid mode switch
return -1;
}
static signed char code128a_ascii_to_code(signed char value)
{
if(value >= ' ' && value <= '_')
return (signed char)(value - ' ');
else if(value >= 0 && value < ' ')
return (signed char)(value + 64);
else if(value == (signed char)CODE128_FNC1)
return 102;
else if(value == (signed char)CODE128_FNC2)
return 97;
else if(value == (signed char)CODE128_FNC3)
return 96;
else if(value == (signed char)CODE128_FNC4)
return 101;
else
return -1;
}
static signed char code128b_ascii_to_code(signed char value)
{
if(value >= ' ') // value <= 127 is implied
return (signed char)(value - ' ');
else if(value == (signed char)CODE128_FNC1)
return 102;
else if(value == (signed char)CODE128_FNC2)
return 97;
else if(value == (signed char)CODE128_FNC3)
return 96;
else if(value == (signed char)CODE128_FNC4)
return 100;
else
return -1;
}
static signed char code128c_ascii_to_code(const char * values)
{
if(values[0] == CODE128_FNC1)
return 102;
if(values[0] >= '0' && values[0] <= '9' &&
values[1] >= '0' && values[1] <= '9') {
char code = 10 * (values[0] - '0') + (values[1] - '0');
return code;
}
return -1;
}
static int code128_do_a_step(struct code128_step * base, int prev_ix, int ix)
{
struct code128_step * previous_step = &base[prev_ix];
struct code128_step * step = &base[ix];
char value = *previous_step->next_input;
// NOTE: Currently we can't encode NULL
if(value == 0)
return 0;
step->code = code128a_ascii_to_code(value);
if(step->code < 0)
return 0;
step->prev_ix = prev_ix;
step->next_input = previous_step->next_input + 1;
step->mode = CODE128_MODE_A;
step->len = previous_step->len + CODE128_CHAR_LEN;
if(step->mode != previous_step->mode)
step->len += CODE128_CHAR_LEN; // Need to switch modes
return 1;
}
static int code128_do_b_step(struct code128_step * base, int prev_ix, int ix)
{
struct code128_step * previous_step = &base[prev_ix];
struct code128_step * step = &base[ix];
char value = *previous_step->next_input;
// NOTE: Currently we can't encode NULL
if(value == 0)
return 0;
step->code = code128b_ascii_to_code(value);
if(step->code < 0)
return 0;
step->prev_ix = prev_ix;
step->next_input = previous_step->next_input + 1;
step->mode = CODE128_MODE_B;
step->len = previous_step->len + CODE128_CHAR_LEN;
if(step->mode != previous_step->mode)
step->len += CODE128_CHAR_LEN; // Need to switch modes
return 1;
}
static int code128_do_c_step(struct code128_step * base, int prev_ix, int ix)
{
struct code128_step * previous_step = &base[prev_ix];
struct code128_step * step = &base[ix];
char value = *previous_step->next_input;
// NOTE: Currently we can't encode NULL
if(value == 0)
return 0;
step->code = code128c_ascii_to_code(previous_step->next_input);
if(step->code < 0)
return 0;
step->prev_ix = prev_ix;
step->next_input = previous_step->next_input + 1;
// Mode C consumes 2 characters for codes 0-99
if(step->code < 100)
step->next_input++;
step->mode = CODE128_MODE_C;
step->len = previous_step->len + CODE128_CHAR_LEN;
if(step->mode != previous_step->mode)
step->len += CODE128_CHAR_LEN; // Need to switch modes
return 1;
}
static struct code128_step * code128_alloc_step(struct code128_state * state)
{
if(state->todo_ix >= state->allocated_steps) {
state->allocated_steps += 1024;
state->steps = (struct code128_step *) CODE128_REALLOC(state->steps,
state->allocated_steps * sizeof(struct code128_step));
}
struct code128_step * step = &state->steps[state->todo_ix];
CODE128_MEMSET(step, 0, sizeof(*step));
return step;
}
static void code128_do_step(struct code128_state * state)
{
struct code128_step * step = &state->steps[state->current_ix];
if(*step->next_input == 0) {
// Done, so see if we have a new shortest encoding.
if((step->len < state->maxlength) ||
(state->best_ix < 0 && step->len == state->maxlength)) {
state->best_ix = state->current_ix;
// Update maxlength to avoid considering anything longer
state->maxlength = step->len;
}
return;
}
// Don't try if we're already at or beyond the max acceptable
// length;
if(step->len >= state->maxlength)
return;
char mode = step->mode;
code128_alloc_step(state);
int mode_c_worked = 0;
// Always try mode C
if(code128_do_c_step(state->steps, state->current_ix, state->todo_ix)) {
state->todo_ix++;
code128_alloc_step(state);
mode_c_worked = 1;
}
if(mode == CODE128_MODE_A) {
// If A works, stick with A. There's no advantage to switching
// to B proactively if A still works.
if(code128_do_a_step(state->steps, state->current_ix, state->todo_ix) ||
code128_do_b_step(state->steps, state->current_ix, state->todo_ix))
state->todo_ix++;
}
else if(mode == CODE128_MODE_B) {
// The same logic applies here. There's no advantage to switching
// proactively to A if B still works.
if(code128_do_b_step(state->steps, state->current_ix, state->todo_ix) ||
code128_do_a_step(state->steps, state->current_ix, state->todo_ix))
state->todo_ix++;
}
else if(!mode_c_worked) {
// In mode C. If mode C worked and we're in mode C, trying anything
// else is pointless since the mode C encoding will be shorter and
// there won't be any mode switches.
// If we're leaving mode C, though, try both in case one ends up
// better than the other.
if(code128_do_a_step(state->steps, state->current_ix, state->todo_ix)) {
state->todo_ix++;
code128_alloc_step(state);
}
if(code128_do_b_step(state->steps, state->current_ix, state->todo_ix))
state->todo_ix++;
}
}
size_t code128_encode_raw(const char * s, char * out, size_t maxlength)
{
struct code128_state state;
const size_t overhead = CODE128_QUIET_ZONE_LEN
+ CODE128_CHAR_LEN // checksum
+ CODE128_STOP_CODE_LEN
+ CODE128_QUIET_ZONE_LEN;
if(maxlength < overhead + CODE128_CHAR_LEN + CODE128_CHAR_LEN) {
// Need space to encode the start character and one additional
// character.
return 0;
}
state.allocated_steps = 256;
state.steps = (struct code128_step *) CODE128_MALLOC(state.allocated_steps * sizeof(struct code128_step));
state.current_ix = 0;
state.todo_ix = 0;
state.maxlength = maxlength - overhead;
state.best_ix = -1;
// Initialize the first 3 steps for the 3 encoding routes (A, B, C)
state.steps[0].prev_ix = -1;
state.steps[0].next_input = s;
state.steps[0].len = CODE128_CHAR_LEN;
state.steps[0].mode = CODE128_MODE_C;
state.steps[0].code = CODE128_START_CODE_C;
state.steps[1].prev_ix = -1;
state.steps[1].next_input = s;
state.steps[1].len = CODE128_CHAR_LEN;
state.steps[1].mode = CODE128_MODE_A;
state.steps[1].code = CODE128_START_CODE_A;
state.steps[2].prev_ix = -1;
state.steps[2].next_input = s;
state.steps[2].len = CODE128_CHAR_LEN;
state.steps[2].mode = CODE128_MODE_B;
state.steps[2].code = CODE128_START_CODE_B;
state.todo_ix = 3;
// Keep going until no more work
do {
code128_do_step(&state);
state.current_ix++;
} while(state.current_ix != state.todo_ix);
// If no best_step, then fail.
if(state.best_ix < 0) {
CODE128_FREE(state.steps);
return 0;
}
// Determine the list of codes
size_t num_codes = state.maxlength / CODE128_CHAR_LEN;
char * codes = CODE128_MALLOC(num_codes);
CODE128_ASSERT(codes);
struct code128_step * step = &state.steps[state.best_ix];
size_t i;
for(i = num_codes - 1; i > 0; --i) {
struct code128_step * prev_step = &state.steps[step->prev_ix];
codes[i] = step->code;
if(step->mode != prev_step->mode) {
--i;
codes[i] = code128_switch_code(prev_step->mode, step->mode);
}
step = prev_step;
}
codes[0] = step->code;
// Encode everything up to the checksum
size_t actual_length = state.maxlength + overhead;
CODE128_MEMSET(out, 0, CODE128_QUIET_ZONE_LEN);
out += CODE128_QUIET_ZONE_LEN;
for(i = 0; i < num_codes; i++)
out += code128_append_code(codes[i], out);
// Compute the checksum
int sum = codes[0];
for(i = 1; i < num_codes; i++)
sum += (int)(codes[i] * i);
out += code128_append_code(sum % 103, out);
// Finalize the code.
out += code128_append_stop_code(out);
CODE128_MEMSET(out, 0, CODE128_QUIET_ZONE_LEN);
CODE128_FREE(codes);
CODE128_FREE(state.steps);
return actual_length;
}
/**
* @brief Encode the GS1 string
*
* This converts [FNC1] sequences to raw FNC1 characters and
* removes spaces before encoding the barcodes.
*
* @return the length of barcode data in bytes
*/
size_t code128_encode_gs1(const char * s, char * out, size_t maxlength)
{
size_t raw_size = CODE128_STRLEN(s) + 1;
char * raw = CODE128_MALLOC(raw_size);
CODE128_ASSERT(raw);
if(!raw) {
return 0;
}
char * p = raw;
for(; *s != '\0'; s++) {
if(strncmp(s, "[FNC1]", 6) == 0) {
*p++ = CODE128_FNC1;
s += 5;
}
else if(*s != ' ') {
*p++ = *s;
}
}
*p = '\0';
size_t length = code128_encode_raw(raw, out, maxlength);
CODE128_FREE(raw);
return length;
}
#endif /*LV_USE_BARCODE*/

View File

@@ -0,0 +1,47 @@
// Copyright (c) 2013-15, LKC Technologies, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer. Redistributions in binary
// form must reproduce the above copyright notice, this list of conditions and
// the following disclaimer in the documentation and/or other materials
// provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
// HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef CODE128_H
#define CODE128_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
// Since the FNCn characters are not ASCII, define versions here to
// simplify encoding strings that include them.
#define CODE128_FNC1 '\xf1'
#define CODE128_FNC2 '\xf2'
#define CODE128_FNC3 '\xf3'
#define CODE128_FNC4 '\xf4'
size_t code128_estimate_len(const char * s);
size_t code128_encode_gs1(const char * s, char * out, size_t maxlength);
size_t code128_encode_raw(const char * s, char * out, size_t maxlength);
#ifdef __cplusplus
}
#endif
#endif // CODE128_H

View File

@@ -0,0 +1,327 @@
/**
* @file lv_barcode.c
*
*/
/*********************
* INCLUDES
*********************/
#include "../../core/lv_obj_class_private.h"
#include "lv_barcode_private.h"
#include "../../lvgl.h"
#if LV_USE_BARCODE
#include "code128.h"
#include "../../misc/cache/lv_cache.h"
/*********************
* DEFINES
*********************/
#define MY_CLASS (&lv_barcode_class)
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void lv_barcode_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
static void lv_barcode_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
static bool lv_barcode_change_buf_size(lv_obj_t * obj, int32_t w, int32_t h);
static void lv_barcode_clear(lv_obj_t * obj);
/**********************
* STATIC VARIABLES
**********************/
const lv_obj_class_t lv_barcode_class = {
.constructor_cb = lv_barcode_constructor,
.destructor_cb = lv_barcode_destructor,
.width_def = LV_SIZE_CONTENT,
.instance_size = sizeof(lv_barcode_t),
.base_class = &lv_canvas_class,
.name = "lv_barcode",
};
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
lv_obj_t * lv_barcode_create(lv_obj_t * parent)
{
LV_LOG_INFO("begin");
lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
lv_obj_class_init_obj(obj);
return obj;
}
void lv_barcode_set_dark_color(lv_obj_t * obj, lv_color_t color)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
barcode->dark_color = color;
}
void lv_barcode_set_light_color(lv_obj_t * obj, lv_color_t color)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
barcode->light_color = color;
}
void lv_barcode_set_scale(lv_obj_t * obj, uint16_t scale)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
if(scale == 0) {
scale = 1;
}
lv_barcode_t * barcode = (lv_barcode_t *)obj;
barcode->scale = scale;
}
void lv_barcode_set_direction(lv_obj_t * obj, lv_dir_t direction)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
barcode->direction = direction;
}
void lv_barcode_set_tiled(lv_obj_t * obj, bool tiled)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
barcode->tiled = tiled;
lv_image_set_inner_align(obj, tiled ? LV_IMAGE_ALIGN_TILE : LV_IMAGE_ALIGN_DEFAULT);
}
void lv_barcode_set_encoding(lv_obj_t * obj, lv_barcode_encoding_t encoding)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
barcode->encoding = encoding;
}
lv_result_t lv_barcode_update(lv_obj_t * obj, const char * data)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
LV_ASSERT_NULL(data);
if(data == NULL || lv_strlen(data) == 0) {
LV_LOG_WARN("data is empty");
lv_barcode_clear(obj);
return LV_RESULT_INVALID;
}
size_t len = code128_estimate_len(data);
LV_LOG_INFO("data: %s, len = %zu", data, len);
char * out_buf = lv_malloc(len);
LV_ASSERT_MALLOC(out_buf);
if(!out_buf) {
LV_LOG_ERROR("malloc failed for out_buf");
lv_barcode_clear(obj);
return LV_RESULT_INVALID;
}
lv_barcode_t * barcode = (lv_barcode_t *)obj;
int32_t barcode_w = 0;
switch(barcode->encoding) {
case LV_BARCODE_ENCODING_CODE128_GS1:
barcode_w = (int32_t) code128_encode_gs1(data, out_buf, len);
break;
case LV_BARCODE_ENCODING_CODE128_RAW:
barcode_w = (int32_t) code128_encode_raw(data, out_buf, len);
break;
}
LV_LOG_INFO("barcode width = %" LV_PRId32, barcode_w);
LV_ASSERT(barcode->scale > 0);
uint16_t scale = barcode->scale;
int32_t buf_w;
int32_t buf_h;
if(barcode->tiled) {
buf_w = (barcode->direction == LV_DIR_HOR) ? barcode_w * scale : 1;
buf_h = (barcode->direction == LV_DIR_VER) ? barcode_w * scale : 1;
}
else {
lv_obj_update_layout(obj);
buf_w = (barcode->direction == LV_DIR_HOR) ? barcode_w * scale : lv_obj_get_width(obj);
buf_h = (barcode->direction == LV_DIR_VER) ? barcode_w * scale : lv_obj_get_height(obj);
}
if(!lv_barcode_change_buf_size(obj, buf_w, buf_h)) {
lv_barcode_clear(obj);
lv_free(out_buf);
return LV_RESULT_INVALID;
}
/* Temporarily disable invalidation to improve the efficiency of lv_canvas_set_px */
lv_display_enable_invalidation(lv_obj_get_display(obj), false);
lv_draw_buf_t * draw_buf = lv_canvas_get_draw_buf(obj);
uint32_t stride = draw_buf->header.stride;
const lv_color_t color = lv_color_hex(1);
/* Clear the canvas */
lv_draw_buf_clear(draw_buf, NULL);
/* Set the palette */
lv_canvas_set_palette(obj, 0, lv_color_to_32(barcode->light_color, LV_OPA_COVER));
lv_canvas_set_palette(obj, 1, lv_color_to_32(barcode->dark_color, LV_OPA_COVER));
for(int32_t x = 0; x < barcode_w; x++) {
/*skip empty data*/
if(out_buf[x] == 0) {
continue;
}
for(uint16_t i = 0; i < scale; i++) {
int32_t offset = x * scale + i;
if(barcode->direction == LV_DIR_HOR) {
lv_canvas_set_px(obj, offset, 0, color, LV_OPA_COVER);
}
else { /*LV_DIR_VER*/
if(barcode->tiled) {
lv_canvas_set_px(obj, 0, offset, color, LV_OPA_COVER);
}
else {
uint8_t * dest = lv_draw_buf_goto_xy(draw_buf, 0, offset);
lv_memset(dest, 0xFF, stride);
}
}
}
}
/* Copy pixels by row */
if(!barcode->tiled && barcode->direction == LV_DIR_HOR && buf_h > 1) {
/* Skip the first row */
int32_t h = buf_h - 1;
const uint8_t * src = lv_draw_buf_goto_xy(draw_buf, 0, 0);
uint8_t * dest = lv_draw_buf_goto_xy(draw_buf, 0, 1);
while(h--) {
lv_memcpy(dest, src, stride);
dest += stride;
}
}
/* invalidate the canvas to refresh it */
lv_display_enable_invalidation(lv_obj_get_display(obj), true);
lv_obj_invalidate(obj);
lv_free(out_buf);
return LV_RESULT_OK;
}
lv_color_t lv_barcode_get_dark_color(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
return barcode->dark_color;
}
lv_color_t lv_barcode_get_light_color(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
return barcode->light_color;
}
uint16_t lv_barcode_get_scale(lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
return barcode->scale;
}
lv_barcode_encoding_t lv_barcode_get_encoding(const lv_obj_t * obj)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
const lv_barcode_t * barcode = (const lv_barcode_t *)obj;
return barcode->encoding;
}
/**********************
* STATIC FUNCTIONS
**********************/
static void lv_barcode_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
{
LV_UNUSED(class_p);
lv_barcode_t * barcode = (lv_barcode_t *)obj;
barcode->dark_color = lv_color_black();
barcode->light_color = lv_color_white();
barcode->scale = 1;
barcode->direction = LV_DIR_HOR;
barcode->encoding = LV_BARCODE_ENCODING_CODE128_GS1;
lv_image_set_inner_align(obj, LV_IMAGE_ALIGN_DEFAULT);
}
static void lv_barcode_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
{
LV_UNUSED(class_p);
lv_draw_buf_t * draw_buf = lv_canvas_get_draw_buf(obj);
if(draw_buf == NULL) return;
lv_image_cache_drop(draw_buf);
/*@fixme destroy buffer in cache free_cb.*/
lv_draw_buf_destroy(draw_buf);
}
static bool lv_barcode_change_buf_size(lv_obj_t * obj, int32_t w, int32_t h)
{
LV_ASSERT_NULL(obj);
if(w <= 0 || h <= 0) {
LV_LOG_WARN("invalid size: %" LV_PRId32 " x %" LV_PRId32, w, h);
return false;
}
lv_draw_buf_t * old_buf = lv_canvas_get_draw_buf(obj);
lv_draw_buf_t * new_buf = lv_draw_buf_create(w, h, LV_COLOR_FORMAT_I1, LV_STRIDE_AUTO);
if(new_buf == NULL) {
LV_LOG_ERROR("malloc failed for canvas buffer");
return false;
}
lv_canvas_set_draw_buf(obj, new_buf);
LV_LOG_INFO("set canvas buffer: %p, width = %" LV_PRId32, (void *)new_buf, w);
if(old_buf != NULL) lv_draw_buf_destroy(old_buf);
return true;
}
static void lv_barcode_clear(lv_obj_t * obj)
{
lv_draw_buf_t * draw_buf = lv_canvas_get_draw_buf(obj);
if(!draw_buf) {
return;
}
lv_draw_buf_clear(draw_buf, NULL);
lv_image_cache_drop(draw_buf);
lv_obj_invalidate(obj);
}
#endif /*LV_USE_BARCODE*/

View File

@@ -0,0 +1,143 @@
/**
* @file lv_barcode.h
*
*/
#ifndef LV_BARCODE_H
#define LV_BARCODE_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include "../../lv_conf_internal.h"
#include "../../misc/lv_types.h"
#include "../../misc/lv_color.h"
#include "../../widgets/canvas/lv_canvas.h"
#if LV_USE_BARCODE
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
typedef enum {
/**
* Code 128 with GS1 encoding. Strips `[FCN1]` and spaces.
*/
LV_BARCODE_ENCODING_CODE128_GS1,
/**
* Code 128 with raw encoding.
*/
LV_BARCODE_ENCODING_CODE128_RAW,
} lv_barcode_encoding_t;
LV_ATTRIBUTE_EXTERN_DATA extern const lv_obj_class_t lv_barcode_class;
/**********************
* GLOBAL PROTOTYPES
**********************/
/**
* Create an empty barcode (an `lv_canvas`) object.
* @param parent point to an object where to create the barcode
* @return pointer to the created barcode object
*/
lv_obj_t * lv_barcode_create(lv_obj_t * parent);
/**
* Set the dark color of a barcode object
* @param obj pointer to barcode object
* @param color dark color of the barcode
*/
void lv_barcode_set_dark_color(lv_obj_t * obj, lv_color_t color);
/**
* Set the light color of a barcode object
* @param obj pointer to barcode object
* @param color light color of the barcode
*/
void lv_barcode_set_light_color(lv_obj_t * obj, lv_color_t color);
/**
* Set the scale of a barcode object
* @param obj pointer to barcode object
* @param scale scale factor
*/
void lv_barcode_set_scale(lv_obj_t * obj, uint16_t scale);
/**
* Set the direction of a barcode object
* @param obj pointer to barcode object
* @param direction draw direction (`LV_DIR_HOR` or `LB_DIR_VER`)
*/
void lv_barcode_set_direction(lv_obj_t * obj, lv_dir_t direction);
/**
* Set the tiled mode of a barcode object
* @param obj pointer to barcode object
* @param tiled true: tiled mode, false: normal mode (default)
*/
void lv_barcode_set_tiled(lv_obj_t * obj, bool tiled);
/**
* Set the encoding of a barcode object
* @param obj pointer to barcode object
* @param encoding encoding (default is `LV_BARCODE_CODE128_GS1`)
*/
void lv_barcode_set_encoding(lv_obj_t * obj, lv_barcode_encoding_t encoding);
/**
* Set the data of a barcode object
* @param obj pointer to barcode object
* @param data data to display
* @return LV_RESULT_OK: if no error; LV_RESULT_INVALID: on error
*/
lv_result_t lv_barcode_update(lv_obj_t * obj, const char * data);
/**
* Get the dark color of a barcode object
* @param obj pointer to barcode object
* @return dark color of the barcode
*/
lv_color_t lv_barcode_get_dark_color(lv_obj_t * obj);
/**
* Get the light color of a barcode object
* @param obj pointer to barcode object
* @return light color of the barcode
*/
lv_color_t lv_barcode_get_light_color(lv_obj_t * obj);
/**
* Get the scale of a barcode object
* @param obj pointer to barcode object
* @return scale factor
*/
uint16_t lv_barcode_get_scale(lv_obj_t * obj);
/**
* Get the encoding of a barcode object
* @param obj pointer to barcode object
* @return encoding
*/
lv_barcode_encoding_t lv_barcode_get_encoding(const lv_obj_t * obj);
/**********************
* MACROS
**********************/
#endif /*LV_USE_BARCODE*/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_BARCODE_H*/

View File

@@ -0,0 +1,56 @@
/**
* @file lv_barcode_private.h
*
*/
#ifndef LV_BARCODE_PRIVATE_H
#define LV_BARCODE_PRIVATE_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include "../../widgets/canvas/lv_canvas_private.h"
#include "lv_barcode.h"
#if LV_USE_BARCODE
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/*Data of barcode*/
struct _lv_barcode_t {
lv_canvas_t canvas;
lv_color_t dark_color;
lv_color_t light_color;
uint16_t scale;
lv_dir_t direction;
bool tiled;
lv_barcode_encoding_t encoding;
};
/**********************
* GLOBAL PROTOTYPES
**********************/
/**********************
* MACROS
**********************/
#endif /* LV_USE_BARCODE */
#ifdef __cplusplus
} /*extern "C"*/
#endif
#endif /*LV_BARCODE_PRIVATE_H*/