GameboyInput.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /******************************************************************************
  2. * M5Snake : Input management from Gameboy faces *
  3. * --------------------------------------------- *
  4. * Management of input coming from the face Gameboy *
  5. * Author: Olivier Staquet *
  6. * Last version available on https://github.com/ostaquet/M5Snake *
  7. *****************************************************************************/
  8. #include "GameboyInput.h"
  9. /**
  10. * Initialize
  11. */
  12. void GameboyInputClass::begin(uint8_t _i2c_address, uint8_t _pin_int_face) {
  13. // Store local info
  14. i2c_address = _i2c_address;
  15. pin_int_face = _pin_int_face;
  16. // Prepare the detection of activity
  17. pinMode(pin_int_face, INPUT_PULLUP);
  18. // Init the I2C
  19. Wire.begin();
  20. }
  21. /**
  22. * Check if button pressed and return which one
  23. */
  24. uint8_t GameboyInputClass::getActivity() {
  25. // Check if there is activity on interrupt
  26. if (digitalRead(pin_int_face) == LOW) {
  27. // If yes, request 1 byte from the panel
  28. Wire.requestFrom(i2c_address, (uint8_t)1);
  29. // Check if data on the I2C is available
  30. while (Wire.available()) {
  31. // Receive one byte as character
  32. uint8_t key_val = Wire.read();
  33. if (key_val != 0x00) {
  34. return key_val;
  35. }
  36. }
  37. }
  38. // No activity to send back
  39. return GAMEBOY_KEY_NONE;
  40. }
  41. GameboyInputClass GameboyInput;