Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gpio): add option to configure switches
- Loading branch information
Showing
7 changed files
with
122 additions
and
1 deletion.
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
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,28 @@ | ||
import { Gpio } from 'onoff'; | ||
import { GpioSwitch } from './gpio.switch'; | ||
|
||
jest.mock('onoff'); | ||
|
||
describe('GpioSwitch', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should write 1 to the GPIO pin when turned on', async () => { | ||
const pin = new Gpio(17, 'out'); | ||
const gpioSwitch = new GpioSwitch('test', 'Test', pin); | ||
|
||
await gpioSwitch.turnOn(); | ||
expect(pin.write).toHaveBeenCalledWith(1); | ||
expect(gpioSwitch.state).toBeTruthy(); | ||
}); | ||
|
||
it('should write 0 to the GPIO pin when turned off', async () => { | ||
const pin = new Gpio(17, 'out'); | ||
const gpioSwitch = new GpioSwitch('test', 'Test', pin); | ||
|
||
await gpioSwitch.turnOff(); | ||
expect(pin.write).toHaveBeenCalledWith(0); | ||
expect(gpioSwitch.state).toBeFalsy(); | ||
}); | ||
}); |
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,21 @@ | ||
import { Switch } from '../../entities/switch'; | ||
import { Gpio } from 'onoff'; | ||
|
||
export class GpioSwitch extends Switch { | ||
private gpio: Gpio; | ||
|
||
constructor(id: string, name: string, gpio: Gpio) { | ||
super(id, name, false); | ||
this.gpio = gpio; | ||
} | ||
|
||
async turnOn(): Promise<void> { | ||
await this.gpio.write(1); | ||
super.turnOn(); | ||
} | ||
|
||
async turnOff(): Promise<void> { | ||
await this.gpio.write(0); | ||
super.turnOff(); | ||
} | ||
} |