forked from reefangel/Libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TimedPort which has properties for specific run times or stop times w…
…hen triggered. Can run on a port on any regular interval.
- Loading branch information
Showing
2 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/* | ||
* 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 "TimedPort.h" | ||
#include "InternalEEPROM.h" | ||
#include "ReefAngel.h" | ||
|
||
TimedPort::TimedPort() | ||
{ | ||
Init(); | ||
} | ||
|
||
void TimedPort::Init() | ||
{ | ||
ID = 0; | ||
Delay = 0; | ||
Repeat = 0; | ||
Offset = 0; | ||
Time = 0; | ||
StartTime = 0; | ||
StopTime = 0; | ||
Scale=1; | ||
|
||
activestatus = true; | ||
started = false; | ||
stopped = false; | ||
running = false; | ||
timer = 0; | ||
} | ||
|
||
void TimedPort::On() | ||
{ | ||
if (Delay) { | ||
DelayedOn(); | ||
} else { | ||
ReefAngel.Relay.On(ID); | ||
running=true; | ||
} | ||
} | ||
|
||
void TimedPort::Off() | ||
{ | ||
ReefAngel.Relay.Off(ID); | ||
running=false; | ||
} | ||
|
||
void TimedPort::DelayedOn() | ||
{ | ||
DelayedOn(Delay); | ||
} | ||
|
||
void TimedPort::DelayedOn(byte delay) | ||
{ | ||
ReefAngel.Relay.DelayedOn(ID, delay); | ||
running=ReefAngel.Relay.Status(ID); | ||
} | ||
|
||
void TimedPort::Auto() | ||
{ | ||
Override(2); | ||
} | ||
|
||
void TimedPort::Override(byte type) | ||
{ | ||
ReefAngel.Relay.Override(ID, type); | ||
running=ReefAngel.Relay.Status(ID); | ||
} | ||
|
||
void TimedPort::Set(bool status) | ||
{ | ||
ReefAngel.Relay.Set(ID, running=status); | ||
} | ||
|
||
void TimedPort::Toggle() | ||
{ | ||
ReefAngel.Relay.Set(ID, running=!running); | ||
} | ||
|
||
void TimedPort::Run() { | ||
Run(Offset, Time, Repeat-Time, true); | ||
} | ||
|
||
void TimedPort::Run(int offset, int ontime, int offtime, bool status) | ||
{ | ||
Osc(offset, ontime, offtime, status); | ||
if (activestatus) { | ||
if (now()<timer) { | ||
if (started) { | ||
On(); | ||
} | ||
if (stopped) { | ||
Off(); | ||
} | ||
} else { | ||
if (started) { | ||
started = false; | ||
} | ||
if (stopped) { | ||
stopped = false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void TimedPort::Osc() | ||
{ | ||
Osc(Offset, Time, Repeat-Time, true); | ||
} | ||
|
||
// Compatibility with the APEX syntax for OSC | ||
// OSC offset/ontime/offtime THEN on/off | ||
// Currently in seconds instead of minute:seconds | ||
void TimedPort::Osc(int offset, int ontime, int offtime, bool status) | ||
{ | ||
time_t o = offset*Scale; | ||
time_t t = ontime*Scale; | ||
time_t f = offtime*Scale; | ||
|
||
running=((now()+o)%(t+f))<t; | ||
|
||
if (!status) running=!running; | ||
ReefAngel.Relay.Set(ID, running); | ||
} | ||
|
||
void TimedPort::Start() | ||
{ | ||
Start(StartTime); | ||
} | ||
|
||
void TimedPort::Start(int time) | ||
{ | ||
time_t t = time*Scale; | ||
|
||
if (!started) { | ||
timer = now()+t; | ||
started = true; | ||
stopped = false; | ||
On(); | ||
} | ||
} | ||
|
||
void TimedPort::Stop() | ||
{ | ||
Stop(StopTime); | ||
} | ||
|
||
void TimedPort::Stop(int time) | ||
{ | ||
time_t t = time*Scale; | ||
|
||
if (!stopped) { | ||
timer = now()+t; | ||
started = false; | ||
stopped = true; | ||
Off(); | ||
} | ||
} | ||
|
||
void TimedPort::Load(int memStart) { | ||
ID=InternalMemory.read(memStart); | ||
Delay=InternalMemory.read(memStart+1); | ||
Time=InternalMemory.read_int(memStart+2); | ||
Offset=InternalMemory.read_int(memStart+4); | ||
Repeat=InternalMemory.read_int(memStart+6); | ||
StartTime=InternalMemory.read_int(memStart+8); | ||
StopTime=InternalMemory.read_int(memStart+10); | ||
Scale=InternalMemory.read_dword(memStart+12); | ||
} | ||
|
||
void TimedPort::Save(int memStart) { | ||
if (ID!=InternalMemory.read(memStart)) InternalMemory.write(memStart, ID); | ||
if (Delay!=InternalMemory.read(memStart+1)) InternalMemory.write(memStart+1, Delay); | ||
if (Time!=InternalMemory.read_int(memStart+2)) InternalMemory.write_int(memStart+2, Time); | ||
if (Offset!=InternalMemory.read_int(memStart+4)) InternalMemory.write_int(memStart+4, Offset); | ||
if (Repeat!=InternalMemory.read_int(memStart+6)) InternalMemory.write_int(memStart+6, Repeat); | ||
if (StartTime!=InternalMemory.read_int(memStart+8)) InternalMemory.write_int(memStart+8, StartTime); | ||
if (StopTime!=InternalMemory.read_int(memStart+10)) InternalMemory.write_int(memStart+10, StopTime); | ||
if (Scale!=InternalMemory.read_dword(memStart+12)) InternalMemory.write_dword(memStart+12, Scale); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
#ifndef __TIMEDPORT_H__ | ||
#define __TIMEDPORT_H__ | ||
|
||
#include <Globals.h> | ||
#include <ReefAngel.h> | ||
|
||
/* | ||
TimedPort Class | ||
*/ | ||
|
||
class TimedPort | ||
{ | ||
public: | ||
TimedPort(); | ||
byte ID; | ||
byte Delay; | ||
int Time; | ||
int Offset; | ||
int Repeat; | ||
int StartTime; | ||
int StopTime; | ||
double Scale; | ||
|
||
void On(); | ||
void Off(); | ||
void Auto(); | ||
void DelayedOn(); | ||
void DelayedOn(byte delay); | ||
void Override(byte type); | ||
void Set(bool status); | ||
void Toggle(); | ||
|
||
void Init(); | ||
inline void Write() { ReefAngel.Relay.Write(); } | ||
inline bool Status() { return ReefAngel.Relay.Status(ID); } | ||
inline bool isMaskOn() { return ReefAngel.Relay.isMaskOn(ID); } | ||
inline bool isMaskOff() { return ReefAngel.Relay.isMaskOff(ID); } | ||
inline bool IsRunning() { return running; } | ||
inline bool IsStarted() { return started; } | ||
inline bool IsStopped() { return stopped; } | ||
inline bool IsActive() { return activestatus; }; | ||
|
||
inline void Pause() { activestatus=false; } | ||
inline void Resume() { activestatus=true; } | ||
inline void SetActive(bool status) { activestatus=status; } | ||
void Run(); // Run Schedule + manual timers | ||
void Osc(); // Ignore timers run schedule | ||
void Run(int offset, int ontime, int offtime, bool status); | ||
void Osc(int offset, int ontime, int offtime, bool status); | ||
|
||
void Start(); // Start now for OnTime seconds | ||
void Stop(); // Disable for OffTime seconds | ||
void Start(int time); // Start now for specified seconds | ||
void Stop(int time); // Disable for specified seconds | ||
|
||
void Load(int memStart); | ||
void Save(int memStart); | ||
|
||
inline unsigned long GetTimer() { return timer; } | ||
|
||
private: | ||
unsigned long timer; | ||
bool started; | ||
bool stopped; | ||
bool running; | ||
bool activestatus; | ||
}; | ||
|
||
#endif // __TIMEDPORT_H__ | ||
|