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.

ColPort_MCP23018.cpp 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "ColPort_MCP23018.h"
  2. /*
  3. configures column port's IODIR, GPIO, and GPPU.
  4. */
  5. ColPort_MCP23018::ColPort_MCP23018(IOExpanderPort& port, const uint8_t colPins)
  6. : ColPort(colPins), port(port), IODIR(port.num), GPIO(port.num + 0x12), GPPU(port.num + 0x0C)
  7. {}
  8. void ColPort_MCP23018::begin(uint8_t activeHigh)
  9. {
  10. Wire.beginTransmission(port.ADDR);
  11. Wire.write(IODIR);
  12. Wire.write(colPins); //0=configure as output (for LED), 1=configure as input (for read)
  13. Wire.endTransmission();
  14. Wire.beginTransmission(port.ADDR);
  15. Wire.write(GPPU);
  16. if (activeHigh)
  17. {
  18. Wire.write(0); //0=pull-up disabled for activeHigh //todo not tested yet
  19. }
  20. else
  21. {
  22. Wire.write(colPins); //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
  23. }
  24. Wire.endTransmission();
  25. }
  26. /*
  27. Saves all port-pin values to portState.
  28. */
  29. void ColPort_MCP23018::read()
  30. {
  31. Wire.beginTransmission(port.ADDR);
  32. Wire.write(GPIO); //GPIO immediately before requestFrom
  33. Wire.endTransmission();
  34. Wire.requestFrom(port.ADDR, 1u); //request one byte from input port
  35. portState = Wire.read();
  36. }