midi_device.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // midi for embedded chips,
  2. // Copyright 2010 Alex Norman
  3. //
  4. // This file is part of avr-midi.
  5. //
  6. // avr-midi is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. //(at your option) any later version.
  10. //
  11. // avr-midi is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with avr-midi. If not, see <http://www.gnu.org/licenses/>.
  18. /**
  19. * @file
  20. * @brief Device implementation functions
  21. */
  22. #ifndef MIDI_DEVICE_H
  23. #define MIDI_DEVICE_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * @defgroup midi_device Functions used when implementing your own midi device.
  29. *
  30. * You use the functions when you are implementing your own midi device.
  31. *
  32. * You set a send function to actually send bytes via your device, this method
  33. * is called when you call a send function with this device, for instance
  34. * midi_send_cc
  35. *
  36. * You use the midi_device_input to process input data from the device and pass
  37. * it through the device's associated callbacks.
  38. *
  39. * You use the midi_device_set_pre_input_process_func if you want to have a
  40. * function called at the beginning of the device's process function, generally
  41. * to poll for input and pass that into midi_device_input
  42. *
  43. * @{
  44. */
  45. #include "midi_function_types.h"
  46. #include "bytequeue/bytequeue.h"
  47. #define MIDI_INPUT_QUEUE_LENGTH 192
  48. typedef enum { IDLE, ONE_BYTE_MESSAGE = 1, TWO_BYTE_MESSAGE = 2, THREE_BYTE_MESSAGE = 3, SYSEX_MESSAGE } input_state_t;
  49. typedef void (*midi_no_byte_func_t)(MidiDevice* device);
  50. /**
  51. * \struct _midi_device
  52. *
  53. * @brief This structure represents the input and output functions and
  54. * processing data for a midi device.
  55. *
  56. * A device can represent an actual physical device [serial port, usb port] or
  57. * something virtual.
  58. * You should not need to modify this structure directly.
  59. */
  60. struct _midi_device {
  61. // output send function
  62. midi_var_byte_func_t send_func;
  63. //********input callbacks
  64. // three byte funcs
  65. midi_three_byte_func_t input_cc_callback;
  66. midi_three_byte_func_t input_noteon_callback;
  67. midi_three_byte_func_t input_noteoff_callback;
  68. midi_three_byte_func_t input_aftertouch_callback;
  69. midi_three_byte_func_t input_pitchbend_callback;
  70. midi_three_byte_func_t input_songposition_callback;
  71. // two byte funcs
  72. midi_two_byte_func_t input_progchange_callback;
  73. midi_two_byte_func_t input_chanpressure_callback;
  74. midi_two_byte_func_t input_songselect_callback;
  75. midi_two_byte_func_t input_tc_quarterframe_callback;
  76. // one byte funcs
  77. midi_one_byte_func_t input_realtime_callback;
  78. midi_one_byte_func_t input_tunerequest_callback;
  79. // sysex
  80. midi_sysex_func_t input_sysex_callback;
  81. // only called if more specific callback is not matched
  82. midi_var_byte_func_t input_fallthrough_callback;
  83. // called if registered, independent of other callbacks
  84. midi_var_byte_func_t input_catchall_callback;
  85. // pre input processing function
  86. midi_no_byte_func_t pre_input_process_callback;
  87. // for internal input processing
  88. uint8_t input_buffer[3];
  89. input_state_t input_state;
  90. uint16_t input_count;
  91. // for queueing data between the input and the processing functions
  92. uint8_t input_queue_data[MIDI_INPUT_QUEUE_LENGTH];
  93. byteQueue_t input_queue;
  94. };
  95. /**
  96. * @brief Process input bytes. This function parses bytes and calls the
  97. * appropriate callbacks associated with the given device. You use this
  98. * function if you are creating a custom device and you want to have midi
  99. * input.
  100. *
  101. * @param device the midi device to associate the input with
  102. * @param cnt the number of bytes you are processing
  103. * @param input the bytes to process
  104. */
  105. void midi_device_input(MidiDevice* device, uint8_t cnt, uint8_t* input);
  106. /**
  107. * @brief Set the callback function that will be used for sending output
  108. * data bytes. This is only used if you're creating a custom device.
  109. * You'll most likely want the callback function to disable interrupts so
  110. * that you can call the various midi send functions without worrying about
  111. * locking.
  112. *
  113. * \param device the midi device to associate this callback with
  114. * \param send_func the callback function that will do the sending
  115. */
  116. void midi_device_set_send_func(MidiDevice* device, midi_var_byte_func_t send_func);
  117. /**
  118. * @brief Set a callback which is called at the beginning of the
  119. * midi_device_process call. This can be used to poll for input
  120. * data and send the data through the midi_device_input function.
  121. * You'll probably only use this if you're creating a custom device.
  122. *
  123. * \param device the midi device to associate this callback with
  124. * \param midi_no_byte_func_t the actual callback function
  125. */
  126. void midi_device_set_pre_input_process_func(MidiDevice* device, midi_no_byte_func_t pre_process_func);
  127. /**@}*/
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif