2424
2525import SMBus from "pins/smbus" ;
2626
27- // Default State: MCP23008
28- const INPUTS_8 = 0b11111111 ;
29- const PULLUPS_8 = 0b00000000 ;
30-
31- // Default State: MCP23017
32- const INPUTS_16 = 0b1111111111111111 ;
33- const PULLUPS_16 = 0b0000000000000000 ;
34-
3527// Modes (Ref: modules/pins/digital)
3628const INPUT = 0 ;
3729const INPUT_PULLUP = 1 ;
@@ -41,32 +33,22 @@ class Expander extends SMBus {
4133 constructor ( dictionary = { address : 0x20 } ) {
4234 super ( dictionary ) ;
4335
44- this . offset = ( this . length >> 3 ) - 1 ;
45-
4636 const {
4737 // User specific state initialization settings
48- inputs = this . offset ? INPUTS_16 : INPUTS_8 ,
49- pullups = this . offset ? PULLUPS_16 : PULLUPS_8 ,
38+ inputs = this . INPUTS ,
39+ pullups = this . PULLUPS ,
5040 } = dictionary ;
5141
5242 // If the value of inputs does not match the default,
5343 // then set the user defined inputs configuration
54- if ( ( inputs !== ( this . offset ? INPUTS_16 : INPUTS_8 ) ) ) {
55- this . writeByte ( this . IODIR , inputs ) ;
56-
57- if ( this . offset ) {
58- this . writeByte ( this . IODIR + this . offset , inputs >> 8 ) ;
59- }
44+ if ( ( inputs !== this . INPUTS ) ) {
45+ this . writeSize ( this . IODIR , inputs ) ;
6046 }
6147
6248 // If the value of pullups does not match the default,
6349 // then set the user defined pullups configuration
64- if ( ( pullups !== ( this . offset ? PULLUPS_16 : PULLUPS_8 ) ) ) {
65- this . writeByte ( this . GPPU , pullups ) ;
66-
67- if ( this . offset ) {
68- this . writeByte ( this . GPPU + this . offset , pullups >> 8 ) ;
69- }
50+ if ( ( pullups !== this . PULLUPS ) ) {
51+ this . writeSize ( this . GPPU , pullups ) ;
7052 }
7153
7254 for ( let pin = 0 ; pin < this . length ; pin ++ ) {
@@ -77,62 +59,32 @@ class Expander extends SMBus {
7759 }
7860
7961 write ( state ) {
80- if ( this . offset ) {
81- // Read IODIR state
82- let iodir = this . readWord ( this . IODIR ) ;
83-
84- // Set IODIR state to OUTPUT
85- this . writeWord ( this . IODIR , 0x0000 ) ;
86-
87- // Write GPIO
88- this . writeWord ( this . GPIO , state ) ;
62+ // Read IODIR state
63+ let iodir = this . readSize ( this . IODIR ) ;
8964
90- // Restore previous IODIR state
91- this . writeWord ( this . IODIR , iodir ) ;
92- } else {
93- // Read IODIR state
94- let iodir = this . readByte ( this . IODIR ) ;
95-
96- // Set IODIR state to OUTPUT
97- this . writeByte ( this . IODIR , 0x00 ) ;
65+ // Set IODIR state to OUTPUT
66+ this . writeSize ( this . IODIR , 0x0000 ) ;
9867
99- // Write GPIO
100- this . writeByte ( this . GPIO , state & 0xFF ) ;
68+ // Write GPIO
69+ this . writeSize ( this . GPIO , state ) ;
10170
102- // Restore previous IODIR state
103- this . writeByte ( this . IODIR , iodir ) ;
104- }
71+ // Restore previous IODIR state
72+ this . writeSize ( this . IODIR , iodir ) ;
10573 }
10674 read ( ) {
107- if ( this . offset ) {
108- // Read IODIR state
109- let iodir = this . readWord ( this . IODIR ) ;
75+ // Read IODIR state
76+ let iodir = this . readSize ( this . IODIR ) ;
11077
111- // Set IODIR state to INPUT
112- this . writeWord ( this . IODIR , 0xFFFF ) ;
78+ // Set IODIR state to INPUT
79+ this . writeSize ( this . IODIR , 0xFFFF ) ;
11380
114- // Read GPIO
115- let gpio = this . readWord ( this . GPIO ) ;
81+ // Read GPIO
82+ let gpio = this . readSize ( this . GPIO ) ;
11683
117- // Restore previous IODIR state
118- this . writeWord ( this . IODIR , iodir ) ;
119-
120- return gpio ;
121- } else {
122- // Read IODIR state
123- let iodir = this . readByte ( this . IODIR ) ;
84+ // Restore previous IODIR state
85+ this . writeSize ( this . IODIR , iodir ) ;
12486
125- // Set IODIR state to INPUT
126- this . writeByte ( this . IODIR , 0xFF ) ;
127-
128- // Read GPIO
129- let gpio = this . readByte ( this . GPIO ) ;
130-
131- // Restore previous IODIR state
132- this . writeByte ( this . IODIR , iodir ) ;
133-
134- return gpio ;
135- }
87+ return gpio ;
13688 }
13789}
13890
@@ -224,16 +176,24 @@ class Pin {
224176
225177class MCP23008 extends Expander { }
226178MCP23008 . prototype . length = 8 ;
179+ MCP23008 . prototype . INPUTS = 0b11111111 ;
180+ MCP23008 . prototype . PULLUPS = 0b00000000 ;
227181MCP23008 . prototype . IODIR = 0x00 ;
228182MCP23008 . prototype . GPPU = 0x06 ;
229183MCP23008 . prototype . GPIO = 0x09 ;
184+ MCP23008 . prototype . writeSize = SMBus . prototype . writeByte ;
185+ MCP23008 . prototype . readSize = SMBus . prototype . readByte ;
230186Object . freeze ( MCP23008 . prototype ) ;
231187
232188class MCP23017 extends Expander { }
233189MCP23017 . prototype . length = 16 ;
190+ MCP23017 . prototype . INPUTS = 0b1111111111111111 ;
191+ MCP23017 . prototype . PULLUPS = 0b0000000000000000 ;
234192MCP23017 . prototype . IODIR = 0x00 ;
235193MCP23017 . prototype . GPPU = 0x0C ;
236194MCP23017 . prototype . GPIO = 0x12 ;
195+ MCP23017 . prototype . writeSize = SMBus . prototype . writeWord ;
196+ MCP23017 . prototype . readSize = SMBus . prototype . readWord ;
237197Object . freeze ( MCP23017 . prototype ) ;
238198
239199
0 commit comments