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.

PortRead_MCP23S17.cpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "PortRead_MCP23S17.h"
  2. /*
  3. begin() is called from Scanner_IOE::begin().
  4. */
  5. void PortRead_MCP23S17::begin(const uint8_t strobeOn)
  6. {
  7. if (strobeOn == LOW) //if active low
  8. {
  9. pullUp = readPins;
  10. }
  11. else
  12. {
  13. pullUp = 0;
  14. }
  15. Keyboard.print("\npullUp=");//todo
  16. Keyboard.print(pullUp);
  17. /*
  18. //todo these 4 lines are duplicated in PortWrite_MCP23S17::begin(), which is called first
  19. pinMode(SS, OUTPUT); //configure controller's Slave Select pin to output
  20. digitalWrite(SS, HIGH); //disable Slave Select
  21. SPI.begin();
  22. SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //control SPI bus todo is slow clock needed?
  23. //SPI.endTransaction() not called to release SPI bus because keyboard only has one SPI device.
  24. */
  25. digitalWrite(SS, LOW); //enable Slave Select
  26. SPI.transfer(port.DEVICE_ADDR << 1); //write command
  27. SPI.transfer(port.num); //configure IODIR
  28. SPI.transfer(readPins); //0=output (for LED), 1=input (for read)
  29. digitalWrite(SS, HIGH); //enable Slave Select
  30. digitalWrite(SS, LOW); //disable Slave Select
  31. SPI.transfer(port.DEVICE_ADDR << 1); //write command
  32. SPI.transfer(port.num + 0x0C); //configure GPPU
  33. SPI.transfer(pullUp); //0=pull-up disabled (for LED), 1=pull-up enabled (for read)
  34. digitalWrite(SS, HIGH); //disable Slave Select
  35. }
  36. /*
  37. read() returns portState.
  38. */
  39. uint8_t PortRead_MCP23S17::read()
  40. {
  41. uint8_t portState; //bit wise
  42. digitalWrite(SS, LOW); //enable Slave Select
  43. SPI.transfer(port.DEVICE_ADDR << 1 | 1); //read command
  44. SPI.transfer(port.num + 0x12); //GPIO register address to read data from
  45. portState = SPI.transfer(0); //save the data (0 is dummy data to send)
  46. digitalWrite(SS, HIGH); //disable Slave Select
  47. return portState;
  48. }