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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /* Port_MCP23S17
  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. slaveSelect is Arduino-pin number connected to pin 11 (CS a.k.a. SS).
  11. Instantiation
  12. ------------
  13. MCP23S17 datasheet identifies ports by letters, while class Port_MCP23S17 uses portNum
  14. for port A, use portNum=0
  15. for port B, use portNum=1
  16. readPins parameter configures port's pins.
  17. Example instantiation:
  18. const uint8_t IOE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded
  19. Port_MCP23S17 portB(IOE_ADDR, 1, 0); //all pins are set to output for strobes and LEDs
  20. Port_MCP23S17 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //pin 0 and pin 1 are set to input for reading,
  21. //remaining pins can be used for LEDs
  22. Diode orientation
  23. ----------------
  24. Diode orientation is explained in keybrd_library_user_guide.md > Diode orientation
  25. MCP23S17 data sheet
  26. ------------------
  27. http://www.onsemi.com/pub_link/Collateral/MCP23S17-D.PDF
  28. */
  29. class Port_MCP23S17 : public PortInterface
  30. {
  31. private:
  32. const uint8_t slaveSelect; //controller-pin number
  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 slaveSelect, const uint8_t deviceAddr, const uint8_t portNum, const uint8_t readPins)
  40. : slaveSelect(slaveSelect), deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
  41. void beginProtocol();
  42. void begin(const uint8_t activeState);
  43. virtual void writeLow(const uint8_t pin);
  44. virtual void writeHigh(const uint8_t pin);
  45. virtual uint8_t read();
  46. };
  47. #endif