-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotoChannel.h
569 lines (478 loc) · 11.4 KB
/
RotoChannel.h
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
/*
* RotoChannel.h
*
* Created on: 27.03.2017
* Author: achristian
*/
#ifndef ROTOCHANNEL_H_
#define ROTOCHANNEL_H_
#include "Arduino.h"
#include "Frontend8Btn8Led.h"
#include <Adafruit_MCP23017.h>
/**
* Time in ms the relay needs to be triggered for set
*/
#define RELAY_SET_TIME 50L
/**
* Time in ms roto requires to trigger open/close action
*/
#define ROTO_TRIGGER_DURATION 175L
/**
* delay for blinking
*/
#define BLINK_DELAY 500L
/**
* delay for blinking while init
*/
#define BLINK_INIT_DELAY 250L
/**
* One second in milliseconds -> 1000ms
*/
#define SECOND 1000L
/**
* 100% / 255(0xff) = 0,39% per 1(0x01)
*/
#define BYTE_PERCENT 0.392156863
/**
* Means: Value is not defined
*/
#define NOT_DEFINED -1
/**
* The offset for comobj calculation.
*
* f.i. group 0 has it's ComObj starting at index=8, then ComObjOffset is set to 8.
* Depending on the group and offset, the Channel can calculate it's ComObj indizes itself.
* So main-sketch just needs to forward all knxEvents to all channels and each channel filters out it's ComObj events.
*/
#define COMOBJ_OFFSET 9
#define COMOBJ_PER_CHANNEL 16
/**
* time interval in which status updates are sent
*/
#define STATUS_UPDATE_INTERVAL 1000
/**
* Move status, like opening, closing, ..
*/
enum RotoChannelMoveStatus {
/**
* channel currently opens
*/
MS_OPENING,
/**
* channel currently closes
*/
MS_CLOSING,
/**
* channel is not moving/is stopped
*/
MS_STOP
};
/**
* Status like, opened, closed, ...
*/
enum RotoChannelStatus {
/**
* completely opened
*/
CS_OPENED = 0,
/**
* completely closed
*/
CS_CLOSED,
/**
* somewhere between completely opened and completely closed
*/
CS_OPEN,
/**
* position unknown
*/
CS_UNDEFINED
};
/**
* Default action on power up
*/
enum RotoAction {
/**
* Do open
*/
A_OPEN,
/**
* Do close
*/
A_CLOSE,
/**
* Do nothing
*/
A_NONE
};
enum RotoLock {
/**
* 0: shutter is locked (lock action = done)
*/
LCK_LOCKED,
/**
* 1: shutter is doing lock-action
*/
LCK_LOCKING,
/**
* 2: shutter is unlocked (unlock action = done)
*/
LCK_UNLOCKED,
/**
* 3: shutter is doing unlock-action
*/
LCK_UNLOCKING,
/**
* 4: Reference run is active
*/
LCK_REFERENCE_RUN
};
enum RotoReferenceRun {
REF_NONE,
/**
*
*/
REF_START,
/**
*
*/
REF_CLOSING,
/**
*
*/
REF_RESTORING,
/**
*
*/
REF_DONE
};
enum RotoVentilate {
VENT_NONE,
/**
*
*/
VENT_START,
/**
*
*/
VENT_VENTILATE,
/**
*
*/
VENT_RESTORING,
/**
*
*/
VENT_DONE
};
enum FrontendLed {
/**
* LED for open-status
*/
LED_OPEN,
/**
* LED for close-status
*/
LED_CLOSE
};
typedef struct {
/**
*
*/
unsigned long ventilationTime;
/**
*/
uint8_t triggerTime;
/**
* Settings: Channel setting:
* 0 = unused
* 1 = roof-light
* 2 = shutter
* 3 = clone of ...
*/
uint8_t setting;
/**
* Settings: Time in seconds the channel requires for full close
*/
uint8_t runTimeClose;
/**
* Settings: Time in seconds the channel requires for full open
*/
uint8_t runTimeOpen;
/**
* Settings: "Fahrzeitverlängerung" in %
* 2=2%, 5=5%, 10=10%, 15=15%, 20=20%
*/
uint8_t runTimeRollover;
/**
* Settings: Verhalten bei Sperren: 0=keine Änderung, 1=auf fahren, 2=zu fahren
*/
uint8_t lockAction;
/**
* Settings: Verhalten bei Rückname der Sperre / Alarme, 00=keine Aktion|01=auf fahren|02=zu fahren|03=vorherige Position anfahren
*/
uint8_t unlockAction;
/**
* Settings: Regenalarm, 00=nicht aktiv|01=aktiv
*/
uint8_t rainAlarm;
/**
* Settings: Regenalarm: Überwachungszeit [0..120min, 0=aus], Default="1E" Min="00" Max="78"
*/
uint8_t rainAlarmObservationTime;
/**
* Settings: Regenalarm: Aktion, 00=keine Aktion|01=auf fahren|02=zu fahren
*/
uint8_t rainAlarmAction;
/**
* Settings: Windalarm, 00=nicht aktiv|01=aktiv
*/
uint8_t windAlarm;
/**
* Settings: Windalarm: Überwachungszeit [0..120min, 0=aus], Default="1E" Min="00" Max="78"
*/
uint8_t windAlarmObservationTime;
/**
* Settings: Windalarm: Aktion, 00=keine Aktion|01=auf fahren|02=zu fahren
*/
uint8_t windAlarmAction;
/**
* Settings: Lüften per zentralem KO 0=false, 1=true
*/
uint8_t ventByComObj;
/**
* Ansteuerung: KO für absolute Position 0=nicht aktiv|1=aktiv
*/
uint8_t absPositionComObj;
/**
* Ansteuerung: KO für "Position anfahren" 0=nicht aktiv|1=aktiv
*/
uint8_t driveToPositionComObj;
/**
* Settings: Wert für "Position anfahren" 00=0%|05=5%|0A=10%|0F=15%|14=20%|19=25%|1E=30%|23=35%|28=40%|2D=45%|32=50%|37=55%|3C=60%|41=65%|46=70%|4B=75%|50=80%|55=85%|5A=90%|5F=95%|64=100%
*/
uint8_t driveToPositionValue;
/**
* Ansteuerung: KO für Referenzfahrt 0=nicht aktiv|1=aktiv
*/
uint8_t referenceRunComObj;
/**
* Status: KO für Verfahrstatus 0=nicht aktiv|1=fährt (1 KO)|2=Auffahrt+Zufahrt (2 KOs)
*/
uint8_t runStatusComObj;
/**
* Status: Status für aktuelle Position 0=nicht aktiv|1=aktiv
*/
uint8_t absPosStatusComObj;
/**
* Status: Status für akt. Richtung & Position auf/zu 0=nicht aktiv|1=aktiv
*/
uint8_t runStatusPositionComObj;
} ChannelConfig;
class RotoChannel {
public:
RotoChannel(int group, int setPinOpen, int resetPinOpen, int setPinClose, int resetPinClose);
virtual ~RotoChannel();
void work();
/**
* initialize and set the relay channel
*/
void init(Adafruit_MCP23017& mcp, Frontend8Btn8Led& frontend);
void setConfig(ChannelConfig config);
ChannelConfig getConfig();
void doButton(bool openButton);
/**
* used by sketch to feed in com-obj events. If event is not used for this channel, method does nothing
* @param index absolute comobj index
* @return if event has been consumed or not
*/
bool knxEvents(byte index);
private:
Adafruit_MCP23017 _mcp;
Frontend8Btn8Led _frontend;
ChannelConfig _config;
/**
* enabled/disabled state of channel
*/
bool _enabled;
/**
* Lock enum
*/
RotoLock _lock;
/**
* Reference run enum
*/
RotoReferenceRun _referenceRun;
/**
* Ventilate enum
*/
RotoVentilate _ventilate;
/**
* millis() at which ventilate started
*/
unsigned long _ventilateStart;
/**
* The comobj base index for this channel/group, means: at this absolute comobj index do the comobj start for this channel
*/
byte _baseIndex;
int _group; // 0..3
/**
* Pin which is connected to SET of the OPEN relay
*/
int _setPinOpen;
/**
* Pin which is connected to RESET of the OPEN relay
*/
int _resetPinOpen;
/**
* Pin which is connected to SET of the CLOSE relay
*/
int _setPinClose;
/**
* Pin which is connected to RESET of the CLOSE relay
*/
int _resetPinClose;
/**
* 1ms of movement in percent
*/
float _openStep;
float _closeStep;
/**
* Flag that inidcates the the init process is DONE
*/
bool _initDone;
/**
* The action that is triggered when the device powers up
*/
RotoAction _startupAction;
/**
* last millis() of blinking
*/
unsigned long _lastBlinkMillis;
unsigned long _startMoveMillis;
/*
* last blink state
*/
bool _lastBlinkState;
/*
* the status of the channel
*/
RotoChannelStatus _status;
/*
* the move status of the channel
*/
RotoChannelMoveStatus _moveStatus;
/*
* the last move status
*/
RotoChannelMoveStatus _lastMoveStatus;
volatile bool _manualMoveRequest;
volatile bool _manualMoveRequestOpenButton;
/**
* Position of window or Shutter
* 0.0 = window closed / shutter open
* 1.0 = window fully opened / shutter fully closed
*/
float _position;
/**
* Calculated new position, base on start-position + current_move_delta.
* After move has been completed, _position is updated with _newPosition
*/
float _newPosition;
/**
* Absolute position the window/shutter has to reach. Used/set with doPosition().
*/
float _targetPosition;
/**
* Last position, set when f.i. lock happens, to remember "previous position"
*/
float _previousPosition;
/** millis when last status was sent */
unsigned long _lastStatusUpdate;
/**
* last status value that has been sent to bus as status update.
* Used to ensure that we don't send a value twice, especially when we
* reach stop, where the latest value is (caused by calculation
* from % to byte) the same
*/
byte _lastSentPosition;
bool _isStopping;
/**
* Open time in ms, including rollover
* @return
*/
uint32_t getTime(uint8_t time);
/**
* Updating the LEDs to show current status
*/
void workLEDs();
/**
* This functions handles status updated of the channel.
* It is f.i. responsible for sending position updates to the bus.
*/
void workStatus();
/**
* The implementation is all about the position of window/shutter, so this method
* is handling the position while moving the window
*/
void workPosition();
/**
* Open shutter or window fully (if not stopped)
*/
void doOpen();
/**
* Close shutter or window fully (if not stopped)
*/
void doClose();
/**
* Stop movement
*/
void doStop();
/**
* Move window/shutter to an absolute position
* @param targetPosition the new absolute position the window/shutter should move to [0-1.0]
*/
void doPosition(float targetPosition);
/**
* Returns true, if shutter is locked or doiong lock-action,
* false if unlocked, or doing unlock-action
* @return bool
*/
bool isLocked();
/**
* returns true, if channel is in move state
* @return bool
*/
bool isMoving();
bool isFullyOpened();
bool isFullyClosed();
/**
* Keeps position in 0..1 range. If bigger, limit to 1, if smaller, limit to 0
*/
float limitPos(float pos);
/**
* Check for window setup
* @return true, if channel/group is window, false if it's shutter
*/
bool isWindow();
/**
* Returns frontend LED index
* @param led enum for open and close LED
* @return frontend LED index
*/
int getLed(FrontendLed led);
/**
* Returns true if just reached stop
* @return
*/
bool isJustStopped();
/**
* calculates the correct comobj index for the given "COMOBJ_...." variable
*/
byte getComObjIndex(byte comObj);
/**
* used by doOpen() and doClose() to send one-time-status of movement when movement begins
*/
void sendMovementStatus();
};
#endif /* ROTOCHANNEL_H_ */