keybrd library is an open source library for creating custom-keyboard firmware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

Port_MCP23S17.h 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef PORT_MCP23S17_H
  2. #define PORT_MCP23S17_H
  3. #include <Arduino.h>
  4. #include <inttypes.h>
  5. #include <SPI.h>
  6. #include <PortInterface.h>
  7. /*
  8. write pins are connected to matrix Row (strobe pin) or LED.
  9. readPins are connected to matrix column to read which keys are pressed.
  10. Slave Select is hardcoded to Arduino Pin 10.
  11. Arduino Pin 10 avoids the speed penalty of digitalWrite.
  12. Instantiation
  13. ------------
  14. MCP23S17 datasheet identifies ports by letters, while class Port_MCP23S17 uses portNum
  15. for port A, use portNum=0
  16. for port B, use portNum=1
  17. readPins parameter configures port's pins.
  18. Example instantiation:
  19. const uint8_t IOE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded
  20. Port_MCP23S17 portB(IOE_ADDR, 1, 0); //all pins are set to output for strobes and LEDs
  21. Port_MCP23S17 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //first two pins are set to input for reading,
  22. //remaining pins can be used for LEDs
  23. Diode orientation
  24. ----------------
  25. Diode orientation is explained in keybrd_library_user_guide.md > Diode orientation
  26. MCP23S17 data sheet
  27. ------------------
  28. http://www.onsemi.com/pub_link/Collateral/MCP23S17-D.PDF
  29. */
  30. class Port_MCP23S17 : public PortInterface
  31. {
  32. private:
  33. const uint8_t deviceAddr;
  34. const uint8_t portNum; //port identification number
  35. uint8_t outputVal; //bit pattern for strobe and LEDs
  36. const uint8_t readPins; //bit pattern, IODIR 0=output, 1=input
  37. uint8_t transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data);
  38. public:
  39. Port_MCP23S17(const uint8_t deviceAddr, const uint8_t portNum, const uint8_t readPins)
  40. : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
  41. void beginProtocol();
  42. void begin(const uint8_t strobeOn);
  43. virtual void write(const uint8_t pin, const bool logicLevel);
  44. virtual uint8_t read();
  45. };
  46. #endif