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.

PortMCP23S17.h 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef PORTMCP23S17_H
  2. #define PORTMCP23S17_H
  3. #include <Arduino.h>
  4. #include <inttypes.h>
  5. #include <SPI.h>
  6. #include <PortIOE.h>
  7. #include <PortInterface.h>
  8. /*
  9. readPins are connected to matrix col
  10. write pin is connected to matrix Row (strobe pin) or LED.
  11. Slave Select is hardcoded to Arduino Pin 10.
  12. Arduino Pin 10 avoids the speed penalty of digitalWrite.
  13. Instantiation
  14. ------------
  15. readPins parameter is configures port's pins.
  16. Example instantiation:
  17. const uint8_t PortIOE::DEVICE_ADDR = 0x20; //MCP23S17 address, all 3 ADDR pins are grounded
  18. PortIOE port_B(1);
  19. Port_MCP23S17 portWrite_B(port_B, 0); //all pins are set to output for strobes and LEDs
  20. PortIOE port_A(0);
  21. Port_MCP23S17 portRead_A(port_A, 1<<0 | 1<<1 ); //pins 0,1 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 PortMCP23S17 : public PortInterface
  31. {
  32. private:
  33. PortIOE& port;
  34. const uint8_t readPins; //bits, IODIR 0=output, 1=input
  35. uint8_t transfer(const uint8_t command, const uint8_t registerAddr, const uint8_t data);
  36. public:
  37. PortMCP23S17(PortIOE& port, const uint8_t readPins) : port(port), readPins(readPins) {}
  38. void beginProtocol();
  39. void begin(const uint8_t strobeOn);
  40. virtual void write(const uint8_t pin, const bool logicLevel);
  41. virtual uint8_t read();
  42. };
  43. #endif