Skip to content

Commit

Permalink
Home Accessory Architect v12.12.8 Merlin
Browse files Browse the repository at this point in the history
  • Loading branch information
RavenSystem committed Jul 5, 2024
1 parent 71eb278 commit 5238148
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 51 deletions.
22 changes: 22 additions & 0 deletions HAA/HAA_Main/main/extra_characteristics.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,63 @@
##__VA_ARGS__

// HAA POWER MONITOR
#define HKCH_CUSTOM_VOLT_MIN (-500)
#define HKCH_CUSTOM_VOLT_MAX (500)

#define HOMEKIT_CHARACTERISTIC_CUSTOM_VOLT HOMEKIT_CUSTOM_EXTRA_UUID("F0000901")
#define HOMEKIT_DECLARE_CHARACTERISTIC_CUSTOM_VOLT(_value, ...) \
.type = HOMEKIT_CHARACTERISTIC_CUSTOM_VOLT, \
.description = "Voltage", \
.format = HOMEKIT_FORMAT_FLOAT, \
.permissions = HOMEKIT_PERMISSIONS_PAIRED_READ \
| HOMEKIT_PERMISSIONS_NOTIFY, \
.min_value = (float[]) {HKCH_CUSTOM_VOLT_MIN}, \
.max_value = (float[]) {HKCH_CUSTOM_VOLT_MAX}, \
.value = HOMEKIT_FLOAT_(_value), \
##__VA_ARGS__

#define HKCH_CUSTOM_AMPERE_MIN (-150)
#define HKCH_CUSTOM_AMPERE_MAX (150)

#define HOMEKIT_CHARACTERISTIC_CUSTOM_AMPERE HOMEKIT_CUSTOM_EXTRA_UUID("F0000902")
#define HOMEKIT_DECLARE_CHARACTERISTIC_CUSTOM_AMPERE(_value, ...) \
.type = HOMEKIT_CHARACTERISTIC_CUSTOM_AMPERE, \
.description = "Current", \
.format = HOMEKIT_FORMAT_FLOAT, \
.permissions = HOMEKIT_PERMISSIONS_PAIRED_READ \
| HOMEKIT_PERMISSIONS_NOTIFY, \
.min_value = (float[]) {HKCH_CUSTOM_AMPERE_MIN}, \
.max_value = (float[]) {HKCH_CUSTOM_AMPERE_MAX}, \
.value = HOMEKIT_FLOAT_(_value), \
##__VA_ARGS__

#define HKCH_CUSTOM_WATT_MIN (-50000)
#define HKCH_CUSTOM_WATT_MAX (50000)

#define HOMEKIT_CHARACTERISTIC_CUSTOM_WATT HOMEKIT_CUSTOM_EXTRA_UUID("F0000903")
#define HOMEKIT_DECLARE_CHARACTERISTIC_CUSTOM_WATT(_value, ...) \
.type = HOMEKIT_CHARACTERISTIC_CUSTOM_WATT, \
.description = "Power", \
.format = HOMEKIT_FORMAT_FLOAT, \
.permissions = HOMEKIT_PERMISSIONS_PAIRED_READ \
| HOMEKIT_PERMISSIONS_NOTIFY, \
.min_value = (float[]) {HKCH_CUSTOM_WATT_MIN}, \
.max_value = (float[]) {HKCH_CUSTOM_WATT_MAX}, \
.value = HOMEKIT_FLOAT_(_value), \
##__VA_ARGS__

#define HKCH_CUSTOM_CONSUMP_MIN (-100000)
#define HKCH_CUSTOM_CONSUMP_MAX (100000)

#define HOMEKIT_CHARACTERISTIC_CUSTOM_CONSUMP HOMEKIT_CUSTOM_EXTRA_UUID("F0000904")
#define HOMEKIT_DECLARE_CHARACTERISTIC_CUSTOM_CONSUMP(_value, ...) \
.type = HOMEKIT_CHARACTERISTIC_CUSTOM_CONSUMP, \
.description = "Consumption", \
.format = HOMEKIT_FORMAT_FLOAT, \
.permissions = HOMEKIT_PERMISSIONS_PAIRED_READ \
| HOMEKIT_PERMISSIONS_NOTIFY, \
.min_value = (float[]) {HKCH_CUSTOM_CONSUMP_MIN}, \
.max_value = (float[]) {HKCH_CUSTOM_CONSUMP_MAX}, \
.value = HOMEKIT_FLOAT_(_value), \
##__VA_ARGS__

Expand All @@ -132,6 +152,8 @@
.format = HOMEKIT_FORMAT_FLOAT, \
.permissions = HOMEKIT_PERMISSIONS_PAIRED_READ \
| HOMEKIT_PERMISSIONS_NOTIFY, \
.min_value = (float[]) {HKCH_CUSTOM_CONSUMP_MIN}, \
.max_value = (float[]) {HKCH_CUSTOM_CONSUMP_MAX}, \
.value = HOMEKIT_FLOAT_(_value), \
##__VA_ARGS__

Expand Down
5 changes: 2 additions & 3 deletions HAA/HAA_Main/main/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "../../common/common_headers.h"

// Version
#define HAA_FIRMWARE_VERSION "12.12.7"
#define HAA_FIRMWARE_VERSION "12.12.8"
#define HAA_FIRMWARE_BETA_REVISION "" // Format: "b01"
#define HAA_FIRMWARE_CODENAME "Merlin"

Expand Down Expand Up @@ -540,8 +540,6 @@
#define FM_OFFSET_DEFAULT (0)
#define FM_OFFSET ch_group->num_f[1]
#define FM_LIMIT_ARRAY_SET "l"
#define FM_LIMIT_LOWER ch_group->num_f[4]
#define FM_LIMIT_UPPER ch_group->num_f[5]
#define FM_MATHS_OPERATION_NONE (0)
#define FM_MATHS_OPERATION_ADD (1)
#define FM_MATHS_OPERATION_SUB (2)
Expand Down Expand Up @@ -607,6 +605,7 @@
#define FM_I2C_TRIGGER_REG ch_group->num_i // from num_i[FM_I2C_TRIGGER_REG_FIRST] to num_i[FM_I2C_TRIGGER_REG_FIRST + 3]
#define FM_I2C_TRIGGER_VAL_FIRST (17)
#define FM_I2C_TRIGGER_VAL ch_group->num_i // from num_i[FM_I2C_TRIGGER_VAL_FIRST] to num_i[FM_I2C_TRIGGER_VAL_FIRST + 3]
#define FM_ACCUMULATVE_VALUE_RESET (-2182017)

#define RECV_UART_POLL_PERIOD_MS (50)

Expand Down
91 changes: 43 additions & 48 deletions HAA/HAA_Main/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ void power_monitor_task(void* args) {
}
}

if (voltage < 500.f && voltage > -500.f) {
if (voltage < HKCH_CUSTOM_VOLT_MAX && voltage > HKCH_CUSTOM_VOLT_MIN) {
if (voltage != ch_group->ch[0]->value.float_value) {
const int old_voltage = ch_group->ch[0]->value.float_value * 10;
ch_group->ch[0]->value.float_value = voltage;
Expand All @@ -1726,7 +1726,7 @@ void power_monitor_task(void* args) {
ERROR("<%i> PM V", ch_group->serv_index);
}

if (current < 150.f && current > -150.f) {
if (current < HKCH_CUSTOM_AMPERE_MAX && current > HKCH_CUSTOM_AMPERE_MIN) {
if (current != ch_group->ch[1]->value.float_value) {
const int old_current = ch_group->ch[1]->value.float_value * 1000;
ch_group->ch[1]->value.float_value = current;
Expand All @@ -1746,7 +1746,7 @@ void power_monitor_task(void* args) {

PM_LAST_SAVED_CONSUPTION += PM_POLL_PERIOD;

if (power < 50000.f && power > -50000.f) {
if (power < HKCH_CUSTOM_WATT_MAX && power > HKCH_CUSTOM_WATT_MIN) {
if (power != ch_group->ch[2]->value.float_value) {
const int old_power = ch_group->ch[2]->value.float_value * 10;
ch_group->ch[2]->value.float_value = power;
Expand Down Expand Up @@ -4380,7 +4380,7 @@ void window_cover_timer_worker(TimerHandle_t xTimer) {

normalize_current_position();

if ((WINDOW_COVER_CH_TARGET_POSITION->value.int_value - margin) >= WINDOW_COVER_HOMEKIT_POSITION) {
if ((WINDOW_COVER_CH_TARGET_POSITION->value.int_value - margin) >= (signed int) WINDOW_COVER_HOMEKIT_POSITION) {
WINDOW_COVER_MUST_STOP = 1;
} else {
send_hk_current_position();
Expand All @@ -4397,7 +4397,7 @@ void window_cover_timer_worker(TimerHandle_t xTimer) {

normalize_current_position();

if ((WINDOW_COVER_CH_TARGET_POSITION->value.int_value + margin) <= WINDOW_COVER_HOMEKIT_POSITION) {
if ((WINDOW_COVER_CH_TARGET_POSITION->value.int_value + margin) <= (signed int) WINDOW_COVER_HOMEKIT_POSITION) {
WINDOW_COVER_MUST_STOP = 1;
} else {
send_hk_current_position();
Expand Down Expand Up @@ -5024,7 +5024,7 @@ void IRAM fm_pulse_interrupt(const uint8_t gpio) {
while (ch_group) {
if ((ch_group->serv_type == SERV_TYPE_FREE_MONITOR ||
ch_group->serv_type == SERV_TYPE_FREE_MONITOR_ACCUMULATVE) &&
private_abs(FM_SENSOR_TYPE) == FM_SENSOR_TYPE_PULSE_US_TIME &&
FM_SENSOR_TYPE == FM_SENSOR_TYPE_PULSE_US_TIME &&
FM_SENSOR_GPIO == gpio) {
if (FM_NEW_VALUE == 0) {
FM_NEW_VALUE = time;
Expand Down Expand Up @@ -5056,7 +5056,7 @@ void IRAM fm_pwm_interrupt(const uint8_t gpio) {
while (ch_group) {
if ((ch_group->serv_type == SERV_TYPE_FREE_MONITOR ||
ch_group->serv_type == SERV_TYPE_FREE_MONITOR_ACCUMULATVE) &&
private_abs(FM_SENSOR_TYPE) == FM_SENSOR_TYPE_PWM_DUTY &&
FM_SENSOR_TYPE == FM_SENSOR_TYPE_PWM_DUTY &&
FM_SENSOR_GPIO == gpio) {

switch (FM_SENSOR_PWM_DUTY_STATUS) {
Expand Down Expand Up @@ -5148,7 +5148,7 @@ void free_monitor_task(void* args) {
float value = 0;
unsigned int get_value = false;

const unsigned int fm_sensor_type = private_abs(FM_SENSOR_TYPE);
const unsigned int fm_sensor_type = FM_SENSOR_TYPE;

if (args) {
if (fm_sensor_type == FM_SENSOR_TYPE_FREE || FM_OVERRIDE_VALUE != NO_LAST_WILDCARD_ACTION) {
Expand Down Expand Up @@ -5706,19 +5706,15 @@ void free_monitor_task(void* args) {
value = (FM_FACTOR * value) + FM_OFFSET;

if (ch_group->serv_type == SERV_TYPE_FREE_MONITOR_ACCUMULATVE) {
if ((int) value == -2182017) {
value = 0;
} else {
value += ch_group->ch[0]->value.float_value;
}
value += ch_group->ch[0]->value.float_value;
}

INFO("<%i> FM: %g", ch_group->serv_index, value);

if (FM_SENSOR_TYPE > 0 ||
(FM_SENSOR_TYPE < 0 &&
value >= FM_LIMIT_LOWER &&
value <= FM_LIMIT_UPPER)) {
if (!ch_group->ch[0]->min_value ||
(ch_group->ch[0]->min_value &&
value >= *ch_group->ch[0]->min_value &&
value <= *ch_group->ch[0]->max_value)) {

const int old_value = ch_group->ch[0]->value.float_value * 1000;
ch_group->ch[0]->value.float_value = value;
Expand Down Expand Up @@ -5764,14 +5760,14 @@ void free_monitor_timer_worker(TimerHandle_t xTimer) {
if (ch_group->main_enabled) {
if (!ch_group->is_working) {
ch_group_t* ch_group_b = NULL;
const unsigned int fm_sensor_type = private_abs(FM_SENSOR_TYPE);
const unsigned int fm_sensor_type = FM_SENSOR_TYPE;

if (fm_sensor_type == FM_SENSOR_TYPE_I2C || fm_sensor_type == FM_SENSOR_TYPE_I2C_TRIGGER) {
ch_group_b = main_config.ch_groups;
while (ch_group_b) {
if ((ch_group_b->serv_type == SERV_TYPE_FREE_MONITOR ||
ch_group_b->serv_type == SERV_TYPE_FREE_MONITOR_ACCUMULATVE) &&
private_abs(FM_SENSOR_TYPE_B) == FM_SENSOR_TYPE_I2C_TRIGGER &&
FM_SENSOR_TYPE_B == FM_SENSOR_TYPE_I2C_TRIGGER &&
FM_I2C_BUS_B == FM_I2C_BUS &&
FM_I2C_ADDR_B == FM_I2C_ADDR &&
ch_group_b->is_working) {
Expand Down Expand Up @@ -7247,19 +7243,25 @@ void do_actions(ch_group_t* ch_group, uint8_t action) {
case SERV_TYPE_FREE_MONITOR:
case SERV_TYPE_FREE_MONITOR_ACCUMULATVE:
if (ch_group->main_enabled) {
if (ch_group == ch_group_ori) {
ch_group->ch[0]->value.float_value = action_serv_manager->value;
if (ch_group->serv_type == SERV_TYPE_FREE_MONITOR_ACCUMULATVE && value_int == FM_ACCUMULATVE_VALUE_RESET) {
ch_group->ch[0]->value.float_value = 0;
homekit_characteristic_notify_safe(ch_group->ch[0]);

} else if (!ch_group->is_working) {
ch_group->is_working = true;
FM_OVERRIDE_VALUE = action_serv_manager->value;
if (xTaskCreate(free_monitor_task, "FM", FREE_MONITOR_TASK_SIZE, (void*) ch_group, FREE_MONITOR_TASK_PRIORITY, NULL) != pdPASS) {
ch_group->is_working = false;
homekit_remove_oldest_client();
ERROR("FM");
}
} else {
ERROR("<%i> Overlaps", ch_group->serv_index);
if (ch_group == ch_group_ori) {
ch_group->ch[0]->value.float_value = action_serv_manager->value;

} else if (!ch_group->is_working) {
ch_group->is_working = true;
FM_OVERRIDE_VALUE = action_serv_manager->value;
if (xTaskCreate(free_monitor_task, "FM", FREE_MONITOR_TASK_SIZE, (void*) ch_group, FREE_MONITOR_TASK_PRIORITY, NULL) != pdPASS) {
ch_group->is_working = false;
homekit_remove_oldest_client();
ERROR("FM");
}
} else {
ERROR("<%i> Overlaps", ch_group->serv_index);
}
}
}
break;
Expand Down Expand Up @@ -12359,11 +12361,6 @@ void normal_mode_init() {
}
}

unsigned int has_limits = 0;
if (cJSON_rsf_GetObjectItemCaseSensitive(json_context, FM_LIMIT_ARRAY_SET) != NULL) {
has_limits = 2;
}

unsigned int is_type_uart = false;
if (fm_sensor_type >= FM_SENSOR_TYPE_UART) {
is_type_uart = true;
Expand Down Expand Up @@ -12403,22 +12400,12 @@ void normal_mode_init() {
(maths_operations << 1),

4 +
has_limits +
maths_operations,

1);

FM_OVERRIDE_VALUE = NO_LAST_WILDCARD_ACTION;

if (has_limits > 0) {
cJSON_rsf* limits_array = cJSON_rsf_GetObjectItemCaseSensitive(json_context, FM_LIMIT_ARRAY_SET);
FM_LIMIT_LOWER = cJSON_rsf_GetArrayItem(limits_array, 0)->valuefloat;
FM_LIMIT_UPPER = cJSON_rsf_GetArrayItem(limits_array, 1)->valuefloat;

FM_SENSOR_TYPE = -fm_sensor_type; // If there are limits, sensor_type is negative
} else {
FM_SENSOR_TYPE = fm_sensor_type;
}
FM_SENSOR_TYPE = fm_sensor_type;

ch_group->serv_type = serv_type;
ch_group->serv_index = service_numerator;
Expand All @@ -12430,7 +12417,7 @@ void normal_mode_init() {

cJSON_rsf* val_data = cJSON_rsf_GetObjectItemCaseSensitive(json_context, FM_READ_COMMAND_DATA_ARRAY);

unsigned int float_index = FM_MATHS_FLOAT_FIRST + has_limits;
unsigned int float_index = FM_MATHS_FLOAT_FIRST;
unsigned int int_index = FM_MATHS_FIRST_OPERATION;
for (unsigned int i = 0; i < (maths_operations * 3); i++) {
for (unsigned int j = 0; j < 2; j++) {
Expand Down Expand Up @@ -12459,7 +12446,15 @@ void normal_mode_init() {
register_wildcard_actions(ch_group, json_context);
new_action_network(ch_group, json_context, 0);

ch_group->ch[0] = NEW_HOMEKIT_CHARACTERISTIC(CUSTOM_FREE_VALUE, 0);
if (cJSON_rsf_GetObjectItemCaseSensitive(json_context, FM_LIMIT_ARRAY_SET) != NULL) {
cJSON_rsf* limits_array = cJSON_rsf_GetObjectItemCaseSensitive(json_context, FM_LIMIT_ARRAY_SET);
const float limit_min_value = cJSON_rsf_GetArrayItem(limits_array, 0)->valuefloat;
const float limit_max_value = cJSON_rsf_GetArrayItem(limits_array, 1)->valuefloat;

ch_group->ch[0] = NEW_HOMEKIT_CHARACTERISTIC(CUSTOM_FREE_VALUE, 0, .min_value=(float[]) {limit_min_value}, .max_value=(float[]) {limit_max_value}, .min_step=NULL);
} else {
ch_group->ch[0] = NEW_HOMEKIT_CHARACTERISTIC(CUSTOM_FREE_VALUE, 0);
}

if (tg_serv != 0) {
ch_group->ch[ch_group->chs - 1] = ch_group_find_by_serv(get_absolut_index(service_numerator, tg_serv))->ch[tg_ch];
Expand Down

0 comments on commit 5238148

Please sign in to comment.