コード例 #1
0
ファイル: image.py プロジェクト: lanbing8023/esphome-tools
def to_code(config):
    from PIL import Image

    path = CORE.relative_path(config[CONF_FILE])
    try:
        image = Image.open(path)
    except Exception as e:
        raise core.EsphomeError(u"Could not load image file {}: {}".format(path, e))

    if CONF_RESIZE in config:
        image.thumbnail(config[CONF_RESIZE])

    image = image.convert('1', dither=Image.NONE)
    width, height = image.size
    if width > 500 or height > 500:
        _LOGGER.warning("The image you requested is very big. Please consider using the resize "
                        "parameter")
    width8 = ((width + 7) // 8) * 8
    data = [0 for _ in range(height * width8 // 8)]
    for y in range(height):
        for x in range(width):
            if image.getpixel((x, y)):
                continue
            pos = x + y * width8
            data[pos // 8] |= 0x80 >> (pos % 8)

    rhs = safe_exp([HexInt(x) for x in data])
    prog_arr = progmem_array(config[CONF_RAW_DATA_ID], rhs)

    rhs = App.make_image(prog_arr, width, height)
    Pvariable(config[CONF_ID], rhs)
コード例 #2
0
def transmitter_base(full_config):
    name = full_config[CONF_NAME]
    key, config = next(
        (k, v) for k, v in full_config.items() if k in REMOTE_KEYS)

    if key == CONF_JVC:
        return JVCTransmitter.new(name, config[CONF_DATA])
    if key == CONF_LG:
        return LGTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS])
    if key == CONF_NEC:
        return NECTransmitter.new(name, config[CONF_ADDRESS],
                                  config[CONF_COMMAND])
    if key == CONF_PANASONIC:
        return PanasonicTransmitter.new(name, config[CONF_ADDRESS],
                                        config[CONF_COMMAND])
    if key == CONF_SAMSUNG:
        return SamsungTransmitter.new(name, config[CONF_DATA])
    if key == CONF_SONY:
        return SonyTransmitter.new(name, config[CONF_DATA], config[CONF_NBITS])
    if key == CONF_RC5:
        return RC5Transmitter.new(name, config[CONF_ADDRESS],
                                  config[CONF_COMMAND])
    if key == CONF_RAW:
        arr = progmem_array(config[CONF_ID], config[CONF_DATA])
        return RawTransmitter.new(name, arr, len(config[CONF_DATA]),
                                  config.get(CONF_CARRIER_FREQUENCY))
    if key == CONF_RC_SWITCH_RAW:
        return RCSwitchRawTransmitter.new(
            name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
            binary_code(config[CONF_CODE]), len(config[CONF_CODE]))
    if key == CONF_RC_SWITCH_TYPE_A:
        return RCSwitchTypeATransmitter.new(
            name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
            binary_code(config[CONF_GROUP]), binary_code(config[CONF_DEVICE]),
            config[CONF_STATE])
    if key == CONF_RC_SWITCH_TYPE_B:
        return RCSwitchTypeBTransmitter.new(
            name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
            config[CONF_ADDRESS], config[CONF_CHANNEL], config[CONF_STATE])
    if key == CONF_RC_SWITCH_TYPE_C:
        return RCSwitchTypeCTransmitter.new(
            name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
            ord(config[CONF_FAMILY][0]) - ord('a'), config[CONF_GROUP],
            config[CONF_DEVICE], config[CONF_STATE])
    if key == CONF_RC_SWITCH_TYPE_D:
        return RCSwitchTypeDTransmitter.new(
            name, build_rc_switch_protocol(config[CONF_PROTOCOL]),
            ord(config[CONF_GROUP][0]) - ord('a'), config[CONF_DEVICE],
            config[CONF_STATE])

    raise NotImplementedError("Unknown transmitter type {}".format(config))
コード例 #3
0
ファイル: font.py プロジェクト: liuse2ee/esphome-tools-dc1
def to_code(config):
    from PIL import ImageFont

    path = CORE.relative_path(config[CONF_FILE])
    try:
        font = ImageFont.truetype(path, config[CONF_SIZE])
    except Exception as e:
        raise core.EsphomeError(u"Could not load truetype file {}: {}".format(
            path, e))

    ascent, descent = font.getmetrics()

    glyph_args = {}
    data = []
    for glyph in config[CONF_GLYPHS]:
        mask = font.getmask(glyph, mode='1')
        _, (offset_x, offset_y) = font.font.getsize(glyph)
        width, height = mask.size
        width8 = ((width + 7) // 8) * 8
        glyph_data = [0 for _ in range(height * width8 // 8)]  # noqa: F812
        for y in range(height):
            for x in range(width):
                if not mask.getpixel((x, y)):
                    continue
                pos = x + y * width8
                glyph_data[pos // 8] |= 0x80 >> (pos % 8)
        glyph_args[glyph] = (len(data), offset_x, offset_y, width, height)
        data += glyph_data

    rhs = safe_exp([HexInt(x) for x in data])
    prog_arr = progmem_array(config[CONF_RAW_DATA_ID], rhs)

    glyphs = []
    for glyph in config[CONF_GLYPHS]:
        glyphs.append(Glyph(glyph, prog_arr, *glyph_args[glyph]))

    rhs = App.make_font(glyphs, ascent, ascent + descent)
    Pvariable(config[CONF_ID], rhs)