Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pi gpio #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/features/digitalRelay/implementations/piGpio/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { exec } from 'child_process';
import { promisify } from 'util';

const execAsync = promisify(exec);

export class PiGpio implements DigitalRelay {
private pin: string

constructor(){
this.pin = process.env.GPIO_PIN || '26'
}

async setup(): Promise<void> {
try{
console.log(`Setting up GPIO pin ${this.pin}...`)
await execAsync(`echo ${this.pin} > /sys/class/gpio/export || true`);
await execAsync(`echo out > /sys/class/gpio/gpio${this.pin}/direction`);
} catch(e){
console.log(`Error during setup..`)
console.log(e)
}
}

// Power on the DUT
async on(): Promise<void> {
console.log(`Toggling gpio pin ${this.pin} on`)
await execAsync(`echo 1 > /sys/class/gpio/gpio${this.pin}/value`);
}

// Power off the DUT
async off(): Promise<void> {
console.log(`Toggling gpio pin ${this.pin} off`)
await execAsync(`echo 0 > /sys/class/gpio/gpio${this.pin}/value`);
}

async getState(): Promise<string> {
// TODO return state of power on/off
let res = await execAsync(`cat /sys/class/gpio/gpio${this.pin}/value`);
return res.stdout
}

async teardown(): Promise<void> {
await this.off();
}

}
2 changes: 2 additions & 0 deletions lib/features/digitalRelay/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { DigitalUSBRelay } from "./implementations/usb-relay";
import { DummyDigitalRelay } from "./implementations/dummy-digital-relay";
import { PiGpio } from "./implementations/piGpio"

const digitalRelayImplementations: {[key: string]: Type<DigitalRelay> } = {
usbRelay: DigitalUSBRelay,
gpio: PiGpio,
dummyPower: DummyDigitalRelay,
};

Expand Down
Loading