diff options
| author | Uri Shaked | 2020-01-08 12:20:38 +0200 |
|---|---|---|
| committer | Uri Shaked | 2020-01-08 12:20:38 +0200 |
| commit | 6a9f238969fc0e877c7bef103a4953dabf0f2936 (patch) | |
| tree | 1f3b639f2309e26624beec31649e7c4037526945 /src/gpio.ts | |
| parent | chore: release 0.5.0 (diff) | |
| download | avr8js-6a9f238969fc0e877c7bef103a4953dabf0f2936.tar.gz avr8js-6a9f238969fc0e877c7bef103a4953dabf0f2936.tar.bz2 avr8js-6a9f238969fc0e877c7bef103a4953dabf0f2936.zip | |
feat(gpio): add pinState() method
close #8
Diffstat (limited to 'src/gpio.ts')
| -rw-r--r-- | src/gpio.ts | 30 |
1 files changed, 28 insertions, 2 deletions
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); |
