Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Oct 9, 2024
1 parent 83b889b commit 27910c1
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 22 deletions.
12 changes: 12 additions & 0 deletions components/tc_bus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
"write_memory": CommandType.COMMAND_TYPE_WRITE_MEMORY
}

Model = tc_bus_ns.enum("Model")
MODELS = {
"generic": Model.MODEL_GENERIC,
"ish_3030": Model.MODEL_ISH_3030,
}

CONF_TC_ID = "tc_bus"

CONF_RX_PIN = "rx_pin"
Expand All @@ -87,6 +93,8 @@
CONF_PAYLOAD = "payload"
CONF_PAYLOAD_LAMBDA = "payload_lambda"

CONF_MODEL = "model"

CONF_BUS_COMMAND = "bus_command"
CONF_HARDWARE_VERSION = "hardware_version"
CONF_DOOR_READINESS = "door_readiness"
Expand All @@ -107,6 +115,7 @@ def validate_config(config):
cv.GenerateID(): cv.declare_id(TCBus),
cv.Optional(CONF_RX_PIN, default=9): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_TX_PIN, default=8): pins.internal_gpio_output_pin_schema,
cv.Optional(CONF_MODEL, default="generic"): cv.enum(MODELS, upper=False),
cv.Optional(CONF_EVENT, default="tc"): cv.string,
cv.Optional(CONF_SERIAL_NUMBER, default=0): cv.hex_uint32_t,
cv.Optional(CONF_SERIAL_NUMBER_LAMBDA): cv.returning_lambda,
Expand Down Expand Up @@ -154,6 +163,9 @@ async def to_code(config):
pin = await cg.gpio_pin_expression(config[CONF_TX_PIN])
cg.add(var.set_tx_pin(pin))

if CONF_MODEL in config:
cg.add(var.set_model(config[CONF_MODEL]))

cg.add(var.set_event("esphome." + config[CONF_EVENT]))

if CONF_SERIAL_NUMBER in config:
Expand Down
5 changes: 5 additions & 0 deletions components/tc_bus/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ namespace esphome
uint8_t internal_call_ringtone = 0;
uint8_t floor_call_ringtone = 0;
};

struct SettingCellData {
uint8_t index = 0;
bool left_nibble = false;
};
}
}
7 changes: 7 additions & 0 deletions components/tc_bus/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ namespace esphome
return data;
}

Model string_to_model(std::string str)
{
if (str == "ISH_3030") return MODEL_ISH_3030;

return MODEL_GENERIC;
}

CommandType string_to_command_type(std::string str)
{
if (str == "DOOR_CALL") return COMMAND_TYPE_DOOR_CALL;
Expand Down
7 changes: 7 additions & 0 deletions components/tc_bus/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace esphome
{
namespace tc_bus
{
enum Model {
MODEL_GENERIC,
MODEL_ISH_3030
};

enum CommandType {
COMMAND_TYPE_UNKNOWN,
COMMAND_TYPE_DOOR_CALL,
Expand Down Expand Up @@ -51,5 +56,7 @@ namespace esphome
const char* command_type_to_string(CommandType type);
CommandType string_to_command_type(std::string str);

const char* model_to_string(Model model);

} // namespace tc_bus
} // namespace esphome
103 changes: 81 additions & 22 deletions components/tc_bus/tc_bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,6 @@ namespace esphome
}
}

void TCBusComponent::set_settings()
{
ESP_LOGD(TAG, "Extracting Settings from Buffer");

// Update Setting values to values from buffer
// TODO: Model specific
settings_.handset_volume = memory_buffer_[21] & 0xF;
settings_.ringtone_volume = memory_buffer_[20] & 0xF;
settings_.door_call_ringtone = (memory_buffer_[3] >> 4) & 0xF;
settings_.internal_call_ringtone = (memory_buffer_[6] >> 4) & 0xF;
settings_.floor_call_ringtone = (memory_buffer_[9] >> 4) & 0xF;

ESP_LOGD(TAG, "Handset volume %i", settings_.handset_volume);
ESP_LOGD(TAG, "Ringtone volume %i", settings_.ringtone_volume);
ESP_LOGD(TAG, "Door Call Ringtone %i", settings_.door_call_ringtone);
ESP_LOGD(TAG, "Floor Call Ringtone %i", settings_.floor_call_ringtone);
ESP_LOGD(TAG, "Internal Call Ringtone %i", settings_.internal_call_ringtone);
}

void TCBusComponent::register_listener(TCBusListener *listener)
{
this->listeners_.push_back(listener);
Expand Down Expand Up @@ -641,6 +622,13 @@ namespace esphome

void TCBusComponent::update_setting(SettingType type, uint8_t new_value, uint32_t serial_number)
{
// No general support
if(model_ == MODEL_GENERIC)
{
ESP_LOGW(TAG, "Please specify the intercom model in order to change the settings!");
return;
}

if(memory_buffer_.size() == 0)
{
ESP_LOGW(TAG, "Memory Buffer is empty! Please read memory first!");
Expand Down Expand Up @@ -675,14 +663,21 @@ namespace esphome

uint8_t index = 0;
uint8_t cell2 = 0;
uint8_t left_nibble = 0;

// TODO: Model specific
// Merge write logic
// Set Cell number and save value depending on cell number
// Generic not supported
switch(type)
{
case SETTING_RINGTONE_DOOR_CALL:
index = 3;
cell2 = memory_buffer_[index] & 0xF;
memory_buffer_[index] = (new_value << 4) | (cell2 & 0xF);
if(model_ == MODEL_ISH_3030)
{
index = 3;
cell2 = memory_buffer_[index] & 0xF;
memory_buffer_[index] = (new_value << 4) | (cell2 & 0xF);
}
settings_.door_call_ringtone = new_value;
break;

Expand Down Expand Up @@ -730,6 +725,70 @@ namespace esphome
}
}

bool TCBusComponent::applySettingValue(SettingType setting, uint8_t new_value)
{
uint8_t byte_index = 0;
bool left_nibble = false;
uint8_t saved_nibble = 0;

// Get Setting Cell Data by Model
SettingCellData cellData = getSettingCellData(setting);

// Apply new nibble and keep other nibble
saved_nibble = (cellData.left_nibble ? memory_buffer_[cellData.index] : (memory_buffer_[cellData.index] >> 4)) & 0xF;
memory_buffer_[cellData.index] = cellData.left_nibble ? ((new_value << 4) | (saved_nibble & 0xF)) : ((saved_nibble << 4) | (new_value & 0xF));

// Transfer memory buffer to settings store
set_settings();

return true;
}

SettingCellData TCBusComponent::getSettingCellData(SettingType setting)
{
SettingCellData data;

switch (model_)
{
case MODEL_ISH_3030:
switch (setting)
{
case SETTING_HANDSET_VOLUME:
data.index = 3;
data.left_nibble = true;
break;

default:
break;
}
break;

default:
break;
}

return data;
}

void TCBusComponent::set_settings()
{
ESP_LOGD(TAG, "Extracting Settings from Buffer");

// Update Setting values to values from buffer
// TODO: Model specific
settings_.handset_volume = memory_buffer_[21] & 0xF;
settings_.ringtone_volume = memory_buffer_[20] & 0xF;
settings_.door_call_ringtone = (memory_buffer_[3] >> 4) & 0xF;
settings_.internal_call_ringtone = (memory_buffer_[6] >> 4) & 0xF;
settings_.floor_call_ringtone = (memory_buffer_[9] >> 4) & 0xF;

ESP_LOGD(TAG, "Handset volume %i", settings_.handset_volume);
ESP_LOGD(TAG, "Ringtone volume %i", settings_.ringtone_volume);
ESP_LOGD(TAG, "Door Call Ringtone %i", settings_.door_call_ringtone);
ESP_LOGD(TAG, "Floor Call Ringtone %i", settings_.floor_call_ringtone);
ESP_LOGD(TAG, "Internal Call Ringtone %i", settings_.internal_call_ringtone);
}

void TCBusComponent::write_memory(uint32_t serial_number)
{
if(memory_buffer_.size() == 0)
Expand Down
4 changes: 4 additions & 0 deletions components/tc_bus/tc_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ namespace esphome

void register_listener(TCBusListener *listener);

void set_model(Model model) { this->model_ = model; }

void set_sn(uint32_t serial_number) { this->serial_number_ = serial_number; }
void set_sn_lambda(std::function<optional<uint32_t>()> &&serial_number_lambda) { this->serial_number_lambda_ = serial_number_lambda; }

Expand Down Expand Up @@ -117,6 +119,8 @@ namespace esphome
InternalGPIOPin *tx_pin_;
const char* event_;

Model model_;

TCBusComponentStore store_;
std::vector<TCBusListener *> listeners_{};

Expand Down
1 change: 1 addition & 0 deletions firmware/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ api:
then:
- tc_bus.send:
command: !lambda 'return command;'
# TODO: Fix String to command
- action: send_tc_command
variables:
type: string
Expand Down

0 comments on commit 27910c1

Please sign in to comment.