Skip to content

Commit 4463cfa

Browse files
authored
Merge pull request #4907 from uwekaditz/P016]-PullRequest
[P016] DecodeType UNKNOWN was not on the web interface (decodetypes UNKNOWN and UNUSED were mixed up!)
2 parents 1e5b7d1 + fae0205 commit 4463cfa

File tree

5 files changed

+154
-134
lines changed

5 files changed

+154
-134
lines changed

src/CustomIR-sample.h

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
// Set flags to enable (1) or disable (0) the DECODE_ and/or SEND_ feature for a specific IR device
2525
// To limit ESPEasy build-size you can disable DECODE_ or SEND_ flags for devices not needed
2626

27+
// Decode any arbitrary IR message into a 32-bit code value:
28+
// #define DECODE_HASH 0 // Instead of decoding using a standard encoding scheme, This will give a unique value for each different code (probably), for most code systems
29+
2730
// SEND-ONLY protocols:
2831
// #define SEND_GLOBALCACHE 0 // Is used by many sending protocols, so should probably be left to default
2932
// #define SEND_PRONTO 0
@@ -34,6 +37,7 @@
3437
// Standard: Use defaults for up to library version 2.8.2
3538
// Change as desired after copying CustomIR-sample.h to CustomIR.h
3639

40+
// #define DECODE_HASH 0
3741
// #define DECODE_RC5 0
3842
// #define SEND_RC5 0
3943
// #define DECODE_RC6 0

src/_P016_IR.ino

+79-62
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
// IRSENDAC,'{"protocol":"COOLIX","power":"on","mode":"dry","fanspeed":"auto","temp":22,"swingv":"max","swingh":"off"}'
2424

2525
/** Changelog:
26+
* 2024-01-26 uwekaditz: Decode type UNKNOWN was not added to the web settings
27+
* Decode types UNKNOWN and UNUSED were mixed up
28+
* Workaround for decode type UNKNOWN is not necessary
29+
* Initalisation of the variables with decode type UNUSED
30+
* 2024-01-23 uwekaditz: Use the new property addToQueue in ExecuteCommand_all() due to the lack of resources
31+
* Using strformat() for the debug messages
32+
* Heap and memory can be reported (P016_CHECK_HEAP)
33+
* 2023-12-11 uwekaditz: Add protocol RAW to UI if 'Accept DecodeType UNKNOWN' is set
34+
* Note: for decoding a RAW message DECODE_HASH must be set
35+
* uint64ToString() in debug message in PLUGIN_TEN_PER_SECOND can not handle decode_type_t::UNKNOWN (-1),
36+
* changed to ll2String()
2637
* 2022-08-08 tonhuisman: Optionally (compile-time) disable command handling by setting #define P016_FEATURE_COMMAND_HDNLING 0
2738
* Make reserved buffer size for receiver configurable 100..1024 uint16_t = 200-2048 bytes
2839
* Change UI to show buffer size in bytes instead of 'units' to avoid confusion.
@@ -42,6 +53,9 @@
4253
# ifdef P016_P035_Extended_AC
4354
# include <IRac.h>
4455
# endif // ifdef P016_P035_Extended_AC
56+
# ifdef P016_CHECK_HEAP
57+
# include "src/Helpers/Memory.h"
58+
# endif // ifdef P016_CHECK_HEAP
4559

4660
# define PLUGIN_016
4761
# define PLUGIN_ID_016 16
@@ -56,6 +70,14 @@
5670
# endif // ifndef P016_SEND_IR_TO_CONTROLLER
5771

5872
// History
73+
// @uwekaditz: 2024-01-23
74+
// CHG: Use the new property addToQueue in ExecuteCommand_all() due to the lack of resources
75+
// NEW: Heap and memory can be reported (P016_CHECK_HEAP)
76+
// MSG: Using strformat() for the debug messages
77+
// @uwekaditz: 2023-12-11
78+
// NEW: Add protocol RAW to UI if 'Accept DecodeType UNKNOWN' is set
79+
// MSG: for decoding a RAW message DECODE_HASH must be set
80+
// FIX: uint64ToString() in debug message in PLUGIN_TEN_PER_SECOND can not handle decode_type_t::UNKNOWN (-1), changed to ll2String()
5981
// @tonhuisman: 2022-08-08
6082
// FIX: Resolve high memory use bu having the default buffer size reduced from 1024 to 100, and make that a setting
6183
// @tonhuisman: 2021-08-05
@@ -145,6 +167,12 @@ const uint16_t kMinUnknownSize = 12;
145167

146168
IRrecv *irReceiver = nullptr;
147169
bool bEnableIRcodeAdding = false;
170+
171+
# ifdef P016_CHECK_HEAP
172+
uint32_t fMem = 0;
173+
uint32_t fFreeStack = 0;
174+
# endif // ifdef P016_CHECK_HEAP
175+
148176
# ifdef P016_P035_USE_RAW_RAW2
149177

150178
/* *INDENT-OFF* */
@@ -155,17 +183,9 @@ boolean displayRawToReadableB32Hex(String& outputStr, decode_results results);
155183
# ifdef PLUGIN_016_DEBUG
156184
void P016_infoLogMemory(const __FlashStringHelper *text) {
157185
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
158-
String log;
159-
160-
if (log.reserve(40 + strlen_P((PGM_P)text))) {
161-
log += F("P016: Free memory ");
162-
log += text;
163-
log += F(": ");
164-
log += FreeMem();
165-
log += F(" stack: ");
166-
log += getCurrentFreeStack();
167-
addLogMove(LOG_LEVEL_INFO, log);
168-
}
186+
addLogMove(LOG_LEVEL_INFO, strformat(
187+
F("P016: %s FreeMem: %d FreeStack:%d"),
188+
text, FreeMem(), getCurrentFreeStack()));
169189
}
170190
}
171191

@@ -181,11 +201,11 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
181201
{
182202
Device[++deviceCount].Number = PLUGIN_ID_016;
183203
Device[deviceCount].Type = DEVICE_TYPE_SINGLE;
184-
#if P016_SEND_IR_TO_CONTROLLER
185-
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_STRING;
186-
#else
187-
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_ULONG;
188-
#endif
204+
# if P016_SEND_IR_TO_CONTROLLER
205+
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_STRING;
206+
# else // if P016_SEND_IR_TO_CONTROLLER
207+
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_ULONG;
208+
# endif // if P016_SEND_IR_TO_CONTROLLER
189209
Device[deviceCount].Ports = 0;
190210
Device[deviceCount].PullUpOption = true;
191211
Device[deviceCount].InverseLogicOption = true;
@@ -357,8 +377,12 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
357377

358378
int protocolCount = 0;
359379

360-
for (int i = 0; i < size; i++) {
361-
const String protocol = typeToString(static_cast<decode_type_t>(i), false);
380+
for (int i = static_cast<int>(decode_type_t::UNKNOWN); i < size; i++) {
381+
const String protocol = typeToString(static_cast<decode_type_t>(i), false);
382+
383+
if ((!bAcceptUnknownType) && (static_cast<decode_type_t>(i) == UNKNOWN)) {
384+
continue;
385+
}
362386

363387
if (protocol.length() > 1) {
364388
decodeTypeOptions.push_back(i);
@@ -371,13 +395,7 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
371395
}
372396

373397
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
374-
String log; // Log this always
375-
376-
if (log.reserve(30)) {
377-
log += F("IR: available decodetypes: ");
378-
log += protocolCount;
379-
addLogMove(LOG_LEVEL_INFO, log);
380-
}
398+
addLogMove(LOG_LEVEL_INFO, strformat(F("IR: available decodetypes: %d"), protocolCount));
381399
}
382400

383401
const String P016_HEX_INPUT_PATTERN = F("(0x)?[0-9a-fA-F]{0,16}"); // 16 nibbles = 64 bit, 0x prefix is allowed but not added by
@@ -391,7 +409,7 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
391409
html_table_header(F("Alt. Decode type"));
392410
html_table_header(F("Repeat"));
393411
html_table_header(F("Alt. Code [Hex]"));
394-
html_TR(); //added to make "tworow" work
412+
html_TR(); // added to make "tworow" work
395413

396414
int rowCnt = 0;
397415

@@ -597,8 +615,14 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
597615
{
598616
decode_results results;
599617

618+
# ifdef P016_CHECK_HEAP
619+
fMem = FreeMem();
620+
fFreeStack = getCurrentFreeStack();
621+
# endif // ifdef P016_CHECK_HEAP
622+
600623
if (irReceiver->decode(&results))
601624
{
625+
success = true;
602626
yield(); // Feed the WDT after a time expensive decoding procedure
603627

604628
if (results.overflow)
@@ -612,34 +636,29 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
612636
if ((results.decode_type != decode_type_t::UNKNOWN) || (bitRead(PCONFIG_LONG(0), P016_BitAcceptUnknownType)))
613637
{
614638
{
615-
String output;
616-
output.reserve(100); // Length of expected string, needed for strings > 11 chars
617639
// String output = String(F("IRSEND,")) + typeToString(results.decode_type, results.repeat) + ',' +
618640
// resultToHexidecimal(&results)
619641
// + ',' + uint64ToString(results.bits);
620642
// addLog(LOG_LEVEL_INFO, output); //Show the appropriate command to the user, so he can replay the message via P035 // Old
621643
// style
622644
// command
623-
output += F("{\"protocol\":\"");
624-
output += typeToString(results.decode_type, results.repeat);
625-
output += F("\",\"data\":\"");
626-
output += resultToHexidecimal(&results);
627-
output += F("\",\"bits\":");
628-
output += uint64ToString(results.bits);
629-
output += '}';
645+
event->String2 = strformat(
646+
F("{\"protocol\":\"%s\",\"data\":\"%s\",\"bits\":%s}"),
647+
typeToString(results.decode_type, results.repeat).c_str(),
648+
resultToHexidecimal(&results).c_str(),
649+
uint64ToString(results.bits).c_str());
630650

631651
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
632652
String Log;
633653

634-
if (Log.reserve(output.length() + 22)) {
654+
if (Log.reserve(event->String2.length() + 22)) {
635655
Log += F("IRSEND,\'");
636-
Log += output;
637-
Log += F("\' type: 0x");
638-
Log += uint64ToString(results.decode_type);
656+
Log += event->String2;
657+
Log += F("\' type as int: ");
658+
Log += ll2String(results.decode_type);
639659
addLogMove(LOG_LEVEL_INFO, Log); // JSON representation of the command
640660
}
641661
}
642-
event->String2 = std::move(output);
643662
}
644663

645664
# if P016_FEATURE_COMMAND_HANDLING
@@ -659,17 +678,22 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
659678
if (strCode.length() <= P16_Cchars) {
660679
iCode += hexToULL(strCode);
661680

662-
if (iCodeDecodeType == decode_type_t::UNKNOWN) {
663-
// set iCodeDecodeType UNKNOWN to RAW, otherwise AddCode() or ExecuteCode() will fail
664-
iCodeDecodeType = decode_type_t::RAW;
665-
}
666-
667681
if (bitRead(PCONFIG_LONG(0), P016_BitAddNewCode) && bEnableIRcodeAdding) {
668682
P016_data->AddCode(iCode, iCodeDecodeType, iCodeFlags); // add code if not saved so far
669683
}
670684

685+
# ifdef P016_CHECK_HEAP
686+
687+
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
688+
addLogMove(LOG_LEVEL_INFO, strformat(
689+
F("Before decode: FreeMem: %d FreeStack:%d / After: FreeMem: %d FreeStack:%d"),
690+
fMem, fFreeStack,
691+
FreeMem(), getCurrentFreeStack()));
692+
}
693+
# endif // ifdef P016_CHECK_HEAP
694+
671695
if (bitRead(PCONFIG_LONG(0), P016_BitExecuteCmd)) {
672-
P016_data->ExecuteCode(iCode, iCodeDecodeType, iCodeFlags); // execute command for code if available
696+
success = P016_data->ExecuteCode(iCode, iCodeDecodeType, iCodeFlags); // execute command for code if available
673697
}
674698
}
675699
}
@@ -725,18 +749,12 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
725749
state.sleep = -1;
726750
state.clock = -1;
727751

728-
String description = IRAcUtils::resultAcToString(&results);
752+
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
753+
String description = IRAcUtils::resultAcToString(&results);
729754

730-
if (!description.isEmpty()) {
731-
if (loglevelActiveFor(LOG_LEVEL_INFO)) {
755+
if (!description.isEmpty()) {
732756
// If we got a human-readable description of the message, display it.
733-
String log;
734-
735-
if (log.reserve(10 + description.length())) {
736-
log += F("AC State: ");
737-
log += description;
738-
addLogMove(LOG_LEVEL_INFO, log);
739-
}
757+
addLogMove(LOG_LEVEL_INFO, strformat(F("AC State: %s"), description.c_str()));
740758
}
741759
}
742760

@@ -824,16 +842,15 @@ boolean Plugin_016(uint8_t function, struct EventStruct *event, String& string)
824842
}
825843
# endif // P016_P035_Extended_AC
826844

827-
#if !P016_SEND_IR_TO_CONTROLLER
845+
# if !P016_SEND_IR_TO_CONTROLLER
828846
{
829847
unsigned long IRcode = results.value;
830848
UserVar.setSensorTypeLong(event->TaskIndex, IRcode);
831849
}
832-
#endif
850+
# endif // if !P016_SEND_IR_TO_CONTROLLER
833851
sendData(event);
852+
break;
834853
}
835-
success = true;
836-
break;
837854
}
838855
}
839856
return success;
@@ -1041,7 +1058,7 @@ unsigned int storeB32Hex(char out[], unsigned int iOut, unsigned int val)
10411058

10421059
void enableIR_RX(boolean enable)
10431060
{
1044-
#ifdef PLUGIN_016
1061+
#ifdef PLUGIN_016
10451062

10461063
if (irReceiver == 0) { return; }
10471064

@@ -1050,5 +1067,5 @@ void enableIR_RX(boolean enable)
10501067
} else {
10511068
irReceiver->disableIRIn(); // Stop the receiver
10521069
}
1053-
#endif // PLUGIN_016
1070+
#endif // PLUGIN_016
10541071
}

src/src/Helpers/StringConverter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ String strformat(const String& format, ...)
117117
{
118118
va_list arg;
119119
va_start(arg, format); // variable args start after parameter 'format'
120-
char temp[64];
120+
static char temp[64];
121121
char* buffer = temp;
122122
int len = vsnprintf_P(temp, sizeof(temp), format.c_str(), arg);
123123
va_end(arg);
@@ -147,7 +147,7 @@ String strformat(const __FlashStringHelper * format, ...)
147147
{
148148
va_list arg;
149149
va_start(arg, format); // variable args start after parameter 'format'
150-
char temp[64];
150+
static char temp[64];
151151
char* buffer = temp;
152152
int len = vsnprintf_P(temp, sizeof(temp), (PGM_P)format, arg);
153153
va_end(arg);

0 commit comments

Comments
 (0)