Refactor iFan code

Refactor iFan code
This commit is contained in:
Theo Arends 2019-07-14 18:17:34 +02:00
parent a68d11c676
commit a053a2cc4a

View File

@ -48,20 +48,16 @@ uint8_t MaxFanspeed(void)
uint8_t GetFanspeed(void) uint8_t GetFanspeed(void)
{ {
if (ifan_fanspeed_timer) { if (ifan_fanspeed_timer) {
return ifan_fanspeed_goal; return ifan_fanspeed_goal; // Do not show sequence fanspeed
} else { } else {
uint8_t fanspeed = 0;
// if (SONOFF_IFAN02 == my_module_type) {
/* Fanspeed is controlled by relay 2, 3 and 4 as in Sonoff 4CH. /* Fanspeed is controlled by relay 2, 3 and 4 as in Sonoff 4CH.
000x = 0 000x = 0
001x = 1 001x = 1
011x = 2 011x = 2
101x = 3 (ifan02) or 100x = 3 (ifan03) 101x = 3 (ifan02) or 100x = 3 (ifan03)
*/ */
fanspeed = (uint8_t)(power &0xF) >> 1; uint8_t fanspeed = (uint8_t)(power &0xF) >> 1;
if (fanspeed) { fanspeed = (fanspeed >> 1) +1; } // 0, 1, 2, 3 if (fanspeed) { fanspeed = (fanspeed >> 1) +1; } // 0, 1, 2, 3
// }
return fanspeed; return fanspeed;
} }
} }
@ -104,20 +100,6 @@ void SonoffIFanSetFanspeed(uint8_t fanspeed, bool sequence)
/*********************************************************************************************/ /*********************************************************************************************/
void SonoffIfanSendAck()
{
// AA 55 01 04 00 00 05
serial_in_buffer[5] = 0;
uint8_t crc = 0;
for (uint32_t i = 2; i < 5; i++) {
crc += serial_in_buffer[i];
}
serial_in_buffer[6] = crc;
for (uint32_t i = 0; i < 7; i++) {
Serial.write(serial_in_buffer[i]);
}
}
void SonoffIfanReceived(void) void SonoffIfanReceived(void)
{ {
char svalue[32]; char svalue[32];
@ -149,7 +131,15 @@ void SonoffIfanReceived(void)
// AA 55 01 07 00 01 01 0A - Rf long press - forget RF codes // AA 55 01 07 00 01 01 0A - Rf long press - forget RF codes
buzzer_count = 6; // Beep three times buzzer_count = 6; // Beep three times
} }
SonoffIfanSendAck();
// Send Acknowledge - Copy first 5 bytes, reset byte 6 and store crc in byte 7
// AA 55 01 04 00 00 05
serial_in_buffer[5] = 0; // Ack
serial_in_buffer[6] = 0; // Crc
for (uint32_t i = 0; i < 7; i++) {
if ((i > 1) && (i < 6)) { serial_in_buffer[6] += serial_in_buffer[i]; }
Serial.write(serial_in_buffer[i]);
}
} }
bool SonoffIfanSerialInput(void) bool SonoffIfanSerialInput(void)
@ -160,6 +150,8 @@ bool SonoffIfanSerialInput(void)
ifan_receive_flag = true; ifan_receive_flag = true;
} }
if (ifan_receive_flag) { if (ifan_receive_flag) {
serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
if (serial_in_byte_counter == 8) {
// AA 55 01 01 00 01 01 04 - Wifi long press - start wifi setup // AA 55 01 01 00 01 01 04 - Wifi long press - start wifi setup
// AA 55 01 01 00 01 02 05 - Rf and Wifi short press // AA 55 01 01 00 01 02 05 - Rf and Wifi short press
// AA 55 01 04 00 01 00 06 - Fan 0 // AA 55 01 04 00 01 00 06 - Fan 0
@ -169,11 +161,7 @@ bool SonoffIfanSerialInput(void)
// AA 55 01 04 00 01 04 0A - Light // AA 55 01 04 00 01 04 0A - Light
// AA 55 01 06 00 01 01 09 - Buzzer // AA 55 01 06 00 01 01 09 - Buzzer
// AA 55 01 07 00 01 01 0A - Rf long press - forget RF codes // AA 55 01 07 00 01 01 0A - Rf long press - forget RF codes
serial_in_buffer[serial_in_byte_counter++] = serial_in_byte;
if (serial_in_byte_counter == 8) {
AddLogSerial(LOG_LEVEL_DEBUG); AddLogSerial(LOG_LEVEL_DEBUG);
uint8_t crc = 0; uint8_t crc = 0;
for (uint32_t i = 2; i < 7; i++) { for (uint32_t i = 2; i < 7; i++) {
crc += serial_in_buffer[i]; crc += serial_in_buffer[i];
@ -260,17 +248,17 @@ bool Xdrv22(uint8_t function)
if (IsModuleIfan()) { if (IsModuleIfan()) {
switch (function) { switch (function) {
case FUNC_MODULE_INIT:
result = SonoffIfanInit();
break;
case FUNC_EVERY_250_MSECOND: case FUNC_EVERY_250_MSECOND:
SonoffIfanUpdate(); SonoffIfanUpdate();
break; break;
case FUNC_SERIAL:
result = SonoffIfanSerialInput();
break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = SonoffIfanCommand(); result = SonoffIfanCommand();
break; break;
case FUNC_SERIAL: case FUNC_MODULE_INIT:
result = SonoffIfanSerialInput(); result = SonoffIfanInit();
break; break;
} }
} }