-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotoChannel.cpp
1312 lines (1108 loc) · 41.8 KB
/
RotoChannel.cpp
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
/*
* RotoChannel.cpp
*
* Created on: 27.03.2017
* Author: achristian
*/
#include "RotoChannel.h"
#include "Constants.h"
#include "DebugUtil.h"
#include "kdevice_DFF_4.1.h"
#include "KonnektingDevice.h"
RotoChannel::RotoChannel(int group, int setPinOpen, int resetPinOpen, int setPinClose, int resetPinClose) {
_group = group;
_setPinOpen = setPinOpen;
_resetPinOpen = resetPinOpen;
_openStep = 0;
_closeStep = 0;
_setPinClose = setPinClose;
_resetPinClose = resetPinClose;
_manualMoveRequest = false;
_moveStatus = MS_STOP;
_status = CS_UNDEFINED;
_initDone = false;
_startupAction = A_NONE;
_lastBlinkMillis = millis();
_lastBlinkState = false;
_lastMoveStatus = MS_STOP;
_position = 0.0f;
_startMoveMillis = NOT_DEFINED;
_isStopping = false;
_targetPosition = NOT_DEFINED;
_referenceRun = REF_NONE;
_config = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
_lock = LCK_UNLOCKED;
}
RotoChannel::~RotoChannel() {
// TODO Auto-generated destructor stub
}
ChannelConfig RotoChannel::getConfig() {
return _config;
}
void RotoChannel::init(Adafruit_MCP23017& mcp, Frontend8Btn8Led& frontend) {
if (!_enabled) {
Debug.println(F("no config, stopping init"));
return;
}
switch (_config.setting) {
case OPTION_SETTINGS_WINDOW:
_startupAction = A_CLOSE;
break;
case OPTION_SETTINGS_SHUTTER:
_startupAction = A_OPEN;
break;
}
_mcp = mcp;
_frontend = frontend;
// configure MCP on relay board
_mcp.pinMode(_setPinOpen, OUTPUT);
_mcp.pinMode(_resetPinOpen, OUTPUT);
_mcp.pinMode(_setPinClose, OUTPUT);
_mcp.pinMode(_resetPinClose, OUTPUT);
Debug.print(F("[%i] Init"), _group);
switch (_startupAction) {
case A_OPEN:
_initDone = false;
Debug.println(F(" to OPEN"));
doOpen();
break;
case A_CLOSE:
_initDone = false;
Debug.println(F(" to CLOSE"));
doClose();
break;
case A_NONE:
default:
_initDone = true;
Debug.println(F(" to NONE"));
_status = CS_UNDEFINED;
_moveStatus = MS_STOP;
break;
}
_baseIndex = COMOBJ_OFFSET + (_group * COMOBJ_PER_CHANNEL);
}
void RotoChannel::setConfig(ChannelConfig config) {
_config = config;
_enabled = true;
Debug.println(F("[%i] setConfig:\n"
"\t_config.setting=%i \n"
"\t_config.runtimeRollover=%is \n"
"\t_config.runTimeOpen=%ims \n"
"\t_config.runTimeClose=%ims"), _group, _config.setting, _config.runTimeRollover, getTime(_config.runTimeOpen), getTime(_config.runTimeClose));
_openStep = (100.0 / (getTime(_config.runTimeOpen))) / 100.0;
_closeStep = (100.0 / (getTime(_config.runTimeClose))) / 100.0;
Debug.println(F("\t_openStep=%3.9f%%/ms"), _group, _openStep);
Debug.println(F("\t_closeStep=%3.9f%%/ms"), _group, _closeStep);
}
void RotoChannel::work() {
if (!_enabled) {
// nothing do to, channel is disabled
return;
}
// Do init-stuff, if not already done
if (!_initDone) {
// check how long we have to wait for final end position
unsigned long waitTime = 0;
switch (_startupAction) {
case A_OPEN:
waitTime = getTime(_config.runTimeOpen);
break;
case A_CLOSE:
waitTime = getTime(_config.runTimeClose);
break;
case A_NONE:
waitTime = 0;
break;
}
// if time is over
if (_startMoveMillis != NOT_DEFINED && millis() - _startMoveMillis > waitTime) {
// set final status
switch (_startupAction) {
case A_OPEN:
_position = 1.0; // open will reduce to 0
_status = CS_OPENED;
break;
case A_CLOSE:
_position = 0.0; // close will increase to 1
_status = CS_CLOSED;
break;
case A_NONE:
_status = CS_UNDEFINED;
break;
}
// ... and finalize init
_initDone = true;
int led = _group * 2;
Debug.println(F("[%i] Init *done*. Pos=%3.9f"), _group, _position);
}
} else if (_manualMoveRequest) {
Debug.print(F("[%i] manualMoveRequest: "), _group);
Debug.print(F("current move status: %i"), _moveStatus);
Debug.println(F("; current status: %i"), _status);
if (_moveStatus != MS_STOP) {
doStop(); // wenn wir noch fahren, anhalten
} else if (_manualMoveRequestOpenButton) { // ansonsten je nach button öffnen oder schließen
doOpen();
} else {
doClose();
}
_manualMoveRequest = false;
} else {
switch (_referenceRun) {
case REF_START:
Debug.println(F("[%i] Reference run START"), _group);
_previousPosition = _position;
doClose();
// next step:
_referenceRun = REF_CLOSING;
break;
case REF_CLOSING:
if (_position == 1.0f) {
Debug.println(F("[%i] Reference CLOSED pos reached, restoring ..."), _group);
doPosition(_previousPosition);
// next step:
_referenceRun = REF_RESTORING;
}
break;
case REF_RESTORING:
if (isJustStopped()) {
Debug.println(F("[%i] Reference PREV pos reached, DONE ..."), _group);
// next step:
_referenceRun = REF_DONE;
}
break;
case REF_DONE:
Debug.println(F("[%i] Reference DONE"), _group);
_lock = LCK_UNLOCKED;
_referenceRun = REF_NONE;
break;
default:
// irrelevant
break;
}
switch (_ventilate) {
case VENT_START:
Debug.println(F("[%i] Ventilate START"), _group);
_previousPosition = _position;
doOpen();
// next step:
_ventilate = VENT_VENTILATE;
_ventilateStart = millis();
break;
case VENT_VENTILATE:
if (millis() - _ventilateStart > _config.ventilationTime) {
_ventilate = VENT_RESTORING;
} else {
unsigned long remain = _config.ventilationTime - (millis() - _ventilateStart);
if (remain % 5000 == 0) {
Debug.println(F("[%i] Ventilate remaining time: %lu"), _group, remain);
}
}
break;
case VENT_RESTORING:
Debug.println(F("[%i] Ventilate RESTORE"), _group);
doPosition(_previousPosition);
_ventilate = VENT_DONE;
break;
case VENT_DONE:
Debug.println(F("[%i] Ventilate DONE"), _group);
_ventilate = VENT_NONE;
break;
default:
break;
}
}
if (_initDone) {
workPosition();
workStatus();
}
if (isJustStopped()) {
// if required, set end of locking/unlocking as channel movement has stopped
bool updated = false;
switch (_lock) {
case LCK_LOCKING:
_lock = LCK_LOCKED;
updated = true;
break;
case LCK_UNLOCKING:
_lock = LCK_UNLOCKED;
updated = true;
break;
}
if (updated) {
Debug.println(F("[%i] Updated lock status [locked=%i]"), _group, isLocked());
}
}
// update LEDs in any case, needs to be done at last
workLEDs();
_lastMoveStatus = _moveStatus;
}
/**
* Send status updates, if required
*/
void RotoChannel::workStatus() {
// if locked, just don't send any updates, we're locked!
if (_lock == LCK_LOCKED) {
return;
}
if (isJustStopped()) {
Debug.print("[%i] STATUS just stopped: [", _group);
/*
* 0-en werden gesendet wenn abgehalten wird, passiert in workStatus()
*/
switch (_config.runStatusComObj) {
case 0x01:
// 1KO: Verfahrstatus
Knx.write(getComObjIndex(COMOBJ_abStatusMovement), 0);
Debug.print(" COMOBJ_abStatusMovement=0", _group);
break;
case 0x02:
// 2KO: Auffahrt + Zufahrt
Knx.write(getComObjIndex(COMOBJ_abStatusMovementOpen), 0);
Knx.write(getComObjIndex(COMOBJ_abStatusMovementClose), 0);
Debug.print(" COMOBJ_abStatusMovement[Open|Close]=0", _group);
break;
case 0x00:
default:
// do nothing
break;
}
/*
* Status: Status für akt. Richtung & Position auf/zu
* --> 3 KOs:
* Status akt. Richtung -> abStatusMovementDirection
* Position auf -> abStatusOpenPos
* Position zu -> abStatusClosePos
*/
if (_config.runStatusPositionComObj != 0x00) { // if not OFF
if (_position == 0.0f) { // = window closed | shutter opened
if (isWindow()) {
Knx.write(getComObjIndex(COMOBJ_abStatusClosePos), DPT1_001_on);
Debug.print(" COMOBJ_abStatusMovementClosePos=1", _group);
} else { //isShutter
Knx.write(getComObjIndex(COMOBJ_abStatusOpenPos), DPT1_001_on);
Debug.print(" COMOBJ_abStatusMovementOpenPos=1", _group);
}
} else if (_position == 1.0f) { // = window opened| shutter closed
if (isWindow()) {
Knx.write(getComObjIndex(COMOBJ_abStatusOpenPos), DPT1_001_on);
Debug.print(" COMOBJ_abStatusMovementOpenPos=1", _group);
} else { //isShutter
Knx.write(getComObjIndex(COMOBJ_abStatusClosePos), DPT1_001_on);
Debug.print(" COMOBJ_abStatusMovementClosePos=1", _group);
}
}
}
Debug.println("]", _group);
}
// if we are in an solid stop, don't send status updates
if (_lastMoveStatus == MS_STOP && _moveStatus == MS_STOP) {
return;
}
// current position is located in different variables, depending on move-status
Debug.println("[%i] isMoving?=%i _newPos=%f _pos=%f", _group, isMoving(), _newPosition, _position);
float currPos = isMoving() ? _newPosition : _position;
uint8_t positionValueToSend = currPos * 255; // map 0..100% to 0..255 unsigned byte
// Debug.println("[%i] STATUS curr pos: %f -> 0x%02x; lastSentPos=0x%02x; millis=%6ld newMillis=%6ld; lastMoveStatus=%i movestatus=%i",
// _group, currPos, positionValueToSend, _lastSentPosition, millis(), (_lastStatusUpdate + STATUS_UPDATE_INTERVAL), _lastMoveStatus, _moveStatus);
if (_initDone && // only send when init is done AND
_config.absPosStatusComObj == 0x01 && // currentpos status comobj is active AND
_lastSentPosition != positionValueToSend && // we need to have different value as last time AND
(
(millis() > (_lastStatusUpdate + STATUS_UPDATE_INTERVAL)) || // if it's time for an update OR
(_lastMoveStatus != MS_STOP && _moveStatus == MS_STOP) // if we just reached STOP status
)) {
if (_config.absPosStatusComObj == 0x01) {
Knx.write((byte) (_baseIndex + (COMOBJ_abStatusCurrentPos - COMOBJ_OFFSET)), positionValueToSend);
Debug.println("[%i] STATUS SEND curr pos: %f -> 0x%02x", _group, currPos, positionValueToSend);
// store last sent status. Ensures with if-clause that we don't send the same value twice (especially on stop)
_lastSentPosition = positionValueToSend;
_lastStatusUpdate = millis();
}
}
}
void RotoChannel::doButton(bool openButton) {
Debug.println(F("[%i] doButton: openButton=%i"), _group, openButton);
if (isLocked()) {
Debug.println(F("[%i] doButton skipped due to lock: %i"), _group, _lock);
}
_manualMoveRequest = true; // signal move request
_manualMoveRequestOpenButton = openButton;
}
/**
*
* @param targetPosition
*/
void RotoChannel::doPosition(float targetPosition) {
Debug.println(F("[%i] doPosition(%f): currPos=%f setting=%i"), _group, targetPosition, _position, _config.setting);
if (!isLocked()) {
// if (isMoving()) {
// Debug.println(F("[%i] doPosition(%f): already moving. stop first."), _group, targetPosition);
// doStop();
// Debug.println(F("[%i] doPosition(%f): stopped, continue with doPosition()"), _group, targetPosition);
// }
_targetPosition = targetPosition;
// window: 0% = closed, 100% = opened
// shutter: 0% = opened, 100% = closed
if (_targetPosition > _position) {
if (isWindow()) {
doOpen();
} else { // shutter
doClose();
}
} else {
if (isWindow()) {
doClose();
} else { // shutter
doOpen();
}
}
} else {
Debug.println(F("[%i] doPosition skipped due to lock."), _group);
}
}
void RotoChannel::doOpen() {
/*
* TODO The delay in here will definitley block the loop(),
* bus this happens not that often, so we won't miss a telegram
*/
Debug.println(F("[%i] doOpen() locked=%i"), _group, _lock);
if (_initDone && _referenceRun!=REF_NONE && isFullyOpened()) {
Debug.println(F("[%i] doOpen() already fully opened. just return."), _group);
return;
}
if (isMoving() && !_isStopping) {
Debug.println(F("[%i] Stop moving first ..."), _group);
doStop();
Debug.println(F("[%i] Stop moving first ...*done*"), _group);
}
// it's okay to trigger relais in unlocked, locking and unlocking state
if (!isLocked()) {
Debug.println(F("[%i] trigger relay to open"), _group);
_mcp.digitalWrite(_setPinOpen, HIGH);
delay(RELAY_SET_TIME);
_mcp.digitalWrite(_setPinOpen, LOW);
delay((unsigned long) _config.triggerTime);
_mcp.digitalWrite(_resetPinOpen, HIGH);
delay(RELAY_SET_TIME);
_mcp.digitalWrite(_resetPinOpen, LOW);
delay((unsigned long) _config.triggerTime); // ensure delay till next relais command
// we're no longer "closed", so turn off that status LED
_frontend.setLED(getLed(LED_CLOSE), false);
} else {
Debug.println(F("[%i] doOpen() locked=%s"), _group, isLocked() ? "true": "false");
}
_moveStatus = MS_OPENING;
sendMovementStatus();
if (_startMoveMillis == NOT_DEFINED) {
_startMoveMillis = millis();
}
}
void RotoChannel::doClose() {
Debug.println(F("[%i] doClose() locked=%i pos=%3.9f"), _group, _lock, _position);
if (_initDone && _referenceRun!=REF_NONE && isFullyClosed()) {
Debug.println(F("[%i] doClose() already fully closed. just return."), _group);
return;
}
// it's okay to trigger relais in unlocked, locking and unlocking state
if (isMoving() && !_isStopping) {
Debug.println(F("[%i] Stop moving first ..."), _group);
doStop();
Debug.println(F("[%i] Stop moving first ...*done*"), _group);
}
if (_lock != LCK_LOCKED) {
Debug.println(F("[%i] trigger relay to close"), _group);
_mcp.digitalWrite(_setPinClose, HIGH);
delay(RELAY_SET_TIME);
_mcp.digitalWrite(_setPinClose, LOW);
delay((unsigned long) _config.triggerTime);
_mcp.digitalWrite(_resetPinClose, HIGH);
delay(RELAY_SET_TIME);
_mcp.digitalWrite(_resetPinClose, LOW);
delay((unsigned long) _config.triggerTime); // ensure delay till next relais command
// we're no longer "opened", so turn off that status LED
_frontend.setLED(getLed(LED_OPEN), false);
} else {
Debug.println(F("[%i] doClose() locked=%s"), _group, isLocked() ? "true": "false");
}
_moveStatus = MS_CLOSING;
sendMovementStatus();
if (_startMoveMillis == NOT_DEFINED) {
_startMoveMillis = millis();
}
}
void RotoChannel::doStop() {
if (isLocked()) {
Debug.println(F("[%i] doStop skipped due to lock."), _group);
return;
}
_isStopping = true;
Debug.print(F("[%i] doStop() moveStatus=%i ->"), _group, _moveStatus);
switch (_moveStatus) {
case MS_CLOSING:
Debug.println(F("CLOSING to STOP"));
doClose(); // call close again to stop motor
break;
case MS_OPENING:
Debug.println(F("OPENING to STOP"));
doOpen(); // call close again to stop motor
break;
default:
Debug.println(F("STOP"));
break;
}
_moveStatus = MS_STOP;
//_startMoveMillis = NOT_DEFINED;
_isStopping = false;
}
/**
*
*/
void RotoChannel::workLEDs() {
// while not yet done with init, led all LEDs blink to signal init
if (!_initDone) {
if (millis() - _lastBlinkMillis > BLINK_INIT_DELAY) {
_frontend.setLED(getLed(LED_CLOSE), !_lastBlinkState);
_frontend.setLED(getLed(LED_OPEN), _lastBlinkState);
_lastBlinkMillis = millis();
_lastBlinkState = !_lastBlinkState;
}
return;
}
// Status Blinken
if (_moveStatus != MS_STOP) {
if (millis() - _lastBlinkMillis > BLINK_DELAY) {
// Debug.println(F("BLINK "));
_lastBlinkState = !_lastBlinkState;
// set blink status on correct LED
switch (_moveStatus) {
case MS_CLOSING:
_frontend.setLED(getLed(LED_CLOSE), _lastBlinkState);
break;
case MS_OPENING:
_frontend.setLED(getLed(LED_OPEN), _lastBlinkState);
break;
default:
break;
}
_lastBlinkMillis = millis();
}
}
// ensure LEDs are off after moving
if (_lastMoveStatus != MS_STOP && _moveStatus == MS_STOP) {
Debug.print(F("[%i] workLEDs: STOP reached. Set LEDs to "), _group);
switch (_status) {
case CS_OPENED:
_frontend.setLED(getLed(LED_OPEN), true); // auf
_frontend.setLED(getLed(LED_CLOSE), false); // zu
Debug.println(F("OPENED"));
break;
case CS_CLOSED:
_frontend.setLED(getLed(LED_OPEN), false); // auf
_frontend.setLED(getLed(LED_CLOSE), true); // zu
Debug.println(F("CLOSED"));
break;
case CS_OPEN:
case CS_UNDEFINED:
default:
_frontend.setLED(getLed(LED_OPEN), false); // weder
_frontend.setLED(getLed(LED_CLOSE), false); // noch
Debug.println(F("OPEN|UNDEFINED"));
}
_lastBlinkState = false;
}
}
void RotoChannel::workPosition() {
#define DEBUG_UPDATE_STATUS
/*
* Es gibt die folgenden Fälle:
*
* - Öffne
* - Schließe
*
* - öffnen gestoppt
* - schließen gestoppt
*
* - öffnen timeout
* - öffnen, target position reached/exceeded
* - schließen timeout
* - schließen, target position reached/exceeded
*
* - weder noch
*/
bool needStop = false;
/* ********************************************
* Fall: Öffne ODER öffnen gestoppt
* ********************************************/
if ((_lastMoveStatus == MS_OPENING && _moveStatus == MS_OPENING) || (_lastMoveStatus == MS_OPENING && _moveStatus == MS_STOP)) {
unsigned long duration = millis() - _startMoveMillis;
float delta = (float) duration * _openStep;
if (isWindow()) {
_newPosition = _position + delta;
} else {
_newPosition = _position - delta;
}
#ifdef DEBUG_UPDATE_STATUS
Debug.print(F("[%i] O: duration: %i"), _group, duration);
Debug.print(F("\tposition: %3.9f"), _position);
Debug.print(F("\tdelta: %3.9f"), delta);
Debug.println(F("\tnew position: %3.9f"), _newPosition);
#endif
// keep pos in range
_newPosition = limitPos(_newPosition);
unsigned long allowedTime = 0.0;
if (isWindow()) {
allowedTime = (1.0 - _position) * getTime(_config.runTimeOpen);
} else {
allowedTime = (_position) * (getTime(_config.runTimeClose));
}
#ifdef DEBUG_UPDATE_STATUS
Debug.println(F("[%i] O: allowed time open: %i"), _group, allowedTime);
#endif
// if open move time exceeds "open time", force position to "completely open"
if (duration > allowedTime) {
if (isWindow()) {
_newPosition = 1.0;
} else {
_newPosition = 0.0;
}
#ifdef DEBUG_UPDATE_STATUS
Debug.println(F("[%i] O: Time limit for OPEN reached"), _group);
#endif
}
// opened condition
bool openedCondition = false;
if (isWindow()) {
openedCondition = _newPosition == 1.0 || (_targetPosition != NOT_DEFINED && _newPosition >= _targetPosition);
} else {
openedCondition = _newPosition == 0.0 || (_targetPosition != NOT_DEFINED && _newPosition <= _targetPosition);
}
if (openedCondition) {
if (_newPosition == 1.0 || _newPosition == 0.0) { // reached endpoint?
_status = CS_OPENED; // opened
} else {
_status = CS_OPEN; // somewhere between opened and closed
}
//_moveStatus = MS_STOP;
needStop = true;
_targetPosition = NOT_DEFINED;
//#ifdef DEBUG_UPDATE_STATUS
Debug.println(F("[%i] Is now OPENED or at abs. target pos"), _group);
//#endif
} else {
_status = CS_OPEN;
}
} else
/* ********************************************
* Fall: Schließe ODER schließen gestoppt
* ********************************************/
if ((_lastMoveStatus == MS_CLOSING && _moveStatus == MS_CLOSING) || (_lastMoveStatus == MS_CLOSING && _moveStatus == MS_STOP)) {
unsigned long duration = millis() - _startMoveMillis;
float delta = (float) duration * _closeStep;
if (isWindow()) {
_newPosition = _position - delta;
} else {
_newPosition = _position + delta;
}
#ifdef DEBUG_UPDATE_STATUS
Debug.print(F("[%i] C: duration: %i"), _group, duration);
Debug.print(F("\tposition: %3.9f"), _position);
Debug.print(F("\tdelta: %3.9f"), delta);
Debug.println(F("\tnew position: %3.9f"), _newPosition);
#endif
// keep pos in range
_newPosition = limitPos(_newPosition);
unsigned long allowedTime = 0.0;
if (isWindow()) {
allowedTime = (_position) * (getTime(_config.runTimeClose));
} else {
allowedTime = (1.0 - _position) * getTime(_config.runTimeOpen);
}
#ifdef DEBUG_UPDATE_STATUS
Debug.println(F("[%i] C: allowed time close: %i"), _group, allowedTime);
#endif
// if open move time exceeds "open time", force position to "completely close"
if (duration > allowedTime) {
if (isWindow()) {
_newPosition = 0.0;
} else {
_newPosition = 1.0;
}
#ifdef DEBUG_UPDATE_STATUS
Debug.println(F("[%i] C: Time limit for CLOSE reached"), _group);
#endif
}
// closed condition
bool closedCondition = false;
if (isWindow()) {
closedCondition = _newPosition == 0.0 || (_targetPosition != NOT_DEFINED && _newPosition <= _targetPosition);
} else {
closedCondition = _newPosition == 1.0 || (_targetPosition != NOT_DEFINED && _newPosition >= _targetPosition);
}
if (closedCondition) {
// reached endpoint?
if (_newPosition == 1.0 || _newPosition == 0.0) {
_status = CS_CLOSED; // closed
} else {
_status = CS_OPEN; // somewhere between opened and closed
}
//_moveStatus = MS_STOP;
needStop = true;
_targetPosition = NOT_DEFINED;
//#ifdef DEBUG_UPDATE_STATUS
Debug.println(F("[%i] Is now CLOSED or at abs. target pos"), _group);
//#endif
} else {
_status = CS_OPEN;
}
}
// if we need to stop ...
if (_lastMoveStatus != MS_STOP && needStop) {
doStop(); // actually do the stop
// apply position
_position = _newPosition;
_startMoveMillis = NOT_DEFINED;
Debug.println(F("[%i] Finally stopping on position %3.9f [locked=%s]"), _group, _position, isLocked()?"true":"false");
} else
// or if we just reached stop
if (_lastMoveStatus!= MS_STOP && _moveStatus == MS_STOP) {
_position = _newPosition;
_startMoveMillis = NOT_DEFINED;
Debug.println(F("[%i] Finally stopped on position %3.9f [locked=%s]"), _group, _position, isLocked()?"true":"false");
}
}
/**
* Returns true, if shutter is locked or doing lock-action,
* false if unlocked, or doing unlock-action
* @return bool
*/
bool RotoChannel::isLocked() {
switch (_lock) {
case LCK_LOCKED:
case LCK_LOCKING:
return true;
case LCK_UNLOCKED:
case LCK_UNLOCKING:
default:
return false;
}
}
/**
* returns true, if channel is in move state
* @return bool
*/
bool RotoChannel::isMoving() {
switch (_moveStatus) {
case MS_CLOSING:
case MS_OPENING:
return true;
case MS_STOP:
default:
return false;
}
}
float RotoChannel::limitPos(float pos) {
if (pos > 1.0f) {
pos = 1.0f;
} else if (pos < 0.0f) {
pos = 0.0f;
}
return pos;
}
/**
* Check for window setup
* @return true, if channel/group is window, false if it's shutter
*/
bool RotoChannel::isWindow() {
return _config.setting == OPTION_SETTINGS_WINDOW;
}
/**
* Check if window or shutter is fully opened
* @return
*/
bool RotoChannel::isFullyOpened() {
if (isWindow() && _position==1.0f) { // window
Debug.println(F("[%i] isFullyOpened() isWindows pos=%3.9f"), _group, _position);
return true;
} else if (!isWindow() && _position==0.0f){ // shutter
Debug.println(F("[%i] isFullyOpened() isShutter pos=%3.9f"), _group, _position);
return true;
} else {
Debug.println(F("[%i] isFullyOpened() else pos=%3.9f"), _group, _position);
return false;
}
}
/**
* Check if window or shutter is fully closed
* @return
*/
bool RotoChannel::isFullyClosed() {
if (isWindow() && _position==0.0f) { // window
return true;
} else if (!isWindow() && _position==1.0f){ // shutter
return true;
} else {
return false;
}
}
/**
* Returns frontend LED index
* @param led enum for open and close LED
* @return frontend LED index
*/
int RotoChannel::getLed(FrontendLed led) {
int ledIndex = _group * 2;
switch (led) {
case LED_OPEN:
ledIndex += 0;
break;
case LED_CLOSE:
ledIndex += 1;
break;
default:
break;
}
return ledIndex;
}
/**
* calculates the correct comobj index for the given "COMOBJ_...." variable
*/
byte RotoChannel::getComObjIndex(byte COMOBJ_var) {
return (byte) (_baseIndex + (COMOBJ_var - COMOBJ_OFFSET));
}
/**
* used by doOpen(), doClose() and doStop() to send one-time-status of movement
* will send only when actually start moving. end-events are handled by workStatus()
*/
void RotoChannel::sendMovementStatus() {
if (_isStopping) {
Debug.println(F("[%i] sendMovementStatus() skipping, due to stopping"), _group);
return;
}
if (_config.runStatusPositionComObj != 0x00) {
Debug.println(F("[%i] sendMovementStatus() COMOBJ_abStatusMovementDirection"), _group);
switch (_moveStatus) {
case MS_OPENING:
Knx.write(getComObjIndex(COMOBJ_abStatusMovementDirection), isWindow() ? DPT1_008_down : DPT1_008_up);
break;
case MS_CLOSING:
Knx.write(getComObjIndex(COMOBJ_abStatusMovementDirection), isWindow() ? DPT1_008_up : DPT1_008_down);
break;
default:
break;
}
}
if (isMoving()) {
/*
* 0-en werden gesendet wenn abgehalten wird, passiert in workStatus()
*/
switch (_config.runStatusComObj) {
case 0x01:
// 1KO: Verfahrstatus
Knx.write(getComObjIndex(COMOBJ_abStatusMovement), 1);
Debug.println(F("[%i] sendMovementStatus() COMOBJ_abStatusMovement=1"), _group);
break;
case 0x02:
// 2KO: Auffahrt + Zufahrt
switch (_moveStatus) {
case MS_OPENING:
Knx.write(getComObjIndex(COMOBJ_abStatusMovementOpen), 1);
Debug.println(F("[%i] sendMovementStatus() COMOBJ_abStatusMovementOpen=1"), _group);
break;
case MS_CLOSING:
Knx.write(getComObjIndex(COMOBJ_abStatusMovementClose), 1);
Debug.println(F("[%i] sendMovementStatus() COMOBJ_abStatusMovementClose=1"), _group);
break;
case MS_STOP:
default:
// do nothing
break;
}
break;
case 0x00:
default:
// do nothing
break;
}
}
}