Skip to content

Commit 87b99fc

Browse files
authored
Merge pull request #78 from Lyr3x/fix-manual-roi-setting
Fix manual roi setting
2 parents 30b6a7e + a977499 commit 87b99fc

File tree

5 files changed

+66
-29
lines changed

5 files changed

+66
-29
lines changed

components/roode/__init__.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
import esphome.codegen as cg
33
import esphome.config_validation as cv
44
from esphome.components import sensor
5-
from esphome.const import CONF_ID, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT, UNIT_EMPTY, UNIT_METER
5+
from esphome.const import (
6+
CONF_ID,
7+
DEVICE_CLASS_EMPTY,
8+
STATE_CLASS_MEASUREMENT,
9+
UNIT_EMPTY,
10+
UNIT_METER,
11+
)
612

713

814
# DEPENDENCIES = ["i2c"]
@@ -32,6 +38,8 @@
3238
CONF_CALIBRATION_ACTIVE = "calibration_active"
3339
CONF_TIMING_BUDGET = "timing_budget"
3440
CONF_USE_SAMPLING = "use_sampling"
41+
CONF_ROI = "roi"
42+
CONF_ROI_ACTIVE = "roi_active"
3543
TYPES = [
3644
CONF_RESTORE_VALUES,
3745
CONF_INVERT_DIRECTION,
@@ -78,27 +86,39 @@
7886
"manual_mode",
7987
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
8088
): cv.int_range(min=-1, max=3),
81-
cv.Inclusive(
82-
CONF_ROI_HEIGHT,
83-
"manual_mode",
84-
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
85-
): cv.int_range(min=4, max=16),
86-
cv.Inclusive(
87-
CONF_ROI_WIDTH,
88-
"manual_mode",
89-
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
90-
): cv.int_range(min=4, max=16),
9189
cv.Inclusive(
9290
CONF_MANUAL_THRESHOLD,
9391
"manual_mode",
9492
f"{CONF_SENSOR_MODE}, {CONF_ROI_HEIGHT}, {CONF_ROI_WIDTH} and {CONF_MANUAL_THRESHOLD} must be used together",
9593
): cv.int_range(min=40, max=4000),
9694
}
9795
),
96+
cv.Optional(CONF_ROI): cv.Schema(
97+
{
98+
cv.Optional(CONF_ROI_ACTIVE, default="true"): cv.boolean,
99+
cv.Optional(CONF_ROI_HEIGHT, default=16): cv.int_range(min=4, max=16),
100+
cv.Optional(CONF_ROI_WIDTH, default=6): cv.int_range(min=4, max=16),
101+
}
102+
),
98103
}
99104
).extend(cv.polling_component_schema("100ms"))
100105

101106

107+
def validate_roi_settings(config):
108+
roi = config.get(CONF_ROI)
109+
manual = config.get(CONF_MANUAL)
110+
if CONF_CALIBRATION in config:
111+
roi_calibration = config.get(CONF_CALIBRATION).get(CONF_ROI_CALIBRATION)
112+
if roi_calibration == True and roi != None:
113+
raise cv.Invalid(
114+
"ROI calibration cannot be used with manual ROI width and height"
115+
)
116+
if roi_calibration == False and roi == None:
117+
raise cv.Invalid("You need to set the ROI manually or use ROI calibration")
118+
if manual != None and roi == None:
119+
raise cv.Invalid("You need to set the ROI manually if manual mode is active")
120+
121+
102122
async def setup_conf(config, key, hub):
103123
if key in config:
104124
cg.add(getattr(hub, f"set_{key}")(config[key]))
@@ -116,12 +136,21 @@ def setup_calibration_mode(config, hub):
116136
cg.add(getattr(hub, f"set_{key}")(calibration[key]))
117137

118138

139+
def setup_manual_roi(config, hub):
140+
roi = config[CONF_ROI]
141+
for key in roi:
142+
cg.add(getattr(hub, f"set_{key}")(roi[key]))
143+
144+
119145
async def to_code(config):
120146
hub = cg.new_Pvariable(config[CONF_ID])
121147
await cg.register_component(hub, config)
122148
cg.add_library("EEPROM", None)
123149
cg.add_library("Wire", None)
124150
cg.add_library("pololu", "1.3.0", "VL53L1X")
151+
152+
validate_roi_settings(config)
153+
125154
for key in TYPES:
126155
await setup_conf(config, key, hub)
127156
if CONF_MANUAL in config:

components/roode/roode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace esphome
5858

5959
void set_calibration_active(bool val) { calibration_active_ = val; }
6060
void set_manual_active(bool val) { manual_active_ = val; }
61+
void set_roi_active(bool val) { roi_active_ = val; }
6162
void set_roi_calibration(bool val) { roi_calibration_ = val; }
6263
void set_timing_budget(int timing_budget) { timing_budget_ = timing_budget; }
6364
void set_manual_threshold(int val) { manual_threshold_ = val; }
@@ -121,6 +122,7 @@ namespace esphome
121122
int getSum(int *values, int size);
122123
bool calibration_active_{false};
123124
bool manual_active_{false};
125+
bool roi_active_{false};
124126
bool roi_calibration_{false};
125127
int sensor_mode{-1};
126128
bool advised_sensor_orientation_{true};

peopleCounter.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,19 @@ i2c:
7171
roode:
7272
id: roode_platform
7373
i2c_address: 0x29
74-
update_interval: 100ms
74+
update_interval: 10ms
75+
# roi:
76+
# roi_height: 16
77+
# roi_width: 6
7578
calibration:
7679
max_threshold_percentage: 85
7780
min_threshold_percentage: 5
7881
roi_calibration: true
7982
# manual:
80-
# sensor_mode: 2
81-
# roi_height: 16
82-
# roi_width: 6
83-
# manual_threshold: 1300
84-
# timing_budget: 100
83+
# sensor_mode: 3
84+
# manual_threshold: 1280
85+
# timing_budget: 200
86+
# use_sampling: true
8587
invert_direction: true
8688
restore_values: false
8789

peopleCounter32.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,19 @@ i2c:
7474
roode:
7575
id: roode_platform
7676
i2c_address: 0x29
77-
update_interval: 100ms
77+
update_interval: 10ms
78+
# roi:
79+
# roi_height: 16
80+
# roi_width: 6
7881
calibration:
7982
max_threshold_percentage: 85
8083
min_threshold_percentage: 5
8184
roi_calibration: true
8285
# manual:
83-
# sensor_mode: 2
84-
# roi_height: 16
85-
# roi_width: 6
86-
# manual_threshold: 1300
87-
# timing_budget: 100
86+
# sensor_mode: 3
87+
# manual_threshold: 1280
88+
# timing_budget: 200
89+
# use_sampling: true
8890
invert_direction: true
8991
restore_values: false
9092

peopleCounterDev.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@ i2c:
6060
roode:
6161
id: roode_platform
6262
i2c_address: 0x29
63-
update_interval: 100ms
63+
update_interval: 10ms
64+
# roi:
65+
# roi_height: 16
66+
# roi_width: 6
6467
calibration:
6568
max_threshold_percentage: 85
6669
min_threshold_percentage: 5
6770
roi_calibration: true
6871
# manual:
69-
# sensor_mode: 2
70-
# roi_height: 16
71-
# roi_width: 6
72-
# manual_threshold: 1300
73-
# timing_budget: 100
72+
# sensor_mode: 3
73+
# manual_threshold: 1280
74+
# timing_budget: 200
75+
# use_sampling: true
7476
invert_direction: true
7577
restore_values: false
7678

0 commit comments

Comments
 (0)