1
0
mirror of https://github.com/esphome/esphome.git synced 2026-03-22 03:06:48 +01:00

Merge branch 'dev' into bump-2023.12.0b1

This commit is contained in:
Jesse Hills 2023-12-14 08:30:45 +09:00
commit 5ab2c74519
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
347 changed files with 16521 additions and 4712 deletions

View File

@ -37,6 +37,7 @@
"!secret scalar",
"!lambda scalar",
"!extend scalar",
"!remove scalar",
"!include_dir_named scalar",
"!include_dir_list scalar",
"!include_dir_merge_list scalar",

97
.github/actions/build-image/action.yaml vendored Normal file
View File

@ -0,0 +1,97 @@
name: Build Image
inputs:
platform:
description: "Platform to build for"
required: true
example: "linux/amd64"
target:
description: "Target to build"
required: true
example: "docker"
baseimg:
description: "Base image type"
required: true
example: "docker"
suffix:
description: "Suffix to add to tags"
required: true
version:
description: "Version to build"
required: true
example: "2023.12.0"
runs:
using: "composite"
steps:
- name: Generate short tags
id: tags
shell: bash
run: |
output=$(docker/generate_tags.py \
--tag "${{ inputs.version }}" \
--suffix "${{ inputs.suffix }}")
echo $output
for l in $output; do
echo $l >> $GITHUB_OUTPUT
done
- name: Build and push to ghcr by digest
id: build-ghcr
uses: docker/build-push-action@v5.0.0
with:
context: .
file: ./docker/Dockerfile
platforms: ${{ inputs.platform }}
target: ${{ inputs.target }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BASEIMGTYPE=${{ inputs.baseimg }}
BUILD_VERSION=${{ inputs.version }}
outputs: |
type=image,name=ghcr.io/${{ steps.tags.outputs.image_name }},push-by-digest=true,name-canonical=true,push=true
- name: Export ghcr digests
shell: bash
run: |
mkdir -p /tmp/digests/${{ inputs.target }}/ghcr
digest="${{ steps.build-ghcr.outputs.digest }}"
touch "/tmp/digests/${{ inputs.target }}/ghcr/${digest#sha256:}"
- name: Upload ghcr digest
uses: actions/upload-artifact@v3.1.3
with:
name: digests-${{ inputs.target }}-ghcr
path: /tmp/digests/${{ inputs.target }}/ghcr/*
if-no-files-found: error
retention-days: 1
- name: Build and push to dockerhub by digest
id: build-dockerhub
uses: docker/build-push-action@v5.0.0
with:
context: .
file: ./docker/Dockerfile
platforms: ${{ inputs.platform }}
target: ${{ inputs.target }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BASEIMGTYPE=${{ inputs.baseimg }}
BUILD_VERSION=${{ inputs.version }}
outputs: |
type=image,name=docker.io/${{ steps.tags.outputs.image_name }},push-by-digest=true,name-canonical=true,push=true
- name: Export dockerhub digests
shell: bash
run: |
mkdir -p /tmp/digests/${{ inputs.target }}/dockerhub
digest="${{ steps.build-dockerhub.outputs.digest }}"
touch "/tmp/digests/${{ inputs.target }}/dockerhub/${digest#sha256:}"
- name: Upload dockerhub digest
uses: actions/upload-artifact@v3.1.3
with:
name: digests-${{ inputs.target }}-dockerhub
path: /tmp/digests/${{ inputs.target }}/dockerhub/*
if-no-files-found: error
retention-days: 1

View File

@ -17,7 +17,7 @@ runs:
steps:
- name: Set up Python ${{ inputs.python-version }}
id: python
uses: actions/setup-python@v4.7.0
uses: actions/setup-python@v5.0.0
with:
python-version: ${{ inputs.python-version }}
- name: Restore Python virtual environment

View File

@ -42,7 +42,7 @@ jobs:
steps:
- uses: actions/checkout@v4.1.1
- name: Set up Python
uses: actions/setup-python@v4.7.1
uses: actions/setup-python@v5.0.0
with:
python-version: "3.9"
- name: Set up Docker Buildx

View File

@ -40,7 +40,7 @@ jobs:
run: echo key="${{ hashFiles('requirements.txt', 'requirements_optional.txt', 'requirements_test.txt') }}" >> $GITHUB_OUTPUT
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
id: python
uses: actions/setup-python@v4.7.1
uses: actions/setup-python@v5.0.0
with:
python-version: ${{ env.DEFAULT_PYTHON }}
- name: Restore Python virtual environment

View File

@ -18,7 +18,7 @@ jobs:
lock:
runs-on: ubuntu-latest
steps:
- uses: dessant/lock-threads@v4.0.1
- uses: dessant/lock-threads@v5.0.1
with:
pr-inactive-days: "1"
pr-lock-reason: ""

View File

@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check for needs-docs label
uses: actions/github-script@v6.4.1
uses: actions/github-script@v7.0.1
with:
script: |
const { data: labels } = await github.rest.issues.listLabelsOnIssue({

View File

@ -45,7 +45,7 @@ jobs:
steps:
- uses: actions/checkout@v4.1.1
- name: Set up Python
uses: actions/setup-python@v4.7.1
uses: actions/setup-python@v5.0.0
with:
python-version: "3.x"
- name: Set up python environment
@ -63,40 +63,31 @@ jobs:
run: twine upload dist/*
deploy-docker:
name: Build and publish ESPHome ${{ matrix.image.title}}
name: Build ESPHome ${{ matrix.platform }}
if: github.repository == 'esphome/esphome'
permissions:
contents: read
packages: write
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.image.title == 'lint' }}
needs: [init]
strategy:
fail-fast: false
matrix:
image:
- title: "ha-addon"
suffix: "hassio"
target: "hassio"
baseimg: "hassio"
- title: "docker"
suffix: ""
target: "docker"
baseimg: "docker"
- title: "lint"
suffix: "lint"
target: "lint"
baseimg: "docker"
platform:
- linux/amd64
- linux/arm/v7
- linux/arm64
steps:
- uses: actions/checkout@v4.1.1
- name: Set up Python
uses: actions/setup-python@v4.7.1
uses: actions/setup-python@v5.0.0
with:
python-version: "3.9"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.0.0
- name: Set up QEMU
if: matrix.platform != 'linux/amd64'
uses: docker/setup-qemu-action@v3.0.0
- name: Log in to docker hub
@ -111,37 +102,108 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build docker
uses: ./.github/actions/build-image
with:
platform: ${{ matrix.platform }}
target: docker
baseimg: docker
suffix: ""
version: ${{ needs.init.outputs.tag }}
- name: Build ha-addon
uses: ./.github/actions/build-image
with:
platform: ${{ matrix.platform }}
target: hassio
baseimg: hassio
suffix: "hassio"
version: ${{ needs.init.outputs.tag }}
- name: Build lint
uses: ./.github/actions/build-image
with:
platform: ${{ matrix.platform }}
target: lint
baseimg: docker
suffix: lint
version: ${{ needs.init.outputs.tag }}
deploy-manifest:
name: Publish ESPHome ${{ matrix.image.title }} to ${{ matrix.registry }}
runs-on: ubuntu-latest
needs:
- init
- deploy-docker
if: github.repository == 'esphome/esphome'
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
image:
- title: "ha-addon"
target: "hassio"
suffix: "hassio"
- title: "docker"
target: "docker"
suffix: ""
- title: "lint"
target: "lint"
suffix: "lint"
registry:
- ghcr
- dockerhub
steps:
- uses: actions/checkout@v4.1.1
- name: Download digests
uses: actions/download-artifact@v3.0.2
with:
name: digests-${{ matrix.image.target }}-${{ matrix.registry }}
path: /tmp/digests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.0.0
- name: Log in to docker hub
if: matrix.registry == 'dockerhub'
uses: docker/login-action@v3.0.0
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Log in to the GitHub container registry
if: matrix.registry == 'ghcr'
uses: docker/login-action@v3.0.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate short tags
id: tags
run: |
docker/generate_tags.py \
output=$(docker/generate_tags.py \
--tag "${{ needs.init.outputs.tag }}" \
--suffix "${{ matrix.image.suffix }}"
--suffix "${{ matrix.image.suffix }}" \
--registry "${{ matrix.registry }}")
echo $output
for l in $output; do
echo $l >> $GITHUB_OUTPUT
done
- name: Build and push
uses: docker/build-push-action@v5.0.0
with:
context: .
file: ./docker/Dockerfile
platforms: linux/amd64,linux/arm/v7,linux/arm64
target: ${{ matrix.image.target }}
push: true
# yamllint disable rule:line-length
cache-from: type=registry,ref=ghcr.io/${{ steps.tags.outputs.image }}:cache-${{ steps.tags.outputs.channel }}
cache-to: type=registry,ref=ghcr.io/${{ steps.tags.outputs.image }}:cache-${{ steps.tags.outputs.channel }},mode=max
# yamllint enable rule:line-length
tags: ${{ steps.tags.outputs.tags }}
build-args: |
BASEIMGTYPE=${{ matrix.image.baseimg }}
BUILD_VERSION=${{ needs.init.outputs.tag }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -Rcnr 'inputs | . / "," | map("-t " + .) | join(" ")' <<< "${{ steps.tags.outputs.tags}}") \
$(printf '${{ steps.tags.outputs.image }}@sha256:%s ' *)
deploy-ha-addon-repo:
if: github.repository == 'esphome/esphome' && github.event_name == 'release'
runs-on: ubuntu-latest
needs: [deploy-docker]
needs: [deploy-manifest]
steps:
- name: Trigger Workflow
uses: actions/github-script@v6.4.1
uses: actions/github-script@v7.0.1
with:
github-token: ${{ secrets.DEPLOY_HA_ADDON_REPO_TOKEN }}
script: |

View File

@ -18,7 +18,7 @@ jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v8.0.0
- uses: actions/stale@v9.0.0
with:
days-before-pr-stale: 90
days-before-pr-close: 7
@ -38,7 +38,7 @@ jobs:
close-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v8.0.0
- uses: actions/stale@v9.0.0
with:
days-before-pr-stale: -1
days-before-pr-close: -1

View File

@ -22,7 +22,7 @@ jobs:
path: lib/home-assistant
- name: Setup Python
uses: actions/setup-python@v4.7.1
uses: actions/setup-python@v5.0.0
with:
python-version: 3.11

View File

@ -19,4 +19,4 @@ jobs:
- name: Check out code from GitHub
uses: actions/checkout@v4.1.1
- name: Run yamllint
uses: frenck/action-yamllint@v1.4.1
uses: frenck/action-yamllint@v1.4.2

View File

@ -3,7 +3,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.10.1
rev: 23.12.0
hooks:
- id: black
args:

View File

@ -12,6 +12,7 @@ esphome/core/* @esphome/core
# Integrations
esphome/components/a01nyub/* @MrSuicideParrot
esphome/components/a02yyuw/* @TH-Braemer
esphome/components/absolute_humidity/* @DAVe3283
esphome/components/ac_dimmer/* @glmnet
esphome/components/adc/* @esphome/core
@ -88,8 +89,9 @@ esphome/components/ds1307/* @badbadc0ffee
esphome/components/dsmr/* @glmnet @zuidwijk
esphome/components/duty_time/* @dudanov
esphome/components/ee895/* @Stock-M
esphome/components/ektf2232/* @jesserockz
esphome/components/ektf2232/touchscreen/* @jesserockz
esphome/components/emc2101/* @ellull
esphome/components/ens160/* @vincentscode
esphome/components/ens210/* @itn3rd77
esphome/components/esp32/* @esphome/core
esphome/components/esp32_ble/* @Rapsssito @jesserockz
@ -109,19 +111,24 @@ esphome/components/fastled_base/* @OttoWinter
esphome/components/feedback/* @ianchi
esphome/components/fingerprint_grow/* @OnFreund @loongyh
esphome/components/fs3000/* @kahrendt
esphome/components/ft5x06/* @clydebarrow
esphome/components/ft63x6/* @gpambrozio
esphome/components/gcja5/* @gcormier
esphome/components/globals/* @esphome/core
esphome/components/gp8403/* @jesserockz
esphome/components/gpio/* @esphome/core
esphome/components/gps/* @coogle
esphome/components/graph/* @synco
esphome/components/graphical_display_menu/* @MrMDavidson
esphome/components/gree/* @orestismers
esphome/components/grove_tb6612fng/* @max246
esphome/components/growatt_solar/* @leeuwte
esphome/components/gt911/* @clydebarrow @jesserockz
esphome/components/haier/* @paveldn
esphome/components/havells_solar/* @sourabhjaiswal
esphome/components/hbridge/fan/* @WeekendWarrior
esphome/components/hbridge/light/* @DotNetDann
esphome/components/he60r/* @clydebarrow
esphome/components/heatpumpir/* @rob-deutsch
esphome/components/hitachi_ac424/* @sourabhjaiswal
esphome/components/hm3301/* @freekode
@ -233,11 +240,17 @@ esphome/components/pmwcs3/* @SeByDocKy
esphome/components/pn532/* @OttoWinter @jesserockz
esphome/components/pn532_i2c/* @OttoWinter @jesserockz
esphome/components/pn532_spi/* @OttoWinter @jesserockz
esphome/components/pn7150/* @jesserockz @kbx81
esphome/components/pn7150_i2c/* @jesserockz @kbx81
esphome/components/pn7160/* @jesserockz @kbx81
esphome/components/pn7160_i2c/* @jesserockz @kbx81
esphome/components/pn7160_spi/* @jesserockz @kbx81
esphome/components/power_supply/* @esphome/core
esphome/components/preferences/* @esphome/core
esphome/components/psram/* @esphome/core
esphome/components/pulse_meter/* @TrentHouliston @cstaahl @stevebaxter
esphome/components/pvvx_mithermometer/* @pasiz
esphome/components/pylontech/* @functionpointer
esphome/components/qmp6988/* @andrewpc
esphome/components/qr_code/* @wjtje
esphome/components/qwiic_pir/* @kahrendt
@ -326,7 +339,7 @@ esphome/components/tmp1075/* @sybrenstuvel
esphome/components/tmp117/* @Azimath
esphome/components/tof10120/* @wstrzalka
esphome/components/toshiba/* @kbx81
esphome/components/touchscreen/* @jesserockz
esphome/components/touchscreen/* @jesserockz @nielsnl68
esphome/components/tsl2591/* @wjcarpenter
esphome/components/tt21100/* @kroimon
esphome/components/tuya/binary_sensor/* @jesserockz
@ -359,6 +372,6 @@ esphome/components/xiaomi_mhoc303/* @drug123
esphome/components/xiaomi_mhoc401/* @vevsvevs
esphome/components/xiaomi_rtcgq02lm/* @jesserockz
esphome/components/xl9535/* @mreditor97
esphome/components/xpt2046/* @nielsnl68 @numo68
esphome/components/xpt2046/touchscreen/* @nielsnl68 @numo68
esphome/components/zhlt01/* @cfeenstra1024
esphome/components/zio_ultrasonic/* @kahrendt

View File

@ -10,5 +10,3 @@ Things to note when contributing:
for more information.
- Please also update the tests in the `tests/` folder. You can do so by just adding a line in one of the YAML files
which checks if your new feature compiles correctly.
- Sometimes I will let pull requests linger because I'm not 100% sure about them. Please feel free to ping
me after some time.

View File

@ -1,13 +1,14 @@
#!/usr/bin/env python3
import re
import os
import argparse
import json
CHANNEL_DEV = "dev"
CHANNEL_BETA = "beta"
CHANNEL_RELEASE = "release"
GHCR = "ghcr"
DOCKERHUB = "dockerhub"
parser = argparse.ArgumentParser()
parser.add_argument(
"--tag",
@ -21,21 +22,31 @@ parser.add_argument(
required=True,
help="The suffix of the tag.",
)
parser.add_argument(
"--registry",
type=str,
choices=[GHCR, DOCKERHUB],
required=False,
action="append",
help="The registry to build tags for.",
)
def main():
args = parser.parse_args()
# detect channel from tag
match = re.match(r"^(\d+\.\d+)(?:\.\d+)?(b\d+)?$", args.tag)
match = re.match(r"^(\d+\.\d+)(?:\.\d+)(?:(b\d+)|(-dev\d+))?$", args.tag)
major_minor_version = None
if match is None:
if match is None: # eg 2023.12.0-dev20231109-testbranch
channel = None # Ran with custom tag for a branch etc
elif match.group(3) is not None: # eg 2023.12.0-dev20231109
channel = CHANNEL_DEV
elif match.group(2) is None:
elif match.group(2) is not None: # eg 2023.12.0b1
channel = CHANNEL_BETA
else: # eg 2023.12.0
major_minor_version = match.group(1)
channel = CHANNEL_RELEASE
else:
channel = CHANNEL_BETA
tags_to_push = [args.tag]
if channel == CHANNEL_DEV:
@ -53,15 +64,28 @@ def main():
suffix = f"-{args.suffix}" if args.suffix else ""
with open(os.environ["GITHUB_OUTPUT"], "w") as f:
print(f"channel={channel}", file=f)
print(f"image=esphome/esphome{suffix}", file=f)
full_tags = []
image_name = f"esphome/esphome{suffix}"
for tag in tags_to_push:
full_tags += [f"ghcr.io/esphome/esphome{suffix}:{tag}"]
full_tags += [f"esphome/esphome{suffix}:{tag}"]
print(f"tags={','.join(full_tags)}", file=f)
print(f"channel={channel}")
if args.registry is None:
args.registry = [GHCR, DOCKERHUB]
elif len(args.registry) == 1:
if GHCR in args.registry:
print(f"image=ghcr.io/{image_name}")
if DOCKERHUB in args.registry:
print(f"image=docker.io/{image_name}")
print(f"image_name={image_name}")
full_tags = []
for tag in tags_to_push:
if GHCR in args.registry:
full_tags += [f"ghcr.io/{image_name}:{tag}"]
if DOCKERHUB in args.registry:
full_tags += [f"docker.io/{image_name}:{tag}"]
print(f"tags={','.join(full_tags)}")
if __name__ == "__main__":

View File

@ -389,7 +389,8 @@ def command_config(args, config):
output = re.sub(
r"(password|key|psk|ssid)\: (.+)", r"\1: \\033[5m\2\\033[6m", output
)
safe_print(output)
if not CORE.quiet:
safe_print(output)
_LOGGER.info("Configuration is valid!")
return 0
@ -514,7 +515,7 @@ def command_clean(args, config):
def command_dashboard(args):
from esphome.dashboard import dashboard
return dashboard.start_web_server(args)
return dashboard.start_dashboard(args)
def command_update_all(args):

View File

@ -8,50 +8,37 @@ namespace esphome {
namespace a01nyub {
static const char *const TAG = "a01nyub.sensor";
static const uint8_t MAX_DATA_LENGTH_BYTES = 4;
void A01nyubComponent::loop() {
uint8_t data;
while (this->available() > 0) {
if (this->read_byte(&data)) {
buffer_.push_back(data);
this->read_byte(&data);
if (this->buffer_.empty() && (data != 0xff))
continue;
buffer_.push_back(data);
if (this->buffer_.size() == 4)
this->check_buffer_();
}
}
}
void A01nyubComponent::check_buffer_() {
if (this->buffer_.size() >= MAX_DATA_LENGTH_BYTES) {
size_t i;
for (i = 0; i < this->buffer_.size(); i++) {
// Look for the first packet
if (this->buffer_[i] == 0xFF) {
if (i + 1 + 3 < this->buffer_.size()) { // Packet is not complete
return; // Wait for completion
}
uint8_t checksum = (this->buffer_[i] + this->buffer_[i + 1] + this->buffer_[i + 2]) & 0xFF;
if (this->buffer_[i + 3] == checksum) {
float distance = (this->buffer_[i + 1] << 8) + this->buffer_[i + 2];
if (distance > 280) {
float meters = distance / 1000.0;
ESP_LOGV(TAG, "Distance from sensor: %f mm, %f m", distance, meters);
this->publish_state(meters);
} else {
ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
}
}
break;
}
uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
if (this->buffer_[3] == checksum) {
float distance = (this->buffer_[1] << 8) + this->buffer_[2];
if (distance > 280) {
float meters = distance / 1000.0;
ESP_LOGV(TAG, "Distance from sensor: %f mm, %f m", distance, meters);
this->publish_state(meters);
} else {
ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
}
this->buffer_.clear();
} else {
ESP_LOGW(TAG, "checksum failed: %02x != %02x", checksum, this->buffer_[3]);
}
this->buffer_.clear();
}
void A01nyubComponent::dump_config() {
ESP_LOGCONFIG(TAG, "A01nyub Sensor:");
LOG_SENSOR(" ", "Distance", this);
}
void A01nyubComponent::dump_config() { LOG_SENSOR("", "A01nyub Sensor", this); }
} // namespace a01nyub
} // namespace esphome

View File

@ -0,0 +1 @@
CODEOWNERS = ["@TH-Braemer"]

View File

@ -0,0 +1,43 @@
// Datasheet https://wiki.dfrobot.com/_A02YYUW_Waterproof_Ultrasonic_Sensor_SKU_SEN0311
#include "a02yyuw.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
namespace a02yyuw {
static const char *const TAG = "a02yyuw.sensor";
void A02yyuwComponent::loop() {
uint8_t data;
while (this->available() > 0) {
this->read_byte(&data);
if (this->buffer_.empty() && (data != 0xff))
continue;
buffer_.push_back(data);
if (this->buffer_.size() == 4)
this->check_buffer_();
}
}
void A02yyuwComponent::check_buffer_() {
uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
if (this->buffer_[3] == checksum) {
float distance = (this->buffer_[1] << 8) + this->buffer_[2];
if (distance > 30) {
ESP_LOGV(TAG, "Distance from sensor: %f mm", distance);
this->publish_state(distance);
} else {
ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
}
} else {
ESP_LOGW(TAG, "checksum failed: %02x != %02x", checksum, this->buffer_[3]);
}
this->buffer_.clear();
}
void A02yyuwComponent::dump_config() { LOG_SENSOR("", "A02yyuw Sensor", this); }
} // namespace a02yyuw
} // namespace esphome

View File

@ -0,0 +1,27 @@
#pragma once
#include <vector>
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace a02yyuw {
class A02yyuwComponent : public sensor::Sensor, public Component, public uart::UARTDevice {
public:
// Nothing really public.
// ========== INTERNAL METHODS ==========
void loop() override;
void dump_config() override;
protected:
void check_buffer_();
std::vector<uint8_t> buffer_;
};
} // namespace a02yyuw
} // namespace esphome

View File

@ -0,0 +1,41 @@
import esphome.codegen as cg
from esphome.components import sensor, uart
from esphome.const import (
STATE_CLASS_MEASUREMENT,
ICON_ARROW_EXPAND_VERTICAL,
DEVICE_CLASS_DISTANCE,
)
CODEOWNERS = ["@TH-Braemer"]
DEPENDENCIES = ["uart"]
UNIT_MILLIMETERS = "mm"
a02yyuw_ns = cg.esphome_ns.namespace("a02yyuw")
A02yyuwComponent = a02yyuw_ns.class_(
"A02yyuwComponent", sensor.Sensor, cg.Component, uart.UARTDevice
)
CONFIG_SCHEMA = sensor.sensor_schema(
A02yyuwComponent,
unit_of_measurement=UNIT_MILLIMETERS,
icon=ICON_ARROW_EXPAND_VERTICAL,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_DISTANCE,
).extend(uart.UART_DEVICE_SCHEMA)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
"a02yyuw",
baud_rate=9600,
require_tx=False,
require_rx=True,
data_bits=8,
parity=None,
stop_bits=1,
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.const import CONF_ANALOG, CONF_INPUT
from esphome.const import CONF_ANALOG, CONF_INPUT, CONF_NUMBER
from esphome.core import CORE
from esphome.components.esp32 import get_esp32_variant
@ -152,7 +152,8 @@ def validate_adc_pin(value):
return cv.only_on_rp2040("TEMPERATURE")
if CORE.is_esp32:
value = pins.internal_gpio_input_pin_number(value)
conf = pins.internal_gpio_input_pin_schema(value)
value = conf[CONF_NUMBER]
variant = get_esp32_variant()
if (
variant not in ESP32_VARIANT_ADC1_PIN_TO_CHANNEL
@ -166,24 +167,23 @@ def validate_adc_pin(value):
):
raise cv.Invalid(f"{variant} doesn't support ADC on this pin")
return pins.internal_gpio_input_pin_schema(value)
return conf
if CORE.is_esp8266:
value = pins.internal_gpio_pin_number({CONF_ANALOG: True, CONF_INPUT: True})(
value
)
if value != 17: # A0
raise cv.Invalid("ESP8266: Only pin A0 (GPIO17) supports ADC")
return pins.gpio_pin_schema(
conf = pins.gpio_pin_schema(
{CONF_ANALOG: True, CONF_INPUT: True}, internal=True
)(value)
if conf[CONF_NUMBER] != 17: # A0
raise cv.Invalid("ESP8266: Only pin A0 (GPIO17) supports ADC")
return conf
if CORE.is_rp2040:
value = pins.internal_gpio_input_pin_number(value)
if value not in (26, 27, 28, 29):
conf = pins.internal_gpio_input_pin_schema(value)
number = conf[CONF_NUMBER]
if number not in (26, 27, 28, 29):
raise cv.Invalid("RP2040: Only pins 26, 27, 28 and 29 support ADC")
return pins.internal_gpio_input_pin_schema(value)
return conf
if CORE.is_libretiny:
return pins.gpio_pin_schema(

View File

@ -10,7 +10,7 @@
namespace esphome {
namespace addressable_light {
class AddressableLightDisplay : public display::DisplayBuffer, public PollingComponent {
class AddressableLightDisplay : public display::DisplayBuffer {
public:
light::AddressableLight *get_light() const { return this->light_; }

View File

@ -45,7 +45,6 @@ async def to_code(config):
cg.add(var.set_height(config[CONF_HEIGHT]))
cg.add(var.set_light(wrapped_light))
await cg.register_component(var, config)
await display.register_display(var, config)
if pixel_mapper := config.get(CONF_PIXEL_MAPPER):

View File

@ -21,36 +21,49 @@ namespace esphome {
namespace aht10 {
static const char *const TAG = "aht10";
static const uint8_t AHT10_CALIBRATE_CMD[] = {0xE1};
static const size_t SIZE_CALIBRATE_CMD = 3;
static const uint8_t AHT10_CALIBRATE_CMD[] = {0xE1, 0x08, 0x00};
static const uint8_t AHT20_CALIBRATE_CMD[] = {0xBE, 0x08, 0x00};
static const uint8_t AHT10_MEASURE_CMD[] = {0xAC, 0x33, 0x00};
static const uint8_t AHT10_DEFAULT_DELAY = 5; // ms, for calibration and temperature measurement
static const uint8_t AHT10_HUMIDITY_DELAY = 30; // ms
static const uint8_t AHT10_ATTEMPTS = 3; // safety margin, normally 3 attempts are enough: 3*30=90ms
static const uint8_t AHT10_CAL_ATTEMPTS = 10;
static const uint8_t AHT10_STATUS_BUSY = 0x80;
void AHT10Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up AHT10...");
const uint8_t *calibrate_cmd;
switch (this->variant_) {
case AHT10Variant::AHT20:
calibrate_cmd = AHT20_CALIBRATE_CMD;
ESP_LOGCONFIG(TAG, "Setting up AHT20");
break;
case AHT10Variant::AHT10:
default:
calibrate_cmd = AHT10_CALIBRATE_CMD;
ESP_LOGCONFIG(TAG, "Setting up AHT10");
}
if (!this->write_bytes(0, AHT10_CALIBRATE_CMD, sizeof(AHT10_CALIBRATE_CMD))) {
if (this->write(calibrate_cmd, SIZE_CALIBRATE_CMD) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Communication with AHT10 failed!");
this->mark_failed();
return;
}
uint8_t data = 0;
if (this->write(&data, 1) != i2c::ERROR_OK) {
ESP_LOGD(TAG, "Communication with AHT10 failed!");
this->mark_failed();
return;
}
delay(AHT10_DEFAULT_DELAY);
if (this->read(&data, 1) != i2c::ERROR_OK) {
ESP_LOGD(TAG, "Communication with AHT10 failed!");
this->mark_failed();
return;
}
if (this->read(&data, 1) != i2c::ERROR_OK) {
ESP_LOGD(TAG, "Communication with AHT10 failed!");
this->mark_failed();
return;
uint8_t data = AHT10_STATUS_BUSY;
int cal_attempts = 0;
while (data & AHT10_STATUS_BUSY) {
delay(AHT10_DEFAULT_DELAY);
if (this->read(&data, 1) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Communication with AHT10 failed!");
this->mark_failed();
return;
}
++cal_attempts;
if (cal_attempts > AHT10_CAL_ATTEMPTS) {
ESP_LOGE(TAG, "AHT10 calibration timed out!");
this->mark_failed();
return;
}
}
if ((data & 0x68) != 0x08) { // Bit[6:5] = 0b00, NORMAL mode and Bit[3] = 0b1, CALIBRATED
ESP_LOGE(TAG, "AHT10 calibration failed!");
@ -62,7 +75,7 @@ void AHT10Component::setup() {
}
void AHT10Component::update() {
if (!this->write_bytes(0, AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD))) {
if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Communication with AHT10 failed!");
this->status_set_warning();
return;
@ -89,7 +102,7 @@ void AHT10Component::update() {
break;
} else {
ESP_LOGD(TAG, "ATH10 Unrealistic humidity (0x0), retrying...");
if (!this->write_bytes(0, AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD))) {
if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Communication with AHT10 failed!");
this->status_set_warning();
return;

View File

@ -1,5 +1,7 @@
#pragma once
#include <utility>
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
@ -7,12 +9,15 @@
namespace esphome {
namespace aht10 {
enum AHT10Variant { AHT10, AHT20 };
class AHT10Component : public PollingComponent, public i2c::I2CDevice {
public:
void setup() override;
void update() override;
void dump_config() override;
float get_setup_priority() const override;
void set_variant(AHT10Variant variant) { this->variant_ = variant; }
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
void set_humidity_sensor(sensor::Sensor *humidity_sensor) { humidity_sensor_ = humidity_sensor; }
@ -20,6 +25,7 @@ class AHT10Component : public PollingComponent, public i2c::I2CDevice {
protected:
sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *humidity_sensor_{nullptr};
AHT10Variant variant_{};
};
} // namespace aht10

View File

@ -10,6 +10,7 @@ from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_PERCENT,
CONF_VARIANT,
)
DEPENDENCIES = ["i2c"]
@ -17,6 +18,12 @@ DEPENDENCIES = ["i2c"]
aht10_ns = cg.esphome_ns.namespace("aht10")
AHT10Component = aht10_ns.class_("AHT10Component", cg.PollingComponent, i2c.I2CDevice)
AHT10Variant = aht10_ns.enum("AHT10Variant")
AHT10_VARIANTS = {
"AHT10": AHT10Variant.AHT10,
"AHT20": AHT10Variant.AHT20,
}
CONFIG_SCHEMA = (
cv.Schema(
{
@ -33,6 +40,9 @@ CONFIG_SCHEMA = (
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_VARIANT, default="AHT10"): cv.enum(
AHT10_VARIANTS, upper=True
),
}
)
.extend(cv.polling_component_schema("60s"))
@ -44,6 +54,7 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
cg.add(var.set_variant(config[CONF_VARIANT]))
if temperature := config.get(CONF_TEMPERATURE):
sens = await sensor.new_sensor(temperature)

View File

@ -365,6 +365,7 @@ message ListEntitiesFanResponse {
bool disabled_by_default = 9;
string icon = 10;
EntityCategory entity_category = 11;
repeated string supported_preset_modes = 12;
}
enum FanSpeed {
FAN_SPEED_LOW = 0;
@ -387,6 +388,7 @@ message FanStateResponse {
FanSpeed speed = 4 [deprecated = true];
FanDirection direction = 5;
int32 speed_level = 6;
string preset_mode = 7;
}
message FanCommandRequest {
option (id) = 31;
@ -405,6 +407,8 @@ message FanCommandRequest {
FanDirection direction = 9;
bool has_speed_level = 10;
int32 speed_level = 11;
bool has_preset_mode = 12;
string preset_mode = 13;
}
// ==================== LIGHT ====================
@ -855,6 +859,10 @@ message ListEntitiesClimateResponse {
string icon = 19;
EntityCategory entity_category = 20;
float visual_current_temperature_step = 21;
bool supports_current_humidity = 22;
bool supports_target_humidity = 23;
float visual_min_humidity = 24;
float visual_max_humidity = 25;
}
message ClimateStateResponse {
option (id) = 47;
@ -875,6 +883,8 @@ message ClimateStateResponse {
string custom_fan_mode = 11;
ClimatePreset preset = 12;
string custom_preset = 13;
float current_humidity = 14;
float target_humidity = 15;
}
message ClimateCommandRequest {
option (id) = 48;
@ -903,6 +913,8 @@ message ClimateCommandRequest {
ClimatePreset preset = 19;
bool has_custom_preset = 20;
string custom_preset = 21;
bool has_target_humidity = 22;
float target_humidity = 23;
}
// ==================== NUMBER ====================

View File

@ -293,6 +293,8 @@ bool APIConnection::send_fan_state(fan::Fan *fan) {
}
if (traits.supports_direction())
resp.direction = static_cast<enums::FanDirection>(fan->direction);
if (traits.supports_preset_modes())
resp.preset_mode = fan->preset_mode;
return this->send_fan_state_response(resp);
}
bool APIConnection::send_fan_info(fan::Fan *fan) {
@ -307,6 +309,8 @@ bool APIConnection::send_fan_info(fan::Fan *fan) {
msg.supports_speed = traits.supports_speed();
msg.supports_direction = traits.supports_direction();
msg.supported_speed_count = traits.supported_speed_count();
for (auto const &preset : traits.supported_preset_modes())
msg.supported_preset_modes.push_back(preset);
msg.disabled_by_default = fan->is_disabled_by_default();
msg.icon = fan->get_icon();
msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
@ -328,6 +332,8 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
}
if (msg.has_direction)
call.set_direction(static_cast<fan::FanDirection>(msg.direction));
if (msg.has_preset_mode)
call.set_preset_mode(msg.preset_mode);
call.perform();
}
#endif
@ -554,6 +560,10 @@ bool APIConnection::send_climate_state(climate::Climate *climate) {
resp.custom_preset = climate->custom_preset.value();
if (traits.get_supports_swing_modes())
resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
if (traits.get_supports_current_humidity())
resp.current_humidity = climate->current_humidity;
if (traits.get_supports_target_humidity())
resp.target_humidity = climate->target_humidity;
return this->send_climate_state_response(resp);
}
bool APIConnection::send_climate_info(climate::Climate *climate) {
@ -570,7 +580,9 @@ bool APIConnection::send_climate_info(climate::Climate *climate) {
msg.entity_category = static_cast<enums::EntityCategory>(climate->get_entity_category());
msg.supports_current_temperature = traits.get_supports_current_temperature();
msg.supports_current_humidity = traits.get_supports_current_humidity();
msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
msg.supports_target_humidity = traits.get_supports_target_humidity();
for (auto mode : traits.get_supported_modes())
msg.supported_modes.push_back(static_cast<enums::ClimateMode>(mode));
@ -579,6 +591,8 @@ bool APIConnection::send_climate_info(climate::Climate *climate) {
msg.visual_max_temperature = traits.get_visual_max_temperature();
msg.visual_target_temperature_step = traits.get_visual_target_temperature_step();
msg.visual_current_temperature_step = traits.get_visual_current_temperature_step();
msg.visual_min_humidity = traits.get_visual_min_humidity();
msg.visual_max_humidity = traits.get_visual_max_humidity();
msg.legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY);
msg.supports_action = traits.get_supports_action();
@ -609,6 +623,8 @@ void APIConnection::climate_command(const ClimateCommandRequest &msg) {
call.set_target_temperature_low(msg.target_temperature_low);
if (msg.has_target_temperature_high)
call.set_target_temperature_high(msg.target_temperature_high);
if (msg.has_target_humidity)
call.set_target_humidity(msg.target_humidity);
if (msg.has_fan_mode)
call.set_fan_mode(static_cast<climate::ClimateFanMode>(msg.fan_mode));
if (msg.has_custom_fan_mode)

View File

@ -1375,6 +1375,10 @@ bool ListEntitiesFanResponse::decode_length(uint32_t field_id, ProtoLengthDelimi
this->icon = value.as_string();
return true;
}
case 12: {
this->supported_preset_modes.push_back(value.as_string());
return true;
}
default:
return false;
}
@ -1401,6 +1405,9 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_bool(9, this->disabled_by_default);
buffer.encode_string(10, this->icon);
buffer.encode_enum<enums::EntityCategory>(11, this->entity_category);
for (auto &it : this->supported_preset_modes) {
buffer.encode_string(12, it, true);
}
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesFanResponse::dump_to(std::string &out) const {
@ -1451,6 +1458,12 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const {
out.append(" entity_category: ");
out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
out.append("\n");
for (const auto &it : this->supported_preset_modes) {
out.append(" supported_preset_modes: ");
out.append("'").append(it).append("'");
out.append("\n");
}
out.append("}");
}
#endif
@ -1480,6 +1493,16 @@ bool FanStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
return false;
}
}
bool FanStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
switch (field_id) {
case 7: {
this->preset_mode = value.as_string();
return true;
}
default:
return false;
}
}
bool FanStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
switch (field_id) {
case 1: {
@ -1497,6 +1520,7 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::FanSpeed>(4, this->speed);
buffer.encode_enum<enums::FanDirection>(5, this->direction);
buffer.encode_int32(6, this->speed_level);
buffer.encode_string(7, this->preset_mode);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void FanStateResponse::dump_to(std::string &out) const {
@ -1527,6 +1551,10 @@ void FanStateResponse::dump_to(std::string &out) const {
sprintf(buffer, "%" PRId32, this->speed_level);
out.append(buffer);
out.append("\n");
out.append(" preset_mode: ");
out.append("'").append(this->preset_mode).append("'");
out.append("\n");
out.append("}");
}
#endif
@ -1572,6 +1600,20 @@ bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
this->speed_level = value.as_int32();
return true;
}
case 12: {
this->has_preset_mode = value.as_bool();
return true;
}
default:
return false;
}
}
bool FanCommandRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
switch (field_id) {
case 13: {
this->preset_mode = value.as_string();
return true;
}
default:
return false;
}
@ -1598,6 +1640,8 @@ void FanCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::FanDirection>(9, this->direction);
buffer.encode_bool(10, this->has_speed_level);
buffer.encode_int32(11, this->speed_level);
buffer.encode_bool(12, this->has_preset_mode);
buffer.encode_string(13, this->preset_mode);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void FanCommandRequest::dump_to(std::string &out) const {
@ -1648,6 +1692,14 @@ void FanCommandRequest::dump_to(std::string &out) const {
sprintf(buffer, "%" PRId32, this->speed_level);
out.append(buffer);
out.append("\n");
out.append(" has_preset_mode: ");
out.append(YESNO(this->has_preset_mode));
out.append("\n");
out.append(" preset_mode: ");
out.append("'").append(this->preset_mode).append("'");
out.append("\n");
out.append("}");
}
#endif
@ -3559,6 +3611,14 @@ bool ListEntitiesClimateResponse::decode_varint(uint32_t field_id, ProtoVarInt v
this->entity_category = value.as_enum<enums::EntityCategory>();
return true;
}
case 22: {
this->supports_current_humidity = value.as_bool();
return true;
}
case 23: {
this->supports_target_humidity = value.as_bool();
return true;
}
default:
return false;
}
@ -3615,6 +3675,14 @@ bool ListEntitiesClimateResponse::decode_32bit(uint32_t field_id, Proto32Bit val
this->visual_current_temperature_step = value.as_float();
return true;
}
case 24: {
this->visual_min_humidity = value.as_float();
return true;
}
case 25: {
this->visual_max_humidity = value.as_float();
return true;
}
default:
return false;
}
@ -3653,6 +3721,10 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_string(19, this->icon);
buffer.encode_enum<enums::EntityCategory>(20, this->entity_category);
buffer.encode_float(21, this->visual_current_temperature_step);
buffer.encode_bool(22, this->supports_current_humidity);
buffer.encode_bool(23, this->supports_target_humidity);
buffer.encode_float(24, this->visual_min_humidity);
buffer.encode_float(25, this->visual_max_humidity);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ListEntitiesClimateResponse::dump_to(std::string &out) const {
@ -3758,7 +3830,24 @@ void ListEntitiesClimateResponse::dump_to(std::string &out) const {
sprintf(buffer, "%g", this->visual_current_temperature_step);
out.append(buffer);
out.append("\n");
out.append("}");
out.append(" supports_current_humidity: ");
out.append(YESNO(this->supports_current_humidity));
out.append("\n");
out.append(" supports_target_humidity: ");
out.append(YESNO(this->supports_target_humidity));
out.append("\n");
out.append(" visual_min_humidity: ");
sprintf(buffer, "%g", this->visual_min_humidity);
out.append(buffer);
out.append("\n");
out.append(" visual_max_humidity: ");
sprintf(buffer, "%g", this->visual_max_humidity);
out.append(buffer);
out.append("\n");
}
#endif
bool ClimateStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
@ -3827,6 +3916,14 @@ bool ClimateStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
this->target_temperature_high = value.as_float();
return true;
}
case 14: {
this->current_humidity = value.as_float();
return true;
}
case 15: {
this->target_humidity = value.as_float();
return true;
}
default:
return false;
}
@ -3845,6 +3942,8 @@ void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_string(11, this->custom_fan_mode);
buffer.encode_enum<enums::ClimatePreset>(12, this->preset);
buffer.encode_string(13, this->custom_preset);
buffer.encode_float(14, this->current_humidity);
buffer.encode_float(15, this->target_humidity);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ClimateStateResponse::dump_to(std::string &out) const {
@ -3906,7 +4005,16 @@ void ClimateStateResponse::dump_to(std::string &out) const {
out.append(" custom_preset: ");
out.append("'").append(this->custom_preset).append("'");
out.append("\n");
out.append("}");
out.append(" current_humidity: ");
sprintf(buffer, "%g", this->current_humidity);
out.append(buffer);
out.append("\n");
out.append(" target_humidity: ");
sprintf(buffer, "%g", this->target_humidity);
out.append(buffer);
out.append("\n");
}
#endif
bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
@ -3971,6 +4079,10 @@ bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value)
this->has_custom_preset = value.as_bool();
return true;
}
case 22: {
this->has_target_humidity = value.as_bool();
return true;
}
default:
return false;
}
@ -4007,6 +4119,10 @@ bool ClimateCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) {
this->target_temperature_high = value.as_float();
return true;
}
case 23: {
this->target_humidity = value.as_float();
return true;
}
default:
return false;
}
@ -4033,6 +4149,8 @@ void ClimateCommandRequest::encode(ProtoWriteBuffer buffer) const {
buffer.encode_enum<enums::ClimatePreset>(19, this->preset);
buffer.encode_bool(20, this->has_custom_preset);
buffer.encode_string(21, this->custom_preset);
buffer.encode_bool(22, this->has_target_humidity);
buffer.encode_float(23, this->target_humidity);
}
#ifdef HAS_PROTO_MESSAGE_DUMP
void ClimateCommandRequest::dump_to(std::string &out) const {
@ -4125,6 +4243,15 @@ void ClimateCommandRequest::dump_to(std::string &out) const {
out.append(" custom_preset: ");
out.append("'").append(this->custom_preset).append("'");
out.append("\n");
out.append(" has_target_humidity: ");
out.append(YESNO(this->has_target_humidity));
out.append("\n");
out.append(" target_humidity: ");
sprintf(buffer, "%g", this->target_humidity);
out.append(buffer);
out.append("\n");
out.append("}");
}
#endif

View File

@ -472,6 +472,7 @@ class ListEntitiesFanResponse : public ProtoMessage {
bool disabled_by_default{false};
std::string icon{};
enums::EntityCategory entity_category{};
std::vector<std::string> supported_preset_modes{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -490,6 +491,7 @@ class FanStateResponse : public ProtoMessage {
enums::FanSpeed speed{};
enums::FanDirection direction{};
int32_t speed_level{0};
std::string preset_mode{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -497,6 +499,7 @@ class FanStateResponse : public ProtoMessage {
protected:
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
};
class FanCommandRequest : public ProtoMessage {
@ -512,6 +515,8 @@ class FanCommandRequest : public ProtoMessage {
enums::FanDirection direction{};
bool has_speed_level{false};
int32_t speed_level{0};
bool has_preset_mode{false};
std::string preset_mode{};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -519,6 +524,7 @@ class FanCommandRequest : public ProtoMessage {
protected:
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
};
class ListEntitiesLightResponse : public ProtoMessage {
@ -979,6 +985,10 @@ class ListEntitiesClimateResponse : public ProtoMessage {
std::string icon{};
enums::EntityCategory entity_category{};
float visual_current_temperature_step{0.0f};
bool supports_current_humidity{false};
bool supports_target_humidity{false};
float visual_min_humidity{0.0f};
float visual_max_humidity{0.0f};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -1004,6 +1014,8 @@ class ClimateStateResponse : public ProtoMessage {
std::string custom_fan_mode{};
enums::ClimatePreset preset{};
std::string custom_preset{};
float current_humidity{0.0f};
float target_humidity{0.0f};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
@ -1037,6 +1049,8 @@ class ClimateCommandRequest : public ProtoMessage {
enums::ClimatePreset preset{};
bool has_custom_preset{false};
std::string custom_preset{};
bool has_target_humidity{false};
float target_humidity{0.0f};
void encode(ProtoWriteBuffer buffer) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;

View File

@ -8,7 +8,6 @@ from typing import Any
from aioesphomeapi import APIClient
from aioesphomeapi.api_pb2 import SubscribeLogsResponse
from aioesphomeapi.log_runner import async_run
from zeroconf.asyncio import AsyncZeroconf
from esphome.const import CONF_KEY, CONF_PASSWORD, CONF_PORT, __version__
from esphome.core import CORE
@ -18,24 +17,22 @@ from . import CONF_ENCRYPTION
_LOGGER = logging.getLogger(__name__)
async def async_run_logs(config, address):
async def async_run_logs(config: dict[str, Any], address: str) -> None:
"""Run the logs command in the event loop."""
conf = config["api"]
name = config["esphome"]["name"]
port: int = int(conf[CONF_PORT])
password: str = conf[CONF_PASSWORD]
noise_psk: str | None = None
if CONF_ENCRYPTION in conf:
noise_psk = conf[CONF_ENCRYPTION][CONF_KEY]
_LOGGER.info("Starting log output from %s using esphome API", address)
aiozc = AsyncZeroconf()
cli = APIClient(
address,
port,
password,
client_info=f"ESPHome Logs {__version__}",
noise_psk=noise_psk,
zeroconf_instance=aiozc.zeroconf,
)
dashboard = CORE.dashboard
@ -48,12 +45,10 @@ async def async_run_logs(config, address):
text = text.replace("\033", "\\033")
print(f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}]{text}")
stop = await async_run(cli, on_log, aio_zeroconf_instance=aiozc)
stop = await async_run(cli, on_log, name=name)
try:
while True:
await asyncio.sleep(60)
await asyncio.Event().wait()
finally:
await aiozc.async_close()
await stop()

View File

@ -15,6 +15,16 @@ void BangBangClimate::setup() {
this->publish_state();
});
this->current_temperature = this->sensor_->state;
// register for humidity values and get initial state
if (this->humidity_sensor_ != nullptr) {
this->humidity_sensor_->add_on_state_callback([this](float state) {
this->current_humidity = state;
this->publish_state();
});
this->current_humidity = this->humidity_sensor_->state;
}
// restore set points
auto restore = this->restore_state_();
if (restore.has_value()) {
@ -47,6 +57,8 @@ void BangBangClimate::control(const climate::ClimateCall &call) {
climate::ClimateTraits BangBangClimate::traits() {
auto traits = climate::ClimateTraits();
traits.set_supports_current_temperature(true);
if (this->humidity_sensor_ != nullptr)
traits.set_supports_current_humidity(true);
traits.set_supported_modes({
climate::CLIMATE_MODE_OFF,
});
@ -171,6 +183,7 @@ void BangBangClimate::set_away_config(const BangBangClimateTargetTempConfig &awa
BangBangClimate::BangBangClimate()
: idle_trigger_(new Trigger<>()), cool_trigger_(new Trigger<>()), heat_trigger_(new Trigger<>()) {}
void BangBangClimate::set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
Trigger<> *BangBangClimate::get_idle_trigger() const { return this->idle_trigger_; }
Trigger<> *BangBangClimate::get_cool_trigger() const { return this->cool_trigger_; }
void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }

View File

@ -24,6 +24,7 @@ class BangBangClimate : public climate::Climate, public Component {
void dump_config() override;
void set_sensor(sensor::Sensor *sensor);
void set_humidity_sensor(sensor::Sensor *humidity_sensor);
Trigger<> *get_idle_trigger() const;
Trigger<> *get_cool_trigger() const;
void set_supports_cool(bool supports_cool);
@ -48,6 +49,9 @@ class BangBangClimate : public climate::Climate, public Component {
/// The sensor used for getting the current temperature
sensor::Sensor *sensor_{nullptr};
/// The sensor used for getting the current humidity
sensor::Sensor *humidity_sensor_{nullptr};
/** The trigger to call when the controller should switch to idle mode.
*
* In idle mode, the controller is assumed to have both heating and cooling disabled.

View File

@ -8,6 +8,7 @@ from esphome.const import (
CONF_DEFAULT_TARGET_TEMPERATURE_HIGH,
CONF_DEFAULT_TARGET_TEMPERATURE_LOW,
CONF_HEAT_ACTION,
CONF_HUMIDITY_SENSOR,
CONF_ID,
CONF_IDLE_ACTION,
CONF_SENSOR,
@ -22,6 +23,7 @@ CONFIG_SCHEMA = cv.All(
{
cv.GenerateID(): cv.declare_id(BangBangClimate),
cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
cv.Optional(CONF_HUMIDITY_SENSOR): cv.use_id(sensor.Sensor),
cv.Required(CONF_DEFAULT_TARGET_TEMPERATURE_LOW): cv.temperature,
cv.Required(CONF_DEFAULT_TARGET_TEMPERATURE_HIGH): cv.temperature,
cv.Required(CONF_IDLE_ACTION): automation.validate_automation(single=True),
@ -47,6 +49,10 @@ async def to_code(config):
sens = await cg.get_variable(config[CONF_SENSOR])
cg.add(var.set_sensor(sens))
if CONF_HUMIDITY_SENSOR in config:
sens = await cg.get_variable(config[CONF_HUMIDITY_SENSOR])
cg.add(var.set_humidity_sensor(sens))
normal_config = BangBangClimateTargetTempConfig(
config[CONF_DEFAULT_TARGET_TEMPERATURE_LOW],
config[CONF_DEFAULT_TARGET_TEMPERATURE_HIGH],

View File

@ -90,40 +90,41 @@ void BP1658CJ::set_channel_value_(uint8_t channel, uint16_t value) {
void BP1658CJ::write_bit_(bool value) {
this->data_pin_->digital_write(value);
this->clock_pin_->digital_write(true);
delayMicroseconds(BP1658CJ_DELAY);
this->clock_pin_->digital_write(true);
delayMicroseconds(BP1658CJ_DELAY);
this->clock_pin_->digital_write(false);
delayMicroseconds(BP1658CJ_DELAY);
}
void BP1658CJ::write_byte_(uint8_t data) {
for (uint8_t mask = 0x80; mask; mask >>= 1) {
this->write_bit_(data & mask);
delayMicroseconds(BP1658CJ_DELAY);
}
// ack bit
this->data_pin_->pin_mode(gpio::FLAG_INPUT);
this->clock_pin_->digital_write(true);
delayMicroseconds(BP1658CJ_DELAY);
this->clock_pin_->digital_write(false);
delayMicroseconds(BP1658CJ_DELAY);
this->data_pin_->pin_mode(gpio::FLAG_OUTPUT);
}
void BP1658CJ::write_buffer_(uint8_t *buffer, uint8_t size) {
this->data_pin_->digital_write(false);
delayMicroseconds(BP1658CJ_DELAY);
this->clock_pin_->digital_write(false);
delayMicroseconds(BP1658CJ_DELAY);
for (uint32_t i = 0; i < size; i++) {
this->write_byte_(buffer[i]);
delayMicroseconds(BP1658CJ_DELAY);
}
this->clock_pin_->digital_write(true);
delayMicroseconds(BP1658CJ_DELAY);
this->data_pin_->digital_write(true);
delayMicroseconds(BP1658CJ_DELAY);
}
} // namespace bp1658cj

View File

@ -8,6 +8,7 @@ from esphome.const import (
CONF_AWAY,
CONF_AWAY_COMMAND_TOPIC,
CONF_AWAY_STATE_TOPIC,
CONF_CURRENT_HUMIDITY_STATE_TOPIC,
CONF_CURRENT_TEMPERATURE_STATE_TOPIC,
CONF_CUSTOM_FAN_MODE,
CONF_CUSTOM_PRESET,
@ -28,6 +29,8 @@ from esphome.const import (
CONF_SWING_MODE,
CONF_SWING_MODE_COMMAND_TOPIC,
CONF_SWING_MODE_STATE_TOPIC,
CONF_TARGET_HUMIDITY_COMMAND_TOPIC,
CONF_TARGET_HUMIDITY_STATE_TOPIC,
CONF_TARGET_TEMPERATURE,
CONF_TARGET_TEMPERATURE_COMMAND_TOPIC,
CONF_TARGET_TEMPERATURE_STATE_TOPIC,
@ -106,6 +109,9 @@ CLIMATE_SWING_MODES = {
validate_climate_swing_mode = cv.enum(CLIMATE_SWING_MODES, upper=True)
CONF_CURRENT_TEMPERATURE = "current_temperature"
CONF_MIN_HUMIDITY = "min_humidity"
CONF_MAX_HUMIDITY = "max_humidity"
CONF_TARGET_HUMIDITY = "target_humidity"
visual_temperature = cv.float_with_unit(
"visual_temperature", "(°C|° C|°|C|° K|° K|K|°F|° F|F)?"
@ -153,6 +159,8 @@ CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).
cv.Optional(CONF_MIN_TEMPERATURE): cv.temperature,
cv.Optional(CONF_MAX_TEMPERATURE): cv.temperature,
cv.Optional(CONF_TEMPERATURE_STEP): VISUAL_TEMPERATURE_STEP_SCHEMA,
cv.Optional(CONF_MIN_HUMIDITY): cv.percentage_int,
cv.Optional(CONF_MAX_HUMIDITY): cv.percentage_int,
}
),
cv.Optional(CONF_ACTION_STATE_TOPIC): cv.All(
@ -167,6 +175,9 @@ CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).
cv.Optional(CONF_CURRENT_TEMPERATURE_STATE_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.publish_topic
),
cv.Optional(CONF_CURRENT_HUMIDITY_STATE_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.publish_topic
),
cv.Optional(CONF_FAN_MODE_COMMAND_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.publish_topic
),
@ -209,6 +220,12 @@ CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).
cv.Optional(CONF_TARGET_TEMPERATURE_LOW_STATE_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.publish_topic
),
cv.Optional(CONF_TARGET_HUMIDITY_COMMAND_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.publish_topic
),
cv.Optional(CONF_TARGET_HUMIDITY_STATE_TOPIC): cv.All(
cv.requires_component("mqtt"), cv.publish_topic
),
cv.Optional(CONF_ON_CONTROL): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ControlTrigger),
@ -238,6 +255,10 @@ async def setup_climate_core_(var, config):
visual[CONF_TEMPERATURE_STEP][CONF_CURRENT_TEMPERATURE],
)
)
if CONF_MIN_HUMIDITY in visual:
cg.add(var.set_visual_min_humidity_override(visual[CONF_MIN_HUMIDITY]))
if CONF_MAX_HUMIDITY in visual:
cg.add(var.set_visual_max_humidity_override(visual[CONF_MAX_HUMIDITY]))
if CONF_MQTT_ID in config:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
@ -255,6 +276,12 @@ async def setup_climate_core_(var, config):
config[CONF_CURRENT_TEMPERATURE_STATE_TOPIC]
)
)
if CONF_CURRENT_HUMIDITY_STATE_TOPIC in config:
cg.add(
mqtt_.set_custom_current_humidity_state_topic(
config[CONF_CURRENT_HUMIDITY_STATE_TOPIC]
)
)
if CONF_FAN_MODE_COMMAND_TOPIC in config:
cg.add(
mqtt_.set_custom_fan_mode_command_topic(
@ -323,6 +350,18 @@ async def setup_climate_core_(var, config):
config[CONF_TARGET_TEMPERATURE_LOW_STATE_TOPIC]
)
)
if CONF_TARGET_HUMIDITY_COMMAND_TOPIC in config:
cg.add(
mqtt_.set_custom_target_humidity_command_topic(
config[CONF_TARGET_HUMIDITY_COMMAND_TOPIC]
)
)
if CONF_TARGET_HUMIDITY_STATE_TOPIC in config:
cg.add(
mqtt_.set_custom_target_humidity_state_topic(
config[CONF_TARGET_HUMIDITY_STATE_TOPIC]
)
)
for conf in config.get(CONF_ON_STATE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
@ -351,6 +390,7 @@ CLIMATE_CONTROL_ACTION_SCHEMA = cv.Schema(
cv.Optional(CONF_TARGET_TEMPERATURE): cv.templatable(cv.temperature),
cv.Optional(CONF_TARGET_TEMPERATURE_LOW): cv.templatable(cv.temperature),
cv.Optional(CONF_TARGET_TEMPERATURE_HIGH): cv.templatable(cv.temperature),
cv.Optional(CONF_TARGET_HUMIDITY): cv.templatable(cv.percentage_int),
cv.Optional(CONF_AWAY): cv.invalid("Use preset instead"),
cv.Exclusive(CONF_FAN_MODE, "fan_mode"): cv.templatable(
validate_climate_fan_mode
@ -387,6 +427,9 @@ async def climate_control_to_code(config, action_id, template_arg, args):
config[CONF_TARGET_TEMPERATURE_HIGH], args, float
)
cg.add(var.set_target_temperature_high(template_))
if CONF_TARGET_HUMIDITY in config:
template_ = await cg.templatable(config[CONF_TARGET_HUMIDITY], args, float)
cg.add(var.set_target_humidity(template_))
if CONF_FAN_MODE in config:
template_ = await cg.templatable(config[CONF_FAN_MODE], args, ClimateFanMode)
cg.add(var.set_fan_mode(template_))

View File

@ -14,6 +14,7 @@ template<typename... Ts> class ControlAction : public Action<Ts...> {
TEMPLATABLE_VALUE(float, target_temperature)
TEMPLATABLE_VALUE(float, target_temperature_low)
TEMPLATABLE_VALUE(float, target_temperature_high)
TEMPLATABLE_VALUE(float, target_humidity)
TEMPLATABLE_VALUE(bool, away)
TEMPLATABLE_VALUE(ClimateFanMode, fan_mode)
TEMPLATABLE_VALUE(std::string, custom_fan_mode)
@ -27,6 +28,7 @@ template<typename... Ts> class ControlAction : public Action<Ts...> {
call.set_target_temperature(this->target_temperature_.optional_value(x...));
call.set_target_temperature_low(this->target_temperature_low_.optional_value(x...));
call.set_target_temperature_high(this->target_temperature_high_.optional_value(x...));
call.set_target_humidity(this->target_humidity_.optional_value(x...));
if (away_.has_value()) {
call.set_preset(away_.value(x...) ? CLIMATE_PRESET_AWAY : CLIMATE_PRESET_HOME);
}

View File

@ -45,6 +45,9 @@ void ClimateCall::perform() {
if (this->target_temperature_high_.has_value()) {
ESP_LOGD(TAG, " Target Temperature High: %.2f", *this->target_temperature_high_);
}
if (this->target_humidity_.has_value()) {
ESP_LOGD(TAG, " Target Humidity: %.0f", *this->target_humidity_);
}
this->parent_->control(*this);
}
void ClimateCall::validate_() {
@ -262,10 +265,16 @@ ClimateCall &ClimateCall::set_target_temperature_high(float target_temperature_h
this->target_temperature_high_ = target_temperature_high;
return *this;
}
ClimateCall &ClimateCall::set_target_humidity(float target_humidity) {
this->target_humidity_ = target_humidity;
return *this;
}
const optional<ClimateMode> &ClimateCall::get_mode() const { return this->mode_; }
const optional<float> &ClimateCall::get_target_temperature() const { return this->target_temperature_; }
const optional<float> &ClimateCall::get_target_temperature_low() const { return this->target_temperature_low_; }
const optional<float> &ClimateCall::get_target_temperature_high() const { return this->target_temperature_high_; }
const optional<float> &ClimateCall::get_target_humidity() const { return this->target_humidity_; }
const optional<ClimateFanMode> &ClimateCall::get_fan_mode() const { return this->fan_mode_; }
const optional<std::string> &ClimateCall::get_custom_fan_mode() const { return this->custom_fan_mode_; }
const optional<ClimatePreset> &ClimateCall::get_preset() const { return this->preset_; }
@ -283,6 +292,10 @@ ClimateCall &ClimateCall::set_target_temperature(optional<float> target_temperat
this->target_temperature_ = target_temperature;
return *this;
}
ClimateCall &ClimateCall::set_target_humidity(optional<float> target_humidity) {
this->target_humidity_ = target_humidity;
return *this;
}
ClimateCall &ClimateCall::set_mode(optional<ClimateMode> mode) {
this->mode_ = mode;
return *this;
@ -343,6 +356,9 @@ void Climate::save_state_() {
} else {
state.target_temperature = this->target_temperature;
}
if (traits.get_supports_target_humidity()) {
state.target_humidity = this->target_humidity;
}
if (traits.get_supports_fan_modes() && fan_mode.has_value()) {
state.uses_custom_fan_mode = false;
state.fan_mode = this->fan_mode.value();
@ -408,6 +424,12 @@ void Climate::publish_state() {
} else {
ESP_LOGD(TAG, " Target Temperature: %.2f°C", this->target_temperature);
}
if (traits.get_supports_current_humidity()) {
ESP_LOGD(TAG, " Current Humidity: %.0f%%", this->current_humidity);
}
if (traits.get_supports_target_humidity()) {
ESP_LOGD(TAG, " Target Humidity: %.0f%%", this->target_humidity);
}
// Send state to frontend
this->state_callback_.call(*this);
@ -427,6 +449,12 @@ ClimateTraits Climate::get_traits() {
traits.set_visual_target_temperature_step(*this->visual_target_temperature_step_override_);
traits.set_visual_current_temperature_step(*this->visual_current_temperature_step_override_);
}
if (this->visual_min_humidity_override_.has_value()) {
traits.set_visual_min_humidity(*this->visual_min_humidity_override_);
}
if (this->visual_max_humidity_override_.has_value()) {
traits.set_visual_max_humidity(*this->visual_max_humidity_override_);
}
return traits;
}
@ -441,6 +469,12 @@ void Climate::set_visual_temperature_step_override(float target, float current)
this->visual_target_temperature_step_override_ = target;
this->visual_current_temperature_step_override_ = current;
}
void Climate::set_visual_min_humidity_override(float visual_min_humidity_override) {
this->visual_min_humidity_override_ = visual_min_humidity_override;
}
void Climate::set_visual_max_humidity_override(float visual_max_humidity_override) {
this->visual_max_humidity_override_ = visual_max_humidity_override;
}
ClimateCall Climate::make_call() { return ClimateCall(this); }
@ -454,6 +488,9 @@ ClimateCall ClimateDeviceRestoreState::to_call(Climate *climate) {
} else {
call.set_target_temperature(this->target_temperature);
}
if (traits.get_supports_target_humidity()) {
call.set_target_humidity(this->target_humidity);
}
if (traits.get_supports_fan_modes() || !traits.get_supported_custom_fan_modes().empty()) {
call.set_fan_mode(this->fan_mode);
}
@ -474,6 +511,9 @@ void ClimateDeviceRestoreState::apply(Climate *climate) {
} else {
climate->target_temperature = this->target_temperature;
}
if (traits.get_supports_target_humidity()) {
climate->target_humidity = this->target_humidity;
}
if (traits.get_supports_fan_modes() && !this->uses_custom_fan_mode) {
climate->fan_mode = this->fan_mode;
}
@ -530,17 +570,25 @@ void Climate::dump_traits_(const char *tag) {
auto traits = this->get_traits();
ESP_LOGCONFIG(tag, "ClimateTraits:");
ESP_LOGCONFIG(tag, " [x] Visual settings:");
ESP_LOGCONFIG(tag, " - Min: %.1f", traits.get_visual_min_temperature());
ESP_LOGCONFIG(tag, " - Max: %.1f", traits.get_visual_max_temperature());
ESP_LOGCONFIG(tag, " - Step:");
ESP_LOGCONFIG(tag, " - Min temperature: %.1f", traits.get_visual_min_temperature());
ESP_LOGCONFIG(tag, " - Max temperature: %.1f", traits.get_visual_max_temperature());
ESP_LOGCONFIG(tag, " - Temperature step:");
ESP_LOGCONFIG(tag, " Target: %.1f", traits.get_visual_target_temperature_step());
ESP_LOGCONFIG(tag, " Current: %.1f", traits.get_visual_current_temperature_step());
ESP_LOGCONFIG(tag, " - Min humidity: %.0f", traits.get_visual_min_humidity());
ESP_LOGCONFIG(tag, " - Max humidity: %.0f", traits.get_visual_max_humidity());
if (traits.get_supports_current_temperature()) {
ESP_LOGCONFIG(tag, " [x] Supports current temperature");
}
if (traits.get_supports_current_humidity()) {
ESP_LOGCONFIG(tag, " [x] Supports current humidity");
}
if (traits.get_supports_two_point_target_temperature()) {
ESP_LOGCONFIG(tag, " [x] Supports two-point target temperature");
}
if (traits.get_supports_target_humidity()) {
ESP_LOGCONFIG(tag, " [x] Supports target humidity");
}
if (traits.get_supports_action()) {
ESP_LOGCONFIG(tag, " [x] Supports action");
}

View File

@ -64,6 +64,10 @@ class ClimateCall {
* For climate devices with two point target temperature control
*/
ClimateCall &set_target_temperature_high(optional<float> target_temperature_high);
/// Set the target humidity of the climate device.
ClimateCall &set_target_humidity(float target_humidity);
/// Set the target humidity of the climate device.
ClimateCall &set_target_humidity(optional<float> target_humidity);
/// Set the fan mode of the climate device.
ClimateCall &set_fan_mode(ClimateFanMode fan_mode);
/// Set the fan mode of the climate device.
@ -93,6 +97,7 @@ class ClimateCall {
const optional<float> &get_target_temperature() const;
const optional<float> &get_target_temperature_low() const;
const optional<float> &get_target_temperature_high() const;
const optional<float> &get_target_humidity() const;
const optional<ClimateFanMode> &get_fan_mode() const;
const optional<ClimateSwingMode> &get_swing_mode() const;
const optional<std::string> &get_custom_fan_mode() const;
@ -107,6 +112,7 @@ class ClimateCall {
optional<float> target_temperature_;
optional<float> target_temperature_low_;
optional<float> target_temperature_high_;
optional<float> target_humidity_;
optional<ClimateFanMode> fan_mode_;
optional<ClimateSwingMode> swing_mode_;
optional<std::string> custom_fan_mode_;
@ -136,6 +142,7 @@ struct ClimateDeviceRestoreState {
float target_temperature_high;
};
};
float target_humidity;
/// Convert this struct to a climate call that can be performed.
ClimateCall to_call(Climate *climate);
@ -160,24 +167,34 @@ struct ClimateDeviceRestoreState {
*/
class Climate : public EntityBase {
public:
Climate() {}
/// The active mode of the climate device.
ClimateMode mode{CLIMATE_MODE_OFF};
/// The active state of the climate device.
ClimateAction action{CLIMATE_ACTION_OFF};
/// The current temperature of the climate device, as reported from the integration.
float current_temperature{NAN};
/// The current humidity of the climate device, as reported from the integration.
float current_humidity{NAN};
union {
/// The target temperature of the climate device.
float target_temperature;
struct {
/// The minimum target temperature of the climate device, for climate devices with split target temperature.
float target_temperature_low;
float target_temperature_low{NAN};
/// The maximum target temperature of the climate device, for climate devices with split target temperature.
float target_temperature_high;
float target_temperature_high{NAN};
};
};
/// The target humidity of the climate device.
float target_humidity;
/// The active fan mode of the climate device.
optional<ClimateFanMode> fan_mode;
@ -231,6 +248,8 @@ class Climate : public EntityBase {
void set_visual_min_temperature_override(float visual_min_temperature_override);
void set_visual_max_temperature_override(float visual_max_temperature_override);
void set_visual_temperature_step_override(float target, float current);
void set_visual_min_humidity_override(float visual_min_humidity_override);
void set_visual_max_humidity_override(float visual_max_humidity_override);
protected:
friend ClimateCall;
@ -280,6 +299,8 @@ class Climate : public EntityBase {
optional<float> visual_max_temperature_override_{};
optional<float> visual_target_temperature_step_override_{};
optional<float> visual_current_temperature_step_override_{};
optional<float> visual_min_humidity_override_{};
optional<float> visual_max_humidity_override_{};
};
} // namespace climate

View File

@ -44,10 +44,18 @@ class ClimateTraits {
void set_supports_current_temperature(bool supports_current_temperature) {
supports_current_temperature_ = supports_current_temperature;
}
bool get_supports_current_humidity() const { return supports_current_humidity_; }
void set_supports_current_humidity(bool supports_current_humidity) {
supports_current_humidity_ = supports_current_humidity;
}
bool get_supports_two_point_target_temperature() const { return supports_two_point_target_temperature_; }
void set_supports_two_point_target_temperature(bool supports_two_point_target_temperature) {
supports_two_point_target_temperature_ = supports_two_point_target_temperature;
}
bool get_supports_target_humidity() const { return supports_target_humidity_; }
void set_supports_target_humidity(bool supports_target_humidity) {
supports_target_humidity_ = supports_target_humidity;
}
void set_supported_modes(std::set<ClimateMode> modes) { supported_modes_ = std::move(modes); }
void add_supported_mode(ClimateMode mode) { supported_modes_.insert(mode); }
ESPDEPRECATED("This method is deprecated, use set_supported_modes() instead", "v1.20")
@ -153,6 +161,11 @@ class ClimateTraits {
int8_t get_target_temperature_accuracy_decimals() const;
int8_t get_current_temperature_accuracy_decimals() const;
float get_visual_min_humidity() const { return visual_min_humidity_; }
void set_visual_min_humidity(float visual_min_humidity) { visual_min_humidity_ = visual_min_humidity; }
float get_visual_max_humidity() const { return visual_max_humidity_; }
void set_visual_max_humidity(float visual_max_humidity) { visual_max_humidity_ = visual_max_humidity; }
protected:
void set_mode_support_(climate::ClimateMode mode, bool supported) {
if (supported) {
@ -177,7 +190,9 @@ class ClimateTraits {
}
bool supports_current_temperature_{false};
bool supports_current_humidity_{false};
bool supports_two_point_target_temperature_{false};
bool supports_target_humidity_{false};
std::set<climate::ClimateMode> supported_modes_ = {climate::CLIMATE_MODE_OFF};
bool supports_action_{false};
std::set<climate::ClimateFanMode> supported_fan_modes_;
@ -190,6 +205,8 @@ class ClimateTraits {
float visual_max_temperature_{30};
float visual_target_temperature_step_{0.1};
float visual_current_temperature_step_{0.1};
float visual_min_humidity_{30};
float visual_max_humidity_{99};
};
} // namespace climate

View File

@ -1,38 +1,37 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import (
climate,
remote_transmitter,
remote_receiver,
sensor,
remote_base,
)
from esphome.components.remote_base import CONF_RECEIVER_ID, CONF_TRANSMITTER_ID
from esphome.components import climate, sensor, remote_base
from esphome.const import CONF_SUPPORTS_COOL, CONF_SUPPORTS_HEAT, CONF_SENSOR
DEPENDENCIES = ["remote_transmitter"]
AUTO_LOAD = ["sensor", "remote_base"]
CODEOWNERS = ["@glmnet"]
climate_ir_ns = cg.esphome_ns.namespace("climate_ir")
ClimateIR = climate_ir_ns.class_(
"ClimateIR", climate.Climate, cg.Component, remote_base.RemoteReceiverListener
"ClimateIR",
climate.Climate,
cg.Component,
remote_base.RemoteReceiverListener,
remote_base.RemoteTransmittable,
)
CLIMATE_IR_SCHEMA = climate.CLIMATE_SCHEMA.extend(
{
cv.GenerateID(CONF_TRANSMITTER_ID): cv.use_id(
remote_transmitter.RemoteTransmitterComponent
),
cv.Optional(CONF_SUPPORTS_COOL, default=True): cv.boolean,
cv.Optional(CONF_SUPPORTS_HEAT, default=True): cv.boolean,
cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor),
}
).extend(cv.COMPONENT_SCHEMA)
CLIMATE_IR_SCHEMA = (
climate.CLIMATE_SCHEMA.extend(
{
cv.Optional(CONF_SUPPORTS_COOL, default=True): cv.boolean,
cv.Optional(CONF_SUPPORTS_HEAT, default=True): cv.boolean,
cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor),
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(remote_base.REMOTE_TRANSMITTABLE_SCHEMA)
)
CLIMATE_IR_WITH_RECEIVER_SCHEMA = CLIMATE_IR_SCHEMA.extend(
{
cv.Optional(CONF_RECEIVER_ID): cv.use_id(
remote_receiver.RemoteReceiverComponent
cv.Optional(remote_base.CONF_RECEIVER_ID): cv.use_id(
remote_base.RemoteReceiverBase
),
}
)
@ -41,15 +40,11 @@ CLIMATE_IR_WITH_RECEIVER_SCHEMA = CLIMATE_IR_SCHEMA.extend(
async def register_climate_ir(var, config):
await cg.register_component(var, config)
await climate.register_climate(var, config)
await remote_base.register_transmittable(var, config)
cg.add(var.set_supports_cool(config[CONF_SUPPORTS_COOL]))
cg.add(var.set_supports_heat(config[CONF_SUPPORTS_HEAT]))
if remote_base.CONF_RECEIVER_ID in config:
await remote_base.register_listener(var, config)
if sensor_id := config.get(CONF_SENSOR):
sens = await cg.get_variable(sensor_id)
cg.add(var.set_sensor(sens))
if receiver_id := config.get(CONF_RECEIVER_ID):
receiver = await cg.get_variable(receiver_id)
cg.add(receiver.register_listener(var))
transmitter = await cg.get_variable(config[CONF_TRANSMITTER_ID])
cg.add(var.set_transmitter(transmitter))

View File

@ -18,7 +18,10 @@ namespace climate_ir {
Likewise to decode a IR into the AC state, implement
bool RemoteReceiverListener::on_receive(remote_base::RemoteReceiveData data) and return true
*/
class ClimateIR : public climate::Climate, public Component, public remote_base::RemoteReceiverListener {
class ClimateIR : public Component,
public climate::Climate,
public remote_base::RemoteReceiverListener,
public remote_base::RemoteTransmittable {
public:
ClimateIR(float minimum_temperature, float maximum_temperature, float temperature_step = 1.0f,
bool supports_dry = false, bool supports_fan_only = false, std::set<climate::ClimateFanMode> fan_modes = {},
@ -35,9 +38,6 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
void setup() override;
void dump_config() override;
void set_transmitter(remote_transmitter::RemoteTransmitterComponent *transmitter) {
this->transmitter_ = transmitter;
}
void set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
void set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
void set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
@ -64,7 +64,6 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
std::set<climate::ClimateSwingMode> swing_modes_ = {};
std::set<climate::ClimatePreset> presets_ = {};
remote_transmitter::RemoteTransmitterComponent *transmitter_;
sensor::Sensor *sensor_{nullptr};
};

View File

@ -102,11 +102,7 @@ void CoolixClimate::transmit_state() {
}
}
ESP_LOGV(TAG, "Sending coolix code: 0x%06" PRIX32, remote_state);
auto transmit = this->transmitter_->transmit();
auto *data = transmit.get_data();
remote_base::CoolixProtocol().encode(data, remote_state);
transmit.perform();
this->transmit_<remote_base::CoolixProtocol>(remote_state);
}
bool CoolixClimate::on_coolix(climate::Climate *parent, remote_base::RemoteReceiveData data) {

View File

@ -12,6 +12,7 @@ void CopyFan::setup() {
this->oscillating = source_->oscillating;
this->speed = source_->speed;
this->direction = source_->direction;
this->preset_mode = source_->preset_mode;
this->publish_state();
});
@ -19,6 +20,7 @@ void CopyFan::setup() {
this->oscillating = source_->oscillating;
this->speed = source_->speed;
this->direction = source_->direction;
this->preset_mode = source_->preset_mode;
this->publish_state();
}
@ -33,6 +35,7 @@ fan::FanTraits CopyFan::get_traits() {
traits.set_speed(base.supports_speed());
traits.set_supported_speed_count(base.supported_speed_count());
traits.set_direction(base.supports_direction());
traits.set_supported_preset_modes(base.supported_preset_modes());
return traits;
}
@ -46,6 +49,8 @@ void CopyFan::control(const fan::FanCall &call) {
call2.set_speed(*call.get_speed());
if (call.get_direction().has_value())
call2.set_direction(*call.get_direction());
if (!call.get_preset_mode().empty())
call2.set_preset_mode(call.get_preset_mode());
call2.perform();
}

View File

@ -58,7 +58,7 @@ BASIC_DISPLAY_SCHEMA = cv.Schema(
{
cv.Optional(CONF_LAMBDA): cv.lambda_,
}
)
).extend(cv.polling_component_schema("1s"))
FULL_DISPLAY_SCHEMA = BASIC_DISPLAY_SCHEMA.extend(
{
@ -116,6 +116,7 @@ async def setup_display_core_(var, config):
async def register_display(var, config):
await cg.register_component(var, config)
await setup_display_core_(var, config)

View File

@ -166,6 +166,13 @@ void Display::qr_code(int x, int y, qr_code::QrCode *qr_code, Color color_on, in
}
#endif // USE_QR_CODE
#ifdef USE_GRAPHICAL_DISPLAY_MENU
void Display::menu(int x, int y, graphical_display_menu::GraphicalDisplayMenu *menu, int width, int height) {
Rect rect(x, y, width, height);
menu->draw(this, &rect);
}
#endif // USE_GRAPHICAL_DISPLAY_MENU
void Display::get_text_bounds(int x, int y, const char *text, BaseFont *font, TextAlign align, int *x1, int *y1,
int *width, int *height) {
int x_offset, baseline;

View File

@ -17,6 +17,10 @@
#include "esphome/components/qr_code/qr_code.h"
#endif
#ifdef USE_GRAPHICAL_DISPLAY_MENU
#include "esphome/components/graphical_display_menu/graphical_display_menu.h"
#endif
namespace esphome {
namespace display {
@ -163,7 +167,7 @@ class BaseFont {
virtual void measure(const char *str, int *width, int *x_offset, int *baseline, int *height) = 0;
};
class Display {
class Display : public PollingComponent {
public:
/// Fill the entire screen with the given color.
virtual void fill(Color color);
@ -392,6 +396,17 @@ class Display {
void qr_code(int x, int y, qr_code::QrCode *qr_code, Color color_on = COLOR_ON, int scale = 1);
#endif
#ifdef USE_GRAPHICAL_DISPLAY_MENU
/**
* @param x The x coordinate of the upper left corner
* @param y The y coordinate of the upper left corner
* @param menu The GraphicalDisplayMenu to draw
* @param width Width of the menu
* @param height Height of the menu
*/
void menu(int x, int y, graphical_display_menu::GraphicalDisplayMenu *menu, int width, int height);
#endif // USE_GRAPHICAL_DISPLAY_MENU
/** Get the text bounds of the given string.
*
* @param x The x coordinate to place the string at, can be 0 if only interested in dimensions.

View File

@ -172,6 +172,8 @@ void DisplayMenuComponent::show_main() {
this->process_initial_();
this->on_before_show();
if (this->active_ && this->editing_)
this->finish_editing_();
@ -188,6 +190,8 @@ void DisplayMenuComponent::show_main() {
}
this->draw_and_update();
this->on_after_show();
}
void DisplayMenuComponent::show() {
@ -196,18 +200,26 @@ void DisplayMenuComponent::show() {
this->process_initial_();
this->on_before_show();
if (!this->active_) {
this->active_ = true;
this->draw_and_update();
}
this->on_after_show();
}
void DisplayMenuComponent::hide() {
if (this->check_healthy_and_active_()) {
this->on_before_hide();
if (this->editing_)
this->finish_editing_();
this->active_ = false;
this->update();
this->on_after_hide();
}
}

View File

@ -60,6 +60,11 @@ class DisplayMenuComponent : public Component {
update();
}
virtual void on_before_show(){};
virtual void on_after_show(){};
virtual void on_before_hide(){};
virtual void on_after_hide(){};
uint8_t rows_;
bool active_;
MenuMode mode_;

View File

@ -5,6 +5,29 @@
namespace esphome {
namespace display_menu_base {
const LogString *menu_item_type_to_string(MenuItemType type) {
switch (type) {
case MenuItemType::MENU_ITEM_LABEL:
return LOG_STR("MENU_ITEM_LABEL");
case MenuItemType::MENU_ITEM_MENU:
return LOG_STR("MENU_ITEM_MENU");
case MenuItemType::MENU_ITEM_BACK:
return LOG_STR("MENU_ITEM_BACK");
case MenuItemType::MENU_ITEM_SELECT:
return LOG_STR("MENU_ITEM_SELECT");
case MenuItemType::MENU_ITEM_NUMBER:
return LOG_STR("MENU_ITEM_NUMBER");
case MenuItemType::MENU_ITEM_SWITCH:
return LOG_STR("MENU_ITEM_SWITCH");
case MenuItemType::MENU_ITEM_COMMAND:
return LOG_STR("MENU_ITEM_COMMAND");
case MenuItemType::MENU_ITEM_CUSTOM:
return LOG_STR("MENU_ITEM_CUSTOM");
default:
return LOG_STR("UNKNOWN");
}
}
void MenuItem::on_enter() { this->on_enter_callbacks_.call(); }
void MenuItem::on_leave() { this->on_leave_callbacks_.call(); }

View File

@ -14,6 +14,7 @@
#endif
#include <vector>
#include "esphome/core/log.h"
namespace esphome {
namespace display_menu_base {
@ -29,6 +30,9 @@ enum MenuItemType {
MENU_ITEM_CUSTOM,
};
/// @brief Returns a string representation of a menu item type suitable for logging
const LogString *menu_item_type_to_string(MenuItemType type);
class MenuItem;
class MenuItemMenu;
using value_getter_t = std::function<std::string(const MenuItem *)>;

View File

@ -12,7 +12,6 @@ ektf2232_ns = cg.esphome_ns.namespace("ektf2232")
EKTF2232Touchscreen = ektf2232_ns.class_(
"EKTF2232Touchscreen",
touchscreen.Touchscreen,
cg.Component,
i2c.I2CDevice,
)
@ -28,17 +27,14 @@ CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend(
),
cv.Required(CONF_RTS_PIN): pins.gpio_output_pin_schema,
}
)
.extend(i2c.i2c_device_schema(0x15))
.extend(cv.COMPONENT_SCHEMA)
).extend(i2c.i2c_device_schema(0x15))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
interrupt_pin = await cg.gpio_pin_expression(config[CONF_INTERRUPT_PIN])
cg.add(var.set_interrupt_pin(interrupt_pin))

View File

@ -15,16 +15,12 @@ static const uint8_t GET_X_RES[4] = {0x53, 0x60, 0x00, 0x00};
static const uint8_t GET_Y_RES[4] = {0x53, 0x63, 0x00, 0x00};
static const uint8_t GET_POWER_STATE_CMD[4] = {0x53, 0x50, 0x00, 0x01};
void EKTF2232TouchscreenStore::gpio_intr(EKTF2232TouchscreenStore *store) { store->touch = true; }
void EKTF2232Touchscreen::setup() {
ESP_LOGCONFIG(TAG, "Setting up EKT2232 Touchscreen...");
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->interrupt_pin_->setup();
this->store_.pin = this->interrupt_pin_->to_isr();
this->interrupt_pin_->attach_interrupt(EKTF2232TouchscreenStore::gpio_intr, &this->store_,
gpio::INTERRUPT_FALLING_EDGE);
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
this->rts_pin_->setup();
@ -45,7 +41,7 @@ void EKTF2232Touchscreen::setup() {
this->mark_failed();
return;
}
this->x_resolution_ = ((received[2])) | ((received[3] & 0xf0) << 4);
this->x_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
this->write(GET_Y_RES, 4);
if (this->read(received, 4)) {
@ -54,19 +50,14 @@ void EKTF2232Touchscreen::setup() {
this->mark_failed();
return;
}
this->y_resolution_ = ((received[2])) | ((received[3] & 0xf0) << 4);
this->store_.touch = false;
this->y_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
this->set_power_state(true);
}
void EKTF2232Touchscreen::loop() {
if (!this->store_.touch)
return;
this->store_.touch = false;
void EKTF2232Touchscreen::update_touches() {
uint8_t touch_count = 0;
std::vector<TouchPoint> touches;
int16_t x_raw, y_raw;
uint8_t raw[8];
this->read(raw, 8);
@ -75,45 +66,15 @@ void EKTF2232Touchscreen::loop() {
touch_count++;
}
if (touch_count == 0) {
for (auto *listener : this->touch_listeners_)
listener->release();
return;
}
touch_count = std::min<uint8_t>(touch_count, 2);
ESP_LOGV(TAG, "Touch count: %d", touch_count);
for (int i = 0; i < touch_count; i++) {
uint8_t *d = raw + 1 + (i * 3);
uint32_t raw_x = (d[0] & 0xF0) << 4 | d[1];
uint32_t raw_y = (d[0] & 0x0F) << 8 | d[2];
raw_x = raw_x * this->display_height_ - 1;
raw_y = raw_y * this->display_width_ - 1;
TouchPoint tp;
switch (this->rotation_) {
case ROTATE_0_DEGREES:
tp.y = raw_x / this->x_resolution_;
tp.x = this->display_width_ - 1 - (raw_y / this->y_resolution_);
break;
case ROTATE_90_DEGREES:
tp.x = raw_x / this->x_resolution_;
tp.y = raw_y / this->y_resolution_;
break;
case ROTATE_180_DEGREES:
tp.y = this->display_height_ - 1 - (raw_x / this->x_resolution_);
tp.x = raw_y / this->y_resolution_;
break;
case ROTATE_270_DEGREES:
tp.x = this->display_height_ - 1 - (raw_x / this->x_resolution_);
tp.y = this->display_width_ - 1 - (raw_y / this->y_resolution_);
break;
}
this->defer([this, tp]() { this->send_touch_(tp); });
x_raw = (d[0] & 0xF0) << 4 | d[1];
y_raw = (d[0] & 0x0F) << 8 | d[2];
this->set_raw_touch_position_(i, x_raw, y_raw);
}
}
@ -126,7 +87,7 @@ void EKTF2232Touchscreen::set_power_state(bool enable) {
bool EKTF2232Touchscreen::get_power_state() {
uint8_t received[4];
this->write(GET_POWER_STATE_CMD, 4);
this->store_.touch = false;
this->store_.touched = false;
this->read(received, 4);
return (received[1] >> 3) & 1;
}
@ -145,14 +106,14 @@ bool EKTF2232Touchscreen::soft_reset_() {
uint8_t received[4];
uint16_t timeout = 1000;
while (!this->store_.touch && timeout > 0) {
while (!this->store_.touched && timeout > 0) {
delay(1);
timeout--;
}
if (timeout > 0)
this->store_.touch = true;
this->store_.touched = true;
this->read(received, 4);
this->store_.touch = false;
this->store_.touched = false;
return !memcmp(received, HELLO, 4);
}

View File

@ -9,19 +9,11 @@
namespace esphome {
namespace ektf2232 {
struct EKTF2232TouchscreenStore {
volatile bool touch;
ISRInternalGPIOPin pin;
static void gpio_intr(EKTF2232TouchscreenStore *store);
};
using namespace touchscreen;
class EKTF2232Touchscreen : public Touchscreen, public Component, public i2c::I2CDevice {
class EKTF2232Touchscreen : public Touchscreen, public i2c::I2CDevice {
public:
void setup() override;
void loop() override;
void dump_config() override;
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
@ -33,12 +25,10 @@ class EKTF2232Touchscreen : public Touchscreen, public Component, public i2c::I2
protected:
void hard_reset_();
bool soft_reset_();
void update_touches() override;
InternalGPIOPin *interrupt_pin_;
GPIOPin *rts_pin_;
EKTF2232TouchscreenStore store_;
uint16_t x_resolution_;
uint16_t y_resolution_;
};
} // namespace ektf2232

View File

@ -0,0 +1 @@
CODEOWNERS = ["@vincentscode"]

View File

@ -0,0 +1,321 @@
// ENS160 sensor with I2C interface from ScioSense
//
// Datasheet: https://www.sciosense.com/wp-content/uploads/documents/SC-001224-DS-7-ENS160-Datasheet.pdf
//
// Implementation based on:
// https://github.com/sciosense/ENS160_driver
#include "ens160.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace ens160 {
static const char *const TAG = "ens160";
static const uint8_t ENS160_BOOTING = 10;
static const uint16_t ENS160_PART_ID = 0x0160;
static const uint8_t ENS160_REG_PART_ID = 0x00;
static const uint8_t ENS160_REG_OPMODE = 0x10;
static const uint8_t ENS160_REG_CONFIG = 0x11;
static const uint8_t ENS160_REG_COMMAND = 0x12;
static const uint8_t ENS160_REG_TEMP_IN = 0x13;
static const uint8_t ENS160_REG_DATA_STATUS = 0x20;
static const uint8_t ENS160_REG_DATA_AQI = 0x21;
static const uint8_t ENS160_REG_DATA_TVOC = 0x22;
static const uint8_t ENS160_REG_DATA_ECO2 = 0x24;
static const uint8_t ENS160_REG_GPR_READ_0 = 0x48;
static const uint8_t ENS160_REG_GPR_READ_4 = ENS160_REG_GPR_READ_0 + 4;
static const uint8_t ENS160_COMMAND_NOP = 0x00;
static const uint8_t ENS160_COMMAND_CLRGPR = 0xCC;
static const uint8_t ENS160_COMMAND_GET_APPVER = 0x0E;
static const uint8_t ENS160_OPMODE_RESET = 0xF0;
static const uint8_t ENS160_OPMODE_IDLE = 0x01;
static const uint8_t ENS160_OPMODE_STD = 0x02;
static const uint8_t ENS160_DATA_STATUS_STATAS = 0x80;
static const uint8_t ENS160_DATA_STATUS_STATER = 0x40;
static const uint8_t ENS160_DATA_STATUS_VALIDITY = 0x0C;
static const uint8_t ENS160_DATA_STATUS_NEWDAT = 0x02;
static const uint8_t ENS160_DATA_STATUS_NEWGPR = 0x01;
// helps remove reserved bits in aqi data register
static const uint8_t ENS160_DATA_AQI = 0x07;
void ENS160Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up ENS160...");
// check part_id
uint16_t part_id;
if (!this->read_bytes(ENS160_REG_PART_ID, reinterpret_cast<uint8_t *>(&part_id), 2)) {
this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed();
return;
}
if (part_id != ENS160_PART_ID) {
this->error_code_ = INVALID_ID;
this->mark_failed();
return;
}
// set mode to reset
if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_RESET)) {
this->error_code_ = WRITE_FAILED;
this->mark_failed();
return;
}
delay(ENS160_BOOTING);
// check status
uint8_t status_value;
if (!this->read_byte(ENS160_REG_DATA_STATUS, &status_value)) {
this->error_code_ = READ_FAILED;
this->mark_failed();
return;
}
this->validity_flag_ = static_cast<ValidityFlag>((ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
if (this->validity_flag_ == INVALID_OUTPUT) {
this->error_code_ = VALIDITY_INVALID;
this->mark_failed();
return;
}
// set mode to idle
if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_IDLE)) {
this->error_code_ = WRITE_FAILED;
this->mark_failed();
return;
}
// clear command
if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_NOP)) {
this->error_code_ = WRITE_FAILED;
this->mark_failed();
return;
}
if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_CLRGPR)) {
this->error_code_ = WRITE_FAILED;
this->mark_failed();
return;
}
// read firmware version
if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_GET_APPVER)) {
this->error_code_ = WRITE_FAILED;
this->mark_failed();
return;
}
uint8_t version_data[3];
if (!this->read_bytes(ENS160_REG_GPR_READ_4, version_data, 3)) {
this->error_code_ = READ_FAILED;
this->mark_failed();
return;
}
this->firmware_ver_major_ = version_data[0];
this->firmware_ver_minor_ = version_data[1];
this->firmware_ver_build_ = version_data[2];
// set mode to standard
if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_STD)) {
this->error_code_ = WRITE_FAILED;
this->mark_failed();
return;
}
// read opmode and check standard mode is achieved before finishing Setup
uint8_t op_mode;
if (!this->read_byte(ENS160_REG_OPMODE, &op_mode)) {
this->error_code_ = READ_FAILED;
this->mark_failed();
return;
}
if (op_mode != ENS160_OPMODE_STD) {
this->error_code_ = STD_OPMODE_FAILED;
this->mark_failed();
return;
}
}
void ENS160Component::update() {
uint8_t status_value, data_ready;
if (!this->read_byte(ENS160_REG_DATA_STATUS, &status_value)) {
ESP_LOGW(TAG, "Error reading status register");
this->status_set_warning();
return;
}
// verbose status logging
ESP_LOGV(TAG, "Status: ENS160 STATAS bit 0x%x",
(ENS160_DATA_STATUS_STATAS & (status_value)) == ENS160_DATA_STATUS_STATAS);
ESP_LOGV(TAG, "Status: ENS160 STATER bit 0x%x",
(ENS160_DATA_STATUS_STATER & (status_value)) == ENS160_DATA_STATUS_STATER);
ESP_LOGV(TAG, "Status: ENS160 VALIDITY FLAG 0x%02x", (ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
ESP_LOGV(TAG, "Status: ENS160 NEWDAT bit 0x%x",
(ENS160_DATA_STATUS_NEWDAT & (status_value)) == ENS160_DATA_STATUS_NEWDAT);
ESP_LOGV(TAG, "Status: ENS160 NEWGPR bit 0x%x",
(ENS160_DATA_STATUS_NEWGPR & (status_value)) == ENS160_DATA_STATUS_NEWGPR);
data_ready = ENS160_DATA_STATUS_NEWDAT & status_value;
this->validity_flag_ = static_cast<ValidityFlag>((ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
switch (validity_flag_) {
case NORMAL_OPERATION:
if (data_ready != ENS160_DATA_STATUS_NEWDAT) {
ESP_LOGD(TAG, "ENS160 readings unavailable - Normal Operation but readings not ready");
return;
}
break;
case INITIAL_STARTUP:
if (!this->initial_startup_) {
this->initial_startup_ = true;
ESP_LOGI(TAG, "ENS160 readings unavailable - 1 hour startup required after first power on");
}
return;
case WARMING_UP:
if (!this->warming_up_) {
this->warming_up_ = true;
ESP_LOGI(TAG, "ENS160 readings not available yet - Warming up requires 3 minutes");
this->send_env_data_();
}
return;
case INVALID_OUTPUT:
ESP_LOGE(TAG, "ENS160 Invalid Status - No Invalid Output");
this->status_set_warning();
return;
}
// read new data
uint16_t data_eco2;
if (!this->read_bytes(ENS160_REG_DATA_ECO2, reinterpret_cast<uint8_t *>(&data_eco2), 2)) {
ESP_LOGW(TAG, "Error reading eCO2 data register");
this->status_set_warning();
return;
}
if (this->co2_ != nullptr) {
this->co2_->publish_state(data_eco2);
}
uint16_t data_tvoc;
if (!this->read_bytes(ENS160_REG_DATA_TVOC, reinterpret_cast<uint8_t *>(&data_tvoc), 2)) {
ESP_LOGW(TAG, "Error reading TVOC data register");
this->status_set_warning();
return;
}
if (this->tvoc_ != nullptr) {
this->tvoc_->publish_state(data_tvoc);
}
uint8_t data_aqi;
if (!this->read_byte(ENS160_REG_DATA_AQI, &data_aqi)) {
ESP_LOGW(TAG, "Error reading AQI data register");
this->status_set_warning();
return;
}
if (this->aqi_ != nullptr) {
// remove reserved bits, just in case they are used in future
data_aqi = ENS160_DATA_AQI & data_aqi;
this->aqi_->publish_state(data_aqi);
}
this->status_clear_warning();
// set temperature and humidity compensation data
this->send_env_data_();
}
void ENS160Component::send_env_data_() {
if (this->temperature_ == nullptr && this->humidity_ == nullptr)
return;
float temperature = NAN;
if (this->temperature_ != nullptr)
temperature = this->temperature_->state;
if (std::isnan(temperature) || temperature < -40.0f || temperature > 85.0f) {
ESP_LOGW(TAG, "Invalid external temperature - compensation values not updated");
return;
} else {
ESP_LOGV(TAG, "External temperature compensation: %.1f°C", temperature);
}
float humidity = NAN;
if (this->humidity_ != nullptr)
humidity = this->humidity_->state;
if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
ESP_LOGW(TAG, "Invalid external humidity - compensation values not updated");
return;
} else {
ESP_LOGV(TAG, "External humidity compensation: %.1f%%", humidity);
}
uint16_t t = (uint16_t) ((temperature + 273.15f) * 64.0f);
uint16_t h = (uint16_t) (humidity * 512.0f);
uint8_t data[4];
data[0] = t & 0xff;
data[1] = (t >> 8) & 0xff;
data[2] = h & 0xff;
data[3] = (h >> 8) & 0xff;
if (!this->write_bytes(ENS160_REG_TEMP_IN, data, 4)) {
ESP_LOGE(TAG, "Error writing compensation values");
this->status_set_warning();
return;
}
}
void ENS160Component::dump_config() {
ESP_LOGCONFIG(TAG, "ENS160:");
switch (this->error_code_) {
case COMMUNICATION_FAILED:
ESP_LOGE(TAG, "Communication failed! Is the sensor connected?");
break;
case READ_FAILED:
ESP_LOGE(TAG, "Error reading from register");
break;
case WRITE_FAILED:
ESP_LOGE(TAG, "Error writing to register");
break;
case INVALID_ID:
ESP_LOGE(TAG, "Sensor reported an invalid ID. Is this a ENS160?");
break;
case VALIDITY_INVALID:
ESP_LOGE(TAG, "Invalid Device Status - No valid output");
break;
case STD_OPMODE_FAILED:
ESP_LOGE(TAG, "Device failed to achieve Standard Operating Mode");
break;
case NONE:
ESP_LOGD(TAG, "Setup successful");
break;
}
ESP_LOGI(TAG, "Firmware Version: %d.%d.%d", this->firmware_ver_major_, this->firmware_ver_minor_,
this->firmware_ver_build_);
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
LOG_SENSOR(" ", "CO2 Sensor:", this->co2_);
LOG_SENSOR(" ", "TVOC Sensor:", this->tvoc_);
LOG_SENSOR(" ", "AQI Sensor:", this->aqi_);
if (this->temperature_ != nullptr && this->humidity_ != nullptr) {
LOG_SENSOR(" ", " Temperature Compensation:", this->temperature_);
LOG_SENSOR(" ", " Humidity Compensation:", this->humidity_);
} else {
ESP_LOGCONFIG(TAG, " Compensation: Not configured");
}
}
} // namespace ens160
} // namespace esphome

View File

@ -0,0 +1,60 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace ens160 {
class ENS160Component : public PollingComponent, public i2c::I2CDevice, public sensor::Sensor {
public:
void set_co2(sensor::Sensor *co2) { co2_ = co2; }
void set_tvoc(sensor::Sensor *tvoc) { tvoc_ = tvoc; }
void set_aqi(sensor::Sensor *aqi) { aqi_ = aqi; }
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
void setup() override;
void update() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
protected:
void send_env_data_();
enum ErrorCode {
NONE = 0,
COMMUNICATION_FAILED,
INVALID_ID,
VALIDITY_INVALID,
READ_FAILED,
WRITE_FAILED,
STD_OPMODE_FAILED,
} error_code_{NONE};
enum ValidityFlag {
NORMAL_OPERATION = 0,
WARMING_UP,
INITIAL_STARTUP,
INVALID_OUTPUT,
} validity_flag_;
bool warming_up_{false};
bool initial_startup_{false};
uint8_t firmware_ver_major_{0};
uint8_t firmware_ver_minor_{0};
uint8_t firmware_ver_build_{0};
sensor::Sensor *co2_{nullptr};
sensor::Sensor *tvoc_{nullptr};
sensor::Sensor *aqi_{nullptr};
sensor::Sensor *humidity_{nullptr};
sensor::Sensor *temperature_{nullptr};
};
} // namespace ens160
} // namespace esphome

View File

@ -0,0 +1,88 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ECO2,
CONF_HUMIDITY,
CONF_ID,
CONF_TEMPERATURE,
CONF_TVOC,
DEVICE_CLASS_AQI,
DEVICE_CLASS_CARBON_DIOXIDE,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
ICON_CHEMICAL_WEAPON,
ICON_MOLECULE_CO2,
ICON_RADIATOR,
STATE_CLASS_MEASUREMENT,
UNIT_PARTS_PER_BILLION,
UNIT_PARTS_PER_MILLION,
)
CODEOWNERS = ["@vincentscode"]
DEPENDENCIES = ["i2c"]
ens160_ns = cg.esphome_ns.namespace("ens160")
ENS160Component = ens160_ns.class_(
"ENS160Component", cg.PollingComponent, i2c.I2CDevice, sensor.Sensor
)
CONF_AQI = "aqi"
CONF_COMPENSATION = "compensation"
UNIT_INDEX = "index"
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(ENS160Component),
cv.Required(CONF_ECO2): sensor.sensor_schema(
unit_of_measurement=UNIT_PARTS_PER_MILLION,
icon=ICON_MOLECULE_CO2,
accuracy_decimals=0,
device_class=DEVICE_CLASS_CARBON_DIOXIDE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Required(CONF_TVOC): sensor.sensor_schema(
unit_of_measurement=UNIT_PARTS_PER_BILLION,
icon=ICON_RADIATOR,
accuracy_decimals=0,
device_class=DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Required(CONF_AQI): sensor.sensor_schema(
unit_of_measurement=UNIT_INDEX,
icon=ICON_CHEMICAL_WEAPON,
accuracy_decimals=0,
device_class=DEVICE_CLASS_AQI,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_COMPENSATION): cv.Schema(
{
cv.Required(CONF_TEMPERATURE): cv.use_id(sensor.Sensor),
cv.Required(CONF_HUMIDITY): cv.use_id(sensor.Sensor),
}
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x53))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
sens = await sensor.new_sensor(config[CONF_ECO2])
cg.add(var.set_co2(sens))
sens = await sensor.new_sensor(config[CONF_TVOC])
cg.add(var.set_tvoc(sens))
sens = await sensor.new_sensor(config[CONF_AQI])
cg.add(var.set_aqi(sens))
if CONF_COMPENSATION in config:
compensation_config = config[CONF_COMPENSATION]
sens = await cg.get_variable(compensation_config[CONF_TEMPERATURE])
cg.add(var.set_temperature(sens))
sens = await cg.get_variable(compensation_config[CONF_HUMIDITY])
cg.add(var.set_humidity(sens))

View File

@ -462,7 +462,7 @@ async def to_code(config):
add_extra_script(
"post",
"post_build2.py",
"post_build.py",
os.path.join(os.path.dirname(__file__), "post_build.py.script"),
)
@ -497,10 +497,11 @@ async def to_code(config):
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
cg.add_platformio_option("board_build.partitions", "partitions.csv")
if CONF_PARTITIONS in config:
cg.add_platformio_option("board_build.partitions", config[CONF_PARTITIONS])
else:
cg.add_platformio_option("board_build.partitions", "partitions.csv")
add_extra_build_file(
"partitions.csv", CORE.relative_config_path(config[CONF_PARTITIONS])
)
for name, value in conf[CONF_SDKCONFIG_OPTIONS].items():
add_idf_sdkconfig_option(name, RawSdkconfigValue(value))
@ -639,20 +640,22 @@ def _write_sdkconfig():
# Called by writer.py
def copy_files():
if CORE.using_arduino:
write_file_if_changed(
CORE.relative_build_path("partitions.csv"),
get_arduino_partition_csv(
CORE.platformio_options.get("board_upload.flash_size")
),
)
if "partitions.csv" not in CORE.data[KEY_ESP32][KEY_EXTRA_BUILD_FILES]:
write_file_if_changed(
CORE.relative_build_path("partitions.csv"),
get_arduino_partition_csv(
CORE.platformio_options.get("board_upload.flash_size")
),
)
if CORE.using_esp_idf:
_write_sdkconfig()
write_file_if_changed(
CORE.relative_build_path("partitions.csv"),
get_idf_partition_csv(
CORE.platformio_options.get("board_upload.flash_size")
),
)
if "partitions.csv" not in CORE.data[KEY_ESP32][KEY_EXTRA_BUILD_FILES]:
write_file_if_changed(
CORE.relative_build_path("partitions.csv"),
get_idf_partition_csv(
CORE.platformio_options.get("board_upload.flash_size")
),
)
# IDF build scripts look for version string to put in the build.
# However, if the build path does not have an initialized git repo,
# and no version.txt file exists, the CMake script fails for some setups.

View File

@ -3,15 +3,13 @@ from typing import Any
from esphome.const import (
CONF_ID,
CONF_INPUT,
CONF_INVERTED,
CONF_MODE,
CONF_NUMBER,
CONF_OPEN_DRAIN,
CONF_OUTPUT,
CONF_PULLDOWN,
CONF_PULLUP,
CONF_IGNORE_STRAPPING_WARNING,
PLATFORM_ESP32,
)
from esphome import pins
from esphome.core import CORE
@ -33,7 +31,6 @@ from .const import (
esp32_ns,
)
from .gpio_esp32 import esp32_validate_gpio_pin, esp32_validate_supports
from .gpio_esp32_s2 import esp32_s2_validate_gpio_pin, esp32_s2_validate_supports
from .gpio_esp32_c3 import esp32_c3_validate_gpio_pin, esp32_c3_validate_supports
@ -42,7 +39,6 @@ from .gpio_esp32_c2 import esp32_c2_validate_gpio_pin, esp32_c2_validate_support
from .gpio_esp32_c6 import esp32_c6_validate_gpio_pin, esp32_c6_validate_supports
from .gpio_esp32_h2 import esp32_h2_validate_gpio_pin, esp32_h2_validate_supports
ESP32InternalGPIOPin = esp32_ns.class_("ESP32InternalGPIOPin", cg.InternalGPIOPin)
@ -161,33 +157,22 @@ DRIVE_STRENGTHS = {
}
gpio_num_t = cg.global_ns.enum("gpio_num_t")
CONF_DRIVE_STRENGTH = "drive_strength"
ESP32_PIN_SCHEMA = cv.All(
{
cv.GenerateID(): cv.declare_id(ESP32InternalGPIOPin),
cv.Required(CONF_NUMBER): validate_gpio_pin,
cv.Optional(CONF_MODE, default={}): cv.Schema(
{
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
cv.Optional(CONF_OPEN_DRAIN, default=False): cv.boolean,
cv.Optional(CONF_PULLUP, default=False): cv.boolean,
cv.Optional(CONF_PULLDOWN, default=False): cv.boolean,
}
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
cv.Optional(CONF_IGNORE_STRAPPING_WARNING, default=False): cv.boolean,
cv.Optional(CONF_DRIVE_STRENGTH, default="20mA"): cv.All(
cv.float_with_unit("current", "mA", optional_unit=True),
cv.enum(DRIVE_STRENGTHS),
),
},
pins.gpio_base_schema(ESP32InternalGPIOPin, validate_gpio_pin).extend(
{
cv.Optional(CONF_IGNORE_STRAPPING_WARNING, default=False): cv.boolean,
cv.Optional(CONF_DRIVE_STRENGTH, default="20mA"): cv.All(
cv.float_with_unit("current", "mA", optional_unit=True),
cv.enum(DRIVE_STRENGTHS),
),
}
),
validate_supports,
)
@pins.PIN_SCHEMA_REGISTRY.register("esp32", ESP32_PIN_SCHEMA)
@pins.PIN_SCHEMA_REGISTRY.register(PLATFORM_ESP32, ESP32_PIN_SCHEMA)
async def esp32_pin_to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
num = config[CONF_NUMBER]

View File

@ -25,6 +25,11 @@ AUTO_LOAD = ["psram"]
esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera")
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase)
ESP32CameraImageData = esp32_camera_ns.struct("CameraImageData")
# Triggers
ESP32CameraImageTrigger = esp32_camera_ns.class_(
"ESP32CameraImageTrigger", automation.Trigger.template()
)
ESP32CameraStreamStartTrigger = esp32_camera_ns.class_(
"ESP32CameraStreamStartTrigger",
automation.Trigger.template(),
@ -139,6 +144,7 @@ CONF_IDLE_FRAMERATE = "idle_framerate"
# stream trigger
CONF_ON_STREAM_START = "on_stream_start"
CONF_ON_STREAM_STOP = "on_stream_stop"
CONF_ON_IMAGE = "on_image"
camera_range_param = cv.int_range(min=-2, max=2)
@ -221,6 +227,11 @@ CONFIG_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
),
}
),
cv.Optional(CONF_ON_IMAGE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESP32CameraImageTrigger),
}
),
}
).extend(cv.COMPONENT_SCHEMA)
@ -289,3 +300,9 @@ async def to_code(config):
for conf in config.get(CONF_ON_STREAM_STOP, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_IMAGE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(ESP32CameraImageData, "image")], conf
)

View File

@ -335,8 +335,8 @@ void ESP32Camera::set_idle_update_interval(uint32_t idle_update_interval) {
}
/* ---------------- public API (specific) ---------------- */
void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f) {
this->new_image_callback_.add(std::move(f));
void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback) {
this->new_image_callback_.add(std::move(callback));
}
void ESP32Camera::add_stream_start_callback(std::function<void()> &&callback) {
this->stream_start_callback_.add(std::move(callback));

View File

@ -86,6 +86,11 @@ class CameraImage {
uint8_t requesters_;
};
struct CameraImageData {
uint8_t *data;
size_t length;
};
/* ---------------- CameraImageReader class ---------------- */
class CameraImageReader {
public:
@ -147,12 +152,12 @@ class ESP32Camera : public Component, public EntityBase {
void dump_config() override;
float get_setup_priority() const override;
/* public API (specific) */
void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f);
void start_stream(CameraRequester requester);
void stop_stream(CameraRequester requester);
void request_image(CameraRequester requester);
void update_camera_parameters();
void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback);
void add_stream_start_callback(std::function<void()> &&callback);
void add_stream_stop_callback(std::function<void()> &&callback);
@ -196,7 +201,7 @@ class ESP32Camera : public Component, public EntityBase {
uint8_t stream_requesters_{0};
QueueHandle_t framebuffer_get_queue_;
QueueHandle_t framebuffer_return_queue_;
CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_;
CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_{};
CallbackManager<void()> stream_start_callback_{};
CallbackManager<void()> stream_stop_callback_{};
@ -207,6 +212,18 @@ class ESP32Camera : public Component, public EntityBase {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
extern ESP32Camera *global_esp32_camera;
class ESP32CameraImageTrigger : public Trigger<CameraImageData> {
public:
explicit ESP32CameraImageTrigger(ESP32Camera *parent) {
parent->add_image_callback([this](const std::shared_ptr<esp32_camera::CameraImage> &image) {
CameraImageData camera_image_data{};
camera_image_data.length = image->get_data_length();
camera_image_data.data = image->get_data_buffer();
this->trigger(camera_image_data);
});
}
};
class ESP32CameraStreamStartTrigger : public Trigger<> {
public:
explicit ESP32CameraStreamStartTrigger(ESP32Camera *parent) {

View File

@ -12,6 +12,7 @@ from esphome.const import (
CONF_OUTPUT,
CONF_PULLDOWN,
CONF_PULLUP,
PLATFORM_ESP8266,
)
from esphome import pins
from esphome.core import CORE, coroutine_with_priority
@ -21,10 +22,8 @@ import esphome.codegen as cg
from . import boards
from .const import KEY_BOARD, KEY_ESP8266, KEY_PIN_INITIAL_STATES, esp8266_ns
_LOGGER = logging.getLogger(__name__)
ESP8266GPIOPin = esp8266_ns.class_("ESP8266GPIOPin", cg.InternalGPIOPin)
@ -124,6 +123,8 @@ def validate_supports(value):
(True, False, False, False, False),
# OUTPUT
(False, True, False, False, False),
# INPUT and OUTPUT, e.g. for i2c
(True, True, False, False, False),
# INPUT_PULLUP
(True, False, False, True, False),
# INPUT_PULLDOWN_16
@ -142,21 +143,11 @@ def validate_supports(value):
ESP8266_PIN_SCHEMA = cv.All(
{
cv.GenerateID(): cv.declare_id(ESP8266GPIOPin),
cv.Required(CONF_NUMBER): validate_gpio_pin,
cv.Optional(CONF_MODE, default={}): cv.Schema(
{
cv.Optional(CONF_ANALOG, default=False): cv.boolean,
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
cv.Optional(CONF_OPEN_DRAIN, default=False): cv.boolean,
cv.Optional(CONF_PULLUP, default=False): cv.boolean,
cv.Optional(CONF_PULLDOWN, default=False): cv.boolean,
}
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
},
pins.gpio_base_schema(
ESP8266GPIOPin,
validate_gpio_pin,
modes=pins.GPIO_STANDARD_MODES + (CONF_ANALOG,),
),
validate_supports,
)
@ -167,7 +158,7 @@ class PinInitialState:
level: int = 255
@pins.PIN_SCHEMA_REGISTRY.register("esp8266", ESP8266_PIN_SCHEMA)
@pins.PIN_SCHEMA_REGISTRY.register(PLATFORM_ESP8266, ESP8266_PIN_SCHEMA)
async def esp8266_pin_to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
num = config[CONF_NUMBER]

View File

@ -18,6 +18,7 @@ from esphome.const import (
CONF_ON_SPEED_SET,
CONF_ON_TURN_OFF,
CONF_ON_TURN_ON,
CONF_ON_PRESET_SET,
CONF_TRIGGER_ID,
CONF_DIRECTION,
CONF_RESTORE_MODE,
@ -57,6 +58,9 @@ CycleSpeedAction = fan_ns.class_("CycleSpeedAction", automation.Action)
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
FanSpeedSetTrigger = fan_ns.class_("FanSpeedSetTrigger", automation.Trigger.template())
FanPresetSetTrigger = fan_ns.class_(
"FanPresetSetTrigger", automation.Trigger.template()
)
FanIsOnCondition = fan_ns.class_("FanIsOnCondition", automation.Condition.template())
FanIsOffCondition = fan_ns.class_("FanIsOffCondition", automation.Condition.template())
@ -101,9 +105,46 @@ FAN_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).exte
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanSpeedSetTrigger),
}
),
cv.Optional(CONF_ON_PRESET_SET): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(FanPresetSetTrigger),
}
),
}
)
_PRESET_MODES_SCHEMA = cv.All(
cv.ensure_list(cv.string_strict),
cv.Length(min=1),
)
def validate_preset_modes(value):
# Check against defined schema
value = _PRESET_MODES_SCHEMA(value)
# Ensure preset names are unique
errors = []
presets = set()
for i, preset in enumerate(value):
# If name does not exist yet add it
if preset not in presets:
presets.add(preset)
continue
# Otherwise it's an error
errors.append(
cv.Invalid(
f"Found duplicate preset name '{preset}'. Presets must have unique names.",
[i],
)
)
if errors:
raise cv.MultipleInvalid(errors)
return value
async def setup_fan_core_(var, config):
await setup_entity(var, config)
@ -154,6 +195,9 @@ async def setup_fan_core_(var, config):
for conf in config.get(CONF_ON_SPEED_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_PRESET_SET, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
async def register_fan(var, config):

View File

@ -165,5 +165,23 @@ class FanSpeedSetTrigger : public Trigger<> {
int last_speed_;
};
class FanPresetSetTrigger : public Trigger<> {
public:
FanPresetSetTrigger(Fan *state) {
state->add_on_state_callback([this, state]() {
auto preset_mode = state->preset_mode;
auto should_trigger = preset_mode != this->last_preset_mode_;
this->last_preset_mode_ = preset_mode;
if (should_trigger) {
this->trigger();
}
});
this->last_preset_mode_ = state->preset_mode;
}
protected:
std::string last_preset_mode_;
};
} // namespace fan
} // namespace esphome

View File

@ -32,9 +32,12 @@ void FanCall::perform() {
if (this->direction_.has_value()) {
ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(*this->direction_)));
}
if (!this->preset_mode_.empty()) {
ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode_.c_str());
}
this->parent_.control(*this);
}
void FanCall::validate_() {
auto traits = this->parent_.get_traits();
@ -62,6 +65,15 @@ void FanCall::validate_() {
ESP_LOGW(TAG, "'%s' - This fan does not support directions!", this->parent_.get_name().c_str());
this->direction_.reset();
}
if (!this->preset_mode_.empty()) {
const auto &preset_modes = traits.supported_preset_modes();
if (preset_modes.find(this->preset_mode_) == preset_modes.end()) {
ESP_LOGW(TAG, "'%s' - This fan does not support preset mode '%s'!", this->parent_.get_name().c_str(),
this->preset_mode_.c_str());
this->preset_mode_.clear();
}
}
}
FanCall FanRestoreState::to_call(Fan &fan) {
@ -70,6 +82,14 @@ FanCall FanRestoreState::to_call(Fan &fan) {
call.set_oscillating(this->oscillating);
call.set_speed(this->speed);
call.set_direction(this->direction);
if (fan.get_traits().supports_preset_modes()) {
// Use stored preset index to get preset name
const auto &preset_modes = fan.get_traits().supported_preset_modes();
if (this->preset_mode < preset_modes.size()) {
call.set_preset_mode(*std::next(preset_modes.begin(), this->preset_mode));
}
}
return call;
}
void FanRestoreState::apply(Fan &fan) {
@ -77,6 +97,14 @@ void FanRestoreState::apply(Fan &fan) {
fan.oscillating = this->oscillating;
fan.speed = this->speed;
fan.direction = this->direction;
if (fan.get_traits().supports_preset_modes()) {
// Use stored preset index to get preset name
const auto &preset_modes = fan.get_traits().supported_preset_modes();
if (this->preset_mode < preset_modes.size()) {
fan.preset_mode = *std::next(preset_modes.begin(), this->preset_mode);
}
}
fan.publish_state();
}
@ -100,7 +128,9 @@ void Fan::publish_state() {
if (traits.supports_direction()) {
ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(this->direction)));
}
if (traits.supports_preset_modes() && !this->preset_mode.empty()) {
ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode.c_str());
}
this->state_callback_.call();
this->save_state_();
}
@ -143,20 +173,36 @@ void Fan::save_state_() {
state.oscillating = this->oscillating;
state.speed = this->speed;
state.direction = this->direction;
if (this->get_traits().supports_preset_modes() && !this->preset_mode.empty()) {
const auto &preset_modes = this->get_traits().supported_preset_modes();
// Store index of current preset mode
auto preset_iterator = preset_modes.find(this->preset_mode);
if (preset_iterator != preset_modes.end())
state.preset_mode = std::distance(preset_modes.begin(), preset_iterator);
}
this->rtc_.save(&state);
}
void Fan::dump_traits_(const char *tag, const char *prefix) {
if (this->get_traits().supports_speed()) {
auto traits = this->get_traits();
if (traits.supports_speed()) {
ESP_LOGCONFIG(tag, "%s Speed: YES", prefix);
ESP_LOGCONFIG(tag, "%s Speed count: %d", prefix, this->get_traits().supported_speed_count());
ESP_LOGCONFIG(tag, "%s Speed count: %d", prefix, traits.supported_speed_count());
}
if (this->get_traits().supports_oscillation()) {
if (traits.supports_oscillation()) {
ESP_LOGCONFIG(tag, "%s Oscillation: YES", prefix);
}
if (this->get_traits().supports_direction()) {
if (traits.supports_direction()) {
ESP_LOGCONFIG(tag, "%s Direction: YES", prefix);
}
if (traits.supports_preset_modes()) {
ESP_LOGCONFIG(tag, "%s Supported presets:", prefix);
for (const std::string &s : traits.supported_preset_modes())
ESP_LOGCONFIG(tag, "%s - %s", prefix, s.c_str());
}
}
} // namespace fan

View File

@ -72,6 +72,11 @@ class FanCall {
return *this;
}
optional<FanDirection> get_direction() const { return this->direction_; }
FanCall &set_preset_mode(const std::string &preset_mode) {
this->preset_mode_ = preset_mode;
return *this;
}
std::string get_preset_mode() const { return this->preset_mode_; }
void perform();
@ -83,6 +88,7 @@ class FanCall {
optional<bool> oscillating_;
optional<int> speed_;
optional<FanDirection> direction_{};
std::string preset_mode_{};
};
struct FanRestoreState {
@ -90,6 +96,7 @@ struct FanRestoreState {
int speed;
bool oscillating;
FanDirection direction;
uint8_t preset_mode;
/// Convert this struct to a fan call that can be performed.
FanCall to_call(Fan &fan);
@ -107,6 +114,8 @@ class Fan : public EntityBase {
int speed{0};
/// The current direction of the fan
FanDirection direction{FanDirection::FORWARD};
// The current preset mode of the fan
std::string preset_mode{};
FanCall turn_on();
FanCall turn_off();

View File

@ -1,3 +1,6 @@
#include <set>
#include <utility>
#pragma once
namespace esphome {
@ -25,12 +28,19 @@ class FanTraits {
bool supports_direction() const { return this->direction_; }
/// Set whether this fan supports changing direction
void set_direction(bool direction) { this->direction_ = direction; }
/// Return the preset modes supported by the fan.
std::set<std::string> supported_preset_modes() const { return this->preset_modes_; }
/// Set the preset modes supported by the fan.
void set_supported_preset_modes(const std::set<std::string> &preset_modes) { this->preset_modes_ = preset_modes; }
/// Return if preset modes are supported
bool supports_preset_modes() const { return !this->preset_modes_.empty(); }
protected:
bool oscillation_{false};
bool speed_{false};
bool direction_{false};
int speed_count_{};
std::set<std::string> preset_modes_{};
};
} // namespace fan

View File

@ -67,13 +67,13 @@ def validate_pillow_installed(value):
except ImportError as err:
raise cv.Invalid(
"Please install the pillow python package to use this feature. "
'(pip install "pillow==10.0.1")'
'(pip install "pillow==10.1.0")'
) from err
if version.parse(PIL.__version__) != version.parse("10.0.1"):
if version.parse(PIL.__version__) != version.parse("10.1.0"):
raise cv.Invalid(
"Please update your pillow installation to 10.0.1. "
'(pip install "pillow==10.0.1")'
"Please update your pillow installation to 10.1.0. "
'(pip install "pillow==10.1.0")'
)
return value

View File

@ -0,0 +1,6 @@
import esphome.codegen as cg
CODEOWNERS = ["@clydebarrow"]
DEPENDENCIES = ["i2c"]
ft5x06_ns = cg.esphome_ns.namespace("ft5x06")

View File

@ -0,0 +1,26 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, touchscreen
from esphome.const import CONF_ID
from .. import ft5x06_ns
FT5x06ButtonListener = ft5x06_ns.class_("FT5x06ButtonListener")
FT5x06Touchscreen = ft5x06_ns.class_(
"FT5x06Touchscreen",
touchscreen.Touchscreen,
cg.Component,
i2c.I2CDevice,
)
CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(FT5x06Touchscreen),
}
).extend(i2c.i2c_device_schema(0x48))
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await i2c.register_i2c_device(var, config)
await touchscreen.register_touchscreen(var, config)

View File

@ -0,0 +1,124 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/touchscreen/touchscreen.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome {
namespace ft5x06 {
static const char *const TAG = "ft5x06.touchscreen";
enum VendorId {
FT5X06_ID_UNKNOWN = 0,
FT5X06_ID_1 = 0x51,
FT5X06_ID_2 = 0x11,
FT5X06_ID_3 = 0xCD,
};
enum FTCmd : uint8_t {
FT5X06_MODE_REG = 0x00,
FT5X06_ORIGIN_REG = 0x08,
FT5X06_RESOLUTION_REG = 0x0C,
FT5X06_VENDOR_ID_REG = 0xA8,
FT5X06_TD_STATUS = 0x02,
FT5X06_TOUCH_DATA = 0x03,
FT5X06_I_MODE = 0xA4,
FT5X06_TOUCH_MAX = 0x4C,
};
enum FTMode : uint8_t {
FT5X06_OP_MODE = 0,
FT5X06_SYSINFO_MODE = 0x10,
FT5X06_TEST_MODE = 0x40,
};
static const size_t MAX_TOUCHES = 5; // max number of possible touches reported
class FT5x06Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
public:
void setup() override {
esph_log_config(TAG, "Setting up FT5x06 Touchscreen...");
// wait 200ms after reset.
this->set_timeout(200, [this] { this->continue_setup_(); });
}
void continue_setup_(void) {
uint8_t data[4];
if (!this->set_mode_(FT5X06_OP_MODE))
return;
if (!this->err_check_(this->read_register(FT5X06_VENDOR_ID_REG, data, 1), "Read Vendor ID"))
return;
switch (data[0]) {
case FT5X06_ID_1:
case FT5X06_ID_2:
case FT5X06_ID_3:
this->vendor_id_ = (VendorId) data[0];
esph_log_d(TAG, "Read vendor ID 0x%X", data[0]);
break;
default:
esph_log_e(TAG, "Unknown vendor ID 0x%X", data[0]);
this->mark_failed();
return;
}
// reading the chip registers to get max x/y does not seem to work.
this->x_raw_max_ = this->display_->get_width();
this->y_raw_max_ = this->display_->get_height();
esph_log_config(TAG, "FT5x06 Touchscreen setup complete");
}
void update_touches() override {
uint8_t touch_cnt;
uint8_t data[MAX_TOUCHES][6];
if (!this->read_byte(FT5X06_TD_STATUS, &touch_cnt) || touch_cnt > MAX_TOUCHES) {
esph_log_w(TAG, "Failed to read status");
return;
}
if (touch_cnt == 0)
return;
if (!this->read_bytes(FT5X06_TOUCH_DATA, (uint8_t *) data, touch_cnt * 6)) {
esph_log_w(TAG, "Failed to read touch data");
return;
}
for (uint8_t i = 0; i != touch_cnt; i++) {
uint8_t status = data[i][0] >> 6;
uint8_t id = data[i][2] >> 3;
uint16_t x = encode_uint16(data[i][0] & 0x0F, data[i][1]);
uint16_t y = encode_uint16(data[i][2] & 0xF, data[i][3]);
esph_log_d(TAG, "Read %X status, id: %d, pos %d/%d", status, id, x, y);
if (status == 0 || status == 2) {
this->set_raw_touch_position_(id, x, y);
}
}
}
void dump_config() override {
esph_log_config(TAG, "FT5x06 Touchscreen:");
esph_log_config(TAG, " Address: 0x%02X", this->address_);
esph_log_config(TAG, " Vendor ID: 0x%X", (int) this->vendor_id_);
}
protected:
bool err_check_(i2c::ErrorCode err, const char *msg) {
if (err != i2c::ERROR_OK) {
this->mark_failed();
esph_log_e(TAG, "%s failed - err 0x%X", msg, err);
return false;
}
return true;
}
bool set_mode_(FTMode mode) {
return this->err_check_(this->write_register(FT5X06_MODE_REG, (uint8_t *) &mode, 1), "Set mode");
}
VendorId vendor_id_{FT5X06_ID_UNKNOWN};
};
} // namespace ft5x06
} // namespace esphome

View File

@ -0,0 +1 @@
CODEOWNERS = ["@gpambrozio"]

View File

@ -0,0 +1,99 @@
/**************************************************************************/
/*!
Author: Gustavo Ambrozio
Based on work by: Atsushi Sasaki (https://github.com/aselectroworks/Arduino-FT6336U)
*/
/**************************************************************************/
#include "ft63x6.h"
#include "esphome/core/log.h"
// Registers
// Reference: https://focuslcds.com/content/FT6236.pdf
namespace esphome {
namespace ft63x6 {
static const uint8_t FT63X6_ADDR_TOUCH_COUNT = 0x02;
static const uint8_t FT63X6_ADDR_TOUCH1_ID = 0x05;
static const uint8_t FT63X6_ADDR_TOUCH1_X = 0x03;
static const uint8_t FT63X6_ADDR_TOUCH1_Y = 0x05;
static const uint8_t FT63X6_ADDR_TOUCH2_ID = 0x0B;
static const uint8_t FT63X6_ADDR_TOUCH2_X = 0x09;
static const uint8_t FT63X6_ADDR_TOUCH2_Y = 0x0B;
static const char *const TAG = "FT63X6Touchscreen";
void FT63X6Touchscreen::setup() {
ESP_LOGCONFIG(TAG, "Setting up FT63X6Touchscreen Touchscreen...");
if (this->interrupt_pin_ != nullptr) {
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->interrupt_pin_->setup();
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
}
if (this->reset_pin_ != nullptr) {
this->reset_pin_->setup();
}
this->hard_reset_();
// Get touch resolution
this->x_raw_max_ = 320;
this->y_raw_max_ = 480;
}
void FT63X6Touchscreen::update_touches() {
int touch_count = this->read_touch_count_();
if (touch_count == 0) {
return;
}
uint8_t touch_id = this->read_touch_id_(FT63X6_ADDR_TOUCH1_ID); // id1 = 0 or 1
int16_t x = this->read_touch_coordinate_(FT63X6_ADDR_TOUCH1_X);
int16_t y = this->read_touch_coordinate_(FT63X6_ADDR_TOUCH1_Y);
this->set_raw_touch_position_(touch_id, x, y);
if (touch_count >= 2) {
touch_id = this->read_touch_id_(FT63X6_ADDR_TOUCH2_ID); // id2 = 0 or 1(~id1 & 0x01)
x = this->read_touch_coordinate_(FT63X6_ADDR_TOUCH2_X);
y = this->read_touch_coordinate_(FT63X6_ADDR_TOUCH2_Y);
this->set_raw_touch_position_(touch_id, x, y);
}
}
void FT63X6Touchscreen::hard_reset_() {
if (this->reset_pin_ != nullptr) {
this->reset_pin_->digital_write(false);
delay(10);
this->reset_pin_->digital_write(true);
}
}
void FT63X6Touchscreen::dump_config() {
ESP_LOGCONFIG(TAG, "FT63X6 Touchscreen:");
LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
}
uint8_t FT63X6Touchscreen::read_touch_count_() { return this->read_byte_(FT63X6_ADDR_TOUCH_COUNT); }
// Touch functions
uint16_t FT63X6Touchscreen::read_touch_coordinate_(uint8_t coordinate) {
uint8_t read_buf[2];
read_buf[0] = this->read_byte_(coordinate);
read_buf[1] = this->read_byte_(coordinate + 1);
return ((read_buf[0] & 0x0f) << 8) | read_buf[1];
}
uint8_t FT63X6Touchscreen::read_touch_id_(uint8_t id_address) { return this->read_byte_(id_address) >> 4; }
uint8_t FT63X6Touchscreen::read_byte_(uint8_t addr) {
uint8_t byte = 0;
this->read_byte(addr, &byte);
return byte;
}
} // namespace ft63x6
} // namespace esphome

View File

@ -0,0 +1,41 @@
/**************************************************************************/
/*!
Author: Gustavo Ambrozio
Based on work by: Atsushi Sasaki (https://github.com/aselectroworks/Arduino-FT6336U)
*/
/**************************************************************************/
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/touchscreen/touchscreen.h"
#include "esphome/core/component.h"
namespace esphome {
namespace ft63x6 {
using namespace touchscreen;
class FT63X6Touchscreen : public Touchscreen, public i2c::I2CDevice {
public:
void setup() override;
void dump_config() override;
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
protected:
void hard_reset_();
uint8_t read_byte_(uint8_t addr);
void update_touches() override;
InternalGPIOPin *interrupt_pin_{nullptr};
GPIOPin *reset_pin_{nullptr};
uint8_t read_touch_count_();
uint16_t read_touch_coordinate_(uint8_t coordinate);
uint8_t read_touch_id_(uint8_t id_address);
};
} // namespace ft63x6
} // namespace esphome

View File

@ -0,0 +1,44 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import i2c, touchscreen
from esphome.const import CONF_ID, CONF_INTERRUPT_PIN, CONF_RESET_PIN
CODEOWNERS = ["@gpambrozio"]
DEPENDENCIES = ["i2c"]
ft6336u_ns = cg.esphome_ns.namespace("ft63x6")
FT63X6Touchscreen = ft6336u_ns.class_(
"FT63X6Touchscreen",
touchscreen.Touchscreen,
i2c.I2CDevice,
)
CONF_FT63X6_ID = "ft63x6_id"
CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(FT63X6Touchscreen),
cv.Optional(CONF_INTERRUPT_PIN): cv.All(
pins.internal_gpio_input_pin_schema
),
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
}
).extend(i2c.i2c_device_schema(0x38))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
if interrupt_pin_config := config.get(CONF_INTERRUPT_PIN):
interrupt_pin = await cg.gpio_pin_expression(interrupt_pin_config)
cg.add(var.set_interrupt_pin(interrupt_pin))
if reset_pin_config := config.get(CONF_RESET_PIN):
reset_pin = await cg.gpio_pin_expression(reset_pin_config)
cg.add(var.set_reset_pin(reset_pin))

View File

@ -0,0 +1,96 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import display, font, color
from esphome.const import CONF_ID, CONF_TRIGGER_ID
from esphome import automation, core
from esphome.components.display_menu_base import (
DISPLAY_MENU_BASE_SCHEMA,
DisplayMenuComponent,
display_menu_to_code,
)
CONF_DISPLAY = "display"
CONF_FONT = "font"
CONF_MENU_ITEM_VALUE = "menu_item_value"
CONF_FOREGROUND_COLOR = "foreground_color"
CONF_BACKGROUND_COLOR = "background_color"
CONF_ON_REDRAW = "on_redraw"
graphical_display_menu_ns = cg.esphome_ns.namespace("graphical_display_menu")
GraphicalDisplayMenu = graphical_display_menu_ns.class_(
"GraphicalDisplayMenu", DisplayMenuComponent
)
GraphicalDisplayMenuConstPtr = GraphicalDisplayMenu.operator("ptr").operator("const")
MenuItemValueArguments = graphical_display_menu_ns.struct("MenuItemValueArguments")
MenuItemValueArgumentsConstPtr = MenuItemValueArguments.operator("ptr").operator(
"const"
)
GraphicalDisplayMenuOnRedrawTrigger = graphical_display_menu_ns.class_(
"GraphicalDisplayMenuOnRedrawTrigger", automation.Trigger
)
CODEOWNERS = ["@MrMDavidson"]
AUTO_LOAD = ["display_menu_base"]
CONFIG_SCHEMA = DISPLAY_MENU_BASE_SCHEMA.extend(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(GraphicalDisplayMenu),
cv.Optional(CONF_DISPLAY): cv.use_id(display.DisplayBuffer),
cv.Required(CONF_FONT): cv.use_id(font.Font),
cv.Optional(CONF_MENU_ITEM_VALUE): cv.templatable(cv.string),
cv.Optional(CONF_FOREGROUND_COLOR): cv.use_id(color.ColorStruct),
cv.Optional(CONF_BACKGROUND_COLOR): cv.use_id(color.ColorStruct),
cv.Optional(CONF_ON_REDRAW): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
GraphicalDisplayMenuOnRedrawTrigger
)
}
),
}
)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
if display_config := config.get(CONF_DISPLAY):
drawing_display = await cg.get_variable(display_config)
cg.add(var.set_display(drawing_display))
menu_font = await cg.get_variable(config[CONF_FONT])
cg.add(var.set_font(menu_font))
if (menu_item_value_config := config.get(CONF_MENU_ITEM_VALUE, None)) is not None:
if isinstance(menu_item_value_config, core.Lambda):
template_ = await cg.templatable(
menu_item_value_config,
[(MenuItemValueArgumentsConstPtr, "it")],
cg.std_string,
)
cg.add(var.set_menu_item_value(template_))
else:
cg.add(var.set_menu_item_value(menu_item_value_config))
if foreground_color_config := config.get(CONF_FOREGROUND_COLOR):
foreground_color = await cg.get_variable(foreground_color_config)
cg.add(var.set_foreground_color(foreground_color))
if background_color_config := config.get(CONF_BACKGROUND_COLOR):
background_color = await cg.get_variable(background_color_config)
cg.add(var.set_background_color(background_color))
for conf in config.get(CONF_ON_REDRAW, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger, [(GraphicalDisplayMenuConstPtr, "it")], conf
)
await display_menu_to_code(var, config)
cg.add_define("USE_GRAPHICAL_DISPLAY_MENU")

View File

@ -0,0 +1,243 @@
#include "graphical_display_menu.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <cstdlib>
#include "esphome/components/display/display.h"
namespace esphome {
namespace graphical_display_menu {
static const char *const TAG = "graphical_display_menu";
void GraphicalDisplayMenu::setup() {
if (this->display_ != nullptr) {
display::display_writer_t writer = [this](display::Display &it) { this->draw_menu(); };
this->display_page_ = make_unique<display::DisplayPage>(writer);
}
if (!this->menu_item_value_.has_value()) {
this->menu_item_value_ = [](const MenuItemValueArguments *it) {
std::string label = " ";
if (it->is_item_selected && it->is_menu_editing) {
label.append(">");
label.append(it->item->get_value_text());
label.append("<");
} else {
label.append("(");
label.append(it->item->get_value_text());
label.append(")");
}
return label;
};
}
display_menu_base::DisplayMenuComponent::setup();
}
void GraphicalDisplayMenu::dump_config() {
ESP_LOGCONFIG(TAG, "Graphical Display Menu");
ESP_LOGCONFIG(TAG, "Has Display: %s", YESNO(this->display_ != nullptr));
ESP_LOGCONFIG(TAG, "Popup Mode: %s", YESNO(this->display_ != nullptr));
ESP_LOGCONFIG(TAG, "Advanced Drawing Mode: %s", YESNO(this->display_ == nullptr));
ESP_LOGCONFIG(TAG, "Has Font: %s", YESNO(this->font_ != nullptr));
ESP_LOGCONFIG(TAG, "Mode: %s", this->mode_ == display_menu_base::MENU_MODE_ROTARY ? "Rotary" : "Joystick");
ESP_LOGCONFIG(TAG, "Active: %s", YESNO(this->active_));
ESP_LOGCONFIG(TAG, "Menu items:");
for (size_t i = 0; i < this->displayed_item_->items_size(); i++) {
auto *item = this->displayed_item_->get_item(i);
ESP_LOGCONFIG(TAG, " %i: %s (Type: %s, Immediate Edit: %s)", i, item->get_text().c_str(),
LOG_STR_ARG(display_menu_base::menu_item_type_to_string(item->get_type())),
YESNO(item->get_immediate_edit()));
}
}
void GraphicalDisplayMenu::set_display(display::Display *display) { this->display_ = display; }
void GraphicalDisplayMenu::set_font(display::BaseFont *font) { this->font_ = font; }
void GraphicalDisplayMenu::set_foreground_color(Color foreground_color) { this->foreground_color_ = foreground_color; }
void GraphicalDisplayMenu::set_background_color(Color background_color) { this->background_color_ = background_color; }
void GraphicalDisplayMenu::on_before_show() {
if (this->display_ != nullptr) {
this->previous_display_page_ = this->display_->get_active_page();
this->display_->show_page(this->display_page_.get());
this->display_->clear();
} else {
this->update();
}
}
void GraphicalDisplayMenu::on_before_hide() {
if (this->previous_display_page_ != nullptr) {
this->display_->show_page((display::DisplayPage *) this->previous_display_page_);
this->display_->clear();
this->update();
this->previous_display_page_ = nullptr;
} else {
this->update();
}
}
void GraphicalDisplayMenu::draw_and_update() {
this->update();
// If we're in advanced drawing mode we won't have a display and will instead require the update callback to do
// our drawing
if (this->display_ != nullptr) {
draw_menu();
}
}
void GraphicalDisplayMenu::draw_menu() {
if (this->display_ == nullptr) {
ESP_LOGE(TAG, "draw_menu() called without a display_. This is only available when using the menu in pop up mode");
return;
}
display::Rect bounds(0, 0, this->display_->get_width(), this->display_->get_height());
this->draw_menu_internal_(this->display_, &bounds);
}
void GraphicalDisplayMenu::draw(display::Display *display, const display::Rect *bounds) {
this->draw_menu_internal_(display, bounds);
}
void GraphicalDisplayMenu::draw_menu_internal_(display::Display *display, const display::Rect *bounds) {
int total_height = 0;
int y_padding = 2;
bool scroll_menu_items = false;
std::vector<display::Rect> menu_dimensions;
int number_items_fit_to_screen = 0;
const int max_item_index = this->displayed_item_->items_size() - 1;
for (size_t i = 0; i <= max_item_index; i++) {
const auto *item = this->displayed_item_->get_item(i);
const bool selected = i == this->cursor_index_;
const display::Rect item_dimensions = this->measure_item(display, item, bounds, selected);
menu_dimensions.push_back(item_dimensions);
total_height += item_dimensions.h + (i == 0 ? 0 : y_padding);
if (total_height <= bounds->h) {
number_items_fit_to_screen++;
} else {
// Scroll the display if the selected item or the item immediately after it overflows
if ((selected) || (i == this->cursor_index_ + 1)) {
scroll_menu_items = true;
}
}
}
// Determine what items to draw
int first_item_index = 0;
int last_item_index = max_item_index;
if (number_items_fit_to_screen <= 1) {
// If only one item can fit to the bounds draw the current cursor item
last_item_index = std::min(last_item_index, this->cursor_index_ + 1);
first_item_index = this->cursor_index_;
} else {
if (scroll_menu_items) {
// Attempt to draw the item after the current item (+1 for equality check in the draw loop)
last_item_index = std::min(last_item_index, this->cursor_index_ + 1);
// Go back through the measurements to determine how many prior items we can fit
int height_left_to_use = bounds->h;
for (int i = last_item_index; i >= 0; i--) {
const display::Rect item_dimensions = menu_dimensions[i];
height_left_to_use -= (item_dimensions.h + y_padding);
if (height_left_to_use <= 0) {
// Ran out of space - this is our first item to draw
first_item_index = i;
break;
}
}
const int items_to_draw = last_item_index - first_item_index;
// Dont't draw last item partially if it is the selected item
if ((this->cursor_index_ == last_item_index) && (number_items_fit_to_screen <= items_to_draw) &&
(first_item_index < max_item_index)) {
first_item_index++;
}
}
}
// Render the items into the view port
display->start_clipping(*bounds);
int y_offset = bounds->y;
for (size_t i = first_item_index; i <= last_item_index; i++) {
const auto *item = this->displayed_item_->get_item(i);
const bool selected = i == this->cursor_index_;
display::Rect dimensions = menu_dimensions[i];
dimensions.y = y_offset;
dimensions.x = bounds->x;
this->draw_item(display, item, &dimensions, selected);
y_offset = dimensions.y + dimensions.h + y_padding;
}
display->end_clipping();
}
display::Rect GraphicalDisplayMenu::measure_item(display::Display *display, const display_menu_base::MenuItem *item,
const display::Rect *bounds, const bool selected) {
display::Rect dimensions(0, 0, 0, 0);
if (selected) {
// TODO: Support selection glyph
dimensions.w += 0;
dimensions.h += 0;
}
std::string label = item->get_text();
if (item->has_value()) {
// Append to label
MenuItemValueArguments args(item, selected, this->editing_);
label.append(this->menu_item_value_.value(&args));
}
int x1;
int y1;
int width;
int height;
display->get_text_bounds(0, 0, label.c_str(), this->font_, display::TextAlign::TOP_LEFT, &x1, &y1, &width, &height);
dimensions.w = std::min((int16_t) width, bounds->w);
dimensions.h = std::min((int16_t) height, bounds->h);
return dimensions;
}
inline void GraphicalDisplayMenu::draw_item(display::Display *display, const display_menu_base::MenuItem *item,
const display::Rect *bounds, const bool selected) {
const auto background_color = selected ? this->foreground_color_ : this->background_color_;
const auto foreground_color = selected ? this->background_color_ : this->foreground_color_;
// int background_width = std::max(bounds->width, available_width);
int background_width = bounds->w;
if (selected) {
display->filled_rectangle(bounds->x, bounds->y, background_width, bounds->h, background_color);
}
std::string label = item->get_text();
if (item->has_value()) {
MenuItemValueArguments args(item, selected, this->editing_);
label.append(this->menu_item_value_.value(&args));
}
display->print(bounds->x, bounds->y, this->font_, foreground_color, display::TextAlign::TOP_LEFT, label.c_str());
}
void GraphicalDisplayMenu::draw_item(const display_menu_base::MenuItem *item, const uint8_t row, const bool selected) {
ESP_LOGE(TAG, "draw_item(MenuItem *item, uint8_t row, bool selected) called. The graphical_display_menu specific "
"draw_item should be called.");
}
void GraphicalDisplayMenu::update() { this->on_redraw_callbacks_.call(); }
} // namespace graphical_display_menu
} // namespace esphome

View File

@ -0,0 +1,84 @@
#pragma once
#include "esphome/core/color.h"
#include "esphome/components/display_menu_base/display_menu_base.h"
#include "esphome/components/display_menu_base/menu_item.h"
#include "esphome/core/automation.h"
#include <cstdlib>
namespace esphome {
// forward declare from display namespace
namespace display {
class Display;
class DisplayPage;
class BaseFont;
class Rect;
} // namespace display
namespace graphical_display_menu {
const Color COLOR_ON(255, 255, 255, 255);
const Color COLOR_OFF(0, 0, 0, 0);
struct MenuItemValueArguments {
MenuItemValueArguments(const display_menu_base::MenuItem *item, bool is_item_selected, bool is_menu_editing) {
this->item = item;
this->is_item_selected = is_item_selected;
this->is_menu_editing = is_menu_editing;
}
const display_menu_base::MenuItem *item;
bool is_item_selected;
bool is_menu_editing;
};
class GraphicalDisplayMenu : public display_menu_base::DisplayMenuComponent {
public:
void setup() override;
void dump_config() override;
void set_display(display::Display *display);
void set_font(display::BaseFont *font);
template<typename V> void set_menu_item_value(V menu_item_value) { this->menu_item_value_ = menu_item_value; }
void set_foreground_color(Color foreground_color);
void set_background_color(Color background_color);
void add_on_redraw_callback(std::function<void()> &&cb) { this->on_redraw_callbacks_.add(std::move(cb)); }
void draw(display::Display *display, const display::Rect *bounds);
protected:
void draw_and_update() override;
void draw_menu() override;
void draw_menu_internal_(display::Display *display, const display::Rect *bounds);
void draw_item(const display_menu_base::MenuItem *item, uint8_t row, bool selected) override;
virtual display::Rect measure_item(display::Display *display, const display_menu_base::MenuItem *item,
const display::Rect *bounds, bool selected);
virtual void draw_item(display::Display *display, const display_menu_base::MenuItem *item,
const display::Rect *bounds, bool selected);
void update() override;
void on_before_show() override;
void on_before_hide() override;
std::unique_ptr<display::DisplayPage> display_page_{nullptr};
const display::DisplayPage *previous_display_page_{nullptr};
display::Display *display_{nullptr};
display::BaseFont *font_{nullptr};
TemplatableValue<std::string, const MenuItemValueArguments *> menu_item_value_;
Color foreground_color_{COLOR_ON};
Color background_color_{COLOR_OFF};
CallbackManager<void()> on_redraw_callbacks_{};
};
class GraphicalDisplayMenuOnRedrawTrigger : public Trigger<const GraphicalDisplayMenu *> {
public:
explicit GraphicalDisplayMenuOnRedrawTrigger(GraphicalDisplayMenu *parent) {
parent->add_on_redraw_callback([this, parent]() { this->trigger(parent); });
}
};
} // namespace graphical_display_menu
} // namespace esphome

View File

@ -0,0 +1,6 @@
import esphome.codegen as cg
CODEOWNERS = ["@jesserockz", "@clydebarrow"]
DEPENDENCIES = ["i2c"]
gt911_ns = cg.esphome_ns.namespace("gt911")

View File

@ -0,0 +1,31 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_INDEX
from .. import gt911_ns
from ..touchscreen import GT911Touchscreen, GT911ButtonListener
CONF_GT911_ID = "gt911_id"
GT911Button = gt911_ns.class_(
"GT911Button",
binary_sensor.BinarySensor,
cg.Component,
GT911ButtonListener,
cg.Parented.template(GT911Touchscreen),
)
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(GT911Button).extend(
{
cv.GenerateID(CONF_GT911_ID): cv.use_id(GT911Touchscreen),
cv.Optional(CONF_INDEX, default=0): cv.int_range(min=0, max=3),
}
)
async def to_code(config):
var = await binary_sensor.new_binary_sensor(config)
await cg.register_component(var, config)
await cg.register_parented(var, config[CONF_GT911_ID])
cg.add(var.set_index(config[CONF_INDEX]))

View File

@ -0,0 +1,27 @@
#include "gt911_button.h"
#include "esphome/core/log.h"
namespace esphome {
namespace gt911 {
static const char *const TAG = "GT911.binary_sensor";
void GT911Button::setup() {
this->parent_->register_button_listener(this);
this->publish_initial_state(false);
}
void GT911Button::dump_config() {
LOG_BINARY_SENSOR("", "GT911 Button", this);
ESP_LOGCONFIG(TAG, " Index: %u", this->index_);
}
void GT911Button::update_button(uint8_t index, bool state) {
if (index != this->index_)
return;
this->publish_state(state);
}
} // namespace gt911
} // namespace esphome

View File

@ -0,0 +1,28 @@
#pragma once
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/gt911/touchscreen/gt911_touchscreen.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace gt911 {
class GT911Button : public binary_sensor::BinarySensor,
public Component,
public GT911ButtonListener,
public Parented<GT911Touchscreen> {
public:
void setup() override;
void dump_config() override;
void set_index(uint8_t index) { this->index_ = index; }
void update_button(uint8_t index, bool state) override;
protected:
uint8_t index_;
};
} // namespace gt911
} // namespace esphome

View File

@ -0,0 +1,31 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import i2c, touchscreen
from esphome.const import CONF_INTERRUPT_PIN, CONF_ID
from .. import gt911_ns
GT911ButtonListener = gt911_ns.class_("GT911ButtonListener")
GT911Touchscreen = gt911_ns.class_(
"GT911Touchscreen",
touchscreen.Touchscreen,
i2c.I2CDevice,
)
CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(GT911Touchscreen),
cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
}
).extend(i2c.i2c_device_schema(0x5D))
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
if interrupt_pin := config.get(CONF_INTERRUPT_PIN):
cg.add(var.set_interrupt_pin(await cg.gpio_pin_expression(interrupt_pin)))

View File

@ -0,0 +1,111 @@
#include "gt911_touchscreen.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
namespace gt911 {
static const char *const TAG = "gt911.touchscreen";
static const uint8_t GET_TOUCH_STATE[2] = {0x81, 0x4E};
static const uint8_t CLEAR_TOUCH_STATE[3] = {0x81, 0x4E, 0x00};
static const uint8_t GET_TOUCHES[2] = {0x81, 0x4F};
static const uint8_t GET_SWITCHES[2] = {0x80, 0x4D};
static const uint8_t GET_MAX_VALUES[2] = {0x80, 0x48};
static const size_t MAX_TOUCHES = 5; // max number of possible touches reported
#define ERROR_CHECK(err) \
if ((err) != i2c::ERROR_OK) { \
ESP_LOGE(TAG, "Failed to communicate!"); \
this->status_set_warning(); \
return; \
}
void GT911Touchscreen::setup() {
i2c::ErrorCode err;
ESP_LOGCONFIG(TAG, "Setting up GT911 Touchscreen...");
// check the configuration of the int line.
uint8_t data[4];
err = this->write(GET_SWITCHES, 2);
if (err == i2c::ERROR_OK) {
err = this->read(data, 1);
if (err == i2c::ERROR_OK) {
ESP_LOGD(TAG, "Read from switches: 0x%02X", data[0]);
if (this->interrupt_pin_ != nullptr) {
// datasheet says NOT to use pullup/down on the int line.
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT);
this->interrupt_pin_->setup();
this->attach_interrupt_(this->interrupt_pin_,
(data[0] & 1) ? gpio::INTERRUPT_FALLING_EDGE : gpio::INTERRUPT_RISING_EDGE);
}
}
}
if (err == i2c::ERROR_OK) {
err = this->write(GET_MAX_VALUES, 2);
if (err == i2c::ERROR_OK) {
err = this->read(data, sizeof(data));
if (err == i2c::ERROR_OK) {
this->x_raw_max_ = encode_uint16(data[1], data[0]);
this->y_raw_max_ = encode_uint16(data[3], data[2]);
esph_log_d(TAG, "Read max_x/max_y %d/%d", this->x_raw_max_, this->y_raw_max_);
}
}
}
if (err != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to communicate!");
this->mark_failed();
return;
}
ESP_LOGCONFIG(TAG, "GT911 Touchscreen setup complete");
}
void GT911Touchscreen::update_touches() {
i2c::ErrorCode err;
uint8_t touch_state = 0;
uint8_t data[MAX_TOUCHES + 1][8]; // 8 bytes each for each point, plus extra space for the key byte
err = this->write(GET_TOUCH_STATE, sizeof(GET_TOUCH_STATE), false);
ERROR_CHECK(err);
err = this->read(&touch_state, 1);
ERROR_CHECK(err);
this->write(CLEAR_TOUCH_STATE, sizeof(CLEAR_TOUCH_STATE));
uint8_t num_of_touches = touch_state & 0x07;
if ((touch_state & 0x80) == 0 || num_of_touches > MAX_TOUCHES) {
this->skip_update_ = true; // skip send touch events, touchscreen is not ready yet.
return;
}
if (num_of_touches == 0)
return;
err = this->write(GET_TOUCHES, sizeof(GET_TOUCHES), false);
ERROR_CHECK(err);
// num_of_touches is guaranteed to be 0..5. Also read the key data
err = this->read(data[0], sizeof(data[0]) * num_of_touches + 1);
ERROR_CHECK(err);
for (uint8_t i = 0; i != num_of_touches; i++) {
uint16_t id = data[i][0];
uint16_t x = encode_uint16(data[i][2], data[i][1]);
uint16_t y = encode_uint16(data[i][4], data[i][3]);
this->set_raw_touch_position_(id, x, y);
}
auto keys = data[num_of_touches][0];
for (size_t i = 0; i != 4; i++) {
for (auto *listener : this->button_listeners_)
listener->update_button(i, (keys & (1 << i)) != 0);
}
}
void GT911Touchscreen::dump_config() {
ESP_LOGCONFIG(TAG, "GT911 Touchscreen:");
LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
}
} // namespace gt911
} // namespace esphome

View File

@ -0,0 +1,32 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/touchscreen/touchscreen.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace gt911 {
class GT911ButtonListener {
public:
virtual void update_button(uint8_t index, bool state) = 0;
};
class GT911Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
public:
void setup() override;
void dump_config() override;
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
void register_button_listener(GT911ButtonListener *listener) { this->button_listeners_.push_back(listener); }
protected:
void update_touches() override;
InternalGPIOPin *interrupt_pin_{};
std::vector<GT911ButtonListener *> button_listeners_;
};
} // namespace gt911
} // namespace esphome

View File

@ -38,16 +38,20 @@ PROTOCOL_MIN_TEMPERATURE = 16.0
PROTOCOL_MAX_TEMPERATURE = 30.0
PROTOCOL_TARGET_TEMPERATURE_STEP = 1.0
PROTOCOL_CURRENT_TEMPERATURE_STEP = 0.5
PROTOCOL_CONTROL_PACKET_SIZE = 10
CODEOWNERS = ["@paveldn"]
AUTO_LOAD = ["sensor"]
DEPENDENCIES = ["climate", "uart"]
CONF_WIFI_SIGNAL = "wifi_signal"
CONF_ALTERNATIVE_SWING_CONTROL = "alternative_swing_control"
CONF_ANSWER_TIMEOUT = "answer_timeout"
CONF_CONTROL_METHOD = "control_method"
CONF_CONTROL_PACKET_SIZE = "control_packet_size"
CONF_DISPLAY = "display"
CONF_HORIZONTAL_AIRFLOW = "horizontal_airflow"
CONF_OUTDOOR_TEMPERATURE = "outdoor_temperature"
CONF_VERTICAL_AIRFLOW = "vertical_airflow"
CONF_HORIZONTAL_AIRFLOW = "horizontal_airflow"
CONF_WIFI_SIGNAL = "wifi_signal"
PROTOCOL_HON = "HON"
PROTOCOL_SMARTAIR2 = "SMARTAIR2"
@ -107,6 +111,13 @@ SUPPORTED_CLIMATE_PRESETS_HON_OPTIONS = {
"SLEEP": ClimatePreset.CLIMATE_PRESET_SLEEP,
}
HonControlMethod = haier_ns.enum("HonControlMethod", True)
SUPPORTED_HON_CONTROL_METHODS = {
"MONITOR_ONLY": HonControlMethod.MONITOR_ONLY,
"SET_GROUP_PARAMETERS": HonControlMethod.SET_GROUP_PARAMETERS,
"SET_SINGLE_PARAMETER": HonControlMethod.SET_SINGLE_PARAMETER,
}
def validate_visual(config):
if CONF_VISUAL in config:
@ -184,6 +195,9 @@ CONFIG_SCHEMA = cv.All(
PROTOCOL_SMARTAIR2: BASE_CONFIG_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(Smartair2Climate),
cv.Optional(
CONF_ALTERNATIVE_SWING_CONTROL, default=False
): cv.boolean,
cv.Optional(
CONF_SUPPORTED_PRESETS,
default=list(
@ -197,7 +211,15 @@ CONFIG_SCHEMA = cv.All(
PROTOCOL_HON: BASE_CONFIG_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(HonClimate),
cv.Optional(
CONF_CONTROL_METHOD, default="SET_GROUP_PARAMETERS"
): cv.ensure_list(
cv.enum(SUPPORTED_HON_CONTROL_METHODS, upper=True)
),
cv.Optional(CONF_BEEPER, default=True): cv.boolean,
cv.Optional(
CONF_CONTROL_PACKET_SIZE, default=PROTOCOL_CONTROL_PACKET_SIZE
): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50),
cv.Optional(
CONF_SUPPORTED_PRESETS,
default=list(SUPPORTED_CLIMATE_PRESETS_HON_OPTIONS.keys()),
@ -408,6 +430,8 @@ async def to_code(config):
await climate.register_climate(var, config)
cg.add(var.set_send_wifi(config[CONF_WIFI_SIGNAL]))
if CONF_CONTROL_METHOD in config:
cg.add(var.set_control_method(config[CONF_CONTROL_METHOD]))
if CONF_BEEPER in config:
cg.add(var.set_beeper_state(config[CONF_BEEPER]))
if CONF_DISPLAY in config:
@ -423,5 +447,15 @@ async def to_code(config):
cg.add(var.set_supported_presets(config[CONF_SUPPORTED_PRESETS]))
if CONF_ANSWER_TIMEOUT in config:
cg.add(var.set_answer_timeout(config[CONF_ANSWER_TIMEOUT]))
if CONF_ALTERNATIVE_SWING_CONTROL in config:
cg.add(
var.set_alternative_swing_control(config[CONF_ALTERNATIVE_SWING_CONTROL])
)
if CONF_CONTROL_PACKET_SIZE in config:
cg.add(
var.set_extra_control_packet_bytes_size(
config[CONF_CONTROL_PACKET_SIZE] - PROTOCOL_CONTROL_PACKET_SIZE
)
)
# https://github.com/paveldn/HaierProtocol
cg.add_library("pavlodn/HaierProtocol", "0.9.20")
cg.add_library("pavlodn/HaierProtocol", "0.9.24")

View File

@ -19,56 +19,45 @@ constexpr size_t STATUS_REQUEST_INTERVAL_MS = 5000;
constexpr size_t PROTOCOL_INITIALIZATION_INTERVAL = 10000;
constexpr size_t DEFAULT_MESSAGES_INTERVAL_MS = 2000;
constexpr size_t CONTROL_MESSAGES_INTERVAL_MS = 400;
constexpr size_t CONTROL_TIMEOUT_MS = 7000;
constexpr size_t NO_COMMAND = 0xFF; // Indicate that there is no command supplied
#if (HAIER_LOG_LEVEL > 4)
// To reduce size of binary this function only available when log level is Verbose
const char *HaierClimateBase::phase_to_string_(ProtocolPhases phase) {
static const char *phase_names[] = {
"SENDING_INIT_1",
"WAITING_INIT_1_ANSWER",
"SENDING_INIT_2",
"WAITING_INIT_2_ANSWER",
"SENDING_FIRST_STATUS_REQUEST",
"WAITING_FIRST_STATUS_ANSWER",
"SENDING_ALARM_STATUS_REQUEST",
"WAITING_ALARM_STATUS_ANSWER",
"IDLE",
"UNKNOWN",
"SENDING_STATUS_REQUEST",
"WAITING_STATUS_ANSWER",
"SENDING_UPDATE_SIGNAL_REQUEST",
"WAITING_UPDATE_SIGNAL_ANSWER",
"SENDING_SIGNAL_LEVEL",
"WAITING_SIGNAL_LEVEL_ANSWER",
"SENDING_CONTROL",
"WAITING_CONTROL_ANSWER",
"SENDING_POWER_ON_COMMAND",
"WAITING_POWER_ON_ANSWER",
"SENDING_POWER_OFF_COMMAND",
"WAITING_POWER_OFF_ANSWER",
"SENDING_ACTION_COMMAND",
"UNKNOWN" // Should be the last!
};
static_assert(
(sizeof(phase_names) / sizeof(char *)) == (((int) ProtocolPhases::NUM_PROTOCOL_PHASES) + 1),
"Wrong phase_names array size. Please, make sure that this array is aligned with the enum ProtocolPhases");
int phase_index = (int) phase;
if ((phase_index > (int) ProtocolPhases::NUM_PROTOCOL_PHASES) || (phase_index < 0))
phase_index = (int) ProtocolPhases::NUM_PROTOCOL_PHASES;
return phase_names[phase_index];
}
#endif
bool check_timeout(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint,
size_t timeout) {
return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
}
HaierClimateBase::HaierClimateBase()
: haier_protocol_(*this),
protocol_phase_(ProtocolPhases::SENDING_INIT_1),
action_request_(ActionRequest::NO_ACTION),
display_status_(true),
health_mode_(false),
force_send_control_(false),
forced_publish_(false),
forced_request_status_(false),
first_control_attempt_(false),
reset_protocol_request_(false),
send_wifi_signal_(true) {
send_wifi_signal_(true),
use_crc_(false) {
this->traits_ = climate::ClimateTraits();
this->traits_.set_supported_modes({climate::CLIMATE_MODE_OFF, climate::CLIMATE_MODE_COOL, climate::CLIMATE_MODE_HEAT,
climate::CLIMATE_MODE_FAN_ONLY, climate::CLIMATE_MODE_DRY,
@ -84,42 +73,43 @@ HaierClimateBase::~HaierClimateBase() {}
void HaierClimateBase::set_phase(ProtocolPhases phase) {
if (this->protocol_phase_ != phase) {
#if (HAIER_LOG_LEVEL > 4)
ESP_LOGV(TAG, "Phase transition: %s => %s", phase_to_string_(this->protocol_phase_), phase_to_string_(phase));
#else
ESP_LOGV(TAG, "Phase transition: %d => %d", (int) this->protocol_phase_, (int) phase);
#endif
this->protocol_phase_ = phase;
}
}
bool HaierClimateBase::check_timeout_(std::chrono::steady_clock::time_point now,
std::chrono::steady_clock::time_point tpoint, size_t timeout) {
return std::chrono::duration_cast<std::chrono::milliseconds>(now - tpoint).count() > timeout;
void HaierClimateBase::reset_phase_() {
this->set_phase((this->protocol_phase_ >= ProtocolPhases::IDLE) ? ProtocolPhases::IDLE
: ProtocolPhases::SENDING_INIT_1);
}
void HaierClimateBase::reset_to_idle_() {
this->force_send_control_ = false;
if (this->current_hvac_settings_.valid)
this->current_hvac_settings_.reset();
this->forced_request_status_ = true;
this->set_phase(ProtocolPhases::IDLE);
this->action_request_.reset();
}
bool HaierClimateBase::is_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
return check_timeout(now, this->last_request_timestamp_, DEFAULT_MESSAGES_INTERVAL_MS);
}
bool HaierClimateBase::is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
}
bool HaierClimateBase::is_control_message_timeout_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->control_request_timestamp_, CONTROL_TIMEOUT_MS);
return check_timeout(now, this->last_status_request_, STATUS_REQUEST_INTERVAL_MS);
}
bool HaierClimateBase::is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
return check_timeout(now, this->last_request_timestamp_, CONTROL_MESSAGES_INTERVAL_MS);
}
bool HaierClimateBase::is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now) {
return this->check_timeout_(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
return check_timeout(now, this->last_request_timestamp_, PROTOCOL_INITIALIZATION_INTERVAL);
}
#ifdef USE_WIFI
haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_(uint8_t message_type) {
haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_() {
static uint8_t wifi_status_data[4] = {0x00, 0x00, 0x00, 0x00};
if (wifi::global_wifi_component->is_connected()) {
wifi_status_data[1] = 0;
@ -131,7 +121,8 @@ haier_protocol::HaierMessage HaierClimateBase::get_wifi_signal_message_(uint8_t
wifi_status_data[1] = 1;
wifi_status_data[3] = 0;
}
return haier_protocol::HaierMessage(message_type, wifi_status_data, sizeof(wifi_status_data));
return haier_protocol::HaierMessage(haier_protocol::FrameType::REPORT_NETWORK_STATUS, wifi_status_data,
sizeof(wifi_status_data));
}
#endif
@ -140,7 +131,7 @@ bool HaierClimateBase::get_display_state() const { return this->display_status_;
void HaierClimateBase::set_display_state(bool state) {
if (this->display_status_ != state) {
this->display_status_ = state;
this->set_force_send_control_(true);
this->force_send_control_ = true;
}
}
@ -149,15 +140,24 @@ bool HaierClimateBase::get_health_mode() const { return this->health_mode_; }
void HaierClimateBase::set_health_mode(bool state) {
if (this->health_mode_ != state) {
this->health_mode_ = state;
this->set_force_send_control_(true);
this->force_send_control_ = true;
}
}
void HaierClimateBase::send_power_on_command() { this->action_request_ = ActionRequest::TURN_POWER_ON; }
void HaierClimateBase::send_power_on_command() {
this->action_request_ =
PendingAction({ActionRequest::TURN_POWER_ON, esphome::optional<haier_protocol::HaierMessage>()});
}
void HaierClimateBase::send_power_off_command() { this->action_request_ = ActionRequest::TURN_POWER_OFF; }
void HaierClimateBase::send_power_off_command() {
this->action_request_ =
PendingAction({ActionRequest::TURN_POWER_OFF, esphome::optional<haier_protocol::HaierMessage>()});
}
void HaierClimateBase::toggle_power() { this->action_request_ = ActionRequest::TOGGLE_POWER; }
void HaierClimateBase::toggle_power() {
this->action_request_ =
PendingAction({ActionRequest::TOGGLE_POWER, esphome::optional<haier_protocol::HaierMessage>()});
}
void HaierClimateBase::set_supported_swing_modes(const std::set<climate::ClimateSwingMode> &modes) {
this->traits_.set_supported_swing_modes(modes);
@ -165,9 +165,7 @@ void HaierClimateBase::set_supported_swing_modes(const std::set<climate::Climate
this->traits_.add_supported_swing_mode(climate::CLIMATE_SWING_OFF);
}
void HaierClimateBase::set_answer_timeout(uint32_t timeout) {
this->answer_timeout_ = std::chrono::milliseconds(timeout);
}
void HaierClimateBase::set_answer_timeout(uint32_t timeout) { this->haier_protocol_.set_answer_timeout(timeout); }
void HaierClimateBase::set_supported_modes(const std::set<climate::ClimateMode> &modes) {
this->traits_.set_supported_modes(modes);
@ -183,29 +181,42 @@ void HaierClimateBase::set_supported_presets(const std::set<climate::ClimatePres
void HaierClimateBase::set_send_wifi(bool send_wifi) { this->send_wifi_signal_ = send_wifi; }
haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(uint8_t request_message_type,
uint8_t expected_request_message_type,
uint8_t answer_message_type,
uint8_t expected_answer_message_type,
ProtocolPhases expected_phase) {
void HaierClimateBase::send_custom_command(const haier_protocol::HaierMessage &message) {
this->action_request_ = PendingAction({ActionRequest::SEND_CUSTOM_COMMAND, message});
}
haier_protocol::HandlerError HaierClimateBase::answer_preprocess_(
haier_protocol::FrameType request_message_type, haier_protocol::FrameType expected_request_message_type,
haier_protocol::FrameType answer_message_type, haier_protocol::FrameType expected_answer_message_type,
ProtocolPhases expected_phase) {
haier_protocol::HandlerError result = haier_protocol::HandlerError::HANDLER_OK;
if ((expected_request_message_type != NO_COMMAND) && (request_message_type != expected_request_message_type))
if ((expected_request_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
(request_message_type != expected_request_message_type))
result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
if ((expected_answer_message_type != NO_COMMAND) && (answer_message_type != expected_answer_message_type))
if ((expected_answer_message_type != haier_protocol::FrameType::UNKNOWN_FRAME_TYPE) &&
(answer_message_type != expected_answer_message_type))
result = haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
if ((expected_phase != ProtocolPhases::UNKNOWN) && (expected_phase != this->protocol_phase_))
if (!this->haier_protocol_.is_waiting_for_answer() ||
((expected_phase != ProtocolPhases::UNKNOWN) && (expected_phase != this->protocol_phase_)))
result = haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
if (is_message_invalid(answer_message_type))
if (answer_message_type == haier_protocol::FrameType::INVALID)
result = haier_protocol::HandlerError::INVALID_ANSWER;
return result;
}
haier_protocol::HandlerError HaierClimateBase::timeout_default_handler_(uint8_t request_type) {
#if (HAIER_LOG_LEVEL > 4)
ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", request_type, phase_to_string_(this->protocol_phase_));
#else
ESP_LOGW(TAG, "Answer timeout for command %02X, phase %d", request_type, (int) this->protocol_phase_);
#endif
haier_protocol::HandlerError HaierClimateBase::report_network_status_answer_handler_(
haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
size_t data_size) {
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, haier_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
haier_protocol::FrameType::CONFIRM, ProtocolPhases::SENDING_SIGNAL_LEVEL);
this->set_phase(ProtocolPhases::IDLE);
return result;
}
haier_protocol::HandlerError HaierClimateBase::timeout_default_handler_(haier_protocol::FrameType request_type) {
ESP_LOGW(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) request_type,
phase_to_string_(this->protocol_phase_));
if (this->protocol_phase_ > ProtocolPhases::IDLE) {
this->set_phase(ProtocolPhases::IDLE);
} else {
@ -219,79 +230,95 @@ void HaierClimateBase::setup() {
// Set timestamp here to give AC time to boot
this->last_request_timestamp_ = std::chrono::steady_clock::now();
this->set_phase(ProtocolPhases::SENDING_INIT_1);
this->set_handlers();
this->haier_protocol_.set_default_timeout_handler(
std::bind(&esphome::haier::HaierClimateBase::timeout_default_handler_, this, std::placeholders::_1));
this->set_handlers();
}
void HaierClimateBase::dump_config() {
LOG_CLIMATE("", "Haier Climate", this);
ESP_LOGCONFIG(TAG, " Device communication status: %s",
(this->protocol_phase_ >= ProtocolPhases::IDLE) ? "established" : "none");
ESP_LOGCONFIG(TAG, " Device communication status: %s", this->valid_connection() ? "established" : "none");
}
void HaierClimateBase::loop() {
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
if ((std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_valid_status_timestamp_).count() >
COMMUNICATION_TIMEOUT_MS) ||
(this->reset_protocol_request_)) {
(this->reset_protocol_request_ && (!this->haier_protocol_.is_waiting_for_answer()))) {
this->last_valid_status_timestamp_ = now;
if (this->protocol_phase_ >= ProtocolPhases::IDLE) {
// No status too long, reseting protocol
// No need to reset protocol if we didn't pass initialization phase
if (this->reset_protocol_request_) {
this->reset_protocol_request_ = false;
ESP_LOGW(TAG, "Protocol reset requested");
} else {
ESP_LOGW(TAG, "Communication timeout, reseting protocol");
}
this->last_valid_status_timestamp_ = now;
this->set_force_send_control_(false);
if (this->hvac_settings_.valid)
this->hvac_settings_.reset();
this->set_phase(ProtocolPhases::SENDING_INIT_1);
this->process_protocol_reset();
return;
} else {
// No need to reset protocol if we didn't pass initialization phase
this->last_valid_status_timestamp_ = now;
}
};
if ((this->protocol_phase_ == ProtocolPhases::IDLE) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_STATUS_REQUEST) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_SIGNAL_LEVEL)) {
if ((!this->haier_protocol_.is_waiting_for_answer()) &&
((this->protocol_phase_ == ProtocolPhases::IDLE) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_STATUS_REQUEST) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST) ||
(this->protocol_phase_ == ProtocolPhases::SENDING_SIGNAL_LEVEL))) {
// If control message or action is pending we should send it ASAP unless we are in initialisation
// procedure or waiting for an answer
if (this->action_request_ != ActionRequest::NO_ACTION) {
this->process_pending_action();
} else if (this->hvac_settings_.valid || this->force_send_control_) {
if (this->action_request_.has_value() && this->prepare_pending_action()) {
this->set_phase(ProtocolPhases::SENDING_ACTION_COMMAND);
} else if (this->next_hvac_settings_.valid || this->force_send_control_) {
ESP_LOGV(TAG, "Control packet is pending...");
this->set_phase(ProtocolPhases::SENDING_CONTROL);
if (this->next_hvac_settings_.valid) {
this->current_hvac_settings_ = this->next_hvac_settings_;
this->next_hvac_settings_.reset();
} else {
this->current_hvac_settings_.reset();
}
}
}
this->process_phase(now);
this->haier_protocol_.loop();
}
void HaierClimateBase::process_pending_action() {
ActionRequest request = this->action_request_;
if (this->action_request_ == ActionRequest::TOGGLE_POWER) {
request = this->mode == CLIMATE_MODE_OFF ? ActionRequest::TURN_POWER_ON : ActionRequest::TURN_POWER_OFF;
}
switch (request) {
case ActionRequest::TURN_POWER_ON:
this->set_phase(ProtocolPhases::SENDING_POWER_ON_COMMAND);
break;
case ActionRequest::TURN_POWER_OFF:
this->set_phase(ProtocolPhases::SENDING_POWER_OFF_COMMAND);
break;
case ActionRequest::TOGGLE_POWER:
case ActionRequest::NO_ACTION:
// shouldn't get here, do nothing
break;
default:
ESP_LOGW(TAG, "Unsupported action: %d", (uint8_t) this->action_request_);
break;
}
this->action_request_ = ActionRequest::NO_ACTION;
void HaierClimateBase::process_protocol_reset() {
this->force_send_control_ = false;
if (this->current_hvac_settings_.valid)
this->current_hvac_settings_.reset();
if (this->next_hvac_settings_.valid)
this->next_hvac_settings_.reset();
this->mode = CLIMATE_MODE_OFF;
this->current_temperature = NAN;
this->target_temperature = NAN;
this->fan_mode.reset();
this->preset.reset();
this->publish_state();
this->set_phase(ProtocolPhases::SENDING_INIT_1);
}
bool HaierClimateBase::prepare_pending_action() {
if (this->action_request_.has_value()) {
switch (this->action_request_.value().action) {
case ActionRequest::SEND_CUSTOM_COMMAND:
return true;
case ActionRequest::TURN_POWER_ON:
this->action_request_.value().message = this->get_power_message(true);
return true;
case ActionRequest::TURN_POWER_OFF:
this->action_request_.value().message = this->get_power_message(false);
return true;
case ActionRequest::TOGGLE_POWER:
this->action_request_.value().message = this->get_power_message(this->mode == ClimateMode::CLIMATE_MODE_OFF);
return true;
default:
ESP_LOGW(TAG, "Unsupported action: %d", (uint8_t) this->action_request_.value().action);
this->action_request_.reset();
return false;
}
} else
return false;
}
ClimateTraits HaierClimateBase::traits() { return traits_; }
@ -302,23 +329,22 @@ void HaierClimateBase::control(const ClimateCall &call) {
ESP_LOGW(TAG, "Can't send control packet, first poll answer not received");
return; // cancel the control, we cant do it without a poll answer.
}
if (this->hvac_settings_.valid) {
ESP_LOGW(TAG, "Overriding old valid settings before they were applied!");
if (this->current_hvac_settings_.valid) {
ESP_LOGW(TAG, "New settings come faster then processed!");
}
{
if (call.get_mode().has_value())
this->hvac_settings_.mode = call.get_mode();
this->next_hvac_settings_.mode = call.get_mode();
if (call.get_fan_mode().has_value())
this->hvac_settings_.fan_mode = call.get_fan_mode();
this->next_hvac_settings_.fan_mode = call.get_fan_mode();
if (call.get_swing_mode().has_value())
this->hvac_settings_.swing_mode = call.get_swing_mode();
this->next_hvac_settings_.swing_mode = call.get_swing_mode();
if (call.get_target_temperature().has_value())
this->hvac_settings_.target_temperature = call.get_target_temperature();
this->next_hvac_settings_.target_temperature = call.get_target_temperature();
if (call.get_preset().has_value())
this->hvac_settings_.preset = call.get_preset();
this->hvac_settings_.valid = true;
this->next_hvac_settings_.preset = call.get_preset();
this->next_hvac_settings_.valid = true;
}
this->first_control_attempt_ = true;
}
void HaierClimateBase::HvacSettings::reset() {
@ -330,19 +356,9 @@ void HaierClimateBase::HvacSettings::reset() {
this->preset.reset();
}
void HaierClimateBase::set_force_send_control_(bool status) {
this->force_send_control_ = status;
if (status) {
this->first_control_attempt_ = true;
}
}
void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc) {
if (this->answer_timeout_.has_value()) {
this->haier_protocol_.send_message(command, use_crc, this->answer_timeout_.value());
} else {
this->haier_protocol_.send_message(command, use_crc);
}
void HaierClimateBase::send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats,
std::chrono::milliseconds interval) {
this->haier_protocol_.send_message(command, use_crc, num_repeats, interval);
this->last_request_timestamp_ = std::chrono::steady_clock::now();
}

View File

@ -11,7 +11,7 @@ namespace esphome {
namespace haier {
enum class ActionRequest : uint8_t {
NO_ACTION = 0,
SEND_CUSTOM_COMMAND = 0,
TURN_POWER_ON = 1,
TURN_POWER_OFF = 2,
TOGGLE_POWER = 3,
@ -33,7 +33,6 @@ class HaierClimateBase : public esphome::Component,
void control(const esphome::climate::ClimateCall &call) override;
void dump_config() override;
float get_setup_priority() const override { return esphome::setup_priority::HARDWARE; }
void set_fahrenheit(bool fahrenheit);
void set_display_state(bool state);
bool get_display_state() const;
void set_health_mode(bool state);
@ -45,6 +44,7 @@ class HaierClimateBase : public esphome::Component,
void set_supported_modes(const std::set<esphome::climate::ClimateMode> &modes);
void set_supported_swing_modes(const std::set<esphome::climate::ClimateSwingMode> &modes);
void set_supported_presets(const std::set<esphome::climate::ClimatePreset> &presets);
bool valid_connection() { return this->protocol_phase_ >= ProtocolPhases::IDLE; };
size_t available() noexcept override { return esphome::uart::UARTDevice::available(); };
size_t read_array(uint8_t *data, size_t len) noexcept override {
return esphome::uart::UARTDevice::read_array(data, len) ? len : 0;
@ -55,63 +55,56 @@ class HaierClimateBase : public esphome::Component,
bool can_send_message() const { return haier_protocol_.get_outgoing_queue_size() == 0; };
void set_answer_timeout(uint32_t timeout);
void set_send_wifi(bool send_wifi);
void send_custom_command(const haier_protocol::HaierMessage &message);
protected:
enum class ProtocolPhases {
UNKNOWN = -1,
// INITIALIZATION
SENDING_INIT_1 = 0,
WAITING_INIT_1_ANSWER = 1,
SENDING_INIT_2 = 2,
WAITING_INIT_2_ANSWER = 3,
SENDING_FIRST_STATUS_REQUEST = 4,
WAITING_FIRST_STATUS_ANSWER = 5,
SENDING_ALARM_STATUS_REQUEST = 6,
WAITING_ALARM_STATUS_ANSWER = 7,
SENDING_INIT_2,
SENDING_FIRST_STATUS_REQUEST,
SENDING_ALARM_STATUS_REQUEST,
// FUNCTIONAL STATE
IDLE = 8,
SENDING_STATUS_REQUEST = 10,
WAITING_STATUS_ANSWER = 11,
SENDING_UPDATE_SIGNAL_REQUEST = 12,
WAITING_UPDATE_SIGNAL_ANSWER = 13,
SENDING_SIGNAL_LEVEL = 14,
WAITING_SIGNAL_LEVEL_ANSWER = 15,
SENDING_CONTROL = 16,
WAITING_CONTROL_ANSWER = 17,
SENDING_POWER_ON_COMMAND = 18,
WAITING_POWER_ON_ANSWER = 19,
SENDING_POWER_OFF_COMMAND = 20,
WAITING_POWER_OFF_ANSWER = 21,
IDLE,
SENDING_STATUS_REQUEST,
SENDING_UPDATE_SIGNAL_REQUEST,
SENDING_SIGNAL_LEVEL,
SENDING_CONTROL,
SENDING_ACTION_COMMAND,
NUM_PROTOCOL_PHASES
};
#if (HAIER_LOG_LEVEL > 4)
const char *phase_to_string_(ProtocolPhases phase);
#endif
virtual void set_handlers() = 0;
virtual void process_phase(std::chrono::steady_clock::time_point now) = 0;
virtual haier_protocol::HaierMessage get_control_message() = 0;
virtual bool is_message_invalid(uint8_t message_type) = 0;
virtual void process_pending_action();
virtual haier_protocol::HaierMessage get_power_message(bool state) = 0;
virtual bool prepare_pending_action();
virtual void process_protocol_reset();
esphome::climate::ClimateTraits traits() override;
// Answers handlers
haier_protocol::HandlerError answer_preprocess_(uint8_t request_message_type, uint8_t expected_request_message_type,
uint8_t answer_message_type, uint8_t expected_answer_message_type,
// Answer handlers
haier_protocol::HandlerError answer_preprocess_(haier_protocol::FrameType request_message_type,
haier_protocol::FrameType expected_request_message_type,
haier_protocol::FrameType answer_message_type,
haier_protocol::FrameType expected_answer_message_type,
ProtocolPhases expected_phase);
haier_protocol::HandlerError report_network_status_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
// Timeout handler
haier_protocol::HandlerError timeout_default_handler_(uint8_t request_type);
haier_protocol::HandlerError timeout_default_handler_(haier_protocol::FrameType request_type);
// Helper functions
void set_force_send_control_(bool status);
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc);
void send_message_(const haier_protocol::HaierMessage &command, bool use_crc, uint8_t num_repeats = 0,
std::chrono::milliseconds interval = std::chrono::milliseconds::zero());
virtual void set_phase(ProtocolPhases phase);
bool check_timeout_(std::chrono::steady_clock::time_point now, std::chrono::steady_clock::time_point tpoint,
size_t timeout);
void reset_phase_();
void reset_to_idle_();
bool is_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
bool is_status_request_interval_exceeded_(std::chrono::steady_clock::time_point now);
bool is_control_message_timeout_exceeded_(std::chrono::steady_clock::time_point now);
bool is_control_message_interval_exceeded_(std::chrono::steady_clock::time_point now);
bool is_protocol_initialisation_interval_exceeded_(std::chrono::steady_clock::time_point now);
#ifdef USE_WIFI
haier_protocol::HaierMessage get_wifi_signal_message_(uint8_t message_type);
haier_protocol::HaierMessage get_wifi_signal_message_();
#endif
struct HvacSettings {
@ -122,29 +115,34 @@ class HaierClimateBase : public esphome::Component,
esphome::optional<esphome::climate::ClimatePreset> preset;
bool valid;
HvacSettings() : valid(false){};
HvacSettings(const HvacSettings &) = default;
HvacSettings &operator=(const HvacSettings &) = default;
void reset();
};
struct PendingAction {
ActionRequest action;
esphome::optional<haier_protocol::HaierMessage> message;
};
haier_protocol::ProtocolHandler haier_protocol_;
ProtocolPhases protocol_phase_;
ActionRequest action_request_;
esphome::optional<PendingAction> action_request_;
uint8_t fan_mode_speed_;
uint8_t other_modes_fan_speed_;
bool display_status_;
bool health_mode_;
bool force_send_control_;
bool forced_publish_;
bool forced_request_status_;
bool first_control_attempt_;
bool reset_protocol_request_;
bool send_wifi_signal_;
bool use_crc_;
esphome::climate::ClimateTraits traits_;
HvacSettings hvac_settings_;
HvacSettings current_hvac_settings_;
HvacSettings next_hvac_settings_;
std::unique_ptr<uint8_t[]> last_status_message_;
std::chrono::steady_clock::time_point last_request_timestamp_; // For interval between messages
std::chrono::steady_clock::time_point last_valid_status_timestamp_; // For protocol timeout
std::chrono::steady_clock::time_point last_status_request_; // To request AC status
std::chrono::steady_clock::time_point control_request_timestamp_; // To send control message
optional<std::chrono::milliseconds> answer_timeout_; // Message answer timeout
bool send_wifi_signal_;
std::chrono::steady_clock::time_point last_signal_request_; // To send WiFI signal level
std::chrono::steady_clock::time_point last_signal_request_; // To send WiFI signal level
};
} // namespace haier

View File

@ -14,6 +14,8 @@ namespace haier {
static const char *const TAG = "haier.climate";
constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS = 10000;
constexpr int PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET = -64;
constexpr uint8_t CONTROL_MESSAGE_RETRIES = 5;
constexpr std::chrono::milliseconds CONTROL_MESSAGE_RETRIES_INTERVAL = std::chrono::milliseconds(500);
hon_protocol::VerticalSwingMode get_vertical_swing_mode(AirflowVerticalDirection direction) {
switch (direction) {
@ -48,14 +50,11 @@ hon_protocol::HorizontalSwingMode get_horizontal_swing_mode(AirflowHorizontalDir
}
HonClimate::HonClimate()
: last_status_message_(new uint8_t[sizeof(hon_protocol::HaierPacketControl)]),
cleaning_status_(CleaningState::NO_CLEANING),
: cleaning_status_(CleaningState::NO_CLEANING),
got_valid_outdoor_temp_(false),
hvac_hardware_info_available_(false),
hvac_functions_{false, false, false, false, false},
use_crc_(hvac_functions_[2]),
active_alarms_{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
outdoor_sensor_(nullptr) {
last_status_message_ = std::unique_ptr<uint8_t[]>(new uint8_t[sizeof(hon_protocol::HaierPacketControl)]);
this->fan_mode_speed_ = (uint8_t) hon_protocol::FanMode::FAN_MID;
this->other_modes_fan_speed_ = (uint8_t) hon_protocol::FanMode::FAN_AUTO;
}
@ -72,14 +71,14 @@ AirflowVerticalDirection HonClimate::get_vertical_airflow() const { return this-
void HonClimate::set_vertical_airflow(AirflowVerticalDirection direction) {
this->vertical_direction_ = direction;
this->set_force_send_control_(true);
this->force_send_control_ = true;
}
AirflowHorizontalDirection HonClimate::get_horizontal_airflow() const { return this->horizontal_direction_; }
void HonClimate::set_horizontal_airflow(AirflowHorizontalDirection direction) {
this->horizontal_direction_ = direction;
this->set_force_send_control_(true);
this->force_send_control_ = true;
}
std::string HonClimate::get_cleaning_status_text() const {
@ -98,35 +97,35 @@ CleaningState HonClimate::get_cleaning_status() const { return this->cleaning_st
void HonClimate::start_self_cleaning() {
if (this->cleaning_status_ == CleaningState::NO_CLEANING) {
ESP_LOGI(TAG, "Sending self cleaning start request");
this->action_request_ = ActionRequest::START_SELF_CLEAN;
this->set_force_send_control_(true);
this->action_request_ =
PendingAction({ActionRequest::START_SELF_CLEAN, esphome::optional<haier_protocol::HaierMessage>()});
}
}
void HonClimate::start_steri_cleaning() {
if (this->cleaning_status_ == CleaningState::NO_CLEANING) {
ESP_LOGI(TAG, "Sending steri cleaning start request");
this->action_request_ = ActionRequest::START_STERI_CLEAN;
this->set_force_send_control_(true);
this->action_request_ =
PendingAction({ActionRequest::START_STERI_CLEAN, esphome::optional<haier_protocol::HaierMessage>()});
}
}
haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size) {
// Should check this before preprocess
if (message_type == (uint8_t) hon_protocol::FrameType::INVALID) {
if (message_type == haier_protocol::FrameType::INVALID) {
ESP_LOGW(TAG, "It looks like your ESPHome Haier climate configuration is wrong. You should use the smartAir2 "
"protocol instead of hOn");
this->set_phase(ProtocolPhases::SENDING_INIT_1);
return haier_protocol::HandlerError::INVALID_ANSWER;
}
haier_protocol::HandlerError result = this->answer_preprocess_(
request_type, (uint8_t) hon_protocol::FrameType::GET_DEVICE_VERSION, message_type,
(uint8_t) hon_protocol::FrameType::GET_DEVICE_VERSION_RESPONSE, ProtocolPhases::WAITING_INIT_1_ANSWER);
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, haier_protocol::FrameType::GET_DEVICE_VERSION, message_type,
haier_protocol::FrameType::GET_DEVICE_VERSION_RESPONSE, ProtocolPhases::SENDING_INIT_1);
if (result == haier_protocol::HandlerError::HANDLER_OK) {
if (data_size < sizeof(hon_protocol::DeviceVersionAnswer)) {
// Wrong structure
this->set_phase(ProtocolPhases::SENDING_INIT_1);
return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
}
// All OK
@ -134,54 +133,57 @@ haier_protocol::HandlerError HonClimate::get_device_version_answer_handler_(uint
char tmp[9];
tmp[8] = 0;
strncpy(tmp, answr->protocol_version, 8);
this->hvac_protocol_version_ = std::string(tmp);
this->hvac_hardware_info_ = HardwareInfo();
this->hvac_hardware_info_.value().protocol_version_ = std::string(tmp);
strncpy(tmp, answr->software_version, 8);
this->hvac_software_version_ = std::string(tmp);
this->hvac_hardware_info_.value().software_version_ = std::string(tmp);
strncpy(tmp, answr->hardware_version, 8);
this->hvac_hardware_version_ = std::string(tmp);
this->hvac_hardware_info_.value().hardware_version_ = std::string(tmp);
strncpy(tmp, answr->device_name, 8);
this->hvac_device_name_ = std::string(tmp);
this->hvac_functions_[0] = (answr->functions[1] & 0x01) != 0; // interactive mode support
this->hvac_functions_[1] = (answr->functions[1] & 0x02) != 0; // controller-device mode support
this->hvac_functions_[2] = (answr->functions[1] & 0x04) != 0; // crc support
this->hvac_functions_[3] = (answr->functions[1] & 0x08) != 0; // multiple AC support
this->hvac_functions_[4] = (answr->functions[1] & 0x20) != 0; // roles support
this->hvac_hardware_info_available_ = true;
this->hvac_hardware_info_.value().device_name_ = std::string(tmp);
this->hvac_hardware_info_.value().functions_[0] = (answr->functions[1] & 0x01) != 0; // interactive mode support
this->hvac_hardware_info_.value().functions_[1] =
(answr->functions[1] & 0x02) != 0; // controller-device mode support
this->hvac_hardware_info_.value().functions_[2] = (answr->functions[1] & 0x04) != 0; // crc support
this->hvac_hardware_info_.value().functions_[3] = (answr->functions[1] & 0x08) != 0; // multiple AC support
this->hvac_hardware_info_.value().functions_[4] = (answr->functions[1] & 0x20) != 0; // roles support
this->use_crc_ = this->hvac_hardware_info_.value().functions_[2];
this->set_phase(ProtocolPhases::SENDING_INIT_2);
return result;
} else {
this->set_phase((this->protocol_phase_ >= ProtocolPhases::IDLE) ? ProtocolPhases::IDLE
: ProtocolPhases::SENDING_INIT_1);
this->reset_phase_();
return result;
}
}
haier_protocol::HandlerError HonClimate::get_device_id_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError HonClimate::get_device_id_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size) {
haier_protocol::HandlerError result = this->answer_preprocess_(
request_type, (uint8_t) hon_protocol::FrameType::GET_DEVICE_ID, message_type,
(uint8_t) hon_protocol::FrameType::GET_DEVICE_ID_RESPONSE, ProtocolPhases::WAITING_INIT_2_ANSWER);
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, haier_protocol::FrameType::GET_DEVICE_ID, message_type,
haier_protocol::FrameType::GET_DEVICE_ID_RESPONSE, ProtocolPhases::SENDING_INIT_2);
if (result == haier_protocol::HandlerError::HANDLER_OK) {
this->set_phase(ProtocolPhases::SENDING_FIRST_STATUS_REQUEST);
return result;
} else {
this->set_phase((this->protocol_phase_ >= ProtocolPhases::IDLE) ? ProtocolPhases::IDLE
: ProtocolPhases::SENDING_INIT_1);
this->reset_phase_();
return result;
}
}
haier_protocol::HandlerError HonClimate::status_handler_(uint8_t request_type, uint8_t message_type,
const uint8_t *data, size_t data_size) {
haier_protocol::HandlerError HonClimate::status_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type, const uint8_t *data,
size_t data_size) {
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, (uint8_t) hon_protocol::FrameType::CONTROL, message_type,
(uint8_t) hon_protocol::FrameType::STATUS, ProtocolPhases::UNKNOWN);
this->answer_preprocess_(request_type, haier_protocol::FrameType::CONTROL, message_type,
haier_protocol::FrameType::STATUS, ProtocolPhases::UNKNOWN);
if (result == haier_protocol::HandlerError::HANDLER_OK) {
result = this->process_status_message_(data, data_size);
if (result != haier_protocol::HandlerError::HANDLER_OK) {
ESP_LOGW(TAG, "Error %d while parsing Status packet", (int) result);
this->set_phase((this->protocol_phase_ >= ProtocolPhases::IDLE) ? ProtocolPhases::IDLE
: ProtocolPhases::SENDING_INIT_1);
this->reset_phase_();
this->action_request_.reset();
this->force_send_control_ = false;
} else {
if (data_size >= sizeof(hon_protocol::HaierPacketControl) + 2) {
memcpy(this->last_status_message_.get(), data + 2, sizeof(hon_protocol::HaierPacketControl));
@ -189,36 +191,48 @@ haier_protocol::HandlerError HonClimate::status_handler_(uint8_t request_type, u
ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size,
sizeof(hon_protocol::HaierPacketControl));
}
if (this->protocol_phase_ == ProtocolPhases::WAITING_FIRST_STATUS_ANSWER) {
ESP_LOGI(TAG, "First HVAC status received");
this->set_phase(ProtocolPhases::SENDING_ALARM_STATUS_REQUEST);
} else if ((this->protocol_phase_ == ProtocolPhases::WAITING_STATUS_ANSWER) ||
(this->protocol_phase_ == ProtocolPhases::WAITING_POWER_ON_ANSWER) ||
(this->protocol_phase_ == ProtocolPhases::WAITING_POWER_OFF_ANSWER)) {
this->set_phase(ProtocolPhases::IDLE);
} else if (this->protocol_phase_ == ProtocolPhases::WAITING_CONTROL_ANSWER) {
this->set_phase(ProtocolPhases::IDLE);
this->set_force_send_control_(false);
if (this->hvac_settings_.valid)
this->hvac_settings_.reset();
switch (this->protocol_phase_) {
case ProtocolPhases::SENDING_FIRST_STATUS_REQUEST:
ESP_LOGI(TAG, "First HVAC status received");
this->set_phase(ProtocolPhases::SENDING_ALARM_STATUS_REQUEST);
break;
case ProtocolPhases::SENDING_ACTION_COMMAND:
// Do nothing, phase will be changed in process_phase
break;
case ProtocolPhases::SENDING_STATUS_REQUEST:
this->set_phase(ProtocolPhases::IDLE);
break;
case ProtocolPhases::SENDING_CONTROL:
if (!this->control_messages_queue_.empty())
this->control_messages_queue_.pop();
if (this->control_messages_queue_.empty()) {
this->set_phase(ProtocolPhases::IDLE);
this->force_send_control_ = false;
if (this->current_hvac_settings_.valid)
this->current_hvac_settings_.reset();
} else {
this->set_phase(ProtocolPhases::SENDING_CONTROL);
}
break;
default:
break;
}
}
return result;
} else {
this->set_phase((this->protocol_phase_ >= ProtocolPhases::IDLE) ? ProtocolPhases::IDLE
: ProtocolPhases::SENDING_INIT_1);
this->action_request_.reset();
this->force_send_control_ = false;
this->reset_phase_();
return result;
}
}
haier_protocol::HandlerError HonClimate::get_management_information_answer_handler_(uint8_t request_type,
uint8_t message_type,
const uint8_t *data,
size_t data_size) {
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, (uint8_t) hon_protocol::FrameType::GET_MANAGEMENT_INFORMATION,
message_type, (uint8_t) hon_protocol::FrameType::GET_MANAGEMENT_INFORMATION_RESPONSE,
ProtocolPhases::WAITING_UPDATE_SIGNAL_ANSWER);
haier_protocol::HandlerError HonClimate::get_management_information_answer_handler_(
haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
size_t data_size) {
haier_protocol::HandlerError result = this->answer_preprocess_(
request_type, haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION, message_type,
haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION_RESPONSE, ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST);
if (result == haier_protocol::HandlerError::HANDLER_OK) {
this->set_phase(ProtocolPhases::SENDING_SIGNAL_LEVEL);
return result;
@ -228,25 +242,16 @@ haier_protocol::HandlerError HonClimate::get_management_information_answer_handl
}
}
haier_protocol::HandlerError HonClimate::report_network_status_answer_handler_(uint8_t request_type,
uint8_t message_type,
const uint8_t *data, size_t data_size) {
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, (uint8_t) hon_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
(uint8_t) hon_protocol::FrameType::CONFIRM, ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER);
this->set_phase(ProtocolPhases::IDLE);
return result;
}
haier_protocol::HandlerError HonClimate::get_alarm_status_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError HonClimate::get_alarm_status_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size) {
if (request_type == (uint8_t) hon_protocol::FrameType::GET_ALARM_STATUS) {
if (message_type != (uint8_t) hon_protocol::FrameType::GET_ALARM_STATUS_RESPONSE) {
if (request_type == haier_protocol::FrameType::GET_ALARM_STATUS) {
if (message_type != haier_protocol::FrameType::GET_ALARM_STATUS_RESPONSE) {
// Unexpected answer to request
this->set_phase(ProtocolPhases::IDLE);
return haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
}
if (this->protocol_phase_ != ProtocolPhases::WAITING_ALARM_STATUS_ANSWER) {
if (this->protocol_phase_ != ProtocolPhases::SENDING_ALARM_STATUS_REQUEST) {
// Don't expect this answer now
this->set_phase(ProtocolPhases::IDLE);
return haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
@ -263,27 +268,27 @@ haier_protocol::HandlerError HonClimate::get_alarm_status_answer_handler_(uint8_
void HonClimate::set_handlers() {
// Set handlers
this->haier_protocol_.set_answer_handler(
(uint8_t) (hon_protocol::FrameType::GET_DEVICE_VERSION),
haier_protocol::FrameType::GET_DEVICE_VERSION,
std::bind(&HonClimate::get_device_version_answer_handler_, this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4));
this->haier_protocol_.set_answer_handler(
(uint8_t) (hon_protocol::FrameType::GET_DEVICE_ID),
haier_protocol::FrameType::GET_DEVICE_ID,
std::bind(&HonClimate::get_device_id_answer_handler_, this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4));
this->haier_protocol_.set_answer_handler(
(uint8_t) (hon_protocol::FrameType::CONTROL),
haier_protocol::FrameType::CONTROL,
std::bind(&HonClimate::status_handler_, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4));
this->haier_protocol_.set_answer_handler(
(uint8_t) (hon_protocol::FrameType::GET_MANAGEMENT_INFORMATION),
haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION,
std::bind(&HonClimate::get_management_information_answer_handler_, this, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
this->haier_protocol_.set_answer_handler(
(uint8_t) (hon_protocol::FrameType::GET_ALARM_STATUS),
haier_protocol::FrameType::GET_ALARM_STATUS,
std::bind(&HonClimate::get_alarm_status_answer_handler_, this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4));
this->haier_protocol_.set_answer_handler(
(uint8_t) (hon_protocol::FrameType::REPORT_NETWORK_STATUS),
haier_protocol::FrameType::REPORT_NETWORK_STATUS,
std::bind(&HonClimate::report_network_status_answer_handler_, this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4));
}
@ -291,14 +296,18 @@ void HonClimate::set_handlers() {
void HonClimate::dump_config() {
HaierClimateBase::dump_config();
ESP_LOGCONFIG(TAG, " Protocol version: hOn");
if (this->hvac_hardware_info_available_) {
ESP_LOGCONFIG(TAG, " Device protocol version: %s", this->hvac_protocol_version_.c_str());
ESP_LOGCONFIG(TAG, " Device software version: %s", this->hvac_software_version_.c_str());
ESP_LOGCONFIG(TAG, " Device hardware version: %s", this->hvac_hardware_version_.c_str());
ESP_LOGCONFIG(TAG, " Device name: %s", this->hvac_device_name_.c_str());
ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s", (this->hvac_functions_[0] ? " interactive" : ""),
(this->hvac_functions_[1] ? " controller-device" : ""), (this->hvac_functions_[2] ? " crc" : ""),
(this->hvac_functions_[3] ? " multinode" : ""), (this->hvac_functions_[4] ? " role" : ""));
ESP_LOGCONFIG(TAG, " Control method: %d", (uint8_t) this->control_method_);
if (this->hvac_hardware_info_.has_value()) {
ESP_LOGCONFIG(TAG, " Device protocol version: %s", this->hvac_hardware_info_.value().protocol_version_.c_str());
ESP_LOGCONFIG(TAG, " Device software version: %s", this->hvac_hardware_info_.value().software_version_.c_str());
ESP_LOGCONFIG(TAG, " Device hardware version: %s", this->hvac_hardware_info_.value().hardware_version_.c_str());
ESP_LOGCONFIG(TAG, " Device name: %s", this->hvac_hardware_info_.value().device_name_.c_str());
ESP_LOGCONFIG(TAG, " Device features:%s%s%s%s%s",
(this->hvac_hardware_info_.value().functions_[0] ? " interactive" : ""),
(this->hvac_hardware_info_.value().functions_[1] ? " controller-device" : ""),
(this->hvac_hardware_info_.value().functions_[2] ? " crc" : ""),
(this->hvac_hardware_info_.value().functions_[3] ? " multinode" : ""),
(this->hvac_hardware_info_.value().functions_[4] ? " role" : ""));
ESP_LOGCONFIG(TAG, " Active alarms: %s", buf_to_hex(this->active_alarms_, sizeof(this->active_alarms_)).c_str());
}
}
@ -307,7 +316,6 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
switch (this->protocol_phase_) {
case ProtocolPhases::SENDING_INIT_1:
if (this->can_send_message() && this->is_protocol_initialisation_interval_exceeded_(now)) {
this->hvac_hardware_info_available_ = false;
// Indicate device capabilities:
// bit 0 - if 1 module support interactive mode
// bit 1 - if 1 module support controller-device mode
@ -316,109 +324,95 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
// bit 4..bit 15 - not used
uint8_t module_capabilities[2] = {0b00000000, 0b00000111};
static const haier_protocol::HaierMessage DEVICE_VERSION_REQUEST(
(uint8_t) hon_protocol::FrameType::GET_DEVICE_VERSION, module_capabilities, sizeof(module_capabilities));
haier_protocol::FrameType::GET_DEVICE_VERSION, module_capabilities, sizeof(module_capabilities));
this->send_message_(DEVICE_VERSION_REQUEST, this->use_crc_);
this->set_phase(ProtocolPhases::WAITING_INIT_1_ANSWER);
}
break;
case ProtocolPhases::SENDING_INIT_2:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
static const haier_protocol::HaierMessage DEVICEID_REQUEST((uint8_t) hon_protocol::FrameType::GET_DEVICE_ID);
static const haier_protocol::HaierMessage DEVICEID_REQUEST(haier_protocol::FrameType::GET_DEVICE_ID);
this->send_message_(DEVICEID_REQUEST, this->use_crc_);
this->set_phase(ProtocolPhases::WAITING_INIT_2_ANSWER);
}
break;
case ProtocolPhases::SENDING_FIRST_STATUS_REQUEST:
case ProtocolPhases::SENDING_STATUS_REQUEST:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
static const haier_protocol::HaierMessage STATUS_REQUEST(
(uint8_t) hon_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::GET_USER_DATA);
haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::GET_USER_DATA);
this->send_message_(STATUS_REQUEST, this->use_crc_);
this->last_status_request_ = now;
this->set_phase((ProtocolPhases) ((uint8_t) this->protocol_phase_ + 1));
}
break;
#ifdef USE_WIFI
case ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
static const haier_protocol::HaierMessage UPDATE_SIGNAL_REQUEST(
(uint8_t) hon_protocol::FrameType::GET_MANAGEMENT_INFORMATION);
haier_protocol::FrameType::GET_MANAGEMENT_INFORMATION);
this->send_message_(UPDATE_SIGNAL_REQUEST, this->use_crc_);
this->last_signal_request_ = now;
this->set_phase(ProtocolPhases::WAITING_UPDATE_SIGNAL_ANSWER);
}
break;
case ProtocolPhases::SENDING_SIGNAL_LEVEL:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
this->send_message_(this->get_wifi_signal_message_((uint8_t) hon_protocol::FrameType::REPORT_NETWORK_STATUS),
this->use_crc_);
this->set_phase(ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER);
this->send_message_(this->get_wifi_signal_message_(), this->use_crc_);
}
break;
case ProtocolPhases::WAITING_UPDATE_SIGNAL_ANSWER:
case ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER:
break;
#else
case ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST:
case ProtocolPhases::SENDING_SIGNAL_LEVEL:
case ProtocolPhases::WAITING_UPDATE_SIGNAL_ANSWER:
case ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER:
this->set_phase(ProtocolPhases::IDLE);
break;
#endif
case ProtocolPhases::SENDING_ALARM_STATUS_REQUEST:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
static const haier_protocol::HaierMessage ALARM_STATUS_REQUEST(
(uint8_t) hon_protocol::FrameType::GET_ALARM_STATUS);
static const haier_protocol::HaierMessage ALARM_STATUS_REQUEST(haier_protocol::FrameType::GET_ALARM_STATUS);
this->send_message_(ALARM_STATUS_REQUEST, this->use_crc_);
this->set_phase(ProtocolPhases::WAITING_ALARM_STATUS_ANSWER);
}
break;
case ProtocolPhases::SENDING_CONTROL:
if (this->first_control_attempt_) {
this->control_request_timestamp_ = now;
this->first_control_attempt_ = false;
if (this->control_messages_queue_.empty()) {
switch (this->control_method_) {
case HonControlMethod::SET_GROUP_PARAMETERS: {
haier_protocol::HaierMessage control_message = this->get_control_message();
this->control_messages_queue_.push(control_message);
} break;
case HonControlMethod::SET_SINGLE_PARAMETER:
this->fill_control_messages_queue_();
break;
case HonControlMethod::MONITOR_ONLY:
ESP_LOGI(TAG, "AC control is disabled, monitor only");
this->reset_to_idle_();
return;
default:
ESP_LOGW(TAG, "Unsupported control method for hOn protocol!");
this->reset_to_idle_();
return;
}
}
if (this->is_control_message_timeout_exceeded_(now)) {
ESP_LOGW(TAG, "Sending control packet timeout!");
this->set_force_send_control_(false);
if (this->hvac_settings_.valid)
this->hvac_settings_.reset();
this->forced_request_status_ = true;
this->forced_publish_ = true;
this->set_phase(ProtocolPhases::IDLE);
if (this->control_messages_queue_.empty()) {
ESP_LOGW(TAG, "Control message queue is empty!");
this->reset_to_idle_();
} else if (this->can_send_message() && this->is_control_message_interval_exceeded_(now)) {
haier_protocol::HaierMessage control_message = get_control_message();
this->send_message_(control_message, this->use_crc_);
ESP_LOGI(TAG, "Control packet sent");
this->set_phase(ProtocolPhases::WAITING_CONTROL_ANSWER);
ESP_LOGI(TAG, "Sending control packet, queue size %d", this->control_messages_queue_.size());
this->send_message_(this->control_messages_queue_.front(), this->use_crc_, CONTROL_MESSAGE_RETRIES,
CONTROL_MESSAGE_RETRIES_INTERVAL);
}
break;
case ProtocolPhases::SENDING_POWER_ON_COMMAND:
case ProtocolPhases::SENDING_POWER_OFF_COMMAND:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
uint8_t pwr_cmd_buf[2] = {0x00, 0x00};
if (this->protocol_phase_ == ProtocolPhases::SENDING_POWER_ON_COMMAND)
pwr_cmd_buf[1] = 0x01;
haier_protocol::HaierMessage power_cmd((uint8_t) hon_protocol::FrameType::CONTROL,
((uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER) + 1,
pwr_cmd_buf, sizeof(pwr_cmd_buf));
this->send_message_(power_cmd, this->use_crc_);
this->set_phase(this->protocol_phase_ == ProtocolPhases::SENDING_POWER_ON_COMMAND
? ProtocolPhases::WAITING_POWER_ON_ANSWER
: ProtocolPhases::WAITING_POWER_OFF_ANSWER);
case ProtocolPhases::SENDING_ACTION_COMMAND:
if (this->action_request_.has_value()) {
if (this->action_request_.value().message.has_value()) {
this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
this->action_request_.value().message.reset();
} else {
// Message already sent, reseting request and return to idle
this->action_request_.reset();
this->set_phase(ProtocolPhases::IDLE);
}
} else {
ESP_LOGW(TAG, "SENDING_ACTION_COMMAND phase without action request!");
this->set_phase(ProtocolPhases::IDLE);
}
break;
case ProtocolPhases::WAITING_INIT_1_ANSWER:
case ProtocolPhases::WAITING_INIT_2_ANSWER:
case ProtocolPhases::WAITING_FIRST_STATUS_ANSWER:
case ProtocolPhases::WAITING_ALARM_STATUS_ANSWER:
case ProtocolPhases::WAITING_STATUS_ANSWER:
case ProtocolPhases::WAITING_CONTROL_ANSWER:
case ProtocolPhases::WAITING_POWER_ON_ANSWER:
case ProtocolPhases::WAITING_POWER_OFF_ANSWER:
break;
case ProtocolPhases::IDLE: {
if (this->forced_request_status_ || this->is_status_request_interval_exceeded_(now)) {
this->set_phase(ProtocolPhases::SENDING_STATUS_REQUEST);
@ -433,26 +427,35 @@ void HonClimate::process_phase(std::chrono::steady_clock::time_point now) {
} break;
default:
// Shouldn't get here
#if (HAIER_LOG_LEVEL > 4)
ESP_LOGE(TAG, "Wrong protocol handler state: %s (%d), resetting communication",
phase_to_string_(this->protocol_phase_), (int) this->protocol_phase_);
#else
ESP_LOGE(TAG, "Wrong protocol handler state: %d, resetting communication", (int) this->protocol_phase_);
#endif
this->set_phase(ProtocolPhases::SENDING_INIT_1);
break;
}
}
haier_protocol::HaierMessage HonClimate::get_power_message(bool state) {
if (state) {
static haier_protocol::HaierMessage power_on_message(
haier_protocol::FrameType::CONTROL, ((uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER) + 1,
std::initializer_list<uint8_t>({0x00, 0x01}).begin(), 2);
return power_on_message;
} else {
static haier_protocol::HaierMessage power_off_message(
haier_protocol::FrameType::CONTROL, ((uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER) + 1,
std::initializer_list<uint8_t>({0x00, 0x00}).begin(), 2);
return power_off_message;
}
}
haier_protocol::HaierMessage HonClimate::get_control_message() {
uint8_t control_out_buffer[sizeof(hon_protocol::HaierPacketControl)];
memcpy(control_out_buffer, this->last_status_message_.get(), sizeof(hon_protocol::HaierPacketControl));
hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
bool has_hvac_settings = false;
if (this->hvac_settings_.valid) {
if (this->current_hvac_settings_.valid) {
has_hvac_settings = true;
HvacSettings climate_control;
climate_control = this->hvac_settings_;
HvacSettings &climate_control = this->current_hvac_settings_;
if (climate_control.mode.has_value()) {
switch (climate_control.mode.value()) {
case CLIMATE_MODE_OFF:
@ -535,7 +538,7 @@ haier_protocol::HaierMessage HonClimate::get_control_message() {
}
if (climate_control.target_temperature.has_value()) {
float target_temp = climate_control.target_temperature.value();
out_data->set_point = ((int) target_temp) - 16; // set the temperature at our offset, subtract 16.
out_data->set_point = ((int) target_temp) - 16; // set the temperature with offset 16
out_data->half_degree = (target_temp - ((int) target_temp) >= 0.49) ? 1 : 0;
}
if (out_data->ac_power == 0) {
@ -587,50 +590,28 @@ haier_protocol::HaierMessage HonClimate::get_control_message() {
control_out_buffer[4] = 0; // This byte should be cleared before setting values
out_data->display_status = this->display_status_ ? 1 : 0;
out_data->health_mode = this->health_mode_ ? 1 : 0;
switch (this->action_request_) {
case ActionRequest::START_SELF_CLEAN:
this->action_request_ = ActionRequest::NO_ACTION;
out_data->self_cleaning_status = 1;
out_data->steri_clean = 0;
out_data->set_point = 0x06;
out_data->vertical_swing_mode = (uint8_t) hon_protocol::VerticalSwingMode::CENTER;
out_data->horizontal_swing_mode = (uint8_t) hon_protocol::HorizontalSwingMode::CENTER;
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
out_data->light_status = 0;
break;
case ActionRequest::START_STERI_CLEAN:
this->action_request_ = ActionRequest::NO_ACTION;
out_data->self_cleaning_status = 0;
out_data->steri_clean = 1;
out_data->set_point = 0x06;
out_data->vertical_swing_mode = (uint8_t) hon_protocol::VerticalSwingMode::CENTER;
out_data->horizontal_swing_mode = (uint8_t) hon_protocol::HorizontalSwingMode::CENTER;
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
out_data->light_status = 0;
break;
default:
// No change
break;
}
return haier_protocol::HaierMessage((uint8_t) hon_protocol::FrameType::CONTROL,
return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
control_out_buffer, sizeof(hon_protocol::HaierPacketControl));
}
haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *packet_buffer, uint8_t size) {
if (size < sizeof(hon_protocol::HaierStatus))
if (size < hon_protocol::HAIER_STATUS_FRAME_SIZE + this->extra_control_packet_bytes_)
return haier_protocol::HandlerError::WRONG_MESSAGE_STRUCTURE;
hon_protocol::HaierStatus packet;
if (size < sizeof(hon_protocol::HaierStatus))
size = sizeof(hon_protocol::HaierStatus);
memcpy(&packet, packet_buffer, size);
struct {
hon_protocol::HaierPacketControl control;
hon_protocol::HaierPacketSensors sensors;
} packet;
memcpy(&packet.control, packet_buffer + 2, sizeof(hon_protocol::HaierPacketControl));
memcpy(&packet.sensors,
packet_buffer + 2 + sizeof(hon_protocol::HaierPacketControl) + this->extra_control_packet_bytes_,
sizeof(hon_protocol::HaierPacketSensors));
if (packet.sensors.error_status != 0) {
ESP_LOGW(TAG, "HVAC error, code=0x%02X", packet.sensors.error_status);
}
if ((this->outdoor_sensor_ != nullptr) && (got_valid_outdoor_temp_ || (packet.sensors.outdoor_temperature > 0))) {
got_valid_outdoor_temp_ = true;
if ((this->outdoor_sensor_ != nullptr) &&
(this->got_valid_outdoor_temp_ || (packet.sensors.outdoor_temperature > 0))) {
this->got_valid_outdoor_temp_ = true;
float otemp = (float) (packet.sensors.outdoor_temperature + PROTOCOL_OUTDOOR_TEMPERATURE_OFFSET);
if ((!this->outdoor_sensor_->has_state()) || (this->outdoor_sensor_->get_raw_state() != otemp))
this->outdoor_sensor_->publish_state(otemp);
@ -703,7 +684,7 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
// Do something only if display status changed
if (this->mode == CLIMATE_MODE_OFF) {
// AC just turned on from remote need to turn off display
this->set_force_send_control_(true);
this->force_send_control_ = true;
} else {
this->display_status_ = disp_status;
}
@ -732,7 +713,8 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
ESP_LOGD(TAG, "Cleaning status change: %d => %d", (uint8_t) this->cleaning_status_, (uint8_t) new_cleaning);
if (new_cleaning == CleaningState::NO_CLEANING) {
// Turning AC off after cleaning
this->action_request_ = ActionRequest::TURN_POWER_OFF;
this->action_request_ =
PendingAction({ActionRequest::TURN_POWER_OFF, esphome::optional<haier_protocol::HaierMessage>()});
}
this->cleaning_status_ = new_cleaning;
}
@ -783,51 +765,257 @@ haier_protocol::HandlerError HonClimate::process_status_message_(const uint8_t *
should_publish = should_publish || (old_swing_mode != this->swing_mode);
}
this->last_valid_status_timestamp_ = std::chrono::steady_clock::now();
if (this->forced_publish_ || should_publish) {
#if (HAIER_LOG_LEVEL > 4)
std::chrono::high_resolution_clock::time_point _publish_start = std::chrono::high_resolution_clock::now();
#endif
if (should_publish) {
this->publish_state();
#if (HAIER_LOG_LEVEL > 4)
ESP_LOGV(TAG, "Publish delay: %lld ms",
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() -
_publish_start)
.count());
#endif
this->forced_publish_ = false;
}
if (should_publish) {
ESP_LOGI(TAG, "HVAC values changed");
}
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"HVAC Mode = 0x%X", packet.control.ac_mode);
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"Fan speed Status = 0x%X", packet.control.fan_mode);
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"Horizontal Swing Status = 0x%X", packet.control.horizontal_swing_mode);
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"Vertical Swing Status = 0x%X", packet.control.vertical_swing_mode);
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"Set Point Status = 0x%X", packet.control.set_point);
int log_level = should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG;
esp_log_printf_(log_level, TAG, __LINE__, "HVAC Mode = 0x%X", packet.control.ac_mode);
esp_log_printf_(log_level, TAG, __LINE__, "Fan speed Status = 0x%X", packet.control.fan_mode);
esp_log_printf_(log_level, TAG, __LINE__, "Horizontal Swing Status = 0x%X", packet.control.horizontal_swing_mode);
esp_log_printf_(log_level, TAG, __LINE__, "Vertical Swing Status = 0x%X", packet.control.vertical_swing_mode);
esp_log_printf_(log_level, TAG, __LINE__, "Set Point Status = 0x%X", packet.control.set_point);
return haier_protocol::HandlerError::HANDLER_OK;
}
bool HonClimate::is_message_invalid(uint8_t message_type) {
return message_type == (uint8_t) hon_protocol::FrameType::INVALID;
void HonClimate::fill_control_messages_queue_() {
static uint8_t one_buf[] = {0x00, 0x01};
static uint8_t zero_buf[] = {0x00, 0x00};
if (!this->current_hvac_settings_.valid && !this->force_send_control_)
return;
this->clear_control_messages_queue_();
HvacSettings climate_control;
climate_control = this->current_hvac_settings_;
// Beeper command
{
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::BEEPER_STATUS,
this->beeper_status_ ? zero_buf : one_buf, 2));
}
// Health mode
{
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::HEALTH_MODE,
this->health_mode_ ? one_buf : zero_buf, 2));
}
// Climate mode
bool new_power = this->mode != CLIMATE_MODE_OFF;
uint8_t fan_mode_buf[] = {0x00, 0xFF};
uint8_t quiet_mode_buf[] = {0x00, 0xFF};
if (climate_control.mode.has_value()) {
uint8_t buffer[2] = {0x00, 0x00};
switch (climate_control.mode.value()) {
case CLIMATE_MODE_OFF:
new_power = false;
break;
case CLIMATE_MODE_HEAT_COOL:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::AUTO;
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::AC_MODE,
buffer, 2));
fan_mode_buf[1] = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_HEAT:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::HEAT;
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::AC_MODE,
buffer, 2));
fan_mode_buf[1] = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_DRY:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::DRY;
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::AC_MODE,
buffer, 2));
fan_mode_buf[1] = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_FAN_ONLY:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::FAN;
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::AC_MODE,
buffer, 2));
fan_mode_buf[1] = this->other_modes_fan_speed_; // Auto doesn't work in fan only mode
// Disabling eco mode for Fan only
quiet_mode_buf[1] = 0;
break;
case CLIMATE_MODE_COOL:
new_power = true;
buffer[1] = (uint8_t) hon_protocol::ConditioningMode::COOL;
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::AC_MODE,
buffer, 2));
fan_mode_buf[1] = this->other_modes_fan_speed_;
break;
default:
ESP_LOGE("Control", "Unsupported climate mode");
break;
}
}
// Climate power
{
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::AC_POWER,
new_power ? one_buf : zero_buf, 2));
}
// CLimate preset
{
uint8_t fast_mode_buf[] = {0x00, 0xFF};
if (!new_power) {
// If AC is off - no presets allowed
quiet_mode_buf[1] = 0x00;
fast_mode_buf[1] = 0x00;
} else if (climate_control.preset.has_value()) {
switch (climate_control.preset.value()) {
case CLIMATE_PRESET_NONE:
quiet_mode_buf[1] = 0x00;
fast_mode_buf[1] = 0x00;
break;
case CLIMATE_PRESET_ECO:
// Eco is not supported in Fan only mode
quiet_mode_buf[1] = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 0x01 : 0x00;
fast_mode_buf[1] = 0x00;
break;
case CLIMATE_PRESET_BOOST:
quiet_mode_buf[1] = 0x00;
// Boost is not supported in Fan only mode
fast_mode_buf[1] = (this->mode != CLIMATE_MODE_FAN_ONLY) ? 0x01 : 0x00;
break;
default:
ESP_LOGE("Control", "Unsupported preset");
break;
}
}
if (quiet_mode_buf[1] != 0xFF) {
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::QUIET_MODE,
quiet_mode_buf, 2));
}
if (fast_mode_buf[1] != 0xFF) {
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::FAST_MODE,
fast_mode_buf, 2));
}
}
// Target temperature
if (climate_control.target_temperature.has_value()) {
uint8_t buffer[2] = {0x00, 0x00};
buffer[1] = ((uint8_t) climate_control.target_temperature.value()) - 16;
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::SET_POINT,
buffer, 2));
}
// Fan mode
if (climate_control.fan_mode.has_value()) {
switch (climate_control.fan_mode.value()) {
case CLIMATE_FAN_LOW:
fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_LOW;
break;
case CLIMATE_FAN_MEDIUM:
fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_MID;
break;
case CLIMATE_FAN_HIGH:
fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_HIGH;
break;
case CLIMATE_FAN_AUTO:
if (mode != CLIMATE_MODE_FAN_ONLY) // if we are not in fan only mode
fan_mode_buf[1] = (uint8_t) hon_protocol::FanMode::FAN_AUTO;
break;
default:
ESP_LOGE("Control", "Unsupported fan mode");
break;
}
if (fan_mode_buf[1] != 0xFF) {
this->control_messages_queue_.push(
haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL,
(uint16_t) hon_protocol::SubcommandsControl::SET_SINGLE_PARAMETER +
(uint8_t) hon_protocol::DataParameters::FAN_MODE,
fan_mode_buf, 2));
}
}
}
void HonClimate::process_pending_action() {
switch (this->action_request_) {
case ActionRequest::START_SELF_CLEAN:
case ActionRequest::START_STERI_CLEAN:
// Will reset action with control message sending
this->set_phase(ProtocolPhases::SENDING_CONTROL);
break;
void HonClimate::clear_control_messages_queue_() {
while (!this->control_messages_queue_.empty())
this->control_messages_queue_.pop();
}
bool HonClimate::prepare_pending_action() {
switch (this->action_request_.value().action) {
case ActionRequest::START_SELF_CLEAN: {
uint8_t control_out_buffer[sizeof(hon_protocol::HaierPacketControl)];
memcpy(control_out_buffer, this->last_status_message_.get(), sizeof(hon_protocol::HaierPacketControl));
hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
out_data->self_cleaning_status = 1;
out_data->steri_clean = 0;
out_data->set_point = 0x06;
out_data->vertical_swing_mode = (uint8_t) hon_protocol::VerticalSwingMode::CENTER;
out_data->horizontal_swing_mode = (uint8_t) hon_protocol::HorizontalSwingMode::CENTER;
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
out_data->light_status = 0;
this->action_request_.value().message = haier_protocol::HaierMessage(
haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
control_out_buffer, sizeof(hon_protocol::HaierPacketControl));
}
return true;
case ActionRequest::START_STERI_CLEAN: {
uint8_t control_out_buffer[sizeof(hon_protocol::HaierPacketControl)];
memcpy(control_out_buffer, this->last_status_message_.get(), sizeof(hon_protocol::HaierPacketControl));
hon_protocol::HaierPacketControl *out_data = (hon_protocol::HaierPacketControl *) control_out_buffer;
out_data->self_cleaning_status = 0;
out_data->steri_clean = 1;
out_data->set_point = 0x06;
out_data->vertical_swing_mode = (uint8_t) hon_protocol::VerticalSwingMode::CENTER;
out_data->horizontal_swing_mode = (uint8_t) hon_protocol::HorizontalSwingMode::CENTER;
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) hon_protocol::ConditioningMode::DRY;
out_data->light_status = 0;
this->action_request_.value().message = haier_protocol::HaierMessage(
haier_protocol::FrameType::CONTROL, (uint16_t) hon_protocol::SubcommandsControl::SET_GROUP_PARAMETERS,
control_out_buffer, sizeof(hon_protocol::HaierPacketControl));
}
return true;
default:
HaierClimateBase::process_pending_action();
break;
return HaierClimateBase::prepare_pending_action();
}
}
void HonClimate::process_protocol_reset() {
HaierClimateBase::process_protocol_reset();
if (this->outdoor_sensor_ != nullptr) {
this->outdoor_sensor_->publish_state(NAN);
}
this->got_valid_outdoor_temp_ = false;
this->hvac_hardware_info_.reset();
}
} // namespace haier
} // namespace esphome

View File

@ -30,6 +30,8 @@ enum class CleaningState : uint8_t {
STERI_CLEAN = 2,
};
enum class HonControlMethod { MONITOR_ONLY = 0, SET_GROUP_PARAMETERS, SET_SINGLE_PARAMETER };
class HonClimate : public HaierClimateBase {
public:
HonClimate();
@ -48,44 +50,57 @@ class HonClimate : public HaierClimateBase {
CleaningState get_cleaning_status() const;
void start_self_cleaning();
void start_steri_cleaning();
void set_extra_control_packet_bytes_size(size_t size) { this->extra_control_packet_bytes_ = size; };
void set_control_method(HonControlMethod method) { this->control_method_ = method; };
protected:
void set_handlers() override;
void process_phase(std::chrono::steady_clock::time_point now) override;
haier_protocol::HaierMessage get_control_message() override;
bool is_message_invalid(uint8_t message_type) override;
void process_pending_action() override;
haier_protocol::HaierMessage get_power_message(bool state) override;
bool prepare_pending_action() override;
void process_protocol_reset() override;
// Answers handlers
haier_protocol::HandlerError get_device_version_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError get_device_id_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError status_handler_(uint8_t request_type, uint8_t message_type, const uint8_t *data,
haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type, const uint8_t *data,
size_t data_size);
haier_protocol::HandlerError get_management_information_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError get_management_information_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError report_network_status_answer_handler_(uint8_t request_type, uint8_t message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError get_alarm_status_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError get_alarm_status_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
// Helper functions
haier_protocol::HandlerError process_status_message_(const uint8_t *packet, uint8_t size);
std::unique_ptr<uint8_t[]> last_status_message_;
void fill_control_messages_queue_();
void clear_control_messages_queue_();
struct HardwareInfo {
std::string protocol_version_;
std::string software_version_;
std::string hardware_version_;
std::string device_name_;
bool functions_[5];
};
bool beeper_status_;
CleaningState cleaning_status_;
bool got_valid_outdoor_temp_;
AirflowVerticalDirection vertical_direction_;
AirflowHorizontalDirection horizontal_direction_;
bool hvac_hardware_info_available_;
std::string hvac_protocol_version_;
std::string hvac_software_version_;
std::string hvac_hardware_version_;
std::string hvac_device_name_;
bool hvac_functions_[5];
bool &use_crc_;
esphome::optional<HardwareInfo> hvac_hardware_info_;
uint8_t active_alarms_[8];
int extra_control_packet_bytes_;
HonControlMethod control_method_;
esphome::sensor::Sensor *outdoor_sensor_;
std::queue<haier_protocol::HaierMessage> control_messages_queue_;
};
} // namespace haier

View File

@ -35,6 +35,20 @@ enum class ConditioningMode : uint8_t {
FAN = 0x06
};
enum class DataParameters : uint8_t {
AC_POWER = 0x01,
SET_POINT = 0x02,
AC_MODE = 0x04,
FAN_MODE = 0x05,
USE_FAHRENHEIT = 0x07,
TEN_DEGREE = 0x0A,
HEALTH_MODE = 0x0B,
BEEPER_STATUS = 0x16,
LOCK_REMOTE = 0x17,
QUIET_MODE = 0x19,
FAST_MODE = 0x1A,
};
enum class SpecialMode : uint8_t { NONE = 0x00, ELDERLY = 0x01, CHILDREN = 0x02, PREGNANT = 0x03 };
enum class FanMode : uint8_t { FAN_HIGH = 0x01, FAN_MID = 0x02, FAN_LOW = 0x03, FAN_AUTO = 0x05 };
@ -124,11 +138,7 @@ struct HaierPacketSensors {
uint16_t co2_value; // CO2 value (0 PPM - 10000 PPM, 1 PPM step)
};
struct HaierStatus {
uint16_t subcommand;
HaierPacketControl control;
HaierPacketSensors sensors;
};
constexpr size_t HAIER_STATUS_FRAME_SIZE = 2 + sizeof(HaierPacketControl) + sizeof(HaierPacketSensors);
struct DeviceVersionAnswer {
char protocol_version[8];
@ -140,76 +150,6 @@ struct DeviceVersionAnswer {
uint8_t functions[2];
};
// In this section comments:
// - module is the ESP32 control module (communication module in Haier protocol document)
// - device is the conditioner control board (network appliances in Haier protocol document)
enum class FrameType : uint8_t {
CONTROL = 0x01, // Requests or sets one or multiple parameters (module <-> device, required)
STATUS = 0x02, // Contains one or multiple parameters values, usually answer to control frame (module <-> device,
// required)
INVALID = 0x03, // Communication error indication (module <-> device, required)
ALARM_STATUS = 0x04, // Alarm status report (module <-> device, interactive, required)
CONFIRM = 0x05, // Acknowledgment, usually used to confirm reception of frame if there is no special answer (module
// <-> device, required)
REPORT = 0x06, // Report frame (module <-> device, interactive, required)
STOP_FAULT_ALARM = 0x09, // Stop fault alarm frame (module -> device, interactive, required)
SYSTEM_DOWNLINK = 0x11, // System downlink frame (module -> device, optional)
DEVICE_UPLINK = 0x12, // Device uplink frame (module <- device , interactive, optional)
SYSTEM_QUERY = 0x13, // System query frame (module -> device, optional)
SYSTEM_QUERY_RESPONSE = 0x14, // System query response frame (module <- device , optional)
DEVICE_QUERY = 0x15, // Device query frame (module <- device, optional)
DEVICE_QUERY_RESPONSE = 0x16, // Device query response frame (module -> device, optional)
GROUP_COMMAND = 0x60, // Group command frame (module -> device, interactive, optional)
GET_DEVICE_VERSION = 0x61, // Requests device version (module -> device, required)
GET_DEVICE_VERSION_RESPONSE = 0x62, // Device version answer (module <- device, required_
GET_ALL_ADDRESSES = 0x67, // Requests all devices addresses (module -> device, interactive, optional)
GET_ALL_ADDRESSES_RESPONSE =
0x68, // Answer to request of all devices addresses (module <- device , interactive, optional)
HANDSET_CHANGE_NOTIFICATION = 0x69, // Handset change notification frame (module <- device , interactive, optional)
GET_DEVICE_ID = 0x70, // Requests Device ID (module -> device, required)
GET_DEVICE_ID_RESPONSE = 0x71, // Response to device ID request (module <- device , required)
GET_ALARM_STATUS = 0x73, // Alarm status request (module -> device, required)
GET_ALARM_STATUS_RESPONSE = 0x74, // Response to alarm status request (module <- device, required)
GET_DEVICE_CONFIGURATION = 0x7C, // Requests device configuration (module -> device, interactive, required)
GET_DEVICE_CONFIGURATION_RESPONSE =
0x7D, // Response to device configuration request (module <- device, interactive, required)
DOWNLINK_TRANSPARENT_TRANSMISSION = 0x8C, // Downlink transparent transmission (proxy data Haier cloud -> device)
// (module -> device, interactive, optional)
UPLINK_TRANSPARENT_TRANSMISSION = 0x8D, // Uplink transparent transmission (proxy data device -> Haier cloud) (module
// <- device, interactive, optional)
START_DEVICE_UPGRADE = 0xE1, // Initiate device OTA upgrade (module -> device, OTA required)
START_DEVICE_UPGRADE_RESPONSE = 0xE2, // Response to initiate device upgrade command (module <- device, OTA required)
GET_FIRMWARE_CONTENT = 0xE5, // Requests to send firmware (module <- device, OTA required)
GET_FIRMWARE_CONTENT_RESPONSE =
0xE6, // Response to send firmware request (module -> device, OTA required) (multipacket?)
CHANGE_BAUD_RATE = 0xE7, // Requests to change port baud rate (module <- device, OTA required)
CHANGE_BAUD_RATE_RESPONSE = 0xE8, // Response to change port baud rate request (module -> device, OTA required)
GET_SUBBOARD_INFO = 0xE9, // Requests subboard information (module -> device, required)
GET_SUBBOARD_INFO_RESPONSE = 0xEA, // Response to subboard information request (module <- device, required)
GET_HARDWARE_INFO = 0xEB, // Requests information about device and subboard (module -> device, required)
GET_HARDWARE_INFO_RESPONSE = 0xEC, // Response to hardware information request (module <- device, required)
GET_UPGRADE_RESULT = 0xED, // Requests result of the firmware update (module <- device, OTA required)
GET_UPGRADE_RESULT_RESPONSE = 0xEF, // Response to firmware update results request (module -> device, OTA required)
GET_NETWORK_STATUS = 0xF0, // Requests network status (module <- device, interactive, optional)
GET_NETWORK_STATUS_RESPONSE = 0xF1, // Response to network status request (module -> device, interactive, optional)
START_WIFI_CONFIGURATION = 0xF2, // Starts WiFi configuration procedure (module <- device, interactive, required)
START_WIFI_CONFIGURATION_RESPONSE =
0xF3, // Response to start WiFi configuration request (module -> device, interactive, required)
STOP_WIFI_CONFIGURATION = 0xF4, // Stop WiFi configuration procedure (module <- device, interactive, required)
STOP_WIFI_CONFIGURATION_RESPONSE =
0xF5, // Response to stop WiFi configuration request (module -> device, interactive, required)
REPORT_NETWORK_STATUS = 0xF7, // Reports network status (module -> device, required)
CLEAR_CONFIGURATION = 0xF8, // Request to clear module configuration (module <- device, interactive, optional)
BIG_DATA_REPORT_CONFIGURATION =
0xFA, // Configuration for autoreport device full status (module -> device, interactive, optional)
BIG_DATA_REPORT_CONFIGURATION_RESPONSE =
0xFB, // Response to set big data configuration (module <- device, interactive, optional)
GET_MANAGEMENT_INFORMATION = 0xFC, // Request management information from device (module -> device, required)
GET_MANAGEMENT_INFORMATION_RESPONSE =
0xFD, // Response to management information request (module <- device, required)
WAKE_UP = 0xFE, // Request to wake up (module <-> device, optional)
};
enum class SubcommandsControl : uint16_t {
GET_PARAMETERS = 0x4C01, // Request specific parameters (packet content: parameter ID1 + parameter ID2 + ...)
GET_USER_DATA = 0x4D01, // Request all user data from device (packet content: None)

View File

@ -12,21 +12,28 @@ namespace haier {
static const char *const TAG = "haier.climate";
constexpr size_t SIGNAL_LEVEL_UPDATE_INTERVAL_MS = 10000;
constexpr uint8_t CONTROL_MESSAGE_RETRIES = 5;
constexpr std::chrono::milliseconds CONTROL_MESSAGE_RETRIES_INTERVAL = std::chrono::milliseconds(500);
constexpr uint8_t INIT_REQUESTS_RETRY = 2;
constexpr std::chrono::milliseconds INIT_REQUESTS_RETRY_INTERVAL = std::chrono::milliseconds(2000);
Smartair2Climate::Smartair2Climate()
: last_status_message_(new uint8_t[sizeof(smartair2_protocol::HaierPacketControl)]), timeouts_counter_(0) {}
Smartair2Climate::Smartair2Climate() {
last_status_message_ = std::unique_ptr<uint8_t[]>(new uint8_t[sizeof(smartair2_protocol::HaierPacketControl)]);
}
haier_protocol::HandlerError Smartair2Climate::status_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError Smartair2Climate::status_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size) {
haier_protocol::HandlerError result =
this->answer_preprocess_(request_type, (uint8_t) smartair2_protocol::FrameType::CONTROL, message_type,
(uint8_t) smartair2_protocol::FrameType::STATUS, ProtocolPhases::UNKNOWN);
this->answer_preprocess_(request_type, haier_protocol::FrameType::CONTROL, message_type,
haier_protocol::FrameType::STATUS, ProtocolPhases::UNKNOWN);
if (result == haier_protocol::HandlerError::HANDLER_OK) {
result = this->process_status_message_(data, data_size);
if (result != haier_protocol::HandlerError::HANDLER_OK) {
ESP_LOGW(TAG, "Error %d while parsing Status packet", (int) result);
this->set_phase((this->protocol_phase_ >= ProtocolPhases::IDLE) ? ProtocolPhases::IDLE
: ProtocolPhases::SENDING_FIRST_STATUS_REQUEST);
this->reset_phase_();
this->action_request_.reset();
this->force_send_control_ = false;
} else {
if (data_size >= sizeof(smartair2_protocol::HaierPacketControl) + 2) {
memcpy(this->last_status_message_.get(), data + 2, sizeof(smartair2_protocol::HaierPacketControl));
@ -34,36 +41,45 @@ haier_protocol::HandlerError Smartair2Climate::status_handler_(uint8_t request_t
ESP_LOGW(TAG, "Status packet too small: %d (should be >= %d)", data_size,
sizeof(smartair2_protocol::HaierPacketControl));
}
if (this->protocol_phase_ == ProtocolPhases::WAITING_FIRST_STATUS_ANSWER) {
ESP_LOGI(TAG, "First HVAC status received");
this->set_phase(ProtocolPhases::IDLE);
} else if (this->protocol_phase_ == ProtocolPhases::WAITING_STATUS_ANSWER) {
this->set_phase(ProtocolPhases::IDLE);
} else if (this->protocol_phase_ == ProtocolPhases::WAITING_CONTROL_ANSWER) {
this->set_phase(ProtocolPhases::IDLE);
this->set_force_send_control_(false);
if (this->hvac_settings_.valid)
this->hvac_settings_.reset();
switch (this->protocol_phase_) {
case ProtocolPhases::SENDING_FIRST_STATUS_REQUEST:
ESP_LOGI(TAG, "First HVAC status received");
this->set_phase(ProtocolPhases::IDLE);
break;
case ProtocolPhases::SENDING_ACTION_COMMAND:
// Do nothing, phase will be changed in process_phase
break;
case ProtocolPhases::SENDING_STATUS_REQUEST:
this->set_phase(ProtocolPhases::IDLE);
break;
case ProtocolPhases::SENDING_CONTROL:
this->set_phase(ProtocolPhases::IDLE);
this->force_send_control_ = false;
if (this->current_hvac_settings_.valid)
this->current_hvac_settings_.reset();
break;
default:
break;
}
}
return result;
} else {
this->set_phase((this->protocol_phase_ >= ProtocolPhases::IDLE) ? ProtocolPhases::IDLE
: ProtocolPhases::SENDING_FIRST_STATUS_REQUEST);
this->action_request_.reset();
this->force_send_control_ = false;
this->reset_phase_();
return result;
}
}
haier_protocol::HandlerError Smartair2Climate::get_device_version_answer_handler_(uint8_t request_type,
uint8_t message_type,
const uint8_t *data,
size_t data_size) {
if (request_type != (uint8_t) smartair2_protocol::FrameType::GET_DEVICE_VERSION)
haier_protocol::HandlerError Smartair2Climate::get_device_version_answer_handler_(
haier_protocol::FrameType request_type, haier_protocol::FrameType message_type, const uint8_t *data,
size_t data_size) {
if (request_type != haier_protocol::FrameType::GET_DEVICE_VERSION)
return haier_protocol::HandlerError::UNSUPPORTED_MESSAGE;
if (ProtocolPhases::WAITING_INIT_1_ANSWER != this->protocol_phase_)
if (ProtocolPhases::SENDING_INIT_1 != this->protocol_phase_)
return haier_protocol::HandlerError::UNEXPECTED_MESSAGE;
// Invalid packet is expected answer
if ((message_type == (uint8_t) smartair2_protocol::FrameType::GET_DEVICE_VERSION_RESPONSE) && (data_size >= 39) &&
if ((message_type == haier_protocol::FrameType::GET_DEVICE_VERSION_RESPONSE) && (data_size >= 39) &&
((data[37] & 0x04) != 0)) {
ESP_LOGW(TAG, "It looks like your ESPHome Haier climate configuration is wrong. You should use the hOn protocol "
"instead of smartAir2");
@ -72,58 +88,35 @@ haier_protocol::HandlerError Smartair2Climate::get_device_version_answer_handler
return haier_protocol::HandlerError::HANDLER_OK;
}
haier_protocol::HandlerError Smartair2Climate::report_network_status_answer_handler_(uint8_t request_type,
uint8_t message_type,
const uint8_t *data,
size_t data_size) {
haier_protocol::HandlerError result = this->answer_preprocess_(
request_type, (uint8_t) smartair2_protocol::FrameType::REPORT_NETWORK_STATUS, message_type,
(uint8_t) smartair2_protocol::FrameType::CONFIRM, ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER);
this->set_phase(ProtocolPhases::IDLE);
return result;
}
haier_protocol::HandlerError Smartair2Climate::initial_messages_timeout_handler_(uint8_t message_type) {
haier_protocol::HandlerError Smartair2Climate::messages_timeout_handler_with_cycle_for_init_(
haier_protocol::FrameType message_type) {
if (this->protocol_phase_ >= ProtocolPhases::IDLE)
return HaierClimateBase::timeout_default_handler_(message_type);
this->timeouts_counter_++;
ESP_LOGI(TAG, "Answer timeout for command %02X, phase %d, timeout counter %d", message_type,
(int) this->protocol_phase_, this->timeouts_counter_);
if (this->timeouts_counter_ >= 3) {
ProtocolPhases new_phase = (ProtocolPhases) ((int) this->protocol_phase_ + 1);
if (new_phase >= ProtocolPhases::SENDING_ALARM_STATUS_REQUEST)
new_phase = ProtocolPhases::SENDING_INIT_1;
this->set_phase(new_phase);
} else {
// Returning to the previous state to try again
this->set_phase((ProtocolPhases) ((int) this->protocol_phase_ - 1));
}
ESP_LOGI(TAG, "Answer timeout for command %02X, phase %s", (uint8_t) message_type,
phase_to_string_(this->protocol_phase_));
ProtocolPhases new_phase = (ProtocolPhases) ((int) this->protocol_phase_ + 1);
if (new_phase >= ProtocolPhases::SENDING_ALARM_STATUS_REQUEST)
new_phase = ProtocolPhases::SENDING_INIT_1;
this->set_phase(new_phase);
return haier_protocol::HandlerError::HANDLER_OK;
}
void Smartair2Climate::set_handlers() {
// Set handlers
this->haier_protocol_.set_answer_handler(
(uint8_t) (smartair2_protocol::FrameType::GET_DEVICE_VERSION),
haier_protocol::FrameType::GET_DEVICE_VERSION,
std::bind(&Smartair2Climate::get_device_version_answer_handler_, this, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
this->haier_protocol_.set_answer_handler(
(uint8_t) (smartair2_protocol::FrameType::CONTROL),
haier_protocol::FrameType::CONTROL,
std::bind(&Smartair2Climate::status_handler_, this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4));
this->haier_protocol_.set_answer_handler(
(uint8_t) (smartair2_protocol::FrameType::REPORT_NETWORK_STATUS),
haier_protocol::FrameType::REPORT_NETWORK_STATUS,
std::bind(&Smartair2Climate::report_network_status_answer_handler_, this, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
this->haier_protocol_.set_timeout_handler(
(uint8_t) (smartair2_protocol::FrameType::GET_DEVICE_ID),
std::bind(&Smartair2Climate::initial_messages_timeout_handler_, this, std::placeholders::_1));
this->haier_protocol_.set_timeout_handler(
(uint8_t) (smartair2_protocol::FrameType::GET_DEVICE_VERSION),
std::bind(&Smartair2Climate::initial_messages_timeout_handler_, this, std::placeholders::_1));
this->haier_protocol_.set_timeout_handler(
(uint8_t) (smartair2_protocol::FrameType::CONTROL),
std::bind(&Smartair2Climate::initial_messages_timeout_handler_, this, std::placeholders::_1));
this->haier_protocol_.set_default_timeout_handler(
std::bind(&Smartair2Climate::messages_timeout_handler_with_cycle_for_init_, this, std::placeholders::_1));
}
void Smartair2Climate::dump_config() {
@ -134,9 +127,7 @@ void Smartair2Climate::dump_config() {
void Smartair2Climate::process_phase(std::chrono::steady_clock::time_point now) {
switch (this->protocol_phase_) {
case ProtocolPhases::SENDING_INIT_1:
if (this->can_send_message() &&
(((this->timeouts_counter_ == 0) && (this->is_protocol_initialisation_interval_exceeded_(now))) ||
((this->timeouts_counter_ > 0) && (this->is_message_interval_exceeded_(now))))) {
if (this->can_send_message() && this->is_protocol_initialisation_interval_exceeded_(now)) {
// Indicate device capabilities:
// bit 0 - if 1 module support interactive mode
// bit 1 - if 1 module support controller-device mode
@ -145,92 +136,65 @@ void Smartair2Climate::process_phase(std::chrono::steady_clock::time_point now)
// bit 4..bit 15 - not used
uint8_t module_capabilities[2] = {0b00000000, 0b00000111};
static const haier_protocol::HaierMessage DEVICE_VERSION_REQUEST(
(uint8_t) smartair2_protocol::FrameType::GET_DEVICE_VERSION, module_capabilities,
sizeof(module_capabilities));
this->send_message_(DEVICE_VERSION_REQUEST, false);
this->set_phase(ProtocolPhases::WAITING_INIT_1_ANSWER);
haier_protocol::FrameType::GET_DEVICE_VERSION, module_capabilities, sizeof(module_capabilities));
this->send_message_(DEVICE_VERSION_REQUEST, this->use_crc_, INIT_REQUESTS_RETRY, INIT_REQUESTS_RETRY_INTERVAL);
}
break;
case ProtocolPhases::SENDING_INIT_2:
case ProtocolPhases::WAITING_INIT_2_ANSWER:
this->set_phase(ProtocolPhases::SENDING_FIRST_STATUS_REQUEST);
break;
case ProtocolPhases::SENDING_FIRST_STATUS_REQUEST:
case ProtocolPhases::SENDING_STATUS_REQUEST:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
static const haier_protocol::HaierMessage STATUS_REQUEST((uint8_t) smartair2_protocol::FrameType::CONTROL,
0x4D01);
this->send_message_(STATUS_REQUEST, false);
static const haier_protocol::HaierMessage STATUS_REQUEST(haier_protocol::FrameType::CONTROL, 0x4D01);
if (this->protocol_phase_ == ProtocolPhases::SENDING_FIRST_STATUS_REQUEST) {
this->send_message_(STATUS_REQUEST, this->use_crc_, INIT_REQUESTS_RETRY, INIT_REQUESTS_RETRY_INTERVAL);
} else {
this->send_message_(STATUS_REQUEST, this->use_crc_);
}
this->last_status_request_ = now;
this->set_phase((ProtocolPhases) ((uint8_t) this->protocol_phase_ + 1));
}
break;
#ifdef USE_WIFI
case ProtocolPhases::SENDING_SIGNAL_LEVEL:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
this->send_message_(
this->get_wifi_signal_message_((uint8_t) smartair2_protocol::FrameType::REPORT_NETWORK_STATUS), false);
this->send_message_(this->get_wifi_signal_message_(), this->use_crc_);
this->last_signal_request_ = now;
this->set_phase(ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER);
}
break;
case ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER:
break;
#else
case ProtocolPhases::SENDING_SIGNAL_LEVEL:
case ProtocolPhases::WAITING_SIGNAL_LEVEL_ANSWER:
this->set_phase(ProtocolPhases::IDLE);
break;
#endif
case ProtocolPhases::SENDING_UPDATE_SIGNAL_REQUEST:
case ProtocolPhases::WAITING_UPDATE_SIGNAL_ANSWER:
this->set_phase(ProtocolPhases::SENDING_SIGNAL_LEVEL);
break;
case ProtocolPhases::SENDING_ALARM_STATUS_REQUEST:
case ProtocolPhases::WAITING_ALARM_STATUS_ANSWER:
this->set_phase(ProtocolPhases::SENDING_INIT_1);
break;
case ProtocolPhases::SENDING_CONTROL:
if (this->first_control_attempt_) {
this->control_request_timestamp_ = now;
this->first_control_attempt_ = false;
if (this->can_send_message() && this->is_control_message_interval_exceeded_(now)) {
ESP_LOGI(TAG, "Sending control packet");
this->send_message_(get_control_message(), this->use_crc_, CONTROL_MESSAGE_RETRIES,
CONTROL_MESSAGE_RETRIES_INTERVAL);
}
if (this->is_control_message_timeout_exceeded_(now)) {
ESP_LOGW(TAG, "Sending control packet timeout!");
this->set_force_send_control_(false);
if (this->hvac_settings_.valid)
this->hvac_settings_.reset();
this->forced_request_status_ = true;
this->forced_publish_ = true;
break;
case ProtocolPhases::SENDING_ACTION_COMMAND:
if (this->action_request_.has_value()) {
if (this->action_request_.value().message.has_value()) {
this->send_message_(this->action_request_.value().message.value(), this->use_crc_);
this->action_request_.value().message.reset();
} else {
// Message already sent, reseting request and return to idle
this->action_request_.reset();
this->set_phase(ProtocolPhases::IDLE);
}
} else {
ESP_LOGW(TAG, "SENDING_ACTION_COMMAND phase without action request!");
this->set_phase(ProtocolPhases::IDLE);
} else if (this->can_send_message() && this->is_control_message_interval_exceeded_(
now)) // Using CONTROL_MESSAGES_INTERVAL_MS to speedup requests
{
haier_protocol::HaierMessage control_message = get_control_message();
this->send_message_(control_message, false);
ESP_LOGI(TAG, "Control packet sent");
this->set_phase(ProtocolPhases::WAITING_CONTROL_ANSWER);
}
break;
case ProtocolPhases::SENDING_POWER_ON_COMMAND:
case ProtocolPhases::SENDING_POWER_OFF_COMMAND:
if (this->can_send_message() && this->is_message_interval_exceeded_(now)) {
haier_protocol::HaierMessage power_cmd(
(uint8_t) smartair2_protocol::FrameType::CONTROL,
this->protocol_phase_ == ProtocolPhases::SENDING_POWER_ON_COMMAND ? 0x4D02 : 0x4D03);
this->send_message_(power_cmd, false);
this->set_phase(this->protocol_phase_ == ProtocolPhases::SENDING_POWER_ON_COMMAND
? ProtocolPhases::WAITING_POWER_ON_ANSWER
: ProtocolPhases::WAITING_POWER_OFF_ANSWER);
}
break;
case ProtocolPhases::WAITING_INIT_1_ANSWER:
case ProtocolPhases::WAITING_FIRST_STATUS_ANSWER:
case ProtocolPhases::WAITING_STATUS_ANSWER:
case ProtocolPhases::WAITING_CONTROL_ANSWER:
case ProtocolPhases::WAITING_POWER_ON_ANSWER:
case ProtocolPhases::WAITING_POWER_OFF_ANSWER:
break;
case ProtocolPhases::IDLE: {
if (this->forced_request_status_ || this->is_status_request_interval_exceeded_(now)) {
this->set_phase(ProtocolPhases::SENDING_STATUS_REQUEST);
@ -245,55 +209,55 @@ void Smartair2Climate::process_phase(std::chrono::steady_clock::time_point now)
} break;
default:
// Shouldn't get here
#if (HAIER_LOG_LEVEL > 4)
ESP_LOGE(TAG, "Wrong protocol handler state: %s (%d), resetting communication",
phase_to_string_(this->protocol_phase_), (int) this->protocol_phase_);
#else
ESP_LOGE(TAG, "Wrong protocol handler state: %d, resetting communication", (int) this->protocol_phase_);
#endif
this->set_phase(ProtocolPhases::SENDING_INIT_1);
break;
}
}
haier_protocol::HaierMessage Smartair2Climate::get_power_message(bool state) {
if (state) {
static haier_protocol::HaierMessage power_on_message(haier_protocol::FrameType::CONTROL, 0x4D02);
return power_on_message;
} else {
static haier_protocol::HaierMessage power_off_message(haier_protocol::FrameType::CONTROL, 0x4D03);
return power_off_message;
}
}
haier_protocol::HaierMessage Smartair2Climate::get_control_message() {
uint8_t control_out_buffer[sizeof(smartair2_protocol::HaierPacketControl)];
memcpy(control_out_buffer, this->last_status_message_.get(), sizeof(smartair2_protocol::HaierPacketControl));
smartair2_protocol::HaierPacketControl *out_data = (smartair2_protocol::HaierPacketControl *) control_out_buffer;
out_data->cntrl = 0;
if (this->hvac_settings_.valid) {
HvacSettings climate_control;
climate_control = this->hvac_settings_;
if (this->current_hvac_settings_.valid) {
HvacSettings &climate_control = this->current_hvac_settings_;
if (climate_control.mode.has_value()) {
switch (climate_control.mode.value()) {
case CLIMATE_MODE_OFF:
out_data->ac_power = 0;
break;
case CLIMATE_MODE_HEAT_COOL:
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) smartair2_protocol::ConditioningMode::AUTO;
out_data->fan_mode = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_HEAT:
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) smartair2_protocol::ConditioningMode::HEAT;
out_data->fan_mode = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_DRY:
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) smartair2_protocol::ConditioningMode::DRY;
out_data->fan_mode = this->other_modes_fan_speed_;
break;
case CLIMATE_MODE_FAN_ONLY:
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) smartair2_protocol::ConditioningMode::FAN;
out_data->fan_mode = this->fan_mode_speed_; // Auto doesn't work in fan only mode
break;
case CLIMATE_MODE_COOL:
out_data->ac_power = 1;
out_data->ac_mode = (uint8_t) smartair2_protocol::ConditioningMode::COOL;
@ -327,32 +291,49 @@ haier_protocol::HaierMessage Smartair2Climate::get_control_message() {
}
// Set swing mode
if (climate_control.swing_mode.has_value()) {
switch (climate_control.swing_mode.value()) {
case CLIMATE_SWING_OFF:
out_data->use_swing_bits = 0;
out_data->swing_both = 0;
break;
case CLIMATE_SWING_VERTICAL:
out_data->swing_both = 0;
out_data->vertical_swing = 1;
out_data->horizontal_swing = 0;
break;
case CLIMATE_SWING_HORIZONTAL:
out_data->swing_both = 0;
out_data->vertical_swing = 0;
out_data->horizontal_swing = 1;
break;
case CLIMATE_SWING_BOTH:
out_data->swing_both = 1;
out_data->use_swing_bits = 0;
out_data->vertical_swing = 0;
out_data->horizontal_swing = 0;
break;
if (this->use_alternative_swing_control_) {
switch (climate_control.swing_mode.value()) {
case CLIMATE_SWING_OFF:
out_data->swing_mode = 0;
break;
case CLIMATE_SWING_VERTICAL:
out_data->swing_mode = 1;
break;
case CLIMATE_SWING_HORIZONTAL:
out_data->swing_mode = 2;
break;
case CLIMATE_SWING_BOTH:
out_data->swing_mode = 3;
break;
}
} else {
switch (climate_control.swing_mode.value()) {
case CLIMATE_SWING_OFF:
out_data->use_swing_bits = 0;
out_data->swing_mode = 0;
break;
case CLIMATE_SWING_VERTICAL:
out_data->swing_mode = 0;
out_data->vertical_swing = 1;
out_data->horizontal_swing = 0;
break;
case CLIMATE_SWING_HORIZONTAL:
out_data->swing_mode = 0;
out_data->vertical_swing = 0;
out_data->horizontal_swing = 1;
break;
case CLIMATE_SWING_BOTH:
out_data->swing_mode = 1;
out_data->use_swing_bits = 0;
out_data->vertical_swing = 0;
out_data->horizontal_swing = 0;
break;
}
}
}
if (climate_control.target_temperature.has_value()) {
float target_temp = climate_control.target_temperature.value();
out_data->set_point = target_temp - 16; // set the temperature with offset 16
out_data->set_point = ((int) target_temp) - 16; // set the temperature with offset 16
out_data->half_degree = (target_temp - ((int) target_temp) >= 0.49) ? 1 : 0;
}
if (out_data->ac_power == 0) {
@ -383,7 +364,7 @@ haier_protocol::HaierMessage Smartair2Climate::get_control_message() {
}
out_data->display_status = this->display_status_ ? 0 : 1;
out_data->health_mode = this->health_mode_ ? 1 : 0;
return haier_protocol::HaierMessage((uint8_t) smartair2_protocol::FrameType::CONTROL, 0x4D5F, control_out_buffer,
return haier_protocol::HaierMessage(haier_protocol::FrameType::CONTROL, 0x4D5F, control_out_buffer,
sizeof(smartair2_protocol::HaierPacketControl));
}
@ -459,13 +440,19 @@ haier_protocol::HandlerError Smartair2Climate::process_status_message_(const uin
// Do something only if display status changed
if (this->mode == CLIMATE_MODE_OFF) {
// AC just turned on from remote need to turn off display
this->set_force_send_control_(true);
this->force_send_control_ = true;
} else {
this->display_status_ = disp_status;
}
}
}
}
{
// Health mode
bool old_health_mode = this->health_mode_;
this->health_mode_ = packet.control.health_mode == 1;
should_publish = should_publish || (old_health_mode != this->health_mode_);
}
{
// Climate mode
ClimateMode old_mode = this->mode;
@ -493,70 +480,57 @@ haier_protocol::HandlerError Smartair2Climate::process_status_message_(const uin
}
should_publish = should_publish || (old_mode != this->mode);
}
{
// Health mode
bool old_health_mode = this->health_mode_;
this->health_mode_ = packet.control.health_mode == 1;
should_publish = should_publish || (old_health_mode != this->health_mode_);
}
{
// Swing mode
ClimateSwingMode old_swing_mode = this->swing_mode;
if (packet.control.swing_both == 0) {
if (packet.control.vertical_swing != 0) {
this->swing_mode = CLIMATE_SWING_VERTICAL;
} else if (packet.control.horizontal_swing != 0) {
this->swing_mode = CLIMATE_SWING_HORIZONTAL;
} else {
this->swing_mode = CLIMATE_SWING_OFF;
if (this->use_alternative_swing_control_) {
switch (packet.control.swing_mode) {
case 1:
this->swing_mode = CLIMATE_SWING_VERTICAL;
break;
case 2:
this->swing_mode = CLIMATE_SWING_HORIZONTAL;
break;
case 3:
this->swing_mode = CLIMATE_SWING_BOTH;
break;
default:
this->swing_mode = CLIMATE_SWING_OFF;
break;
}
} else {
swing_mode = CLIMATE_SWING_BOTH;
if (packet.control.swing_mode == 0) {
if (packet.control.vertical_swing != 0) {
this->swing_mode = CLIMATE_SWING_VERTICAL;
} else if (packet.control.horizontal_swing != 0) {
this->swing_mode = CLIMATE_SWING_HORIZONTAL;
} else {
this->swing_mode = CLIMATE_SWING_OFF;
}
} else {
swing_mode = CLIMATE_SWING_BOTH;
}
}
should_publish = should_publish || (old_swing_mode != this->swing_mode);
}
this->last_valid_status_timestamp_ = std::chrono::steady_clock::now();
if (this->forced_publish_ || should_publish) {
#if (HAIER_LOG_LEVEL > 4)
std::chrono::high_resolution_clock::time_point _publish_start = std::chrono::high_resolution_clock::now();
#endif
if (should_publish) {
this->publish_state();
#if (HAIER_LOG_LEVEL > 4)
ESP_LOGV(TAG, "Publish delay: %lld ms",
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() -
_publish_start)
.count());
#endif
this->forced_publish_ = false;
}
if (should_publish) {
ESP_LOGI(TAG, "HVAC values changed");
}
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"HVAC Mode = 0x%X", packet.control.ac_mode);
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"Fan speed Status = 0x%X", packet.control.fan_mode);
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"Horizontal Swing Status = 0x%X", packet.control.horizontal_swing);
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"Vertical Swing Status = 0x%X", packet.control.vertical_swing);
esp_log_printf_((should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG), TAG, __LINE__,
"Set Point Status = 0x%X", packet.control.set_point);
int log_level = should_publish ? ESPHOME_LOG_LEVEL_INFO : ESPHOME_LOG_LEVEL_DEBUG;
esp_log_printf_(log_level, TAG, __LINE__, "HVAC Mode = 0x%X", packet.control.ac_mode);
esp_log_printf_(log_level, TAG, __LINE__, "Fan speed Status = 0x%X", packet.control.fan_mode);
esp_log_printf_(log_level, TAG, __LINE__, "Horizontal Swing Status = 0x%X", packet.control.horizontal_swing);
esp_log_printf_(log_level, TAG, __LINE__, "Vertical Swing Status = 0x%X", packet.control.vertical_swing);
esp_log_printf_(log_level, TAG, __LINE__, "Set Point Status = 0x%X", packet.control.set_point);
return haier_protocol::HandlerError::HANDLER_OK;
}
bool Smartair2Climate::is_message_invalid(uint8_t message_type) {
return message_type == (uint8_t) smartair2_protocol::FrameType::INVALID;
}
void Smartair2Climate::set_phase(HaierClimateBase::ProtocolPhases phase) {
int old_phase = (int) this->protocol_phase_;
int new_phase = (int) phase;
int min_p = std::min(old_phase, new_phase);
int max_p = std::max(old_phase, new_phase);
if ((min_p % 2 != 0) || (max_p - min_p > 1))
this->timeouts_counter_ = 0;
HaierClimateBase::set_phase(phase);
void Smartair2Climate::set_alternative_swing_control(bool swing_control) {
this->use_alternative_swing_control_ = swing_control;
}
} // namespace haier

View File

@ -13,27 +13,27 @@ class Smartair2Climate : public HaierClimateBase {
Smartair2Climate &operator=(const Smartair2Climate &) = delete;
~Smartair2Climate();
void dump_config() override;
void set_alternative_swing_control(bool swing_control);
protected:
void set_handlers() override;
void process_phase(std::chrono::steady_clock::time_point now) override;
haier_protocol::HaierMessage get_power_message(bool state) override;
haier_protocol::HaierMessage get_control_message() override;
bool is_message_invalid(uint8_t message_type) override;
void set_phase(HaierClimateBase::ProtocolPhases phase) override;
// Answer and timeout handlers
haier_protocol::HandlerError status_handler_(uint8_t request_type, uint8_t message_type, const uint8_t *data,
// Answer handlers
haier_protocol::HandlerError status_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type, const uint8_t *data,
size_t data_size);
haier_protocol::HandlerError get_device_version_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError get_device_version_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError get_device_id_answer_handler_(uint8_t request_type, uint8_t message_type,
haier_protocol::HandlerError get_device_id_answer_handler_(haier_protocol::FrameType request_type,
haier_protocol::FrameType message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError report_network_status_answer_handler_(uint8_t request_type, uint8_t message_type,
const uint8_t *data, size_t data_size);
haier_protocol::HandlerError initial_messages_timeout_handler_(uint8_t message_type);
haier_protocol::HandlerError messages_timeout_handler_with_cycle_for_init_(haier_protocol::FrameType message_type);
// Helper functions
haier_protocol::HandlerError process_status_message_(const uint8_t *packet, uint8_t size);
std::unique_ptr<uint8_t[]> last_status_message_;
unsigned int timeouts_counter_;
bool use_alternative_swing_control_;
};
} // namespace haier

View File

@ -41,8 +41,9 @@ struct HaierPacketControl {
// 24
uint8_t : 8;
// 25
uint8_t swing_both; // If 1 - swing both direction, if 0 - horizontal_swing and vertical_swing define
// vertical/horizontal/off
uint8_t swing_mode; // In normal mode: If 1 - swing both direction, if 0 - horizontal_swing and
// vertical_swing define vertical/horizontal/off
// In alternative mode: 0 - off, 01 - vertical, 02 - horizontal, 03 - both
// 26
uint8_t : 3;
uint8_t use_fahrenheit : 1;
@ -82,19 +83,6 @@ struct HaierStatus {
HaierPacketControl control;
};
enum class FrameType : uint8_t {
CONTROL = 0x01,
STATUS = 0x02,
INVALID = 0x03,
CONFIRM = 0x05,
GET_DEVICE_VERSION = 0x61,
GET_DEVICE_VERSION_RESPONSE = 0x62,
GET_DEVICE_ID = 0x70,
GET_DEVICE_ID_RESPONSE = 0x71,
REPORT_NETWORK_STATUS = 0xF7,
NO_COMMAND = 0xFF,
};
} // namespace smartair2_protocol
} // namespace haier
} // namespace esphome

View File

@ -3,6 +3,7 @@ import esphome.config_validation as cv
from esphome import automation
from esphome.automation import maybe_simple_id
from esphome.components import fan, output
from esphome.components.fan import validate_preset_modes
from esphome.const import (
CONF_ID,
CONF_DECAY_MODE,
@ -10,6 +11,7 @@ from esphome.const import (
CONF_PIN_A,
CONF_PIN_B,
CONF_ENABLE_PIN,
CONF_PRESET_MODES,
)
from .. import hbridge_ns
@ -28,7 +30,6 @@ DECAY_MODE_OPTIONS = {
# Actions
BrakeAction = hbridge_ns.class_("BrakeAction", automation.Action)
CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
{
cv.GenerateID(CONF_ID): cv.declare_id(HBridgeFan),
@ -39,6 +40,7 @@ CONFIG_SCHEMA = fan.FAN_SCHEMA.extend(
),
cv.Optional(CONF_SPEED_COUNT, default=100): cv.int_range(min=1),
cv.Optional(CONF_ENABLE_PIN): cv.use_id(output.FloatOutput),
cv.Optional(CONF_PRESET_MODES): validate_preset_modes,
}
).extend(cv.COMPONENT_SCHEMA)
@ -69,3 +71,6 @@ async def to_code(config):
if CONF_ENABLE_PIN in config:
enable_pin = await cg.get_variable(config[CONF_ENABLE_PIN])
cg.add(var.set_enable_pin(enable_pin))
if CONF_PRESET_MODES in config:
cg.add(var.set_preset_modes(config[CONF_PRESET_MODES]))

View File

@ -33,7 +33,12 @@ void HBridgeFan::setup() {
restore->apply(*this);
this->write_state_();
}
// Construct traits
this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_);
this->traits_.set_supported_preset_modes(this->preset_modes_);
}
void HBridgeFan::dump_config() {
LOG_FAN("", "H-Bridge Fan", this);
if (this->decay_mode_ == DECAY_MODE_SLOW) {
@ -42,9 +47,7 @@ void HBridgeFan::dump_config() {
ESP_LOGCONFIG(TAG, " Decay Mode: Fast");
}
}
fan::FanTraits HBridgeFan::get_traits() {
return fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_);
}
void HBridgeFan::control(const fan::FanCall &call) {
if (call.get_state().has_value())
this->state = *call.get_state();
@ -54,10 +57,12 @@ void HBridgeFan::control(const fan::FanCall &call) {
this->oscillating = *call.get_oscillating();
if (call.get_direction().has_value())
this->direction = *call.get_direction();
this->preset_mode = call.get_preset_mode();
this->write_state_();
this->publish_state();
}
void HBridgeFan::write_state_() {
float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
if (speed == 0.0f) { // off means idle

View File

@ -1,5 +1,7 @@
#pragma once
#include <set>
#include "esphome/core/automation.h"
#include "esphome/components/output/binary_output.h"
#include "esphome/components/output/float_output.h"
@ -20,10 +22,11 @@ class HBridgeFan : public Component, public fan::Fan {
void set_pin_a(output::FloatOutput *pin_a) { pin_a_ = pin_a; }
void set_pin_b(output::FloatOutput *pin_b) { pin_b_ = pin_b; }
void set_enable_pin(output::FloatOutput *enable) { enable_ = enable; }
void set_preset_modes(const std::set<std::string> &presets) { preset_modes_ = presets; }
void setup() override;
void dump_config() override;
fan::FanTraits get_traits() override;
fan::FanTraits get_traits() override { return this->traits_; }
fan::FanCall brake();
@ -34,6 +37,8 @@ class HBridgeFan : public Component, public fan::Fan {
output::BinaryOutput *oscillating_{nullptr};
int speed_count_{};
DecayMode decay_mode_{DECAY_MODE_SLOW};
fan::FanTraits traits_;
std::set<std::string> preset_modes_{};
void control(const fan::FanCall &call) override;
void write_state_();

View File

@ -0,0 +1 @@
CODEOWNERS = ["@clydebarrow"]

View File

@ -0,0 +1,47 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import cover, uart
from esphome.const import (
CONF_CLOSE_DURATION,
CONF_ID,
CONF_OPEN_DURATION,
)
he60r_ns = cg.esphome_ns.namespace("he60r")
HE60rCover = he60r_ns.class_("HE60rCover", cover.Cover, cg.Component)
CONFIG_SCHEMA = (
cover.COVER_SCHEMA.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
.extend(
{
cv.GenerateID(): cv.declare_id(HE60rCover),
cv.Optional(
CONF_OPEN_DURATION, default="15s"
): cv.positive_time_period_milliseconds,
cv.Optional(
CONF_CLOSE_DURATION, default="15s"
): cv.positive_time_period_milliseconds,
}
)
)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
"he60r",
baud_rate=1200,
require_tx=True,
require_rx=True,
data_bits=8,
parity="EVEN",
stop_bits=1,
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await cover.register_cover(var, config)
await uart.register_uart_device(var, config)
cg.add(var.set_close_duration(config[CONF_CLOSE_DURATION]))
cg.add(var.set_open_duration(config[CONF_OPEN_DURATION]))

View File

@ -0,0 +1,265 @@
#include "he60r.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome {
namespace he60r {
static const char *const TAG = "he60r.cover";
static const uint8_t QUERY_BYTE = 0x38;
static const uint8_t TOGGLE_BYTE = 0x30;
using namespace esphome::cover;
void HE60rCover::setup() {
auto restore = this->restore_state_();
if (restore.has_value()) {
restore->apply(this);
this->publish_state(false);
} else {
// if no other information, assume half open
this->position = 0.5f;
}
this->current_operation = COVER_OPERATION_IDLE;
this->last_recompute_time_ = this->start_dir_time_ = millis();
this->set_interval(300, [this]() { this->update_(); });
}
CoverTraits HE60rCover::get_traits() {
auto traits = CoverTraits();
traits.set_supports_stop(true);
traits.set_supports_position(true);
traits.set_supports_toggle(true);
traits.set_is_assumed_state(false);
return traits;
}
void HE60rCover::dump_config() {
LOG_COVER("", "HE60R Cover", this);
this->check_uart_settings(1200, 1, uart::UART_CONFIG_PARITY_EVEN, 8);
ESP_LOGCONFIG(TAG, " Open Duration: %.1fs", this->open_duration_ / 1e3f);
ESP_LOGCONFIG(TAG, " Close Duration: %.1fs", this->close_duration_ / 1e3f);
auto restore = this->restore_state_();
if (restore.has_value())
ESP_LOGCONFIG(TAG, " Saved position %d%%", (int) (restore->position * 100.f));
}
void HE60rCover::endstop_reached_(CoverOperation operation) {
const uint32_t now = millis();
this->set_current_operation_(COVER_OPERATION_IDLE);
auto new_position = operation == COVER_OPERATION_OPENING ? COVER_OPEN : COVER_CLOSED;
if (new_position != this->position || this->current_operation != COVER_OPERATION_IDLE) {
this->position = new_position;
this->current_operation = COVER_OPERATION_IDLE;
if (this->last_command_ == operation) {
float dur = (now - this->start_dir_time_) / 1e3f;
ESP_LOGD(TAG, "'%s' - %s endstop reached. Took %.1fs.", this->name_.c_str(),
operation == COVER_OPERATION_OPENING ? "Open" : "Close", dur);
}
this->publish_state();
}
}
void HE60rCover::set_current_operation_(cover::CoverOperation operation) {
if (this->current_operation != operation) {
this->current_operation = operation;
if (operation != COVER_OPERATION_IDLE)
this->last_recompute_time_ = millis();
this->publish_state();
}
}
void HE60rCover::process_rx_(uint8_t data) {
ESP_LOGV(TAG, "Process RX data %X", data);
if (!this->query_seen_) {
this->query_seen_ = data == QUERY_BYTE;
if (!this->query_seen_)
ESP_LOGD(TAG, "RX Byte %02X", data);
return;
}
switch (data) {
case 0xB5: // at closed endstop, jammed?
case 0xF5: // at closed endstop, jammed?
case 0x55: // at closed endstop
this->next_direction_ = COVER_OPERATION_OPENING;
this->endstop_reached_(COVER_OPERATION_CLOSING);
break;
case 0x52: // at opened endstop
this->next_direction_ = COVER_OPERATION_CLOSING;
this->endstop_reached_(COVER_OPERATION_OPENING);
break;
case 0x51: // travelling up after encountering obstacle
case 0x01: // travelling up
case 0x11: // travelling up, triggered by remote
this->set_current_operation_(COVER_OPERATION_OPENING);
this->next_direction_ = COVER_OPERATION_IDLE;
break;
case 0x44: // travelling down
case 0x14: // travelling down, triggered by remote
this->next_direction_ = COVER_OPERATION_IDLE;
this->set_current_operation_(COVER_OPERATION_CLOSING);
break;
case 0x86: // Stopped, jammed?
case 0x16: // stopped midway while opening, by remote
case 0x06: // stopped midway while opening
this->next_direction_ = COVER_OPERATION_CLOSING;
this->set_current_operation_(COVER_OPERATION_IDLE);
break;
case 0x10: // stopped midway while closing, by remote
case 0x00: // stopped midway while closing
this->next_direction_ = COVER_OPERATION_OPENING;
this->set_current_operation_(COVER_OPERATION_IDLE);
break;
default:
break;
}
}
void HE60rCover::update_() {
if (toggles_needed_ != 0) {
if ((this->counter_++ & 0x3) == 0) {
toggles_needed_--;
ESP_LOGD(TAG, "Writing byte 0x30, still needed=%d", toggles_needed_);
this->write_byte(TOGGLE_BYTE);
} else {
this->write_byte(QUERY_BYTE);
}
} else {
this->write_byte(QUERY_BYTE);
this->counter_ = 0;
}
if (this->current_operation != COVER_OPERATION_IDLE) {
this->recompute_position_();
// if we initiated the move, check if we reached the target position
if (this->last_command_ != COVER_OPERATION_IDLE) {
if (this->is_at_target_()) {
this->start_direction_(COVER_OPERATION_IDLE);
}
}
}
}
void HE60rCover::loop() {
uint8_t data;
while (this->available() > 0) {
if (this->read_byte(&data)) {
this->process_rx_(data);
}
}
}
void HE60rCover::control(const CoverCall &call) {
if (call.get_stop()) {
this->start_direction_(COVER_OPERATION_IDLE);
} else if (call.get_toggle().has_value()) {
// toggle action logic: OPEN - STOP - CLOSE
if (this->last_command_ != COVER_OPERATION_IDLE) {
this->start_direction_(COVER_OPERATION_IDLE);
} else {
this->toggles_needed_++;
}
} else if (call.get_position().has_value()) {
// go to position action
auto pos = *call.get_position();
// are we at the target?
if (pos == this->position) {
this->start_direction_(COVER_OPERATION_IDLE);
} else {
this->target_position_ = pos;
this->start_direction_(pos < this->position ? COVER_OPERATION_CLOSING : COVER_OPERATION_OPENING);
}
}
}
/**
* Check if the cover has reached or passed the target position. This is used only
* for partial open/close requests - endstops are used for full open/close.
* @return True if the cover has reached or passed its target position. For full open/close target always return false.
*/
bool HE60rCover::is_at_target_() const {
// equality of floats is fraught with peril - this is reliable since the values are 0.0 or 1.0 which are
// exactly representable.
if (this->target_position_ == COVER_OPEN || this->target_position_ == COVER_CLOSED)
return false;
// aiming for an intermediate position - exact comparison here will not work and we need to allow for overshoot
switch (this->last_command_) {
case COVER_OPERATION_OPENING:
return this->position >= this->target_position_;
case COVER_OPERATION_CLOSING:
return this->position <= this->target_position_;
case COVER_OPERATION_IDLE:
return this->current_operation == COVER_OPERATION_IDLE;
default:
return true;
}
}
void HE60rCover::start_direction_(CoverOperation dir) {
this->last_command_ = dir;
if (this->current_operation == dir)
return;
ESP_LOGD(TAG, "'%s' - Direction '%s' requested.", this->name_.c_str(),
dir == COVER_OPERATION_OPENING ? "OPEN"
: dir == COVER_OPERATION_CLOSING ? "CLOSE"
: "STOP");
if (dir == this->next_direction_) {
// either moving and needs to stop, or stopped and will move correctly on one trigger
this->toggles_needed_ = 1;
} else {
if (this->current_operation == COVER_OPERATION_IDLE) {
// if stopped, but will go the wrong way, need 3 triggers.
this->toggles_needed_ = 3;
} else {
// just stop and reverse
this->toggles_needed_ = 2;
}
ESP_LOGD(TAG, "'%s' - Reversing direction.", this->name_.c_str());
}
this->start_dir_time_ = millis();
}
void HE60rCover::recompute_position_() {
if (this->current_operation == COVER_OPERATION_IDLE)
return;
const uint32_t now = millis();
float dir;
float action_dur;
switch (this->current_operation) {
case COVER_OPERATION_OPENING:
dir = 1.0f;
action_dur = this->open_duration_;
break;
case COVER_OPERATION_CLOSING:
dir = -1.0f;
action_dur = this->close_duration_;
break;
default:
return;
}
if (now > this->last_recompute_time_) {
auto diff = now - last_recompute_time_;
auto delta = dir * diff / action_dur;
// make sure our guesstimate never reaches full open or close.
this->position = clamp(delta + this->position, COVER_CLOSED + 0.01f, COVER_OPEN - 0.01f);
ESP_LOGD(TAG, "Recompute %dms, dir=%f, action_dur=%f, delta=%f, pos=%f", (int) diff, dir, action_dur, delta,
this->position);
this->last_recompute_time_ = now;
this->publish_state();
}
}
} // namespace he60r
} // namespace esphome

View File

@ -0,0 +1,47 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/uart/uart.h"
#include "esphome/components/cover/cover.h"
namespace esphome {
namespace he60r {
class HE60rCover : public cover::Cover, public Component, public uart::UARTDevice {
public:
void setup() override;
void loop() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; };
void set_open_duration(uint32_t duration) { this->open_duration_ = duration; }
void set_close_duration(uint32_t duration) { this->close_duration_ = duration; }
cover::CoverTraits get_traits() override;
protected:
void update_();
void control(const cover::CoverCall &call) override;
bool is_at_target_() const;
void start_direction_(cover::CoverOperation dir);
void update_operation_(cover::CoverOperation dir);
void endstop_reached_(cover::CoverOperation operation);
void recompute_position_();
void set_current_operation_(cover::CoverOperation operation);
void process_rx_(uint8_t data);
uint32_t open_duration_{0};
uint32_t close_duration_{0};
uint32_t toggles_needed_{0};
cover::CoverOperation next_direction_{cover::COVER_OPERATION_IDLE};
cover::CoverOperation last_command_{cover::COVER_OPERATION_IDLE};
uint32_t last_recompute_time_{0};
uint32_t start_dir_time_{0};
float target_position_{0};
bool query_seen_{};
uint8_t counter_{};
};
} // namespace he60r
} // namespace esphome

View File

@ -3,7 +3,6 @@
#ifdef USE_ARDUINO
#include "esphome/components/remote_base/remote_base.h"
#include "esphome/components/remote_transmitter/remote_transmitter.h"
#include <IRSender.h> // arduino-heatpump library
namespace esphome {
@ -11,14 +10,13 @@ namespace heatpumpir {
class IRSenderESPHome : public IRSender {
public:
IRSenderESPHome(remote_transmitter::RemoteTransmitterComponent *transmitter)
: IRSender(0), transmit_(transmitter->transmit()){};
IRSenderESPHome(remote_base::RemoteTransmitterBase *transmitter) : IRSender(0), transmit_(transmitter->transmit()){};
void setFrequency(int frequency) override; // NOLINT(readability-identifier-naming)
void space(int space_length) override;
void mark(int mark_length) override;
protected:
remote_transmitter::RemoteTransmitterComponent::TransmitCall transmit_;
remote_base::RemoteTransmitterBase::TransmitCall transmit_;
};
} // namespace heatpumpir

View File

@ -17,10 +17,8 @@ import esphome.codegen as cg
from .const import host_ns
_LOGGER = logging.getLogger(__name__)
HostGPIOPin = host_ns.class_("HostGPIOPin", cg.InternalGPIOPin)
@ -45,21 +43,10 @@ def validate_gpio_pin(value):
return _translate_pin(value)
HOST_PIN_SCHEMA = cv.All(
{
cv.GenerateID(): cv.declare_id(HostGPIOPin),
cv.Required(CONF_NUMBER): validate_gpio_pin,
cv.Optional(CONF_MODE, default={}): cv.Schema(
{
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
cv.Optional(CONF_OPEN_DRAIN, default=False): cv.boolean,
cv.Optional(CONF_PULLUP, default=False): cv.boolean,
cv.Optional(CONF_PULLDOWN, default=False): cv.boolean,
}
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
},
HOST_PIN_SCHEMA = pins.gpio_base_schema(
HostGPIOPin,
validate_gpio_pin,
modes=[CONF_INPUT, CONF_OUTPUT, CONF_OPEN_DRAIN, CONF_PULLUP, CONF_PULLDOWN],
)

View File

@ -39,9 +39,8 @@ def _bus_declare_type(value):
raise NotImplementedError
pin_with_input_and_output_support = cv.All(
pins.internal_gpio_pin_number({CONF_INPUT: True}),
pins.internal_gpio_pin_number({CONF_OUTPUT: True}),
pin_with_input_and_output_support = pins.internal_gpio_pin_number(
{CONF_OUTPUT: True, CONF_INPUT: True}
)

View File

@ -2,6 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import core, pins
from esphome.components import display, spi, font
from esphome.components.display import validate_rotation
from esphome.core import CORE, HexInt
from esphome.const import (
CONF_COLOR_PALETTE,
@ -13,6 +14,9 @@ from esphome.const import (
CONF_PAGES,
CONF_RESET_PIN,
CONF_DIMENSIONS,
CONF_WIDTH,
CONF_HEIGHT,
CONF_ROTATION,
)
DEPENDENCIES = ["spi"]
@ -26,28 +30,39 @@ def AUTO_LOAD():
CODEOWNERS = ["@nielsnl68", "@clydebarrow"]
ili9XXX_ns = cg.esphome_ns.namespace("ili9xxx")
ili9XXXSPI = ili9XXX_ns.class_(
"ILI9XXXDisplay", cg.PollingComponent, spi.SPIDevice, display.DisplayBuffer
ili9xxx_ns = cg.esphome_ns.namespace("ili9xxx")
ILI9XXXDisplay = ili9xxx_ns.class_(
"ILI9XXXDisplay",
cg.PollingComponent,
spi.SPIDevice,
display.Display,
display.DisplayBuffer,
)
ILI9XXXColorMode = ili9XXX_ns.enum("ILI9XXXColorMode")
ILI9XXXColorMode = ili9xxx_ns.enum("ILI9XXXColorMode")
ColorOrder = display.display_ns.enum("ColorMode")
MODELS = {
"M5STACK": ili9XXX_ns.class_("ILI9XXXM5Stack", ili9XXXSPI),
"M5CORE": ili9XXX_ns.class_("ILI9XXXM5CORE", ili9XXXSPI),
"TFT_2.4": ili9XXX_ns.class_("ILI9XXXILI9341", ili9XXXSPI),
"TFT_2.4R": ili9XXX_ns.class_("ILI9XXXILI9342", ili9XXXSPI),
"ILI9341": ili9XXX_ns.class_("ILI9XXXILI9341", ili9XXXSPI),
"ILI9342": ili9XXX_ns.class_("ILI9XXXILI9342", ili9XXXSPI),
"ILI9481": ili9XXX_ns.class_("ILI9XXXILI9481", ili9XXXSPI),
"ILI9481-18": ili9XXX_ns.class_("ILI9XXXILI948118", ili9XXXSPI),
"ILI9486": ili9XXX_ns.class_("ILI9XXXILI9486", ili9XXXSPI),
"ILI9488": ili9XXX_ns.class_("ILI9XXXILI9488", ili9XXXSPI),
"ILI9488_A": ili9XXX_ns.class_("ILI9XXXILI9488A", ili9XXXSPI),
"ST7796": ili9XXX_ns.class_("ILI9XXXST7796", ili9XXXSPI),
"S3BOX": ili9XXX_ns.class_("ILI9XXXS3Box", ili9XXXSPI),
"S3BOX_LITE": ili9XXX_ns.class_("ILI9XXXS3BoxLite", ili9XXXSPI),
"M5STACK": ili9xxx_ns.class_("ILI9XXXM5Stack", ILI9XXXDisplay),
"M5CORE": ili9xxx_ns.class_("ILI9XXXM5CORE", ILI9XXXDisplay),
"TFT_2.4": ili9xxx_ns.class_("ILI9XXXILI9341", ILI9XXXDisplay),
"TFT_2.4R": ili9xxx_ns.class_("ILI9XXXILI9342", ILI9XXXDisplay),
"ILI9341": ili9xxx_ns.class_("ILI9XXXILI9341", ILI9XXXDisplay),
"ILI9342": ili9xxx_ns.class_("ILI9XXXILI9342", ILI9XXXDisplay),
"ILI9481": ili9xxx_ns.class_("ILI9XXXILI9481", ILI9XXXDisplay),
"ILI9481-18": ili9xxx_ns.class_("ILI9XXXILI948118", ILI9XXXDisplay),
"ILI9486": ili9xxx_ns.class_("ILI9XXXILI9486", ILI9XXXDisplay),
"ILI9488": ili9xxx_ns.class_("ILI9XXXILI9488", ILI9XXXDisplay),
"ILI9488_A": ili9xxx_ns.class_("ILI9XXXILI9488A", ILI9XXXDisplay),
"ST7796": ili9xxx_ns.class_("ILI9XXXST7796", ILI9XXXDisplay),
"ST7789V": ili9xxx_ns.class_("ILI9XXXST7789V", ILI9XXXDisplay),
"S3BOX": ili9xxx_ns.class_("ILI9XXXS3Box", ILI9XXXDisplay),
"S3BOX_LITE": ili9xxx_ns.class_("ILI9XXXS3BoxLite", ILI9XXXDisplay),
}
COLOR_ORDERS = {
"RGB": ColorOrder.COLOR_ORDER_RGB,
"BGR": ColorOrder.COLOR_ORDER_BGR,
}
COLOR_PALETTE = cv.one_of("NONE", "GRAYSCALE", "IMAGE_ADAPTIVE")
@ -55,6 +70,14 @@ COLOR_PALETTE = cv.one_of("NONE", "GRAYSCALE", "IMAGE_ADAPTIVE")
CONF_LED_PIN = "led_pin"
CONF_COLOR_PALETTE_IMAGES = "color_palette_images"
CONF_INVERT_DISPLAY = "invert_display"
CONF_INVERT_COLORS = "invert_colors"
CONF_MIRROR_X = "mirror_x"
CONF_MIRROR_Y = "mirror_y"
CONF_SWAP_XY = "swap_xy"
CONF_COLOR_ORDER = "color_order"
CONF_OFFSET_HEIGHT = "offset_height"
CONF_OFFSET_WIDTH = "offset_width"
CONF_TRANSFORM = "transform"
def _validate(config):
@ -77,6 +100,7 @@ def _validate(config):
"TFT_2.4R",
"ILI9341",
"ILI9342",
"ST7789V",
]:
raise cv.Invalid(
"Provided model can't run on ESP8266. Use an ESP32 with PSRAM onboard"
@ -88,9 +112,19 @@ CONFIG_SCHEMA = cv.All(
font.validate_pillow_installed,
display.FULL_DISPLAY_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(ili9XXXSPI),
cv.GenerateID(): cv.declare_id(ILI9XXXDisplay),
cv.Required(CONF_MODEL): cv.enum(MODELS, upper=True, space="_"),
cv.Optional(CONF_DIMENSIONS): cv.dimensions,
cv.Optional(CONF_DIMENSIONS): cv.Any(
cv.dimensions,
cv.Schema(
{
cv.Required(CONF_WIDTH): cv.int_,
cv.Required(CONF_HEIGHT): cv.int_,
cv.Optional(CONF_OFFSET_HEIGHT, default=0): cv.int_,
cv.Optional(CONF_OFFSET_WIDTH, default=0): cv.int_,
}
),
),
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_LED_PIN): cv.invalid(
@ -101,7 +135,19 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_COLOR_PALETTE_IMAGES, default=[]): cv.ensure_list(
cv.file_
),
cv.Optional(CONF_INVERT_DISPLAY): cv.boolean,
cv.Optional(CONF_INVERT_DISPLAY): cv.invalid(
"'invert_display' has been replaced by 'invert_colors'"
),
cv.Optional(CONF_INVERT_COLORS): cv.boolean,
cv.Optional(CONF_COLOR_ORDER): cv.one_of(*COLOR_ORDERS.keys(), upper=True),
cv.Exclusive(CONF_ROTATION, CONF_ROTATION): validate_rotation,
cv.Exclusive(CONF_TRANSFORM, CONF_ROTATION): cv.Schema(
{
cv.Optional(CONF_SWAP_XY, default=False): cv.boolean,
cv.Optional(CONF_MIRROR_X, default=False): cv.boolean,
cv.Optional(CONF_MIRROR_Y, default=False): cv.boolean,
}
),
}
)
.extend(cv.polling_component_schema("1s"))
@ -115,11 +161,17 @@ async def to_code(config):
rhs = MODELS[config[CONF_MODEL]].new()
var = cg.Pvariable(config[CONF_ID], rhs)
await cg.register_component(var, config)
await display.register_display(var, config)
await spi.register_spi_device(var, config)
dc = await cg.gpio_pin_expression(config[CONF_DC_PIN])
cg.add(var.set_dc_pin(dc))
if CONF_COLOR_ORDER in config:
cg.add(var.set_color_order(COLOR_ORDERS[config[CONF_COLOR_ORDER]]))
if CONF_TRANSFORM in config:
transform = config[CONF_TRANSFORM]
cg.add(var.set_swap_xy(transform[CONF_SWAP_XY]))
cg.add(var.set_mirror_x(transform[CONF_MIRROR_X]))
cg.add(var.set_mirror_y(transform[CONF_MIRROR_Y]))
if CONF_LAMBDA in config:
lambda_ = await cg.process_lambda(
@ -132,9 +184,17 @@ async def to_code(config):
cg.add(var.set_reset_pin(reset))
if CONF_DIMENSIONS in config:
cg.add(
var.set_dimentions(config[CONF_DIMENSIONS][0], config[CONF_DIMENSIONS][1])
)
dimensions = config[CONF_DIMENSIONS]
if isinstance(dimensions, dict):
cg.add(var.set_dimensions(dimensions[CONF_WIDTH], dimensions[CONF_HEIGHT]))
cg.add(
var.set_offsets(
dimensions[CONF_OFFSET_WIDTH], dimensions[CONF_OFFSET_HEIGHT]
)
)
else:
(width, height) = dimensions
cg.add(var.set_dimensions(width, height))
rhs = None
if config[CONF_COLOR_PALETTE] == "GRAYSCALE":
@ -179,5 +239,5 @@ async def to_code(config):
prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs)
cg.add(var.set_palette(prog_arr))
if CONF_INVERT_DISPLAY in config:
cg.add(var.invert_display(config[CONF_INVERT_DISPLAY]))
if CONF_INVERT_COLORS in config:
cg.add(var.invert_colors(config[CONF_INVERT_COLORS]))

View File

@ -8,11 +8,31 @@ namespace esphome {
namespace ili9xxx {
static const char *const TAG = "ili9xxx";
static const uint16_t SPI_SETUP_US = 100; // estimated fixed overhead in microseconds for an SPI write
static const uint16_t SPI_MAX_BLOCK_SIZE = 4092; // Max size of continuous SPI transfer
// store a 16 bit value in a buffer, big endian.
static inline void put16_be(uint8_t *buf, uint16_t value) {
buf[0] = value >> 8;
buf[1] = value;
}
void ILI9XXXDisplay::setup() {
ESP_LOGD(TAG, "Setting up ILI9xxx");
this->setup_pins_();
this->initialize();
this->command(this->pre_invertdisplay_ ? ILI9XXX_INVON : ILI9XXX_INVOFF);
this->init_lcd_();
this->command(this->pre_invertcolors_ ? ILI9XXX_INVON : ILI9XXX_INVOFF);
// custom x/y transform and color order
uint8_t mad = this->color_order_ == display::COLOR_ORDER_BGR ? MADCTL_BGR : MADCTL_RGB;
if (this->swap_xy_)
mad |= MADCTL_MV;
if (this->mirror_x_)
mad |= MADCTL_MX;
if (this->mirror_y_)
mad |= MADCTL_MY;
this->send_command(ILI9XXX_MADCTL, &mad, 1);
this->x_low_ = this->width_;
this->y_low_ = this->height_;
@ -47,6 +67,8 @@ void ILI9XXXDisplay::setup_pins_() {
void ILI9XXXDisplay::dump_config() {
LOG_DISPLAY("", "ili9xxx", this);
ESP_LOGCONFIG(TAG, " Width Offset: %u", this->offset_x_);
ESP_LOGCONFIG(TAG, " Height Offset: %u", this->offset_y_);
switch (this->buffer_color_mode_) {
case BITS_8_INDEXED:
ESP_LOGCONFIG(TAG, " Color mode: 8bit Indexed");
@ -64,8 +86,12 @@ void ILI9XXXDisplay::dump_config() {
ESP_LOGCONFIG(TAG, " Data rate: %dMHz", (unsigned) (this->data_rate_ / 1000000));
LOG_PIN(" Reset Pin: ", this->reset_pin_);
LOG_PIN(" CS Pin: ", this->cs_);
LOG_PIN(" DC Pin: ", this->dc_pin_);
LOG_PIN(" Busy Pin: ", this->busy_pin_);
ESP_LOGCONFIG(TAG, " Swap_xy: %s", YESNO(this->swap_xy_));
ESP_LOGCONFIG(TAG, " Mirror_x: %s", YESNO(this->mirror_x_));
ESP_LOGCONFIG(TAG, " Mirror_y: %s", YESNO(this->mirror_y_));
if (this->is_failed()) {
ESP_LOGCONFIG(TAG, " => Failed to init Memory: YES!");
@ -141,12 +167,14 @@ void HOT ILI9XXXDisplay::draw_absolute_pixel_internal(int x, int y, Color color)
}
if (updated) {
// low and high watermark may speed up drawing from buffer
this->x_low_ = (x < this->x_low_) ? x : this->x_low_;
this->y_low_ = (y < this->y_low_) ? y : this->y_low_;
this->x_high_ = (x > this->x_high_) ? x : this->x_high_;
this->y_high_ = (y > this->y_high_) ? y : this->y_high_;
// ESP_LOGVV(TAG, "=>>> pixel (x:%d, y:%d) (xl:%d, xh:%d, yl:%d, yh:%d", x, y, this->x_low_, this->x_high_,
// this->y_low_, this->y_high_);
if (x < this->x_low_)
this->x_low_ = x;
if (y < this->y_low_)
this->y_low_ = y;
if (x > this->x_high_)
this->x_high_ = x;
if (y > this->y_high_)
this->y_high_ = y;
}
}
@ -165,59 +193,82 @@ void ILI9XXXDisplay::update() {
}
void ILI9XXXDisplay::display_() {
// we will only update the changed window to the display
uint16_t w = this->x_high_ - this->x_low_ + 1; // NOLINT
uint16_t h = this->y_high_ - this->y_low_ + 1; // NOLINT
uint32_t start_pos = ((this->y_low_ * this->width_) + x_low_);
uint8_t transfer_buffer[ILI9XXX_TRANSFER_BUFFER_SIZE];
// check if something was displayed
if ((this->x_high_ < this->x_low_) || (this->y_high_ < this->y_low_)) {
ESP_LOGV(TAG, "Nothing to display");
return;
}
set_addr_window_(this->x_low_, this->y_low_, w, h);
// we will only update the changed rows to the display
size_t const w = this->x_high_ - this->x_low_ + 1;
size_t const h = this->y_high_ - this->y_low_ + 1;
size_t mhz = this->data_rate_ / 1000000;
// estimate time for a single write
size_t sw_time = this->width_ * h * 16 / mhz + this->width_ * h * 2 / SPI_MAX_BLOCK_SIZE * SPI_SETUP_US * 2;
// estimate time for multiple writes
size_t mw_time = (w * h * 16) / mhz + w * h * 2 / ILI9XXX_TRANSFER_BUFFER_SIZE * SPI_SETUP_US;
ESP_LOGV(TAG,
"Start display(xlow:%d, ylow:%d, xhigh:%d, yhigh:%d, width:%d, "
"heigth:%d, start_pos:%" PRId32 ")",
this->x_low_, this->y_low_, this->x_high_, this->y_high_, w, h, start_pos);
this->start_data_();
for (uint16_t row = 0; row < h; row++) {
uint32_t pos = start_pos + (row * width_);
uint32_t rem = w;
while (rem > 0) {
uint32_t sz = std::min(rem, ILI9XXX_TRANSFER_BUFFER_SIZE);
// ESP_LOGVV(TAG, "Send to display(pos:%d, rem:%d, zs:%d)", pos, rem, sz);
buffer_to_transfer_(pos, sz);
if (this->is_18bitdisplay_) {
for (uint32_t i = 0; i < sz; ++i) {
uint16_t color_val = transfer_buffer_[i];
uint8_t red = color_val & 0x1F;
uint8_t green = (color_val & 0x7E0) >> 5;
uint8_t blue = (color_val & 0xF800) >> 11;
uint8_t pass_buff[3];
pass_buff[2] = (uint8_t) ((red / 32.0) * 64) << 2;
pass_buff[1] = (uint8_t) green << 2;
pass_buff[0] = (uint8_t) ((blue / 32.0) * 64) << 2;
this->write_array(pass_buff, sizeof(pass_buff));
}
} else {
this->write_array16(transfer_buffer_, sz);
"height:%d, mode=%d, 18bit=%d, sw_time=%dus, mw_time=%dus)",
this->x_low_, this->y_low_, this->x_high_, this->y_high_, w, h, this->buffer_color_mode_,
this->is_18bitdisplay_, sw_time, mw_time);
auto now = millis();
this->enable();
if (this->buffer_color_mode_ == BITS_16 && !this->is_18bitdisplay_ && sw_time < mw_time) {
// 16 bit mode maps directly to display format
ESP_LOGV(TAG, "Doing single write of %d bytes", this->width_ * h * 2);
set_addr_window_(0, this->y_low_, this->width_ - 1, this->y_high_);
this->write_array(this->buffer_ + this->y_low_ * this->width_ * 2, h * this->width_ * 2);
} else {
ESP_LOGV(TAG, "Doing multiple write");
size_t rem = h * w; // remaining number of pixels to write
set_addr_window_(this->x_low_, this->y_low_, this->x_high_, this->y_high_);
size_t idx = 0; // index into transfer_buffer
size_t pixel = 0; // pixel number offset
size_t pos = this->y_low_ * this->width_ + this->x_low_;
while (rem-- != 0) {
uint16_t color_val;
switch (this->buffer_color_mode_) {
case BITS_8:
color_val = display::ColorUtil::color_to_565(display::ColorUtil::rgb332_to_color(this->buffer_[pos++]));
break;
case BITS_8_INDEXED:
color_val = display::ColorUtil::color_to_565(
display::ColorUtil::index8_to_color_palette888(this->buffer_[pos++], this->palette_));
break;
default: // case BITS_16:
color_val = (buffer_[pos * 2] << 8) + buffer_[pos * 2 + 1];
pos++;
break;
}
if (this->is_18bitdisplay_) {
transfer_buffer[idx++] = (uint8_t) ((color_val & 0xF800) >> 8); // Blue
transfer_buffer[idx++] = (uint8_t) ((color_val & 0x7E0) >> 3); // Green
transfer_buffer[idx++] = (uint8_t) (color_val << 3); // Red
} else {
put16_be(transfer_buffer + idx, color_val);
idx += 2;
}
if (idx == ILI9XXX_TRANSFER_BUFFER_SIZE) {
this->write_array(transfer_buffer, idx);
idx = 0;
App.feed_wdt();
}
// end of line? Skip to the next.
if (++pixel == w) {
pixel = 0;
pos += this->width_ - w;
}
pos += sz;
rem -= sz;
}
App.feed_wdt();
// flush any balance.
if (idx != 0) {
this->write_array(transfer_buffer, idx);
}
}
this->end_data_();
this->disable();
ESP_LOGV(TAG, "Data write took %dms", (unsigned) (millis() - now));
// invalidate watermarks
this->x_low_ = this->width_;
this->y_low_ = this->height_;
@ -225,26 +276,6 @@ void ILI9XXXDisplay::display_() {
this->y_high_ = 0;
}
uint32_t ILI9XXXDisplay::buffer_to_transfer_(uint32_t pos, uint32_t sz) {
for (uint32_t i = 0; i < sz; ++i) {
switch (this->buffer_color_mode_) {
case BITS_8_INDEXED:
transfer_buffer_[i] = display::ColorUtil::color_to_565(
display::ColorUtil::index8_to_color_palette888(this->buffer_[pos + i], this->palette_));
break;
case BITS_16:
transfer_buffer_[i] = ((uint16_t) this->buffer_[(pos + i) * 2] << 8) | this->buffer_[((pos + i) * 2) + 1];
continue;
break;
default:
transfer_buffer_[i] =
display::ColorUtil::color_to_565(display::ColorUtil::rgb332_to_color(this->buffer_[pos + i]));
break;
}
}
return sz;
}
// should return the total size: return this->get_width_internal() * this->get_height_internal() * 2 // 16bit color
// values per bit is huge
uint32_t ILI9XXXDisplay::get_buffer_length_() { return this->get_width_internal() * this->get_height_internal(); }
@ -303,11 +334,11 @@ void ILI9XXXDisplay::reset_() {
}
}
void ILI9XXXDisplay::init_lcd_(const uint8_t *init_cmd) {
void ILI9XXXDisplay::init_lcd_() {
uint8_t cmd, x, num_args;
const uint8_t *addr = init_cmd;
while ((cmd = progmem_read_byte(addr++)) > 0) {
x = progmem_read_byte(addr++);
const uint8_t *addr = this->init_sequence_;
while ((cmd = *addr++) > 0) {
x = *addr++;
num_args = x & 0x7F;
send_command(cmd, addr, num_args);
addr += num_args;
@ -316,27 +347,29 @@ void ILI9XXXDisplay::init_lcd_(const uint8_t *init_cmd) {
}
}
void ILI9XXXDisplay::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t w, uint16_t h) {
uint16_t x2 = (x1 + w - 1), y2 = (y1 + h - 1);
this->command(ILI9XXX_CASET); // Column address set
this->start_data_();
this->write_byte(x1 >> 8);
this->write_byte(x1);
this->write_byte(x2 >> 8);
this->write_byte(x2);
this->end_data_();
this->command(ILI9XXX_PASET); // Row address set
this->start_data_();
this->write_byte(y1 >> 8);
this->write_byte(y1);
this->write_byte(y2 >> 8);
this->write_byte(y2);
this->end_data_();
this->command(ILI9XXX_RAMWR); // Write to RAM
// Tell the display controller where we want to draw pixels.
// when called, the SPI should have already been enabled, only the D/C pin will be toggled here.
void ILI9XXXDisplay::set_addr_window_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
uint8_t buf[4];
this->dc_pin_->digital_write(false);
this->write_byte(ILI9XXX_CASET); // Column address set
put16_be(buf, x1 + this->offset_x_);
put16_be(buf + 2, x2 + this->offset_x_);
this->dc_pin_->digital_write(true);
this->write_array(buf, sizeof buf);
this->dc_pin_->digital_write(false);
this->write_byte(ILI9XXX_PASET); // Row address set
put16_be(buf, y1 + this->offset_y_);
put16_be(buf + 2, y2 + this->offset_y_);
this->dc_pin_->digital_write(true);
this->write_array(buf, sizeof buf);
this->dc_pin_->digital_write(false);
this->write_byte(ILI9XXX_RAMWR); // Write to RAM
this->dc_pin_->digital_write(true);
}
void ILI9XXXDisplay::invert_display(bool invert) {
this->pre_invertdisplay_ = invert;
void ILI9XXXDisplay::invert_colors(bool invert) {
this->pre_invertcolors_ = invert;
if (is_ready()) {
this->command(invert ? ILI9XXX_INVON : ILI9XXX_INVOFF);
}
@ -345,132 +378,5 @@ void ILI9XXXDisplay::invert_display(bool invert) {
int ILI9XXXDisplay::get_width_internal() { return this->width_; }
int ILI9XXXDisplay::get_height_internal() { return this->height_; }
// M5Stack display
void ILI9XXXM5Stack::initialize() {
this->init_lcd_(INITCMD_M5STACK);
if (this->width_ == 0)
this->width_ = 320;
if (this->height_ == 0)
this->height_ = 240;
this->pre_invertdisplay_ = true;
}
// M5CORE display // Based on the configuration settings of M5stact's M5GFX code.
void ILI9XXXM5CORE::initialize() {
this->init_lcd_(INITCMD_M5CORE);
if (this->width_ == 0)
this->width_ = 320;
if (this->height_ == 0)
this->height_ = 240;
this->pre_invertdisplay_ = true;
}
// 24_TFT display
void ILI9XXXILI9341::initialize() {
this->init_lcd_(INITCMD_ILI9341);
if (this->width_ == 0)
this->width_ = 240;
if (this->height_ == 0)
this->height_ = 320;
}
// 24_TFT rotated display
void ILI9XXXILI9342::initialize() {
this->init_lcd_(INITCMD_ILI9341);
if (this->width_ == 0) {
this->width_ = 320;
}
if (this->height_ == 0) {
this->height_ = 240;
}
}
// 35_TFT display
void ILI9XXXILI9481::initialize() {
this->init_lcd_(INITCMD_ILI9481);
if (this->width_ == 0) {
this->width_ = 480;
}
if (this->height_ == 0) {
this->height_ = 320;
}
}
void ILI9XXXILI948118::initialize() {
this->init_lcd_(INITCMD_ILI9481_18);
if (this->width_ == 0) {
this->width_ = 320;
}
if (this->height_ == 0) {
this->height_ = 480;
}
this->is_18bitdisplay_ = true;
}
// 35_TFT display
void ILI9XXXILI9486::initialize() {
this->init_lcd_(INITCMD_ILI9486);
if (this->width_ == 0) {
this->width_ = 480;
}
if (this->height_ == 0) {
this->height_ = 320;
}
}
// 40_TFT display
void ILI9XXXILI9488::initialize() {
this->init_lcd_(INITCMD_ILI9488);
if (this->width_ == 0) {
this->width_ = 480;
}
if (this->height_ == 0) {
this->height_ = 320;
}
this->is_18bitdisplay_ = true;
}
// 40_TFT display
void ILI9XXXILI9488A::initialize() {
this->init_lcd_(INITCMD_ILI9488_A);
if (this->width_ == 0) {
this->width_ = 480;
}
if (this->height_ == 0) {
this->height_ = 320;
}
this->is_18bitdisplay_ = true;
}
// 40_TFT display
void ILI9XXXST7796::initialize() {
this->init_lcd_(INITCMD_ST7796);
if (this->width_ == 0) {
this->width_ = 320;
}
if (this->height_ == 0) {
this->height_ = 480;
}
}
// 24_TFT rotated display
void ILI9XXXS3Box::initialize() {
this->init_lcd_(INITCMD_S3BOX);
if (this->width_ == 0) {
this->width_ = 320;
}
if (this->height_ == 0) {
this->height_ = 240;
}
}
// 24_TFT rotated display
void ILI9XXXS3BoxLite::initialize() {
this->init_lcd_(INITCMD_S3BOXLITE);
if (this->width_ == 0) {
this->width_ = 320;
}
if (this->height_ == 0) {
this->height_ = 240;
}
this->pre_invertdisplay_ = true;
}
} // namespace ili9xxx
} // namespace esphome

View File

@ -7,7 +7,7 @@
namespace esphome {
namespace ili9xxx {
const uint32_t ILI9XXX_TRANSFER_BUFFER_SIZE = 64;
const size_t ILI9XXX_TRANSFER_BUFFER_SIZE = 126; // ensure this is divisible by 6
enum ILI9XXXColorMode {
BITS_8 = 0x08,
@ -19,25 +19,62 @@ enum ILI9XXXColorMode {
#define ILI9XXXDisplay_DATA_RATE spi::DATA_RATE_40MHZ
#endif // ILI9XXXDisplay_DATA_RATE
class ILI9XXXDisplay : public PollingComponent,
public display::DisplayBuffer,
class ILI9XXXDisplay : public display::DisplayBuffer,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
spi::CLOCK_PHASE_LEADING, ILI9XXXDisplay_DATA_RATE> {
public:
ILI9XXXDisplay() = default;
ILI9XXXDisplay(uint8_t const *init_sequence, int16_t width, int16_t height, bool invert_colors)
: init_sequence_{init_sequence}, width_{width}, height_{height}, pre_invertcolors_{invert_colors} {
uint8_t cmd, num_args, bits;
const uint8_t *addr = init_sequence;
while ((cmd = *addr++) != 0) {
num_args = *addr++ & 0x7F;
bits = *addr;
switch (cmd) {
case ILI9XXX_MADCTL: {
this->swap_xy_ = (bits & MADCTL_MV) != 0;
this->mirror_x_ = (bits & MADCTL_MX) != 0;
this->mirror_y_ = (bits & MADCTL_MY) != 0;
this->color_order_ = (bits & MADCTL_BGR) ? display::COLOR_ORDER_BGR : display::COLOR_ORDER_RGB;
break;
}
case ILI9XXX_PIXFMT: {
if ((bits & 0xF) == 6)
this->is_18bitdisplay_ = true;
break;
}
default:
break;
}
addr += num_args;
}
}
void set_dc_pin(GPIOPin *dc_pin) { dc_pin_ = dc_pin; }
float get_setup_priority() const override;
void set_reset_pin(GPIOPin *reset) { this->reset_pin_ = reset; }
void set_palette(const uint8_t *palette) { this->palette_ = palette; }
void set_buffer_color_mode(ILI9XXXColorMode color_mode) { this->buffer_color_mode_ = color_mode; }
void set_dimentions(int16_t width, int16_t height) {
void set_dimensions(int16_t width, int16_t height) {
this->height_ = height;
this->width_ = width;
}
void invert_display(bool invert);
void set_offsets(int16_t offset_x, int16_t offset_y) {
this->offset_x_ = offset_x;
this->offset_y_ = offset_y;
}
void invert_colors(bool invert);
void command(uint8_t value);
void data(uint8_t value);
void send_command(uint8_t command_byte, const uint8_t *data_bytes, uint8_t num_data_bytes);
uint8_t read_command(uint8_t command_byte, uint8_t index);
void set_color_order(display::ColorOrder color_order) { this->color_order_ = color_order; }
void set_swap_xy(bool swap_xy) { this->swap_xy_ = swap_xy; }
void set_mirror_x(bool mirror_x) { this->mirror_x_ = mirror_x; }
void set_mirror_y(bool mirror_y) { this->mirror_y_ = mirror_y; }
void update() override;
@ -51,16 +88,17 @@ class ILI9XXXDisplay : public PollingComponent,
protected:
void draw_absolute_pixel_internal(int x, int y, Color color) override;
void setup_pins_();
virtual void initialize() = 0;
void display_();
void init_lcd_(const uint8_t *init_cmd);
void set_addr_window_(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
void init_lcd_();
void set_addr_window_(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2);
void reset_();
uint8_t const *init_sequence_{};
int16_t width_{0}; ///< Display width as modified by current rotation
int16_t height_{0}; ///< Display height as modified by current rotation
int16_t offset_x_{0};
int16_t offset_y_{0};
uint16_t x_low_{0};
uint16_t y_low_{0};
uint16_t x_high_{0};
@ -78,10 +116,6 @@ class ILI9XXXDisplay : public PollingComponent,
void start_data_();
void end_data_();
uint16_t transfer_buffer_[ILI9XXX_TRANSFER_BUFFER_SIZE];
uint32_t buffer_to_transfer_(uint32_t pos, uint32_t sz);
GPIOPin *reset_pin_{nullptr};
GPIOPin *dc_pin_{nullptr};
GPIOPin *busy_pin_{nullptr};
@ -89,77 +123,87 @@ class ILI9XXXDisplay : public PollingComponent,
bool prossing_update_ = false;
bool need_update_ = false;
bool is_18bitdisplay_ = false;
bool pre_invertdisplay_ = false;
bool pre_invertcolors_ = false;
display::ColorOrder color_order_{};
bool swap_xy_{};
bool mirror_x_{};
bool mirror_y_{};
};
//----------- M5Stack display --------------
class ILI9XXXM5Stack : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXM5Stack() : ILI9XXXDisplay(INITCMD_M5STACK, 320, 240, true) {}
};
//----------- M5Stack display --------------
class ILI9XXXM5CORE : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXM5CORE() : ILI9XXXDisplay(INITCMD_M5CORE, 320, 240, true) {}
};
//----------- ST7789V display --------------
class ILI9XXXST7789V : public ILI9XXXDisplay {
public:
ILI9XXXST7789V() : ILI9XXXDisplay(INITCMD_ST7789V, 240, 320, false) {}
};
//----------- ILI9XXX_24_TFT display --------------
class ILI9XXXILI9341 : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXILI9341() : ILI9XXXDisplay(INITCMD_ILI9341, 240, 320, false) {}
};
//----------- ILI9XXX_24_TFT rotated display --------------
class ILI9XXXILI9342 : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXILI9342() : ILI9XXXDisplay(INITCMD_ILI9341, 320, 240, false) {}
};
//----------- ILI9XXX_??_TFT rotated display --------------
class ILI9XXXILI9481 : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXILI9481() : ILI9XXXDisplay(INITCMD_ILI9481, 480, 320, false) {}
};
//----------- ILI9481 in 18 bit mode --------------
class ILI9XXXILI948118 : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXILI948118() : ILI9XXXDisplay(INITCMD_ILI9481_18, 320, 480, true) {}
};
//----------- ILI9XXX_35_TFT rotated display --------------
class ILI9XXXILI9486 : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXILI9486() : ILI9XXXDisplay(INITCMD_ILI9486, 480, 320, false) {}
};
//----------- ILI9XXX_35_TFT rotated display --------------
class ILI9XXXILI9488 : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXILI9488() : ILI9XXXDisplay(INITCMD_ILI9488, 480, 320, true) {}
};
//----------- ILI9XXX_35_TFT origin colors rotated display --------------
class ILI9XXXILI9488A : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXILI9488A() : ILI9XXXDisplay(INITCMD_ILI9488_A, 480, 320, true) {}
};
//----------- ILI9XXX_35_TFT rotated display --------------
class ILI9XXXST7796 : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXST7796() : ILI9XXXDisplay(INITCMD_ST7796, 320, 480, false) {}
};
class ILI9XXXS3Box : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXS3Box() : ILI9XXXDisplay(INITCMD_S3BOX, 320, 240, false) {}
};
class ILI9XXXS3BoxLite : public ILI9XXXDisplay {
protected:
void initialize() override;
public:
ILI9XXXS3BoxLite() : ILI9XXXDisplay(INITCMD_S3BOXLITE, 320, 240, true) {}
};
} // namespace ili9xxx

View File

@ -289,6 +289,33 @@ static const uint8_t PROGMEM INITCMD_S3BOXLITE[] = {
0x00 // End of list
};
static const uint8_t PROGMEM INITCMD_ST7789V[] = {
ILI9XXX_SLPOUT , 0x80, // Exit Sleep
ILI9XXX_DISPON , 0x80, // Display on
ILI9XXX_MADCTL , 1, 0x08, // Memory Access Control, BGR
ILI9XXX_DFUNCTR, 2, 0x0A, 0x82,
ILI9XXX_PIXFMT , 1, 0x55,
ILI9XXX_FRMCTR2, 5, 0x0C, 0x0C, 0x00, 0x33, 0x33,
ILI9XXX_ETMOD, 1, 0x35, 0xBB, 1, 0x28,
ILI9XXX_PWCTR1 , 1, 0x0C, // Power control VRH[5:0]
ILI9XXX_PWCTR3 , 2, 0x01, 0xFF,
ILI9XXX_PWCTR4 , 1, 0x10,
ILI9XXX_PWCTR5 , 1, 0x20,
ILI9XXX_IFCTR , 1, 0x0F,
ILI9XXX_PWSET, 2, 0xA4, 0xA1,
ILI9XXX_GMCTRP1 , 14,
0xd0, 0x00, 0x02, 0x07, 0x0a,
0x28, 0x32, 0x44, 0x42, 0x06, 0x0e,
0x12, 0x14, 0x17,
ILI9XXX_GMCTRN1 , 14,
0xd0, 0x00, 0x02, 0x07, 0x0a,
0x28, 0x31, 0x54, 0x47,
0x0e, 0x1c, 0x17, 0x1b,
0x1e,
ILI9XXX_DISPON , 0x80, // Display on
0x00 // End of list
};
// clang-format on
} // namespace ili9xxx
} // namespace esphome

View File

@ -39,7 +39,11 @@ CONF_VCOM_PIN = "vcom_pin"
inkplate6_ns = cg.esphome_ns.namespace("inkplate6")
Inkplate6 = inkplate6_ns.class_(
"Inkplate6", cg.PollingComponent, i2c.I2CDevice, display.DisplayBuffer
"Inkplate6",
cg.PollingComponent,
i2c.I2CDevice,
display.Display,
display.DisplayBuffer,
)
InkplateModel = inkplate6_ns.enum("InkplateModel")
@ -110,7 +114,6 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await display.register_display(var, config)
await i2c.register_i2c_device(var, config)

View File

@ -17,7 +17,7 @@ enum InkplateModel : uint8_t {
INKPLATE_6_V2 = 3,
};
class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public i2c::I2CDevice {
class Inkplate6 : public display::DisplayBuffer, public i2c::I2CDevice {
public:
const uint8_t LUT2[16] = {0xAA, 0xA9, 0xA6, 0xA5, 0x9A, 0x99, 0x96, 0x95,
0x6A, 0x69, 0x66, 0x65, 0x5A, 0x59, 0x56, 0x55};

View File

@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.const import CONF_ID, CONF_INTERVAL
from esphome.const import CONF_ID, CONF_INTERVAL, CONF_STARTUP_DELAY
CODEOWNERS = ["@esphome/core"]
interval_ns = cg.esphome_ns.namespace("interval")
@ -13,6 +13,9 @@ CONFIG_SCHEMA = automation.validate_automation(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(IntervalTrigger),
cv.Optional(
CONF_STARTUP_DELAY, default="0s"
): cv.positive_time_period_milliseconds,
cv.Required(CONF_INTERVAL): cv.positive_time_period_milliseconds,
}
).extend(cv.COMPONENT_SCHEMA)
@ -26,3 +29,4 @@ async def to_code(config):
await automation.build_automation(var, [], conf)
cg.add(var.set_update_interval(conf[CONF_INTERVAL]))
cg.add(var.set_startup_delay(conf[CONF_STARTUP_DELAY]))

View File

@ -8,8 +8,26 @@ namespace interval {
class IntervalTrigger : public Trigger<>, public PollingComponent {
public:
void update() override { this->trigger(); }
void update() override {
if (this->started_)
this->trigger();
}
void setup() override {
if (this->startup_delay_ == 0) {
this->started_ = true;
} else {
this->set_timeout(this->startup_delay_, [this] { this->started_ = true; });
}
}
void set_startup_delay(const uint32_t startup_delay) { this->startup_delay_ = startup_delay; }
float get_setup_priority() const override { return setup_priority::DATA; }
protected:
uint32_t startup_delay_{0};
bool started_{false};
};
} // namespace interval

View File

@ -52,7 +52,6 @@ LCD_SCHEMA = display.BASIC_DISPLAY_SCHEMA.extend(
async def setup_lcd_display(var, config):
await cg.register_component(var, config)
await display.register_display(var, config)
cg.add(var.set_dimensions(config[CONF_DIMENSIONS][0], config[CONF_DIMENSIONS][1]))
if CONF_USER_CHARACTERS in config:

View File

@ -255,12 +255,11 @@ class LD2420Component : public Component, public uart::UARTDevice {
uint16_t gate_energy_[LD2420_TOTAL_GATES];
CmdReplyT cmd_reply_;
uint32_t timeout_;
uint32_t max_distance_gate_;
uint32_t min_distance_gate_;
uint16_t system_mode_{CMD_SYSTEM_MODE_ENERGY};
bool cmd_active_{false};
char ld2420_firmware_ver_[8];
char ld2420_firmware_ver_[8]{"v0.0.0"};
bool presence_{false};
bool calibration_{false};
uint16_t distance_{0};

View File

@ -186,25 +186,11 @@ def validate_gpio_usage(value):
return value
BASE_PIN_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(ArduinoInternalGPIOPin),
cv.Required(CONF_NUMBER): validate_gpio_pin,
cv.Optional(CONF_MODE, default={}): cv.Schema(
{
cv.Optional(CONF_ANALOG, default=False): cv.boolean,
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
cv.Optional(CONF_OPEN_DRAIN, default=False): cv.boolean,
cv.Optional(CONF_PULLUP, default=False): cv.boolean,
cv.Optional(CONF_PULLDOWN, default=False): cv.boolean,
}
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
},
)
BASE_PIN_SCHEMA.add_extra(validate_gpio_usage)
BASE_PIN_SCHEMA = pins.gpio_base_schema(
ArduinoInternalGPIOPin,
validate_gpio_pin,
modes=pins.GPIO_STANDARD_MODES + (CONF_ANALOG,),
).add_extra(validate_gpio_usage)
async def component_pin_to_code(config):

View File

@ -57,7 +57,7 @@ class AddressableLambdaLightEffect : public AddressableLightEffect {
void start() override { this->initial_run_ = true; }
void apply(AddressableLight &it, const Color &current_color) override {
const uint32_t now = millis();
if (now - this->last_run_ >= this->update_interval_) {
if (now - this->last_run_ >= this->update_interval_ || this->initial_run_) {
this->last_run_ = now;
this->f_(it, current_color, this->initial_run_);
this->initial_run_ = false;

View File

@ -118,7 +118,7 @@ class LambdaLightEffect : public LightEffect {
void start() override { this->initial_run_ = true; }
void apply() override {
const uint32_t now = millis();
if (now - this->last_run_ >= this->update_interval_) {
if (now - this->last_run_ >= this->update_interval_ || this->initial_run_) {
this->last_run_ = now;
this->f_(this->initial_run_);
this->initial_run_ = false;

View File

@ -13,7 +13,6 @@ DEPENDENCIES = ["i2c"]
LilygoT547Touchscreen = lilygo_t5_47_ns.class_(
"LilygoT547Touchscreen",
touchscreen.Touchscreen,
cg.Component,
i2c.I2CDevice,
)
@ -27,17 +26,14 @@ CONFIG_SCHEMA = touchscreen.TOUCHSCREEN_SCHEMA.extend(
pins.internal_gpio_input_pin_schema
),
}
)
.extend(i2c.i2c_device_schema(0x5A))
.extend(cv.COMPONENT_SCHEMA)
).extend(i2c.i2c_device_schema(0x5A))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
await touchscreen.register_touchscreen(var, config)
await i2c.register_i2c_device(var, config)
interrupt_pin = await cg.gpio_pin_expression(config[CONF_INTERRUPT_PIN])
cg.add(var.set_interrupt_pin(interrupt_pin))

View File

@ -23,15 +23,12 @@ static const uint8_t READ_TOUCH[1] = {0x07};
return; \
}
void Store::gpio_intr(Store *store) { store->touch = true; }
void LilygoT547Touchscreen::setup() {
ESP_LOGCONFIG(TAG, "Setting up Lilygo T5 4.7 Touchscreen...");
this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
this->interrupt_pin_->setup();
this->store_.pin = this->interrupt_pin_->to_isr();
this->interrupt_pin_->attach_interrupt(Store::gpio_intr, &this->store_, gpio::INTERRUPT_FALLING_EDGE);
this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
if (this->write(nullptr, 0) != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to communicate!");
@ -41,19 +38,14 @@ void LilygoT547Touchscreen::setup() {
}
this->write_register(POWER_REGISTER, WAKEUP_CMD, 1);
this->x_raw_max_ = this->get_width_();
this->y_raw_max_ = this->get_height_();
}
void LilygoT547Touchscreen::loop() {
if (!this->store_.touch) {
for (auto *listener : this->touch_listeners_)
listener->release();
return;
}
this->store_.touch = false;
void LilygoT547Touchscreen::update_touches() {
uint8_t point = 0;
uint8_t buffer[40] = {0};
uint32_t sum_l = 0, sum_h = 0;
i2c::ErrorCode err;
err = this->write_register(TOUCH_REGISTER, READ_FLAGS, 1);
@ -69,102 +61,30 @@ void LilygoT547Touchscreen::loop() {
point = buffer[5] & 0xF;
if (point == 0) {
for (auto *listener : this->touch_listeners_)
listener->release();
return;
} else if (point == 1) {
if (point == 1) {
err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
ERROR_CHECK(err);
err = this->read(&buffer[5], 2);
ERROR_CHECK(err);
sum_l = buffer[5] << 8 | buffer[6];
} else if (point > 1) {
err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
ERROR_CHECK(err);
err = this->read(&buffer[5], 5 * (point - 1) + 3);
ERROR_CHECK(err);
sum_l = buffer[5 * point + 1] << 8 | buffer[5 * point + 2];
}
this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
for (int i = 0; i < 5 * point; i++)
sum_h += buffer[i];
if (point == 0)
point = 1;
if (sum_l != sum_h)
point = 0;
if (point) {
uint8_t offset;
for (int i = 0; i < point; i++) {
if (i == 0) {
offset = 0;
} else {
offset = 4;
}
TouchPoint tp;
tp.id = (buffer[i * 5 + offset] >> 4) & 0x0F;
tp.state = buffer[i * 5 + offset] & 0x0F;
if (tp.state == 0x06)
tp.state = 0x07;
uint16_t y = (uint16_t) ((buffer[i * 5 + 1 + offset] << 4) | ((buffer[i * 5 + 3 + offset] >> 4) & 0x0F));
uint16_t x = (uint16_t) ((buffer[i * 5 + 2 + offset] << 4) | (buffer[i * 5 + 3 + offset] & 0x0F));
switch (this->rotation_) {
case ROTATE_0_DEGREES:
tp.y = this->display_height_ - y;
tp.x = x;
break;
case ROTATE_90_DEGREES:
tp.x = this->display_height_ - y;
tp.y = this->display_width_ - x;
break;
case ROTATE_180_DEGREES:
tp.y = y;
tp.x = this->display_width_ - x;
break;
case ROTATE_270_DEGREES:
tp.x = y;
tp.y = x;
break;
}
this->defer([this, tp]() { this->send_touch_(tp); });
}
} else {
TouchPoint tp;
tp.id = (buffer[0] >> 4) & 0x0F;
tp.state = 0x06;
uint16_t y = (uint16_t) ((buffer[0 * 5 + 1] << 4) | ((buffer[0 * 5 + 3] >> 4) & 0x0F));
uint16_t x = (uint16_t) ((buffer[0 * 5 + 2] << 4) | (buffer[0 * 5 + 3] & 0x0F));
switch (this->rotation_) {
case ROTATE_0_DEGREES:
tp.y = this->display_height_ - y;
tp.x = x;
break;
case ROTATE_90_DEGREES:
tp.x = this->display_height_ - y;
tp.y = this->display_width_ - x;
break;
case ROTATE_180_DEGREES:
tp.y = y;
tp.x = this->display_width_ - x;
break;
case ROTATE_270_DEGREES:
tp.x = y;
tp.y = x;
break;
}
this->defer([this, tp]() { this->send_touch_(tp); });
uint16_t id, x_raw, y_raw;
for (uint8_t i = 0; i < point; i++) {
id = (buffer[i * 5] >> 4) & 0x0F;
y_raw = (uint16_t) ((buffer[i * 5 + 1] << 4) | ((buffer[i * 5 + 3] >> 4) & 0x0F));
x_raw = (uint16_t) ((buffer[i * 5 + 2] << 4) | (buffer[i * 5 + 3] & 0x0F));
this->set_raw_touch_position_(id, x_raw, y_raw);
}
this->status_clear_warning();

View File

@ -6,29 +6,25 @@
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include <vector>
namespace esphome {
namespace lilygo_t5_47 {
struct Store {
volatile bool touch;
ISRInternalGPIOPin pin;
static void gpio_intr(Store *store);
};
using namespace touchscreen;
class LilygoT547Touchscreen : public Touchscreen, public Component, public i2c::I2CDevice {
class LilygoT547Touchscreen : public Touchscreen, public i2c::I2CDevice {
public:
void setup() override;
void loop() override;
void dump_config() override;
void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
protected:
void update_touches() override;
InternalGPIOPin *interrupt_pin_;
Store store_;
};
} // namespace lilygo_t5_47

View File

@ -74,20 +74,14 @@ def validate_mode(value):
CONF_MAX6956 = "max6956"
MAX6956_PIN_SCHEMA = cv.All(
MAX6956_PIN_SCHEMA = pins.gpio_base_schema(
MAX6956GPIOPin,
cv.int_range(min=4, max=31),
modes=[CONF_INPUT, CONF_PULLUP, CONF_OUTPUT],
mode_validator=validate_mode,
).extend(
{
cv.GenerateID(): cv.declare_id(MAX6956GPIOPin),
cv.Required(CONF_MAX6956): cv.use_id(MAX6956),
cv.Required(CONF_NUMBER): cv.int_range(min=4, max=31),
cv.Optional(CONF_MODE, default={}): cv.All(
{
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_PULLUP, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
},
validate_mode,
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
}
)

View File

@ -29,7 +29,6 @@ CONFIG_SCHEMA = (
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await spi.register_spi_device(var, config)
await display.register_display(var, config)

View File

@ -39,7 +39,7 @@ CHIP_MODES = {
max7219_ns = cg.esphome_ns.namespace("max7219digit")
MAX7219Component = max7219_ns.class_(
"MAX7219Component", cg.PollingComponent, spi.SPIDevice, display.DisplayBuffer
"MAX7219Component", spi.SPIDevice, display.DisplayBuffer, cg.PollingComponent
)
MAX7219ComponentRef = MAX7219Component.operator("ref")
@ -78,7 +78,6 @@ CONFIG_SCHEMA = (
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await spi.register_spi_device(var, config)
await display.register_display(var, config)

View File

@ -25,8 +25,7 @@ class MAX7219Component;
using max7219_writer_t = std::function<void(MAX7219Component &)>;
class MAX7219Component : public PollingComponent,
public display::DisplayBuffer,
class MAX7219Component : public display::DisplayBuffer,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_1MHZ> {
public:

View File

@ -45,19 +45,15 @@ def validate_mode(value):
CONF_MCP23016 = "mcp23016"
MCP23016_PIN_SCHEMA = cv.All(
MCP23016_PIN_SCHEMA = pins.gpio_base_schema(
MCP23016GPIOPin,
cv.int_range(min=0, max=15),
modes=[CONF_INPUT, CONF_OUTPUT],
mode_validator=validate_mode,
invertable=True,
).extend(
{
cv.GenerateID(): cv.declare_id(MCP23016GPIOPin),
cv.Required(CONF_MCP23016): cv.use_id(MCP23016),
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=15),
cv.Optional(CONF_MODE, default={}): cv.All(
{
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
},
validate_mode,
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
}
)

View File

@ -54,20 +54,16 @@ def validate_mode(value):
CONF_MCP23XXX = "mcp23xxx"
MCP23XXX_PIN_SCHEMA = cv.All(
MCP23XXX_PIN_SCHEMA = pins.gpio_base_schema(
MCP23XXXGPIOPin,
cv.int_range(min=0, max=15),
modes=[CONF_INPUT, CONF_OUTPUT, CONF_PULLUP],
mode_validator=validate_mode,
invertable=True,
).extend(
{
cv.GenerateID(): cv.declare_id(MCP23XXXGPIOPin),
cv.Required(CONF_MCP23XXX): cv.use_id(MCP23XXXBase),
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=15),
cv.Optional(CONF_MODE, default={}): cv.All(
{
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_PULLUP, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
},
validate_mode,
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
cv.Optional(CONF_INTERRUPT, default="NO_INTERRUPT"): cv.enum(
MCP23XXX_INTERRUPT_MODES, upper=True
),

View File

@ -1,4 +1,6 @@
#include "mcp3008.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
@ -32,28 +34,10 @@ float MCP3008::read_data(uint8_t pin) {
this->disable();
int data = data_msb << 8 | data_lsb;
uint16_t data = encode_uint16(data_msb, data_lsb);
return data / 1023.0f;
}
MCP3008Sensor::MCP3008Sensor(MCP3008 *parent, uint8_t pin, float reference_voltage)
: PollingComponent(1000), parent_(parent), pin_(pin), reference_voltage_(reference_voltage) {}
float MCP3008Sensor::get_setup_priority() const { return setup_priority::DATA; }
void MCP3008Sensor::setup() { LOG_SENSOR("", "Setting up MCP3008 Sensor '%s'...", this); }
void MCP3008Sensor::dump_config() {
ESP_LOGCONFIG(TAG, "MCP3008Sensor:");
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
}
float MCP3008Sensor::sample() {
float value_v = this->parent_->read_data(pin_);
value_v = (value_v * this->reference_voltage_);
return value_v;
}
void MCP3008Sensor::update() { this->publish_state(this->sample()); }
} // namespace mcp3008
} // namespace esphome

View File

@ -1,10 +1,8 @@
#pragma once
#include "esphome/components/spi/spi.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/spi/spi.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
namespace esphome {
namespace mcp3008 {
@ -14,31 +12,10 @@ class MCP3008 : public Component,
spi::DATA_RATE_75KHZ> { // Running at the slowest max speed supported by the
// mcp3008. 2.7v = 75ksps
public:
MCP3008() = default;
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
float read_data(uint8_t pin);
protected:
};
class MCP3008Sensor : public PollingComponent, public sensor::Sensor, public voltage_sampler::VoltageSampler {
public:
MCP3008Sensor(MCP3008 *parent, uint8_t pin, float reference_voltage);
void set_reference_voltage(float reference_voltage) { reference_voltage_ = reference_voltage; }
void setup() override;
void update() override;
void dump_config() override;
float get_setup_priority() const override;
float sample() override;
protected:
MCP3008 *parent_;
uint8_t pin_;
float reference_voltage_;
};
} // namespace mcp3008

View File

@ -1,39 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import CONF_ID, CONF_NUMBER
from . import mcp3008_ns, MCP3008
AUTO_LOAD = ["voltage_sampler"]
DEPENDENCIES = ["mcp3008"]
MCP3008Sensor = mcp3008_ns.class_(
"MCP3008Sensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
)
CONF_REFERENCE_VOLTAGE = "reference_voltage"
CONF_MCP3008_ID = "mcp3008_id"
CONFIG_SCHEMA = (
sensor.sensor_schema(MCP3008Sensor)
.extend(
{
cv.GenerateID(CONF_MCP3008_ID): cv.use_id(MCP3008),
cv.Required(CONF_NUMBER): cv.int_,
cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage,
}
)
.extend(cv.polling_component_schema("1s"))
)
async def to_code(config):
parent = await cg.get_variable(config[CONF_MCP3008_ID])
var = cg.new_Pvariable(
config[CONF_ID],
parent,
config[CONF_NUMBER],
config[CONF_REFERENCE_VOLTAGE],
)
await cg.register_component(var, config)
await sensor.register_sensor(var, config)

View File

@ -0,0 +1,53 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import (
CONF_ID,
CONF_NUMBER,
UNIT_VOLT,
STATE_CLASS_MEASUREMENT,
DEVICE_CLASS_VOLTAGE,
)
from .. import mcp3008_ns, MCP3008
AUTO_LOAD = ["voltage_sampler"]
DEPENDENCIES = ["mcp3008"]
MCP3008Sensor = mcp3008_ns.class_(
"MCP3008Sensor",
sensor.Sensor,
cg.PollingComponent,
voltage_sampler.VoltageSampler,
cg.Parented.template(MCP3008),
)
CONF_REFERENCE_VOLTAGE = "reference_voltage"
CONF_MCP3008_ID = "mcp3008_id"
CONFIG_SCHEMA = (
sensor.sensor_schema(
MCP3008Sensor,
unit_of_measurement=UNIT_VOLT,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_VOLTAGE,
)
.extend(
{
cv.GenerateID(CONF_MCP3008_ID): cv.use_id(MCP3008),
cv.Required(CONF_NUMBER): cv.int_,
cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage,
}
)
.extend(cv.polling_component_schema("60s"))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_parented(var, config[CONF_MCP3008_ID])
await cg.register_component(var, config)
await sensor.register_sensor(var, config)
cg.add(var.set_pin(config[CONF_NUMBER]))
cg.add(var.set_reference_voltage(config[CONF_REFERENCE_VOLTAGE]))

View File

@ -0,0 +1,27 @@
#include "mcp3008_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mcp3008 {
static const char *const TAG = "mcp3008.sensor";
float MCP3008Sensor::get_setup_priority() const { return setup_priority::DATA; }
void MCP3008Sensor::dump_config() {
ESP_LOGCONFIG(TAG, "MCP3008Sensor:");
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
}
float MCP3008Sensor::sample() {
float value_v = this->parent_->read_data(pin_);
value_v = (value_v * this->reference_voltage_);
return value_v;
}
void MCP3008Sensor::update() { this->publish_state(this->sample()); }
} // namespace mcp3008
} // namespace esphome

View File

@ -0,0 +1,31 @@
#pragma once
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
#include "esphome/core/component.h"
#include "../mcp3008.h"
namespace esphome {
namespace mcp3008 {
class MCP3008Sensor : public PollingComponent,
public sensor::Sensor,
public voltage_sampler::VoltageSampler,
public Parented<MCP3008> {
public:
void set_reference_voltage(float reference_voltage) { this->reference_voltage_ = reference_voltage; }
void set_pin(uint8_t pin) { this->pin_ = pin; }
void update() override;
void dump_config() override;
float get_setup_priority() const override;
float sample() override;
protected:
uint8_t pin_;
float reference_voltage_;
};
} // namespace mcp3008
} // namespace esphome

View File

@ -54,7 +54,8 @@ bool MopekaProCheck::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
if (static_cast<SensorType>(manu_data.data[0]) != STANDARD_BOTTOM_UP &&
static_cast<SensorType>(manu_data.data[0]) != LIPPERT_BOTTOM_UP &&
static_cast<SensorType>(manu_data.data[0]) != PLUS_BOTTOM_UP) {
static_cast<SensorType>(manu_data.data[0]) != PLUS_BOTTOM_UP &&
static_cast<SensorType>(manu_data.data[0]) != PRO_UNIVERSAL) {
ESP_LOGE(TAG, "Unsupported Sensor Type (0x%X)", manu_data.data[0]);
return false;
}

View File

@ -17,7 +17,9 @@ enum SensorType {
TOP_DOWN_AIR_ABOVE = 0x04,
BOTTOM_UP_WATER = 0x05,
LIPPERT_BOTTOM_UP = 0x06,
PLUS_BOTTOM_UP = 0x08
PLUS_BOTTOM_UP = 0x08,
PRO_UNIVERSAL = 0xC // Pro Check Universal
// all other values are reserved
};

View File

@ -17,9 +17,12 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
auto traits = this->device_->get_traits();
// current_temperature_topic
if (traits.get_supports_current_temperature()) {
// current_temperature_topic
root[MQTT_CURRENT_TEMPERATURE_TOPIC] = this->get_current_temperature_state_topic();
}
// current_humidity_topic
if (traits.get_supports_current_humidity()) {
root[MQTT_CURRENT_HUMIDITY_TOPIC] = this->get_current_humidity_state_topic();
}
// mode_command_topic
root[MQTT_MODE_COMMAND_TOPIC] = this->get_mode_command_topic();
// mode_state_topic
@ -57,6 +60,13 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
root[MQTT_TEMPERATURE_STATE_TOPIC] = this->get_target_temperature_state_topic();
}
if (traits.get_supports_target_humidity()) {
// target_humidity_command_topic
root[MQTT_TARGET_HUMIDITY_COMMAND_TOPIC] = this->get_target_humidity_command_topic();
// target_humidity_state_topic
root[MQTT_TARGET_HUMIDITY_STATE_TOPIC] = this->get_target_humidity_state_topic();
}
// min_temp
root[MQTT_MIN_TEMP] = traits.get_visual_min_temperature();
// max_temp
@ -66,6 +76,11 @@ void MQTTClimateComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCo
// temperature units are always coerced to Celsius internally
root[MQTT_TEMPERATURE_UNIT] = "C";
// min_humidity
root[MQTT_MIN_HUMIDITY] = traits.get_visual_min_humidity();
// max_humidity
root[MQTT_MAX_HUMIDITY] = traits.get_visual_max_humidity();
if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
// preset_mode_command_topic
root[MQTT_PRESET_MODE_COMMAND_TOPIC] = this->get_preset_command_topic();
@ -192,6 +207,20 @@ void MQTTClimateComponent::setup() {
});
}
if (traits.get_supports_target_humidity()) {
this->subscribe(this->get_target_humidity_command_topic(),
[this](const std::string &topic, const std::string &payload) {
auto val = parse_number<float>(payload);
if (!val.has_value()) {
ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str());
return;
}
auto call = this->device_->make_call();
call.set_target_humidity(*val);
call.perform();
});
}
if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
this->subscribe(this->get_preset_command_topic(), [this](const std::string &topic, const std::string &payload) {
auto call = this->device_->make_call();
@ -273,6 +302,17 @@ bool MQTTClimateComponent::publish_state_() {
success = false;
}
if (traits.get_supports_current_humidity() && !std::isnan(this->device_->current_humidity)) {
std::string payload = value_accuracy_to_string(this->device_->current_humidity, 0);
if (!this->publish(this->get_current_humidity_state_topic(), payload))
success = false;
}
if (traits.get_supports_target_humidity() && !std::isnan(this->device_->target_humidity)) {
std::string payload = value_accuracy_to_string(this->device_->target_humidity, 0);
if (!this->publish(this->get_target_humidity_state_topic(), payload))
success = false;
}
if (traits.get_supports_presets() || !traits.get_supported_custom_presets().empty()) {
std::string payload;
if (this->device_->preset.has_value()) {

View File

@ -20,6 +20,7 @@ class MQTTClimateComponent : public mqtt::MQTTComponent {
void setup() override;
MQTT_COMPONENT_CUSTOM_TOPIC(current_temperature, state)
MQTT_COMPONENT_CUSTOM_TOPIC(current_humidity, state)
MQTT_COMPONENT_CUSTOM_TOPIC(mode, state)
MQTT_COMPONENT_CUSTOM_TOPIC(mode, command)
MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature, state)
@ -28,6 +29,8 @@ class MQTTClimateComponent : public mqtt::MQTTComponent {
MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_low, command)
MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_high, state)
MQTT_COMPONENT_CUSTOM_TOPIC(target_temperature_high, command)
MQTT_COMPONENT_CUSTOM_TOPIC(target_humidity, state)
MQTT_COMPONENT_CUSTOM_TOPIC(target_humidity, command)
MQTT_COMPONENT_CUSTOM_TOPIC(away, state)
MQTT_COMPONENT_CUSTOM_TOPIC(away, command)
MQTT_COMPONENT_CUSTOM_TOPIC(action, state)

View File

@ -76,7 +76,11 @@ bool MQTTComponent::send_discovery_() {
this->send_discovery(root, config);
// Fields from EntityBase
root[MQTT_NAME] = this->friendly_name();
if (this->get_entity()->has_own_name()) {
root[MQTT_NAME] = this->friendly_name();
} else {
root[MQTT_NAME] = "";
}
if (this->is_disabled_by_default())
root[MQTT_ENABLED_BY_DEFAULT] = false;
if (!this->get_icon().empty())

View File

@ -51,6 +51,8 @@ constexpr const char *const MQTT_CODE_ARM_REQUIRED = "cod_arm_req";
constexpr const char *const MQTT_CODE_DISARM_REQUIRED = "cod_dis_req";
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TOPIC = "curr_temp_t";
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TEMPLATE = "curr_temp_tpl";
constexpr const char *const MQTT_CURRENT_HUMIDITY_TOPIC = "curr_hum_t";
constexpr const char *const MQTT_CURRENT_HUMIDITY_TEMPLATE = "curr_hum_tpl";
constexpr const char *const MQTT_DEVICE = "dev";
constexpr const char *const MQTT_DEVICE_CLASS = "dev_cla";
constexpr const char *const MQTT_DOCKED_TOPIC = "dock_t";
@ -305,6 +307,8 @@ constexpr const char *const MQTT_CODE_ARM_REQUIRED = "code_arm_required";
constexpr const char *const MQTT_CODE_DISARM_REQUIRED = "code_disarm_required";
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TOPIC = "current_temperature_topic";
constexpr const char *const MQTT_CURRENT_TEMPERATURE_TEMPLATE = "current_temperature_template";
constexpr const char *const MQTT_CURRENT_HUMIDITY_TOPIC = "current_humidity_topic";
constexpr const char *const MQTT_CURRENT_HUMIDITY_TEMPLATE = "current_humidity_template";
constexpr const char *const MQTT_DEVICE = "device";
constexpr const char *const MQTT_DEVICE_CLASS = "device_class";
constexpr const char *const MQTT_DOCKED_TOPIC = "docked_topic";

View File

@ -33,5 +33,14 @@ class PageTrigger : public Trigger<uint8_t> {
}
};
class TouchTrigger : public Trigger<uint8_t, uint8_t, bool> {
public:
explicit TouchTrigger(Nextion *nextion) {
nextion->add_touch_event_callback([this](uint8_t page_id, uint8_t component_id, bool touch_event) {
this->trigger(page_id, component_id, touch_event);
});
}
};
} // namespace nextion
} // namespace esphome

View File

@ -29,6 +29,7 @@ CONF_BACKGROUND_PRESSED_COLOR = "background_pressed_color"
CONF_FOREGROUND_COLOR = "foreground_color"
CONF_FOREGROUND_PRESSED_COLOR = "foreground_pressed_color"
CONF_FONT_ID = "font_id"
CONF_EXIT_REPARSE_ON_START = "exit_reparse_on_start"
def NextionName(value):

View File

@ -7,6 +7,7 @@ from esphome.const import (
CONF_LAMBDA,
CONF_BRIGHTNESS,
CONF_TRIGGER_ID,
CONF_ON_TOUCH,
)
from esphome.core import CORE
from . import Nextion, nextion_ns, nextion_ref
@ -20,6 +21,7 @@ from .base_component import (
CONF_WAKE_UP_PAGE,
CONF_START_UP_PAGE,
CONF_AUTO_WAKE_ON_TOUCH,
CONF_EXIT_REPARSE_ON_START,
)
CODEOWNERS = ["@senexcrenshaw"]
@ -31,12 +33,13 @@ SetupTrigger = nextion_ns.class_("SetupTrigger", automation.Trigger.template())
SleepTrigger = nextion_ns.class_("SleepTrigger", automation.Trigger.template())
WakeTrigger = nextion_ns.class_("WakeTrigger", automation.Trigger.template())
PageTrigger = nextion_ns.class_("PageTrigger", automation.Trigger.template())
TouchTrigger = nextion_ns.class_("TouchTrigger", automation.Trigger.template())
CONFIG_SCHEMA = (
display.BASIC_DISPLAY_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(Nextion),
cv.Optional(CONF_TFT_URL): cv.All(cv.string, cv.only_with_arduino),
cv.Optional(CONF_TFT_URL): cv.url,
cv.Optional(CONF_BRIGHTNESS, default=1.0): cv.percentage,
cv.Optional(CONF_ON_SETUP): automation.validate_automation(
{
@ -58,10 +61,16 @@ CONFIG_SCHEMA = (
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PageTrigger),
}
),
cv.Optional(CONF_ON_TOUCH): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(TouchTrigger),
}
),
cv.Optional(CONF_TOUCH_SLEEP_TIMEOUT): cv.int_range(min=3, max=65535),
cv.Optional(CONF_WAKE_UP_PAGE): cv.positive_int,
cv.Optional(CONF_START_UP_PAGE): cv.positive_int,
cv.Optional(CONF_AUTO_WAKE_ON_TOUCH, default=True): cv.boolean,
cv.Optional(CONF_EXIT_REPARSE_ON_START, default=False): cv.boolean,
}
)
.extend(cv.polling_component_schema("5s"))
@ -71,7 +80,6 @@ CONFIG_SCHEMA = (
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
if CONF_BRIGHTNESS in config:
@ -85,10 +93,10 @@ async def to_code(config):
if CONF_TFT_URL in config:
cg.add_define("USE_NEXTION_TFT_UPLOAD")
cg.add(var.set_tft_url(config[CONF_TFT_URL]))
if CORE.is_esp32:
if CORE.is_esp32 and CORE.using_arduino:
cg.add_library("WiFiClientSecure", None)
cg.add_library("HTTPClient", None)
if CORE.is_esp8266:
elif CORE.is_esp8266 and CORE.using_arduino:
cg.add_library("ESP8266HTTPClient", None)
if CONF_TOUCH_SLEEP_TIMEOUT in config:
@ -100,8 +108,9 @@ async def to_code(config):
if CONF_START_UP_PAGE in config:
cg.add(var.set_start_up_page_internal(config[CONF_START_UP_PAGE]))
if CONF_AUTO_WAKE_ON_TOUCH in config:
cg.add(var.set_auto_wake_on_touch_internal(config[CONF_AUTO_WAKE_ON_TOUCH]))
cg.add(var.set_auto_wake_on_touch_internal(config[CONF_AUTO_WAKE_ON_TOUCH]))
cg.add(var.set_exit_reparse_on_start_internal(config[CONF_EXIT_REPARSE_ON_START]))
await display.register_display(var, config)
@ -120,3 +129,15 @@ async def to_code(config):
for conf in config.get(CONF_ON_PAGE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(cg.uint8, "x")], conf)
for conf in config.get(CONF_ON_TOUCH, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(
trigger,
[
(cg.uint8, "page_id"),
(cg.uint8, "component_id"),
(cg.bool_, "touch_event"),
],
conf,
)

View File

@ -2,6 +2,7 @@
#include "esphome/core/util.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include <cinttypes>
namespace esphome {
namespace nextion {
@ -47,6 +48,9 @@ bool Nextion::check_connect_() {
this->ignore_is_setup_ = true;
this->send_command_("boguscommand=0"); // bogus command. needed sometimes after updating
if (this->exit_reparse_on_start_) {
this->send_command_("DRAKJHSUYDGBNCJHGJKSHBDN");
}
this->send_command_("connect");
this->comok_sent_ = millis();
@ -93,7 +97,8 @@ bool Nextion::check_connect_() {
connect_info.push_back(response.substr(start, end - start));
}
if (connect_info.size() == 7) {
this->is_detected_ = (connect_info.size() == 7);
if (this->is_detected_) {
ESP_LOGN(TAG, "Received connect_info %zu", connect_info.size());
this->device_model_ = connect_info[2];
@ -125,18 +130,19 @@ void Nextion::dump_config() {
ESP_LOGCONFIG(TAG, " Firmware Version: %s", this->firmware_version_.c_str());
ESP_LOGCONFIG(TAG, " Serial Number: %s", this->serial_number_.c_str());
ESP_LOGCONFIG(TAG, " Flash Size: %s", this->flash_size_.c_str());
ESP_LOGCONFIG(TAG, " Wake On Touch: %s", this->auto_wake_on_touch_ ? "True" : "False");
ESP_LOGCONFIG(TAG, " Wake On Touch: %s", YESNO(this->auto_wake_on_touch_));
ESP_LOGCONFIG(TAG, " Exit reparse: %s", YESNO(this->exit_reparse_on_start_));
if (this->touch_sleep_timeout_ != 0) {
ESP_LOGCONFIG(TAG, " Touch Timeout: %d", this->touch_sleep_timeout_);
ESP_LOGCONFIG(TAG, " Touch Timeout: %" PRIu32, this->touch_sleep_timeout_);
}
if (this->wake_up_page_ != -1) {
ESP_LOGCONFIG(TAG, " Wake Up Page : %d", this->wake_up_page_);
ESP_LOGCONFIG(TAG, " Wake Up Page: %d", this->wake_up_page_);
}
if (this->start_up_page_ != -1) {
ESP_LOGCONFIG(TAG, " Start Up Page : %d", this->start_up_page_);
ESP_LOGCONFIG(TAG, " Start Up Page: %d", this->start_up_page_);
}
}
@ -166,6 +172,10 @@ void Nextion::add_new_page_callback(std::function<void(uint8_t)> &&callback) {
this->page_callback_.add(std::move(callback));
}
void Nextion::add_touch_event_callback(std::function<void(uint8_t, uint8_t, bool)> &&callback) {
this->touch_callback_.add(std::move(callback));
}
void Nextion::update_all_components() {
if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping())
return;
@ -242,6 +252,7 @@ void Nextion::loop() {
}
this->set_auto_wake_on_touch(this->auto_wake_on_touch_);
this->set_exit_reparse_on_start(this->exit_reparse_on_start_);
if (this->touch_sleep_timeout_ != 0) {
this->set_touch_sleep_timeout(this->touch_sleep_timeout_);
@ -432,11 +443,14 @@ void Nextion::process_nextion_commands_() {
uint8_t page_id = to_process[0];
uint8_t component_id = to_process[1];
uint8_t touch_event = to_process[2]; // 0 -> release, 1 -> press
ESP_LOGD(TAG, "Got touch page=%u component=%u type=%s", page_id, component_id,
touch_event ? "PRESS" : "RELEASE");
ESP_LOGD(TAG, "Got touch event:");
ESP_LOGD(TAG, " page_id: %u", page_id);
ESP_LOGD(TAG, " component_id: %u", component_id);
ESP_LOGD(TAG, " event type: %s", touch_event ? "PRESS" : "RELEASE");
for (auto *touch : this->touch_) {
touch->process_touch(page_id, component_id, touch_event != 0);
}
this->touch_callback_.call(page_id, component_id, touch_event != 0);
break;
}
case 0x66: { // Nextion initiated new page event return data.
@ -447,7 +461,7 @@ void Nextion::process_nextion_commands_() {
}
uint8_t page_id = to_process[0];
ESP_LOGD(TAG, "Got new page=%u", page_id);
ESP_LOGD(TAG, "Got new page: %u", page_id);
this->page_callback_.call(page_id);
break;
}
@ -465,7 +479,10 @@ void Nextion::process_nextion_commands_() {
uint16_t x = (uint16_t(to_process[0]) << 8) | to_process[1];
uint16_t y = (uint16_t(to_process[2]) << 8) | to_process[3];
uint8_t touch_event = to_process[4]; // 0 -> release, 1 -> press
ESP_LOGD(TAG, "Got touch at x=%u y=%u type=%s", x, y, touch_event ? "PRESS" : "RELEASE");
ESP_LOGD(TAG, "Got touch event:");
ESP_LOGD(TAG, " x: %u", x);
ESP_LOGD(TAG, " y: %u", y);
ESP_LOGD(TAG, " type: %s", touch_event ? "PRESS" : "RELEASE");
break;
}
@ -587,7 +604,9 @@ void Nextion::process_nextion_commands_() {
variable_name = to_process.substr(0, index);
++index;
ESP_LOGN(TAG, "Got Switch variable_name=%s value=%d", variable_name.c_str(), to_process[0] != 0);
ESP_LOGN(TAG, "Got Switch:");
ESP_LOGN(TAG, " variable_name: %s", variable_name.c_str());
ESP_LOGN(TAG, " value: %d", to_process[0] != 0);
for (auto *switchtype : this->switchtype_) {
switchtype->process_bool(variable_name, to_process[index] != 0);
@ -618,7 +637,9 @@ void Nextion::process_nextion_commands_() {
value += to_process[i + index + 1] << (8 * i);
}
ESP_LOGN(TAG, "Got sensor variable_name=%s value=%d", variable_name.c_str(), value);
ESP_LOGN(TAG, "Got sensor:");
ESP_LOGN(TAG, " variable_name: %s", variable_name.c_str());
ESP_LOGN(TAG, " value: %d", value);
for (auto *sensor : this->sensortype_) {
sensor->process_sensor(variable_name, value);
@ -650,7 +671,9 @@ void Nextion::process_nextion_commands_() {
text_value = to_process.substr(index);
ESP_LOGN(TAG, "Got Text Sensor variable_name=%s value=%s", variable_name.c_str(), text_value.c_str());
ESP_LOGN(TAG, "Got Text Sensor:");
ESP_LOGN(TAG, " variable_name: %s", variable_name.c_str());
ESP_LOGN(TAG, " value: %s", text_value.c_str());
// NextionTextSensorResponseQueue *nq = new NextionTextSensorResponseQueue;
// nq->variable_name = variable_name;
@ -681,7 +704,9 @@ void Nextion::process_nextion_commands_() {
variable_name = to_process.substr(0, index);
++index;
ESP_LOGN(TAG, "Got Binary Sensor variable_name=%s value=%d", variable_name.c_str(), to_process[index] != 0);
ESP_LOGN(TAG, "Got Binary Sensor:");
ESP_LOGN(TAG, " variable_name: %s", variable_name.c_str());
ESP_LOGN(TAG, " value: %d", to_process[index] != 0);
for (auto *binarysensortype : this->binarysensortype_) {
binarysensortype->process_bool(&variable_name[0], to_process[index] != 0);
@ -771,7 +796,10 @@ void Nextion::set_nextion_sensor_state(int queue_type, const std::string &name,
}
void Nextion::set_nextion_sensor_state(NextionQueueType queue_type, const std::string &name, float state) {
ESP_LOGN(TAG, "Received state for variable %s, state %lf for queue type %d", name.c_str(), state, queue_type);
ESP_LOGN(TAG, "Received state:");
ESP_LOGN(TAG, " variable: %s", name.c_str());
ESP_LOGN(TAG, " state: %lf", state);
ESP_LOGN(TAG, " queue type: %d", queue_type);
switch (queue_type) {
case NextionQueueType::SENSOR: {
@ -808,7 +836,9 @@ void Nextion::set_nextion_sensor_state(NextionQueueType queue_type, const std::s
}
void Nextion::set_nextion_text_state(const std::string &name, const std::string &state) {
ESP_LOGD(TAG, "Received state for variable %s, state %s", name.c_str(), state.c_str());
ESP_LOGD(TAG, "Received state:");
ESP_LOGD(TAG, " variable: %s", name.c_str());
ESP_LOGD(TAG, " state: %s", state.c_str());
for (auto *sensor : this->textsensortype_) {
if (name == sensor->get_variable_name()) {
@ -868,6 +898,12 @@ uint16_t Nextion::recv_ret_string_(std::string &response, uint32_t timeout, bool
start = millis();
while ((timeout == 0 && this->available()) || millis() - start <= timeout) {
if (!this->available()) {
App.feed_wdt();
delay(1);
continue;
}
this->read_byte(&c);
if (c == 0xFF) {
nr_of_ff_bytes++;
@ -886,7 +922,7 @@ uint16_t Nextion::recv_ret_string_(std::string &response, uint32_t timeout, bool
}
}
App.feed_wdt();
delay(1);
delay(2);
if (exit_flag || ff_flag) {
break;

View File

@ -12,14 +12,18 @@
#include "esphome/components/display/display_color_utils.h"
#ifdef USE_NEXTION_TFT_UPLOAD
#ifdef ARDUINO
#ifdef USE_ESP32
#include <HTTPClient.h>
#endif
#endif // USE_ESP32
#ifdef USE_ESP8266
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#endif
#endif
#endif // USE_ESP8266
#elif defined(USE_ESP_IDF)
#include <esp_http_client.h>
#endif // ARDUINO vs ESP-IDF
#endif // USE_NEXTION_TFT_UPLOAD
namespace esphome {
namespace nextion {
@ -91,16 +95,18 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
/**
* Set the background color of a component.
* @param component The component name.
* @param color The color (as a uint32_t).
* @param color The color (as a uint16_t).
*
* Example:
* ```cpp
* it.set_component_background_color("button", 0xFF0000);
* it.set_component_background_color("button", 63488);
* ```
*
* This will change the background color of the component `button` to red.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void set_component_background_color(const char *component, uint32_t color);
void set_component_background_color(const char *component, uint16_t color);
/**
* Set the background color of a component.
* @param component The component name.
@ -111,9 +117,8 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* it.set_component_background_color("button", "RED");
* ```
*
* This will change the background color of the component `button` to blue.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
* This will change the background color of the component `button` to red.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void set_component_background_color(const char *component, const char *color);
/**
@ -123,26 +128,29 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.set_component_background_color("button", color);
* auto blue = Color(0, 0, 255);
* it.set_component_background_color("button", blue);
* ```
*
* This will change the background color of the component `button` to what color contains.
* This will change the background color of the component `button` to blue.
*/
void set_component_background_color(const char *component, Color color) override;
/**
* Set the pressed background color of a component.
* @param component The component name.
* @param color The color (as a int).
* @param color The color (as a uint16_t).
*
* Example:
* ```cpp
* it.set_component_pressed_background_color("button", 0xFF0000 );
* it.set_component_pressed_background_color("button", 63488);
* ```
*
* This will change the pressed background color of the component `button` to red. This is the background color that
* is shown when the component is pressed.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void set_component_pressed_background_color(const char *component, uint32_t color);
void set_component_pressed_background_color(const char *component, uint16_t color);
/**
* Set the pressed background color of a component.
* @param component The component name.
@ -153,10 +161,9 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* it.set_component_pressed_background_color("button", "RED");
* ```
*
* This will change the pressed background color of the component `button` to blue. This is the background color that
* is shown when the component is pressed. Use this [color
* picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI
* colors.
* This will change the pressed background color of the component `button` to red. This is the background color that
* is shown when the component is pressed.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void set_component_pressed_background_color(const char *component, const char *color);
/**
@ -166,15 +173,102 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.set_component_pressed_background_color("button", color);
* auto red = Color(255, 0, 0);
* it.set_component_pressed_background_color("button", red);
* ```
*
* This will change the pressed background color of the component `button` to blue. This is the background color that
* is shown when the component is pressed. Use this [color
* picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI
* colors.
* This will change the pressed background color of the component `button` to red. This is the background color that
* is shown when the component is pressed.
*/
void set_component_pressed_background_color(const char *component, Color color) override;
/**
* Set the foreground color of a component.
* @param component The component name.
* @param color The color (as a uint16_t).
*
* Example:
* ```cpp
* it.set_component_foreground_color("button", 63488);
* ```
*
* This will change the foreground color of the component `button` to red.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void set_component_foreground_color(const char *component, uint16_t color);
/**
* Set the foreground color of a component.
* @param component The component name.
* @param color The color (as a string).
*
* Example:
* ```cpp
* it.set_component_foreground_color("button", "RED");
* ```
*
* This will change the foreground color of the component `button` to red.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void set_component_foreground_color(const char *component, const char *color);
/**
* Set the foreground color of a component.
* @param component The component name.
* @param color The color (as Color).
*
* Example:
* ```cpp
* it.set_component_foreground_color("button", Color::BLACK);
* ```
*
* This will change the foreground color of the component `button` to black.
*/
void set_component_foreground_color(const char *component, Color color) override;
/**
* Set the pressed foreground color of a component.
* @param component The component name.
* @param color The color (as a uint16_t).
*
* Example:
* ```cpp
* it.set_component_pressed_foreground_color("button", 63488 );
* ```
*
* This will change the pressed foreground color of the component `button` to red. This is the foreground color that
* is shown when the component is pressed.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void set_component_pressed_foreground_color(const char *component, uint16_t color);
/**
* Set the pressed foreground color of a component.
* @param component The component name.
* @param color The color (as a string).
*
* Example:
* ```cpp
* it.set_component_pressed_foreground_color("button", "RED");
* ```
*
* This will change the pressed foreground color of the component `button` to red. This is the foreground color that
* is shown when the component is pressed.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void set_component_pressed_foreground_color(const char *component, const char *color);
/**
* Set the pressed foreground color of a component.
* @param component The component name.
* @param color The color (as Color).
*
* Example:
* ```cpp
* auto blue = Color(0, 0, 255);
* it.set_component_pressed_foreground_color("button", blue);
* ```
*
* This will change the pressed foreground color of the component `button` to blue. This is the foreground color that
* is shown when the component is pressed.
*/
void set_component_pressed_foreground_color(const char *component, Color color) override;
/**
* Set the picture id of a component.
@ -206,16 +300,18 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
/**
* Set the font color of a component.
* @param component The component name.
* @param color The color (as a uint32_t ).
* @param color The color (as a uint16_t).
*
* Example:
* ```cpp
* it.set_component_font_color("textview", 0xFF0000);
* it.set_component_font_color("textview", 63488);
* ```
*
* This will change the font color of the component `textview` to a red color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void set_component_font_color(const char *component, uint32_t color);
void set_component_font_color(const char *component, uint16_t color);
/**
* Set the font color of a component.
* @param component The component name.
@ -226,9 +322,8 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* it.set_component_font_color("textview", "RED");
* ```
*
* This will change the font color of the component `textview` to a blue color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
* This will change the font color of the component `textview` to a red color.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void set_component_font_color(const char *component, const char *color);
/**
@ -238,27 +333,27 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.set_component_font_color("textview", color);
* it.set_component_font_color("textview", Color::BLACK);
* ```
*
* This will change the font color of the component `textview` to a blue color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
* This will change the font color of the component `textview` to black.
*/
void set_component_font_color(const char *component, Color color) override;
/**
* Set the pressed font color of a component.
* @param component The component name.
* @param color The color (as a uint32_t).
* @param color The color (as a uint16_t).
*
* Example:
* ```cpp
* it.set_component_pressed_font_color("button", 0xFF0000);
* it.set_component_pressed_font_color("button", 63488);
* ```
*
* This will change the pressed font color of the component `button` to a red.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void set_component_pressed_font_color(const char *component, uint32_t color);
void set_component_pressed_font_color(const char *component, uint16_t color);
/**
* Set the pressed font color of a component.
* @param component The component name.
@ -269,9 +364,8 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* it.set_component_pressed_font_color("button", "RED");
* ```
*
* This will change the pressed font color of the component `button` to a blue color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
* This will change the pressed font color of the component `button` to a red color.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void set_component_pressed_font_color(const char *component, const char *color);
/**
@ -281,12 +375,10 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.set_component_pressed_font_color("button", color);
* it.set_component_pressed_font_color("button", Color::BLACK);
* ```
*
* This will change the pressed font color of the component `button` to a blue color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
* This will change the pressed font color of the component `button` to black.
*/
void set_component_pressed_font_color(const char *component, Color color) override;
/**
@ -416,6 +508,25 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* Displays the picture who has the id `2` at the x coordinates `15` and y coordinates `25`.
*/
void display_picture(int picture_id, int x_start, int y_start);
/**
* Fill a rectangle with a color.
* @param x1 The starting x coordinate.
* @param y1 The starting y coordinate.
* @param width The width to draw.
* @param height The height to draw.
* @param color The color to draw with (number).
*
* Example:
* ```cpp
* fill_area(50, 50, 100, 100, 63488);
* ```
*
* Fills an area that starts at x coordinate `50` and y coordinate `50` with a height of `100` and width of `100` with
* the red color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void fill_area(int x1, int y1, int width, int height, uint16_t color);
/**
* Fill a rectangle with a color.
* @param x1 The starting x coordinate.
@ -430,8 +541,8 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* ```
*
* Fills an area that starts at x coordinate `50` and y coordinate `50` with a height of `100` and width of `100` with
* the color of blue. Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to
* convert color codes to Nextion HMI colors
* the red color.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void fill_area(int x1, int y1, int width, int height, const char *color);
/**
@ -444,14 +555,33 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* fill_area(50, 50, 100, 100, color);
* auto blue = Color(0, 0, 255);
* fill_area(50, 50, 100, 100, blue);
* ```
*
* Fills an area that starts at x coordinate `50` and y coordinate `50` with a height of `100` and width of `100` with
* the color of blue. Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to
* convert color codes to Nextion HMI colors
* blue color.
*/
void fill_area(int x1, int y1, int width, int height, Color color);
/**
* Draw a line on the screen.
* @param x1 The starting x coordinate.
* @param y1 The starting y coordinate.
* @param x2 The ending x coordinate.
* @param y2 The ending y coordinate.
* @param color The color to draw with (number).
*
* Example:
* ```cpp
* it.line(50, 50, 75, 75, 63488);
* ```
*
* Makes a line that starts at x coordinate `50` and y coordinate `50` and ends at x coordinate `75` and y coordinate
* `75` with the red color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void line(int x1, int y1, int x2, int y2, uint16_t color);
/**
* Draw a line on the screen.
* @param x1 The starting x coordinate.
@ -462,13 +592,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.line(50, 50, 75, 75, "17013");
* it.line(50, 50, 75, 75, "BLUE");
* ```
*
* Makes a line that starts at x coordinate `50` and y coordinate `50` and ends at x coordinate `75` and y coordinate
* `75` with the color of blue. Use this [color
* picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI
* colors.
* `75` with the blue color.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void line(int x1, int y1, int x2, int y2, const char *color);
/**
@ -481,15 +610,33 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.line(50, 50, 75, 75, "17013");
* auto blue = Color(0, 0, 255);
* it.line(50, 50, 75, 75, blue);
* ```
*
* Makes a line that starts at x coordinate `50` and y coordinate `50` and ends at x coordinate `75` and y coordinate
* `75` with the color of blue. Use this [color
* picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI
* colors.
* `75` with blue color.
*/
void line(int x1, int y1, int x2, int y2, Color color);
/**
* Draw a rectangle outline.
* @param x1 The starting x coordinate.
* @param y1 The starting y coordinate.
* @param width The width of the rectangle.
* @param height The height of the rectangle.
* @param color The color to draw with (number).
*
* Example:
* ```cpp
* it.rectangle(25, 35, 40, 50, 63488);
* ```
*
* Makes a outline of a rectangle that starts at x coordinate `25` and y coordinate `35` and has a width of `40` and a
* length of `50` with the red color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void rectangle(int x1, int y1, int width, int height, uint16_t color);
/**
* Draw a rectangle outline.
* @param x1 The starting x coordinate.
@ -500,13 +647,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.rectangle(25, 35, 40, 50, "17013");
* it.rectangle(25, 35, 40, 50, "BLUE");
* ```
*
* Makes a outline of a rectangle that starts at x coordinate `25` and y coordinate `35` and has a width of `40` and a
* length of `50` with color of blue. Use this [color
* picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI
* colors.
* length of `50` with the blue color.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void rectangle(int x1, int y1, int width, int height, const char *color);
/**
@ -519,21 +665,31 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.rectangle(25, 35, 40, 50, "17013");
* auto blue = Color(0, 0, 255);
* it.rectangle(25, 35, 40, 50, blue);
* ```
*
* Makes a outline of a rectangle that starts at x coordinate `25` and y coordinate `35` and has a width of `40` and a
* length of `50` with color of blue. Use this [color
* picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to Nextion HMI
* colors.
* length of `50` with blue color.
*/
void rectangle(int x1, int y1, int width, int height, Color color);
/**
* Draw a circle outline
* @param center_x The center x coordinate.
* @param center_y The center y coordinate.
* @param radius The circle radius.
* @param color The color to draw with (number).
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void circle(int center_x, int center_y, int radius, uint16_t color);
/**
* Draw a circle outline
* @param center_x The center x coordinate.
* @param center_y The center y coordinate.
* @param radius The circle radius.
* @param color The color to draw with (as a string).
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void circle(int center_x, int center_y, int radius, const char *color);
/**
@ -544,6 +700,23 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* @param color The color to draw with (as Color).
*/
void circle(int center_x, int center_y, int radius, Color color);
/**
* Draw a filled circled.
* @param center_x The center x coordinate.
* @param center_y The center y coordinate.
* @param radius The circle radius.
* @param color The color to draw with (number).
*
* Example:
* ```cpp
* it.filled_cricle(25, 25, 10, 63488);
* ```
*
* Makes a filled circle at the x coordinate `25` and y coordinate `25` with a radius of `10` with the red color.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
*/
void filled_circle(int center_x, int center_y, int radius, uint16_t color);
/**
* Draw a filled circled.
* @param center_x The center x coordinate.
@ -553,12 +726,11 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.filled_cricle(25, 25, 10, "17013");
* it.filled_cricle(25, 25, 10, "BLUE");
* ```
*
* Makes a filled circle at the x coordinate `25` and y coordinate `25` with a radius of `10` with a color of blue.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
* Makes a filled circle at the x coordinate `25` and y coordinate `25` with a radius of `10` with the blue color.
* Use [Nextion Instruction Set](https://nextion.tech/instruction-set/#s5) for a list of Nextion HMI colors constants.
*/
void filled_circle(int center_x, int center_y, int radius, const char *color);
/**
@ -570,12 +742,11 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*
* Example:
* ```cpp
* it.filled_cricle(25, 25, 10, color);
* auto blue = Color(0, 0, 255);
* it.filled_cricle(25, 25, 10, blue);
* ```
*
* Makes a filled circle at the x coordinate `25` and y coordinate `25` with a radius of `10` with a color of blue.
* Use this [color picker](https://nodtem66.github.io/nextion-hmi-color-convert/index.html) to convert color codes to
* Nextion HMI colors.
* Makes a filled circle at the x coordinate `25` and y coordinate `25` with a radius of `10` with blue color.
*/
void filled_circle(int center_x, int center_y, int radius, Color color);
@ -644,6 +815,19 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* The display will wake up by touch.
*/
void set_auto_wake_on_touch(bool auto_wake);
/**
* Sets if Nextion should exit the active reparse mode before the "connect" command is sent
* @param exit_reparse True or false. When exit_reparse is true, the exit reparse command
* will be sent before requesting the connection from Nextion.
*
* Example:
* ```cpp
* it.set_exit_reparse_on_start(true);
* ```
*
* The display will be requested to leave active reparse mode before setup.
*/
void set_exit_reparse_on_start(bool exit_reparse);
/**
* Sets Nextion mode between sleep and awake
* @param True or false. Sleep=true to enter sleep mode or sleep=false to exit sleep mode.
@ -685,16 +869,18 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
#ifdef USE_NEXTION_TFT_UPLOAD
/**
* Set the tft file URL. https seems problamtic with arduino..
* Set the tft file URL. https seems problematic with arduino..
*/
void set_tft_url(const std::string &tft_url) { this->tft_url_ = tft_url; }
#endif
/**
* Upload the tft file and softreset the Nextion
* Upload the tft file and soft reset Nextion
* @return bool True: Transfer completed successfuly, False: Transfer failed.
*/
void upload_tft();
bool upload_tft();
void dump_config() override;
/**
@ -726,6 +912,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
*/
void add_new_page_callback(std::function<void(uint8_t)> &&callback);
/** Add a callback to be notified when Nextion has a touch event.
*
* @param callback The void() callback.
*/
void add_touch_event_callback(std::function<void(uint8_t, uint8_t, bool)> &&callback);
void update_all_components();
/**
@ -764,6 +956,9 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
void set_wake_up_page_internal(uint8_t wake_up_page) { this->wake_up_page_ = wake_up_page; }
void set_start_up_page_internal(uint8_t start_up_page) { this->start_up_page_ = start_up_page; }
void set_auto_wake_on_touch_internal(bool auto_wake_on_touch) { this->auto_wake_on_touch_ = auto_wake_on_touch; }
void set_exit_reparse_on_start_internal(bool exit_reparse_on_start) {
this->exit_reparse_on_start_ = exit_reparse_on_start;
}
protected:
std::deque<NextionQueue *> nextion_queue_;
@ -787,6 +982,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
int wake_up_page_ = -1;
int start_up_page_ = -1;
bool auto_wake_on_touch_ = true;
bool exit_reparse_on_start_ = false;
/**
* Manually send a raw command to the display and don't wait for an acknowledgement packet.
@ -817,16 +1013,16 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
BearSSL::WiFiClientSecure *wifi_client_secure_{nullptr};
WiFiClient *get_wifi_client_();
#endif
int content_length_ = 0;
int tft_size_ = 0;
#ifdef ARDUINO
/**
* will request chunk_size chunks from the web server
* and send each to the nextion
* @param int contentLength Total size of the file
* @param uint32_t chunk_size
* @return true if success, false for failure.
* @param HTTPClient http HTTP client handler.
* @param int range_start Position of next byte to transfer.
* @return position of last byte transferred, -1 for failure.
*/
int content_length_ = 0;
int tft_size_ = 0;
int upload_by_chunks_(HTTPClient *http, int range_start);
bool upload_with_range_(uint32_t range_start, uint32_t range_end);
@ -839,7 +1035,30 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
* @return true if success, false for failure.
*/
bool upload_from_buffer_(const uint8_t *file_buf, size_t buf_size);
void upload_end_();
/**
* Ends the upload process, restart Nextion and, if successful,
* restarts ESP
* @param bool url successful True: Transfer completed successfuly, False: Transfer failed.
* @return bool True: Transfer completed successfuly, False: Transfer failed.
*/
bool upload_end_(bool successful);
#elif defined(USE_ESP_IDF)
/**
* will request 4096 bytes chunks from the web server
* and send each to Nextion
* @param std::string url Full url for download.
* @param int range_start Position of next byte to transfer.
* @return position of last byte transferred, -1 for failure.
*/
int upload_range(const std::string &url, int range_start);
/**
* Ends the upload process, restart Nextion and, if successful,
* restarts ESP
* @param bool url successful True: Transfer completed successfuly, False: Transfer failed.
* @return bool True: Transfer completed successfuly, False: Transfer failed.
*/
bool upload_end(bool successful);
#endif // ARDUINO vs ESP-IDF
#endif // USE_NEXTION_TFT_UPLOAD
@ -856,6 +1075,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
CallbackManager<void()> sleep_callback_{};
CallbackManager<void()> wake_callback_{};
CallbackManager<void(uint8_t)> page_callback_{};
CallbackManager<void(uint8_t, uint8_t, bool)> touch_callback_{};
optional<nextion_writer_t> writer_;
float brightness_{1.0};

View File

@ -39,6 +39,8 @@ class NextionBase {
virtual void set_component_background_color(const char *component, Color color) = 0;
virtual void set_component_pressed_background_color(const char *component, Color color) = 0;
virtual void set_component_foreground_color(const char *component, Color color) = 0;
virtual void set_component_pressed_foreground_color(const char *component, Color color) = 0;
virtual void set_component_font_color(const char *component, Color color) = 0;
virtual void set_component_pressed_font_color(const char *component, Color color) = 0;
virtual void set_component_font(const char *component, uint8_t font_id) = 0;
@ -48,10 +50,12 @@ class NextionBase {
bool is_sleeping() { return this->is_sleeping_; }
bool is_setup() { return this->is_setup_; }
bool is_detected() { return this->is_detected_; }
protected:
bool is_setup_ = false;
bool is_sleeping_ = false;
bool is_detected_ = false;
};
} // namespace nextion

View File

@ -1,6 +1,7 @@
#include "nextion.h"
#include "esphome/core/util.h"
#include "esphome/core/log.h"
#include <cinttypes>
namespace esphome {
namespace nextion {
@ -52,10 +53,11 @@ void Nextion::set_protocol_reparse_mode(bool active_mode) {
this->write_str("connect");
this->write_array(to_send, sizeof(to_send));
}
void Nextion::set_exit_reparse_on_start(bool exit_reparse) { this->exit_reparse_on_start_ = exit_reparse; }
// Set Colors
void Nextion::set_component_background_color(const char *component, uint32_t color) {
this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%d", component, color);
// Set Colors - Background
void Nextion::set_component_background_color(const char *component, uint16_t color) {
this->add_no_result_to_queue_with_printf_("set_component_background_color", "%s.bco=%" PRIu16, component, color);
}
void Nextion::set_component_background_color(const char *component, const char *color) {
@ -67,8 +69,10 @@ void Nextion::set_component_background_color(const char *component, Color color)
display::ColorUtil::color_to_565(color));
}
void Nextion::set_component_pressed_background_color(const char *component, uint32_t color) {
this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%d", component, color);
// Set Colors - Background (pressed)
void Nextion::set_component_pressed_background_color(const char *component, uint16_t color) {
this->add_no_result_to_queue_with_printf_("set_component_pressed_background_color", "%s.bco2=%" PRIu16, component,
color);
}
void Nextion::set_component_pressed_background_color(const char *component, const char *color) {
@ -80,16 +84,38 @@ void Nextion::set_component_pressed_background_color(const char *component, Colo
display::ColorUtil::color_to_565(color));
}
void Nextion::set_component_pic(const char *component, uint8_t pic_id) {
this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.pic=%d", component, pic_id);
// Set Colors - Foreground
void Nextion::set_component_foreground_color(const char *component, uint16_t color) {
this->add_no_result_to_queue_with_printf_("set_component_foreground_color", "%s.pco=%" PRIu16, component, color);
}
void Nextion::set_component_picc(const char *component, uint8_t pic_id) {
this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.picc=%d", component, pic_id);
void Nextion::set_component_foreground_color(const char *component, const char *color) {
this->add_no_result_to_queue_with_printf_("set_component_foreground_color", "%s.pco=%s", component, color);
}
void Nextion::set_component_font_color(const char *component, uint32_t color) {
this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%d", component, color);
void Nextion::set_component_foreground_color(const char *component, Color color) {
this->add_no_result_to_queue_with_printf_("set_component_foreground_color", "%s.pco=%d", component,
display::ColorUtil::color_to_565(color));
}
// Set Colors - Foreground (pressed)
void Nextion::set_component_pressed_foreground_color(const char *component, uint16_t color) {
this->add_no_result_to_queue_with_printf_("set_component_pressed_foreground_color", "%s.pco2=%" PRIu16, component,
color);
}
void Nextion::set_component_pressed_foreground_color(const char *component, const char *color) {
this->add_no_result_to_queue_with_printf_("set_component_pressed_foreground_color", " %s.pco2=%s", component, color);
}
void Nextion::set_component_pressed_foreground_color(const char *component, Color color) {
this->add_no_result_to_queue_with_printf_("set_component_pressed_foreground_color", "%s.pco2=%d", component,
display::ColorUtil::color_to_565(color));
}
// Set Colors - Font
void Nextion::set_component_font_color(const char *component, uint16_t color) {
this->add_no_result_to_queue_with_printf_("set_component_font_color", "%s.pco=%" PRIu16, component, color);
}
void Nextion::set_component_font_color(const char *component, const char *color) {
@ -101,8 +127,9 @@ void Nextion::set_component_font_color(const char *component, Color color) {
display::ColorUtil::color_to_565(color));
}
void Nextion::set_component_pressed_font_color(const char *component, uint32_t color) {
this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", "%s.pco2=%d", component, color);
// Set Colors - Font (pressed)
void Nextion::set_component_pressed_font_color(const char *component, uint16_t color) {
this->add_no_result_to_queue_with_printf_("set_component_pressed_font_color", "%s.pco2=%" PRIu16, component, color);
}
void Nextion::set_component_pressed_font_color(const char *component, const char *color) {
@ -114,6 +141,15 @@ void Nextion::set_component_pressed_font_color(const char *component, Color colo
display::ColorUtil::color_to_565(color));
}
// Set picture
void Nextion::set_component_pic(const char *component, uint8_t pic_id) {
this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.pic=%d", component, pic_id);
}
void Nextion::set_component_picc(const char *component, uint8_t pic_id) {
this->add_no_result_to_queue_with_printf_("set_component_pic", "%s.picc=%d", component, pic_id);
}
void Nextion::set_component_text_printf(const char *component, const char *format, ...) {
va_list arg;
va_start(arg, format);
@ -192,6 +228,10 @@ void Nextion::display_picture(int picture_id, int x_start, int y_start) {
this->add_no_result_to_queue_with_printf_("display_picture", "pic %d, %d, %d", x_start, y_start, picture_id);
}
void Nextion::fill_area(int x1, int y1, int width, int height, uint16_t color) {
this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%" PRIu16, x1, y1, width, height, color);
}
void Nextion::fill_area(int x1, int y1, int width, int height, const char *color) {
this->add_no_result_to_queue_with_printf_("fill_area", "fill %d,%d,%d,%d,%s", x1, y1, width, height, color);
}
@ -201,6 +241,10 @@ void Nextion::fill_area(int x1, int y1, int width, int height, Color color) {
display::ColorUtil::color_to_565(color));
}
void Nextion::line(int x1, int y1, int x2, int y2, uint16_t color) {
this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%" PRIu16, x1, y1, x2, y2, color);
}
void Nextion::line(int x1, int y1, int x2, int y2, const char *color) {
this->add_no_result_to_queue_with_printf_("line", "line %d,%d,%d,%d,%s", x1, y1, x2, y2, color);
}
@ -210,6 +254,11 @@ void Nextion::line(int x1, int y1, int x2, int y2, Color color) {
display::ColorUtil::color_to_565(color));
}
void Nextion::rectangle(int x1, int y1, int width, int height, uint16_t color) {
this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%" PRIu16, x1, y1, x1 + width, y1 + height,
color);
}
void Nextion::rectangle(int x1, int y1, int width, int height, const char *color) {
this->add_no_result_to_queue_with_printf_("draw", "draw %d,%d,%d,%d,%s", x1, y1, x1 + width, y1 + height, color);
}
@ -219,6 +268,10 @@ void Nextion::rectangle(int x1, int y1, int width, int height, Color color) {
display::ColorUtil::color_to_565(color));
}
void Nextion::circle(int center_x, int center_y, int radius, uint16_t color) {
this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%" PRIu16, center_x, center_y, radius, color);
}
void Nextion::circle(int center_x, int center_y, int radius, const char *color) {
this->add_no_result_to_queue_with_printf_("cir", "cir %d,%d,%d,%s", center_x, center_y, radius, color);
}
@ -228,6 +281,10 @@ void Nextion::circle(int center_x, int center_y, int radius, Color color) {
display::ColorUtil::color_to_565(color));
}
void Nextion::filled_circle(int center_x, int center_y, int radius, uint16_t color) {
this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%" PRIu16, center_x, center_y, radius, color);
}
void Nextion::filled_circle(int center_x, int center_y, int radius, const char *color) {
this->add_no_result_to_queue_with_printf_("cirs", "cirs %d,%d,%d,%s", center_x, center_y, radius, color);
}

View File

@ -99,11 +99,11 @@ void NextionComponent::update_component_settings(bool force_update) {
this->bco2_needs_update_ = false;
}
if (this->pco_needs_update_ || (force_update && this->pco_is_set_)) {
this->nextion_->set_component_font_color(this->variable_name_.c_str(), this->pco_);
this->nextion_->set_component_foreground_color(this->variable_name_.c_str(), this->pco_);
this->pco_needs_update_ = false;
}
if (this->pco2_needs_update_ || (force_update && this->pco2_is_set_)) {
this->nextion_->set_component_pressed_font_color(this->variable_name_.c_str(), this->pco2_);
this->nextion_->set_component_pressed_foreground_color(this->variable_name_.c_str(), this->pco2_);
this->pco2_needs_update_ = false;
}

View File

@ -1,5 +1,6 @@
#include "nextion.h"
#ifdef ARDUINO
#ifdef USE_NEXTION_TFT_UPLOAD
#include "esphome/core/application.h"
@ -14,7 +15,7 @@
namespace esphome {
namespace nextion {
static const char *const TAG = "nextion_upload";
static const char *const TAG = "nextion.upload.arduino";
// Followed guide
// https://unofficialnextion.com/t/nextion-upload-protocol-v1-2-the-fast-one/1044/2
@ -40,7 +41,7 @@ int Nextion::upload_by_chunks_(HTTPClient *http, int range_start) {
#if USE_ARDUINO_VERSION_CODE >= VERSION_CODE(2, 6, 0)
http->setRedirectLimit(3);
#endif
#endif
#endif // USE_ESP8266
char range_header[64];
sprintf(range_header, "bytes=%d-%d", range_start, range_end);
@ -61,6 +62,7 @@ int Nextion::upload_by_chunks_(HTTPClient *http, int range_start) {
++tries;
if (!begin_status) {
ESP_LOGD(TAG, "upload_by_chunks_: connection failed");
delay(500); // NOLINT
continue;
}
@ -83,10 +85,10 @@ int Nextion::upload_by_chunks_(HTTPClient *http, int range_start) {
std::string recv_string;
size_t size = 0;
int sent = 0;
int fetched = 0;
int range = range_end - range_start;
while (sent < range) {
while (fetched < range) {
size = http->getStreamPtr()->available();
if (!size) {
App.feed_wdt();
@ -94,28 +96,38 @@ int Nextion::upload_by_chunks_(HTTPClient *http, int range_start) {
continue;
}
int c = http->getStreamPtr()->readBytes(
&this->transfer_buffer_[sent], ((size > this->transfer_buffer_size_) ? this->transfer_buffer_size_ : size));
sent += c;
&this->transfer_buffer_[fetched], ((size > this->transfer_buffer_size_) ? this->transfer_buffer_size_ : size));
fetched += c;
}
http->end();
ESP_LOGN(TAG, "this->content_length_ %d sent %d", this->content_length_, sent);
ESP_LOGN(TAG, "Fetched %d of %d bytes", fetched, this->content_length_);
// upload fetched segments to the display in 4KB chunks
int write_len;
for (int i = 0; i < range; i += 4096) {
this->write_array(&this->transfer_buffer_[i], 4096);
this->content_length_ -= 4096;
ESP_LOGN(TAG, "this->content_length_ %d range %d range_end %d range_start %d", this->content_length_, range,
range_end, range_start);
App.feed_wdt();
write_len = this->content_length_ < 4096 ? this->content_length_ : 4096;
this->write_array(&this->transfer_buffer_[i], write_len);
this->content_length_ -= write_len;
ESP_LOGD(TAG, "Uploaded %0.2f %%; %d bytes remaining",
100.0 * (this->tft_size_ - this->content_length_) / this->tft_size_, this->content_length_);
if (!this->upload_first_chunk_sent_) {
this->upload_first_chunk_sent_ = true;
delay(500); // NOLINT
App.feed_wdt();
}
this->recv_ret_string_(recv_string, 2048, true);
if (recv_string[0] == 0x08) {
this->recv_ret_string_(recv_string, 4096, true);
if (recv_string[0] != 0x05) { // 0x05 == "ok"
ESP_LOGD(TAG, "recv_string [%s]",
format_hex_pretty(reinterpret_cast<const uint8_t *>(recv_string.data()), recv_string.size()).c_str());
}
// handle partial upload request
if (recv_string[0] == 0x08 && recv_string.size() == 5) {
uint32_t result = 0;
for (int i = 0; i < 4; ++i) {
result += static_cast<uint8_t>(recv_string[i + 1]) << (8 * i);
for (int j = 0; j < 4; ++j) {
result += static_cast<uint8_t>(recv_string[j + 1]) << (8 * j);
}
if (result > 0) {
ESP_LOGD(TAG, "Nextion reported new range %d", result);
@ -125,18 +137,22 @@ int Nextion::upload_by_chunks_(HTTPClient *http, int range_start) {
}
recv_string.clear();
}
return range_end + 1;
}
void Nextion::upload_tft() {
bool Nextion::upload_tft() {
ESP_LOGD(TAG, "Nextion TFT upload requested");
ESP_LOGD(TAG, "URL: %s", this->tft_url_.c_str());
if (this->is_updating_) {
ESP_LOGD(TAG, "Currently updating");
return;
return false;
}
if (!network::is_connected()) {
ESP_LOGD(TAG, "network is not connected");
return;
return false;
}
this->is_updating_ = true;
@ -161,10 +177,10 @@ void Nextion::upload_tft() {
if (!begin_status) {
this->is_updating_ = false;
ESP_LOGD(TAG, "connection failed");
ESP_LOGD(TAG, "Connection failed");
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
allocator.deallocate(this->transfer_buffer_, this->transfer_buffer_size_);
return;
return false;
} else {
ESP_LOGD(TAG, "Connected");
}
@ -192,7 +208,7 @@ void Nextion::upload_tft() {
}
if ((code != 200 && code != 206) || tries > 5) {
this->upload_end_();
return this->upload_end_(false);
}
String content_range_string = http.header("Content-Range");
@ -203,7 +219,7 @@ void Nextion::upload_tft() {
if (this->content_length_ < 4096) {
ESP_LOGE(TAG, "Failed to get file size");
this->upload_end_();
return this->upload_end_(false);
}
ESP_LOGD(TAG, "Updating Nextion %s...", this->device_model_.c_str());
@ -236,7 +252,9 @@ void Nextion::upload_tft() {
this->recv_ret_string_(response, 2000, true); // This can take some time to return
// The Nextion display will, if it's ready to accept data, send a 0x05 byte.
ESP_LOGD(TAG, "Upgrade response is %s %zu", response.c_str(), response.length());
ESP_LOGD(TAG, "Upgrade response is [%s] - %zu bytes",
format_hex_pretty(reinterpret_cast<const uint8_t *>(response.data()), response.size()).c_str(),
response.length());
for (size_t i = 0; i < response.length(); i++) {
ESP_LOGD(TAG, "Available %d : 0x%02X", i, response[i]);
@ -246,7 +264,7 @@ void Nextion::upload_tft() {
ESP_LOGD(TAG, "preparation for tft update done");
} else {
ESP_LOGD(TAG, "preparation for tft update failed %d \"%s\"", response[0], response.c_str());
this->upload_end_();
return this->upload_end_(false);
}
// Nextion wants 4096 bytes at a time. Make chunk_size a multiple of 4096
@ -255,17 +273,17 @@ void Nextion::upload_tft() {
if (heap_caps_get_free_size(MALLOC_CAP_SPIRAM) > 0) {
chunk_size = this->content_length_;
} else {
if (ESP.getFreeHeap() > 40960) { // 32K to keep on hand
int chunk = int((ESP.getFreeHeap() - 32768) / 4096);
chunk_size = chunk * 4096;
if (ESP.getFreeHeap() > 81920) { // Ensure some FreeHeap to other things and limit chunk size
chunk_size = ESP.getFreeHeap() - 65536;
chunk_size = int(chunk_size / 4096) * 4096;
chunk_size = chunk_size > 65536 ? 65536 : chunk_size;
} else if (ESP.getFreeHeap() < 10240) {
} else if (ESP.getFreeHeap() < 32768) {
chunk_size = 4096;
}
}
#else
// NOLINTNEXTLINE(readability-static-accessed-through-instance)
uint32_t chunk_size = ESP.getFreeHeap() < 10240 ? 4096 : 8192;
uint32_t chunk_size = ESP.getFreeHeap() < 16384 ? 4096 : 8192;
#endif
if (this->transfer_buffer_ == nullptr) {
@ -280,7 +298,7 @@ void Nextion::upload_tft() {
this->transfer_buffer_ = allocator.allocate(chunk_size);
if (!this->transfer_buffer_)
this->upload_end_();
return this->upload_end_(false);
}
this->transfer_buffer_size_ = chunk_size;
@ -295,7 +313,7 @@ void Nextion::upload_tft() {
result = this->upload_by_chunks_(&http, result);
if (result < 0) {
ESP_LOGD(TAG, "Error updating Nextion!");
this->upload_end_();
return this->upload_end_(false);
}
App.feed_wdt();
// NOLINTNEXTLINE(readability-static-accessed-through-instance)
@ -303,15 +321,19 @@ void Nextion::upload_tft() {
}
ESP_LOGD(TAG, "Successfully updated Nextion!");
this->upload_end_();
return this->upload_end_(true);
}
void Nextion::upload_end_() {
bool Nextion::upload_end_(bool successful) {
this->is_updating_ = false;
ESP_LOGD(TAG, "Restarting Nextion");
this->soft_reset();
delay(1500); // NOLINT
ESP_LOGD(TAG, "Restarting esphome");
ESP.restart(); // NOLINT(readability-static-accessed-through-instance)
if (successful) {
delay(1500); // NOLINT
ESP_LOGD(TAG, "Restarting esphome");
ESP.restart(); // NOLINT(readability-static-accessed-through-instance)
}
return successful;
}
#ifdef USE_ESP8266
@ -337,3 +359,4 @@ WiFiClient *Nextion::get_wifi_client_() {
} // namespace esphome
#endif // USE_NEXTION_TFT_UPLOAD
#endif // ARDUINO

View File

@ -0,0 +1,269 @@
#include "nextion.h"
#ifdef USE_ESP_IDF
#ifdef USE_NEXTION_TFT_UPLOAD
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#include "esphome/core/util.h"
#include "esphome/core/log.h"
#include "esphome/components/network/util.h"
#include <esp_heap_caps.h>
#include <esp_http_client.h>
#include <cinttypes>
namespace esphome {
namespace nextion {
static const char *const TAG = "nextion.upload.idf";
// Followed guide
// https://unofficialnextion.com/t/nextion-upload-protocol-v1-2-the-fast-one/1044/2
int Nextion::upload_range(const std::string &url, int range_start) {
ESP_LOGVV(TAG, "url: %s", url.c_str());
uint range_size = this->tft_size_ - range_start;
ESP_LOGVV(TAG, "tft_size_: %i", this->tft_size_);
ESP_LOGV(TAG, "Available heap: %u", esp_get_free_heap_size());
int range_end = (range_start == 0) ? std::min(this->tft_size_, 16383) : this->tft_size_;
if (range_size <= 0 or range_end <= range_start) {
ESP_LOGE(TAG, "Invalid range");
ESP_LOGD(TAG, "Range start: %i", range_start);
ESP_LOGD(TAG, "Range end: %i", range_end);
ESP_LOGD(TAG, "Range size: %i", range_size);
return -1;
}
esp_http_client_config_t config = {
.url = url.c_str(),
.cert_pem = nullptr,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
char range_header[64];
sprintf(range_header, "bytes=%d-%d", range_start, range_end);
ESP_LOGV(TAG, "Requesting range: %s", range_header);
esp_http_client_set_header(client, "Range", range_header);
ESP_LOGVV(TAG, "Available heap: %u", esp_get_free_heap_size());
ESP_LOGV(TAG, "Opening http connetion");
esp_err_t err;
if ((err = esp_http_client_open(client, 0)) != ESP_OK) {
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
esp_http_client_cleanup(client);
return -1;
}
ESP_LOGV(TAG, "Fetch content length");
int content_length = esp_http_client_fetch_headers(client);
ESP_LOGV(TAG, "content_length = %d", content_length);
if (content_length <= 0) {
ESP_LOGE(TAG, "Failed to get content length: %d", content_length);
esp_http_client_cleanup(client);
return -1;
}
int total_read_len = 0, read_len;
ESP_LOGV(TAG, "Allocate buffer");
uint8_t *buffer = new uint8_t[4096];
std::string recv_string;
if (buffer == nullptr) {
ESP_LOGE(TAG, "Failed to allocate memory for buffer");
ESP_LOGV(TAG, "Available heap: %u", esp_get_free_heap_size());
} else {
ESP_LOGV(TAG, "Memory for buffer allocated successfully");
while (true) {
App.feed_wdt();
ESP_LOGVV(TAG, "Available heap: %u", esp_get_free_heap_size());
int read_len = esp_http_client_read(client, reinterpret_cast<char *>(buffer), 4096);
ESP_LOGVV(TAG, "Read %d bytes from HTTP client, writing to UART", read_len);
if (read_len > 0) {
this->write_array(buffer, read_len);
ESP_LOGVV(TAG, "Write to UART successful");
this->recv_ret_string_(recv_string, 5000, true);
this->content_length_ -= read_len;
ESP_LOGD(TAG, "Uploaded %0.2f %%, remaining %d bytes",
100.0 * (this->tft_size_ - this->content_length_) / this->tft_size_, this->content_length_);
if (recv_string[0] != 0x05) { // 0x05 == "ok"
ESP_LOGD(
TAG, "recv_string [%s]",
format_hex_pretty(reinterpret_cast<const uint8_t *>(recv_string.data()), recv_string.size()).c_str());
}
// handle partial upload request
if (recv_string[0] == 0x08 && recv_string.size() == 5) {
uint32_t result = 0;
for (int j = 0; j < 4; ++j) {
result += static_cast<uint8_t>(recv_string[j + 1]) << (8 * j);
}
if (result > 0) {
ESP_LOGI(TAG, "Nextion reported new range %" PRIu32, result);
this->content_length_ = this->tft_size_ - result;
// Deallocate the buffer when done
delete[] buffer;
ESP_LOGVV(TAG, "Memory for buffer deallocated");
esp_http_client_cleanup(client);
esp_http_client_close(client);
return result;
}
}
recv_string.clear();
} else if (read_len == 0) {
ESP_LOGV(TAG, "End of HTTP response reached");
break; // Exit the loop if there is no more data to read
} else {
ESP_LOGE(TAG, "Failed to read from HTTP client, error code: %d", read_len);
break; // Exit the loop on error
}
}
// Deallocate the buffer when done
delete[] buffer;
ESP_LOGVV(TAG, "Memory for buffer deallocated");
}
esp_http_client_cleanup(client);
esp_http_client_close(client);
return range_end + 1;
}
bool Nextion::upload_tft() {
ESP_LOGD(TAG, "Nextion TFT upload requested");
ESP_LOGD(TAG, "url: %s", this->tft_url_.c_str());
if (this->is_updating_) {
ESP_LOGW(TAG, "Currently updating");
return false;
}
if (!network::is_connected()) {
ESP_LOGE(TAG, "Network is not connected");
return false;
}
this->is_updating_ = true;
// Define the configuration for the HTTP client
ESP_LOGV(TAG, "Establishing connection to HTTP server");
ESP_LOGVV(TAG, "Available heap: %u", esp_get_free_heap_size());
esp_http_client_config_t config = {
.url = this->tft_url_.c_str(),
.cert_pem = nullptr,
.method = HTTP_METHOD_HEAD,
.timeout_ms = 15000,
};
// Initialize the HTTP client with the configuration
ESP_LOGV(TAG, "Initializing HTTP client");
ESP_LOGV(TAG, "Available heap: %u", esp_get_free_heap_size());
esp_http_client_handle_t http = esp_http_client_init(&config);
if (!http) {
ESP_LOGE(TAG, "Failed to initialize HTTP client.");
return this->upload_end(false);
}
// Perform the HTTP request
ESP_LOGV(TAG, "Check if the client could connect");
ESP_LOGV(TAG, "Available heap: %u", esp_get_free_heap_size());
esp_err_t err = esp_http_client_perform(http);
if (err != ESP_OK) {
ESP_LOGE(TAG, "HTTP request failed: %s", esp_err_to_name(err));
esp_http_client_cleanup(http);
return this->upload_end(false);
}
// Check the HTTP Status Code
int status_code = esp_http_client_get_status_code(http);
ESP_LOGV(TAG, "HTTP Status Code: %d", status_code);
size_t tft_file_size = esp_http_client_get_content_length(http);
ESP_LOGD(TAG, "TFT file size: %zu", tft_file_size);
if (tft_file_size < 4096) {
ESP_LOGE(TAG, "File size check failed. Size: %zu", tft_file_size);
esp_http_client_cleanup(http);
return this->upload_end(false);
} else {
ESP_LOGV(TAG, "File size check passed. Proceeding...");
}
this->content_length_ = tft_file_size;
this->tft_size_ = tft_file_size;
ESP_LOGD(TAG, "Updating Nextion");
// The Nextion will ignore the update command if it is sleeping
this->send_command_("sleep=0");
this->set_backlight_brightness(1.0);
vTaskDelay(pdMS_TO_TICKS(250)); // NOLINT
App.feed_wdt();
char command[128];
// Tells the Nextion the content length of the tft file and baud rate it will be sent at
// Once the Nextion accepts the command it will wait until the file is successfully uploaded
// If it fails for any reason a power cycle of the display will be needed
sprintf(command, "whmi-wris %d,%" PRIu32 ",1", this->content_length_, this->parent_->get_baud_rate());
// Clear serial receive buffer
uint8_t d;
while (this->available()) {
this->read_byte(&d);
};
this->send_command_(command);
std::string response;
ESP_LOGV(TAG, "Waiting for upgrade response");
this->recv_ret_string_(response, 2048, true); // This can take some time to return
// The Nextion display will, if it's ready to accept data, send a 0x05 byte.
ESP_LOGD(TAG, "Upgrade response is [%s]",
format_hex_pretty(reinterpret_cast<const uint8_t *>(response.data()), response.size()).c_str());
if (response.find(0x05) != std::string::npos) {
ESP_LOGV(TAG, "Preparation for tft update done");
} else {
ESP_LOGE(TAG, "Preparation for tft update failed %d \"%s\"", response[0], response.c_str());
esp_http_client_cleanup(http);
return this->upload_end(false);
}
ESP_LOGD(TAG, "Updating tft from \"%s\" with a file size of %d, Heap Size %" PRIu32, this->tft_url_.c_str(),
content_length_, esp_get_free_heap_size());
ESP_LOGV(TAG, "Starting transfer by chunks loop");
int result = 0;
while (content_length_ > 0) {
result = upload_range(this->tft_url_.c_str(), result);
if (result < 0) {
ESP_LOGE(TAG, "Error updating Nextion!");
esp_http_client_cleanup(http);
return this->upload_end(false);
}
App.feed_wdt();
ESP_LOGV(TAG, "Heap Size %" PRIu32 ", Bytes left %d", esp_get_free_heap_size(), content_length_);
}
ESP_LOGD(TAG, "Successfully updated Nextion!");
ESP_LOGD(TAG, "Close HTTP connection");
esp_http_client_close(http);
esp_http_client_cleanup(http);
return upload_end(true);
}
bool Nextion::upload_end(bool successful) {
this->is_updating_ = false;
ESP_LOGD(TAG, "Restarting Nextion");
this->soft_reset();
vTaskDelay(pdMS_TO_TICKS(1500)); // NOLINT
if (successful) {
ESP_LOGD(TAG, "Restarting esphome");
esp_restart(); // NOLINT(readability-static-accessed-through-instance)
}
return successful;
}
} // namespace nextion
} // namespace esphome
#endif // USE_NEXTION_TFT_UPLOAD
#endif // USE_ESP_IDF

View File

@ -0,0 +1,144 @@
#pragma once
#include "esphome/core/helpers.h"
#include <vector>
namespace esphome {
namespace nfc {
// Header info
static const uint8_t NCI_PKT_HEADER_SIZE = 3; // NCI packet (pkt) headers are always three bytes
static const uint8_t NCI_PKT_MT_GID_OFFSET = 0; // NCI packet (pkt) MT and GID offsets
static const uint8_t NCI_PKT_OID_OFFSET = 1; // NCI packet (pkt) OID offset
static const uint8_t NCI_PKT_LENGTH_OFFSET = 2; // NCI packet (pkt) message length (size) offset
static const uint8_t NCI_PKT_PAYLOAD_OFFSET = 3; // NCI packet (pkt) payload offset
// Important masks
static const uint8_t NCI_PKT_MT_MASK = 0xE0; // NCI packet (pkt) message type mask
static const uint8_t NCI_PKT_PBF_MASK = 0x10; // packet boundary flag bit
static const uint8_t NCI_PKT_GID_MASK = 0x0F;
static const uint8_t NCI_PKT_OID_MASK = 0x3F;
// Message types
static const uint8_t NCI_PKT_MT_DATA = 0x00; // For sending commands to NFC endpoint (card/tag)
static const uint8_t NCI_PKT_MT_CTRL_COMMAND = 0x20; // For sending commands to NFCC
static const uint8_t NCI_PKT_MT_CTRL_RESPONSE = 0x40; // Response from NFCC to commands
static const uint8_t NCI_PKT_MT_CTRL_NOTIFICATION = 0x60; // Notification from NFCC
// GIDs
static const uint8_t NCI_CORE_GID = 0x0;
static const uint8_t RF_GID = 0x1;
static const uint8_t NFCEE_GID = 0x1;
static const uint8_t NCI_PROPRIETARY_GID = 0xF;
// OIDs
static const uint8_t NCI_CORE_RESET_OID = 0x00;
static const uint8_t NCI_CORE_INIT_OID = 0x01;
static const uint8_t NCI_CORE_SET_CONFIG_OID = 0x02;
static const uint8_t NCI_CORE_GET_CONFIG_OID = 0x03;
static const uint8_t NCI_CORE_CONN_CREATE_OID = 0x04;
static const uint8_t NCI_CORE_CONN_CLOSE_OID = 0x05;
static const uint8_t NCI_CORE_CONN_CREDITS_OID = 0x06;
static const uint8_t NCI_CORE_GENERIC_ERROR_OID = 0x07;
static const uint8_t NCI_CORE_INTERFACE_ERROR_OID = 0x08;
static const uint8_t RF_DISCOVER_MAP_OID = 0x00;
static const uint8_t RF_SET_LISTEN_MODE_ROUTING_OID = 0x01;
static const uint8_t RF_GET_LISTEN_MODE_ROUTING_OID = 0x02;
static const uint8_t RF_DISCOVER_OID = 0x03;
static const uint8_t RF_DISCOVER_SELECT_OID = 0x04;
static const uint8_t RF_INTF_ACTIVATED_OID = 0x05;
static const uint8_t RF_DEACTIVATE_OID = 0x06;
static const uint8_t RF_FIELD_INFO_OID = 0x07;
static const uint8_t RF_T3T_POLLING_OID = 0x08;
static const uint8_t RF_NFCEE_ACTION_OID = 0x09;
static const uint8_t RF_NFCEE_DISCOVERY_REQ_OID = 0x0A;
static const uint8_t RF_PARAMETER_UPDATE_OID = 0x0B;
static const uint8_t NFCEE_DISCOVER_OID = 0x00;
static const uint8_t NFCEE_MODE_SET_OID = 0x01;
// Interfaces
static const uint8_t INTF_NFCEE_DIRECT = 0x00;
static const uint8_t INTF_FRAME = 0x01;
static const uint8_t INTF_ISODEP = 0x02;
static const uint8_t INTF_NFCDEP = 0x03;
static const uint8_t INTF_TAGCMD = 0x80; // NXP proprietary
// Bit rates
static const uint8_t NFC_BIT_RATE_106 = 0x00;
static const uint8_t NFC_BIT_RATE_212 = 0x01;
static const uint8_t NFC_BIT_RATE_424 = 0x02;
static const uint8_t NFC_BIT_RATE_848 = 0x03;
static const uint8_t NFC_BIT_RATE_1695 = 0x04;
static const uint8_t NFC_BIT_RATE_3390 = 0x05;
static const uint8_t NFC_BIT_RATE_6780 = 0x06;
// Protocols
static const uint8_t PROT_UNDETERMINED = 0x00;
static const uint8_t PROT_T1T = 0x01;
static const uint8_t PROT_T2T = 0x02;
static const uint8_t PROT_T3T = 0x03;
static const uint8_t PROT_ISODEP = 0x04;
static const uint8_t PROT_NFCDEP = 0x05;
static const uint8_t PROT_T5T = 0x06;
static const uint8_t PROT_MIFARE = 0x80;
// RF Technologies
static const uint8_t NFC_RF_TECH_A = 0x00;
static const uint8_t NFC_RF_TECH_B = 0x01;
static const uint8_t NFC_RF_TECH_F = 0x02;
static const uint8_t NFC_RF_TECH_15693 = 0x03;
// RF Technology & Modes
static const uint8_t MODE_MASK = 0xF0;
static const uint8_t MODE_LISTEN_MASK = 0x80;
static const uint8_t MODE_POLL = 0x00;
static const uint8_t TECH_PASSIVE_NFCA = 0x00;
static const uint8_t TECH_PASSIVE_NFCB = 0x01;
static const uint8_t TECH_PASSIVE_NFCF = 0x02;
static const uint8_t TECH_ACTIVE_NFCA = 0x03;
static const uint8_t TECH_ACTIVE_NFCF = 0x05;
static const uint8_t TECH_PASSIVE_15693 = 0x06;
// Status codes
static const uint8_t STATUS_OK = 0x00;
static const uint8_t STATUS_REJECTED = 0x01;
static const uint8_t STATUS_RF_FRAME_CORRUPTED = 0x02;
static const uint8_t STATUS_FAILED = 0x03;
static const uint8_t STATUS_NOT_INITIALIZED = 0x04;
static const uint8_t STATUS_SYNTAX_ERROR = 0x05;
static const uint8_t STATUS_SEMANTIC_ERROR = 0x06;
static const uint8_t STATUS_INVALID_PARAM = 0x09;
static const uint8_t STATUS_MESSAGE_SIZE_EXCEEDED = 0x0A;
static const uint8_t DISCOVERY_ALREADY_STARTED = 0xA0;
static const uint8_t DISCOVERY_TARGET_ACTIVATION_FAILED = 0xA1;
static const uint8_t DISCOVERY_TEAR_DOWN = 0xA2;
static const uint8_t RF_TRANSMISSION_ERROR = 0xB0;
static const uint8_t RF_PROTOCOL_ERROR = 0xB1;
static const uint8_t RF_TIMEOUT_ERROR = 0xB2;
static const uint8_t NFCEE_INTERFACE_ACTIVATION_FAILED = 0xC0;
static const uint8_t NFCEE_TRANSMISSION_ERROR = 0xC1;
static const uint8_t NFCEE_PROTOCOL_ERROR = 0xC2;
static const uint8_t NFCEE_TIMEOUT_ERROR = 0xC3;
// Deactivation types/reasons
static const uint8_t DEACTIVATION_TYPE_IDLE = 0x00;
static const uint8_t DEACTIVATION_TYPE_SLEEP = 0x01;
static const uint8_t DEACTIVATION_TYPE_SLEEP_AF = 0x02;
static const uint8_t DEACTIVATION_TYPE_DISCOVERY = 0x03;
// RF discover map modes
static const uint8_t RF_DISCOVER_MAP_MODE_POLL = 0x1;
static const uint8_t RF_DISCOVER_MAP_MODE_LISTEN = 0x2;
// RF discover notification types
static const uint8_t RF_DISCOVER_NTF_NT_LAST = 0x00;
static const uint8_t RF_DISCOVER_NTF_NT_LAST_RL = 0x01;
static const uint8_t RF_DISCOVER_NTF_NT_MORE = 0x02;
// Important message offsets
static const uint8_t RF_DISCOVER_NTF_DISCOVERY_ID = 0 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_DISCOVER_NTF_PROTOCOL = 1 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_DISCOVER_NTF_MODE_TECH = 2 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_DISCOVER_NTF_RF_TECH_LENGTH = 3 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_DISCOVER_NTF_RF_TECH_PARAMS = 4 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_INTF_ACTIVATED_NTF_DISCOVERY_ID = 0 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_INTF_ACTIVATED_NTF_INTERFACE = 1 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_INTF_ACTIVATED_NTF_PROTOCOL = 2 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_INTF_ACTIVATED_NTF_MODE_TECH = 3 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_INTF_ACTIVATED_NTF_MAX_SIZE = 4 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_INTF_ACTIVATED_NTF_INIT_CRED = 5 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_INTF_ACTIVATED_NTF_RF_TECH_LENGTH = 6 + NCI_PKT_HEADER_SIZE;
static const uint8_t RF_INTF_ACTIVATED_NTF_RF_TECH_PARAMS = 7 + NCI_PKT_HEADER_SIZE;
} // namespace nfc
} // namespace esphome

View File

@ -0,0 +1,166 @@
#include "nci_core.h"
#include "nci_message.h"
#include "esphome/core/log.h"
#include <cstdio>
namespace esphome {
namespace nfc {
static const char *const TAG = "NciMessage";
NciMessage::NciMessage(const uint8_t message_type, const std::vector<uint8_t> &payload) {
this->set_message(message_type, payload);
}
NciMessage::NciMessage(const uint8_t message_type, const uint8_t gid, const uint8_t oid) {
this->set_header(message_type, gid, oid);
}
NciMessage::NciMessage(const uint8_t message_type, const uint8_t gid, const uint8_t oid,
const std::vector<uint8_t> &payload) {
this->set_message(message_type, gid, oid, payload);
}
NciMessage::NciMessage(const std::vector<uint8_t> &raw_packet) { this->nci_message_ = raw_packet; };
std::vector<uint8_t> NciMessage::encode() {
this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = this->nci_message_.size() - nfc::NCI_PKT_HEADER_SIZE;
std::vector<uint8_t> message = this->nci_message_;
return message;
}
void NciMessage::reset() { this->nci_message_ = {0, 0, 0}; }
uint8_t NciMessage::get_message_type() const {
return this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_MT_MASK;
}
uint8_t NciMessage::get_gid() const { return this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_GID_MASK; }
uint8_t NciMessage::get_oid() const { return this->nci_message_[nfc::NCI_PKT_OID_OFFSET] & nfc::NCI_PKT_OID_MASK; }
uint8_t NciMessage::get_payload_size(const bool recompute) {
if (!this->nci_message_.empty()) {
if (recompute) {
this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = this->nci_message_.size() - nfc::NCI_PKT_HEADER_SIZE;
}
return this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET];
}
return 0;
}
uint8_t NciMessage::get_simple_status_response() const {
if (this->nci_message_.size() > nfc::NCI_PKT_PAYLOAD_OFFSET) {
return this->nci_message_[nfc::NCI_PKT_PAYLOAD_OFFSET];
}
return STATUS_FAILED;
}
uint8_t NciMessage::get_message_byte(const uint8_t offset) const {
if (this->nci_message_.size() > offset) {
return this->nci_message_[offset];
}
return 0;
}
std::vector<uint8_t> &NciMessage::get_message() { return this->nci_message_; }
bool NciMessage::has_payload() const { return this->nci_message_.size() > nfc::NCI_PKT_HEADER_SIZE; }
bool NciMessage::message_type_is(const uint8_t message_type) const {
if (!this->nci_message_.empty()) {
return message_type == (this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_MT_MASK);
}
return false;
}
bool NciMessage::message_length_is(const uint8_t message_length, const bool recompute) {
if (this->nci_message_.size() > nfc::NCI_PKT_LENGTH_OFFSET) {
if (recompute) {
this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = this->nci_message_.size() - nfc::NCI_PKT_HEADER_SIZE;
}
return message_length == this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET];
}
return false;
}
bool NciMessage::gid_is(const uint8_t gid) const {
if (this->nci_message_.size() > nfc::NCI_PKT_MT_GID_OFFSET) {
return gid == (this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_GID_MASK);
}
return false;
}
bool NciMessage::oid_is(const uint8_t oid) const {
if (this->nci_message_.size() > nfc::NCI_PKT_OID_OFFSET) {
return oid == (this->nci_message_[nfc::NCI_PKT_OID_OFFSET] & nfc::NCI_PKT_OID_MASK);
}
return false;
}
bool NciMessage::simple_status_response_is(const uint8_t response) const {
if (this->nci_message_.size() > nfc::NCI_PKT_PAYLOAD_OFFSET) {
return response == this->nci_message_[nfc::NCI_PKT_PAYLOAD_OFFSET];
}
return false;
}
void NciMessage::set_header(const uint8_t message_type, const uint8_t gid, const uint8_t oid) {
if (this->nci_message_.size() < nfc::NCI_PKT_HEADER_SIZE) {
this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
}
this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] =
(message_type & nfc::NCI_PKT_MT_MASK) | (gid & nfc::NCI_PKT_GID_MASK);
this->nci_message_[nfc::NCI_PKT_OID_OFFSET] = oid & nfc::NCI_PKT_OID_MASK;
}
void NciMessage::set_message(const uint8_t message_type, const std::vector<uint8_t> &payload) {
this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = payload.size();
this->nci_message_.insert(this->nci_message_.end(), payload.begin(), payload.end());
}
void NciMessage::set_message(const uint8_t message_type, const uint8_t gid, const uint8_t oid,
const std::vector<uint8_t> &payload) {
this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] =
(message_type & nfc::NCI_PKT_MT_MASK) | (gid & nfc::NCI_PKT_GID_MASK);
this->nci_message_[nfc::NCI_PKT_OID_OFFSET] = oid & nfc::NCI_PKT_OID_MASK;
this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = payload.size();
this->nci_message_.insert(this->nci_message_.end(), payload.begin(), payload.end());
}
void NciMessage::set_message_type(const uint8_t message_type) {
if (this->nci_message_.size() < nfc::NCI_PKT_HEADER_SIZE) {
this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
}
auto mt_masked = this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & ~nfc::NCI_PKT_MT_MASK;
this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] = mt_masked | (message_type & nfc::NCI_PKT_MT_MASK);
}
void NciMessage::set_gid(const uint8_t gid) {
if (this->nci_message_.size() < nfc::NCI_PKT_HEADER_SIZE) {
this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
}
auto gid_masked = this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & ~nfc::NCI_PKT_GID_MASK;
this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] = gid_masked | (gid & nfc::NCI_PKT_GID_MASK);
}
void NciMessage::set_oid(const uint8_t oid) {
if (this->nci_message_.size() < nfc::NCI_PKT_HEADER_SIZE) {
this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
}
this->nci_message_[nfc::NCI_PKT_OID_OFFSET] = oid & nfc::NCI_PKT_OID_MASK;
}
void NciMessage::set_payload(const std::vector<uint8_t> &payload) {
std::vector<uint8_t> message(this->nci_message_.begin(), this->nci_message_.begin() + nfc::NCI_PKT_HEADER_SIZE);
message.insert(message.end(), payload.begin(), payload.end());
message[nfc::NCI_PKT_LENGTH_OFFSET] = payload.size();
this->nci_message_ = message;
}
} // namespace nfc
} // namespace esphome

View File

@ -0,0 +1,50 @@
#pragma once
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include <vector>
namespace esphome {
namespace nfc {
class NciMessage {
public:
NciMessage() {}
NciMessage(uint8_t message_type, const std::vector<uint8_t> &payload);
NciMessage(uint8_t message_type, uint8_t gid, uint8_t oid);
NciMessage(uint8_t message_type, uint8_t gid, uint8_t oid, const std::vector<uint8_t> &payload);
NciMessage(const std::vector<uint8_t> &raw_packet);
std::vector<uint8_t> encode();
void reset();
uint8_t get_message_type() const;
uint8_t get_gid() const;
uint8_t get_oid() const;
uint8_t get_payload_size(bool recompute = false);
uint8_t get_simple_status_response() const;
uint8_t get_message_byte(uint8_t offset) const;
std::vector<uint8_t> &get_message();
bool has_payload() const;
bool message_type_is(uint8_t message_type) const;
bool message_length_is(uint8_t message_length, bool recompute = false);
bool gid_is(uint8_t gid) const;
bool oid_is(uint8_t oid) const;
bool simple_status_response_is(uint8_t response) const;
void set_header(uint8_t message_type, uint8_t gid, uint8_t oid);
void set_message(uint8_t message_type, const std::vector<uint8_t> &payload);
void set_message(uint8_t message_type, uint8_t gid, uint8_t oid, const std::vector<uint8_t> &payload);
void set_message_type(uint8_t message_type);
void set_gid(uint8_t gid);
void set_oid(uint8_t oid);
void set_payload(const std::vector<uint8_t> &payload);
protected:
std::vector<uint8_t> nci_message_{0, 0, 0}; // three bytes, MT/PBF/GID, OID, payload length/size
};
} // namespace nfc
} // namespace esphome

View File

@ -53,7 +53,7 @@ uint8_t get_mifare_classic_ndef_start_index(std::vector<uint8_t> &data) {
}
bool decode_mifare_classic_tlv(std::vector<uint8_t> &data, uint32_t &message_length, uint8_t &message_start_index) {
uint8_t i = get_mifare_classic_ndef_start_index(data);
auto i = get_mifare_classic_ndef_start_index(data);
if (data[i] != 0x03) {
ESP_LOGE(TAG, "Error, Can't decode message length.");
return false;

View File

@ -0,0 +1,47 @@
#include "nfc_helpers.h"
namespace esphome {
namespace nfc {
static const char *const TAG = "nfc.helpers";
bool has_ha_tag_ndef(NfcTag &tag) { return !get_ha_tag_ndef(tag).empty(); }
std::string get_ha_tag_ndef(NfcTag &tag) {
if (!tag.has_ndef_message()) {
return std::string();
}
auto message = tag.get_ndef_message();
auto records = message->get_records();
for (const auto &record : records) {
std::string payload = record->get_payload();
size_t pos = payload.find(HA_TAG_ID_PREFIX);
if (pos != std::string::npos) {
return payload.substr(pos + sizeof(HA_TAG_ID_PREFIX) - 1);
}
}
return std::string();
}
std::string get_random_ha_tag_ndef() {
static const char ALPHANUM[] = "0123456789abcdef";
std::string uri = HA_TAG_ID_PREFIX;
for (int i = 0; i < 8; i++) {
uri += ALPHANUM[random_uint32() % (sizeof(ALPHANUM) - 1)];
}
uri += "-";
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 4; i++) {
uri += ALPHANUM[random_uint32() % (sizeof(ALPHANUM) - 1)];
}
uri += "-";
}
for (int i = 0; i < 12; i++) {
uri += ALPHANUM[random_uint32() % (sizeof(ALPHANUM) - 1)];
}
ESP_LOGD("pn7160", "Payload to be written: %s", uri.c_str());
return uri;
}
} // namespace nfc
} // namespace esphome

View File

@ -0,0 +1,17 @@
#pragma once
#include "nfc_tag.h"
namespace esphome {
namespace nfc {
static const char HA_TAG_ID_EXT_RECORD_TYPE[] = "android.com:pkg";
static const char HA_TAG_ID_EXT_RECORD_PAYLOAD[] = "io.homeassistant.companion.android";
static const char HA_TAG_ID_PREFIX[] = "https://www.home-assistant.io/tag/";
std::string get_ha_tag_ndef(NfcTag &tag);
std::string get_random_ha_tag_ndef();
bool has_ha_tag_ndef(NfcTag &tag);
} // namespace nfc
} // namespace esphome

View File

@ -257,8 +257,8 @@ async def register_number(
)
async def new_number(config, *, min_value: float, max_value: float, step: float):
var = cg.new_Pvariable(config[CONF_ID])
async def new_number(config, *args, min_value: float, max_value: float, step: float):
var = cg.new_Pvariable(config[CONF_ID], *args)
await register_number(
var, config, min_value=min_value, max_value=max_value, step=step
)

View File

@ -52,20 +52,15 @@ def validate_mode(value):
return value
PCA9554_PIN_SCHEMA = cv.All(
PCA9554_PIN_SCHEMA = pins.gpio_base_schema(
PCA9554GPIOPin,
cv.int_range(min=0, max=15),
modes=[CONF_INPUT, CONF_OUTPUT],
mode_validator=validate_mode,
).extend(
{
cv.GenerateID(): cv.declare_id(PCA9554GPIOPin),
cv.Required(CONF_PCA9554): cv.use_id(PCA9554Component),
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=15),
cv.Optional(CONF_MODE, default={}): cv.All(
{
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
},
validate_mode,
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
},
}
)

View File

@ -39,7 +39,6 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await display.register_display(var, config)
await spi.register_spi_device(var, config)

View File

@ -7,8 +7,7 @@
namespace esphome {
namespace pcd8544 {
class PCD8544 : public PollingComponent,
public display::DisplayBuffer,
class PCD8544 : public display::DisplayBuffer,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH, spi::CLOCK_PHASE_TRAILING,
spi::DATA_RATE_8MHZ> {
public:

View File

@ -48,19 +48,15 @@ def validate_mode(value):
return value
PCF8574_PIN_SCHEMA = cv.All(
PCF8574_PIN_SCHEMA = pins.gpio_base_schema(
PCF8574GPIOPin,
cv.int_range(min=0, max=17),
modes=[CONF_INPUT, CONF_OUTPUT],
mode_validator=validate_mode,
invertable=True,
).extend(
{
cv.GenerateID(): cv.declare_id(PCF8574GPIOPin),
cv.Required(CONF_PCF8574): cv.use_id(PCF8574Component),
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=17),
cv.Optional(CONF_MODE, default={}): cv.All(
{
cv.Optional(CONF_INPUT, default=False): cv.boolean,
cv.Optional(CONF_OUTPUT, default=False): cv.boolean,
},
validate_mode,
),
cv.Optional(CONF_INVERTED, default=False): cv.boolean,
}
)

View File

@ -2,7 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import climate, sensor, output
from esphome.const import CONF_ID, CONF_SENSOR
from esphome.const import CONF_HUMIDITY_SENSOR, CONF_ID, CONF_SENSOR
pid_ns = cg.esphome_ns.namespace("pid")
PIDClimate = pid_ns.class_("PIDClimate", climate.Climate, cg.Component)
@ -45,6 +45,7 @@ CONFIG_SCHEMA = cv.All(
{
cv.GenerateID(): cv.declare_id(PIDClimate),
cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
cv.Optional(CONF_HUMIDITY_SENSOR): cv.use_id(sensor.Sensor),
cv.Required(CONF_DEFAULT_TARGET_TEMPERATURE): cv.temperature,
cv.Optional(CONF_COOL_OUTPUT): cv.use_id(output.FloatOutput),
cv.Optional(CONF_HEAT_OUTPUT): cv.use_id(output.FloatOutput),
@ -86,6 +87,10 @@ async def to_code(config):
sens = await cg.get_variable(config[CONF_SENSOR])
cg.add(var.set_sensor(sens))
if CONF_HUMIDITY_SENSOR in config:
sens = await cg.get_variable(config[CONF_HUMIDITY_SENSOR])
cg.add(var.set_humidity_sensor(sens))
if CONF_COOL_OUTPUT in config:
out = await cg.get_variable(config[CONF_COOL_OUTPUT])
cg.add(var.set_cool_output(out))

View File

@ -14,6 +14,16 @@ void PIDClimate::setup() {
this->update_pid_();
});
this->current_temperature = this->sensor_->state;
// register for humidity values and get initial state
if (this->humidity_sensor_ != nullptr) {
this->humidity_sensor_->add_on_state_callback([this](float state) {
this->current_humidity = state;
this->publish_state();
});
this->current_humidity = this->humidity_sensor_->state;
}
// restore set points
auto restore = this->restore_state_();
if (restore.has_value()) {
@ -47,6 +57,9 @@ climate::ClimateTraits PIDClimate::traits() {
traits.set_supports_current_temperature(true);
traits.set_supports_two_point_target_temperature(false);
if (this->humidity_sensor_ != nullptr)
traits.set_supports_current_humidity(true);
traits.set_supported_modes({climate::CLIMATE_MODE_OFF});
if (supports_cool_())
traits.add_supported_mode(climate::CLIMATE_MODE_COOL);

View File

@ -19,6 +19,7 @@ class PIDClimate : public climate::Climate, public Component {
void dump_config() override;
void set_sensor(sensor::Sensor *sensor) { sensor_ = sensor; }
void set_humidity_sensor(sensor::Sensor *sensor) { humidity_sensor_ = sensor; }
void set_cool_output(output::FloatOutput *cool_output) { cool_output_ = cool_output; }
void set_heat_output(output::FloatOutput *heat_output) { heat_output_ = heat_output; }
void set_kp(float kp) { controller_.kp_ = kp; }
@ -85,6 +86,8 @@ class PIDClimate : public climate::Climate, public Component {
/// The sensor used for getting the current temperature
sensor::Sensor *sensor_;
/// The sensor used for getting the current humidity
sensor::Sensor *humidity_sensor_{nullptr};
output::FloatOutput *cool_output_{nullptr};
output::FloatOutput *heat_output_{nullptr};
PIDController controller_;

View File

@ -16,7 +16,7 @@ float PIDController::update(float setpoint, float process_value) {
calculate_proportional_term_();
calculate_integral_term_();
calculate_derivative_term_();
calculate_derivative_term_(setpoint);
// u(t) := p(t) + i(t) + d(t)
float output = proportional_term_ + integral_term_ + derivative_term_;
@ -69,13 +69,18 @@ void PIDController::calculate_integral_term_() {
integral_term_ = accumulated_integral_;
}
void PIDController::calculate_derivative_term_() {
void PIDController::calculate_derivative_term_(float setpoint) {
// derivative_term_
// d(t) := K_d * de(t)/dt
float derivative = 0.0f;
if (dt_ != 0.0f)
if (dt_ != 0.0f) {
// remove changes to setpoint from error
if (!std::isnan(previous_setpoint_) && previous_setpoint_ != setpoint)
previous_error_ -= previous_setpoint_ - setpoint;
derivative = (error_ - previous_error_) / dt_;
}
previous_error_ = error_;
previous_setpoint_ = setpoint;
// smooth the derivative samples
derivative = weighted_average_(derivative_list_, derivative, derivative_samples_);

View File

@ -49,12 +49,13 @@ struct PIDController {
void calculate_proportional_term_();
void calculate_integral_term_();
void calculate_derivative_term_();
void calculate_derivative_term_(float setpoint);
float weighted_average_(std::deque<float> &list, float new_value, int samples);
float calculate_relative_time_();
/// Error from previous update used for derivative term
float previous_error_ = 0;
float previous_setpoint_ = NAN;
/// Accumulated integral value
float accumulated_integral_ = 0;
uint32_t last_time_ = 0;

View File

@ -127,8 +127,18 @@ void PN532::loop() {
if (!this->requested_read_)
return;
auto ready = this->read_ready_(false);
if (ready == WOULDBLOCK)
return;
bool success = false;
std::vector<uint8_t> read;
bool success = this->read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
if (ready == READY) {
success = this->read_response(PN532_COMMAND_INLISTPASSIVETARGET, read);
} else {
this->send_ack_(); // abort still running InListPassiveTarget
}
this->requested_read_ = false;
@ -286,12 +296,58 @@ bool PN532::read_ack_() {
return matches;
}
void PN532::send_ack_() {
ESP_LOGV(TAG, "Sending ACK for abort");
this->write_data({0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00});
delay(10);
}
void PN532::send_nack_() {
ESP_LOGV(TAG, "Sending NACK for retransmit");
this->write_data({0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
delay(10);
}
enum PN532ReadReady PN532::read_ready_(bool block) {
if (this->rd_ready_ == READY) {
if (block) {
this->rd_start_time_ = 0;
this->rd_ready_ = WOULDBLOCK;
}
return READY;
}
if (!this->rd_start_time_) {
this->rd_start_time_ = millis();
}
while (true) {
if (this->is_read_ready()) {
this->rd_ready_ = READY;
break;
}
if (millis() - this->rd_start_time_ > 100) {
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
this->rd_ready_ = TIMEOUT;
break;
}
if (!block) {
this->rd_ready_ = WOULDBLOCK;
break;
}
yield();
}
auto rdy = this->rd_ready_;
if (block || rdy == TIMEOUT) {
this->rd_start_time_ = 0;
this->rd_ready_ = WOULDBLOCK;
}
return rdy;
}
void PN532::turn_off_rf_() {
ESP_LOGV(TAG, "Turning RF field OFF");
this->write_command_({

View File

@ -20,6 +20,12 @@ static const uint8_t PN532_COMMAND_INDATAEXCHANGE = 0x40;
static const uint8_t PN532_COMMAND_INLISTPASSIVETARGET = 0x4A;
static const uint8_t PN532_COMMAND_POWERDOWN = 0x16;
enum PN532ReadReady {
WOULDBLOCK = 0,
TIMEOUT,
READY,
};
class PN532BinarySensor;
class PN532 : public PollingComponent {
@ -54,8 +60,11 @@ class PN532 : public PollingComponent {
void turn_off_rf_();
bool write_command_(const std::vector<uint8_t> &data);
bool read_ack_();
void send_ack_();
void send_nack_();
enum PN532ReadReady read_ready_(bool block);
virtual bool is_read_ready() = 0;
virtual bool write_data(const std::vector<uint8_t> &data) = 0;
virtual bool read_data(std::vector<uint8_t> &data, uint8_t len) = 0;
virtual bool read_response(uint8_t command, std::vector<uint8_t> &data) = 0;
@ -91,6 +100,8 @@ class PN532 : public PollingComponent {
std::vector<nfc::NfcOnTagTrigger *> triggers_ontagremoved_;
std::vector<uint8_t> current_uid_;
nfc::NdefMessage *next_task_message_to_write_;
uint32_t rd_start_time_{0};
enum PN532ReadReady rd_ready_ { WOULDBLOCK };
enum NfcTask {
READ = 0,
CLEAN,

View File

@ -12,6 +12,14 @@ namespace pn532_i2c {
static const char *const TAG = "pn532_i2c";
bool PN532I2C::is_read_ready() {
uint8_t ready;
if (!this->read_bytes_raw(&ready, 1)) {
return false;
}
return ready == 0x01;
}
bool PN532I2C::write_data(const std::vector<uint8_t> &data) {
return this->write(data.data(), data.size()) == i2c::ERROR_OK;
}
@ -19,19 +27,8 @@ bool PN532I2C::write_data(const std::vector<uint8_t> &data) {
bool PN532I2C::read_data(std::vector<uint8_t> &data, uint8_t len) {
delay(1);
std::vector<uint8_t> ready;
ready.resize(1);
uint32_t start_time = millis();
while (true) {
if (this->read_bytes_raw(ready.data(), 1)) {
if (ready[0] == 0x01)
break;
}
if (millis() - start_time > 100) {
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
return false;
}
if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
return false;
}
data.resize(len + 1);

View File

@ -14,6 +14,7 @@ class PN532I2C : public pn532::PN532, public i2c::I2CDevice {
void dump_config() override;
protected:
bool is_read_ready() override;
bool write_data(const std::vector<uint8_t> &data) override;
bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
bool read_response(uint8_t command, std::vector<uint8_t> &data) override;

View File

@ -21,6 +21,14 @@ void PN532Spi::setup() {
PN532::setup();
}
bool PN532Spi::is_read_ready() {
this->enable();
this->write_byte(0x02);
bool ready = this->read_byte() == 0x01;
this->disable();
return ready;
}
bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
this->enable();
delay(2);
@ -34,24 +42,8 @@ bool PN532Spi::write_data(const std::vector<uint8_t> &data) {
}
bool PN532Spi::read_data(std::vector<uint8_t> &data, uint8_t len) {
ESP_LOGV(TAG, "Waiting for ready byte...");
uint32_t start_time = millis();
while (true) {
this->enable();
// First byte, communication mode: Read state
this->write_byte(0x02);
bool ready = this->read_byte() == 0x01;
this->disable();
if (ready)
break;
ESP_LOGV(TAG, "Not ready yet...");
if (millis() - start_time > 100) {
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
return false;
}
yield();
if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
return false;
}
// Read data (transmission from the PN532 to the host)
@ -72,22 +64,8 @@ bool PN532Spi::read_data(std::vector<uint8_t> &data, uint8_t len) {
bool PN532Spi::read_response(uint8_t command, std::vector<uint8_t> &data) {
ESP_LOGV(TAG, "Reading response");
uint32_t start_time = millis();
while (true) {
this->enable();
// First byte, communication mode: Read state
this->write_byte(0x02);
bool ready = this->read_byte() == 0x01;
this->disable();
if (ready)
break;
ESP_LOGV(TAG, "Not ready yet...");
if (millis() - start_time > 100) {
ESP_LOGV(TAG, "Timed out waiting for readiness from PN532!");
return false;
}
yield();
if (this->read_ready_(true) != pn532::PN532ReadReady::READY) {
return false;
}
this->enable();

View File

@ -18,6 +18,7 @@ class PN532Spi : public pn532::PN532,
void dump_config() override;
protected:
bool is_read_ready() override;
bool write_data(const std::vector<uint8_t> &data) override;
bool read_data(std::vector<uint8_t> &data, uint8_t len) override;
bool read_response(uint8_t command, std::vector<uint8_t> &data) override;

View File

@ -0,0 +1,215 @@
from esphome import automation, pins
from esphome.automation import maybe_simple_id
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import nfc
from esphome.const import (
CONF_ID,
CONF_IRQ_PIN,
CONF_ON_TAG_REMOVED,
CONF_ON_TAG,
CONF_TRIGGER_ID,
)
AUTO_LOAD = ["binary_sensor", "nfc"]
CODEOWNERS = ["@kbx81", "@jesserockz"]
CONF_EMULATION_MESSAGE = "emulation_message"
CONF_EMULATION_OFF = "emulation_off"
CONF_EMULATION_ON = "emulation_on"
CONF_INCLUDE_ANDROID_APP_RECORD = "include_android_app_record"
CONF_MESSAGE = "message"
CONF_ON_FINISHED_WRITE = "on_finished_write"
CONF_ON_EMULATED_TAG_SCAN = "on_emulated_tag_scan"
CONF_PN7150_ID = "pn7150_id"
CONF_POLLING_OFF = "polling_off"
CONF_POLLING_ON = "polling_on"
CONF_SET_CLEAN_MODE = "set_clean_mode"
CONF_SET_EMULATION_MESSAGE = "set_emulation_message"
CONF_SET_FORMAT_MODE = "set_format_mode"
CONF_SET_READ_MODE = "set_read_mode"
CONF_SET_WRITE_MESSAGE = "set_write_message"
CONF_SET_WRITE_MODE = "set_write_mode"
CONF_TAG_TTL = "tag_ttl"
CONF_VEN_PIN = "ven_pin"
pn7150_ns = cg.esphome_ns.namespace("pn7150")
PN7150 = pn7150_ns.class_("PN7150", cg.Component)
EmulationOffAction = pn7150_ns.class_("EmulationOffAction", automation.Action)
EmulationOnAction = pn7150_ns.class_("EmulationOnAction", automation.Action)
PollingOffAction = pn7150_ns.class_("PollingOffAction", automation.Action)
PollingOnAction = pn7150_ns.class_("PollingOnAction", automation.Action)
SetCleanModeAction = pn7150_ns.class_("SetCleanModeAction", automation.Action)
SetEmulationMessageAction = pn7150_ns.class_(
"SetEmulationMessageAction", automation.Action
)
SetFormatModeAction = pn7150_ns.class_("SetFormatModeAction", automation.Action)
SetReadModeAction = pn7150_ns.class_("SetReadModeAction", automation.Action)
SetWriteMessageAction = pn7150_ns.class_("SetWriteMessageAction", automation.Action)
SetWriteModeAction = pn7150_ns.class_("SetWriteModeAction", automation.Action)
PN7150OnEmulatedTagScanTrigger = pn7150_ns.class_(
"PN7150OnEmulatedTagScanTrigger", automation.Trigger.template()
)
PN7150OnFinishedWriteTrigger = pn7150_ns.class_(
"PN7150OnFinishedWriteTrigger", automation.Trigger.template()
)
PN7150IsWritingCondition = pn7150_ns.class_(
"PN7150IsWritingCondition", automation.Condition
)
IsWritingCondition = nfc.nfc_ns.class_("IsWritingCondition", automation.Condition)
SIMPLE_ACTION_SCHEMA = maybe_simple_id(
{
cv.Required(CONF_ID): cv.use_id(PN7150),
}
)
SET_MESSAGE_ACTION_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.use_id(PN7150),
cv.Required(CONF_MESSAGE): cv.templatable(cv.string),
cv.Optional(CONF_INCLUDE_ANDROID_APP_RECORD, default=True): cv.boolean,
}
)
PN7150_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(PN7150),
cv.Optional(CONF_ON_EMULATED_TAG_SCAN): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
PN7150OnEmulatedTagScanTrigger
),
}
),
cv.Optional(CONF_ON_FINISHED_WRITE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
PN7150OnFinishedWriteTrigger
),
}
),
cv.Optional(CONF_ON_TAG): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(nfc.NfcOnTagTrigger),
}
),
cv.Optional(CONF_ON_TAG_REMOVED): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(nfc.NfcOnTagTrigger),
}
),
cv.Required(CONF_IRQ_PIN): pins.gpio_input_pin_schema,
cv.Required(CONF_VEN_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_EMULATION_MESSAGE): cv.string,
cv.Optional(CONF_TAG_TTL): cv.positive_time_period_milliseconds,
}
).extend(cv.COMPONENT_SCHEMA)
@automation.register_action(
"tag.set_emulation_message",
SetEmulationMessageAction,
SET_MESSAGE_ACTION_SCHEMA,
)
@automation.register_action(
"tag.set_write_message",
SetWriteMessageAction,
SET_MESSAGE_ACTION_SCHEMA,
)
async def pn7150_set_message_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_MESSAGE], args, cg.std_string)
cg.add(var.set_message(template_))
template_ = await cg.templatable(
config[CONF_INCLUDE_ANDROID_APP_RECORD], args, cg.bool_
)
cg.add(var.set_include_android_app_record(template_))
return var
@automation.register_action(
"tag.emulation_off", EmulationOffAction, SIMPLE_ACTION_SCHEMA
)
@automation.register_action("tag.emulation_on", EmulationOnAction, SIMPLE_ACTION_SCHEMA)
@automation.register_action("tag.polling_off", PollingOffAction, SIMPLE_ACTION_SCHEMA)
@automation.register_action("tag.polling_on", PollingOnAction, SIMPLE_ACTION_SCHEMA)
@automation.register_action(
"tag.set_clean_mode", SetCleanModeAction, SIMPLE_ACTION_SCHEMA
)
@automation.register_action(
"tag.set_format_mode", SetFormatModeAction, SIMPLE_ACTION_SCHEMA
)
@automation.register_action(
"tag.set_read_mode", SetReadModeAction, SIMPLE_ACTION_SCHEMA
)
@automation.register_action(
"tag.set_write_mode", SetWriteModeAction, SIMPLE_ACTION_SCHEMA
)
async def pn7150_simple_action_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
async def setup_pn7150(var, config):
await cg.register_component(var, config)
pin = await cg.gpio_pin_expression(config[CONF_IRQ_PIN])
cg.add(var.set_irq_pin(pin))
pin = await cg.gpio_pin_expression(config[CONF_VEN_PIN])
cg.add(var.set_ven_pin(pin))
if emulation_message_config := config.get(CONF_EMULATION_MESSAGE):
cg.add(var.set_tag_emulation_message(emulation_message_config))
cg.add(var.set_tag_emulation_on())
if CONF_TAG_TTL in config:
cg.add(var.set_tag_ttl(config[CONF_TAG_TTL]))
for conf in config.get(CONF_ON_TAG, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
cg.add(var.register_ontag_trigger(trigger))
await automation.build_automation(
trigger, [(cg.std_string, "x"), (nfc.NfcTag, "tag")], conf
)
for conf in config.get(CONF_ON_TAG_REMOVED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
cg.add(var.register_ontagremoved_trigger(trigger))
await automation.build_automation(
trigger, [(cg.std_string, "x"), (nfc.NfcTag, "tag")], conf
)
for conf in config.get(CONF_ON_EMULATED_TAG_SCAN, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_FINISHED_WRITE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
@automation.register_condition(
"pn7150.is_writing",
PN7150IsWritingCondition,
cv.Schema(
{
cv.GenerateID(): cv.use_id(PN7150),
}
),
)
async def pn7150_is_writing_to_code(config, condition_id, template_arg, args):
var = cg.new_Pvariable(condition_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var

View File

@ -0,0 +1,82 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/components/pn7150/pn7150.h"
namespace esphome {
namespace pn7150 {
class PN7150OnEmulatedTagScanTrigger : public Trigger<> {
public:
explicit PN7150OnEmulatedTagScanTrigger(PN7150 *parent) {
parent->add_on_emulated_tag_scan_callback([this]() { this->trigger(); });
}
};
class PN7150OnFinishedWriteTrigger : public Trigger<> {
public:
explicit PN7150OnFinishedWriteTrigger(PN7150 *parent) {
parent->add_on_finished_write_callback([this]() { this->trigger(); });
}
};
template<typename... Ts> class PN7150IsWritingCondition : public Condition<Ts...>, public Parented<PN7150> {
public:
bool check(Ts... x) override { return this->parent_->is_writing(); }
};
template<typename... Ts> class EmulationOffAction : public Action<Ts...>, public Parented<PN7150> {
void play(Ts... x) override { this->parent_->set_tag_emulation_off(); }
};
template<typename... Ts> class EmulationOnAction : public Action<Ts...>, public Parented<PN7150> {
void play(Ts... x) override { this->parent_->set_tag_emulation_on(); }
};
template<typename... Ts> class PollingOffAction : public Action<Ts...>, public Parented<PN7150> {
void play(Ts... x) override { this->parent_->set_polling_off(); }
};
template<typename... Ts> class PollingOnAction : public Action<Ts...>, public Parented<PN7150> {
void play(Ts... x) override { this->parent_->set_polling_on(); }
};
template<typename... Ts> class SetCleanModeAction : public Action<Ts...>, public Parented<PN7150> {
void play(Ts... x) override { this->parent_->clean_mode(); }
};
template<typename... Ts> class SetFormatModeAction : public Action<Ts...>, public Parented<PN7150> {
void play(Ts... x) override { this->parent_->format_mode(); }
};
template<typename... Ts> class SetReadModeAction : public Action<Ts...>, public Parented<PN7150> {
void play(Ts... x) override { this->parent_->read_mode(); }
};
template<typename... Ts> class SetEmulationMessageAction : public Action<Ts...>, public Parented<PN7150> {
TEMPLATABLE_VALUE(std::string, message)
TEMPLATABLE_VALUE(bool, include_android_app_record)
void play(Ts... x) override {
this->parent_->set_tag_emulation_message(this->message_.optional_value(x...),
this->include_android_app_record_.optional_value(x...));
}
};
template<typename... Ts> class SetWriteMessageAction : public Action<Ts...>, public Parented<PN7150> {
TEMPLATABLE_VALUE(std::string, message)
TEMPLATABLE_VALUE(bool, include_android_app_record)
void play(Ts... x) override {
this->parent_->set_tag_write_message(this->message_.optional_value(x...),
this->include_android_app_record_.optional_value(x...));
}
};
template<typename... Ts> class SetWriteModeAction : public Action<Ts...>, public Parented<PN7150> {
void play(Ts... x) override { this->parent_->write_mode(); }
};
} // namespace pn7150
} // namespace esphome

View File

@ -0,0 +1,1137 @@
#include "automation.h"
#include "pn7150.h"
#include <utility>
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
namespace pn7150 {
static const char *const TAG = "pn7150";
void PN7150::setup() {
this->irq_pin_->setup();
this->ven_pin_->setup();
this->nci_fsm_transition_(); // kick off reset & init processes
}
void PN7150::dump_config() {
ESP_LOGCONFIG(TAG, "PN7150:");
LOG_PIN(" IRQ pin: ", this->irq_pin_);
LOG_PIN(" VEN pin: ", this->ven_pin_);
}
void PN7150::loop() {
this->nci_fsm_transition_();
this->purge_old_tags_();
}
void PN7150::set_tag_emulation_message(std::shared_ptr<nfc::NdefMessage> message) {
this->card_emulation_message_ = std::move(message);
ESP_LOGD(TAG, "Tag emulation message set");
}
void PN7150::set_tag_emulation_message(const optional<std::string> &message,
const optional<bool> include_android_app_record) {
if (!message.has_value()) {
return;
}
auto ndef_message = make_unique<nfc::NdefMessage>();
ndef_message->add_uri_record(message.value());
if (!include_android_app_record.has_value() || include_android_app_record.value()) {
auto ext_record = make_unique<nfc::NdefRecord>();
ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
ndef_message->add_record(std::move(ext_record));
}
this->card_emulation_message_ = std::move(ndef_message);
ESP_LOGD(TAG, "Tag emulation message set");
}
void PN7150::set_tag_emulation_message(const char *message, const bool include_android_app_record) {
this->set_tag_emulation_message(std::string(message), include_android_app_record);
}
void PN7150::set_tag_emulation_off() {
if (this->listening_enabled_) {
this->listening_enabled_ = false;
this->config_refresh_pending_ = true;
}
ESP_LOGD(TAG, "Tag emulation disabled");
}
void PN7150::set_tag_emulation_on() {
if (this->card_emulation_message_ == nullptr) {
ESP_LOGE(TAG, "No NDEF message is set; tag emulation cannot be enabled");
return;
}
if (!this->listening_enabled_) {
this->listening_enabled_ = true;
this->config_refresh_pending_ = true;
}
ESP_LOGD(TAG, "Tag emulation enabled");
}
void PN7150::set_polling_off() {
if (this->polling_enabled_) {
this->polling_enabled_ = false;
this->config_refresh_pending_ = true;
}
ESP_LOGD(TAG, "Tag polling disabled");
}
void PN7150::set_polling_on() {
if (!this->polling_enabled_) {
this->polling_enabled_ = true;
this->config_refresh_pending_ = true;
}
ESP_LOGD(TAG, "Tag polling enabled");
}
void PN7150::read_mode() {
this->next_task_ = EP_READ;
ESP_LOGD(TAG, "Waiting to read next tag");
}
void PN7150::clean_mode() {
this->next_task_ = EP_CLEAN;
ESP_LOGD(TAG, "Waiting to clean next tag");
}
void PN7150::format_mode() {
this->next_task_ = EP_FORMAT;
ESP_LOGD(TAG, "Waiting to format next tag");
}
void PN7150::write_mode() {
if (this->next_task_message_to_write_ == nullptr) {
ESP_LOGW(TAG, "Message to write must be set before setting write mode");
return;
}
this->next_task_ = EP_WRITE;
ESP_LOGD(TAG, "Waiting to write next tag");
}
void PN7150::set_tag_write_message(std::shared_ptr<nfc::NdefMessage> message) {
this->next_task_message_to_write_ = std::move(message);
ESP_LOGD(TAG, "Message to write has been set");
}
void PN7150::set_tag_write_message(optional<std::string> message, optional<bool> include_android_app_record) {
if (!message.has_value()) {
return;
}
auto ndef_message = make_unique<nfc::NdefMessage>();
ndef_message->add_uri_record(message.value());
if (!include_android_app_record.has_value() || include_android_app_record.value()) {
auto ext_record = make_unique<nfc::NdefRecord>();
ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
ndef_message->add_record(std::move(ext_record));
}
this->next_task_message_to_write_ = std::move(ndef_message);
ESP_LOGD(TAG, "Message to write has been set");
}
uint8_t PN7150::set_test_mode(const TestMode test_mode, const std::vector<uint8_t> &data,
std::vector<uint8_t> &result) {
auto test_oid = TEST_PRBS_OID;
switch (test_mode) {
case TestMode::TEST_PRBS:
// test_oid = TEST_PRBS_OID;
break;
case TestMode::TEST_ANTENNA:
test_oid = TEST_ANTENNA_OID;
break;
case TestMode::TEST_GET_REGISTER:
test_oid = TEST_GET_REGISTER_OID;
break;
case TestMode::TEST_NONE:
default:
ESP_LOGD(TAG, "Exiting test mode");
this->nci_fsm_set_state_(NCIState::NFCC_RESET);
return nfc::STATUS_OK;
}
if (this->reset_core_(true, true) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to reset NCI core");
this->nci_fsm_set_error_state_(NCIState::NFCC_RESET);
result.clear();
return nfc::STATUS_FAILED;
} else {
this->nci_fsm_set_state_(NCIState::NFCC_INIT);
}
if (this->init_core_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to initialise NCI core");
this->nci_fsm_set_error_state_(NCIState::NFCC_INIT);
result.clear();
return nfc::STATUS_FAILED;
} else {
this->nci_fsm_set_state_(NCIState::TEST);
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, test_oid, data);
ESP_LOGW(TAG, "Starting test mode, OID 0x%02X", test_oid);
auto status = this->transceive_(tx, rx, NFCC_INIT_TIMEOUT);
if (status != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to start test mode, OID 0x%02X", test_oid);
this->nci_fsm_set_state_(NCIState::NFCC_RESET);
result.clear();
} else {
result = rx.get_message();
result.erase(result.begin(), result.begin() + 4); // remove NCI header
if (!result.empty()) {
ESP_LOGW(TAG, "Test results: %s", nfc::format_bytes(result).c_str());
}
}
return status;
}
uint8_t PN7150::reset_core_(const bool reset_config, const bool power) {
if (power) {
this->ven_pin_->digital_write(true);
delay(NFCC_DEFAULT_TIMEOUT);
this->ven_pin_->digital_write(false);
delay(NFCC_DEFAULT_TIMEOUT);
this->ven_pin_->digital_write(true);
delay(NFCC_INIT_TIMEOUT);
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_RESET_OID,
{(uint8_t) reset_config});
if (this->transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending reset command");
return nfc::STATUS_FAILED;
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Invalid reset response: %s", nfc::format_bytes(rx.get_message()).c_str());
return rx.get_simple_status_response();
}
// verify reset response
if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_RESPONSE)) || (!rx.message_length_is(3)) ||
(rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != 0x11) ||
(rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 2] != (uint8_t) reset_config)) {
ESP_LOGE(TAG, "Reset response was malformed: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
ESP_LOGD(TAG, "Configuration %s", rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 2] ? "reset" : "retained");
ESP_LOGD(TAG, "NCI version: %s", rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] == 0x20 ? "2.0" : "1.0");
return nfc::STATUS_OK;
}
uint8_t PN7150::init_core_() {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_INIT_OID);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending initialise command");
return nfc::STATUS_FAILED;
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Invalid initialise response: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
uint8_t manf_id = rx.get_message()[15 + rx.get_message()[8]];
uint8_t hw_version = rx.get_message()[16 + rx.get_message()[8]];
uint8_t rom_code_version = rx.get_message()[17 + rx.get_message()[8]];
uint8_t flash_major_version = rx.get_message()[18 + rx.get_message()[8]];
uint8_t flash_minor_version = rx.get_message()[19 + rx.get_message()[8]];
ESP_LOGD(TAG, "Manufacturer ID: 0x%02X", manf_id);
ESP_LOGD(TAG, "Hardware version: 0x%02X", hw_version);
ESP_LOGD(TAG, "ROM code version: 0x%02X", rom_code_version);
ESP_LOGD(TAG, "FLASH major version: 0x%02X", flash_major_version);
ESP_LOGD(TAG, "FLASH minor version: 0x%02X", flash_minor_version);
return rx.get_simple_status_response();
}
uint8_t PN7150::send_init_config_() {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, nfc::NCI_CORE_SET_CONFIG_OID);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error enabling proprietary extensions");
return nfc::STATUS_FAILED;
}
tx.set_message(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
std::vector<uint8_t>(std::begin(PMU_CFG), std::end(PMU_CFG)));
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending PMU config");
return nfc::STATUS_FAILED;
}
return this->send_core_config_();
}
uint8_t PN7150::send_core_config_() {
const auto *core_config_begin = std::begin(CORE_CONFIG_SOLO);
const auto *core_config_end = std::end(CORE_CONFIG_SOLO);
this->core_config_is_solo_ = true;
if (this->listening_enabled_ && this->polling_enabled_) {
core_config_begin = std::begin(CORE_CONFIG_RW_CE);
core_config_end = std::end(CORE_CONFIG_RW_CE);
this->core_config_is_solo_ = false;
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
std::vector<uint8_t>(core_config_begin, core_config_end));
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "Error sending core config");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7150::refresh_core_config_() {
bool core_config_should_be_solo = !(this->listening_enabled_ && this->polling_enabled_);
if (this->nci_state_ == NCIState::RFST_DISCOVERY) {
if (this->stop_discovery_() != nfc::STATUS_OK) {
this->nci_fsm_set_state_(NCIState::NFCC_RESET);
return nfc::STATUS_FAILED;
}
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
}
if (this->core_config_is_solo_ != core_config_should_be_solo) {
if (this->send_core_config_() != nfc::STATUS_OK) {
ESP_LOGV(TAG, "Failed to refresh core config");
return nfc::STATUS_FAILED;
}
}
this->config_refresh_pending_ = false;
return nfc::STATUS_OK;
}
uint8_t PN7150::set_discover_map_() {
std::vector<uint8_t> discover_map = {sizeof(RF_DISCOVER_MAP_CONFIG) / 3};
discover_map.insert(discover_map.end(), std::begin(RF_DISCOVER_MAP_CONFIG), std::end(RF_DISCOVER_MAP_CONFIG));
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_MAP_OID, discover_map);
if (this->transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending discover map poll config");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7150::set_listen_mode_routing_() {
nfc::NciMessage rx;
nfc::NciMessage tx(
nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_SET_LISTEN_MODE_ROUTING_OID,
std::vector<uint8_t>(std::begin(RF_LISTEN_MODE_ROUTING_CONFIG), std::end(RF_LISTEN_MODE_ROUTING_CONFIG)));
if (this->transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error setting listen mode routing config");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7150::start_discovery_() {
const uint8_t *rf_discovery_config = RF_DISCOVERY_CONFIG;
uint8_t length = sizeof(RF_DISCOVERY_CONFIG);
if (!this->listening_enabled_) {
length = sizeof(RF_DISCOVERY_POLL_CONFIG);
rf_discovery_config = RF_DISCOVERY_POLL_CONFIG;
} else if (!this->polling_enabled_) {
length = sizeof(RF_DISCOVERY_LISTEN_CONFIG);
rf_discovery_config = RF_DISCOVERY_LISTEN_CONFIG;
}
std::vector<uint8_t> discover_config = std::vector<uint8_t>((length * 2) + 1);
discover_config[0] = length;
for (uint8_t i = 0; i < length; i++) {
discover_config[(i * 2) + 1] = rf_discovery_config[i];
discover_config[(i * 2) + 2] = 0x01; // RF Technology and Mode will be executed in every discovery period
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_OID, discover_config);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
switch (rx.get_simple_status_response()) {
// in any of these cases, we are either already in or will remain in discovery, which satisfies the function call
case nfc::STATUS_OK:
case nfc::DISCOVERY_ALREADY_STARTED:
case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
case nfc::DISCOVERY_TEAR_DOWN:
return nfc::STATUS_OK;
default:
ESP_LOGE(TAG, "Error starting discovery");
return nfc::STATUS_FAILED;
}
}
return nfc::STATUS_OK;
}
uint8_t PN7150::stop_discovery_() { return this->deactivate_(nfc::DEACTIVATION_TYPE_IDLE, NFCC_TAG_WRITE_TIMEOUT); }
uint8_t PN7150::deactivate_(const uint8_t type, const uint16_t timeout) {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DEACTIVATE_OID, {type});
auto status = this->transceive_(tx, rx, timeout);
// if (status != nfc::STATUS_OK) {
// ESP_LOGE(TAG, "Error sending deactivate type %u", type);
// return nfc::STATUS_FAILED;
// }
return status;
}
void PN7150::select_endpoint_() {
if (this->discovered_endpoint_.empty()) {
ESP_LOGW(TAG, "No cached tags to select");
this->stop_discovery_();
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
return;
}
std::vector<uint8_t> endpoint_data = {this->discovered_endpoint_[0].id, this->discovered_endpoint_[0].protocol,
0x01}; // that last byte is the interface ID
for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
if (!this->discovered_endpoint_[i].trig_called) {
endpoint_data = {this->discovered_endpoint_[i].id, this->discovered_endpoint_[i].protocol,
0x01}; // that last byte is the interface ID
this->selecting_endpoint_ = i;
break;
}
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_SELECT_OID, endpoint_data);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error selecting endpoint");
} else {
this->nci_fsm_set_state_(NCIState::EP_SELECTING);
}
}
uint8_t PN7150::read_endpoint_data_(nfc::NfcTag &tag) {
uint8_t type = nfc::guess_tag_type(tag.get_uid().size());
switch (type) {
case nfc::TAG_TYPE_MIFARE_CLASSIC:
ESP_LOGV(TAG, "Reading Mifare classic");
return this->read_mifare_classic_tag_(tag);
case nfc::TAG_TYPE_2:
ESP_LOGV(TAG, "Reading Mifare ultralight");
return this->read_mifare_ultralight_tag_(tag);
case nfc::TAG_TYPE_UNKNOWN:
default:
ESP_LOGV(TAG, "Cannot determine tag type");
break;
}
return nfc::STATUS_FAILED;
}
uint8_t PN7150::clean_endpoint_(std::vector<uint8_t> &uid) {
uint8_t type = nfc::guess_tag_type(uid.size());
switch (type) {
case nfc::TAG_TYPE_MIFARE_CLASSIC:
return this->format_mifare_classic_mifare_();
case nfc::TAG_TYPE_2:
return this->clean_mifare_ultralight_();
default:
ESP_LOGE(TAG, "Unsupported tag for cleaning");
break;
}
return nfc::STATUS_FAILED;
}
uint8_t PN7150::format_endpoint_(std::vector<uint8_t> &uid) {
uint8_t type = nfc::guess_tag_type(uid.size());
switch (type) {
case nfc::TAG_TYPE_MIFARE_CLASSIC:
return this->format_mifare_classic_ndef_();
case nfc::TAG_TYPE_2:
return this->clean_mifare_ultralight_();
default:
ESP_LOGE(TAG, "Unsupported tag for formatting");
break;
}
return nfc::STATUS_FAILED;
}
uint8_t PN7150::write_endpoint_(std::vector<uint8_t> &uid, std::shared_ptr<nfc::NdefMessage> &message) {
uint8_t type = nfc::guess_tag_type(uid.size());
switch (type) {
case nfc::TAG_TYPE_MIFARE_CLASSIC:
return this->write_mifare_classic_tag_(message);
case nfc::TAG_TYPE_2:
return this->write_mifare_ultralight_tag_(uid, message);
default:
ESP_LOGE(TAG, "Unsupported tag for writing");
break;
}
return nfc::STATUS_FAILED;
}
std::unique_ptr<nfc::NfcTag> PN7150::build_tag_(const uint8_t mode_tech, const std::vector<uint8_t> &data) {
switch (mode_tech) {
case (nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA): {
uint8_t uid_length = data[2];
if (!uid_length) {
ESP_LOGE(TAG, "UID length cannot be zero");
return nullptr;
}
std::vector<uint8_t> uid(data.begin() + 3, data.begin() + 3 + uid_length);
const auto *tag_type_str =
nfc::guess_tag_type(uid_length) == nfc::TAG_TYPE_MIFARE_CLASSIC ? nfc::MIFARE_CLASSIC : nfc::NFC_FORUM_TYPE_2;
return make_unique<nfc::NfcTag>(uid, tag_type_str);
}
}
return nullptr;
}
optional<size_t> PN7150::find_tag_uid_(const std::vector<uint8_t> &uid) {
if (!this->discovered_endpoint_.empty()) {
for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
auto existing_tag_uid = this->discovered_endpoint_[i].tag->get_uid();
bool uid_match = (uid.size() == existing_tag_uid.size());
if (uid_match) {
for (size_t i = 0; i < uid.size(); i++) {
uid_match &= (uid[i] == existing_tag_uid[i]);
}
if (uid_match) {
return i;
}
}
}
}
return nullopt;
}
void PN7150::purge_old_tags_() {
for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
if (millis() - this->discovered_endpoint_[i].last_seen > this->tag_ttl_) {
this->erase_tag_(i);
}
}
}
void PN7150::erase_tag_(const uint8_t tag_index) {
if (tag_index < this->discovered_endpoint_.size()) {
for (auto *trigger : this->triggers_ontagremoved_) {
trigger->process(this->discovered_endpoint_[tag_index].tag);
}
ESP_LOGI(TAG, "Tag %s removed", nfc::format_uid(this->discovered_endpoint_[tag_index].tag->get_uid()).c_str());
this->discovered_endpoint_.erase(this->discovered_endpoint_.begin() + tag_index);
}
}
void PN7150::nci_fsm_transition_() {
switch (this->nci_state_) {
case NCIState::NFCC_RESET:
if (this->reset_core_(true, true) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to reset NCI core");
this->nci_fsm_set_error_state_(NCIState::NFCC_RESET);
return;
} else {
this->nci_fsm_set_state_(NCIState::NFCC_INIT);
}
// fall through
case NCIState::NFCC_INIT:
if (this->init_core_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to initialise NCI core");
this->nci_fsm_set_error_state_(NCIState::NFCC_INIT);
return;
} else {
this->nci_fsm_set_state_(NCIState::NFCC_CONFIG);
}
// fall through
case NCIState::NFCC_CONFIG:
if (this->send_init_config_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to send initial config");
this->nci_fsm_set_error_state_(NCIState::NFCC_CONFIG);
return;
} else {
this->config_refresh_pending_ = false;
this->nci_fsm_set_state_(NCIState::NFCC_SET_DISCOVER_MAP);
}
// fall through
case NCIState::NFCC_SET_DISCOVER_MAP:
if (this->set_discover_map_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to set discover map");
this->nci_fsm_set_error_state_(NCIState::NFCC_SET_LISTEN_MODE_ROUTING);
return;
} else {
this->nci_fsm_set_state_(NCIState::NFCC_SET_LISTEN_MODE_ROUTING);
}
// fall through
case NCIState::NFCC_SET_LISTEN_MODE_ROUTING:
if (this->set_listen_mode_routing_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to set listen mode routing");
this->nci_fsm_set_error_state_(NCIState::RFST_IDLE);
return;
} else {
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
}
// fall through
case NCIState::RFST_IDLE:
if (this->nci_state_error_ == NCIState::RFST_DISCOVERY) {
this->stop_discovery_();
}
if (this->config_refresh_pending_) {
this->refresh_core_config_();
}
if (!this->listening_enabled_ && !this->polling_enabled_) {
return;
}
if (this->start_discovery_() != nfc::STATUS_OK) {
ESP_LOGV(TAG, "Failed to start discovery");
this->nci_fsm_set_error_state_(NCIState::RFST_DISCOVERY);
} else {
this->nci_fsm_set_state_(NCIState::RFST_DISCOVERY);
}
return;
case NCIState::RFST_W4_HOST_SELECT:
select_endpoint_();
// fall through
// All cases below are waiting for NOTIFICATION messages
case NCIState::RFST_DISCOVERY:
if (this->config_refresh_pending_) {
this->refresh_core_config_();
}
// fall through
case NCIState::RFST_LISTEN_ACTIVE:
case NCIState::RFST_LISTEN_SLEEP:
case NCIState::RFST_POLL_ACTIVE:
case NCIState::EP_SELECTING:
case NCIState::EP_DEACTIVATING:
if (this->irq_pin_->digital_read()) {
this->process_message_();
}
break;
case NCIState::TEST:
case NCIState::FAILED:
case NCIState::NONE:
default:
return;
}
}
void PN7150::nci_fsm_set_state_(NCIState new_state) {
ESP_LOGVV(TAG, "nci_fsm_set_state_(%u)", (uint8_t) new_state);
this->nci_state_ = new_state;
this->nci_state_error_ = NCIState::NONE;
this->error_count_ = 0;
this->last_nci_state_change_ = millis();
}
bool PN7150::nci_fsm_set_error_state_(NCIState new_state) {
ESP_LOGVV(TAG, "nci_fsm_set_error_state_(%u); error_count_ = %u", (uint8_t) new_state, this->error_count_);
this->nci_state_error_ = new_state;
if (this->error_count_++ > NFCC_MAX_ERROR_COUNT) {
if ((this->nci_state_error_ == NCIState::NFCC_RESET) || (this->nci_state_error_ == NCIState::NFCC_INIT) ||
(this->nci_state_error_ == NCIState::NFCC_CONFIG)) {
ESP_LOGE(TAG, "Too many initialization failures -- check device connections");
this->mark_failed();
this->nci_fsm_set_state_(NCIState::FAILED);
} else {
ESP_LOGW(TAG, "Too many errors transitioning to state %u; resetting NFCC", (uint8_t) this->nci_state_error_);
this->nci_fsm_set_state_(NCIState::NFCC_RESET);
}
}
return this->error_count_ > NFCC_MAX_ERROR_COUNT;
}
void PN7150::process_message_() {
nfc::NciMessage rx;
if (this->read_nfcc(rx, NFCC_DEFAULT_TIMEOUT) != nfc::STATUS_OK) {
return; // No data
}
switch (rx.get_message_type()) {
case nfc::NCI_PKT_MT_CTRL_NOTIFICATION:
if (rx.get_gid() == nfc::RF_GID) {
switch (rx.get_oid()) {
case nfc::RF_INTF_ACTIVATED_OID:
ESP_LOGVV(TAG, "RF_INTF_ACTIVATED_OID");
this->process_rf_intf_activated_oid_(rx);
return;
case nfc::RF_DISCOVER_OID:
ESP_LOGVV(TAG, "RF_DISCOVER_OID");
this->process_rf_discover_oid_(rx);
return;
case nfc::RF_DEACTIVATE_OID:
ESP_LOGVV(TAG, "RF_DEACTIVATE_OID: type: 0x%02X, reason: 0x%02X", rx.get_message()[3], rx.get_message()[4]);
this->process_rf_deactivate_oid_(rx);
return;
default:
ESP_LOGV(TAG, "Unimplemented RF OID received: 0x%02X", rx.get_oid());
}
} else if (rx.get_gid() == nfc::NCI_CORE_GID) {
switch (rx.get_oid()) {
case nfc::NCI_CORE_GENERIC_ERROR_OID:
ESP_LOGV(TAG, "NCI_CORE_GENERIC_ERROR_OID:");
switch (rx.get_simple_status_response()) {
case nfc::DISCOVERY_ALREADY_STARTED:
ESP_LOGV(TAG, " DISCOVERY_ALREADY_STARTED");
break;
case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
// Tag removed too soon
ESP_LOGV(TAG, " DISCOVERY_TARGET_ACTIVATION_FAILED");
if (this->nci_state_ == NCIState::EP_SELECTING) {
this->nci_fsm_set_state_(NCIState::RFST_W4_HOST_SELECT);
if (!this->discovered_endpoint_.empty()) {
this->erase_tag_(this->selecting_endpoint_);
}
} else {
this->stop_discovery_();
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
}
break;
case nfc::DISCOVERY_TEAR_DOWN:
ESP_LOGV(TAG, " DISCOVERY_TEAR_DOWN");
break;
default:
ESP_LOGW(TAG, "Unknown error: 0x%02X", rx.get_simple_status_response());
break;
}
break;
default:
ESP_LOGV(TAG, "Unimplemented NCI Core OID received: 0x%02X", rx.get_oid());
}
} else {
ESP_LOGV(TAG, "Unimplemented notification: %s", nfc::format_bytes(rx.get_message()).c_str());
}
break;
case nfc::NCI_PKT_MT_CTRL_RESPONSE:
ESP_LOGV(TAG, "Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.get_gid(), rx.get_oid(),
nfc::format_bytes(rx.get_message()).c_str());
break;
case nfc::NCI_PKT_MT_CTRL_COMMAND:
ESP_LOGV(TAG, "Unimplemented command: %s", nfc::format_bytes(rx.get_message()).c_str());
break;
case nfc::NCI_PKT_MT_DATA:
this->process_data_message_(rx);
break;
default:
ESP_LOGV(TAG, "Unimplemented message type: %s", nfc::format_bytes(rx.get_message()).c_str());
break;
}
}
void PN7150::process_rf_intf_activated_oid_(nfc::NciMessage &rx) { // an endpoint was activated
uint8_t discovery_id = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_DISCOVERY_ID);
uint8_t interface = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_INTERFACE);
uint8_t protocol = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_PROTOCOL);
uint8_t mode_tech = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MODE_TECH);
uint8_t max_size = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MAX_SIZE);
ESP_LOGVV(TAG, "Endpoint activated -- interface: 0x%02X, protocol: 0x%02X, mode&tech: 0x%02X, max payload: %u",
interface, protocol, mode_tech, max_size);
if (mode_tech & nfc::MODE_LISTEN_MASK) {
ESP_LOGVV(TAG, "Tag activated in listen mode");
this->nci_fsm_set_state_(NCIState::RFST_LISTEN_ACTIVE);
return;
}
this->nci_fsm_set_state_(NCIState::RFST_POLL_ACTIVE);
auto incoming_tag =
this->build_tag_(mode_tech, std::vector<uint8_t>(rx.get_message().begin() + 10, rx.get_message().end()));
if (incoming_tag == nullptr) {
ESP_LOGE(TAG, "Could not build tag");
} else {
auto tag_loc = this->find_tag_uid_(incoming_tag->get_uid());
if (tag_loc.has_value()) {
this->discovered_endpoint_[tag_loc.value()].id = discovery_id;
this->discovered_endpoint_[tag_loc.value()].protocol = protocol;
this->discovered_endpoint_[tag_loc.value()].last_seen = millis();
ESP_LOGVV(TAG, "Tag cache updated");
} else {
this->discovered_endpoint_.emplace_back(
DiscoveredEndpoint{discovery_id, protocol, millis(), std::move(incoming_tag), false});
tag_loc = this->discovered_endpoint_.size() - 1;
ESP_LOGVV(TAG, "Tag added to cache");
}
auto &working_endpoint = this->discovered_endpoint_[tag_loc.value()];
switch (this->next_task_) {
case EP_CLEAN:
ESP_LOGD(TAG, " Tag cleaning...");
if (this->clean_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
ESP_LOGE(TAG, " Tag cleaning incomplete");
}
ESP_LOGD(TAG, " Tag cleaned!");
break;
case EP_FORMAT:
ESP_LOGD(TAG, " Tag formatting...");
if (this->format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error formatting tag as NDEF");
}
ESP_LOGD(TAG, " Tag formatted!");
break;
case EP_WRITE:
if (this->next_task_message_to_write_ != nullptr) {
ESP_LOGD(TAG, " Tag writing...");
ESP_LOGD(TAG, " Tag formatting...");
if (this->format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
ESP_LOGE(TAG, " Tag could not be formatted for writing");
} else {
ESP_LOGD(TAG, " Writing NDEF data");
if (this->write_endpoint_(working_endpoint.tag->get_uid(), this->next_task_message_to_write_) !=
nfc::STATUS_OK) {
ESP_LOGE(TAG, " Failed to write message to tag");
}
ESP_LOGD(TAG, " Finished writing NDEF data");
this->next_task_message_to_write_ = nullptr;
this->on_finished_write_callback_.call();
}
}
break;
case EP_READ:
default:
if (!working_endpoint.trig_called) {
ESP_LOGI(TAG, "Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(),
nfc::format_uid(working_endpoint.tag->get_uid()).c_str());
if (this->read_endpoint_data_(*working_endpoint.tag) != nfc::STATUS_OK) {
ESP_LOGW(TAG, " Unable to read NDEF record(s)");
} else if (working_endpoint.tag->has_ndef_message()) {
const auto message = working_endpoint.tag->get_ndef_message();
const auto records = message->get_records();
ESP_LOGD(TAG, " NDEF record(s):");
for (const auto &record : records) {
ESP_LOGD(TAG, " %s - %s", record->get_type().c_str(), record->get_payload().c_str());
}
} else {
ESP_LOGW(TAG, " No NDEF records found");
}
for (auto *trigger : this->triggers_ontag_) {
trigger->process(working_endpoint.tag);
}
working_endpoint.trig_called = true;
break;
}
}
if (working_endpoint.tag->get_tag_type() == nfc::MIFARE_CLASSIC) {
this->halt_mifare_classic_tag_();
}
}
if (this->next_task_ != EP_READ) {
this->read_mode();
}
this->stop_discovery_();
this->nci_fsm_set_state_(NCIState::EP_DEACTIVATING);
}
void PN7150::process_rf_discover_oid_(nfc::NciMessage &rx) {
auto incoming_tag = this->build_tag_(rx.get_message_byte(nfc::RF_DISCOVER_NTF_MODE_TECH),
std::vector<uint8_t>(rx.get_message().begin() + 7, rx.get_message().end()));
if (incoming_tag == nullptr) {
ESP_LOGE(TAG, "Could not build tag!");
} else {
auto tag_loc = this->find_tag_uid_(incoming_tag->get_uid());
if (tag_loc.has_value()) {
this->discovered_endpoint_[tag_loc.value()].id = rx.get_message_byte(nfc::RF_DISCOVER_NTF_DISCOVERY_ID);
this->discovered_endpoint_[tag_loc.value()].protocol = rx.get_message_byte(nfc::RF_DISCOVER_NTF_PROTOCOL);
this->discovered_endpoint_[tag_loc.value()].last_seen = millis();
ESP_LOGVV(TAG, "Tag found & updated");
} else {
this->discovered_endpoint_.emplace_back(DiscoveredEndpoint{rx.get_message_byte(nfc::RF_DISCOVER_NTF_DISCOVERY_ID),
rx.get_message_byte(nfc::RF_DISCOVER_NTF_PROTOCOL),
millis(), std::move(incoming_tag), false});
ESP_LOGVV(TAG, "Tag saved");
}
}
if (rx.get_message().back() != nfc::RF_DISCOVER_NTF_NT_MORE) {
this->nci_fsm_set_state_(NCIState::RFST_W4_HOST_SELECT);
ESP_LOGVV(TAG, "Discovered %u endpoints", this->discovered_endpoint_.size());
}
}
void PN7150::process_rf_deactivate_oid_(nfc::NciMessage &rx) {
this->ce_state_ = CardEmulationState::CARD_EMU_IDLE;
switch (rx.get_simple_status_response()) {
case nfc::DEACTIVATION_TYPE_DISCOVERY:
this->nci_fsm_set_state_(NCIState::RFST_DISCOVERY);
break;
case nfc::DEACTIVATION_TYPE_IDLE:
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
break;
case nfc::DEACTIVATION_TYPE_SLEEP:
case nfc::DEACTIVATION_TYPE_SLEEP_AF:
if (this->nci_state_ == NCIState::RFST_LISTEN_ACTIVE) {
this->nci_fsm_set_state_(NCIState::RFST_LISTEN_SLEEP);
} else if (this->nci_state_ == NCIState::RFST_POLL_ACTIVE) {
this->nci_fsm_set_state_(NCIState::RFST_W4_HOST_SELECT);
} else {
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
}
break;
default:
break;
}
}
void PN7150::process_data_message_(nfc::NciMessage &rx) {
ESP_LOGVV(TAG, "Received data message: %s", nfc::format_bytes(rx.get_message()).c_str());
std::vector<uint8_t> ndef_response;
this->card_emu_t4t_get_response_(rx.get_message(), ndef_response);
uint16_t ndef_response_size = ndef_response.size();
if (!ndef_response_size) {
return; // no message returned, we cannot respond
}
std::vector<uint8_t> tx_msg = {nfc::NCI_PKT_MT_DATA, uint8_t((ndef_response_size & 0xFF00) >> 8),
uint8_t(ndef_response_size & 0x00FF)};
tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end());
nfc::NciMessage tx(tx_msg);
ESP_LOGVV(TAG, "Sending data message: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending reply for card emulation failed");
}
}
void PN7150::card_emu_t4t_get_response_(std::vector<uint8_t> &response, std::vector<uint8_t> &ndef_response) {
if (this->card_emulation_message_ == nullptr) {
ESP_LOGE(TAG, "No NDEF message is set; tag emulation not possible");
ndef_response.clear();
return;
}
if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_APP_SELECT))) {
// CARD_EMU_T4T_APP_SELECT
ESP_LOGVV(TAG, "CARD_EMU_NDEF_APP_SELECTED");
this->ce_state_ = CardEmulationState::CARD_EMU_NDEF_APP_SELECTED;
ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
} else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_CC_SELECT))) {
// CARD_EMU_T4T_CC_SELECT
if (this->ce_state_ == CardEmulationState::CARD_EMU_NDEF_APP_SELECTED) {
ESP_LOGVV(TAG, "CARD_EMU_CC_SELECTED");
this->ce_state_ = CardEmulationState::CARD_EMU_CC_SELECTED;
ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
}
} else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_NDEF_SELECT))) {
// CARD_EMU_T4T_NDEF_SELECT
ESP_LOGVV(TAG, "CARD_EMU_NDEF_SELECTED");
this->ce_state_ = CardEmulationState::CARD_EMU_NDEF_SELECTED;
ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
} else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
response.begin() + nfc::NCI_PKT_HEADER_SIZE + sizeof(CARD_EMU_T4T_READ),
std::begin(CARD_EMU_T4T_READ))) {
// CARD_EMU_T4T_READ
if (this->ce_state_ == CardEmulationState::CARD_EMU_CC_SELECTED) {
// CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED
ESP_LOGVV(TAG, "CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED");
uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
if (length <= (sizeof(CARD_EMU_T4T_CC) + offset + 2)) {
ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_CC) + offset,
std::begin(CARD_EMU_T4T_CC) + offset + length);
ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
}
} else if (this->ce_state_ == CardEmulationState::CARD_EMU_NDEF_SELECTED) {
// CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED
ESP_LOGVV(TAG, "CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED");
auto ndef_message = this->card_emulation_message_->encode();
uint16_t ndef_msg_size = ndef_message.size();
uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
ESP_LOGVV(TAG, "Encoded NDEF message: %s", nfc::format_bytes(ndef_message).c_str());
if (length <= (ndef_msg_size + offset + 2)) {
if (offset == 0) {
ndef_response.resize(2);
ndef_response[0] = (ndef_msg_size & 0xFF00) >> 8;
ndef_response[1] = (ndef_msg_size & 0x00FF);
if (length > 2) {
ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 2);
}
} else if (offset == 1) {
ndef_response.resize(1);
ndef_response[0] = (ndef_msg_size & 0x00FF);
if (length > 1) {
ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 1);
}
} else {
ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length);
}
ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
if ((offset + length) >= (ndef_msg_size + 2)) {
ESP_LOGD(TAG, "NDEF message sent");
this->on_emulated_tag_scan_callback_.call();
}
}
}
} else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
response.begin() + nfc::NCI_PKT_HEADER_SIZE + sizeof(CARD_EMU_T4T_WRITE),
std::begin(CARD_EMU_T4T_WRITE))) {
// CARD_EMU_T4T_WRITE
if (this->ce_state_ == CardEmulationState::CARD_EMU_NDEF_SELECTED) {
ESP_LOGVV(TAG, "CARD_EMU_T4T_WRITE");
uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
std::vector<uint8_t> ndef_msg_written;
ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5,
response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 + length);
ESP_LOGD(TAG, "Received %u-byte NDEF message: %s", length, nfc::format_bytes(ndef_msg_written).c_str());
ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
}
}
}
uint8_t PN7150::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint16_t timeout,
const bool expect_notification) {
uint8_t retries = NFCC_MAX_COMM_FAILS;
while (retries) {
// first, send the message we need to send
if (this->write_nfcc(tx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending message");
return nfc::STATUS_FAILED;
}
ESP_LOGVV(TAG, "Wrote: %s", nfc::format_bytes(tx.get_message()).c_str());
// next, the NFCC should send back a response
if (this->read_nfcc(rx, timeout) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "Error receiving message");
if (!retries--) {
ESP_LOGE(TAG, " ...giving up");
return nfc::STATUS_FAILED;
}
} else {
break;
}
}
ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes(rx.get_message()).c_str());
// validate the response based on the message type that was sent (command vs. data)
if (!tx.message_type_is(nfc::NCI_PKT_MT_DATA)) {
// for commands, the GID and OID should match and the status should be OK
if ((rx.get_gid() != tx.get_gid()) || (rx.get_oid()) != tx.get_oid()) {
ESP_LOGE(TAG, "Incorrect response to command: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Error in response to command: %s", nfc::format_bytes(rx.get_message()).c_str());
}
return rx.get_simple_status_response();
} else {
// when requesting data from the endpoint, the first response is from the NFCC; we must validate this, first
if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.gid_is(nfc::NCI_CORE_GID)) ||
(!rx.oid_is(nfc::NCI_CORE_CONN_CREDITS_OID)) || (!rx.message_length_is(3))) {
ESP_LOGE(TAG, "Incorrect response to data message: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
if (expect_notification) {
// if the NFCC said "OK", there will be additional data to read; this comes back in a notification message
if (this->read_nfcc(rx, timeout) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error receiving data from endpoint");
return nfc::STATUS_FAILED;
}
ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes(rx.get_message()).c_str());
}
return nfc::STATUS_OK;
}
}
uint8_t PN7150::wait_for_irq_(uint16_t timeout, bool pin_state) {
auto start_time = millis();
while (millis() - start_time < timeout) {
if (this->irq_pin_->digital_read() == pin_state) {
return nfc::STATUS_OK;
}
}
ESP_LOGW(TAG, "Timed out waiting for IRQ state");
return nfc::STATUS_FAILED;
}
} // namespace pn7150
} // namespace esphome

View File

@ -0,0 +1,296 @@
#pragma once
#include "esphome/components/nfc/automation.h"
#include "esphome/components/nfc/nci_core.h"
#include "esphome/components/nfc/nci_message.h"
#include "esphome/components/nfc/nfc.h"
#include "esphome/components/nfc/nfc_helpers.h"
#include "esphome/core/component.h"
#include "esphome/core/gpio.h"
#include "esphome/core/helpers.h"
#include <functional>
namespace esphome {
namespace pn7150 {
static const uint16_t NFCC_DEFAULT_TIMEOUT = 10;
static const uint16_t NFCC_INIT_TIMEOUT = 50;
static const uint16_t NFCC_TAG_WRITE_TIMEOUT = 15;
static const uint8_t NFCC_MAX_COMM_FAILS = 3;
static const uint8_t NFCC_MAX_ERROR_COUNT = 10;
static const uint8_t XCHG_DATA_OID = 0x10;
static const uint8_t MF_SECTORSEL_OID = 0x32;
static const uint8_t MFC_AUTHENTICATE_OID = 0x40;
static const uint8_t TEST_PRBS_OID = 0x30;
static const uint8_t TEST_ANTENNA_OID = 0x3D;
static const uint8_t TEST_GET_REGISTER_OID = 0x33;
static const uint8_t MFC_AUTHENTICATE_PARAM_KS_A = 0x00; // key select A
static const uint8_t MFC_AUTHENTICATE_PARAM_KS_B = 0x80; // key select B
static const uint8_t MFC_AUTHENTICATE_PARAM_EMBED_KEY = 0x10;
static const uint8_t CARD_EMU_T4T_APP_SELECT[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xD2, 0x76,
0x00, 0x00, 0x85, 0x01, 0x01, 0x00};
static const uint8_t CARD_EMU_T4T_CC[] = {0x00, 0x0F, 0x20, 0x00, 0xFF, 0x00, 0xFF, 0x04,
0x06, 0xE1, 0x04, 0x00, 0xFF, 0x00, 0x00};
static const uint8_t CARD_EMU_T4T_CC_SELECT[] = {0x00, 0xA4, 0x00, 0x0C, 0x02, 0xE1, 0x03};
static const uint8_t CARD_EMU_T4T_NDEF_SELECT[] = {0x00, 0xA4, 0x00, 0x0C, 0x02, 0xE1, 0x04};
static const uint8_t CARD_EMU_T4T_READ[] = {0x00, 0xB0};
static const uint8_t CARD_EMU_T4T_WRITE[] = {0x00, 0xD6};
static const uint8_t CARD_EMU_T4T_OK[] = {0x90, 0x00};
static const uint8_t CARD_EMU_T4T_NOK[] = {0x6A, 0x82};
static const uint8_t CORE_CONFIG_SOLO[] = {0x01, // Number of parameter fields
0x00, // config param identifier (TOTAL_DURATION)
0x02, // length of value
0x01, // TOTAL_DURATION (low)...
0x00}; // TOTAL_DURATION (high): 1 ms
static const uint8_t CORE_CONFIG_RW_CE[] = {0x01, // Number of parameter fields
0x00, // config param identifier (TOTAL_DURATION)
0x02, // length of value
0xF8, // TOTAL_DURATION (low)...
0x02}; // TOTAL_DURATION (high): 760 ms
static const uint8_t PMU_CFG[] = {
0x01, // Number of parameters
0xA0, 0x0E, // ext. tag
3, // length
0x06, // VBAT1 connected to 5V (CFG2)
0x64, // TVDD monitoring threshold = 5.0V; TxLDO voltage = 4.7V (in reader & card modes)
0x01, // RFU; must be 0x00 for CFG1 and 0x01 for CFG2
};
static const uint8_t RF_DISCOVER_MAP_CONFIG[] = { // poll modes
nfc::PROT_T1T, nfc::RF_DISCOVER_MAP_MODE_POLL,
nfc::INTF_FRAME, // poll mode
nfc::PROT_T2T, nfc::RF_DISCOVER_MAP_MODE_POLL,
nfc::INTF_FRAME, // poll mode
nfc::PROT_T3T, nfc::RF_DISCOVER_MAP_MODE_POLL,
nfc::INTF_FRAME, // poll mode
nfc::PROT_ISODEP, nfc::RF_DISCOVER_MAP_MODE_POLL | nfc::RF_DISCOVER_MAP_MODE_LISTEN,
nfc::INTF_ISODEP, // poll & listen mode
nfc::PROT_MIFARE, nfc::RF_DISCOVER_MAP_MODE_POLL,
nfc::INTF_TAGCMD}; // poll mode
static const uint8_t RF_DISCOVERY_LISTEN_CONFIG[] = {nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCA, // listen mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCB, // listen mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCF}; // listen mode
static const uint8_t RF_DISCOVERY_POLL_CONFIG[] = {nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA, // poll mode
nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCB, // poll mode
nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCF}; // poll mode
static const uint8_t RF_DISCOVERY_CONFIG[] = {nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA, // poll mode
nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCB, // poll mode
nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCF, // poll mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCA, // listen mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCB, // listen mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCF}; // listen mode
static const uint8_t RF_LISTEN_MODE_ROUTING_CONFIG[] = {0x00, // "more" (another message is coming)
1, // number of table entries
0x01, // type = protocol-based
3, // length
0, // DH NFCEE ID, a static ID representing the DH-NFCEE
0x01, // power state
nfc::PROT_ISODEP}; // protocol
enum class CardEmulationState : uint8_t {
CARD_EMU_IDLE,
CARD_EMU_NDEF_APP_SELECTED,
CARD_EMU_CC_SELECTED,
CARD_EMU_NDEF_SELECTED,
CARD_EMU_DESFIRE_PROD,
};
enum class NCIState : uint8_t {
NONE = 0x00,
NFCC_RESET,
NFCC_INIT,
NFCC_CONFIG,
NFCC_SET_DISCOVER_MAP,
NFCC_SET_LISTEN_MODE_ROUTING,
RFST_IDLE,
RFST_DISCOVERY,
RFST_W4_ALL_DISCOVERIES,
RFST_W4_HOST_SELECT,
RFST_LISTEN_ACTIVE,
RFST_LISTEN_SLEEP,
RFST_POLL_ACTIVE,
EP_DEACTIVATING,
EP_SELECTING,
TEST = 0XFE,
FAILED = 0XFF,
};
enum class TestMode : uint8_t {
TEST_NONE = 0x00,
TEST_PRBS,
TEST_ANTENNA,
TEST_GET_REGISTER,
};
struct DiscoveredEndpoint {
uint8_t id;
uint8_t protocol;
uint32_t last_seen;
std::unique_ptr<nfc::NfcTag> tag;
bool trig_called;
};
class PN7150 : public Component {
public:
void setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void loop() override;
void set_irq_pin(GPIOPin *irq_pin) { this->irq_pin_ = irq_pin; }
void set_ven_pin(GPIOPin *ven_pin) { this->ven_pin_ = ven_pin; }
void set_tag_ttl(uint32_t ttl) { this->tag_ttl_ = ttl; }
void set_tag_emulation_message(std::shared_ptr<nfc::NdefMessage> message);
void set_tag_emulation_message(const optional<std::string> &message, optional<bool> include_android_app_record);
void set_tag_emulation_message(const char *message, bool include_android_app_record = true);
void set_tag_emulation_off();
void set_tag_emulation_on();
bool tag_emulation_enabled() { return this->listening_enabled_; }
void set_polling_off();
void set_polling_on();
bool polling_enabled() { return this->polling_enabled_; }
void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); }
void register_ontagremoved_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontagremoved_.push_back(trig); }
void add_on_emulated_tag_scan_callback(std::function<void()> callback) {
this->on_emulated_tag_scan_callback_.add(std::move(callback));
}
void add_on_finished_write_callback(std::function<void()> callback) {
this->on_finished_write_callback_.add(std::move(callback));
}
bool is_writing() { return this->next_task_ != EP_READ; };
void read_mode();
void clean_mode();
void format_mode();
void write_mode();
void set_tag_write_message(std::shared_ptr<nfc::NdefMessage> message);
void set_tag_write_message(optional<std::string> message, optional<bool> include_android_app_record);
uint8_t set_test_mode(TestMode test_mode, const std::vector<uint8_t> &data, std::vector<uint8_t> &result);
protected:
uint8_t reset_core_(bool reset_config, bool power);
uint8_t init_core_();
uint8_t send_init_config_();
uint8_t send_core_config_();
uint8_t refresh_core_config_();
uint8_t set_discover_map_();
uint8_t set_listen_mode_routing_();
uint8_t start_discovery_();
uint8_t stop_discovery_();
uint8_t deactivate_(uint8_t type, uint16_t timeout = NFCC_DEFAULT_TIMEOUT);
void select_endpoint_();
uint8_t read_endpoint_data_(nfc::NfcTag &tag);
uint8_t clean_endpoint_(std::vector<uint8_t> &uid);
uint8_t format_endpoint_(std::vector<uint8_t> &uid);
uint8_t write_endpoint_(std::vector<uint8_t> &uid, std::shared_ptr<nfc::NdefMessage> &message);
std::unique_ptr<nfc::NfcTag> build_tag_(uint8_t mode_tech, const std::vector<uint8_t> &data);
optional<size_t> find_tag_uid_(const std::vector<uint8_t> &uid);
void purge_old_tags_();
void erase_tag_(uint8_t tag_index);
/// advance controller state as required
void nci_fsm_transition_();
/// set new controller state
void nci_fsm_set_state_(NCIState new_state);
/// setting controller to this state caused an error; returns true if too many errors/failures
bool nci_fsm_set_error_state_(NCIState new_state);
/// parse & process incoming messages from the NFCC
void process_message_();
void process_rf_intf_activated_oid_(nfc::NciMessage &rx);
void process_rf_discover_oid_(nfc::NciMessage &rx);
void process_rf_deactivate_oid_(nfc::NciMessage &rx);
void process_data_message_(nfc::NciMessage &rx);
void card_emu_t4t_get_response_(std::vector<uint8_t> &response, std::vector<uint8_t> &ndef_response);
uint8_t transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, uint16_t timeout = NFCC_DEFAULT_TIMEOUT,
bool expect_notification = true);
virtual uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) = 0;
virtual uint8_t write_nfcc(nfc::NciMessage &tx) = 0;
uint8_t wait_for_irq_(uint16_t timeout = NFCC_DEFAULT_TIMEOUT, bool pin_state = true);
uint8_t read_mifare_classic_tag_(nfc::NfcTag &tag);
uint8_t read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data);
uint8_t write_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data);
uint8_t auth_mifare_classic_block_(uint8_t block_num, uint8_t key_num, const uint8_t *key);
uint8_t sect_to_auth_(uint8_t block_num);
uint8_t format_mifare_classic_mifare_();
uint8_t format_mifare_classic_ndef_();
uint8_t write_mifare_classic_tag_(const std::shared_ptr<nfc::NdefMessage> &message);
uint8_t halt_mifare_classic_tag_();
uint8_t read_mifare_ultralight_tag_(nfc::NfcTag &tag);
uint8_t read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector<uint8_t> &data);
bool is_mifare_ultralight_formatted_(const std::vector<uint8_t> &page_3_to_6);
uint16_t read_mifare_ultralight_capacity_();
uint8_t find_mifare_ultralight_ndef_(const std::vector<uint8_t> &page_3_to_6, uint8_t &message_length,
uint8_t &message_start_index);
uint8_t write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &write_data);
uint8_t write_mifare_ultralight_tag_(std::vector<uint8_t> &uid, const std::shared_ptr<nfc::NdefMessage> &message);
uint8_t clean_mifare_ultralight_();
enum NfcTask : uint8_t {
EP_READ = 0,
EP_CLEAN,
EP_FORMAT,
EP_WRITE,
} next_task_{EP_READ};
bool config_refresh_pending_{false};
bool core_config_is_solo_{false};
bool listening_enabled_{false};
bool polling_enabled_{true};
uint8_t error_count_{0};
uint8_t fail_count_{0};
uint32_t last_nci_state_change_{0};
uint8_t selecting_endpoint_{0};
uint32_t tag_ttl_{250};
GPIOPin *irq_pin_{nullptr};
GPIOPin *ven_pin_{nullptr};
CallbackManager<void()> on_emulated_tag_scan_callback_;
CallbackManager<void()> on_finished_write_callback_;
std::vector<DiscoveredEndpoint> discovered_endpoint_;
CardEmulationState ce_state_{CardEmulationState::CARD_EMU_IDLE};
NCIState nci_state_{NCIState::NFCC_RESET};
NCIState nci_state_error_{NCIState::NONE};
std::shared_ptr<nfc::NdefMessage> card_emulation_message_;
std::shared_ptr<nfc::NdefMessage> next_task_message_to_write_;
std::vector<nfc::NfcOnTagTrigger *> triggers_ontag_;
std::vector<nfc::NfcOnTagTrigger *> triggers_ontagremoved_;
};
} // namespace pn7150
} // namespace esphome

View File

@ -0,0 +1,322 @@
#include <memory>
#include "pn7150.h"
#include "esphome/core/log.h"
namespace esphome {
namespace pn7150 {
static const char *const TAG = "pn7150.mifare_classic";
uint8_t PN7150::read_mifare_classic_tag_(nfc::NfcTag &tag) {
uint8_t current_block = 4;
uint8_t message_start_index = 0;
uint32_t message_length = 0;
if (this->auth_mifare_classic_block_(current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Tag auth failed while attempting to read tag data");
return nfc::STATUS_FAILED;
}
std::vector<uint8_t> data;
if (this->read_mifare_classic_block_(current_block, data) == nfc::STATUS_OK) {
if (!nfc::decode_mifare_classic_tlv(data, message_length, message_start_index)) {
return nfc::STATUS_FAILED;
}
} else {
ESP_LOGE(TAG, "Failed to read block %u", current_block);
return nfc::STATUS_FAILED;
}
uint32_t index = 0;
uint32_t buffer_size = nfc::get_mifare_classic_buffer_size(message_length);
std::vector<uint8_t> buffer;
while (index < buffer_size) {
if (nfc::mifare_classic_is_first_block(current_block)) {
if (this->auth_mifare_classic_block_(current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Block authentication failed for %u", current_block);
return nfc::STATUS_FAILED;
}
}
std::vector<uint8_t> block_data;
if (this->read_mifare_classic_block_(current_block, block_data) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error reading block %u", current_block);
return nfc::STATUS_FAILED;
} else {
buffer.insert(buffer.end(), block_data.begin(), block_data.end());
}
index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
current_block++;
if (nfc::mifare_classic_is_trailer_block(current_block)) {
current_block++;
}
}
if (buffer.begin() + message_start_index < buffer.end()) {
buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
} else {
return nfc::STATUS_FAILED;
}
tag.set_ndef_message(make_unique<nfc::NdefMessage>(buffer));
return nfc::STATUS_OK;
}
uint8_t PN7150::read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data) {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_READ, block_num});
ESP_LOGVV(TAG, "Read XCHG_DATA_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Timeout reading tag data");
return nfc::STATUS_FAILED;
}
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(XCHG_DATA_OID)) ||
(!rx.message_length_is(18))) {
ESP_LOGE(TAG, "MFC read block failed - block 0x%02x", block_num);
ESP_LOGV(TAG, "Read response: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
data.insert(data.begin(), rx.get_message().begin() + 4, rx.get_message().end() - 1);
ESP_LOGVV(TAG, " Block %u: %s", block_num, nfc::format_bytes(data).c_str());
return nfc::STATUS_OK;
}
uint8_t PN7150::auth_mifare_classic_block_(uint8_t block_num, uint8_t key_num, const uint8_t *key) {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {MFC_AUTHENTICATE_OID, this->sect_to_auth_(block_num), key_num});
switch (key_num) {
case nfc::MIFARE_CMD_AUTH_A:
tx.get_message().back() = MFC_AUTHENTICATE_PARAM_KS_A;
break;
case nfc::MIFARE_CMD_AUTH_B:
tx.get_message().back() = MFC_AUTHENTICATE_PARAM_KS_B;
break;
default:
break;
}
if (key != nullptr) {
tx.get_message().back() |= MFC_AUTHENTICATE_PARAM_EMBED_KEY;
tx.get_message().insert(tx.get_message().end(), key, key + 6);
}
ESP_LOGVV(TAG, "MFC_AUTHENTICATE_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending MFC_AUTHENTICATE_REQ failed");
return nfc::STATUS_FAILED;
}
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(MFC_AUTHENTICATE_OID)) ||
(rx.get_message()[4] != nfc::STATUS_OK)) {
ESP_LOGE(TAG, "MFC authentication failed - block 0x%02x", block_num);
ESP_LOGVV(TAG, "MFC_AUTHENTICATE_RSP: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
ESP_LOGV(TAG, "MFC block %u authentication succeeded", block_num);
return nfc::STATUS_OK;
}
uint8_t PN7150::sect_to_auth_(const uint8_t block_num) {
const uint8_t first_high_block = nfc::MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * nfc::MIFARE_CLASSIC_16BLOCK_SECT_START;
if (block_num >= first_high_block) {
return ((block_num - first_high_block) / nfc::MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH) +
nfc::MIFARE_CLASSIC_16BLOCK_SECT_START;
}
return block_num / nfc::MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW;
}
uint8_t PN7150::format_mifare_classic_mifare_() {
std::vector<uint8_t> blank_buffer(
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
std::vector<uint8_t> trailer_buffer(
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
auto status = nfc::STATUS_OK;
for (int block = 0; block < 64; block += 4) {
if (this->auth_mifare_classic_block_(block + 3, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY) != nfc::STATUS_OK) {
continue;
}
if (block != 0) {
if (this->write_mifare_classic_block_(block, blank_buffer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block);
status = nfc::STATUS_FAILED;
}
}
if (this->write_mifare_classic_block_(block + 1, blank_buffer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 1);
status = nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(block + 2, blank_buffer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 2);
status = nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(block + 3, trailer_buffer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 3);
status = nfc::STATUS_FAILED;
}
}
return status;
}
uint8_t PN7150::format_mifare_classic_ndef_() {
std::vector<uint8_t> empty_ndef_message(
{0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
std::vector<uint8_t> blank_block(
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
std::vector<uint8_t> block_1_data(
{0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
std::vector<uint8_t> block_2_data(
{0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
std::vector<uint8_t> block_3_trailer(
{0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
std::vector<uint8_t> ndef_trailer(
{0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
if (this->auth_mifare_classic_block_(0, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to authenticate block 0 for formatting");
return nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(1, block_1_data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(2, block_2_data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(3, block_3_trailer) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
ESP_LOGD(TAG, "Sector 0 formatted with NDEF");
auto status = nfc::STATUS_OK;
for (int block = 4; block < 64; block += 4) {
if (this->auth_mifare_classic_block_(block + 3, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
if (block == 4) {
if (this->write_mifare_classic_block_(block, empty_ndef_message) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block);
status = nfc::STATUS_FAILED;
}
} else {
if (this->write_mifare_classic_block_(block, blank_block) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block);
status = nfc::STATUS_FAILED;
}
}
if (this->write_mifare_classic_block_(block + 1, blank_block) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 1);
status = nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(block + 2, blank_block) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 2);
status = nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(block + 3, ndef_trailer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write trailer block %u", block + 3);
status = nfc::STATUS_FAILED;
}
}
return status;
}
uint8_t PN7150::write_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &write_data) {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_WRITE, block_num});
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 1: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending XCHG_DATA_REQ failed");
return nfc::STATUS_FAILED;
}
// write command part two
tx.set_payload({XCHG_DATA_OID});
tx.get_message().insert(tx.get_message().end(), write_data.begin(), write_data.end());
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 2: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "MFC XCHG_DATA timed out waiting for XCHG_DATA_RSP during block write");
return nfc::STATUS_FAILED;
}
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(XCHG_DATA_OID)) ||
(rx.get_message()[4] != nfc::MIFARE_CMD_ACK)) {
ESP_LOGE(TAG, "MFC write block failed - block 0x%02x", block_num);
ESP_LOGV(TAG, "Write response: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7150::write_mifare_classic_tag_(const std::shared_ptr<nfc::NdefMessage> &message) {
auto encoded = message->encode();
uint32_t message_length = encoded.size();
uint32_t buffer_length = nfc::get_mifare_classic_buffer_size(message_length);
encoded.insert(encoded.begin(), 0x03);
if (message_length < 255) {
encoded.insert(encoded.begin() + 1, message_length);
} else {
encoded.insert(encoded.begin() + 1, 0xFF);
encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
encoded.insert(encoded.begin() + 3, message_length & 0xFF);
}
encoded.push_back(0xFE);
encoded.resize(buffer_length, 0);
uint32_t index = 0;
uint8_t current_block = 4;
while (index < buffer_length) {
if (nfc::mifare_classic_is_first_block(current_block)) {
if (this->auth_mifare_classic_block_(current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
}
std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_CLASSIC_BLOCK_SIZE);
if (this->write_mifare_classic_block_(current_block, data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
current_block++;
if (nfc::mifare_classic_is_trailer_block(current_block)) {
// Skipping as cannot write to trailer
current_block++;
}
}
return nfc::STATUS_OK;
}
uint8_t PN7150::halt_mifare_classic_tag_() {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_HALT, 0});
ESP_LOGVV(TAG, "Halt XCHG_DATA_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending halt XCHG_DATA_REQ failed");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
} // namespace pn7150
} // namespace esphome

View File

@ -0,0 +1,186 @@
#include <cinttypes>
#include <memory>
#include "pn7150.h"
#include "esphome/core/log.h"
namespace esphome {
namespace pn7150 {
static const char *const TAG = "pn7150.mifare_ultralight";
uint8_t PN7150::read_mifare_ultralight_tag_(nfc::NfcTag &tag) {
std::vector<uint8_t> data;
// pages 3 to 6 contain various info we are interested in -- do one read to grab it all
if (this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE * nfc::MIFARE_ULTRALIGHT_READ_SIZE,
data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
if (!this->is_mifare_ultralight_formatted_(data)) {
ESP_LOGW(TAG, "Not NDEF formatted");
return nfc::STATUS_FAILED;
}
uint8_t message_length;
uint8_t message_start_index;
if (this->find_mifare_ultralight_ndef_(data, message_length, message_start_index) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "Couldn't find NDEF message");
return nfc::STATUS_FAILED;
}
ESP_LOGVV(TAG, "NDEF message length: %u, start: %u", message_length, message_start_index);
if (message_length == 0) {
return nfc::STATUS_FAILED;
}
// we already read pages 3-6 earlier -- pick up where we left off so we're not re-reading pages
const uint8_t read_length = message_length + message_start_index > 12 ? message_length + message_start_index - 12 : 0;
if (read_length) {
if (read_mifare_ultralight_bytes_(nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE + 3, read_length, data) !=
nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error reading tag data");
return nfc::STATUS_FAILED;
}
}
// we need to trim off page 3 as well as any bytes ahead of message_start_index
data.erase(data.begin(), data.begin() + message_start_index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
tag.set_ndef_message(make_unique<nfc::NdefMessage>(data));
return nfc::STATUS_OK;
}
uint8_t PN7150::read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector<uint8_t> &data) {
const uint8_t read_increment = nfc::MIFARE_ULTRALIGHT_READ_SIZE * nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {nfc::MIFARE_CMD_READ, start_page});
for (size_t i = 0; i * read_increment < num_bytes; i++) {
tx.get_message().back() = i * nfc::MIFARE_ULTRALIGHT_READ_SIZE + start_page;
do { // loop because sometimes we struggle here...???...
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error reading tag data");
return nfc::STATUS_FAILED;
}
} while (rx.get_payload_size() < read_increment);
uint16_t bytes_offset = (i + 1) * read_increment;
auto pages_in_end_itr = bytes_offset <= num_bytes ? rx.get_message().end() - 1
: rx.get_message().end() - (bytes_offset - num_bytes + 1);
if ((pages_in_end_itr > rx.get_message().begin()) && (pages_in_end_itr < rx.get_message().end())) {
data.insert(data.end(), rx.get_message().begin() + nfc::NCI_PKT_HEADER_SIZE, pages_in_end_itr);
}
}
ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes(data).c_str());
return nfc::STATUS_OK;
}
bool PN7150::is_mifare_ultralight_formatted_(const std::vector<uint8_t> &page_3_to_6) {
const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector
return (page_3_to_6.size() > p4_offset + 3) &&
!((page_3_to_6[p4_offset + 0] == 0xFF) && (page_3_to_6[p4_offset + 1] == 0xFF) &&
(page_3_to_6[p4_offset + 2] == 0xFF) && (page_3_to_6[p4_offset + 3] == 0xFF));
}
uint16_t PN7150::read_mifare_ultralight_capacity_() {
std::vector<uint8_t> data;
if (this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE, data) == nfc::STATUS_OK) {
ESP_LOGV(TAG, "Tag capacity is %u bytes", data[2] * 8U);
return data[2] * 8U;
}
return 0;
}
uint8_t PN7150::find_mifare_ultralight_ndef_(const std::vector<uint8_t> &page_3_to_6, uint8_t &message_length,
uint8_t &message_start_index) {
const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector
if (!(page_3_to_6.size() > p4_offset + 5)) {
return nfc::STATUS_FAILED;
}
if (page_3_to_6[p4_offset + 0] == 0x03) {
message_length = page_3_to_6[p4_offset + 1];
message_start_index = 2;
return nfc::STATUS_OK;
} else if (page_3_to_6[p4_offset + 5] == 0x03) {
message_length = page_3_to_6[p4_offset + 6];
message_start_index = 7;
return nfc::STATUS_OK;
}
return nfc::STATUS_FAILED;
}
uint8_t PN7150::write_mifare_ultralight_tag_(std::vector<uint8_t> &uid,
const std::shared_ptr<nfc::NdefMessage> &message) {
uint32_t capacity = this->read_mifare_ultralight_capacity_();
auto encoded = message->encode();
uint32_t message_length = encoded.size();
uint32_t buffer_length = nfc::get_mifare_ultralight_buffer_size(message_length);
if (buffer_length > capacity) {
ESP_LOGE(TAG, "Message length exceeds tag capacity %" PRIu32 " > %" PRIu32, buffer_length, capacity);
return nfc::STATUS_FAILED;
}
encoded.insert(encoded.begin(), 0x03);
if (message_length < 255) {
encoded.insert(encoded.begin() + 1, message_length);
} else {
encoded.insert(encoded.begin() + 1, 0xFF);
encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
encoded.insert(encoded.begin() + 2, message_length & 0xFF);
}
encoded.push_back(0xFE);
encoded.resize(buffer_length, 0);
uint32_t index = 0;
uint8_t current_page = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
while (index < buffer_length) {
std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
if (this->write_mifare_ultralight_page_(current_page, data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
index += nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
current_page++;
}
return nfc::STATUS_OK;
}
uint8_t PN7150::clean_mifare_ultralight_() {
uint32_t capacity = this->read_mifare_ultralight_capacity_();
uint8_t pages = (capacity / nfc::MIFARE_ULTRALIGHT_PAGE_SIZE) + nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
std::vector<uint8_t> blank_data = {0x00, 0x00, 0x00, 0x00};
for (int i = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; i < pages; i++) {
if (this->write_mifare_ultralight_page_(i, blank_data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
}
return nfc::STATUS_OK;
}
uint8_t PN7150::write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &write_data) {
std::vector<uint8_t> payload = {nfc::MIFARE_CMD_WRITE_ULTRALIGHT, page_num};
payload.insert(payload.end(), write_data.begin(), write_data.end());
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, payload);
if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error writing page %u", page_num);
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
} // namespace pn7150
} // namespace esphome

View File

@ -0,0 +1,25 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, pn7150
from esphome.const import CONF_ID
AUTO_LOAD = ["pn7150"]
CODEOWNERS = ["@kbx81", "@jesserockz"]
DEPENDENCIES = ["i2c"]
pn7150_i2c_ns = cg.esphome_ns.namespace("pn7150_i2c")
PN7150I2C = pn7150_i2c_ns.class_("PN7150I2C", pn7150.PN7150, i2c.I2CDevice)
CONFIG_SCHEMA = cv.All(
pn7150.PN7150_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(PN7150I2C),
}
).extend(i2c.i2c_device_schema(0x28))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await pn7150.setup_pn7150(var, config)
await i2c.register_i2c_device(var, config)

View File

@ -0,0 +1,49 @@
#include "pn7150_i2c.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace pn7150_i2c {
static const char *const TAG = "pn7150_i2c";
uint8_t PN7150I2C::read_nfcc(nfc::NciMessage &rx, const uint16_t timeout) {
if (this->wait_for_irq_(timeout) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "read_nfcc_() timeout waiting for IRQ");
return nfc::STATUS_FAILED;
}
rx.get_message().resize(nfc::NCI_PKT_HEADER_SIZE);
if (!this->read_bytes_raw(rx.get_message().data(), nfc::NCI_PKT_HEADER_SIZE)) {
return nfc::STATUS_FAILED;
}
uint8_t length = rx.get_payload_size();
if (length > 0) {
rx.get_message().resize(length + nfc::NCI_PKT_HEADER_SIZE);
if (!this->read_bytes_raw(rx.get_message().data() + nfc::NCI_PKT_HEADER_SIZE, length)) {
return nfc::STATUS_FAILED;
}
}
// semaphore to ensure transaction is complete before returning
if (this->wait_for_irq_(pn7150::NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "read_nfcc_() post-read timeout waiting for IRQ line to clear");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7150I2C::write_nfcc(nfc::NciMessage &tx) {
if (this->write(tx.encode().data(), tx.encode().size()) == i2c::ERROR_OK) {
return nfc::STATUS_OK;
}
return nfc::STATUS_FAILED;
}
void PN7150I2C::dump_config() {
PN7150::dump_config();
LOG_I2C_DEVICE(this);
}
} // namespace pn7150_i2c
} // namespace esphome

View File

@ -0,0 +1,22 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/pn7150/pn7150.h"
#include "esphome/components/i2c/i2c.h"
#include <vector>
namespace esphome {
namespace pn7150_i2c {
class PN7150I2C : public pn7150::PN7150, public i2c::I2CDevice {
public:
void dump_config() override;
protected:
uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) override;
uint8_t write_nfcc(nfc::NciMessage &tx) override;
};
} // namespace pn7150_i2c
} // namespace esphome

View File

@ -0,0 +1,227 @@
from esphome import automation, pins
from esphome.automation import maybe_simple_id
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import nfc
from esphome.const import (
CONF_ID,
CONF_IRQ_PIN,
CONF_ON_TAG_REMOVED,
CONF_ON_TAG,
CONF_TRIGGER_ID,
)
AUTO_LOAD = ["binary_sensor", "nfc"]
CODEOWNERS = ["@kbx81", "@jesserockz"]
CONF_DWL_REQ_PIN = "dwl_req_pin"
CONF_EMULATION_MESSAGE = "emulation_message"
CONF_EMULATION_OFF = "emulation_off"
CONF_EMULATION_ON = "emulation_on"
CONF_INCLUDE_ANDROID_APP_RECORD = "include_android_app_record"
CONF_MESSAGE = "message"
CONF_ON_FINISHED_WRITE = "on_finished_write"
CONF_ON_EMULATED_TAG_SCAN = "on_emulated_tag_scan"
CONF_PN7160_ID = "pn7160_id"
CONF_POLLING_OFF = "polling_off"
CONF_POLLING_ON = "polling_on"
CONF_SET_CLEAN_MODE = "set_clean_mode"
CONF_SET_EMULATION_MESSAGE = "set_emulation_message"
CONF_SET_FORMAT_MODE = "set_format_mode"
CONF_SET_READ_MODE = "set_read_mode"
CONF_SET_WRITE_MESSAGE = "set_write_message"
CONF_SET_WRITE_MODE = "set_write_mode"
CONF_TAG_TTL = "tag_ttl"
CONF_VEN_PIN = "ven_pin"
CONF_WKUP_REQ_PIN = "wkup_req_pin"
pn7160_ns = cg.esphome_ns.namespace("pn7160")
PN7160 = pn7160_ns.class_("PN7160", cg.Component)
EmulationOffAction = pn7160_ns.class_("EmulationOffAction", automation.Action)
EmulationOnAction = pn7160_ns.class_("EmulationOnAction", automation.Action)
PollingOffAction = pn7160_ns.class_("PollingOffAction", automation.Action)
PollingOnAction = pn7160_ns.class_("PollingOnAction", automation.Action)
SetCleanModeAction = pn7160_ns.class_("SetCleanModeAction", automation.Action)
SetEmulationMessageAction = pn7160_ns.class_(
"SetEmulationMessageAction", automation.Action
)
SetFormatModeAction = pn7160_ns.class_("SetFormatModeAction", automation.Action)
SetReadModeAction = pn7160_ns.class_("SetReadModeAction", automation.Action)
SetWriteMessageAction = pn7160_ns.class_("SetWriteMessageAction", automation.Action)
SetWriteModeAction = pn7160_ns.class_("SetWriteModeAction", automation.Action)
PN7160OnEmulatedTagScanTrigger = pn7160_ns.class_(
"PN7160OnEmulatedTagScanTrigger", automation.Trigger.template()
)
PN7160OnFinishedWriteTrigger = pn7160_ns.class_(
"PN7160OnFinishedWriteTrigger", automation.Trigger.template()
)
PN7160IsWritingCondition = pn7160_ns.class_(
"PN7160IsWritingCondition", automation.Condition
)
IsWritingCondition = nfc.nfc_ns.class_("IsWritingCondition", automation.Condition)
SIMPLE_ACTION_SCHEMA = maybe_simple_id(
{
cv.Required(CONF_ID): cv.use_id(PN7160),
}
)
SET_MESSAGE_ACTION_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.use_id(PN7160),
cv.Required(CONF_MESSAGE): cv.templatable(cv.string),
cv.Optional(CONF_INCLUDE_ANDROID_APP_RECORD, default=True): cv.boolean,
}
)
PN7160_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(PN7160),
cv.Optional(CONF_ON_EMULATED_TAG_SCAN): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
PN7160OnEmulatedTagScanTrigger
),
}
),
cv.Optional(CONF_ON_FINISHED_WRITE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
PN7160OnFinishedWriteTrigger
),
}
),
cv.Optional(CONF_ON_TAG): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(nfc.NfcOnTagTrigger),
}
),
cv.Optional(CONF_ON_TAG_REMOVED): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(nfc.NfcOnTagTrigger),
}
),
cv.Optional(CONF_DWL_REQ_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_IRQ_PIN): pins.gpio_input_pin_schema,
cv.Required(CONF_VEN_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_WKUP_REQ_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_EMULATION_MESSAGE): cv.string,
cv.Optional(CONF_TAG_TTL): cv.positive_time_period_milliseconds,
}
).extend(cv.COMPONENT_SCHEMA)
@automation.register_action(
"tag.set_emulation_message",
SetEmulationMessageAction,
SET_MESSAGE_ACTION_SCHEMA,
)
@automation.register_action(
"tag.set_write_message",
SetWriteMessageAction,
SET_MESSAGE_ACTION_SCHEMA,
)
async def pn7160_set_message_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_MESSAGE], args, cg.std_string)
cg.add(var.set_message(template_))
template_ = await cg.templatable(
config[CONF_INCLUDE_ANDROID_APP_RECORD], args, cg.bool_
)
cg.add(var.set_include_android_app_record(template_))
return var
@automation.register_action(
"tag.emulation_off", EmulationOffAction, SIMPLE_ACTION_SCHEMA
)
@automation.register_action("tag.emulation_on", EmulationOnAction, SIMPLE_ACTION_SCHEMA)
@automation.register_action("tag.polling_off", PollingOffAction, SIMPLE_ACTION_SCHEMA)
@automation.register_action("tag.polling_on", PollingOnAction, SIMPLE_ACTION_SCHEMA)
@automation.register_action(
"tag.set_clean_mode", SetCleanModeAction, SIMPLE_ACTION_SCHEMA
)
@automation.register_action(
"tag.set_format_mode", SetFormatModeAction, SIMPLE_ACTION_SCHEMA
)
@automation.register_action(
"tag.set_read_mode", SetReadModeAction, SIMPLE_ACTION_SCHEMA
)
@automation.register_action(
"tag.set_write_mode", SetWriteModeAction, SIMPLE_ACTION_SCHEMA
)
async def pn7160_simple_action_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
async def setup_pn7160(var, config):
await cg.register_component(var, config)
if dwl_req_pin_config := config.get(CONF_DWL_REQ_PIN):
pin = await cg.gpio_pin_expression(dwl_req_pin_config)
cg.add(var.set_dwl_req_pin(pin))
pin = await cg.gpio_pin_expression(config[CONF_IRQ_PIN])
cg.add(var.set_irq_pin(pin))
pin = await cg.gpio_pin_expression(config[CONF_VEN_PIN])
cg.add(var.set_ven_pin(pin))
if wakeup_req_pin_config := config.get(CONF_WKUP_REQ_PIN):
pin = await cg.gpio_pin_expression(wakeup_req_pin_config)
cg.add(var.set_wkup_req_pin(pin))
if emulation_message_config := config.get(CONF_EMULATION_MESSAGE):
cg.add(var.set_tag_emulation_message(emulation_message_config))
cg.add(var.set_tag_emulation_on())
if CONF_TAG_TTL in config:
cg.add(var.set_tag_ttl(config[CONF_TAG_TTL]))
for conf in config.get(CONF_ON_TAG, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
cg.add(var.register_ontag_trigger(trigger))
await automation.build_automation(
trigger, [(cg.std_string, "x"), (nfc.NfcTag, "tag")], conf
)
for conf in config.get(CONF_ON_TAG_REMOVED, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
cg.add(var.register_ontagremoved_trigger(trigger))
await automation.build_automation(
trigger, [(cg.std_string, "x"), (nfc.NfcTag, "tag")], conf
)
for conf in config.get(CONF_ON_EMULATED_TAG_SCAN, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_FINISHED_WRITE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf)
@automation.register_condition(
"pn7160.is_writing",
PN7160IsWritingCondition,
cv.Schema(
{
cv.GenerateID(): cv.use_id(PN7160),
}
),
)
async def pn7160_is_writing_to_code(config, condition_id, template_arg, args):
var = cg.new_Pvariable(condition_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var

View File

@ -0,0 +1,82 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/components/pn7160/pn7160.h"
namespace esphome {
namespace pn7160 {
class PN7160OnEmulatedTagScanTrigger : public Trigger<> {
public:
explicit PN7160OnEmulatedTagScanTrigger(PN7160 *parent) {
parent->add_on_emulated_tag_scan_callback([this]() { this->trigger(); });
}
};
class PN7160OnFinishedWriteTrigger : public Trigger<> {
public:
explicit PN7160OnFinishedWriteTrigger(PN7160 *parent) {
parent->add_on_finished_write_callback([this]() { this->trigger(); });
}
};
template<typename... Ts> class PN7160IsWritingCondition : public Condition<Ts...>, public Parented<PN7160> {
public:
bool check(Ts... x) override { return this->parent_->is_writing(); }
};
template<typename... Ts> class EmulationOffAction : public Action<Ts...>, public Parented<PN7160> {
void play(Ts... x) override { this->parent_->set_tag_emulation_off(); }
};
template<typename... Ts> class EmulationOnAction : public Action<Ts...>, public Parented<PN7160> {
void play(Ts... x) override { this->parent_->set_tag_emulation_on(); }
};
template<typename... Ts> class PollingOffAction : public Action<Ts...>, public Parented<PN7160> {
void play(Ts... x) override { this->parent_->set_polling_off(); }
};
template<typename... Ts> class PollingOnAction : public Action<Ts...>, public Parented<PN7160> {
void play(Ts... x) override { this->parent_->set_polling_on(); }
};
template<typename... Ts> class SetCleanModeAction : public Action<Ts...>, public Parented<PN7160> {
void play(Ts... x) override { this->parent_->clean_mode(); }
};
template<typename... Ts> class SetFormatModeAction : public Action<Ts...>, public Parented<PN7160> {
void play(Ts... x) override { this->parent_->format_mode(); }
};
template<typename... Ts> class SetReadModeAction : public Action<Ts...>, public Parented<PN7160> {
void play(Ts... x) override { this->parent_->read_mode(); }
};
template<typename... Ts> class SetEmulationMessageAction : public Action<Ts...>, public Parented<PN7160> {
TEMPLATABLE_VALUE(std::string, message)
TEMPLATABLE_VALUE(bool, include_android_app_record)
void play(Ts... x) override {
this->parent_->set_tag_emulation_message(this->message_.optional_value(x...),
this->include_android_app_record_.optional_value(x...));
}
};
template<typename... Ts> class SetWriteMessageAction : public Action<Ts...>, public Parented<PN7160> {
TEMPLATABLE_VALUE(std::string, message)
TEMPLATABLE_VALUE(bool, include_android_app_record)
void play(Ts... x) override {
this->parent_->set_tag_write_message(this->message_.optional_value(x...),
this->include_android_app_record_.optional_value(x...));
}
};
template<typename... Ts> class SetWriteModeAction : public Action<Ts...>, public Parented<PN7160> {
void play(Ts... x) override { this->parent_->write_mode(); }
};
} // namespace pn7160
} // namespace esphome

View File

@ -0,0 +1,1161 @@
#include <utility>
#include "automation.h"
#include "pn7160.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
namespace pn7160 {
static const char *const TAG = "pn7160";
void PN7160::setup() {
this->irq_pin_->setup();
this->ven_pin_->setup();
if (this->dwl_req_pin_ != nullptr) {
this->dwl_req_pin_->setup();
}
if (this->wkup_req_pin_ != nullptr) {
this->wkup_req_pin_->setup();
}
this->nci_fsm_transition_(); // kick off reset & init processes
}
void PN7160::dump_config() {
ESP_LOGCONFIG(TAG, "PN7160:");
if (this->dwl_req_pin_ != nullptr) {
LOG_PIN(" DWL_REQ pin: ", this->dwl_req_pin_);
}
LOG_PIN(" IRQ pin: ", this->irq_pin_);
LOG_PIN(" VEN pin: ", this->ven_pin_);
if (this->wkup_req_pin_ != nullptr) {
LOG_PIN(" WKUP_REQ pin: ", this->wkup_req_pin_);
}
}
void PN7160::loop() {
this->nci_fsm_transition_();
this->purge_old_tags_();
}
void PN7160::set_tag_emulation_message(std::shared_ptr<nfc::NdefMessage> message) {
this->card_emulation_message_ = std::move(message);
ESP_LOGD(TAG, "Tag emulation message set");
}
void PN7160::set_tag_emulation_message(const optional<std::string> &message,
const optional<bool> include_android_app_record) {
if (!message.has_value()) {
return;
}
auto ndef_message = make_unique<nfc::NdefMessage>();
ndef_message->add_uri_record(message.value());
if (!include_android_app_record.has_value() || include_android_app_record.value()) {
auto ext_record = make_unique<nfc::NdefRecord>();
ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
ndef_message->add_record(std::move(ext_record));
}
this->card_emulation_message_ = std::move(ndef_message);
ESP_LOGD(TAG, "Tag emulation message set");
}
void PN7160::set_tag_emulation_message(const char *message, const bool include_android_app_record) {
this->set_tag_emulation_message(std::string(message), include_android_app_record);
}
void PN7160::set_tag_emulation_off() {
if (this->listening_enabled_) {
this->listening_enabled_ = false;
this->config_refresh_pending_ = true;
}
ESP_LOGD(TAG, "Tag emulation disabled");
}
void PN7160::set_tag_emulation_on() {
if (this->card_emulation_message_ == nullptr) {
ESP_LOGE(TAG, "No NDEF message is set; tag emulation cannot be enabled");
return;
}
if (!this->listening_enabled_) {
this->listening_enabled_ = true;
this->config_refresh_pending_ = true;
}
ESP_LOGD(TAG, "Tag emulation enabled");
}
void PN7160::set_polling_off() {
if (this->polling_enabled_) {
this->polling_enabled_ = false;
this->config_refresh_pending_ = true;
}
ESP_LOGD(TAG, "Tag polling disabled");
}
void PN7160::set_polling_on() {
if (!this->polling_enabled_) {
this->polling_enabled_ = true;
this->config_refresh_pending_ = true;
}
ESP_LOGD(TAG, "Tag polling enabled");
}
void PN7160::read_mode() {
this->next_task_ = EP_READ;
ESP_LOGD(TAG, "Waiting to read next tag");
}
void PN7160::clean_mode() {
this->next_task_ = EP_CLEAN;
ESP_LOGD(TAG, "Waiting to clean next tag");
}
void PN7160::format_mode() {
this->next_task_ = EP_FORMAT;
ESP_LOGD(TAG, "Waiting to format next tag");
}
void PN7160::write_mode() {
if (this->next_task_message_to_write_ == nullptr) {
ESP_LOGW(TAG, "Message to write must be set before setting write mode");
return;
}
this->next_task_ = EP_WRITE;
ESP_LOGD(TAG, "Waiting to write next tag");
}
void PN7160::set_tag_write_message(std::shared_ptr<nfc::NdefMessage> message) {
this->next_task_message_to_write_ = std::move(message);
ESP_LOGD(TAG, "Message to write has been set");
}
void PN7160::set_tag_write_message(optional<std::string> message, optional<bool> include_android_app_record) {
if (!message.has_value()) {
return;
}
auto ndef_message = make_unique<nfc::NdefMessage>();
ndef_message->add_uri_record(message.value());
if (!include_android_app_record.has_value() || include_android_app_record.value()) {
auto ext_record = make_unique<nfc::NdefRecord>();
ext_record->set_tnf(nfc::TNF_EXTERNAL_TYPE);
ext_record->set_type(nfc::HA_TAG_ID_EXT_RECORD_TYPE);
ext_record->set_payload(nfc::HA_TAG_ID_EXT_RECORD_PAYLOAD);
ndef_message->add_record(std::move(ext_record));
}
this->next_task_message_to_write_ = std::move(ndef_message);
ESP_LOGD(TAG, "Message to write has been set");
}
uint8_t PN7160::set_test_mode(const TestMode test_mode, const std::vector<uint8_t> &data,
std::vector<uint8_t> &result) {
auto test_oid = TEST_PRBS_OID;
switch (test_mode) {
case TestMode::TEST_PRBS:
// test_oid = TEST_PRBS_OID;
break;
case TestMode::TEST_ANTENNA:
test_oid = TEST_ANTENNA_OID;
break;
case TestMode::TEST_GET_REGISTER:
test_oid = TEST_GET_REGISTER_OID;
break;
case TestMode::TEST_NONE:
default:
ESP_LOGD(TAG, "Exiting test mode");
this->nci_fsm_set_state_(NCIState::NFCC_RESET);
return nfc::STATUS_OK;
}
if (this->reset_core_(true, true) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to reset NCI core");
this->nci_fsm_set_error_state_(NCIState::NFCC_RESET);
result.clear();
return nfc::STATUS_FAILED;
} else {
this->nci_fsm_set_state_(NCIState::NFCC_INIT);
}
if (this->init_core_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to initialise NCI core");
this->nci_fsm_set_error_state_(NCIState::NFCC_INIT);
result.clear();
return nfc::STATUS_FAILED;
} else {
this->nci_fsm_set_state_(NCIState::TEST);
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, test_oid, data);
ESP_LOGW(TAG, "Starting test mode, OID 0x%02X", test_oid);
auto status = this->transceive_(tx, rx, NFCC_INIT_TIMEOUT);
if (status != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to start test mode, OID 0x%02X", test_oid);
this->nci_fsm_set_state_(NCIState::NFCC_RESET);
result.clear();
} else {
result = rx.get_message();
result.erase(result.begin(), result.begin() + 4); // remove NCI header
if (!result.empty()) {
ESP_LOGW(TAG, "Test results: %s", nfc::format_bytes(result).c_str());
}
}
return status;
}
uint8_t PN7160::reset_core_(const bool reset_config, const bool power) {
if (this->dwl_req_pin_ != nullptr) {
this->dwl_req_pin_->digital_write(false);
delay(NFCC_DEFAULT_TIMEOUT);
}
if (power) {
this->ven_pin_->digital_write(true);
delay(NFCC_DEFAULT_TIMEOUT);
this->ven_pin_->digital_write(false);
delay(NFCC_DEFAULT_TIMEOUT);
this->ven_pin_->digital_write(true);
delay(NFCC_INIT_TIMEOUT);
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_RESET_OID,
{(uint8_t) reset_config});
if (this->transceive_(tx, rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending reset command");
return nfc::STATUS_FAILED;
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Invalid reset response: %s", nfc::format_bytes(rx.get_message()).c_str());
return rx.get_simple_status_response();
}
// read reset notification
if (this->read_nfcc(rx, NFCC_INIT_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Reset notification was not received");
return nfc::STATUS_FAILED;
}
// verify reset notification
if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.message_length_is(9)) ||
(rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET] != 0x02) ||
(rx.get_message()[nfc::NCI_PKT_PAYLOAD_OFFSET + 1] != (uint8_t) reset_config)) {
ESP_LOGE(TAG, "Reset notification was malformed: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
ESP_LOGD(TAG, "Configuration %s", rx.get_message()[4] ? "reset" : "retained");
ESP_LOGD(TAG, "NCI version: %s", rx.get_message()[5] == 0x20 ? "2.0" : "1.0");
ESP_LOGD(TAG, "Manufacturer ID: 0x%02X", rx.get_message()[6]);
rx.get_message().erase(rx.get_message().begin(), rx.get_message().begin() + 8);
ESP_LOGD(TAG, "Manufacturer info: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_OK;
}
uint8_t PN7160::init_core_() {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_INIT_OID);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending initialise command");
return nfc::STATUS_FAILED;
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Invalid initialise response: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
uint8_t hw_version = rx.get_message()[17 + rx.get_message()[8]];
uint8_t rom_code_version = rx.get_message()[18 + rx.get_message()[8]];
uint8_t flash_major_version = rx.get_message()[19 + rx.get_message()[8]];
uint8_t flash_minor_version = rx.get_message()[20 + rx.get_message()[8]];
std::vector<uint8_t> features(rx.get_message().begin() + 4, rx.get_message().begin() + 8);
ESP_LOGD(TAG, "Hardware version: %u", hw_version);
ESP_LOGD(TAG, "ROM code version: %u", rom_code_version);
ESP_LOGD(TAG, "FLASH major version: %u", flash_major_version);
ESP_LOGD(TAG, "FLASH minor version: %u", flash_minor_version);
ESP_LOGD(TAG, "Features: %s", nfc::format_bytes(features).c_str());
return rx.get_simple_status_response();
}
uint8_t PN7160::send_init_config_() {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_PROPRIETARY_GID, nfc::NCI_CORE_SET_CONFIG_OID);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error enabling proprietary extensions");
return nfc::STATUS_FAILED;
}
tx.set_message(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
std::vector<uint8_t>(std::begin(PMU_CFG), std::end(PMU_CFG)));
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending PMU config");
return nfc::STATUS_FAILED;
}
return this->send_core_config_();
}
uint8_t PN7160::send_core_config_() {
const auto *core_config_begin = std::begin(CORE_CONFIG_SOLO);
const auto *core_config_end = std::end(CORE_CONFIG_SOLO);
this->core_config_is_solo_ = true;
if (this->listening_enabled_ && this->polling_enabled_) {
core_config_begin = std::begin(CORE_CONFIG_RW_CE);
core_config_end = std::end(CORE_CONFIG_RW_CE);
this->core_config_is_solo_ = false;
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::NCI_CORE_GID, nfc::NCI_CORE_SET_CONFIG_OID,
std::vector<uint8_t>(core_config_begin, core_config_end));
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "Error sending core config");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7160::refresh_core_config_() {
bool core_config_should_be_solo = !(this->listening_enabled_ && this->polling_enabled_);
if (this->nci_state_ == NCIState::RFST_DISCOVERY) {
if (this->stop_discovery_() != nfc::STATUS_OK) {
this->nci_fsm_set_state_(NCIState::NFCC_RESET);
return nfc::STATUS_FAILED;
}
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
}
if (this->core_config_is_solo_ != core_config_should_be_solo) {
if (this->send_core_config_() != nfc::STATUS_OK) {
ESP_LOGV(TAG, "Failed to refresh core config");
return nfc::STATUS_FAILED;
}
}
this->config_refresh_pending_ = false;
return nfc::STATUS_OK;
}
uint8_t PN7160::set_discover_map_() {
std::vector<uint8_t> discover_map = {sizeof(RF_DISCOVER_MAP_CONFIG) / 3};
discover_map.insert(discover_map.end(), std::begin(RF_DISCOVER_MAP_CONFIG), std::end(RF_DISCOVER_MAP_CONFIG));
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_MAP_OID, discover_map);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending discover map poll config");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7160::set_listen_mode_routing_() {
nfc::NciMessage rx;
nfc::NciMessage tx(
nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_SET_LISTEN_MODE_ROUTING_OID,
std::vector<uint8_t>(std::begin(RF_LISTEN_MODE_ROUTING_CONFIG), std::end(RF_LISTEN_MODE_ROUTING_CONFIG)));
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error setting listen mode routing config");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7160::start_discovery_() {
const uint8_t *rf_discovery_config = RF_DISCOVERY_CONFIG;
uint8_t length = sizeof(RF_DISCOVERY_CONFIG);
if (!this->listening_enabled_) {
length = sizeof(RF_DISCOVERY_POLL_CONFIG);
rf_discovery_config = RF_DISCOVERY_POLL_CONFIG;
} else if (!this->polling_enabled_) {
length = sizeof(RF_DISCOVERY_LISTEN_CONFIG);
rf_discovery_config = RF_DISCOVERY_LISTEN_CONFIG;
}
std::vector<uint8_t> discover_config = std::vector<uint8_t>((length * 2) + 1);
discover_config[0] = length;
for (uint8_t i = 0; i < length; i++) {
discover_config[(i * 2) + 1] = rf_discovery_config[i];
discover_config[(i * 2) + 2] = 0x01; // RF Technology and Mode will be executed in every discovery period
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_OID, discover_config);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
switch (rx.get_simple_status_response()) {
// in any of these cases, we are either already in or will remain in discovery, which satisfies the function call
case nfc::STATUS_OK:
case nfc::DISCOVERY_ALREADY_STARTED:
case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
case nfc::DISCOVERY_TEAR_DOWN:
return nfc::STATUS_OK;
default:
ESP_LOGE(TAG, "Error starting discovery");
return nfc::STATUS_FAILED;
}
}
return nfc::STATUS_OK;
}
uint8_t PN7160::stop_discovery_() { return this->deactivate_(nfc::DEACTIVATION_TYPE_IDLE, NFCC_TAG_WRITE_TIMEOUT); }
uint8_t PN7160::deactivate_(const uint8_t type, const uint16_t timeout) {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DEACTIVATE_OID, {type});
auto status = this->transceive_(tx, rx, timeout);
// if (status != nfc::STATUS_OK) {
// ESP_LOGE(TAG, "Error sending deactivate type %u", type);
// return nfc::STATUS_FAILED;
// }
return status;
}
void PN7160::select_endpoint_() {
if (this->discovered_endpoint_.empty()) {
ESP_LOGW(TAG, "No cached tags to select");
this->stop_discovery_();
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
return;
}
std::vector<uint8_t> endpoint_data = {this->discovered_endpoint_[0].id, this->discovered_endpoint_[0].protocol,
0x01}; // that last byte is the interface ID
for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
if (!this->discovered_endpoint_[i].trig_called) {
endpoint_data = {this->discovered_endpoint_[i].id, this->discovered_endpoint_[i].protocol,
0x01}; // that last byte is the interface ID
this->selecting_endpoint_ = i;
break;
}
}
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_CTRL_COMMAND, nfc::RF_GID, nfc::RF_DISCOVER_SELECT_OID, endpoint_data);
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error selecting endpoint");
} else {
this->nci_fsm_set_state_(NCIState::EP_SELECTING);
}
}
uint8_t PN7160::read_endpoint_data_(nfc::NfcTag &tag) {
uint8_t type = nfc::guess_tag_type(tag.get_uid().size());
switch (type) {
case nfc::TAG_TYPE_MIFARE_CLASSIC:
ESP_LOGV(TAG, "Reading Mifare classic");
return this->read_mifare_classic_tag_(tag);
case nfc::TAG_TYPE_2:
ESP_LOGV(TAG, "Reading Mifare ultralight");
return this->read_mifare_ultralight_tag_(tag);
case nfc::TAG_TYPE_UNKNOWN:
default:
ESP_LOGV(TAG, "Cannot determine tag type");
break;
}
return nfc::STATUS_FAILED;
}
uint8_t PN7160::clean_endpoint_(std::vector<uint8_t> &uid) {
uint8_t type = nfc::guess_tag_type(uid.size());
switch (type) {
case nfc::TAG_TYPE_MIFARE_CLASSIC:
return this->format_mifare_classic_mifare_();
case nfc::TAG_TYPE_2:
return this->clean_mifare_ultralight_();
default:
ESP_LOGE(TAG, "Unsupported tag for cleaning");
break;
}
return nfc::STATUS_FAILED;
}
uint8_t PN7160::format_endpoint_(std::vector<uint8_t> &uid) {
uint8_t type = nfc::guess_tag_type(uid.size());
switch (type) {
case nfc::TAG_TYPE_MIFARE_CLASSIC:
return this->format_mifare_classic_ndef_();
case nfc::TAG_TYPE_2:
return this->clean_mifare_ultralight_();
default:
ESP_LOGE(TAG, "Unsupported tag for formatting");
break;
}
return nfc::STATUS_FAILED;
}
uint8_t PN7160::write_endpoint_(std::vector<uint8_t> &uid, std::shared_ptr<nfc::NdefMessage> &message) {
uint8_t type = nfc::guess_tag_type(uid.size());
switch (type) {
case nfc::TAG_TYPE_MIFARE_CLASSIC:
return this->write_mifare_classic_tag_(message);
case nfc::TAG_TYPE_2:
return this->write_mifare_ultralight_tag_(uid, message);
default:
ESP_LOGE(TAG, "Unsupported tag for writing");
break;
}
return nfc::STATUS_FAILED;
}
std::unique_ptr<nfc::NfcTag> PN7160::build_tag_(const uint8_t mode_tech, const std::vector<uint8_t> &data) {
switch (mode_tech) {
case (nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA): {
uint8_t uid_length = data[2];
if (!uid_length) {
ESP_LOGE(TAG, "UID length cannot be zero");
return nullptr;
}
std::vector<uint8_t> uid(data.begin() + 3, data.begin() + 3 + uid_length);
const auto *tag_type_str =
nfc::guess_tag_type(uid_length) == nfc::TAG_TYPE_MIFARE_CLASSIC ? nfc::MIFARE_CLASSIC : nfc::NFC_FORUM_TYPE_2;
return make_unique<nfc::NfcTag>(uid, tag_type_str);
}
}
return nullptr;
}
optional<size_t> PN7160::find_tag_uid_(const std::vector<uint8_t> &uid) {
if (!this->discovered_endpoint_.empty()) {
for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
auto existing_tag_uid = this->discovered_endpoint_[i].tag->get_uid();
bool uid_match = (uid.size() == existing_tag_uid.size());
if (uid_match) {
for (size_t i = 0; i < uid.size(); i++) {
uid_match &= (uid[i] == existing_tag_uid[i]);
}
if (uid_match) {
return i;
}
}
}
}
return nullopt;
}
void PN7160::purge_old_tags_() {
for (size_t i = 0; i < this->discovered_endpoint_.size(); i++) {
if (millis() - this->discovered_endpoint_[i].last_seen > this->tag_ttl_) {
this->erase_tag_(i);
}
}
}
void PN7160::erase_tag_(const uint8_t tag_index) {
if (tag_index < this->discovered_endpoint_.size()) {
for (auto *trigger : this->triggers_ontagremoved_) {
trigger->process(this->discovered_endpoint_[tag_index].tag);
}
ESP_LOGI(TAG, "Tag %s removed", nfc::format_uid(this->discovered_endpoint_[tag_index].tag->get_uid()).c_str());
this->discovered_endpoint_.erase(this->discovered_endpoint_.begin() + tag_index);
}
}
void PN7160::nci_fsm_transition_() {
switch (this->nci_state_) {
case NCIState::NFCC_RESET:
if (this->reset_core_(true, true) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to reset NCI core");
this->nci_fsm_set_error_state_(NCIState::NFCC_RESET);
return;
} else {
this->nci_fsm_set_state_(NCIState::NFCC_INIT);
}
// fall through
case NCIState::NFCC_INIT:
if (this->init_core_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to initialise NCI core");
this->nci_fsm_set_error_state_(NCIState::NFCC_INIT);
return;
} else {
this->nci_fsm_set_state_(NCIState::NFCC_CONFIG);
}
// fall through
case NCIState::NFCC_CONFIG:
if (this->send_init_config_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to send initial config");
this->nci_fsm_set_error_state_(NCIState::NFCC_CONFIG);
return;
} else {
this->config_refresh_pending_ = false;
this->nci_fsm_set_state_(NCIState::NFCC_SET_DISCOVER_MAP);
}
// fall through
case NCIState::NFCC_SET_DISCOVER_MAP:
if (this->set_discover_map_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to set discover map");
this->nci_fsm_set_error_state_(NCIState::NFCC_SET_LISTEN_MODE_ROUTING);
return;
} else {
this->nci_fsm_set_state_(NCIState::NFCC_SET_LISTEN_MODE_ROUTING);
}
// fall through
case NCIState::NFCC_SET_LISTEN_MODE_ROUTING:
if (this->set_listen_mode_routing_() != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Failed to set listen mode routing");
this->nci_fsm_set_error_state_(NCIState::RFST_IDLE);
return;
} else {
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
}
// fall through
case NCIState::RFST_IDLE:
if (this->nci_state_error_ == NCIState::RFST_DISCOVERY) {
this->stop_discovery_();
}
if (this->config_refresh_pending_) {
this->refresh_core_config_();
}
if (!this->listening_enabled_ && !this->polling_enabled_) {
return;
}
if (this->start_discovery_() != nfc::STATUS_OK) {
ESP_LOGV(TAG, "Failed to start discovery");
this->nci_fsm_set_error_state_(NCIState::RFST_DISCOVERY);
} else {
this->nci_fsm_set_state_(NCIState::RFST_DISCOVERY);
}
return;
case NCIState::RFST_W4_HOST_SELECT:
select_endpoint_();
// fall through
// All cases below are waiting for NOTIFICATION messages
case NCIState::RFST_DISCOVERY:
if (this->config_refresh_pending_) {
this->refresh_core_config_();
}
// fall through
case NCIState::RFST_LISTEN_ACTIVE:
case NCIState::RFST_LISTEN_SLEEP:
case NCIState::RFST_POLL_ACTIVE:
case NCIState::EP_SELECTING:
case NCIState::EP_DEACTIVATING:
if (this->irq_pin_->digital_read()) {
this->process_message_();
}
break;
case NCIState::FAILED:
case NCIState::NONE:
default:
return;
}
}
void PN7160::nci_fsm_set_state_(NCIState new_state) {
ESP_LOGVV(TAG, "nci_fsm_set_state_(%u)", (uint8_t) new_state);
this->nci_state_ = new_state;
this->nci_state_error_ = NCIState::NONE;
this->error_count_ = 0;
this->last_nci_state_change_ = millis();
}
bool PN7160::nci_fsm_set_error_state_(NCIState new_state) {
ESP_LOGVV(TAG, "nci_fsm_set_error_state_(%u); error_count_ = %u", (uint8_t) new_state, this->error_count_);
this->nci_state_error_ = new_state;
if (this->error_count_++ > NFCC_MAX_ERROR_COUNT) {
if ((this->nci_state_error_ == NCIState::NFCC_RESET) || (this->nci_state_error_ == NCIState::NFCC_INIT) ||
(this->nci_state_error_ == NCIState::NFCC_CONFIG)) {
ESP_LOGE(TAG, "Too many initialization failures -- check device connections");
this->mark_failed();
this->nci_fsm_set_state_(NCIState::FAILED);
} else {
ESP_LOGW(TAG, "Too many errors transitioning to state %u; resetting NFCC", (uint8_t) this->nci_state_error_);
this->nci_fsm_set_state_(NCIState::NFCC_RESET);
}
}
return this->error_count_ > NFCC_MAX_ERROR_COUNT;
}
void PN7160::process_message_() {
nfc::NciMessage rx;
if (this->read_nfcc(rx, NFCC_DEFAULT_TIMEOUT) != nfc::STATUS_OK) {
return; // No data
}
switch (rx.get_message_type()) {
case nfc::NCI_PKT_MT_CTRL_NOTIFICATION:
if (rx.get_gid() == nfc::RF_GID) {
switch (rx.get_oid()) {
case nfc::RF_INTF_ACTIVATED_OID:
ESP_LOGVV(TAG, "RF_INTF_ACTIVATED_OID");
this->process_rf_intf_activated_oid_(rx);
return;
case nfc::RF_DISCOVER_OID:
ESP_LOGVV(TAG, "RF_DISCOVER_OID");
this->process_rf_discover_oid_(rx);
return;
case nfc::RF_DEACTIVATE_OID:
ESP_LOGVV(TAG, "RF_DEACTIVATE_OID: type: 0x%02X, reason: 0x%02X", rx.get_message()[3], rx.get_message()[4]);
this->process_rf_deactivate_oid_(rx);
return;
default:
ESP_LOGV(TAG, "Unimplemented RF OID received: 0x%02X", rx.get_oid());
}
} else if (rx.get_gid() == nfc::NCI_CORE_GID) {
switch (rx.get_oid()) {
case nfc::NCI_CORE_GENERIC_ERROR_OID:
ESP_LOGV(TAG, "NCI_CORE_GENERIC_ERROR_OID:");
switch (rx.get_simple_status_response()) {
case nfc::DISCOVERY_ALREADY_STARTED:
ESP_LOGV(TAG, " DISCOVERY_ALREADY_STARTED");
break;
case nfc::DISCOVERY_TARGET_ACTIVATION_FAILED:
// Tag removed too soon
ESP_LOGV(TAG, " DISCOVERY_TARGET_ACTIVATION_FAILED");
if (this->nci_state_ == NCIState::EP_SELECTING) {
this->nci_fsm_set_state_(NCIState::RFST_W4_HOST_SELECT);
if (!this->discovered_endpoint_.empty()) {
this->erase_tag_(this->selecting_endpoint_);
}
} else {
this->stop_discovery_();
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
}
break;
case nfc::DISCOVERY_TEAR_DOWN:
ESP_LOGV(TAG, " DISCOVERY_TEAR_DOWN");
break;
default:
ESP_LOGW(TAG, "Unknown error: 0x%02X", rx.get_simple_status_response());
break;
}
break;
default:
ESP_LOGV(TAG, "Unimplemented NCI Core OID received: 0x%02X", rx.get_oid());
}
} else {
ESP_LOGV(TAG, "Unimplemented notification: %s", nfc::format_bytes(rx.get_message()).c_str());
}
break;
case nfc::NCI_PKT_MT_CTRL_RESPONSE:
ESP_LOGV(TAG, "Unimplemented GID: 0x%02X OID: 0x%02X Full response: %s", rx.get_gid(), rx.get_oid(),
nfc::format_bytes(rx.get_message()).c_str());
break;
case nfc::NCI_PKT_MT_CTRL_COMMAND:
ESP_LOGV(TAG, "Unimplemented command: %s", nfc::format_bytes(rx.get_message()).c_str());
break;
case nfc::NCI_PKT_MT_DATA:
this->process_data_message_(rx);
break;
default:
ESP_LOGV(TAG, "Unimplemented message type: %s", nfc::format_bytes(rx.get_message()).c_str());
break;
}
}
void PN7160::process_rf_intf_activated_oid_(nfc::NciMessage &rx) { // an endpoint was activated
uint8_t discovery_id = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_DISCOVERY_ID);
uint8_t interface = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_INTERFACE);
uint8_t protocol = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_PROTOCOL);
uint8_t mode_tech = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MODE_TECH);
uint8_t max_size = rx.get_message_byte(nfc::RF_INTF_ACTIVATED_NTF_MAX_SIZE);
ESP_LOGVV(TAG, "Endpoint activated -- interface: 0x%02X, protocol: 0x%02X, mode&tech: 0x%02X, max payload: %u",
interface, protocol, mode_tech, max_size);
if (mode_tech & nfc::MODE_LISTEN_MASK) {
ESP_LOGVV(TAG, "Tag activated in listen mode");
this->nci_fsm_set_state_(NCIState::RFST_LISTEN_ACTIVE);
return;
}
this->nci_fsm_set_state_(NCIState::RFST_POLL_ACTIVE);
auto incoming_tag =
this->build_tag_(mode_tech, std::vector<uint8_t>(rx.get_message().begin() + 10, rx.get_message().end()));
if (incoming_tag == nullptr) {
ESP_LOGE(TAG, "Could not build tag");
} else {
auto tag_loc = this->find_tag_uid_(incoming_tag->get_uid());
if (tag_loc.has_value()) {
this->discovered_endpoint_[tag_loc.value()].id = discovery_id;
this->discovered_endpoint_[tag_loc.value()].protocol = protocol;
this->discovered_endpoint_[tag_loc.value()].last_seen = millis();
ESP_LOGVV(TAG, "Tag cache updated");
} else {
this->discovered_endpoint_.emplace_back(
DiscoveredEndpoint{discovery_id, protocol, millis(), std::move(incoming_tag), false});
tag_loc = this->discovered_endpoint_.size() - 1;
ESP_LOGVV(TAG, "Tag added to cache");
}
auto &working_endpoint = this->discovered_endpoint_[tag_loc.value()];
switch (this->next_task_) {
case EP_CLEAN:
ESP_LOGD(TAG, " Tag cleaning...");
if (this->clean_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
ESP_LOGE(TAG, " Tag cleaning incomplete");
}
ESP_LOGD(TAG, " Tag cleaned!");
break;
case EP_FORMAT:
ESP_LOGD(TAG, " Tag formatting...");
if (this->format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error formatting tag as NDEF");
}
ESP_LOGD(TAG, " Tag formatted!");
break;
case EP_WRITE:
if (this->next_task_message_to_write_ != nullptr) {
ESP_LOGD(TAG, " Tag writing...");
ESP_LOGD(TAG, " Tag formatting...");
if (this->format_endpoint_(working_endpoint.tag->get_uid()) != nfc::STATUS_OK) {
ESP_LOGE(TAG, " Tag could not be formatted for writing");
} else {
ESP_LOGD(TAG, " Writing NDEF data");
if (this->write_endpoint_(working_endpoint.tag->get_uid(), this->next_task_message_to_write_) !=
nfc::STATUS_OK) {
ESP_LOGE(TAG, " Failed to write message to tag");
}
ESP_LOGD(TAG, " Finished writing NDEF data");
this->next_task_message_to_write_ = nullptr;
this->on_finished_write_callback_.call();
}
}
break;
case EP_READ:
default:
if (!working_endpoint.trig_called) {
ESP_LOGI(TAG, "Read tag type %s with UID %s", working_endpoint.tag->get_tag_type().c_str(),
nfc::format_uid(working_endpoint.tag->get_uid()).c_str());
if (this->read_endpoint_data_(*working_endpoint.tag) != nfc::STATUS_OK) {
ESP_LOGW(TAG, " Unable to read NDEF record(s)");
} else if (working_endpoint.tag->has_ndef_message()) {
const auto message = working_endpoint.tag->get_ndef_message();
const auto records = message->get_records();
ESP_LOGD(TAG, " NDEF record(s):");
for (const auto &record : records) {
ESP_LOGD(TAG, " %s - %s", record->get_type().c_str(), record->get_payload().c_str());
}
} else {
ESP_LOGW(TAG, " No NDEF records found");
}
for (auto *trigger : this->triggers_ontag_) {
trigger->process(working_endpoint.tag);
}
working_endpoint.trig_called = true;
break;
}
}
if (working_endpoint.tag->get_tag_type() == nfc::MIFARE_CLASSIC) {
this->halt_mifare_classic_tag_();
}
}
if (this->next_task_ != EP_READ) {
this->read_mode();
}
this->stop_discovery_();
this->nci_fsm_set_state_(NCIState::EP_DEACTIVATING);
}
void PN7160::process_rf_discover_oid_(nfc::NciMessage &rx) {
auto incoming_tag = this->build_tag_(rx.get_message_byte(nfc::RF_DISCOVER_NTF_MODE_TECH),
std::vector<uint8_t>(rx.get_message().begin() + 7, rx.get_message().end()));
if (incoming_tag == nullptr) {
ESP_LOGE(TAG, "Could not build tag!");
} else {
auto tag_loc = this->find_tag_uid_(incoming_tag->get_uid());
if (tag_loc.has_value()) {
this->discovered_endpoint_[tag_loc.value()].id = rx.get_message_byte(nfc::RF_DISCOVER_NTF_DISCOVERY_ID);
this->discovered_endpoint_[tag_loc.value()].protocol = rx.get_message_byte(nfc::RF_DISCOVER_NTF_PROTOCOL);
this->discovered_endpoint_[tag_loc.value()].last_seen = millis();
ESP_LOGVV(TAG, "Tag found & updated");
} else {
this->discovered_endpoint_.emplace_back(DiscoveredEndpoint{rx.get_message_byte(nfc::RF_DISCOVER_NTF_DISCOVERY_ID),
rx.get_message_byte(nfc::RF_DISCOVER_NTF_PROTOCOL),
millis(), std::move(incoming_tag), false});
ESP_LOGVV(TAG, "Tag saved");
}
}
if (rx.get_message().back() != nfc::RF_DISCOVER_NTF_NT_MORE) {
this->nci_fsm_set_state_(NCIState::RFST_W4_HOST_SELECT);
ESP_LOGVV(TAG, "Discovered %u endpoints", this->discovered_endpoint_.size());
}
}
void PN7160::process_rf_deactivate_oid_(nfc::NciMessage &rx) {
this->ce_state_ = CardEmulationState::CARD_EMU_IDLE;
switch (rx.get_simple_status_response()) {
case nfc::DEACTIVATION_TYPE_DISCOVERY:
this->nci_fsm_set_state_(NCIState::RFST_DISCOVERY);
break;
case nfc::DEACTIVATION_TYPE_IDLE:
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
break;
case nfc::DEACTIVATION_TYPE_SLEEP:
case nfc::DEACTIVATION_TYPE_SLEEP_AF:
if (this->nci_state_ == NCIState::RFST_LISTEN_ACTIVE) {
this->nci_fsm_set_state_(NCIState::RFST_LISTEN_SLEEP);
} else if (this->nci_state_ == NCIState::RFST_POLL_ACTIVE) {
this->nci_fsm_set_state_(NCIState::RFST_W4_HOST_SELECT);
} else {
this->nci_fsm_set_state_(NCIState::RFST_IDLE);
}
break;
default:
break;
}
}
void PN7160::process_data_message_(nfc::NciMessage &rx) {
ESP_LOGVV(TAG, "Received data message: %s", nfc::format_bytes(rx.get_message()).c_str());
std::vector<uint8_t> ndef_response;
this->card_emu_t4t_get_response_(rx.get_message(), ndef_response);
uint16_t ndef_response_size = ndef_response.size();
if (!ndef_response_size) {
return; // no message returned, we cannot respond
}
std::vector<uint8_t> tx_msg = {nfc::NCI_PKT_MT_DATA, uint8_t((ndef_response_size & 0xFF00) >> 8),
uint8_t(ndef_response_size & 0x00FF)};
tx_msg.insert(tx_msg.end(), ndef_response.begin(), ndef_response.end());
nfc::NciMessage tx(tx_msg);
ESP_LOGVV(TAG, "Sending data message: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx, NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending reply for card emulation failed");
}
}
void PN7160::card_emu_t4t_get_response_(std::vector<uint8_t> &response, std::vector<uint8_t> &ndef_response) {
if (this->card_emulation_message_ == nullptr) {
ESP_LOGE(TAG, "No NDEF message is set; tag emulation not possible");
ndef_response.clear();
return;
}
if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_APP_SELECT))) {
// CARD_EMU_T4T_APP_SELECT
ESP_LOGVV(TAG, "CARD_EMU_NDEF_APP_SELECTED");
this->ce_state_ = CardEmulationState::CARD_EMU_NDEF_APP_SELECTED;
ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
} else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_CC_SELECT))) {
// CARD_EMU_T4T_CC_SELECT
if (this->ce_state_ == CardEmulationState::CARD_EMU_NDEF_APP_SELECTED) {
ESP_LOGVV(TAG, "CARD_EMU_CC_SELECTED");
this->ce_state_ = CardEmulationState::CARD_EMU_CC_SELECTED;
ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
}
} else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE, response.end(), std::begin(CARD_EMU_T4T_NDEF_SELECT))) {
// CARD_EMU_T4T_NDEF_SELECT
ESP_LOGVV(TAG, "CARD_EMU_NDEF_SELECTED");
this->ce_state_ = CardEmulationState::CARD_EMU_NDEF_SELECTED;
ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
} else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
response.begin() + nfc::NCI_PKT_HEADER_SIZE + sizeof(CARD_EMU_T4T_READ),
std::begin(CARD_EMU_T4T_READ))) {
// CARD_EMU_T4T_READ
if (this->ce_state_ == CardEmulationState::CARD_EMU_CC_SELECTED) {
// CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED
ESP_LOGVV(TAG, "CARD_EMU_T4T_READ with CARD_EMU_CC_SELECTED");
uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
if (length <= (sizeof(CARD_EMU_T4T_CC) + offset + 2)) {
ndef_response.insert(ndef_response.begin(), std::begin(CARD_EMU_T4T_CC) + offset,
std::begin(CARD_EMU_T4T_CC) + offset + length);
ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
}
} else if (this->ce_state_ == CardEmulationState::CARD_EMU_NDEF_SELECTED) {
// CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED
ESP_LOGVV(TAG, "CARD_EMU_T4T_READ with CARD_EMU_NDEF_SELECTED");
auto ndef_message = this->card_emulation_message_->encode();
uint16_t ndef_msg_size = ndef_message.size();
uint16_t offset = (response[nfc::NCI_PKT_HEADER_SIZE + 2] << 8) + response[nfc::NCI_PKT_HEADER_SIZE + 3];
uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
ESP_LOGVV(TAG, "Encoded NDEF message: %s", nfc::format_bytes(ndef_message).c_str());
if (length <= (ndef_msg_size + offset + 2)) {
if (offset == 0) {
ndef_response.resize(2);
ndef_response[0] = (ndef_msg_size & 0xFF00) >> 8;
ndef_response[1] = (ndef_msg_size & 0x00FF);
if (length > 2) {
ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 2);
}
} else if (offset == 1) {
ndef_response.resize(1);
ndef_response[0] = (ndef_msg_size & 0x00FF);
if (length > 1) {
ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length - 1);
}
} else {
ndef_response.insert(ndef_response.end(), ndef_message.begin(), ndef_message.begin() + length);
}
ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
if ((offset + length) >= (ndef_msg_size + 2)) {
ESP_LOGD(TAG, "NDEF message sent");
this->on_emulated_tag_scan_callback_.call();
}
}
}
} else if (equal(response.begin() + nfc::NCI_PKT_HEADER_SIZE,
response.begin() + nfc::NCI_PKT_HEADER_SIZE + sizeof(CARD_EMU_T4T_WRITE),
std::begin(CARD_EMU_T4T_WRITE))) {
// CARD_EMU_T4T_WRITE
if (this->ce_state_ == CardEmulationState::CARD_EMU_NDEF_SELECTED) {
ESP_LOGVV(TAG, "CARD_EMU_T4T_WRITE");
uint8_t length = response[nfc::NCI_PKT_HEADER_SIZE + 4];
std::vector<uint8_t> ndef_msg_written;
ndef_msg_written.insert(ndef_msg_written.end(), response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5,
response.begin() + nfc::NCI_PKT_HEADER_SIZE + 5 + length);
ESP_LOGD(TAG, "Received %u-byte NDEF message: %s", length, nfc::format_bytes(ndef_msg_written).c_str());
ndef_response.insert(ndef_response.end(), std::begin(CARD_EMU_T4T_OK), std::end(CARD_EMU_T4T_OK));
}
}
}
uint8_t PN7160::transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, const uint16_t timeout,
const bool expect_notification) {
uint8_t retries = NFCC_MAX_COMM_FAILS;
while (retries) {
// first, send the message we need to send
if (this->write_nfcc(tx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error sending message");
return nfc::STATUS_FAILED;
}
ESP_LOGVV(TAG, "Wrote: %s", nfc::format_bytes(tx.get_message()).c_str());
// next, the NFCC should send back a response
if (this->read_nfcc(rx, timeout) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "Error receiving message");
if (!retries--) {
ESP_LOGE(TAG, " ...giving up");
return nfc::STATUS_FAILED;
}
} else {
break;
}
}
ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes(rx.get_message()).c_str());
// validate the response based on the message type that was sent (command vs. data)
if (!tx.message_type_is(nfc::NCI_PKT_MT_DATA)) {
// for commands, the GID and OID should match and the status should be OK
if ((rx.get_gid() != tx.get_gid()) || (rx.get_oid()) != tx.get_oid()) {
ESP_LOGE(TAG, "Incorrect response to command: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
if (!rx.simple_status_response_is(nfc::STATUS_OK)) {
ESP_LOGE(TAG, "Error in response to command: %s", nfc::format_bytes(rx.get_message()).c_str());
}
return rx.get_simple_status_response();
} else {
// when requesting data from the endpoint, the first response is from the NFCC; we must validate this, first
if ((!rx.message_type_is(nfc::NCI_PKT_MT_CTRL_NOTIFICATION)) || (!rx.gid_is(nfc::NCI_CORE_GID)) ||
(!rx.oid_is(nfc::NCI_CORE_CONN_CREDITS_OID)) || (!rx.message_length_is(3))) {
ESP_LOGE(TAG, "Incorrect response to data message: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
if (expect_notification) {
// if the NFCC said "OK", there will be additional data to read; this comes back in a notification message
if (this->read_nfcc(rx, timeout) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error receiving data from endpoint");
return nfc::STATUS_FAILED;
}
ESP_LOGVV(TAG, "Read: %s", nfc::format_bytes(rx.get_message()).c_str());
}
return nfc::STATUS_OK;
}
}
uint8_t PN7160::wait_for_irq_(uint16_t timeout, bool pin_state) {
auto start_time = millis();
while (millis() - start_time < timeout) {
if (this->irq_pin_->digital_read() == pin_state) {
return nfc::STATUS_OK;
}
}
ESP_LOGW(TAG, "Timed out waiting for IRQ state");
return nfc::STATUS_FAILED;
}
} // namespace pn7160
} // namespace esphome

View File

@ -0,0 +1,315 @@
#pragma once
#include "esphome/components/nfc/automation.h"
#include "esphome/components/nfc/nci_core.h"
#include "esphome/components/nfc/nci_message.h"
#include "esphome/components/nfc/nfc.h"
#include "esphome/components/nfc/nfc_helpers.h"
#include "esphome/core/component.h"
#include "esphome/core/gpio.h"
#include "esphome/core/helpers.h"
#include <functional>
namespace esphome {
namespace pn7160 {
static const uint16_t NFCC_DEFAULT_TIMEOUT = 10;
static const uint16_t NFCC_INIT_TIMEOUT = 50;
static const uint16_t NFCC_TAG_WRITE_TIMEOUT = 15;
static const uint8_t NFCC_MAX_COMM_FAILS = 3;
static const uint8_t NFCC_MAX_ERROR_COUNT = 10;
static const uint8_t XCHG_DATA_OID = 0x10;
static const uint8_t MF_SECTORSEL_OID = 0x32;
static const uint8_t MFC_AUTHENTICATE_OID = 0x40;
static const uint8_t TEST_PRBS_OID = 0x30;
static const uint8_t TEST_ANTENNA_OID = 0x3D;
static const uint8_t TEST_GET_REGISTER_OID = 0x33;
static const uint8_t MFC_AUTHENTICATE_PARAM_KS_A = 0x00; // key select A
static const uint8_t MFC_AUTHENTICATE_PARAM_KS_B = 0x80; // key select B
static const uint8_t MFC_AUTHENTICATE_PARAM_EMBED_KEY = 0x10;
static const uint8_t CARD_EMU_T4T_APP_SELECT[] = {0x00, 0xA4, 0x04, 0x00, 0x07, 0xD2, 0x76,
0x00, 0x00, 0x85, 0x01, 0x01, 0x00};
static const uint8_t CARD_EMU_T4T_CC[] = {0x00, 0x0F, 0x20, 0x00, 0xFF, 0x00, 0xFF, 0x04,
0x06, 0xE1, 0x04, 0x00, 0xFF, 0x00, 0x00};
static const uint8_t CARD_EMU_T4T_CC_SELECT[] = {0x00, 0xA4, 0x00, 0x0C, 0x02, 0xE1, 0x03};
static const uint8_t CARD_EMU_T4T_NDEF_SELECT[] = {0x00, 0xA4, 0x00, 0x0C, 0x02, 0xE1, 0x04};
static const uint8_t CARD_EMU_T4T_READ[] = {0x00, 0xB0};
static const uint8_t CARD_EMU_T4T_WRITE[] = {0x00, 0xD6};
static const uint8_t CARD_EMU_T4T_OK[] = {0x90, 0x00};
static const uint8_t CARD_EMU_T4T_NOK[] = {0x6A, 0x82};
static const uint8_t CORE_CONFIG_SOLO[] = {0x01, // Number of parameter fields
0x00, // config param identifier (TOTAL_DURATION)
0x02, // length of value
0x01, // TOTAL_DURATION (low)...
0x00}; // TOTAL_DURATION (high): 1 ms
static const uint8_t CORE_CONFIG_RW_CE[] = {0x01, // Number of parameter fields
0x00, // config param identifier (TOTAL_DURATION)
0x02, // length of value
0xF8, // TOTAL_DURATION (low)...
0x02}; // TOTAL_DURATION (high): 760 ms
static const uint8_t PMU_CFG[] = {
0x01, // Number of parameters
0xA0, 0x0E, // ext. tag
11, // length
0x11, // IRQ Enable: PVDD + temp sensor IRQs
0x01, // RFU
0x01, // Power and Clock Configuration, device on (CFG1)
0x01, // Power and Clock Configuration, device off (CFG1)
0x00, // RFU
0x00, // DC-DC 0
0x00, // DC-DC 1
// 0x14, // TXLDO (3.3V / 4.75V)
// 0xBB, // TXLDO (4.7V / 4.7V)
0xFF, // TXLDO (5.0V / 5.0V)
0x00, // RFU
0xD0, // TXLDO check
0x0C, // RFU
};
static const uint8_t RF_DISCOVER_MAP_CONFIG[] = { // poll modes
nfc::PROT_T1T, nfc::RF_DISCOVER_MAP_MODE_POLL,
nfc::INTF_FRAME, // poll mode
nfc::PROT_T2T, nfc::RF_DISCOVER_MAP_MODE_POLL,
nfc::INTF_FRAME, // poll mode
nfc::PROT_T3T, nfc::RF_DISCOVER_MAP_MODE_POLL,
nfc::INTF_FRAME, // poll mode
nfc::PROT_ISODEP, nfc::RF_DISCOVER_MAP_MODE_POLL | nfc::RF_DISCOVER_MAP_MODE_LISTEN,
nfc::INTF_ISODEP, // poll & listen mode
nfc::PROT_MIFARE, nfc::RF_DISCOVER_MAP_MODE_POLL,
nfc::INTF_TAGCMD}; // poll mode
static const uint8_t RF_DISCOVERY_LISTEN_CONFIG[] = {nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCA, // listen mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCB, // listen mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCF}; // listen mode
static const uint8_t RF_DISCOVERY_POLL_CONFIG[] = {nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA, // poll mode
nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCB, // poll mode
nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCF}; // poll mode
static const uint8_t RF_DISCOVERY_CONFIG[] = {nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCA, // poll mode
nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCB, // poll mode
nfc::MODE_POLL | nfc::TECH_PASSIVE_NFCF, // poll mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCA, // listen mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCB, // listen mode
nfc::MODE_LISTEN_MASK | nfc::TECH_PASSIVE_NFCF}; // listen mode
static const uint8_t RF_LISTEN_MODE_ROUTING_CONFIG[] = {0x00, // "more" (another message is coming)
2, // number of table entries
0x01, // type = protocol-based
3, // length
0, // DH NFCEE ID, a static ID representing the DH-NFCEE
0x07, // power state
nfc::PROT_ISODEP, // protocol
0x00, // type = technology-based
3, // length
0, // DH NFCEE ID, a static ID representing the DH-NFCEE
0x07, // power state
nfc::TECH_PASSIVE_NFCA}; // technology
enum class CardEmulationState : uint8_t {
CARD_EMU_IDLE,
CARD_EMU_NDEF_APP_SELECTED,
CARD_EMU_CC_SELECTED,
CARD_EMU_NDEF_SELECTED,
CARD_EMU_DESFIRE_PROD,
};
enum class NCIState : uint8_t {
NONE = 0x00,
NFCC_RESET,
NFCC_INIT,
NFCC_CONFIG,
NFCC_SET_DISCOVER_MAP,
NFCC_SET_LISTEN_MODE_ROUTING,
RFST_IDLE,
RFST_DISCOVERY,
RFST_W4_ALL_DISCOVERIES,
RFST_W4_HOST_SELECT,
RFST_LISTEN_ACTIVE,
RFST_LISTEN_SLEEP,
RFST_POLL_ACTIVE,
EP_DEACTIVATING,
EP_SELECTING,
TEST = 0XFE,
FAILED = 0XFF,
};
enum class TestMode : uint8_t {
TEST_NONE = 0x00,
TEST_PRBS,
TEST_ANTENNA,
TEST_GET_REGISTER,
};
struct DiscoveredEndpoint {
uint8_t id;
uint8_t protocol;
uint32_t last_seen;
std::unique_ptr<nfc::NfcTag> tag;
bool trig_called;
};
class PN7160 : public Component {
public:
void setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void loop() override;
void set_dwl_req_pin(GPIOPin *dwl_req_pin) { this->dwl_req_pin_ = dwl_req_pin; }
void set_irq_pin(GPIOPin *irq_pin) { this->irq_pin_ = irq_pin; }
void set_ven_pin(GPIOPin *ven_pin) { this->ven_pin_ = ven_pin; }
void set_wkup_req_pin(GPIOPin *wkup_req_pin) { this->wkup_req_pin_ = wkup_req_pin; }
void set_tag_ttl(uint32_t ttl) { this->tag_ttl_ = ttl; }
void set_tag_emulation_message(std::shared_ptr<nfc::NdefMessage> message);
void set_tag_emulation_message(const optional<std::string> &message, optional<bool> include_android_app_record);
void set_tag_emulation_message(const char *message, bool include_android_app_record = true);
void set_tag_emulation_off();
void set_tag_emulation_on();
bool tag_emulation_enabled() { return this->listening_enabled_; }
void set_polling_off();
void set_polling_on();
bool polling_enabled() { return this->polling_enabled_; }
void register_ontag_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontag_.push_back(trig); }
void register_ontagremoved_trigger(nfc::NfcOnTagTrigger *trig) { this->triggers_ontagremoved_.push_back(trig); }
void add_on_emulated_tag_scan_callback(std::function<void()> callback) {
this->on_emulated_tag_scan_callback_.add(std::move(callback));
}
void add_on_finished_write_callback(std::function<void()> callback) {
this->on_finished_write_callback_.add(std::move(callback));
}
bool is_writing() { return this->next_task_ != EP_READ; };
void read_mode();
void clean_mode();
void format_mode();
void write_mode();
void set_tag_write_message(std::shared_ptr<nfc::NdefMessage> message);
void set_tag_write_message(optional<std::string> message, optional<bool> include_android_app_record);
uint8_t set_test_mode(TestMode test_mode, const std::vector<uint8_t> &data, std::vector<uint8_t> &result);
protected:
uint8_t reset_core_(bool reset_config, bool power);
uint8_t init_core_();
uint8_t send_init_config_();
uint8_t send_core_config_();
uint8_t refresh_core_config_();
uint8_t set_discover_map_();
uint8_t set_listen_mode_routing_();
uint8_t start_discovery_();
uint8_t stop_discovery_();
uint8_t deactivate_(uint8_t type, uint16_t timeout = NFCC_DEFAULT_TIMEOUT);
void select_endpoint_();
uint8_t read_endpoint_data_(nfc::NfcTag &tag);
uint8_t clean_endpoint_(std::vector<uint8_t> &uid);
uint8_t format_endpoint_(std::vector<uint8_t> &uid);
uint8_t write_endpoint_(std::vector<uint8_t> &uid, std::shared_ptr<nfc::NdefMessage> &message);
std::unique_ptr<nfc::NfcTag> build_tag_(uint8_t mode_tech, const std::vector<uint8_t> &data);
optional<size_t> find_tag_uid_(const std::vector<uint8_t> &uid);
void purge_old_tags_();
void erase_tag_(uint8_t tag_index);
/// advance controller state as required
void nci_fsm_transition_();
/// set new controller state
void nci_fsm_set_state_(NCIState new_state);
/// setting controller to this state caused an error; returns true if too many errors/failures
bool nci_fsm_set_error_state_(NCIState new_state);
/// parse & process incoming messages from the NFCC
void process_message_();
void process_rf_intf_activated_oid_(nfc::NciMessage &rx);
void process_rf_discover_oid_(nfc::NciMessage &rx);
void process_rf_deactivate_oid_(nfc::NciMessage &rx);
void process_data_message_(nfc::NciMessage &rx);
void card_emu_t4t_get_response_(std::vector<uint8_t> &response, std::vector<uint8_t> &ndef_response);
uint8_t transceive_(nfc::NciMessage &tx, nfc::NciMessage &rx, uint16_t timeout = NFCC_DEFAULT_TIMEOUT,
bool expect_notification = true);
virtual uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) = 0;
virtual uint8_t write_nfcc(nfc::NciMessage &tx) = 0;
uint8_t wait_for_irq_(uint16_t timeout = NFCC_DEFAULT_TIMEOUT, bool pin_state = true);
uint8_t read_mifare_classic_tag_(nfc::NfcTag &tag);
uint8_t read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data);
uint8_t write_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data);
uint8_t auth_mifare_classic_block_(uint8_t block_num, uint8_t key_num, const uint8_t *key);
uint8_t sect_to_auth_(uint8_t block_num);
uint8_t format_mifare_classic_mifare_();
uint8_t format_mifare_classic_ndef_();
uint8_t write_mifare_classic_tag_(const std::shared_ptr<nfc::NdefMessage> &message);
uint8_t halt_mifare_classic_tag_();
uint8_t read_mifare_ultralight_tag_(nfc::NfcTag &tag);
uint8_t read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector<uint8_t> &data);
bool is_mifare_ultralight_formatted_(const std::vector<uint8_t> &page_3_to_6);
uint16_t read_mifare_ultralight_capacity_();
uint8_t find_mifare_ultralight_ndef_(const std::vector<uint8_t> &page_3_to_6, uint8_t &message_length,
uint8_t &message_start_index);
uint8_t write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &write_data);
uint8_t write_mifare_ultralight_tag_(std::vector<uint8_t> &uid, const std::shared_ptr<nfc::NdefMessage> &message);
uint8_t clean_mifare_ultralight_();
enum NfcTask : uint8_t {
EP_READ = 0,
EP_CLEAN,
EP_FORMAT,
EP_WRITE,
} next_task_{EP_READ};
bool config_refresh_pending_{false};
bool core_config_is_solo_{false};
bool listening_enabled_{false};
bool polling_enabled_{true};
uint8_t error_count_{0};
uint8_t fail_count_{0};
uint32_t last_nci_state_change_{0};
uint8_t selecting_endpoint_{0};
uint32_t tag_ttl_{250};
GPIOPin *dwl_req_pin_{nullptr};
GPIOPin *irq_pin_{nullptr};
GPIOPin *ven_pin_{nullptr};
GPIOPin *wkup_req_pin_{nullptr};
CallbackManager<void()> on_emulated_tag_scan_callback_;
CallbackManager<void()> on_finished_write_callback_;
std::vector<DiscoveredEndpoint> discovered_endpoint_;
CardEmulationState ce_state_{CardEmulationState::CARD_EMU_IDLE};
NCIState nci_state_{NCIState::NFCC_RESET};
NCIState nci_state_error_{NCIState::NONE};
std::shared_ptr<nfc::NdefMessage> card_emulation_message_;
std::shared_ptr<nfc::NdefMessage> next_task_message_to_write_;
std::vector<nfc::NfcOnTagTrigger *> triggers_ontag_;
std::vector<nfc::NfcOnTagTrigger *> triggers_ontagremoved_;
};
} // namespace pn7160
} // namespace esphome

View File

@ -0,0 +1,322 @@
#include <memory>
#include "pn7160.h"
#include "esphome/core/log.h"
namespace esphome {
namespace pn7160 {
static const char *const TAG = "pn7160.mifare_classic";
uint8_t PN7160::read_mifare_classic_tag_(nfc::NfcTag &tag) {
uint8_t current_block = 4;
uint8_t message_start_index = 0;
uint32_t message_length = 0;
if (this->auth_mifare_classic_block_(current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Tag auth failed while attempting to read tag data");
return nfc::STATUS_FAILED;
}
std::vector<uint8_t> data;
if (this->read_mifare_classic_block_(current_block, data) == nfc::STATUS_OK) {
if (!nfc::decode_mifare_classic_tlv(data, message_length, message_start_index)) {
return nfc::STATUS_FAILED;
}
} else {
ESP_LOGE(TAG, "Failed to read block %u", current_block);
return nfc::STATUS_FAILED;
}
uint32_t index = 0;
uint32_t buffer_size = nfc::get_mifare_classic_buffer_size(message_length);
std::vector<uint8_t> buffer;
while (index < buffer_size) {
if (nfc::mifare_classic_is_first_block(current_block)) {
if (this->auth_mifare_classic_block_(current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Block authentication failed for %u", current_block);
return nfc::STATUS_FAILED;
}
}
std::vector<uint8_t> block_data;
if (this->read_mifare_classic_block_(current_block, block_data) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error reading block %u", current_block);
return nfc::STATUS_FAILED;
} else {
buffer.insert(buffer.end(), block_data.begin(), block_data.end());
}
index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
current_block++;
if (nfc::mifare_classic_is_trailer_block(current_block)) {
current_block++;
}
}
if (buffer.begin() + message_start_index < buffer.end()) {
buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
} else {
return nfc::STATUS_FAILED;
}
tag.set_ndef_message(make_unique<nfc::NdefMessage>(buffer));
return nfc::STATUS_OK;
}
uint8_t PN7160::read_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &data) {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_READ, block_num});
ESP_LOGVV(TAG, "Read XCHG_DATA_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Timeout reading tag data");
return nfc::STATUS_FAILED;
}
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(XCHG_DATA_OID)) ||
(!rx.message_length_is(18))) {
ESP_LOGE(TAG, "MFC read block failed - block 0x%02x", block_num);
ESP_LOGV(TAG, "Read response: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
data.insert(data.begin(), rx.get_message().begin() + 4, rx.get_message().end() - 1);
ESP_LOGVV(TAG, " Block %u: %s", block_num, nfc::format_bytes(data).c_str());
return nfc::STATUS_OK;
}
uint8_t PN7160::auth_mifare_classic_block_(uint8_t block_num, uint8_t key_num, const uint8_t *key) {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {MFC_AUTHENTICATE_OID, this->sect_to_auth_(block_num), key_num});
switch (key_num) {
case nfc::MIFARE_CMD_AUTH_A:
tx.get_message().back() = MFC_AUTHENTICATE_PARAM_KS_A;
break;
case nfc::MIFARE_CMD_AUTH_B:
tx.get_message().back() = MFC_AUTHENTICATE_PARAM_KS_B;
break;
default:
break;
}
if (key != nullptr) {
tx.get_message().back() |= MFC_AUTHENTICATE_PARAM_EMBED_KEY;
tx.get_message().insert(tx.get_message().end(), key, key + 6);
}
ESP_LOGVV(TAG, "MFC_AUTHENTICATE_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending MFC_AUTHENTICATE_REQ failed");
return nfc::STATUS_FAILED;
}
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(MFC_AUTHENTICATE_OID)) ||
(rx.get_message()[4] != nfc::STATUS_OK)) {
ESP_LOGE(TAG, "MFC authentication failed - block 0x%02x", block_num);
ESP_LOGVV(TAG, "MFC_AUTHENTICATE_RSP: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
ESP_LOGV(TAG, "MFC block %u authentication succeeded", block_num);
return nfc::STATUS_OK;
}
uint8_t PN7160::sect_to_auth_(const uint8_t block_num) {
const uint8_t first_high_block = nfc::MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * nfc::MIFARE_CLASSIC_16BLOCK_SECT_START;
if (block_num >= first_high_block) {
return ((block_num - first_high_block) / nfc::MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH) +
nfc::MIFARE_CLASSIC_16BLOCK_SECT_START;
}
return block_num / nfc::MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW;
}
uint8_t PN7160::format_mifare_classic_mifare_() {
std::vector<uint8_t> blank_buffer(
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
std::vector<uint8_t> trailer_buffer(
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x80, 0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
auto status = nfc::STATUS_OK;
for (int block = 0; block < 64; block += 4) {
if (this->auth_mifare_classic_block_(block + 3, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY) != nfc::STATUS_OK) {
continue;
}
if (block != 0) {
if (this->write_mifare_classic_block_(block, blank_buffer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block);
status = nfc::STATUS_FAILED;
}
}
if (this->write_mifare_classic_block_(block + 1, blank_buffer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 1);
status = nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(block + 2, blank_buffer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 2);
status = nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(block + 3, trailer_buffer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 3);
status = nfc::STATUS_FAILED;
}
}
return status;
}
uint8_t PN7160::format_mifare_classic_ndef_() {
std::vector<uint8_t> empty_ndef_message(
{0x03, 0x03, 0xD0, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
std::vector<uint8_t> blank_block(
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
std::vector<uint8_t> block_1_data(
{0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
std::vector<uint8_t> block_2_data(
{0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1});
std::vector<uint8_t> block_3_trailer(
{0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
std::vector<uint8_t> ndef_trailer(
{0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
if (this->auth_mifare_classic_block_(0, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to authenticate block 0 for formatting");
return nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(1, block_1_data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(2, block_2_data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(3, block_3_trailer) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
ESP_LOGD(TAG, "Sector 0 formatted with NDEF");
auto status = nfc::STATUS_OK;
for (int block = 4; block < 64; block += 4) {
if (this->auth_mifare_classic_block_(block + 3, nfc::MIFARE_CMD_AUTH_B, nfc::DEFAULT_KEY) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
if (block == 4) {
if (this->write_mifare_classic_block_(block, empty_ndef_message) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block);
status = nfc::STATUS_FAILED;
}
} else {
if (this->write_mifare_classic_block_(block, blank_block) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block);
status = nfc::STATUS_FAILED;
}
}
if (this->write_mifare_classic_block_(block + 1, blank_block) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 1);
status = nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(block + 2, blank_block) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write block %u", block + 2);
status = nfc::STATUS_FAILED;
}
if (this->write_mifare_classic_block_(block + 3, ndef_trailer) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Unable to write trailer block %u", block + 3);
status = nfc::STATUS_FAILED;
}
}
return status;
}
uint8_t PN7160::write_mifare_classic_block_(uint8_t block_num, std::vector<uint8_t> &write_data) {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_WRITE, block_num});
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 1: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending XCHG_DATA_REQ failed");
return nfc::STATUS_FAILED;
}
// write command part two
tx.set_payload({XCHG_DATA_OID});
tx.get_message().insert(tx.get_message().end(), write_data.begin(), write_data.end());
ESP_LOGVV(TAG, "Write XCHG_DATA_REQ 2: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "MFC XCHG_DATA timed out waiting for XCHG_DATA_RSP during block write");
return nfc::STATUS_FAILED;
}
if ((!rx.message_type_is(nfc::NCI_PKT_MT_DATA)) || (!rx.simple_status_response_is(XCHG_DATA_OID)) ||
(rx.get_message()[4] != nfc::MIFARE_CMD_ACK)) {
ESP_LOGE(TAG, "MFC write block failed - block 0x%02x", block_num);
ESP_LOGV(TAG, "Write response: %s", nfc::format_bytes(rx.get_message()).c_str());
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7160::write_mifare_classic_tag_(const std::shared_ptr<nfc::NdefMessage> &message) {
auto encoded = message->encode();
uint32_t message_length = encoded.size();
uint32_t buffer_length = nfc::get_mifare_classic_buffer_size(message_length);
encoded.insert(encoded.begin(), 0x03);
if (message_length < 255) {
encoded.insert(encoded.begin() + 1, message_length);
} else {
encoded.insert(encoded.begin() + 1, 0xFF);
encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
encoded.insert(encoded.begin() + 3, message_length & 0xFF);
}
encoded.push_back(0xFE);
encoded.resize(buffer_length, 0);
uint32_t index = 0;
uint8_t current_block = 4;
while (index < buffer_length) {
if (nfc::mifare_classic_is_first_block(current_block)) {
if (this->auth_mifare_classic_block_(current_block, nfc::MIFARE_CMD_AUTH_A, nfc::NDEF_KEY) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
}
std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_CLASSIC_BLOCK_SIZE);
if (this->write_mifare_classic_block_(current_block, data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
index += nfc::MIFARE_CLASSIC_BLOCK_SIZE;
current_block++;
if (nfc::mifare_classic_is_trailer_block(current_block)) {
// Skipping as cannot write to trailer
current_block++;
}
}
return nfc::STATUS_OK;
}
uint8_t PN7160::halt_mifare_classic_tag_() {
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {XCHG_DATA_OID, nfc::MIFARE_CMD_HALT, 0});
ESP_LOGVV(TAG, "Halt XCHG_DATA_REQ: %s", nfc::format_bytes(tx.get_message()).c_str());
if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Sending halt XCHG_DATA_REQ failed");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
} // namespace pn7160
} // namespace esphome

View File

@ -0,0 +1,186 @@
#include <cinttypes>
#include <memory>
#include "pn7160.h"
#include "esphome/core/log.h"
namespace esphome {
namespace pn7160 {
static const char *const TAG = "pn7160.mifare_ultralight";
uint8_t PN7160::read_mifare_ultralight_tag_(nfc::NfcTag &tag) {
std::vector<uint8_t> data;
// pages 3 to 6 contain various info we are interested in -- do one read to grab it all
if (this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE * nfc::MIFARE_ULTRALIGHT_READ_SIZE,
data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
if (!this->is_mifare_ultralight_formatted_(data)) {
ESP_LOGW(TAG, "Not NDEF formatted");
return nfc::STATUS_FAILED;
}
uint8_t message_length;
uint8_t message_start_index;
if (this->find_mifare_ultralight_ndef_(data, message_length, message_start_index) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "Couldn't find NDEF message");
return nfc::STATUS_FAILED;
}
ESP_LOGVV(TAG, "NDEF message length: %u, start: %u", message_length, message_start_index);
if (message_length == 0) {
return nfc::STATUS_FAILED;
}
// we already read pages 3-6 earlier -- pick up where we left off so we're not re-reading pages
const uint8_t read_length = message_length + message_start_index > 12 ? message_length + message_start_index - 12 : 0;
if (read_length) {
if (read_mifare_ultralight_bytes_(nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE + 3, read_length, data) !=
nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error reading tag data");
return nfc::STATUS_FAILED;
}
}
// we need to trim off page 3 as well as any bytes ahead of message_start_index
data.erase(data.begin(), data.begin() + message_start_index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
tag.set_ndef_message(make_unique<nfc::NdefMessage>(data));
return nfc::STATUS_OK;
}
uint8_t PN7160::read_mifare_ultralight_bytes_(uint8_t start_page, uint16_t num_bytes, std::vector<uint8_t> &data) {
const uint8_t read_increment = nfc::MIFARE_ULTRALIGHT_READ_SIZE * nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, {nfc::MIFARE_CMD_READ, start_page});
for (size_t i = 0; i * read_increment < num_bytes; i++) {
tx.get_message().back() = i * nfc::MIFARE_ULTRALIGHT_READ_SIZE + start_page;
do { // loop because sometimes we struggle here...???...
if (this->transceive_(tx, rx) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error reading tag data");
return nfc::STATUS_FAILED;
}
} while (rx.get_payload_size() < read_increment);
uint16_t bytes_offset = (i + 1) * read_increment;
auto pages_in_end_itr = bytes_offset <= num_bytes ? rx.get_message().end() - 1
: rx.get_message().end() - (bytes_offset - num_bytes + 1);
if ((pages_in_end_itr > rx.get_message().begin()) && (pages_in_end_itr < rx.get_message().end())) {
data.insert(data.end(), rx.get_message().begin() + nfc::NCI_PKT_HEADER_SIZE, pages_in_end_itr);
}
}
ESP_LOGVV(TAG, "Data read: %s", nfc::format_bytes(data).c_str());
return nfc::STATUS_OK;
}
bool PN7160::is_mifare_ultralight_formatted_(const std::vector<uint8_t> &page_3_to_6) {
const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector
return (page_3_to_6.size() > p4_offset + 3) &&
!((page_3_to_6[p4_offset + 0] == 0xFF) && (page_3_to_6[p4_offset + 1] == 0xFF) &&
(page_3_to_6[p4_offset + 2] == 0xFF) && (page_3_to_6[p4_offset + 3] == 0xFF));
}
uint16_t PN7160::read_mifare_ultralight_capacity_() {
std::vector<uint8_t> data;
if (this->read_mifare_ultralight_bytes_(3, nfc::MIFARE_ULTRALIGHT_PAGE_SIZE, data) == nfc::STATUS_OK) {
ESP_LOGV(TAG, "Tag capacity is %u bytes", data[2] * 8U);
return data[2] * 8U;
}
return 0;
}
uint8_t PN7160::find_mifare_ultralight_ndef_(const std::vector<uint8_t> &page_3_to_6, uint8_t &message_length,
uint8_t &message_start_index) {
const uint8_t p4_offset = nfc::MIFARE_ULTRALIGHT_PAGE_SIZE; // page 4 will begin 4 bytes into the vector
if (!(page_3_to_6.size() > p4_offset + 5)) {
return nfc::STATUS_FAILED;
}
if (page_3_to_6[p4_offset + 0] == 0x03) {
message_length = page_3_to_6[p4_offset + 1];
message_start_index = 2;
return nfc::STATUS_OK;
} else if (page_3_to_6[p4_offset + 5] == 0x03) {
message_length = page_3_to_6[p4_offset + 6];
message_start_index = 7;
return nfc::STATUS_OK;
}
return nfc::STATUS_FAILED;
}
uint8_t PN7160::write_mifare_ultralight_tag_(std::vector<uint8_t> &uid,
const std::shared_ptr<nfc::NdefMessage> &message) {
uint32_t capacity = this->read_mifare_ultralight_capacity_();
auto encoded = message->encode();
uint32_t message_length = encoded.size();
uint32_t buffer_length = nfc::get_mifare_ultralight_buffer_size(message_length);
if (buffer_length > capacity) {
ESP_LOGE(TAG, "Message length exceeds tag capacity %" PRIu32 " > %" PRIu32, buffer_length, capacity);
return nfc::STATUS_FAILED;
}
encoded.insert(encoded.begin(), 0x03);
if (message_length < 255) {
encoded.insert(encoded.begin() + 1, message_length);
} else {
encoded.insert(encoded.begin() + 1, 0xFF);
encoded.insert(encoded.begin() + 2, (message_length >> 8) & 0xFF);
encoded.insert(encoded.begin() + 2, message_length & 0xFF);
}
encoded.push_back(0xFE);
encoded.resize(buffer_length, 0);
uint32_t index = 0;
uint8_t current_page = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
while (index < buffer_length) {
std::vector<uint8_t> data(encoded.begin() + index, encoded.begin() + index + nfc::MIFARE_ULTRALIGHT_PAGE_SIZE);
if (this->write_mifare_ultralight_page_(current_page, data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
index += nfc::MIFARE_ULTRALIGHT_PAGE_SIZE;
current_page++;
}
return nfc::STATUS_OK;
}
uint8_t PN7160::clean_mifare_ultralight_() {
uint32_t capacity = this->read_mifare_ultralight_capacity_();
uint8_t pages = (capacity / nfc::MIFARE_ULTRALIGHT_PAGE_SIZE) + nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE;
std::vector<uint8_t> blank_data = {0x00, 0x00, 0x00, 0x00};
for (int i = nfc::MIFARE_ULTRALIGHT_DATA_START_PAGE; i < pages; i++) {
if (this->write_mifare_ultralight_page_(i, blank_data) != nfc::STATUS_OK) {
return nfc::STATUS_FAILED;
}
}
return nfc::STATUS_OK;
}
uint8_t PN7160::write_mifare_ultralight_page_(uint8_t page_num, std::vector<uint8_t> &write_data) {
std::vector<uint8_t> payload = {nfc::MIFARE_CMD_WRITE_ULTRALIGHT, page_num};
payload.insert(payload.end(), write_data.begin(), write_data.end());
nfc::NciMessage rx;
nfc::NciMessage tx(nfc::NCI_PKT_MT_DATA, payload);
if (this->transceive_(tx, rx, NFCC_TAG_WRITE_TIMEOUT) != nfc::STATUS_OK) {
ESP_LOGE(TAG, "Error writing page %u", page_num);
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
} // namespace pn7160
} // namespace esphome

View File

@ -0,0 +1,25 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, pn7160
from esphome.const import CONF_ID
AUTO_LOAD = ["pn7160"]
CODEOWNERS = ["@kbx81", "@jesserockz"]
DEPENDENCIES = ["i2c"]
pn7160_i2c_ns = cg.esphome_ns.namespace("pn7160_i2c")
PN7160I2C = pn7160_i2c_ns.class_("PN7160I2C", pn7160.PN7160, i2c.I2CDevice)
CONFIG_SCHEMA = cv.All(
pn7160.PN7160_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(PN7160I2C),
}
).extend(i2c.i2c_device_schema(0x28))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await pn7160.setup_pn7160(var, config)
await i2c.register_i2c_device(var, config)

View File

@ -0,0 +1,49 @@
#include "pn7160_i2c.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace pn7160_i2c {
static const char *const TAG = "pn7160_i2c";
uint8_t PN7160I2C::read_nfcc(nfc::NciMessage &rx, const uint16_t timeout) {
if (this->wait_for_irq_(timeout) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "read_nfcc_() timeout waiting for IRQ");
return nfc::STATUS_FAILED;
}
rx.get_message().resize(nfc::NCI_PKT_HEADER_SIZE);
if (!this->read_bytes_raw(rx.get_message().data(), nfc::NCI_PKT_HEADER_SIZE)) {
return nfc::STATUS_FAILED;
}
uint8_t length = rx.get_payload_size();
if (length > 0) {
rx.get_message().resize(length + nfc::NCI_PKT_HEADER_SIZE);
if (!this->read_bytes_raw(rx.get_message().data() + nfc::NCI_PKT_HEADER_SIZE, length)) {
return nfc::STATUS_FAILED;
}
}
// semaphore to ensure transaction is complete before returning
if (this->wait_for_irq_(pn7160::NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "read_nfcc_() post-read timeout waiting for IRQ line to clear");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7160I2C::write_nfcc(nfc::NciMessage &tx) {
if (this->write(tx.encode().data(), tx.encode().size()) == i2c::ERROR_OK) {
return nfc::STATUS_OK;
}
return nfc::STATUS_FAILED;
}
void PN7160I2C::dump_config() {
PN7160::dump_config();
LOG_I2C_DEVICE(this);
}
} // namespace pn7160_i2c
} // namespace esphome

View File

@ -0,0 +1,22 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/pn7160/pn7160.h"
#include "esphome/components/i2c/i2c.h"
#include <vector>
namespace esphome {
namespace pn7160_i2c {
class PN7160I2C : public pn7160::PN7160, public i2c::I2CDevice {
public:
void dump_config() override;
protected:
uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) override;
uint8_t write_nfcc(nfc::NciMessage &tx) override;
};
} // namespace pn7160_i2c
} // namespace esphome

View File

@ -0,0 +1,26 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import spi, pn7160
from esphome.const import CONF_ID
AUTO_LOAD = ["pn7160"]
CODEOWNERS = ["@kbx81", "@jesserockz"]
DEPENDENCIES = ["spi"]
MULTI_CONF = True
pn7160_spi_ns = cg.esphome_ns.namespace("pn7160_spi")
PN7160Spi = pn7160_spi_ns.class_("PN7160Spi", pn7160.PN7160, spi.SPIDevice)
CONFIG_SCHEMA = cv.All(
pn7160.PN7160_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(PN7160Spi),
}
).extend(spi.spi_device_schema(cs_pin_required=True))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await pn7160.setup_pn7160(var, config)
await spi.register_spi_device(var, config)

View File

@ -0,0 +1,54 @@
#include "pn7160_spi.h"
#include "esphome/core/log.h"
namespace esphome {
namespace pn7160_spi {
static const char *const TAG = "pn7160_spi";
void PN7160Spi::setup() {
this->spi_setup();
this->cs_->digital_write(false);
PN7160::setup();
}
uint8_t PN7160Spi::read_nfcc(nfc::NciMessage &rx, const uint16_t timeout) {
if (this->wait_for_irq_(timeout) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "read_nfcc_() timeout waiting for IRQ");
return nfc::STATUS_FAILED;
}
rx.get_message().resize(nfc::NCI_PKT_HEADER_SIZE);
this->enable();
this->write_byte(TDD_SPI_READ); // send "transfer direction detector"
this->read_array(rx.get_message().data(), nfc::NCI_PKT_HEADER_SIZE);
uint8_t length = rx.get_payload_size();
if (length > 0) {
rx.get_message().resize(length + nfc::NCI_PKT_HEADER_SIZE);
this->read_array(rx.get_message().data() + nfc::NCI_PKT_HEADER_SIZE, length);
}
this->disable();
// semaphore to ensure transaction is complete before returning
if (this->wait_for_irq_(pn7160::NFCC_DEFAULT_TIMEOUT, false) != nfc::STATUS_OK) {
ESP_LOGW(TAG, "read_nfcc_() post-read timeout waiting for IRQ line to clear");
return nfc::STATUS_FAILED;
}
return nfc::STATUS_OK;
}
uint8_t PN7160Spi::write_nfcc(nfc::NciMessage &tx) {
this->enable();
this->write_byte(TDD_SPI_WRITE); // send "transfer direction detector"
this->write_array(tx.encode().data(), tx.encode().size());
this->disable();
return nfc::STATUS_OK;
}
void PN7160Spi::dump_config() {
PN7160::dump_config();
LOG_PIN(" CS Pin: ", this->cs_);
}
} // namespace pn7160_spi
} // namespace esphome

View File

@ -0,0 +1,30 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/nfc/nci_core.h"
#include "esphome/components/pn7160/pn7160.h"
#include "esphome/components/spi/spi.h"
#include <vector>
namespace esphome {
namespace pn7160_spi {
static const uint8_t TDD_SPI_READ = 0xFF;
static const uint8_t TDD_SPI_WRITE = 0x0A;
class PN7160Spi : public pn7160::PN7160,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
spi::DATA_RATE_4MHZ> {
public:
void setup() override;
void dump_config() override;
protected:
uint8_t read_nfcc(nfc::NciMessage &rx, uint16_t timeout) override;
uint8_t write_nfcc(nfc::NciMessage &tx) override;
};
} // namespace pn7160_spi
} // namespace esphome

View File

@ -8,6 +8,8 @@ power_supply_ns = cg.esphome_ns.namespace("power_supply")
PowerSupply = power_supply_ns.class_("PowerSupply", cg.Component)
MULTI_CONF = True
CONF_ENABLE_ON_BOOT = "enable_on_boot"
CONFIG_SCHEMA = cv.Schema(
{
cv.Required(CONF_ID): cv.declare_id(PowerSupply),
@ -18,6 +20,7 @@ CONFIG_SCHEMA = cv.Schema(
cv.Optional(
CONF_KEEP_ON_TIME, default="10s"
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_ENABLE_ON_BOOT, default=False): cv.boolean,
}
).extend(cv.COMPONENT_SCHEMA)
@ -30,5 +33,6 @@ async def to_code(config):
cg.add(var.set_pin(pin))
cg.add(var.set_enable_time(config[CONF_ENABLE_TIME]))
cg.add(var.set_keep_on_time(config[CONF_KEEP_ON_TIME]))
cg.add(var.set_enable_on_boot(config[CONF_ENABLE_ON_BOOT]))
cg.add_define("USE_POWER_SUPPLY")

View File

@ -11,47 +11,42 @@ void PowerSupply::setup() {
this->pin_->setup();
this->pin_->digital_write(false);
this->enabled_ = false;
if (this->enable_on_boot_)
this->request_high_power();
}
void PowerSupply::dump_config() {
ESP_LOGCONFIG(TAG, "Power Supply:");
LOG_PIN(" Pin: ", this->pin_);
ESP_LOGCONFIG(TAG, " Time to enable: %" PRIu32 " ms", this->enable_time_);
ESP_LOGCONFIG(TAG, " Keep on time: %.1f s", this->keep_on_time_ / 1000.0f);
if (this->enable_on_boot_)
ESP_LOGCONFIG(TAG, " Enabled at startup: True");
}
float PowerSupply::get_setup_priority() const { return setup_priority::IO; }
bool PowerSupply::is_enabled() const { return this->enabled_; }
bool PowerSupply::is_enabled() const { return this->active_requests_ != 0; }
void PowerSupply::request_high_power() {
this->cancel_timeout("power-supply-off");
this->pin_->digital_write(true);
if (this->active_requests_ == 0) {
// we need to enable the power supply.
// cancel old timeout if it exists because we now definitely have a high power mode.
this->cancel_timeout("power-supply-off");
ESP_LOGD(TAG, "Enabling power supply.");
this->pin_->digital_write(true);
delay(this->enable_time_);
}
this->enabled_ = true;
// increase active requests
this->active_requests_++;
}
void PowerSupply::unrequest_high_power() {
this->active_requests_--;
if (this->active_requests_ < 0) {
// we're just going to use 0 as our new counter.
this->active_requests_ = 0;
}
if (this->active_requests_ == 0) {
// set timeout for power supply off
ESP_LOGW(TAG, "Invalid call to unrequest_high_power");
return;
}
this->active_requests_--;
if (this->active_requests_ == 0) {
this->set_timeout("power-supply-off", this->keep_on_time_, [this]() {
ESP_LOGD(TAG, "Disabling power supply.");
this->pin_->digital_write(false);
this->enabled_ = false;
});
}
}

View File

@ -13,6 +13,7 @@ class PowerSupply : public Component {
void set_pin(GPIOPin *pin) { pin_ = pin; }
void set_enable_time(uint32_t enable_time) { enable_time_ = enable_time; }
void set_keep_on_time(uint32_t keep_on_time) { keep_on_time_ = keep_on_time; }
void set_enable_on_boot(bool enable_on_boot) { enable_on_boot_ = enable_on_boot; }
/// Is this power supply currently on?
bool is_enabled() const;
@ -35,7 +36,7 @@ class PowerSupply : public Component {
protected:
GPIOPin *pin_;
bool enabled_{false};
bool enable_on_boot_{false};
uint32_t enable_time_;
uint32_t keep_on_time_;
int16_t active_requests_{0}; // use signed integer to make catching negative requests easier.

View File

@ -38,7 +38,6 @@ CONFIG_SCHEMA = (
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await display.register_display(var, config)
await ble_client.register_ble_node(var, config)
cg.add(var.set_disconnect_delay(config[CONF_DISCONNECT_DELAY].total_milliseconds))

View File

@ -13,7 +13,9 @@ void PVVXDisplay::dump_config() {
ESP_LOGCONFIG(TAG, " Service UUID : %s", this->service_uuid_.to_string().c_str());
ESP_LOGCONFIG(TAG, " Characteristic UUID : %s", this->char_uuid_.to_string().c_str());
ESP_LOGCONFIG(TAG, " Auto clear : %s", YESNO(this->auto_clear_enabled_));
#ifdef USE_TIME
ESP_LOGCONFIG(TAG, " Set time on connection: %s", YESNO(this->time_ != nullptr));
#endif
ESP_LOGCONFIG(TAG, " Disconnect delay : %" PRIu32 "ms", this->disconnect_delay_ms_);
LOG_UPDATE_INTERVAL(this);
}

View File

@ -0,0 +1,46 @@
import logging
import esphome.codegen as cg
from esphome.components import uart
import esphome.config_validation as cv
from esphome.const import CONF_ID
_LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@functionpointer"]
DEPENDENCIES = ["uart"]
MULTI_CONF = True
CONF_PYLONTECH_ID = "pylontech_id"
CONF_BATTERY = "battery"
pylontech_ns = cg.esphome_ns.namespace("pylontech")
PylontechComponent = pylontech_ns.class_(
"PylontechComponent", cg.PollingComponent, uart.UARTDevice
)
PylontechBattery = pylontech_ns.class_("PylontechBattery")
CV_NUM_BATTERIES = cv.int_range(1, 6)
PYLONTECH_COMPONENT_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_PYLONTECH_ID): cv.use_id(PylontechComponent),
cv.Required(CONF_BATTERY): CV_NUM_BATTERIES,
}
)
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(PylontechComponent),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)

Some files were not shown because too many files have changed in this diff Show More