Skip to content

Commit 9906467

Browse files
rwaldronmkellner
authored andcommitted
modules/drivers: MCP230XX refactor/redesign #23
1 parent 756ba0f commit 9906467

20 files changed

+637
-880
lines changed
27.3 KB
Binary file not shown.
274 KB
Loading
41 KB
Binary file not shown.
316 KB
Loading
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
# MCP230XX
2+
3+
Copyright 2017 Moddable Tech, Inc.
4+
5+
Revised: Dec 19, 2017
6+
7+
The [MCP23008](http://www.microchip.com/wwwproducts/en/MCP23008) device provides 8-bit, general purpose, parallel I/O expansion for I2C bus applications. (Description from MCP23008 product page)
8+
9+
The [MCP23017](http://www.microchip.com/wwwproducts/en/MCP23017) device provides 16-bit, general purpose, parallel I/O expansion for I2C bus applications. (Description from MCP23017 product page)
10+
11+
12+
## module "MCP230XX"
13+
14+
The driver module "MCP230XX" exports the following:
15+
16+
```js
17+
export {
18+
MCP23008,
19+
MCP23017
20+
};
21+
```
22+
23+
24+
25+
## class MCP23008
26+
27+
The `MCP23008` class produces instances that represent a single MCP23008 IC on the I2C bus. The `MCP23008` class extends an internal `Expander` class, which extends the `SMBus` class. `Expander` is not exported.
28+
29+
Instance objects of `MCP23008` contain 8 `Pin` instance object entries.
30+
31+
32+
```js
33+
import Timer from "timer";
34+
import {MCP23008} from "MCP230XX";
35+
36+
37+
export default function() {
38+
let leds = new MCP23008(); // defaults to 0x20!
39+
let mask = 0x88;
40+
41+
Timer.repeat(() => {
42+
mask = ((mask << 1) | (mask >> 7)) & 0xFF;
43+
44+
for (let i = 0; i < 8; i++) {
45+
leds[i].write(mask & (1 << i) ? 1 : 0);
46+
}
47+
}, 50);
48+
}
49+
```
50+
51+
![](ESP8266-MCP23008-leds.png)
52+
53+
### Properties
54+
55+
| Property Name | Description | Read Only |
56+
|---------------| ----------- | ----------|
57+
| `length` | Number of pins in collection: `8` | Yes |
58+
| `offset` | Register offset: `0` | Yes |
59+
| `IODIR` | `IODIR` register: `0x00` | Yes |
60+
| `GPIO` | `GPIO` register: `0x06` | Yes |
61+
| `GPPU` | `GPPU` register: `0x09` | Yes |
62+
| 0-8 | `Pin` instances | Yes |
63+
64+
65+
### Methods
66+
67+
#### constructor({ [address], [hz], [sda], [scl], [inputs], [pullups] })
68+
69+
| Property | Type | Value/Description | Default | Required |
70+
|----------|--------|----------------------|---------|----------|
71+
| `address` | Number | The address of the I2C device | `0x20` | no |
72+
| `hz` | Number | The clock speed of the I2C device. | 100kHz | no |
73+
| `sda` | Number | The I2C sda (data) pin. | 4 | no |
74+
| `scl` | Number | The I2C scl (clock) pin. | 5 | no |
75+
| `inputs` | Byte | A byte representing the input/output initialization state of the 8 GPIO pins. `1` for input, `0` for output | `0b11111111` | no |
76+
| pullups | Byte | A byte representing the pullup initialization state of the 8 GPIO pins. `1` for pullup, `0` for default | `0b00000000` | no |
77+
78+
#### bankWrite(byte)
79+
80+
The method will temporarily set the _mode/direction_ of all pins to _output_ mode and write all pins at once.
81+
82+
```js
83+
let expander = new MCP23008(); // defaults to 0x20!
84+
expander.bankWrite(0b11111111); // Set all pins to 1
85+
```
86+
87+
#### bankRead() -> byte
88+
89+
The method will temporarily set the _mode/direction_ of all pins to _input_ mode and read all pins at once.
90+
91+
```js
92+
let expander = new MCP23008(); // defaults to 0x20!
93+
trace(`${expander.bankRead()}\n`);
94+
```
95+
96+
97+
## class MCP23017
98+
99+
The `MCP23017` class produces instances that represent a single MCP23017 IC on the I2C bus. The `MCP23017` class extends an internal `Expander` class, which extends the `SMBus` class.
100+
101+
Instance objects of `MCP23017` contain 16 `Pin` instance object entries.
102+
103+
104+
```js
105+
import Timer from "timer";
106+
import {MCP23017} from "MCP230XX";
107+
108+
109+
export default function() {
110+
let leds = new MCP23017(); // defaults to 0x20!
111+
let mask = 0x8888;
112+
113+
Timer.repeat(() => {
114+
mask = ((mask << 1) | (mask >> 15)) & 0xFFFF;
115+
116+
for (let i = 0; i < 16; i++) {
117+
leds[i].write(mask & (1 << i) ? 1 : 0);
118+
}
119+
}, 50);
120+
}
121+
```
122+
123+
![](ESP8266-MCP23017-leds.png)
124+
125+
### Properties
126+
127+
| Property Name | Description | Read Only |
128+
|---------------| ----------- | ----------|
129+
| `length` | Number of pins in collection: `16` | Yes |
130+
| `offset` | Register offset: `1` | Yes |
131+
| `IODIR` | `IODIR` register: `0x00` | Yes |
132+
| `GPIO` | `GPIO` register: `0x0C` | Yes |
133+
| `GPPU` | `GPPU` register: `0x12` | Yes |
134+
| 0-16 | `Pin` instances | Yes |
135+
136+
137+
### Methods
138+
139+
#### constructor({ [address], [hz], [sda], [scl], [inputs], [pullups] })
140+
141+
| Property | Type | Value/Description | Default | Required |
142+
|----------|--------|----------------------|---------|----------|
143+
| `address` | Number | The address of the I2C device | `0x20` | no |
144+
| `hz` | Number | The clock speed of the I2C device. | 100kHz | no |
145+
| `sda` | Number | The I2C sda (data) pin. | 4 | no |
146+
| `scl` | Number | The I2C scl (clock) pin. | 5 | no |
147+
| `inputs` | Word | A byte representing the input/output initialization state of the 8 GPIO pins. `1` for input, `0` for output | `0b1111111111111111` | no |
148+
| `pullups` | Word | A byte representing the pullup initialization state of the 16 GPIO pins. `1` for pullup, `0` for default | `0b0000000000000000` | no |
149+
150+
#### bankWrite(word)
151+
152+
The method will temporarily set the _mode/direction_ of all pins to _output_ mode and write all pins at once.
153+
154+
```js
155+
let expander = new MCP23017(); // defaults to 0x20!
156+
expander.bankWrite(0b1111111111111111); // Set all pins to 1
157+
```
158+
159+
#### bankRead() -> word
160+
161+
The method will temporarily set the _mode/direction_ of all pins to _input_ mode and read all pins at once.
162+
163+
```js
164+
let expander = new MCP23017(); // defaults to 0x20!
165+
trace(`${expander.bankRead()}\n`);
166+
```
167+
168+
## class Pin
169+
170+
The `Pin` class represents a single pin within a `MCP23008` instance object. `Pin` is not exported.
171+
172+
173+
```js
174+
import Timer from "timer";
175+
import { MCP23008 } from "MCP230XX";
176+
177+
178+
export default function() {
179+
const leds = new MCP23008({
180+
inputs: 0b00000000
181+
});
182+
183+
leds[0].write(1);
184+
leds[1].write(0);
185+
leds[2].write(1);
186+
leds[3].write(0);
187+
leds[4].write(1);
188+
leds[5].write(0);
189+
leds[6].write(1);
190+
leds[7].write(0);
191+
}
192+
```
193+
194+
![](ESP8266-MCP23008-leds.png)
195+
196+
## Properties
197+
198+
| Property Name | Description | Read Only |
199+
|---------------| ----------- | ----------|
200+
| `pin` | The GPIO pin number | Yes |
201+
| `expander` | The instance of `Expander` that this `Pin` belongs to | Yes |
202+
203+
204+
## Methods
205+
206+
#### constructor({ pin, expander })
207+
208+
| Property | Type | Value/Description | Default | Required |
209+
|----------|--------|----------------------|---------|----------|
210+
| pin | Number | The GPIO pin number | n/a | yes |
211+
| expander | Number | The instance of `Expander` that created this `Pin` instance | n/a | yes |
212+
213+
#### mode(mode)
214+
215+
Set the _mode/direction_ of the pin by object.
216+
217+
#### read()
218+
219+
Set the _mode/direction_ to _input_ and read the value of the pin object.
220+
221+
#### write(value)
222+
223+
Set the _mode/direction_ to _output_ and write the value to the pin object.
224+
225+
226+
227+
## Manifest Example
228+
229+
```
230+
{
231+
"include": [
232+
"$(MODDABLE)/examples/manifest_base.json",
233+
"$(MODULES)/drivers/mcp230/manifest.json"
234+
],
235+
"modules": {
236+
"*": [
237+
"./main",
238+
]
239+
},
240+
"platforms": {
241+
"esp": {
242+
"modules": {
243+
"*": "$(MODULES)/pins/i2c/esp/*",
244+
},
245+
},
246+
"esp32": {
247+
"modules": {
248+
"*": "$(MODULES)/pins/i2c/esp32/*",
249+
},
250+
},
251+
}
252+
}
253+
```
-253 KB
Binary file not shown.
37.2 KB
Binary file not shown.
493 KB
Loading

examples/drivers/mcp23008/main.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2016-2017 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK.
5-
*
5+
*
66
* This work is licensed under the
77
* Creative Commons Attribution 4.0 International License.
88
* To view a copy of this license, visit
@@ -13,15 +13,36 @@
1313
*/
1414

1515
import Timer from "timer";
16-
import MCP23008 from "mcp23008";
16+
import Digital from "pins/digital";
17+
import {MCP23008} from "MCP230XX";
1718

1819
export default function() {
20+
const leds = new MCP23008(/* defaults to 0x20! */);
21+
const io = new MCP23008({ address: 0x21 });
1922
let mask = 0x88;
2023

24+
// These are buttons.
25+
io[0].mode(Digital.Input);
26+
io[1].mode(Digital.Input);
27+
io[2].mode(Digital.Input);
28+
29+
// These are LEDs, writing to them will
30+
// set the direction to OUTPUT.
31+
io[3].write(0);
32+
io[4].write(0);
33+
io[5].write(0);
34+
2135
Timer.repeat(() => {
2236
mask = ((mask << 1) | (mask >> 7)) & 0xFF;
2337

24-
for (let i = 0; i < 8; i++)
25-
MCP23008.write(i, (mask & (1 << i)) ? 1 : 0);
38+
// Update the leds
39+
for (let i = 0; i < 8; i++) {
40+
leds[i].write(mask & (1 << i) ? 1 : 0);
41+
42+
// Read the buttons and update the outputs
43+
if (i < 3) {
44+
io[i + 3].write(io[i].read() ? 0 : 1);
45+
}
46+
}
2647
}, 50);
2748
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
{
2-
"include": "$(MODDABLE)/examples/manifest_base.json",
2+
"include": [
3+
"$(MODDABLE)/examples/manifest_base.json",
4+
"$(MODULES)/drivers/mcp230/manifest.json"
5+
],
36
"modules": {
47
"*": [
58
"./main",
6-
"$(MODULES)/drivers/mcp23008/*",
79
]
810
},
9-
"preload": [
10-
"mcp23008"
11-
],
1211
"platforms": {
1312
"esp": {
1413
"modules": {
15-
"pins/i2c": "$(MODULES)/pins/i2c/i2c",
16-
"*": [
17-
"$(MODULES)/pins/i2c/esp/*",
18-
],
14+
"*": "$(MODULES)/pins/i2c/esp/*",
15+
},
16+
},
17+
"esp32": {
18+
"modules": {
19+
"*": "$(MODULES)/pins/i2c/esp32/*",
1920
},
20-
"preload": [
21-
"pins/i2c",
22-
]
2321
},
2422
}
2523
}

0 commit comments

Comments
 (0)