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_MCP23018.h 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef PORT_MCP23018_H
  2. #define PORT_MCP23018_H
  3. #include <Arduino.h>
  4. #include <inttypes.h>
  5. #include <Wire.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. MCP23018 has open-drain outputs (open-drain can only sink current). If LEDs are used, connect:
  11. LED anodes (the longer lead) to power
  12. LED cathodes (the shorter lead) to GPIO pin
  13. Instantiation
  14. ------------
  15. Example instantiation:
  16. const uint8_t IOE_ADDR = 0x20; //MCP23018 address pin grounded
  17. Port_MCP23018 portB(IOE_ADDR, 1, 0); //all pins are set to output for strobes and LEDs
  18. Port_MCP23018 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //pin 0 and pin 1 are set to input for reading,
  19. //remaining pins can be used for LEDs
  20. Diode orientation
  21. ----------------
  22. Diode orientation is explained in keybrd_library_user_guide.md > Diode orientation
  23. MCP23018 data sheet
  24. ----------------
  25. http://ww1.microchip.com/downloads/en/DeviceDoc/22103a.pdf
  26. */
  27. class Port_MCP23018 : public PortInterface
  28. {
  29. private:
  30. const uint8_t deviceAddr;
  31. const uint8_t portNum; //port identification number, 0=A, 1=B
  32. uint8_t outputVal; //bit pattern for strobe and LEDs
  33. const uint8_t readPins; //bit pattern, IODIR 0=output, 1=input
  34. public:
  35. Port_MCP23018(const uint8_t deviceAddr, const uint8_t portNum, const uint8_t readPins)
  36. : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
  37. void beginProtocol();
  38. void begin(const uint8_t strobeOn);
  39. virtual void setLow(const uint8_t pin);
  40. virtual void setHigh(const uint8_t pin);
  41. virtual uint8_t read();
  42. };
  43. #endif