Update of the bombatuino with a pro micro board with native usb midi support.
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.

118 lines
2.9 KiB

  1. #include <Wire.h>
  2. #include "Arduino.h"
  3. #include "bombatuino_INPUT_MCP23017.h"
  4. void INPUT_MCP23017::begin(uint8_t addr,CallbackFunction cbF) {
  5. Wire.begin();
  6. //check hardware address
  7. if (addr > 7)
  8. _addr = 7;
  9. else _addr = addr;
  10. _callbackFunction = cbF;
  11. //set all ports as inputs
  12. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  13. Wire.write((byte)MCP23017_IODIR_A); //PORT A
  14. Wire.write(0xFF);
  15. Wire.endTransmission();
  16. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  17. Wire.write(MCP23017_IODIR_B); //PORT B
  18. Wire.write(0xFF);
  19. Wire.endTransmission();
  20. //activate pullup resistors
  21. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  22. Wire.write(MCP23017_GPPU_A); //PORT A
  23. Wire.write(0xFF);
  24. Wire.endTransmission();
  25. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  26. Wire.write(MCP23017_GPPU_B); //PORT B
  27. Wire.write(0xFF);
  28. Wire.endTransmission();
  29. //inverse all inputs
  30. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  31. Wire.write((byte)MCP23017_IPOL_A); //PORT A
  32. Wire.write(0xFF);
  33. Wire.endTransmission();
  34. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  35. Wire.write(MCP23017_IPOL_B); //PORT B
  36. Wire.write(0xFF);
  37. Wire.endTransmission();
  38. //init start values
  39. uint8_t pin,bank;
  40. //read bank A
  41. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  42. Wire.write(MCP23017_GPIO_A);
  43. Wire.endTransmission();
  44. Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
  45. bank = Wire.read();
  46. for (pin=0; pin<8; pin++)
  47. _value[pin] = (bank >> pin) & 0x1;
  48. //read bank B
  49. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  50. Wire.write(MCP23017_GPIO_B);
  51. Wire.endTransmission();
  52. Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
  53. bank = Wire.read();
  54. for (pin=8; pin<16; pin++)
  55. _value[pin] = (bank >> (pin-8)) & 0x1;
  56. }
  57. void INPUT_MCP23017::loop() {
  58. uint8_t pin,bank;
  59. int value;
  60. //read bank A
  61. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  62. Wire.write(MCP23017_GPIO_A);
  63. Wire.endTransmission();
  64. Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
  65. bank = Wire.read();
  66. for (pin=0; pin<8; pin++) {
  67. value = (bank >> pin) & 0x1;
  68. if (_value[pin] != value) {
  69. _value[pin] = value;
  70. (*_callbackFunction)(_addr,pin,value);
  71. }
  72. }
  73. //read bank B
  74. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  75. Wire.write(MCP23017_GPIO_B);
  76. Wire.endTransmission();
  77. Wire.requestFrom(MCP23017_ADDRESS | _addr, 1);
  78. bank = Wire.read();
  79. for (pin=8; pin<16; pin++) {
  80. value = (bank >> (pin-8)) & 0x1;
  81. if (_value[pin] != value) {
  82. _value[pin] = value;
  83. (*_callbackFunction)(_addr,pin,value);
  84. }
  85. }
  86. }
  87. //maybe useful later
  88. int INPUT_MCP23017::getSpecificValue(uint8_t pin) {
  89. if (pin > 16)
  90. return LOW;
  91. Wire.beginTransmission(MCP23017_ADDRESS | _addr);
  92. uint8_t p = pin;
  93. if (pin > 8) {
  94. Wire.write(MCP23017_GPIO_B);
  95. p -= 8;
  96. } else
  97. Wire.write(MCP23017_GPIO_A);
  98. Wire.endTransmission();
  99. uint8_t bank = Wire.read();
  100. int value = (bank >> p) & 0x1;
  101. _value[pin] = value;
  102. return value;
  103. }