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.

PortWrite_MCP23S17.cpp 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "PortWrite_MCP23S17.h"
  2. /* writePort() sets registerAddr to data.
  3. */
  4. void PortWrite_MCP23S17::writePort(const uint8_t registerAddr, const uint8_t data)
  5. {
  6. digitalWrite(SS, LOW); //enable Slave Select
  7. SPI.transfer(port.DEVICE_ADDR << 1); //write command
  8. SPI.transfer(registerAddr); //register address to write data to
  9. SPI.transfer(data); //data
  10. digitalWrite(SS, HIGH); //disable Slave Select
  11. }
  12. /* begin() is called from Scanner_IOE::begin(). Initiates SPI bus and configures write pins.
  13. PortRead_MCP23S17 and PortWrite_MCP23S17 should be on seperate ports on the same MCP23S17.
  14. Output pins can be used for strobe pins and LEDs.
  15. */
  16. void PortWrite_MCP23S17::begin()
  17. {
  18. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  19. digitalWrite(SS, HIGH); //disable Slave Select
  20. SPI.begin();
  21. SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //control SPI bus todo is slow clock needed?
  22. //SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device.
  23. writePort(port.num, 0); //configure port direction (port.num) to output (0)
  24. }
  25. /*
  26. strobePin is bitwise, where pin being strobed is 1.
  27. pinLogicLevel is HIGH or LOW.
  28. port.outputVal can be shared by LEDs.
  29. The function does not reset the other pins so that they can be used for LEDs.
  30. */
  31. void PortWrite_MCP23S17::write(const uint8_t strobePin, const bool pinLogicLevel)
  32. {
  33. if (pinLogicLevel == LOW)
  34. {
  35. port.outputVal &= ~strobePin; //set strobePin output to low
  36. }
  37. else
  38. {
  39. port.outputVal |= strobePin; //set strobePin output to high
  40. }
  41. writePort(port.num + 0x12, port.outputVal); //set GPIO port pins for strobe and LEDs
  42. }