MCP23S17 Library
 All Classes Functions Pages
MCP23S17.h
1 /*
2  * Copyright (c) , Majenko Technologies
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of Majenko Technologies nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 #ifndef _MCP23S17_H
33 #define _MCP23S17_H
34 
35 #if (ARDUINO >= 100)
36 # include <Arduino.h>
37 #else
38 # include <WProgram.h>
39 #endif
40 
41 #ifdef __PIC32MX__
42 #include <DSPI.h>
43 #else
44 #include <SPI.h>
45 #endif
46 
47 class MCP23S17 {
48  private:
49 #ifdef __PIC32MX__
50  DSPI *_spi;
51 #else
52  SPIClass *_spi;
53 #endif
54  uint8_t _cs;
55  uint8_t _addr;
57  uint8_t _reg[22];
59  enum {
60  IODIRA, IODIRB,
61  IPOLA, IPOLB,
62  GPINTENA, GPINTENB,
63  DEFVALA, DEFVALB,
64  INTCONA, INTCONB,
65  IOCONA, IOCONB,
66  GPPUA, GPPUB,
67  INTFA, INTFB,
68  INTCAPA, INTCAPB,
69  GPIOA, GPIOB,
70  OLATA, OLATB
71  };
72 
73  void readRegister(uint8_t addr);
74  void writeRegister(uint8_t addr);
75  void readAll();
76  void writeAll();
77 
78  public:
79 #ifdef __PIC32MX__
80  MCP23S17(DSPI *spi, uint8_t cs, uint8_t addr);
81 #else
82  MCP23S17(SPIClass *spi, uint8_t cs, uint8_t addr);
83 #endif
84  void begin();
85  void pinMode(uint8_t pin, uint8_t mode);
86  void digitalWrite(uint8_t pin, uint8_t value);
87  uint8_t digitalRead(uint8_t pin);
88 
89  uint8_t readPort(uint8_t port);
90  uint16_t readPort();
91  void writePort(uint8_t port, uint8_t val);
92  void writePort(uint16_t val);
93  void enableInterrupt(uint8_t pin, uint8_t type);
94  void disableInterrupt(uint8_t pin);
95  void setMirror(boolean m);
96  uint16_t getInterruptPins();
97  uint16_t getInterruptValue();
98  void setInterruptLevel(uint8_t level);
99  void setInterruptOD(boolean openDrain);
100 
101 };
102 #endif