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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. Port_MCP23018 can only be active low (Scanner_IOE::activeState = LOW).
  11. Open-drain active high would not work because pull down resistors have no effect on sink.
  12. https://en.wikipedia.org/wiki/Open_collector
  13. Use LED_PortOpenDrain class for indicator LEDs.
  14. Instantiation
  15. ------------
  16. Example instantiation:
  17. const uint8_t IOE_ADDR = 0x20; //MCP23018 address pin grounded
  18. Port_MCP23018 portB(IOE_ADDR, 1, 0); //all pins are set to output for strobes and LEDs
  19. Port_MCP23018 portA(IOE_ADDR, 0, 1<<0 | 1<<1 ); //pin 0 and pin 1 are set to input for reading,
  20. //remaining pins can be used for LEDs
  21. Diode orientation
  22. ----------------
  23. Diode orientation is explained in keybrd_library_user_guide.md > Diode orientation
  24. MCP23018 data sheet
  25. ----------------
  26. http://ww1.microchip.com/downloads/en/DeviceDoc/22103a.pdf
  27. */
  28. class Port_MCP23018 : public PortInterface
  29. {
  30. private:
  31. const uint8_t deviceAddr;
  32. const uint8_t portNum; //port identification number, 0=A, 1=B
  33. uint8_t outputVal; //bit pattern for strobe and LEDs
  34. const uint8_t readPins; //bit pattern, IODIR 0=output, 1=input
  35. public:
  36. Port_MCP23018(const uint8_t deviceAddr, const uint8_t portNum, const uint8_t readPins)
  37. : deviceAddr(deviceAddr), portNum(portNum), outputVal(0), readPins(readPins) {}
  38. void beginProtocol();
  39. void begin(const uint8_t activeState);
  40. virtual void writeLow(const uint8_t pin);
  41. virtual void writeHigh(const uint8_t pin);
  42. virtual uint8_t read();
  43. };
  44. #endif