-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLogicChannel.cpp
More file actions
3090 lines (2945 loc) · 110 KB
/
LogicChannel.cpp
File metadata and controls
3090 lines (2945 loc) · 110 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "LogicChannel.h"
#include "Logic.h"
#include "LogicFunction.h"
#include "OpenKNX.h"
#include "PCA9632.h"
#ifndef abs
#define abs(x) ((x) > 0 ? (x) : -(x))
#endif
Timer &LogicChannel::sTimer = Timer::instance();
TimerRestore &LogicChannel::sTimerRestore = TimerRestore::instance(); // singleton
uint8_t LogicChannel::pLoadCounterMax = 0;
uint8_t LogicChannel::pLoadChannel = 0;
#if LOGIC_TRACE
char LogicChannel::sFilter[30] = "";
char LogicChannel::sTimeOutputBuffer[10] = "";
#endif
/******************************
* Constructors
* ***************************/
LogicChannel::LogicChannel(uint8_t iChannelNumber)
{
_channelIndex = iChannelNumber;
// initialize most important runtime fields
pCurrentPipeline = 0;
pValidActiveIO = 0;
pTriggerIO = 0;
pCurrentIn = BIT_INITIAL_GATE;
pCurrentOut = BIT_OUTPUT_INITIAL; // tri-state output, at the beginning we are undefined
}
LogicChannel::~LogicChannel()
{
}
/******************************
* Debug helper
* ***************************/
#if LOGIC_TRACE
char *LogicChannel::logTimeBase(uint16_t iParamIndex)
{
uint16_t lTime = getWordParam(iParamIndex);
switch (lTime & 0xC000)
{
case 0x0000:
/* seconds */
sprintf(sTimeOutputBuffer, "%5i s", lTime & 0x3FFF);
break;
case 0x4000:
/* minutes */
sprintf(sTimeOutputBuffer, "%5i m", lTime & 0x3FFF);
break;
case 0x8000:
/* hours */
sprintf(sTimeOutputBuffer, "%5i h", lTime & 0x3FFF);
break;
case 0xC000:
/* 1/10 s*/
sprintf(sTimeOutputBuffer, "%5.1f s", (lTime & 0x3FFF) / 10.0);
break;
default:
sprintf(sTimeOutputBuffer, "(no time)");
break;
}
return sTimeOutputBuffer;
}
// channel debug output
int LogicChannel::logChannel(const char *iFormat, ...)
{
// char lBuffer[256];
// uint8_t lBufferPos = channelIndex() * 2;
// memset(lBuffer, ' ', lBufferPos + 1);
// lBuffer[lBufferPos] = 'C';
// sprintf(lBuffer + lBufferPos + 1, "%02i-", channelIndex() + 1);
// va_list lArgs;
// va_start(lArgs, iFormat);
// int lResult = vsnprintf(lBuffer + lBufferPos + 4, 252 - lBufferPos, iFormat, lArgs);
// va_end(lArgs);
// logInfoP(lBuffer);
// return lResult;
// char lBuffer[255];
// sprintf(lBuffer, "C%02i-", channelIndex() + 1);
logIndent((channelIndex() % 20) + 1);
va_list lArgs;
va_start(lArgs, iFormat);
logTraceP(iFormat, lArgs);
va_end(lArgs);
logIndent(0);
return 1;
}
bool LogicChannel::debugFilter()
{
char lChannel[3];
bool lReturn = true;
if (sFilter[0])
{
sprintf(lChannel, "%02i", channelIndex() + 1);
lReturn = (sFilter[0] == lChannel[0]) && (sFilter[1] == lChannel[1]);
for (uint8_t i = 2; !lReturn && sFilter[i] && i < 30; i = i + 2)
{
lReturn = (sFilter[i] == lChannel[0]) && (sFilter[i + 1] == lChannel[1]);
}
}
return lReturn;
}
#endif
/******************************
* Parameter helper
* ***************************/
uint32_t LogicChannel::calcParamIndex(uint16_t iParamIndex)
{
uint32_t lResult = iParamIndex + channelIndex() * LOG_ParamBlockSize + LOG_ParamBlockOffset;
return lResult;
}
uint8_t LogicChannel::getByteParam(uint16_t iParamIndex)
{
uint8_t lValue = knx.paramByte(calcParamIndex(iParamIndex));
return lValue;
}
int8_t LogicChannel::getSByteParam(uint16_t iParamIndex)
{
uint8_t *lRef = knx.paramData(calcParamIndex(iParamIndex));
return lRef[0];
}
uint16_t LogicChannel::getWordParam(uint16_t iParamIndex)
{
return knx.paramWord(calcParamIndex(iParamIndex));
}
int16_t LogicChannel::getSWordParam(uint16_t iParamIndex)
{
uint8_t *lRef = knx.paramData(calcParamIndex(iParamIndex));
return lRef[0] * 256 + lRef[1];
}
uint32_t LogicChannel::getIntParam(uint16_t iParamIndex)
{
return knx.paramInt(calcParamIndex(iParamIndex));
}
int32_t LogicChannel::getSIntParam(uint16_t iParamIndex)
{
return knx.paramInt(calcParamIndex(iParamIndex));
}
float LogicChannel::getFloatParam(uint16_t iParamIndex)
{
uint16_t lIndex = calcParamIndex(iParamIndex);
float lFloat = knx.paramFloat(lIndex, Float_Enc_IEEE754Single);
return lFloat;
}
uint8_t *LogicChannel::getStringParam(uint16_t iParamIndex)
{
uint16_t lIndex = calcParamIndex(iParamIndex);
return knx.paramData(lIndex);
}
uint32_t LogicChannel::getTimeDelayParam(uint16_t iParamIndex, bool iAsSeconds /* = false */)
{
return getDelayPattern(calcParamIndex(iParamIndex), iAsSeconds);
}
/*******************************
* ComObject helper
* ****************************/
// static
uint16_t LogicChannel::calcKoNumber(uint8_t iIOIndex, uint8_t iChannelId)
{
// do not use iIOIndex = 0
uint16_t lKoNumber = LOG_KoOffset + iChannelId * LOG_KoBlockSize;
switch (iIOIndex)
{
case IO_Input1:
lKoNumber += LOG_KoKOfE1;
break;
case IO_Input2:
lKoNumber += LOG_KoKOfE2;
break;
case IO_Output:
lKoNumber += LOG_KoKOfO;
break;
default:
lKoNumber = iChannelId;
break;
}
return lKoNumber;
}
// static
GroupObject *LogicChannel::getKoForChannel(uint8_t iIOIndex, uint8_t iChannelId)
{
return &knx.getGroupObject(calcKoNumber(iIOIndex, iChannelId));
}
uint16_t LogicChannel::calcKoNumber(uint8_t iIOIndex)
{
return LogicChannel::calcKoNumber(iIOIndex, channelIndex());
}
GroupObject *LogicChannel::getKo(uint8_t iIOIndex)
{
// new behaviour since 4.0: We support also external KO for input
GroupObject *lKo = nullptr;
int16_t lExternalAccess = 0;
uint8_t lAbsRel = 0;
if (iIOIndex == IO_Input1)
{
lExternalAccess = ParamLOG_fE1OtherKORel;
lAbsRel = ParamLOG_fE1UseOtherKO;
}
else if (iIOIndex == IO_Input2)
{
lExternalAccess = ParamLOG_fE2OtherKORel;
lAbsRel = ParamLOG_fE2UseOtherKO;
}
uint16_t lKoNumber = calcKoNumber(iIOIndex);
switch (lAbsRel)
{
case VAL_AbsRel_Absolute:
if (lExternalAccess > 0 && lExternalAccess < MAIN_MaxKoNumber)
lKoNumber = lExternalAccess;
break;
case VAL_AbsRel_Relative:
{
int16_t lNewKoNumber = lKoNumber + lExternalAccess;
if (lNewKoNumber > 0 && lNewKoNumber < MAIN_MaxKoNumber)
lKoNumber = lNewKoNumber;
}
break;
default:
break;
}
lKo = &knx.getGroupObject(lKoNumber);
return lKo;
}
Dpt &LogicChannel::getKoDPT(uint8_t iIOIndex, bool iHandleDpt2asByte /* = false */)
{
uint8_t lDpt;
switch (iIOIndex)
{
case IO_Input1:
lDpt = ParamLOG_fE1Dpt;
break;
case IO_Input2:
lDpt = ParamLOG_fE2Dpt;
break;
case IO_Output:
lDpt = ParamLOG_fODpt;
break;
default:
lDpt = 0;
break;
}
if (iHandleDpt2asByte && lDpt == VAL_DPT_2) // DPT2
lDpt = VAL_DPT_5; // handle DPT2 as DPT5 for internal processing
return getDPT(lDpt);
}
uint16_t LogicChannel::checkAdditionalWrite(bool iOn)
{
int16_t lKoNumber = 0;
uint16_t lAbsRel = 0;
if (iOn)
{
lAbsRel = ParamLOG_fOOnKOSend;
switch (lAbsRel)
{
case VAL_AbsRel_Absolute:
lKoNumber = ParamLOG_fOOnKOSendNumber;
break;
case VAL_AbsRel_Relative:
lKoNumber = ParamLOG_fOOnKOSendNumberRel + calcKoNumber(IO_Output);
break;
default:
lKoNumber = 0;
break;
}
}
else
{
lAbsRel = ParamLOG_fOOffKOSend;
switch (lAbsRel)
{
case VAL_AbsRel_Absolute:
lKoNumber = ParamLOG_fOOffKOSendNumber;
break;
case VAL_AbsRel_Relative:
lKoNumber = ParamLOG_fOOffKOSendNumberRel + calcKoNumber(IO_Output);
break;
default:
lKoNumber = 0;
break;
}
}
if (lKoNumber < 1 || lKoNumber > MAIN_MaxKoNumber)
lKoNumber = 0;
return lKoNumber;
}
void LogicChannel::knxWrite(uint8_t iIOIndex, KNXValue &iValue, bool iOn, bool iAdditional /* = true */)
{
bool lSendOnChanged = ParamLOG_fOSendOnChange;
GroupObject *lKo = getKo(iIOIndex);
bool lChanged = false;
Dpt &lDpt = getKoDPT(iIOIndex, true);
if (lSendOnChanged)
lChanged = lKo->valueNoSendCompare(iValue, lDpt);
else
lKo->value(iValue, lDpt);
if (lChanged)
lKo->objectWritten();
if (iAdditional)
{
uint16_t lKoNumber = checkAdditionalWrite(iOn);
if (lKoNumber > 0)
{
lKo = &knx.getGroupObject(lKoNumber);
lChanged = false;
if (lSendOnChanged)
lChanged = lKo->valueNoSendCompare(iValue, lDpt);
else
lKo->value(iValue, lDpt);
if (lChanged)
lKo->objectWritten();
}
}
}
// write value to bus
void LogicChannel::knxWriteBool(uint8_t iIOIndex, bool iValue, bool iOn)
{
#if LOGIC_TRACE
logChannel("knxWrite KO %d bool value %d", calcKoNumber(iIOIndex), iValue);
#endif
KNXValue lValue = iValue;
knxWrite(iIOIndex, lValue, iOn);
}
void LogicChannel::knxWriteInt(uint8_t iIOIndex, int32_t iValue, bool iOn)
{
#if LOGIC_TRACE
logChannel("knxWrite KO %d int value %li", calcKoNumber(iIOIndex), iValue);
#endif
KNXValue lValue = iValue;
knxWrite(iIOIndex, lValue, iOn);
}
// void LogicChannel::knxWriteRawInt(uint8_t iIOIndex, int32_t iValue, bool iOn)
// {
// #if LOGIC_TRACE
// logChannel("knxWrite KO %d int value %li", calcKoNumber(iIOIndex), iValue);
// #endif
// GroupObject *lKo = getKo(iIOIndex);
// uint8_t *lValueRef = lKo->valueRef();
// *lValueRef = iValue;
// lKo->objectWritten();
// uint16_t lKoNumber = checkAdditionalWrite(iOn);
// if (lKoNumber > 0)
// {
// GroupObject &lKo = knx.getGroupObject(lKoNumber);
// uint8_t *lValueRef = lKo.valueRef();
// *lValueRef = iValue;
// lKo.objectWritten();
// }
// }
void LogicChannel::knxWriteFloat(uint8_t iIOIndex, float iValue, bool iOn)
{
#if LOGIC_TRACE
logChannel("knxWrite KO %d float value %f", calcKoNumber(iIOIndex), iValue);
#endif
KNXValue lValue = iValue;
knxWrite(iIOIndex, lValue, iOn);
}
void LogicChannel::knxWriteString(uint8_t iIOIndex, const char *iValue)
{
#if LOGIC_TRACE
logChannel("knxWrite KO %d string value %s", calcKoNumber(iIOIndex), iValue);
#endif
KNXValue lValue = iValue;
knxWrite(iIOIndex, lValue, false, false);
}
// send read request on bus
void LogicChannel::knxRead(uint8_t iIOIndex)
{
#if LOGIC_TRACE
logChannel("knxReadRequest send from KO %d", calcKoNumber(iIOIndex));
#endif
getKo(iIOIndex)->requestObjectRead();
}
// send reset device to bus
void LogicChannel::knxResetDevice(uint16_t iParamIndex)
{
uint16_t lAddress = getWordParam(iParamIndex);
uint16_t lLocalAddress = knx.individualAddress();
#if LOGIC_TRACE
uint8_t lHigh = lAddress >> 8;
logChannel("knxResetDevice with PA %d.%d.%d", lHigh >> 4, lHigh & 0xF, lAddress & 0xFF);
#endif
if (lAddress == lLocalAddress)
{
// here we have to do a local restart (restart own device)
if (knx.beforeRestartCallback() != 0)
knx.beforeRestartCallback()();
// Flush the Flash before resetting
knx.writeMemory();
knx.platform().restart();
}
else
knx.restart(lAddress);
}
// turn on/off RGBLed
void LogicChannel::setRGBColor(uint16_t iParamIndex)
{
#ifdef I2C_RGBLED_DEVICE_ADDRESS
if ((getByteParam(LOG_fAlarm) & LOG_fAlarmMask) || !knx.getGroupObject(LOG_KoLedLock).value(getDPT(VAL_DPT_1)))
{
uint32_t lRGBColor = getIntParam(iParamIndex);
uint8_t lRed = lRGBColor >> 24;
uint8_t lGreen = lRGBColor >> 16;
uint8_t lBlue = lRGBColor >> 8;
// we have to map colors to correct pins
switch (ParamLOG_LedMapping)
{
case 2: // R, B, G
PCA9632_SetColor(lRed, lBlue, lGreen);
break;
case 3: // G, R, B
PCA9632_SetColor(lGreen, lRed, lBlue);
break;
case 4: // G, B, R
PCA9632_SetColor(lGreen, lBlue, lRed);
break;
case 5: // B, G, R
PCA9632_SetColor(lBlue, lGreen, lRed);
break;
case 6: // B, R, G
PCA9632_SetColor(lBlue, lRed, lGreen);
break;
default: // R, G, B
PCA9632_SetColor(lRed, lGreen, lBlue);
break;
}
}
else
{
// in case of lock we turn off led
PCA9632_SetColor(0, 0, 0);
}
#endif
}
// turn on/off Buzzer
void LogicChannel::setBuzzer(uint16_t iParamIndex)
{
#ifdef BUZZER_PIN
// check for global lock and alarm
if (ParamLOG_fAlarm || !KoLOG_BuzzerLock.value(getDPT(VAL_DPT_1)))
{
switch (getByteParam(iParamIndex))
{
case VAL_Buzzer_Off:
noTone(BUZZER_PIN);
break;
case VAL_Buzzer_Loud:
tone(BUZZER_PIN, ParamLOG_BuzzerLoud);
break;
case VAL_Buzzer_Silent:
tone(BUZZER_PIN, ParamLOG_BuzzerSilent);
break;
case VAL_Buzzer_Normal:
tone(BUZZER_PIN, ParamLOG_BuzzerNormal);
break;
default:
break;
}
}
else
{
// in case of lock we turn off buzzer
noTone(BUZZER_PIN);
}
#endif
}
/********************************
* Logic helper functions
* *****************************/
// we get an dpt dependant parameter value for different
// input evaluation
LogicValue LogicChannel::getParamForDelta(uint8_t iDpt, uint16_t iParamIndex)
{
if (iDpt == VAL_DPT_9 || iDpt == VAL_DPT_14)
{
LogicValue lValue = getFloatParam(iParamIndex);
return lValue;
}
else
{
LogicValue lValue = (int32_t)getIntParam(iParamIndex);
return lValue;
}
}
// we get here numeric params by their DPT
// DPT1,2,5,6,7,8,17,232 => straight forward int values
// DPT2,17 => straight forward byte values
// DPT5001 => scale down to [0..100]
// DPT9 => transport as float
LogicValue LogicChannel::getParamByDpt(uint8_t iDpt, uint16_t iParamIndex)
{
switch (iDpt)
{
case VAL_DPT_1:
{
LogicValue lValue = getByteParam(iParamIndex) != 0;
return lValue;
}
case VAL_DPT_2:
case VAL_DPT_3:
case VAL_DPT_5:
case VAL_DPT_17:
case VAL_DPT_5001:
{
LogicValue lValue = getByteParam(iParamIndex);
return lValue;
}
case VAL_DPT_6:
{
LogicValue lValue = getSByteParam(iParamIndex);
return lValue;
}
case VAL_DPT_7:
{
LogicValue lValue = getWordParam(iParamIndex);
return lValue;
}
case VAL_DPT_8:
{
LogicValue lValue = getSWordParam(iParamIndex);
return lValue;
}
case VAL_DPT_232:
{
LogicValue lValue = getIntParam(iParamIndex);
return lValue;
}
case VAL_DPT_9:
case VAL_DPT_14:
{
LogicValue lValue = getFloatParam(iParamIndex);
return lValue;
}
case VAL_DPT_12:
{
LogicValue lValue = getIntParam(iParamIndex);
return lValue;
}
case VAL_DPT_13:
{
LogicValue lValue = getSIntParam(iParamIndex);
return lValue;
}
default:
{
LogicValue lValue = getIntParam(iParamIndex);
return lValue;
}
}
}
// on input level, we have just numeric values, so all DPT are converted to int:
// DPT1,2,5,6,7,8,17,232 => straight forward
// DPT5001 => scale down to [0..100]
// DPT9 => transport as float
LogicValue LogicChannel::getInputValue(uint8_t iIOIndex, uint8_t *eDpt)
{
// check for timer
if (ParamLOG_fLogic == VAL_Logic_Timer && iIOIndex == IO_Input1)
{
// timer value is handled as scene, of output type is scene
if (ParamLOG_fODpt == VAL_DPT_17)
{
*eDpt = VAL_DPT_17;
LogicValue lValue = (uint8_t)(pCurrentTimerValueNum > 0 ? pCurrentTimerValueNum - 1 : 0);
return lValue;
}
else
{
*eDpt = VAL_DPT_5;
LogicValue lValue = pCurrentTimerValueNum;
return lValue;
}
}
else
{
// check for constant
uint16_t lParamIndex = (iIOIndex == IO_Input1) ? LOG_fE1Convert : LOG_fE2Convert;
uint8_t lConvert = (getByteParam(lParamIndex) & LOG_fE1ConvertMask) >> LOG_fE1ConvertShift;
lParamIndex = (iIOIndex == IO_Input1) ? LOG_fE1Dpt : LOG_fE2Dpt;
*eDpt = getByteParam(lParamIndex);
if (lConvert == VAL_InputConvert_Constant)
{
// input value is a constant stored in param memory
uint16_t lParamIndex = (iIOIndex == IO_Input1) ? LOG_fE1LowDelta : LOG_fE2LowDelta;
LogicValue lValue = getParamByDpt(*eDpt, lParamIndex);
return lValue;
}
else
{
return getKoValue(iIOIndex, *eDpt);
}
}
}
LogicValue LogicChannel::getOtherKoValue(uint16_t iKoNumber, uint8_t iDptParamIndex)
{
GroupObject *lKo = &knx.getGroupObject(iKoNumber);
uint8_t lDpt = getByteParam(iDptParamIndex);
return getKoValue(lKo, lDpt, false);
}
LogicValue LogicChannel::getKoValue(uint8_t iIOIndex, uint8_t iDpt)
{
GroupObject *lKo = getKo(iIOIndex);
return getKoValue(lKo, iDpt, iIOIndex < IO_Output);
}
LogicValue LogicChannel::getKoValue(GroupObject *iKo, uint8_t iDpt, bool iIsInput)
{
LogicValue lValue = false;
// based on dpt, we read the correct c type.
switch (iDpt)
{
case VAL_DPT_2:
{
lValue = iKo->valueRef()[0];
break;
}
case VAL_DPT_3:
{
lValue = (uint8_t)(iKo->valueRef()[0] & 0x0F);
break;
}
case VAL_DPT_6:
{
lValue = (int8_t)iKo->value(getDPT(VAL_DPT_6));
break;
}
case VAL_DPT_8:
{
lValue = (int16_t)iKo->value(getDPT(VAL_DPT_8));
break;
}
case VAL_DPT_12:
{
lValue = (uint32_t)iKo->value(getDPT(VAL_DPT_12));
break;
}
// case VAL_DPT_7:
// LogicValue lValue = lKo->valueRef()[0] + 256 * lKo->valueRef()[1];
// break;
// case VAL_DPT_232:
// lValue =
// lKo->valueRef()[0] + 256 * lKo->valueRef()[1] + 65536 * lKo->valueRef()[2];
// break;
case VAL_DPT_9:
case VAL_DPT_14:
{
lValue = (float)iKo->value(getDPT(iDpt));
break;
} // case VAL_DPT_17:
default:
{
lValue = (int32_t)iKo->value(getDPT(iDpt));
break;
}
}
bool lInitial = !iKo->initialized();
// bool lInitial = iKo->commFlag() == ComFlag::Uninitialized;
// // inputs might send their readRequests to the bus, in this case they are uninitialized, but in state Transmitting
// if (!lInitial && !iKo->readEnable())
// lInitial = iKo->commFlag() == ComFlag::Transmitting;
// if (!lInitial && iIsInput && !(pCurrentOut & BIT_OUTPUT_INITIAL))
// lInitial = iKo->commFlag() == ComFlag::Transmitting;
// // this is the case when we restore a saved value and do not send it to the bus.
// if (iKo->commFlag() == ComFlag::Transmitting && iIsInput && (pCurrentOut & BIT_OUTPUT_INITIAL))
// iKo->commFlag(ComFlag::Ok);
lValue.isInitial(lInitial);
return lValue;
}
void LogicChannel::writeConstantValue(uint16_t iParamIndex, bool iOn)
{
uint8_t lDpt = getByteParam(LOG_fODpt);
switch (lDpt)
{
uint8_t lValueByte;
case VAL_DPT_1:
bool lValueBool;
lValueBool = getByteParam(iParamIndex) != 0;
knxWriteBool(IO_Output, lValueBool, iOn);
break;
case VAL_DPT_2:
case VAL_DPT_3:
lValueByte = getByteParam(iParamIndex);
knxWriteInt(IO_Output, lValueByte, iOn);
break;
case VAL_DPT_5:
case VAL_DPT_5001: // correct value is calculated by dpt handling
lValueByte = getByteParam(iParamIndex);
knxWriteInt(IO_Output, lValueByte, iOn);
break;
case VAL_DPT_17:
lValueByte = getByteParam(iParamIndex) - 1;
knxWriteInt(IO_Output, lValueByte, iOn);
break;
case VAL_DPT_6:
int8_t lValueShort;
lValueShort = getSByteParam(iParamIndex);
knxWriteInt(IO_Output, lValueShort, iOn);
break;
case VAL_DPT_7:
uint16_t lValueUWord;
lValueUWord = getWordParam(iParamIndex);
knxWriteInt(IO_Output, lValueUWord, iOn);
break;
case VAL_DPT_8:
int16_t lValueSWord;
lValueSWord = getSWordParam(iParamIndex);
knxWriteInt(IO_Output, lValueSWord, iOn);
break;
case VAL_DPT_9:
case VAL_DPT_14:
float lValueFloat;
lValueFloat = getFloatParam(iParamIndex);
knxWriteFloat(IO_Output, lValueFloat, iOn);
break;
case VAL_DPT_12:
uint32_t lValueInt;
lValueInt = getIntParam(iParamIndex);
knxWriteInt(IO_Output, lValueInt, iOn);
break;
case VAL_DPT_13:
int32_t lValueSInt;
lValueSInt = getSIntParam(iParamIndex);
knxWriteInt(IO_Output, lValueSInt, iOn);
break;
case VAL_DPT_16:
uint8_t *lValueStr;
lValueStr = getStringParam(iParamIndex);
knxWriteString(IO_Output, (char *)lValueStr);
break;
case VAL_DPT_232:
int32_t lValueRGB;
lValueRGB = getIntParam(iParamIndex) >> 8;
knxWriteInt(IO_Output, lValueRGB, iOn);
break;
default:
break;
}
}
void LogicChannel::writeParameterValue(uint8_t iIOIndex, bool iOn)
{
uint8_t lInputDpt;
LogicValue lValue = getInputValue(iIOIndex, &lInputDpt);
writeValue(lValue, iOn);
}
void LogicChannel::writeOtherKoValue(uint16_t iKoParamIndex, bool iIsRelative, uint16_t iDptIndex, bool iOn)
{
int16_t lKoNumber = getSWordParam(iKoParamIndex);
if (iIsRelative)
lKoNumber += calcKoNumber(IO_Output);
if (lKoNumber > 1 && lKoNumber <= MAIN_MaxKoNumber)
{
LogicValue lValue = getOtherKoValue(lKoNumber, iDptIndex);
writeValue(lValue, iOn);
}
}
void LogicChannel::writeFunctionValue(uint16_t iParamIndex, bool iOn)
{
uint8_t lFunction = getByteParam(iParamIndex);
uint8_t lDptE1;
uint8_t lDptE2;
LogicValue lE1 = getInputValue(BIT_EXT_INPUT_1, &lDptE1);
LogicValue lE2 = getInputValue(BIT_EXT_INPUT_2, &lDptE2);
uint8_t lDptOut = getByteParam(LOG_fODpt);
LogicValue lKoValue = getKoValue(IO_Output, lDptOut);
LogicValue lValue = LogicFunction::callFunction(_channelIndex, lFunction, lDptE1, lE1, lDptE2, lE2, &lDptOut, lKoValue);
if (isfinite((double)lValue))
writeValue(lValue, iOn);
}
void LogicChannel::writeValue(LogicValue iValue, bool iOn)
{
uint8_t lDpt = getByteParam(LOG_fODpt);
uint8_t lValueByte;
switch (lDpt)
{
case VAL_DPT_1:
knxWriteBool(IO_Output, (bool)iValue, iOn);
break;
case VAL_DPT_2:
lValueByte = iValue;
lValueByte &= 3;
knxWriteInt(IO_Output, lValueByte, iOn);
break;
case VAL_DPT_3:
lValueByte = iValue;
lValueByte &= 0x0F;
knxWriteInt(IO_Output, lValueByte, iOn);
break;
case VAL_DPT_5:
case VAL_DPT_5001:
knxWriteInt(IO_Output, (uint8_t)iValue, iOn);
break;
case VAL_DPT_6:
knxWriteInt(IO_Output, (int8_t)iValue, iOn);
break;
// lValueByte = lValue;
// // DPT5 means, that input value range is [0..100], output value range is
// // [0..255]
// lValueByte = (lValueByte / 100.0) * 255.0;
// knxWrite(0, lValueByte);
// break;
case VAL_DPT_7:
// iValue = (uint16_t)abs((int16_t)iValue);
knxWriteInt(IO_Output, (uint16_t)iValue, iOn);
break;
case VAL_DPT_8:
knxWriteInt(IO_Output, (int16_t)iValue, iOn);
break;
case VAL_DPT_9:
case VAL_DPT_14:
knxWriteFloat(IO_Output, (float)iValue, iOn);
break;
case VAL_DPT_16:
knxWriteString(IO_Output, ((const char *)iValue));
break;
case VAL_DPT_17:
lValueByte = abs((int8_t)iValue);
lValueByte &= 0x3F;
knxWriteInt(IO_Output, lValueByte, iOn);
break;
case VAL_DPT_12:
case VAL_DPT_13:
case VAL_DPT_232:
knxWriteInt(IO_Output, iValue, iOn);
break;
default:
break;
}
}
/********************************
* Logic functions
*******************************/
bool LogicChannel::isInputActive(uint8_t iIOIndex)
{
uint8_t lIsActive = ((iIOIndex == IO_Input1) ? ParamLOG_fE1 : ParamLOG_fE2) & BIT_INPUT_MASK;
if (lIsActive == 0)
{
// input might be also activated by a delta input converter, means from the other input
lIsActive = (iIOIndex == IO_Input2) ? ParamLOG_fE1Convert : ParamLOG_fE2Convert;
lIsActive = (lIsActive < VAL_InputConvert_Values) && (lIsActive & 1);
}
return (lIsActive > 0);
}
bool LogicChannel::isInputValid(uint8_t iIOIndex)
{
return (pValidActiveIO & iIOIndex);
}
// channel startup delay
void LogicChannel::startStartup()
{
pOnDelay = millis();
pCurrentPipeline |= PIP_STARTUP;
#if LOGIC_TRACE
if (debugFilter())
logChannel("startStartup: wait for %s", logTimeBase(LOG_fChannelDelayTime));
#endif
}
void LogicChannel::processStartup()
{
if (delayCheck(pOnDelay, ParamLOG_fChannelDelayTimeMS))
{
// we waited enough, remove pipeline marker
#if LOGIC_TRACE
if (debugFilter())
logChannel("endedStartup: waited %i ms", millis() - pOnDelay);
#endif
pCurrentPipeline &= ~PIP_STARTUP;
pCurrentPipeline |= PIP_RUNNING;
pOnDelay = 0;
}
}
void LogicChannel::processInput(uint8_t iIOIndex)
{
if (iIOIndex == IO_Absolute || iIOIndex == IO_Output)
return;
// we have now an event for an input, first we check, if this input is active
uint8_t lActive = ((iIOIndex == IO_Input1) ? ParamLOG_fE1 : ParamLOG_fE2) & BIT_INPUT_MASK;
if (lActive > 0)
{
// this input is we start convert for this input
startConvert(iIOIndex, iIOIndex);
// we also add that this input was used and is now valid
pValidActiveIO |= iIOIndex;
}
// this input might also be used for delta conversion in the other input
uint8_t lConverter = (iIOIndex == IO_Input2) ? ParamLOG_fE1Convert : ParamLOG_fE2Convert;
if ((lConverter <= VAL_InputConvert_Constant) && (lConverter & 1))
{
// reading "the other" Input is just necessary, if this input was not activated by the user
// otherwise this value is fetched by normal KO input processing
// for constants this is always necessary
if (lConverter == VAL_InputConvert_Constant || lActive == 0)
{
// delta and constant conversion, we start convert for the other input
startConvert(IO_Output - iIOIndex, iIOIndex);
// we also add that this input was used and is now valid
pValidActiveIO |= iIOIndex;
}
}
}
// we send an ReadRequest if reading from input 1 should be repeated
void LogicChannel::processRepeatInput1()
{
uint32_t lRepeatTime = ParamLOG_fE1RepeatTimeMS;
if (delayCheck(pInputProcessing.repeatInput1Delay, lRepeatTime))
{
knxRead(IO_Input1);
pInputProcessing.repeatInput1Delay = millis();
if (lRepeatTime == 0)
pCurrentPipeline &= ~PIP_REPEAT_INPUT1;
}
}
// we send an ReadRequest if reading from input 1 should be repeated
void LogicChannel::processRepeatInput2()
{
uint32_t lRepeatTime = ParamLOG_fE2RepeatTimeMS;
if (delayCheck(pInputProcessing.repeatInput2Delay, lRepeatTime))
{
knxRead(IO_Input2);
pInputProcessing.repeatInput2Delay = millis();
if (lRepeatTime == 0)
pCurrentPipeline &= ~PIP_REPEAT_INPUT2;
}
}
void LogicChannel::stopRepeatInput(uint8_t iIOIndex)
{
// repeated read on an input KO is stopped in following cases:
// 1. There is one single read on startup and this read was executed (is solved in processRepeatInputX())
// 2. There is one single read on startup, the read was not yet executed (channel is not running) but
// nevertheless the telegram was received (i.E. through an other read of a running channel)
// 3. There is a continuous read with condition "until telegram received"
uint16_t lRepeatInputBit;
uint32_t lRepeatTime;
bool lJustOneTelegram;
switch (iIOIndex)
{
case IO_Input1:
lRepeatInputBit = PIP_REPEAT_INPUT1;
lRepeatTime = ParamLOG_fE1RepeatTimeMS;
lJustOneTelegram = ParamLOG_fE1DefaultRepeat;
break;
case IO_Input2:
lRepeatInputBit = PIP_REPEAT_INPUT2;