From 6a9f238969fc0e877c7bef103a4953dabf0f2936 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Wed, 8 Jan 2020 12:20:38 +0200 Subject: feat(gpio): add pinState() method close #8 --- src/gpio.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src/gpio.ts') diff --git a/src/gpio.ts b/src/gpio.ts index d39a918..293c559 100644 --- a/src/gpio.ts +++ b/src/gpio.ts @@ -3,7 +3,7 @@ * Part of AVR8js * Reference: http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf * - * Copyright (C) 2019, Uri Shaked + * Copyright (C) 2019, 2020, Uri Shaked */ import { CPU } from './cpu'; import { u8 } from './types'; @@ -83,10 +83,17 @@ export const portLConfig: AVRPortConfig = { PORT: 0x10b }; +export enum PinState { + Low, + High, + Input, + InputPullUp +} + export class AVRIOPort { private listeners: GPIOListener[] = []; - constructor(cpu: CPU, portConfig: AVRPortConfig) { + constructor(private cpu: CPU, private portConfig: AVRPortConfig) { cpu.writeHooks[portConfig.PORT] = (value: u8, oldValue: u8) => { const ddrMask = cpu.data[portConfig.DDR]; value &= ddrMask; @@ -114,6 +121,25 @@ export class AVRIOPort { this.listeners = this.listeners.filter((l) => l !== listener); } + /** + * Get the state of a given GPIO pin + * + * @param index Pin index to return from 0 to 7 + * @returns PinState.Low or PinState.High if the pin is set to output, PinState.Input if the pin is set + * to input, and PinState.InputPullUp if the pin is set to input and the internal pull-up resistor has + * been enabled. + */ + pinState(index: number) { + const ddr = this.cpu.data[this.portConfig.DDR]; + const port = this.cpu.data[this.portConfig.PORT]; + const bitMask = 1 << index; + if (ddr & bitMask) { + return port & bitMask ? PinState.High : PinState.Low; + } else { + return port & bitMask ? PinState.InputPullUp : PinState.Input; + } + } + private writeGpio(value: u8, oldValue: u8) { for (const listener of this.listeners) { listener(value, oldValue); -- cgit v1.2.3