Tasmota/tasmota/xdrv_32_hotplug.ino

102 lines
2.8 KiB
Arduino
Raw Normal View History

2019-12-28 20:12:46 +00:00
/*
xdrv_32_hotplug.ino - HotPlug support for sensors
2021-01-01 12:44:04 +00:00
Copyright (C) 2021 Leonid Myravjev
2019-12-28 20:12:46 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_HOTPLUG
/*********************************************************************************************\
* HotPlug Support
*
* - Rescan bus every N seconds. It send FUNC_HOTPLUG_SCAN event to every sensors.
* - If HotPlug is 0 or 0xFF -- HotPlug is off
\*********************************************************************************************/
2019-12-30 13:23:37 +00:00
#define XDRV_32 32
2019-12-28 20:12:46 +00:00
2019-12-30 13:23:37 +00:00
const uint32_t HOTPLUG_MAX = 254; // 0 and 0xFF is OFF
2019-12-28 20:12:46 +00:00
2019-12-30 13:23:37 +00:00
const char kHotPlugCommands[] PROGMEM = "|" // No prefix
D_CMND_HOTPLUG;
2019-12-28 20:12:46 +00:00
2019-12-30 13:23:37 +00:00
void (* const HotPlugCommand[])(void) PROGMEM = {
&CmndHotPlugTime };
2019-12-28 20:12:46 +00:00
2019-12-30 13:23:37 +00:00
struct {
// uint32_t sleeptime = 0;
bool enabled = false;
uint8_t timeout = 0;
} Hotplug;
2019-12-28 20:12:46 +00:00
void HotPlugInit(void)
{
// If empty eeprom is 0xFF by default
2021-06-11 17:14:12 +01:00
if (Settings->hotplug_scan == 0xFF) { Settings->hotplug_scan = 0; }
if (Settings->hotplug_scan != 0) {
2019-12-30 13:23:37 +00:00
Hotplug.enabled = true;
Hotplug.timeout = 1; // First scan in a second
2019-12-28 20:12:46 +00:00
} else
2019-12-30 13:23:37 +00:00
Hotplug.enabled = false;
2019-12-28 20:12:46 +00:00
}
void HotPlugEverySecond(void)
{
2019-12-30 13:23:37 +00:00
if (Hotplug.enabled) {
if (Hotplug.timeout == 0) {
2019-12-28 20:12:46 +00:00
XsnsCall(FUNC_HOTPLUG_SCAN);
2021-06-11 17:14:12 +01:00
Hotplug.timeout = Settings->hotplug_scan;
2019-12-28 20:12:46 +00:00
}
2019-12-30 13:23:37 +00:00
Hotplug.timeout--;
2019-12-28 20:12:46 +00:00
}
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
void CmndHotPlugTime(void)
{
if (XdrvMailbox.payload <= HOTPLUG_MAX) {
2021-06-11 17:14:12 +01:00
Settings->hotplug_scan = XdrvMailbox.payload;
2019-12-28 20:12:46 +00:00
HotPlugInit();
}
2021-06-11 17:14:12 +01:00
ResponseCmndNumber(Settings->hotplug_scan);
2019-12-28 20:12:46 +00:00
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xdrv32(uint8_t function)
{
bool result = false;
switch (function) {
case FUNC_EVERY_SECOND:
HotPlugEverySecond();
break;
case FUNC_COMMAND:
result = DecodeCommand(kHotPlugCommands, HotPlugCommand);
break;
case FUNC_PRE_INIT:
HotPlugInit();
break;
}
return result;
}
2019-12-30 13:23:37 +00:00
2019-12-28 20:12:46 +00:00
#endif //USE_HOTPLUG