fork download
  1. #include <iostream>
  2.  
  3. class TwoWire
  4. {
  5. private:
  6. static void (*user_onReceive)(size_t);
  7.  
  8. public:
  9. TwoWire();
  10. void begin();
  11. virtual int read(void);
  12. void onReceive(void (*)(int));
  13. void onReceive(void (*)(size_t));
  14. };
  15.  
  16. void (*TwoWire::user_onReceive)(size_t);
  17.  
  18. TwoWire::TwoWire() {}
  19.  
  20. void TwoWire::begin()
  21. {
  22. if (user_onReceive)
  23. {
  24. user_onReceive(123);
  25. }
  26. }
  27.  
  28. int TwoWire::read(void)
  29. {
  30. return 12;
  31. }
  32.  
  33. void TwoWire::onReceive(void (*function)(int))
  34. {
  35. user_onReceive = reinterpret_cast<void(*)(size_t)>(function);
  36. }
  37.  
  38. void TwoWire::onReceive(void (*function)(size_t))
  39. {
  40. user_onReceive = function;
  41. }
  42.  
  43. TwoWire Wire;
  44.  
  45.  
  46. class hardwire {
  47. private:
  48. inline static uint8_t _rx = 0;
  49.  
  50. public:
  51. void hardwireStart(){
  52. Wire.onReceive(hardwireReceive);
  53. Wire.begin();
  54. }
  55.  
  56. static void hardwireReceive(int bytes){
  57. _rx = Wire.read();
  58. printf("_rx = %d, bytes = %d", _rx, bytes);
  59. }
  60. };
  61.  
  62.  
  63. hardwire chip;
  64.  
  65. int main()
  66. {
  67. chip.hardwireStart();
  68.  
  69. return 0;
  70. }
Success #stdin #stdout 0.01s 5392KB
stdin
Standard input is empty
stdout
_rx = 12,  bytes = 123