Skip to content

Commit

Permalink
优化程序
Browse files Browse the repository at this point in the history
  • Loading branch information
qlwz committed Sep 5, 2020
1 parent 7bdb84c commit d5f33aa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
4 changes: 2 additions & 2 deletions include/RadioReceive.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class RadioReceive
protected:
RCSwitch *mySwitch;
unsigned long lastVaue = 0;
int lastTime = 0;
int studyTime = 0;
unsigned long lastTime = 0;
unsigned long studyTime = 0;
Relay *relay;

public:
Expand Down
21 changes: 6 additions & 15 deletions include/Relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@

#define MAX_STUDY_RECEIVER_NUM 10 // 遥控最大学习数

const char HASS_DISCOVER_RELAY[] PROGMEM =
"{\"name\":\"%s_%d\","
"\"cmd_t\":\"%s\","
"\"stat_t\":\"%s\","
"\"pl_off\":\"off\","
"\"pl_on\":\"on\","
"\"avty_t\":\"%s\","
"\"pl_avail\":\"online\","
"\"pl_not_avail\":\"offline\"}";

static const uint8_t DEBOUNCED_STATE = 0b00000001;
static const uint8_t UNSTABLE_STATE = 0b00000010;
static const uint8_t BUTTON_DEBOUNCE_TIME = 50; // 消抖时间

#ifdef USE_RCSWITCH
class RadioReceive;
Expand All @@ -36,8 +27,7 @@ class Relay : public Module
// 按键
// 等待开关再次切换的时间(以毫秒为单位)。
// 300对我来说效果很好,几乎没有引起注意。 如果您不想使用此功能,请设置为0。
unsigned long specialFunctionTimeout = 300;
uint8_t buttonDebounceTime = 50;
uint16_t specialFunctionTimeout = 300;
unsigned long buttonTimingStart[4];
unsigned long buttonIntervalStart[4];
uint8_t buttonStateFlag[4];
Expand All @@ -46,8 +36,8 @@ class Relay : public Module
void cheackButton(uint8_t ch);

// PWM
int ledLevel = 0;
int ledLight = 2023;
uint16_t ledLevel = 0;
uint16_t ledLight = 2023;
bool ledUp = true;
bool canLed = true;
void led(uint8_t ch, bool isOn);
Expand All @@ -64,6 +54,7 @@ class Relay : public Module

void loadModule(uint8_t module);
void reportPower();
void reportChannel(uint8_t ch);

public:
uint8_t GPIO_PIN[GPIO_MAX];
Expand All @@ -79,7 +70,7 @@ class Relay : public Module
void init();
String getModuleName() { return F("relay"); }
String getModuleCNName();
String getModuleVersion() { return F("2020.02.29.2100"); }
String getModuleVersion() { return F("2020.09.05.2200"); }
String getModuleAuthor() { return F("情留メ蚊子"); }
bool moduleLed();

Expand Down
20 changes: 10 additions & 10 deletions src/RadioReceive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void RadioReceive::delAll()

void RadioReceive::loop()
{
if (studyCH != 0 && millis() > studyTime + 10000) // 10秒超时
if (studyCH != 0 && millis() - studyTime > 10000) // 10秒超时
{
Debug::AddInfo(PSTR("Receive study timeout"));
studyCH = 0;
Expand All @@ -51,7 +51,7 @@ void RadioReceive::loop()
unsigned long value = mySwitch->getReceivedValue();
//Debug::AddError(PSTR("315Mhz: %d"), value);
mySwitch->resetAvailable();
if (lastVaue == value && millis() < lastTime + 300)
if (lastVaue == value && millis() - lastTime < 300)
{
return;
}
Expand All @@ -77,8 +77,8 @@ void RadioReceive::loop()
//Debug::AddInfo(PSTR("study channel %d index %d"), ch, config.relay_study_index[ch]);
for (size_t i = 0; i < relay->config.study_index[ch]; i++)
{
//Debug::AddInfo(PSTR("study id %d"), (ch * 10) + i);
if (relay->config.study[(ch * 10) + i] == value)
//Debug::AddInfo(PSTR("study id %d"), (ch * MAX_STUDY_RECEIVER_NUM) + i);
if (relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + i] == value)
{
isOk = true;
Debug::AddInfo(PSTR("Received %d to channel %d"), value, ch + 1);
Expand All @@ -100,11 +100,11 @@ void RadioReceive::loop()
uint8_t index = relay->config.study_index[ch];
for (int i = 0; i <= index; ++i)
{
if (relay->config.study[(ch * 10) + i] == value)
if (relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + i] == value)
{
for (int j = i; j <= index - 1; j++)
{
relay->config.study[(ch * 10) + j] = relay->config.study[(ch * 10) + (j + 1)];
relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + j] = relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + (j + 1)];
}
relay->config.study_index[ch] = --index;
Config::saveConfig();
Expand All @@ -122,7 +122,7 @@ void RadioReceive::loop()
Debug::AddInfo(PSTR("study index %d %d"), ch, index);
for (uint8_t i = 0; i < index; i++)
{
if (relay->config.study[(ch * 10) + i] == value)
if (relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + i] == value)
{
Debug::AddInfo(PSTR("Received %d study to channel %d is has"), value, (ch) + 1);
studyCH = 0;
Expand All @@ -134,13 +134,13 @@ void RadioReceive::loop()
{
for (int j = 0; j < index - 1; j++)
{
relay->config.study[(ch * 10) + j] = relay->config.study[(ch * 10) + (j + 1)];
relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + j] = relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + (j + 1)];
}
relay->config.study[(ch * 10) + (MAX_STUDY_RECEIVER_NUM - 1)] = value;
relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + (MAX_STUDY_RECEIVER_NUM - 1)] = value;
}
else
{
relay->config.study[(ch * 10) + index] = value;
relay->config.study[(ch * MAX_STUDY_RECEIVER_NUM) + index] = value;
relay->config.study_index[ch] = ++index;
}
Config::saveConfig();
Expand Down
28 changes: 20 additions & 8 deletions src/Relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,15 @@ void Relay::mqttDiscovery(bool isEnable)
{
cmndTopic[strlen(cmndTopic) - 1] = ch + 49; // 48 + 1 + ch
powerStatTopic[strlen(powerStatTopic) - 1] = ch + 49; // 48 + 1 + ch
sprintf(message, HASS_DISCOVER_RELAY, UID, (ch + 1),
sprintf(message, PSTR("{\"name\":\"%s_%d\","
"\"cmd_t\":\"%s\","
"\"stat_t\":\"%s\","
"\"pl_off\":\"off\","
"\"pl_on\":\"on\","
"\"avty_t\":\"%s\","
"\"pl_avail\":\"online\","
"\"pl_not_avail\":\"offline\"}"),
UID, (ch + 1),
cmndTopic,
powerStatTopic,
availability.c_str());
Expand Down Expand Up @@ -663,7 +671,7 @@ void Relay::ledPWM(uint8_t ch, bool isOn)
{
if (!ledTicker.active())
{
ledTicker.attach_ms(config.led_time, std::bind(&Relay::ledTickerHandle, this));
ledTicker.attach_ms(config.led_time, []() { ((Relay *)module)->ledTickerHandle(); });
Debug::AddInfo(PSTR("ledTicker active"));
}
}
Expand Down Expand Up @@ -735,7 +743,6 @@ void Relay::switchRelay(uint8_t ch, bool isOn, bool isSave)
Debug::AddInfo(PSTR("invalid channel: %d"), ch);
return;
}
Debug::AddInfo(PSTR("Relay %d . . . %s"), ch + 1, isOn ? "ON" : "OFF");

if (isOn && config.power_mode == 1)
{
Expand All @@ -749,10 +756,10 @@ void Relay::switchRelay(uint8_t ch, bool isOn, bool isSave)
}

bitWrite(lastState, ch, isOn);
Debug::AddInfo(PSTR("Relay %d . . . %s"), ch + 1, isOn ? "ON" : "OFF");
digitalWrite(GPIO_PIN[GPIO_REL1 + ch], isOn ? HIGH : LOW);

powerStatTopic[strlen(powerStatTopic) - 1] = ch + 49; // 48 + 1 + ch
Mqtt::publish(powerStatTopic, isOn ? "on" : "off", globalConfig.mqtt.retain);
reportChannel(ch);

if (isSave && config.power_on_state > 0)
{
Expand All @@ -777,7 +784,7 @@ void Relay::cheackButton(uint8_t ch)
buttonTimingStart[ch] = millis();
buttonStateFlag[ch] ^= UNSTABLE_STATE;
}
else if (millis() - buttonTimingStart[ch] >= buttonDebounceTime)
else if (millis() - buttonTimingStart[ch] >= BUTTON_DEBOUNCE_TIME)
{
if (currentState != ((buttonStateFlag[ch] & DEBOUNCED_STATE) != 0))
{
Expand Down Expand Up @@ -867,7 +874,12 @@ void Relay::reportPower()
{
for (size_t ch = 0; ch < channels; ch++)
{
powerStatTopic[strlen(powerStatTopic) - 1] = ch + 49; // 48 + 1 + ch
Mqtt::publish(powerStatTopic, bitRead(lastState, ch) ? "on" : "off", globalConfig.mqtt.retain);
reportChannel(ch);
}
}

void Relay::reportChannel(uint8_t ch)
{
powerStatTopic[strlen(powerStatTopic) - 1] = ch + 49; // 48 + 1 + ch
Mqtt::publish(powerStatTopic, bitRead(lastState, ch) ? "on" : "off", globalConfig.mqtt.retain);
}

0 comments on commit d5f33aa

Please sign in to comment.