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_PCA9655E.cpp 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "Port_PCA9655E.h"
  2. /* beginProtocol() is called from Scanner_IOE::begin(). Initiates I2C bus.
  3. PCA9655E supports I2C SCL Clock Frequencies: 100 kHz, 400 kHz, 1000 kHz (Datasheet page 1 & 6)
  4. The electrical limitation to bus speed is bus capacitance and the length of the wires involved.
  5. Longer wires require lower clock speeds.
  6. http://playground.arduino.cc/Main/WireLibraryDetailedReference > Wire.setclock()
  7. */
  8. void Port_PCA9655E::beginProtocol()
  9. {
  10. Wire.begin(); //initiate I2C bus to 100 kHz
  11. //Wire.setClock(400000L); //set I2C bus to 400 kHz (have not tested 400 kHz)
  12. }
  13. /* begin() is called from Scanner_IOE::begin().
  14. Configures read pins to input.
  15. activeState is not used because PCA9655E has no internal pull-up resistors.
  16. */
  17. void Port_PCA9655E::begin(const uint8_t activeState)
  18. {
  19. Wire.beginTransmission(deviceAddr);
  20. Wire.write(portNum + 6); //configure direction
  21. Wire.write(readPins); //0=output (for strobe and LED), 1=input (for read)
  22. Wire.endTransmission();
  23. }
  24. /* writeLow() sets pin output LOW.
  25. pin is bit pattern, where pin being set is 1.
  26. */
  27. void Port_PCA9655E::writeLow(const uint8_t pin)
  28. {
  29. outputVal &= ~pin; //set pin output to low
  30. Wire.beginTransmission(deviceAddr);
  31. Wire.write(portNum + 2); //output Byte command
  32. Wire.write(outputVal);
  33. Wire.endTransmission();
  34. }
  35. /* writeHigh() sets pin output HIGH.
  36. pin is bit pattern, where pin being set is 1.
  37. */
  38. void Port_PCA9655E::writeHigh(const uint8_t pin)
  39. {
  40. outputVal |= pin; //set pin output to high
  41. Wire.beginTransmission(deviceAddr);
  42. Wire.write(portNum + 2); //output Byte command
  43. Wire.write(outputVal);
  44. Wire.endTransmission();
  45. }
  46. /* read() returns portState.
  47. Only portState bits of readPins are valid.
  48. */
  49. uint8_t Port_PCA9655E::read()
  50. {
  51. Wire.beginTransmission(deviceAddr);
  52. Wire.write(portNum); //input byte command
  53. Wire.endTransmission(false); //PCA9655E needs false to send a restart
  54. Wire.requestFrom(deviceAddr, 1u); //request one byte from input port
  55. return Wire.read();
  56. }