|
1 | 1 | import { CPU } from './cpu'; |
2 | | -import { AVRIOPort, portBConfig } from './gpio'; |
| 2 | +import { AVRIOPort, portBConfig, PinState } from './gpio'; |
3 | 3 |
|
4 | 4 | describe('GPIO', () => { |
5 | 5 | it('should invoke the listeners when the port is written to', () => { |
@@ -37,4 +37,36 @@ describe('GPIO', () => { |
37 | 37 | expect(listener).not.toHaveBeenCalled(); |
38 | 38 | }); |
39 | 39 | }); |
| 40 | + |
| 41 | + describe('pinState', () => { |
| 42 | + it('should return PinState.High when the pin set to output and HIGH', () => { |
| 43 | + const cpu = new CPU(new Uint16Array(1024)); |
| 44 | + const port = new AVRIOPort(cpu, portBConfig); |
| 45 | + cpu.writeData(0x24, 0x1); // DDRB <- 0x1 |
| 46 | + cpu.writeData(0x25, 0x1); // PORTB <- 0x1 |
| 47 | + expect(port.pinState(0)).toEqual(PinState.High); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return PinState.Low when the pin set to output and LOW', () => { |
| 51 | + const cpu = new CPU(new Uint16Array(1024)); |
| 52 | + const port = new AVRIOPort(cpu, portBConfig); |
| 53 | + cpu.writeData(0x24, 0x8); // DDRB <- 0x8 |
| 54 | + cpu.writeData(0x25, 0xf7); // PORTB <- 0xF7 (~8) |
| 55 | + expect(port.pinState(3)).toEqual(PinState.Low); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return PinState.Input by default (reset state)', () => { |
| 59 | + const cpu = new CPU(new Uint16Array(1024)); |
| 60 | + const port = new AVRIOPort(cpu, portBConfig); |
| 61 | + expect(port.pinState(1)).toEqual(PinState.Input); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return PinState.InputPullUp when the pin is set to input with pullup', () => { |
| 65 | + const cpu = new CPU(new Uint16Array(1024)); |
| 66 | + const port = new AVRIOPort(cpu, portBConfig); |
| 67 | + cpu.writeData(0x24, 0); // DDRB <- 0 |
| 68 | + cpu.writeData(0x25, 0x2); // PORTB <- 0x2 |
| 69 | + expect(port.pinState(1)).toEqual(PinState.InputPullUp); |
| 70 | + }); |
| 71 | + }); |
40 | 72 | }); |
0 commit comments