@@ -32,138 +32,104 @@ const PULLUPS_8 = 0b00000000;
3232const INPUTS_16 = 0b1111111111111111 ;
3333const PULLUPS_16 = 0b0000000000000000 ;
3434
35- // Default Config (Both)
36- const ADDRESS = 0x20 ;
37- const HZ = 0x00 ;
38- const SDA = 0x04 ;
39- const SCL = 0x05 ;
40-
4135// Modes (Ref: modules/pins/digital)
4236const INPUT = 0 ;
4337const INPUT_PULLUP = 1 ;
4438const OUTPUT = 8 ;
4539
4640class Expander extends SMBus {
47- constructor ( dictionary ) {
41+ constructor ( dictionary = { address : 0x20 } ) {
42+ super ( dictionary ) ;
4843
49- const offset = ( dictionary . pins >> 3 ) - 1 ;
50- const {
51- address = ADDRESS ,
52- hz = HZ ,
53- sda = SDA ,
54- scl = SCL ,
44+ this . offset = ( this . length >> 3 ) - 1 ;
5545
46+ const {
5647 // User specific state initialization settings
57- inputs = offset ? INPUTS_16 : INPUTS_8 ,
58- pullups = offset ? PULLUPS_16 : PULLUPS_8 ,
59- pins,
60- reg,
61- reg : { IODIR , GPIO , GPPU }
62- } = dictionary || { } ;
63-
64- super ( { address, hz, sda, scl } ) ;
65-
66- // trace(`IODIR: 0x${IODIR.toString(16)}\n`);
67- // trace(`GPIO: 0x${GPIO.toString(16)}\n`);
68- // trace(`GPPU: 0x${GPPU.toString(16)}\n`);
48+ inputs = this . offset ? INPUTS_16 : INPUTS_8 ,
49+ pullups = this . offset ? PULLUPS_16 : PULLUPS_8 ,
50+ } = dictionary ;
6951
7052 // If the value of inputs does not match the default,
7153 // then set the user defined inputs configuration
72- if ( ( inputs !== ( offset ? INPUTS_16 : INPUTS_8 ) ) ) {
73- this . writeByte ( IODIR , inputs ) ;
54+ if ( ( inputs !== ( this . offset ? INPUTS_16 : INPUTS_8 ) ) ) {
55+ this . writeByte ( this . IODIR , inputs ) ;
7456
75- if ( offset ) {
76- this . writeByte ( IODIR + offset , inputs >> 8 ) ;
57+ if ( this . offset ) {
58+ this . writeByte ( this . IODIR + this . offset , inputs >> 8 ) ;
7759 }
7860 }
7961
8062 // If the value of pullups does not match the default,
8163 // then set the user defined pullups configuration
82- if ( ( pullups !== ( offset ? PULLUPS_16 : PULLUPS_8 ) ) ) {
83- this . writeByte ( GPPU , pullups ) ;
64+ if ( ( pullups !== ( this . offset ? PULLUPS_16 : PULLUPS_8 ) ) ) {
65+ this . writeByte ( this . GPPU , pullups ) ;
8466
85- if ( offset ) {
86- this . writeByte ( GPPU + offset , pullups >> 8 ) ;
67+ if ( this . offset ) {
68+ this . writeByte ( this . GPPU + this . offset , pullups >> 8 ) ;
8769 }
8870 }
8971
90- for ( let pin = 0 ; pin < pins ; pin ++ ) {
72+ for ( let pin = 0 ; pin < this . length ; pin ++ ) {
9173 this [ pin ] = new Pin ( { pin, expander : this } ) ;
9274 }
9375
94- this . length = pins ;
95- this . offset = offset ;
96- this . reg = Object . freeze ( reg ) ;
97-
9876 Object . freeze ( this ) ;
9977 }
10078
101- * [ Symbol . iterator ] ( ) {
102- let i = 0 ;
103- while ( i < this . length ) {
104- yield this [ i ] ;
105- i ++ ;
106- }
107- }
108-
10979 bankWrite ( state ) {
110- const { IODIR , GPIO } = this . reg ;
111-
11280 if ( this . offset ) {
11381 // Read IODIR state
114- let iodir = this . readWord ( IODIR ) ;
82+ let iodir = this . readWord ( this . IODIR ) ;
11583
11684 // Set IODIR state to OUTPUT
117- this . writeWord ( IODIR , 0x0000 ) ;
85+ this . writeWord ( this . IODIR , 0x0000 ) ;
11886
11987 // Write GPIO
120- this . writeWord ( GPIO , state ) ;
88+ this . writeWord ( this . GPIO , state ) ;
12189
12290 // Restore previous IODIR state
123- this . writeWord ( IODIR , iodir ) ;
91+ this . writeWord ( this . IODIR , iodir ) ;
12492 } else {
12593 // Read IODIR state
126- let iodir = this . readByte ( IODIR ) ;
94+ let iodir = this . readByte ( this . IODIR ) ;
12795
12896 // Set IODIR state to OUTPUT
129- this . writeByte ( IODIR , 0x00 ) ;
97+ this . writeByte ( this . IODIR , 0x00 ) ;
13098
13199 // Write GPIO
132- this . writeByte ( GPIO , state & 0xFF ) ;
100+ this . writeByte ( this . GPIO , state & 0xFF ) ;
133101
134102 // Restore previous IODIR state
135- this . writeByte ( IODIR , iodir ) ;
103+ this . writeByte ( this . IODIR , iodir ) ;
136104 }
137105 }
138106 bankRead ( ) {
139- const { IODIR , GPIO } = this . reg ;
140-
141107 if ( this . offset ) {
142108 // Read IODIR state
143- let iodir = this . readWord ( IODIR ) ;
109+ let iodir = this . readWord ( this . IODIR ) ;
144110
145111 // Set IODIR state to INPUT
146- this . writeWord ( IODIR , 0xFFFF ) ;
112+ this . writeWord ( this . IODIR , 0xFFFF ) ;
147113
148114 // Read GPIO
149- let gpio = this . readWord ( GPIO ) ;
115+ let gpio = this . readWord ( this . GPIO ) ;
150116
151117 // Restore previous IODIR state
152- this . writeWord ( IODIR , iodir ) ;
118+ this . writeWord ( this . IODIR , iodir ) ;
153119
154120 return gpio ;
155121 } else {
156122 // Read IODIR state
157- let iodir = this . readByte ( IODIR ) ;
123+ let iodir = this . readByte ( this . IODIR ) ;
158124
159125 // Set IODIR state to INPUT
160- this . writeByte ( IODIR , 0xFF ) ;
126+ this . writeByte ( this . IODIR , 0xFF ) ;
161127
162128 // Read GPIO
163- let gpio = this . readByte ( GPIO ) ;
129+ let gpio = this . readByte ( this . GPIO ) ;
164130
165131 // Restore previous IODIR state
166- this . writeByte ( IODIR , iodir ) ;
132+ this . writeByte ( this . IODIR , iodir ) ;
167133
168134 return gpio ;
169135 }
@@ -180,8 +146,8 @@ class Pin {
180146 const offset = this . pin >> 3 ;
181147 const pin = offset ? this . pin - 8 : this . pin ;
182148 const pinMask = 1 << pin ;
183- const IODIR = this . expander . reg . IODIR + offset ;
184- const GPPU = this . expander . reg . GPPU + offset ;
149+ const IODIR = this . expander . IODIR + offset ;
150+ const GPPU = this . expander . GPPU + offset ;
185151
186152 let iodir = this . expander . readByte ( IODIR ) ;
187153 let gppu = this . expander . readByte ( GPPU ) ;
@@ -214,8 +180,8 @@ class Pin {
214180 const offset = this . pin >> 3 ;
215181 const pin = offset ? this . pin - 8 : this . pin ;
216182 const pinMask = 1 << pin ;
217- const IODIR = this . expander . reg . IODIR + offset ;
218- const GPIO = this . expander . reg . GPIO + offset ;
183+ const IODIR = this . expander . IODIR + offset ;
184+ const GPIO = this . expander . GPIO + offset ;
219185
220186 let iodir = this . expander . readByte ( IODIR ) ;
221187
@@ -233,9 +199,8 @@ class Pin {
233199 const offset = this . pin >> 3 ;
234200 const pin = offset ? this . pin - 8 : this . pin ;
235201 const pinMask = 1 << pin ;
236- const IODIR = this . expander . reg . IODIR + offset ;
237- const GPIO = this . expander . reg . GPIO + offset ;
238-
202+ const IODIR = this . expander . IODIR + offset ;
203+ const GPIO = this . expander . GPIO + offset ;
239204
240205 let gpio = this . expander . readByte ( GPIO ) ;
241206 let iodir = this . expander . readByte ( IODIR ) ;
@@ -257,34 +222,19 @@ class Pin {
257222 }
258223}
259224
260- class MCP23008 extends Expander {
261- constructor ( dictionary = { } ) {
262- super (
263- Object . assign ( dictionary , {
264- pins : 8 ,
265- reg : {
266- IODIR : 0x00 ,
267- GPPU : 0x06 ,
268- GPIO : 0x09 ,
269- }
270- } )
271- ) ;
272- }
273- }
225+ class MCP23008 extends Expander { }
226+ MCP23008 . prototype . length = 8 ;
227+ MCP23008 . prototype . IODIR = 0x00 ;
228+ MCP23008 . prototype . GPPU = 0x06 ;
229+ MCP23008 . prototype . GPIO = 0x09 ;
230+ Object . freeze ( MCP23008 . prototype ) ;
231+
232+ class MCP23017 extends Expander { }
233+ MCP23017 . prototype . length = 16 ;
234+ MCP23017 . prototype . IODIR = 0x00 ;
235+ MCP23017 . prototype . GPPU = 0x0C ;
236+ MCP23017 . prototype . GPIO = 0x12 ;
237+ Object . freeze ( MCP23017 . prototype ) ;
274238
275- class MCP23017 extends Expander {
276- constructor ( dictionary = { } ) {
277- super (
278- Object . assign ( dictionary , {
279- pins : 16 ,
280- reg : {
281- IODIR : 0x00 ,
282- GPPU : 0x0C ,
283- GPIO : 0x12 ,
284- }
285- } )
286- ) ;
287- }
288- }
289239
290240export { MCP23008 , MCP23017 } ;
0 commit comments