-
Notifications
You must be signed in to change notification settings - Fork 33
/
ReefAngel.cpp
2878 lines (2720 loc) · 76.7 KB
/
ReefAngel.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
/*
* Copyright 2010 Reef Angel / Roberto Imai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Updated by: Curt Binder
* Updates Released under Apache License, Version 2.0
*/
#include <Globals.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include "ReefAngel.h"
byte ButtonPress = 0;
#if defined DisplayLEDPWM && ! defined RemoveAllLights || defined DCPUMPCONTROL
boolean LightsOverride=true;
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
#ifdef RA_STANDARD
#include <Standard/instance.h>
#elif defined RA_PLUS
#include <Plus/instance.h>
#elif defined RA_STAR
#include <Star/instance.h>
#elif defined RA_TOUCH || defined RA_TOUCHDISPLAY
#include <Touch/instance.h>
#elif defined RA_EVOLUTION
#include <Evolution/instance.h>
#endif // RA_STANDARD
/* Constants declare in ReefAngel.h */
const byte ReefAngelClass::PH_MAXIMUM_RANGE[2]={4, 10};
const byte ReefAngelClass::PH_DEFAULT_RANGE[2]={7, 10};
const char ReefAngelClass::PH_SETUP_MENU_LABEL[2][19]={"Calibrate pH", "Calibrate pH(Exp.)"};
const char ReefAngelClass::PH_SETUP_MENU_STEP[2][13]={"First value", "Second value"};
void ReefAngelClass::Init()
{
Serial.begin(57600);
Serial.setTimeout(100);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
#ifdef RA_STANDARD
#include <Standard/init.h>
#elif defined RA_PLUS
#include <Plus/init.h>
#elif defined RA_STAR
#include <Star/init.h>
#elif defined RA_TOUCH || defined RA_TOUCHDISPLAY
#include <Touch/init.h>
#elif defined RA_EVOLUTION
#include <Evolution/init.h>
#endif // RA_STANDARD
pinMode(lowATOPin,INPUT);
pinMode(highATOPin,INPUT);
digitalWrite(lowATOPin,HIGH); //pull up resistor on lowATOPin
digitalWrite(highATOPin,HIGH); //pull up resistor on highATOPin
#if defined DisplayLEDPWM
pinMode(actinicPWMPin,OUTPUT);
pinMode(daylightPWMPin,OUTPUT);
digitalWrite(actinicPWMPin,LOW); //pull down resistor on actinicPWMPin
digitalWrite(daylightPWMPin,LOW); //pull down resistor on daylightPWMPin
#endif // DisplayLEDPWM
digitalWrite(0,HIGH); //pull up resistor on RX
digitalWrite(1,HIGH); //pull up resistor on TX
#ifdef __AVR_ATmega2560__
digitalWrite(14,HIGH); //pull up resistor on TX3
digitalWrite(15,HIGH); //pull up resistor on RX3
digitalWrite(16,HIGH); //pull up resistor on TX2
digitalWrite(17,HIGH); //pull up resistor on RX2
digitalWrite(18,HIGH); //pull up resistor on TX1
digitalWrite(19,HIGH); //pull up resistor on RX1
#endif // __AVR_ATmega2560__
TempSensor.Init();
RAStart=now();
LastFeedingMode=now();
LastWaterChangeMode=now();
LastStart = RAStart; // Set the time normal mode is started
BusLocked=false; // Bus is not locked
OldTempRelay=255;
OldDaylight=255;
OldActinic=255;
ChangeMode=0;
AlertFlags = 0;
StatusFlags = 0;
Splash=true;
Relay.AllOff();
CEM=0;
OverheatProbe = T2_PROBE;
TempProbe = T1_PROBE;
#ifdef ENABLE_ATO_LOGGING
AtoEventCount = 0;
#endif // ENABLE_ATO_LOGGING
#ifdef ENABLE_EXCEED_FLAGS
InternalMemory.write(Overheat_Exceed_Flag, 0);
InternalMemory.write(ATO_Exceed_Flag, 0);
InternalMemory.write(ATO_Single_Exceed_Flag, 0);
#endif // ENABLE_EXCEED_FLAGS
PHMin = InternalMemory.PHMin_read();
PHMax = InternalMemory.PHMax_read();
#ifdef ORPEXPANSION
ORPMin = InternalMemory.ORPMin_read();
ORPMax = InternalMemory.ORPMax_read();
#endif // ORPEXPANSION
#ifdef SALINITYEXPANSION
SalMax = InternalMemory.SalMax_read();
#endif // SALINITYEXPANSION
#ifdef PHEXPANSION
PHExpMin = InternalMemory.PHExpMin_read();
PHExpMax = InternalMemory.PHExpMax_read();
#endif // PHEXPANSION
taddr = InternalMemory.T1Pointer_read();
Params.Salinity=0;
Params.ORP=0;
Params.PHExp=0;
if ((taddr>120) || (taddr<0))
{
InternalMemory.T1Pointer_write(0);
taddr = 0;
}
Timer[FEEDING_TIMER].SetInterval(InternalMemory.FeedingTimer_read()); // Default Feeding timer
if ( InternalMemory.LCDTimer_read() < 60 ) InternalMemory.LCDTimer_write(60); // if it's less than 60, force it to 60
Timer[LCD_TIMER].SetInterval(InternalMemory.LCDTimer_read()); // LCD Sleep Mode timer
Timer[LCD_TIMER].Start(); // start timer
Timer[PORTAL_TIMER].SetInterval(300); // Portal
Timer[PORTAL_TIMER].Start(); // start timer
Timer[STORE_PARAMS_TIMER].SetInterval(720); // Store Params
Timer[STORE_PARAMS_TIMER].ForceTrigger();
// Set the default ports to be turned on & off during the 2 modes
FeedingModePorts = 0;
WaterChangePorts = 0;
// Set the ports that get shutoff when the overheat value is reached
OverheatShutoffPorts = 0;
// DelayedOn ports, do not manually modify this variable, let the DelayedOn function modify it
DelayedOnPorts = 0;
// Set the ports that get turned on when you select the Lights On
LightsOnPorts = 0;
#ifdef OVERRIDE_PORTS
// Override all relay masks for the following ports
OverridePorts = 0;
#endif // OVERRIDE_PORTS
#ifdef RelayExp
// Expansion Module ports to toggle, defaults to not toggle any ports
for ( byte i = 0; i < MAX_RELAY_EXPANSION_MODULES; i++ )
{
FeedingModePortsE[i] = 0;
WaterChangePortsE[i] = 0;
OverheatShutoffPortsE[i] = 0;
DelayedOnPortsE[i] = 0;
LightsOnPortsE[i] = 0;
#ifdef OVERRIDE_PORTS
// Override all relay masks for the following ports
OverridePortsE[i] = 0;
#endif // OVERRIDE_PORTS
}
#endif // RelayExp
#if defined wifi || defined I2CMASTER || defined ETH_WIZ5100
EM = PWMEbit + RFEbit + AIbit + Salbit + ORPbit + IObit + PHbit + WLbit;
EM1 = HUMbit + DCPumpbit + Leakbit + PARbit + SCPWMbit;
#ifdef RelayExp
for (byte a=0;a<InstalledRelayExpansionModules;a++)
{
REM+=1<<a;
}
#else // RelayExp
REM = 0;
#endif // RelayExp
#endif // wifi || I2CMASTER
#ifdef CUSTOM_VARIABLES
for ( byte EID = 0; EID < 8; EID++ )
{
CustomVar[EID]=0;
}
#endif //CUSTOM_VARIABLES
#ifdef RA_TOUCHDISPLAY
SendMaster(MESSAGE_RESEND_ALL,0,0);
#endif // RA_TOUCHDISPLAY
}
void ReefAngelClass::Refresh()
{
WDTReset();
switch (ChangeMode)
{
case FEEDING_MODE:
FeedingModeStart();
break;
case WATERCHANGE_MODE:
WaterChangeModeStart();
break;
case PH_CALIBRATE_MENU:
#if defined RA_TOUCH || defined RA_TOUCHDISPLAY || defined RA_EVOLUTION || defined RA_STAR
SetupTouchCalibratePH();
#else
StartSetupCalibrateChoicePH();
#endif // RA_TOUCH
break;
#ifdef SALINITYEXPANSION
case SAL_CALIBRATE_MENU:
#if defined RA_TOUCH || defined RA_TOUCHDISPLAY || defined RA_EVOLUTION || defined RA_STAR
SetupTouchCalibrateSal();
#else
StartSetupCalibrateSalinity();
#endif // RA_TOUCH
break;
#endif // SALINITYEXPANSION
#ifdef ORPEXPANSION
case ORP_CALIBRATE_MENU:
#if defined RA_TOUCH || defined RA_TOUCHDISPLAY || defined RA_EVOLUTION || defined RA_STAR
SetupTouchCalibrateORP();
#else
SetupCalibrateORP();
#endif // RA_TOUCH
break;
#endif // ORPEXPANSION
#ifdef PHEXPANSION
case PHE_CALIBRATE_MENU:
#if defined RA_TOUCH || defined RA_TOUCHDISPLAY || defined RA_EVOLUTION || defined RA_STAR
SetupTouchCalibratePHExp();
#else
StartSetupCalibrateChoicePHExp();
#endif // RA_TOUCH
break;
#endif // PHEXPANSION
#if defined WATERLEVELEXPANSION || defined MULTIWATERLEVELEXPANSION
case WL_CALIBRATE_MENU:
case WL1_CALIBRATE_MENU:
case WL2_CALIBRATE_MENU:
case WL3_CALIBRATE_MENU:
case WL4_CALIBRATE_MENU:
#if defined RA_TOUCH || defined RA_TOUCHDISPLAY || defined RA_EVOLUTION || defined RA_STAR
SetupTouchCalibrateWL(ChangeMode-WL_CALIBRATE_MENU);
#else
SetupCalibrateWaterLevel();
#endif // RA_TOUCH
break;
#endif // WATERLEVELEXPANSION || MULTIWATERLEVELEXPANSION
}
ChangeMode=0;
boolean LightRelayOn=false;
for (int l=0;l<8;l++)
{
if (LightsOnPorts & 1<<l)
if (ReefAngel.Relay.RelayMaskOn & 1<<l) LightRelayOn=true;
}
#ifdef DCPUMPCONTROL
if (DCPump.UseMemory)
{
DCPump.Mode=InternalMemory.DCPumpMode_read();
DCPump.Speed=InternalMemory.DCPumpSpeed_read();
DCPump.Duration=InternalMemory.DCPumpDuration_read();
DCPump.Threshold=InternalMemory.DCPumpThreshold_read();
}
byte SyncSpeed;
byte AntiSyncSpeed;
switch (DCPump.Mode)
{
case Constant:
{
SyncSpeed=DCPump.Speed;
AntiSyncSpeed=DCPump.Speed;
break;
}
case Lagoon:
{
SyncSpeed=ReefCrestMode(DCPump.Speed,10,true);
AntiSyncSpeed=ReefCrestMode(DCPump.Speed,10,false);
break;
}
case ReefCrest:
{
SyncSpeed=ReefCrestMode(DCPump.Speed,20,true);
AntiSyncSpeed=ReefCrestMode(DCPump.Speed,20,false);
break;
}
case ShortPulse:
{
SyncSpeed=ShortPulseMode(0,DCPump.Speed,DCPump.Duration*10,true);
AntiSyncSpeed=ShortPulseMode(0,DCPump.Speed,DCPump.Duration*10,false);
break;
}
case LongPulse:
{
SyncSpeed=LongPulseMode(0,DCPump.Speed,DCPump.Duration,true);
AntiSyncSpeed=LongPulseMode(0,DCPump.Speed,DCPump.Duration,false);
break;
}
case Gyre:
{
SyncSpeed=GyreMode(DCPump.Threshold,DCPump.Speed,DCPump.Duration,true);
AntiSyncSpeed=GyreMode(DCPump.Threshold,DCPump.Speed,DCPump.Duration,false);
break;
}
case NutrientTransport:
{
SyncSpeed=NutrientTransportMode(0,DCPump.Speed,DCPump.Duration*10,true);
AntiSyncSpeed=NutrientTransportMode(0,DCPump.Speed,DCPump.Duration*10,false);
break;
}
case TidalSwell:
{
SyncSpeed=TidalSwellMode(DCPump.Speed,true);
AntiSyncSpeed=TidalSwellMode(DCPump.Speed,false);
break;
}
case Sine:
{
SyncSpeed=SineMode(DCPump.Threshold,DCPump.Speed,DCPump.Duration,true);
AntiSyncSpeed=SineMode(DCPump.Threshold,DCPump.Speed,DCPump.Duration,false);
break;
}
case Else:
{
SyncSpeed=ElseMode(DCPump.Speed,DCPump.Duration,true);
AntiSyncSpeed=ElseMode(DCPump.Speed,DCPump.Duration,false);
break;
}
}
if (DisplayedMenu==FEEDING_MODE)
{
if (DCPump.FeedingSpeed < 100)
{
SyncSpeed=DCPump.FeedingSpeed;
AntiSyncSpeed=DCPump.FeedingSpeed;
}
}
if (DisplayedMenu==WATERCHANGE_MODE)
{
if (DCPump.WaterChangeSpeed < 100)
{
SyncSpeed=DCPump.WaterChangeSpeed;
AntiSyncSpeed=DCPump.WaterChangeSpeed;
}
}
SetDCPumpChannels(SyncSpeed,AntiSyncSpeed);
#endif // DCPUMPCONTROL
#if defined DisplayLEDPWM && !defined REEFANGEL_MINI
#ifndef DCPUMPCONTROL
if (LightRelayOn && LightsOverride)
{
#if defined(__SAM3X8E__)
VariableControl.SetActinic(InternalMemory.LEDPWMActinic_read());
VariableControl.SetDaylight(InternalMemory.LEDPWMDaylight_read());
#else // __SAM3X8E__
PWM.SetActinic(InternalMemory.LEDPWMActinic_read());
PWM.SetDaylight(InternalMemory.LEDPWMDaylight_read());
#endif // __SAM3X8E__
#if defined RA_STAR
#if defined(__SAM3X8E__)
VariableControl.SetActinic2(InternalMemory.LEDPWMActinic2_read());
VariableControl.SetDaylight2(InternalMemory.LEDPWMDaylight2_read());
#else // __SAM3X8E__
PWM.SetActinic2(InternalMemory.LEDPWMActinic2_read());
PWM.SetDaylight2(InternalMemory.LEDPWMDaylight2_read());
#endif // __SAM3X8E__
#endif // RA_STAR
}
#endif // DCPUMPCONTROL
// issue #3: Redundant code
// issue #12: Revert back
#if defined(__SAM3X8E__)
analogWrite(actinicPWMPin, map(VariableControl.GetActinicValueRaw(),0,4095,0,255));
analogWrite(daylightPWMPin, map(VariableControl.GetDaylightValueRaw(),0,4095,0,255));
#else // __SAM3X8E__
#ifdef RA_PLUS
if (relaytest)
{
PWM.SetActinic((millis()%2000)/20);
PWM.SetDaylight(100-((millis()%2000)/20));
}
#endif // RA_PLUS
analogWrite(actinicPWMPin, map(PWM.GetActinicValueRaw(),0,4095,0,255));
analogWrite(daylightPWMPin, map(PWM.GetDaylightValueRaw(),0,4095,0,255));
#endif // __SAM3X8E__
#if defined RA_STAR
analogWrite(actinic2PWMPin, map(PWM.GetActinic2ValueRaw(),0,4095,0,255));
analogWrite(daylight2PWMPin, map(PWM.GetDaylight2ValueRaw(),0,4095,0,255));
SDFound=(PINJ & (1<<PJ3))==0;
#endif // RA_STAR
#if defined(__SAM3X8E__)
analogWrite(actinic2PWMPin, map(VariableControl.GetActinic2ValueRaw(),0,4095,0,255));
analogWrite(daylight2PWMPin, map(VariableControl.GetDaylight2ValueRaw(),0,4095,0,255));
#endif // __SAM3X8E__
#endif // defined DisplayLEDPWM && !defined REEFANGEL_MINI
#if defined RA_TOUCH || defined RA_TOUCHDISPLAY || defined RA_EVOLUTION || defined RA_STAR
if (!Splash)
{
#if not defined NOTILT
if (!ReefAngel.Sleeping)
{
Tilt.Refresh();
SetOrientation(Tilt.GetOrientation());
}
#endif // NOTILT
}
if ((millis()>SplashDuration) && Splash)
{
Splash=false;
if (TS.IsCalibrationNeeded())
{
CalibrateTouchScreen();
}
TouchLCD.FullClear(BKCOLOR);
}
#endif // RA_TOUCH
#if not defined RA_TOUCHDISPLAY
#ifdef RFEXPANSION
byte RFRecv=0;
RFRecv=RF.RFCheck();
if (RFRecv==1)
{
ClearScreen(DefaultBGColor);
FeedingModeStart();
}
if (RFRecv==2)
{
Timer[FEEDING_TIMER].ForceTrigger();
}
if (DisplayedMenu!=FEEDING_MODE && RF.UseMemory) RF.SetMode(InternalMemory.RFMode_read(),InternalMemory.RFSpeed_read(),InternalMemory.RFDuration_read());
if (LightRelayOn)
{
for (byte a=0; a<RF_CHANNELS; a++)
RF.SetChannel(a,InternalMemory.read(Mem_B_RadionSlopeEndW+(3*a)));
}
#ifdef RADION_COMM
RF.RadionWrite();
#endif // RADION_COMM
#endif // RFEXPANSION
#ifdef AI_LED
if (LightRelayOn)
{
for (byte a=0; a<AI_CHANNELS; a++)
AI.SetChannel(a,InternalMemory.read(Mem_B_AISlopeEndW+(3*a)));
}
if (millis()-AI.AImillis>AI.StreamDelay)
{
AI.Send();
AI.AImillis=millis();
}
#endif // AI_LED
#if defined PWMEXPANSION && defined DisplayLEDPWM
#if defined(__SAM3X8E__)
VariableControl.ExpansionWrite();
#else // __SAM3X8E__
PWM.ExpansionWrite();
#endif // __SAM3X8E__
#endif // PWMEXPANSION
#ifdef SIXTEENCHPWMEXPANSION && defined DisplayLEDPWM
#if defined(__SAM3X8E__)
VariableControl.SIXTEENChExpansionWrite();
#else // __SAM3X8E__
PWM.SIXTEENChExpansionWrite();
#endif // __SAM3X8E__
#endif // SIXTEENCHPWMEXPANSION
#ifdef IOEXPANSION
if (bitRead(ReefAngel.CEM,CloudIOBit)==0)
IO.GetChannel();
#endif // IOEXPANSION
#endif // RA_TOUCHDISPLAY
#ifdef OVERRIDE_PORTS
// Reset relay masks for ports we want always in their programmed states.
ReefAngel.Relay.RelayMaskOn &= ~OverridePorts;
ReefAngel.Relay.RelayMaskOff |= OverridePorts;
#ifdef RelayExp
byte i;
for ( i = 0; i < MAX_RELAY_EXPANSION_MODULES; i++ )
{
Relay.RelayMaskOnE[i] &= ~OverridePortsE[i];
Relay.RelayMaskOffE[i] |= OverridePortsE[i];
}
#endif // RelayExp
#endif // OVERRRIDE_PORTS
#ifdef RA_PLUS
if (relaytest)
{
Relay.RelayData=0;
Relay.RelayMaskOff=255;
Relay.RelayMaskOn=1<<((millis()%3200)/400);
}
#endif // RA_PLUS
Relay.Write();
#ifdef ETH_WIZ5100
Network.Update();
#endif // ETH_WIZ5100
#ifdef RANET
// Send RANet data
if (millis()-RANetlastmillis>RANetDelay)
{
RANetlastmillis=millis();
RANetCRC=0;
RANetData[0]=RANetSeq;
RANetData[1]=RANET_SIZE;
for (int a=0;a<MAX_RELAY_EXPANSION_MODULES;a++)
{
#ifdef RelayExp
byte TempRelay = Relay.RelayDataE[a];
TempRelay &= Relay.RelayMaskOffE[a];
TempRelay |= Relay.RelayMaskOnE[a];
RANetData[2+a]=TempRelay;
RANetData[10+a]=Relay.RANetFallBackE[a];
#else
RANetData[2+a]=0;
RANetData[10+a]=0;
#endif // RelayExp
}
for (int a=0;a<PWM_EXPANSION_CHANNELS*2;a=a+2)
{
#ifdef PWMEXPANSION
#if defined(__SAM3X8E__)
RANetData[18+a]=VariableControl.GetChannelValue(a);
#else
int newdata=PWM.GetChannelValueRaw(a/2);
RANetData[18+a]=newdata&0xff; // LSB
RANetData[18+a+1]=newdata>>8; // MSB
#endif
#else
RANetData[18+a]=0;
RANetData[18+a+1]=0;
#endif // PWMEXPANSION
}
for (int a=0;a<SIXTEENCH_PWM_EXPANSION_CHANNELS*2;a=a+2)
{
#ifdef SIXTEENCHPWMEXPANSION
#if defined(__SAM3X8E__)
RANetData[26+a]=VariableControl.Get16ChannelValue(a);
#else
int newdata=PWM.Get16ChannelValueRaw(a/2);
RANetData[30+a]=newdata&0xff; // LSB
RANetData[30+a+1]=newdata>>8; // MSB
#endif
#else
RANetData[30+a]=0;
RANetData[30+a+1]=0;
#endif // SIXTEENCHPWMEXPANSION
}
// char buf[3];
RANetData[62]=TriggerValue; // Trigger byte
TriggerValue=0; // Reset to 0
for (int a=0;a<RANET_SIZE-2;a++)
{
RANetCRC+=RANetData[a];
RANET_SERIAL.write(RANetData[a]);
// sprintf(buf,"%02x",RANetData[a]);
// RANET_SERIAL.print(buf);
}
RANET_SERIAL.write(RANetCRC);
RANET_SERIAL.println();
// sprintf(buf,"%02x",RANetCRC);
// RANET_SERIAL.println(buf);
RANetSeq++;
}
#endif // RANET
#if defined wifi || defined RA_STAR
ReefAngel.Network.ReceiveData();
#endif // wifi || defined RA_STAR
if (ds.read_bit()==0) return; // ds for OneWire TempSensor
now();
#ifdef DirectTempSensor
RefreshScreen();
Params.Temp[T1_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT1);
RefreshScreen();
Params.Temp[T2_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT2);
RefreshScreen();
Params.Temp[T3_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT3);
RefreshScreen();
#ifdef EXTRA_TEMP_PROBES
Params.Temp[T4_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT4);
RefreshScreen();
Params.Temp[T5_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT5);
RefreshScreen();
Params.Temp[T6_PROBE]=TempSensor.ReadTemperature(TempSensor.addrT6);
RefreshScreen();
#endif // EXTRA_TEMP_PROBES
#else // DirectTempSensor
int x = TempSensor.ReadTemperature(TempSensor.addrT1);
RefreshScreen();
int y;
y = x - Params.Temp[T1_PROBE];
// check to make sure the temp readings aren't beyond max allowed
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T1_PROBE] == 0 || ~x) Params.Temp[T1_PROBE] = x;
x = TempSensor.ReadTemperature(TempSensor.addrT2);
RefreshScreen();
y = x - Params.Temp[T2_PROBE];
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T2_PROBE] == 0 || ~x) Params.Temp[T2_PROBE] = x;
x = TempSensor.ReadTemperature(TempSensor.addrT3);
RefreshScreen();
y = x - Params.Temp[T3_PROBE];
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T3_PROBE] == 0 || ~x) Params.Temp[T3_PROBE] = x;
#ifdef EXTRA_TEMP_PROBES
x = TempSensor.ReadTemperature(TempSensor.addrT4);
RefreshScreen();
y = x - Params.Temp[T4_PROBE];
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T4_PROBE] == 0 || ~x) Params.Temp[T4_PROBE] = x;
x = TempSensor.ReadTemperature(TempSensor.addrT5);
RefreshScreen();
y = x - Params.Temp[T5_PROBE];
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T5_PROBE] == 0 || ~x) Params.Temp[T5_PROBE] = x;
x = TempSensor.ReadTemperature(TempSensor.addrT6);
RefreshScreen();
y = x - Params.Temp[T6_PROBE];
if ( abs(y) < MAX_TEMP_SWING || Params.Temp[T6_PROBE] == 0 || ~x) Params.Temp[T6_PROBE] = x;
#endif // EXTRA_TEMP_PROBES
#endif // DirectTempSensor
Params.PH=0;
for (int a=0;a<20;a++)
{
Params.PH+=analogRead(PHPin);
}
Params.PH/=20;
RefreshScreen();
Params.PH=map(Params.PH, PHMin, PHMax, 700, 1000); // apply the calibration to the sensor reading
Params.PH=constrain(Params.PH,100,1400);
RefreshScreen();
TempSensor.RequestConversion();
RefreshScreen();
#if defined SALINITYEXPANSION
if (bitRead(ReefAngel.CEM,CloudSalinityBit)==0)
{
unsigned long tempsal=0;
for (int a=0;a<20;a++)
{
tempsal+=Salinity.Read();
}
Params.Salinity=tempsal/20;
ApplySalinityCompensation();
Params.Salinity=map(Params.Salinity, 0, SalMax, 60, 350); // apply the calibration to the sensor reading
}
RefreshScreen();
#endif // defined SALINITYEXPANSION
#if defined ORPEXPANSION
if (bitRead(ReefAngel.CEM,CloudORPBit)==0)
{
unsigned long temporp=0;
for (int a=0;a<20;a++)
{
temporp+=ORP.Read();
}
Params.ORP=temporp/20;
if (Params.ORP!=0)
{
Params.ORP=map(Params.ORP, ORPMin, ORPMax, 0, 470); // apply the calibration to the sensor reading
Params.ORP=constrain(Params.ORP,0,550);
}
}
RefreshScreen();
#endif // defined ORPEXPANSION
#if defined PHEXPANSION
if (bitRead(ReefAngel.CEM,CloudPHExpBit)==0)
{
unsigned long tempph=0;
for (int a=0;a<5;a++)
{
tempph+=PH.Read();
}
Params.PHExp=tempph/5;
if (Params.PHExp!=0)
{
Params.PHExp=map(Params.PHExp, PHExpMin, PHExpMax, 700, 1000); // apply the calibration to the sensor reading
Params.PHExp=constrain(Params.PHExp,100,1400);
}
}
RefreshScreen();
#endif // defined PHEXPANSION
#if defined WATERLEVELEXPANSION || defined MULTIWATERLEVELEXPANSION
if (bitRead(ReefAngel.CEM,CloudWLBit)==0)
WaterLevel.Convert();
if (bitRead(ReefAngel.CEM,CloudMultiWLBit)==0)
WaterLevel.ConvertMulti();
RefreshScreen();
#endif // WATERLEVELEXPANSION || MULTIWATERLEVELEXPANSION
#if defined HUMIDITYEXPANSION
Humidity.Read();
RefreshScreen();
#endif // defined HUMIDITYEXPANSION
OverheatCheck();
#ifdef LEAKDETECTOREXPANSION
LeakCheck();
RefreshScreen();
#endif // LEAKDETECTOREXPANSION
#if defined PAREXPANSION
if (bitRead(ReefAngel.CEM,CloudPARBit)==0)
PAR.Convert();
RefreshScreen();
#endif // defined PAREXPANSION
#ifdef BUSCHECK
Wire.beginTransmission(0x68);
Wire.write(0);
int a=Wire.endTransmission();
if (a==5)
BusLocked=true; // Bus is locked
else
BusLocked=false; // Bus is not locked
if (BusLocked)
{
LED.On();
delay(20);
LED.Off();
bitSet(AlertFlags,BusLockFlag);
sbi(PORTH,2); // Turn off exp bus power
}
else
{
bitClear(AlertFlags,BusLockFlag);
cbi(PORTH,2); // Turn on exp bus power
}
#endif // BUSCHECK
}
void ReefAngelClass::Reboot()
{
#ifdef RA_STAR
TouchLCD.FullClear(COLOR_WHITE); // Clear screen
#endif
while(1);
}
#ifdef RANET
void ReefAngelClass::RANetTrigger(byte Trigger)
{
TriggerValue = Trigger;
}
#endif // RANET
void ReefAngelClass::SetTemperatureUnit(byte unit)
{
// 0 (or DEGREE_F) for farenheit
// 1 (or DEGREE_C) for celsius
TempSensor.unit = unit;
}
void ReefAngelClass::ConvertTempUnit()
{
// check to see if the internal memory values are set correctly
// if they are not, convert them from F to C or vice versa
int x;
if ( TempSensor.unit )
{
// C
// if the values are larger than the highest temp, then we know we have F stored
bool fConvert = false;
x = InternalMemory.HeaterTempOn_read();
if ( x > DEGREE_C_HIGH_TEMP )
{
fConvert = true;
x = CONVERT_TO_C(x);
InternalMemory.HeaterTempOn_write(x);
}
x = InternalMemory.HeaterTempOff_read();
if ( x > DEGREE_C_HIGH_TEMP )
{
fConvert = true;
x = CONVERT_TO_C(x);
InternalMemory.HeaterTempOff_write(x);
}
x = InternalMemory.ChillerTempOn_read();
if ( x > DEGREE_C_HIGH_TEMP )
{
fConvert = true;
x = CONVERT_TO_C(x);
InternalMemory.ChillerTempOn_write(x);
}
x = InternalMemory.ChillerTempOff_read();
if ( x > DEGREE_C_HIGH_TEMP )
{
fConvert = true;
x = CONVERT_TO_C(x);
InternalMemory.ChillerTempOff_write(x);
}
x = InternalMemory.OverheatTemp_read();
if ( (x > DEGREE_C_OVERHEAT_HIGH_TEMP) || fConvert )
{
x = CONVERT_TO_C(x);
InternalMemory.OverheatTemp_write(x);
}
}
else
{
// F
// if the values are smaller than lowest temp, then we know we have C stored
bool fConvert = false;
x = InternalMemory.HeaterTempOn_read();
if ( x < DEGREE_F_LOW_TEMP )
{
fConvert = true;
x = CONVERT_TO_F(x);
InternalMemory.HeaterTempOn_write(x);
}
x = InternalMemory.HeaterTempOff_read();
if ( x < DEGREE_F_LOW_TEMP )
{
fConvert = true;
x = CONVERT_TO_F(x);
InternalMemory.HeaterTempOff_write(x);
}
x = InternalMemory.ChillerTempOn_read();
if ( x < DEGREE_F_LOW_TEMP )
{
fConvert = true;
x = CONVERT_TO_F(x);
InternalMemory.ChillerTempOn_write(x);
}
x = InternalMemory.ChillerTempOff_read();
if ( x < DEGREE_F_LOW_TEMP )
{
fConvert = true;
x = CONVERT_TO_F(x);
InternalMemory.ChillerTempOff_write(x);
}
x = InternalMemory.OverheatTemp_read();
if ( (x < DEGREE_F_OVERHEAT_LOW_TEMP) || fConvert )
{
x = CONVERT_TO_F(x);
InternalMemory.OverheatTemp_write(x);
}
}
}
#if defined SALINITYEXPANSION
void ReefAngelClass::ApplySalinityCompensation()
{
// Salinity Compensation was contributed by ahmedess
// http://forum.reefangel.com/viewtopic.php?p=7386#p7386
// Credits to dazza1304
// http://forum.reefangel.com/viewtopic.php?f=3&t=2670
if (Salinity.TemperatureCompensation!=-1 && Params.Temp[TempProbe]>0)
{
double SalCompensation;
double SalConstant=Salinity.TemperatureCompensation;
if (Salinity.TemperatureCompensation==0)
{
if (TempSensor.unit)
SalConstant=0.0024;
else
SalConstant=0.001333;
}
SalCompensation=Params.Salinity/(1+((Params.Temp[TempProbe]-InternalMemory.SalTempComp_read())*SalConstant));
Params.Salinity=round(SalCompensation);
}
}
#endif // SALINITYEXPANSION
#ifdef BUSCHECK
boolean ReefAngelClass::isBusLock()
{
return bitRead(AlertFlags, BusLockFlag);
}
#endif //BUSCHECK
#ifdef LEAKDETECTOREXPANSION
boolean ReefAngelClass::IsLeakDetected()
{
boolean detect=false;
if (bitRead(ReefAngel.CEM,CloudLeakBit)==0)
{
int iLeak=0;
Wire.requestFrom(I2CLeak, 2);
if (Wire.available())
{
iLeak = Wire.read();
iLeak = iLeak<<8;
iLeak += Wire.read();
}
detect=iLeak>2000;
#ifdef EMBEDDED_LEAK
detect|=analogRead(LeakPin)<400;
LeakValue=detect;
#endif // EMBEDDED_LEAK
#ifdef RA_TOUCHDISPLAY
detect=LeakStatus;
#endif // RA_TOUCHDISPLAY
}
else
{
detect=LeakValue;
}
return detect;
}
void ReefAngelClass::LeakCheck()
{
// if leak is detected
if ( !IsLeakDetected() )
Leakmillis=millis();
if (millis()-Leakmillis>3000) // Only flag leak if we have a leak for 3 seconds
{
LED.On();
bitSet(AlertFlags,LeakFlag);
// invert the ports that are activated
Relay.RelayMaskOff &= ~LeakShutoffPorts;
#ifdef RelayExp
for ( byte i = 0; i < MAX_RELAY_EXPANSION_MODULES; i++ )
{
Relay.RelayMaskOffE[i] &= ~LeakShutoffPortsE[i];
}
#endif // RelayExp
}
}
void ReefAngelClass::LeakClear()
{
LED.Off();
bitClear(AlertFlags,LeakFlag);
Relay.RelayMaskOff |= LeakShutoffPorts;
#ifdef RelayExp
for ( byte i = 0; i < MAX_RELAY_EXPANSION_MODULES; i++ )
{
Relay.RelayMaskOffE[i] |= LeakShutoffPortsE[i];
}
#endif // RelayExp
Relay.Write();
#if defined RA_TOUCH || defined RA_TOUCHDISPLAY || defined RA_STAR
if (DisplayedMenu==TOUCH_MENU)
SetDisplayedMenu(DEFAULT_MENU);
#endif // RA_TOUCH
#ifdef RA_TOUCHDISPLAY
SendMaster(MESSAGE_COMMAND,COMMAND_CLEAR_LEAK,0);
#endif // RA_TOUCHDISPLAY
}
boolean ReefAngelClass::isLeak()
{
return bitRead(AlertFlags, LeakFlag);
}
#endif // LEAKDETECTOREXPANSION
void ReefAngelClass::StandardLights(byte LightsRelay, byte OnHour, byte OnMinute, byte OffHour, byte OffMinute)
{
int NumMinsToday=NumMins(hour(),minute());
if (NumMins(OffHour,OffMinute) > NumMins(OnHour,OnMinute))
{
if (NumMinsToday >= NumMins(OnHour,OnMinute)) Relay.On(LightsRelay); else Relay.Off(LightsRelay);
if (NumMinsToday >= NumMins(OffHour,OffMinute)) Relay.Off(LightsRelay);
}
else
{
if (NumMinsToday >= NumMins(OffHour,OffMinute)) Relay.Off(LightsRelay); else Relay.On(LightsRelay);
if (NumMinsToday >= NumMins(OnHour,OnMinute)) Relay.On(LightsRelay);
}
}
void ReefAngelClass::MHLights(byte LightsRelay, byte OnHour, byte OnMinute, byte OffHour, byte OffMinute, byte MHDelay)
{
unsigned int MHTimer = MHDelay;
MHTimer *= SECS_PER_MIN;
if ( now()-RAStart > MHTimer )
StandardLights(LightsRelay, OnHour, OnMinute, OffHour, OffMinute);
}
void ReefAngelClass::StandardHeater(byte Probe, byte HeaterRelay, int LowTemp, int HighTemp)
{
if (LowTemp < 100) LowTemp *= 10; // Correct temp settings that aren't in the correct range
if (HighTemp < 100) HighTemp *= 10; // Correct temp settings that aren't in the correct range
if (Params.Temp[Probe] == 0) return; // Don't turn the heater on if the temp is reading 0
if (Params.Temp[Probe] <= LowTemp && Params.Temp[Probe] > 0) Relay.On(HeaterRelay); // If sensor 1 temperature <= LowTemp - turn on heater
if (Params.Temp[Probe] >= HighTemp) Relay.Off(HeaterRelay); // If sensor 1 temperature >= HighTemp - turn off heater
}
void ReefAngelClass::StandardHeater(byte HeaterRelay, int LowTemp, int HighTemp)
{