forked from esphome/esphome
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87944f0
commit d18bb34
Showing
28 changed files
with
2,002 additions
and
579 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from esphome import automation | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.const import CONF_DURATION, CONF_ID | ||
|
||
from ...cpp_generator import MockObj | ||
from .automation import action_to_code | ||
from .defines import CONF_AUTO_START, CONF_MAIN, CONF_REPEAT_COUNT, CONF_SRC | ||
from .helpers import lvgl_components_required | ||
from .img import CONF_IMAGE | ||
from .label import CONF_LABEL | ||
from .lv_validation import lv_image, lv_milliseconds | ||
from .lvcode import lv, lv_expr | ||
from .types import LvType, ObjUpdateAction, void_ptr | ||
from .widget import Widget, WidgetType, get_widgets | ||
|
||
CONF_ANIMIMG = "animimg" | ||
CONF_SRC_LIST_ID = "src_list_id" | ||
|
||
|
||
def lv_repeat_count(value): | ||
if isinstance(value, str) and value.lower() in ("forever", "infinite"): | ||
value = 0xFFFF | ||
return cv.int_range(min=0, max=0xFFFF)(value) | ||
|
||
|
||
ANIMIMG_BASE_SCHEMA = cv.Schema( | ||
{ | ||
cv.Optional(CONF_REPEAT_COUNT, default="forever"): lv_repeat_count, | ||
cv.Optional(CONF_AUTO_START, default=True): cv.boolean, | ||
} | ||
) | ||
ANIMIMG_SCHEMA = ANIMIMG_BASE_SCHEMA.extend( | ||
{ | ||
cv.Required(CONF_DURATION): lv_milliseconds, | ||
cv.Required(CONF_SRC): cv.ensure_list(lv_image), | ||
cv.GenerateID(CONF_SRC_LIST_ID): cv.declare_id(void_ptr), | ||
} | ||
) | ||
|
||
ANIMIMG_MODIFY_SCHEMA = ANIMIMG_BASE_SCHEMA.extend( | ||
{ | ||
cv.Optional(CONF_DURATION): lv_milliseconds, | ||
} | ||
) | ||
|
||
lv_animimg_t = LvType("lv_animimg_t") | ||
|
||
|
||
class AnimimgType(WidgetType): | ||
def __init__(self): | ||
super().__init__( | ||
CONF_ANIMIMG, | ||
lv_animimg_t, | ||
(CONF_MAIN,), | ||
ANIMIMG_SCHEMA, | ||
ANIMIMG_MODIFY_SCHEMA, | ||
) | ||
|
||
async def to_code(self, w: Widget, config): | ||
lvgl_components_required.add(CONF_IMAGE) | ||
lvgl_components_required.add(CONF_ANIMIMG) | ||
if CONF_SRC in config: | ||
for x in config[CONF_SRC]: | ||
await cg.get_variable(x) | ||
srcs = [lv_expr.img_from(MockObj(x)) for x in config[CONF_SRC]] | ||
src_id = cg.static_const_array(config[CONF_SRC_LIST_ID], srcs) | ||
count = len(config[CONF_SRC]) | ||
lv.animimg_set_src(w.obj, src_id, count) | ||
lv.animimg_set_repeat_count(w.obj, config[CONF_REPEAT_COUNT]) | ||
lv.animimg_set_duration(w.obj, config[CONF_DURATION]) | ||
if config.get(CONF_AUTO_START): | ||
lv.animimg_start(w.obj) | ||
|
||
def get_uses(self): | ||
return CONF_IMAGE, CONF_LABEL | ||
|
||
|
||
animimg_spec = AnimimgType() | ||
|
||
|
||
@automation.register_action( | ||
"lvgl.animimg.start", | ||
ObjUpdateAction, | ||
cv.maybe_simple_value( | ||
{ | ||
cv.Required(CONF_ID): cv.use_id(lv_animimg_t), | ||
}, | ||
key=CONF_ID, | ||
), | ||
) | ||
async def animimg_start(config, action_id, template_arg, args): | ||
widget = await get_widgets(config) | ||
|
||
async def do_start(w: Widget): | ||
lv.animimg_start(w.obj) | ||
|
||
return await action_to_code(widget, do_start, action_id, template_arg, args) | ||
|
||
|
||
@automation.register_action( | ||
"lvgl.animimg.stop", | ||
ObjUpdateAction, | ||
cv.maybe_simple_value( | ||
{ | ||
cv.Required(CONF_ID): cv.use_id(lv_animimg_t), | ||
}, | ||
key=CONF_ID, | ||
), | ||
) | ||
async def animimg_stop(config, action_id, template_arg, args): | ||
widget = await get_widgets(config) | ||
|
||
async def do_stop(w: Widget): | ||
lv.animimg_stop(w.obj) | ||
|
||
return await action_to_code(widget, do_stop, action_id, template_arg, args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import esphome.config_validation as cv | ||
from esphome.const import ( | ||
CONF_MAX_VALUE, | ||
CONF_MIN_VALUE, | ||
CONF_MODE, | ||
CONF_ROTATION, | ||
CONF_VALUE, | ||
) | ||
from esphome.cpp_types import nullptr | ||
|
||
from .defines import ( | ||
ARC_MODES, | ||
CONF_ADJUSTABLE, | ||
CONF_CHANGE_RATE, | ||
CONF_END_ANGLE, | ||
CONF_INDICATOR, | ||
CONF_KNOB, | ||
CONF_MAIN, | ||
CONF_START_ANGLE, | ||
literal, | ||
) | ||
from .lv_validation import angle, get_start_value, lv_float | ||
from .lvcode import lv, lv_obj | ||
from .types import LvNumber, NumberType | ||
from .widget import Widget | ||
|
||
CONF_ARC = "arc" | ||
ARC_SCHEMA = cv.Schema( | ||
{ | ||
cv.Optional(CONF_VALUE): lv_float, | ||
cv.Optional(CONF_MIN_VALUE, default=0): cv.int_, | ||
cv.Optional(CONF_MAX_VALUE, default=100): cv.int_, | ||
cv.Optional(CONF_START_ANGLE, default=135): angle, | ||
cv.Optional(CONF_END_ANGLE, default=45): angle, | ||
cv.Optional(CONF_ROTATION, default=0.0): angle, | ||
cv.Optional(CONF_ADJUSTABLE, default=False): bool, | ||
cv.Optional(CONF_MODE, default="NORMAL"): ARC_MODES.one_of, | ||
cv.Optional(CONF_CHANGE_RATE, default=720): cv.uint16_t, | ||
} | ||
) | ||
|
||
ARC_MODIFY_SCHEMA = cv.Schema( | ||
{ | ||
cv.Optional(CONF_VALUE): lv_float, | ||
} | ||
) | ||
|
||
|
||
class ArcType(NumberType): | ||
def __init__(self): | ||
super().__init__( | ||
CONF_ARC, | ||
LvNumber("lv_arc_t"), | ||
parts=(CONF_MAIN, CONF_INDICATOR, CONF_KNOB), | ||
schema=ARC_SCHEMA, | ||
modify_schema=ARC_MODIFY_SCHEMA, | ||
) | ||
|
||
async def to_code(self, w: Widget, config): | ||
if CONF_MIN_VALUE in config: | ||
lv.arc_set_range(w.obj, config[CONF_MIN_VALUE], config[CONF_MAX_VALUE]) | ||
lv.arc_set_bg_angles( | ||
w.obj, config[CONF_START_ANGLE] // 10, config[CONF_END_ANGLE] // 10 | ||
) | ||
lv.arc_set_rotation(w.obj, config[CONF_ROTATION] // 10) | ||
lv.arc_set_mode(w.obj, literal(config[CONF_MODE])) | ||
lv.arc_set_change_rate(w.obj, config[CONF_CHANGE_RATE]) | ||
|
||
if config.get(CONF_ADJUSTABLE) is False: | ||
lv_obj.remove_style(w.obj, nullptr, literal("LV_PART_KNOB")) | ||
w.clear_flag("LV_OBJ_FLAG_CLICKABLE") | ||
|
||
value = await get_start_value(config) | ||
if value is not None: | ||
lv.arc_set_value(w.obj, value) | ||
|
||
|
||
arc_spec = ArcType() |
Oops, something went wrong.