This repository has been archived by the owner on Aug 17, 2021. It is now read-only.
-
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.
controller: mechanical relay implementation
- Loading branch information
1 parent
5ce1fa5
commit 5c7ca1e
Showing
7 changed files
with
108 additions
and
7 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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,74 @@ | ||
import * as rpio from 'rpio'; | ||
|
||
import config from './config'; | ||
import { Relay } from './interfaces'; | ||
|
||
|
||
export class MechanicalRelay implements Relay { | ||
public on: boolean; | ||
|
||
constructor(private pin: number) { | ||
this.on = false; | ||
rpio.open(this.pin, rpio.OUTPUT); | ||
} | ||
|
||
switchOn(): void { | ||
this.toggle(true); | ||
} | ||
|
||
switchOff(): void { | ||
this.toggle(false); | ||
} | ||
|
||
toggle(on: boolean): void { | ||
rpio.write(this.pin, !on ? rpio.HIGH : rpio.LOW); | ||
this.on = !on; | ||
} | ||
} | ||
|
||
// in order to spread out the wear on the mechanical relays, keep | ||
// a pointer to the current relay and only turn it on when a relay | ||
// needs is activated | ||
// on the next activation increment the relay pointer | ||
export class RelayArray implements Relay { | ||
private currentRelayIndex: number; | ||
private relays: Relay[]; | ||
|
||
constructor(private reversed = true) { | ||
this.currentRelayIndex = 0; | ||
this.relays = config.Relay.pins.map(pin => new MechanicalRelay(pin)); | ||
} | ||
|
||
public get on(): boolean { | ||
return this.currentRelay.on; | ||
} | ||
|
||
private get currentRelay(): Relay { | ||
return this.relays[this.currentRelayIndex]; | ||
} | ||
|
||
switchOn(): void { | ||
// check that the current relay is not already switched on | ||
if (!this.currentRelay.on) { | ||
// increment the relay pointer | ||
this.currentRelayIndex = (this.currentRelayIndex + 1) % this.relays.length; | ||
// switch on the current relay | ||
this.currentRelay.switchOn(); | ||
} | ||
} | ||
|
||
switchOff(): void { | ||
// turn off any relays that are on | ||
this.relays | ||
.filter(relay => relay.on) | ||
.forEach(relay => relay.switchOff()); | ||
} | ||
|
||
toggle(on: boolean): void { | ||
if (!(on && this.reversed)) { | ||
this.switchOn(); | ||
} else { | ||
this.switchOff(); | ||
} | ||
} | ||
} |