Skip to content

Commit

Permalink
add pi gpio
Browse files Browse the repository at this point in the history
Change-type: patch
Signed-off-by: Ryan Cooke <[email protected]>
  • Loading branch information
rcooke-warwick committed Feb 14, 2024
1 parent e284b53 commit 2842fd2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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

0 comments on commit 2842fd2

Please sign in to comment.