upload
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.

midi_device.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 {
  49. IDLE,
  50. ONE_BYTE_MESSAGE = 1,
  51. TWO_BYTE_MESSAGE = 2,
  52. THREE_BYTE_MESSAGE = 3,
  53. SYSEX_MESSAGE} input_state_t;
  54. typedef void (* midi_no_byte_func_t)(MidiDevice * device);
  55. /**
  56. * \struct _midi_device
  57. *
  58. * @brief This structure represents the input and output functions and
  59. * processing data for a midi device.
  60. *
  61. * A device can represent an actual physical device [serial port, usb port] or
  62. * something virtual.
  63. * You should not need to modify this structure directly.
  64. */
  65. struct _midi_device {
  66. //output send function
  67. midi_var_byte_func_t send_func;
  68. //********input callbacks
  69. //three byte funcs
  70. midi_three_byte_func_t input_cc_callback;
  71. midi_three_byte_func_t input_noteon_callback;
  72. midi_three_byte_func_t input_noteoff_callback;
  73. midi_three_byte_func_t input_aftertouch_callback;
  74. midi_three_byte_func_t input_pitchbend_callback;
  75. midi_three_byte_func_t input_songposition_callback;
  76. //two byte funcs
  77. midi_two_byte_func_t input_progchange_callback;
  78. midi_two_byte_func_t input_chanpressure_callback;
  79. midi_two_byte_func_t input_songselect_callback;
  80. midi_two_byte_func_t input_tc_quarterframe_callback;
  81. //one byte funcs
  82. midi_one_byte_func_t input_realtime_callback;
  83. midi_one_byte_func_t input_tunerequest_callback;
  84. //sysex
  85. midi_sysex_func_t input_sysex_callback;
  86. //only called if more specific callback is not matched
  87. midi_var_byte_func_t input_fallthrough_callback;
  88. //called if registered, independent of other callbacks
  89. midi_var_byte_func_t input_catchall_callback;
  90. //pre input processing function
  91. midi_no_byte_func_t pre_input_process_callback;
  92. //for internal input processing
  93. uint8_t input_buffer[3];
  94. input_state_t input_state;
  95. uint16_t input_count;
  96. //for queueing data between the input and the processing functions
  97. uint8_t input_queue_data[MIDI_INPUT_QUEUE_LENGTH];
  98. byteQueue_t input_queue;
  99. };
  100. /**
  101. * @brief Process input bytes. This function parses bytes and calls the
  102. * appropriate callbacks associated with the given device. You use this
  103. * function if you are creating a custom device and you want to have midi
  104. * input.
  105. *
  106. * @param device the midi device to associate the input with
  107. * @param cnt the number of bytes you are processing
  108. * @param input the bytes to process
  109. */
  110. void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input);
  111. /**
  112. * @brief Set the callback function that will be used for sending output
  113. * data bytes. This is only used if you're creating a custom device.
  114. * You'll most likely want the callback function to disable interrupts so
  115. * that you can call the various midi send functions without worrying about
  116. * locking.
  117. *
  118. * \param device the midi device to associate this callback with
  119. * \param send_func the callback function that will do the sending
  120. */
  121. void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func);
  122. /**
  123. * @brief Set a callback which is called at the beginning of the
  124. * midi_device_process call. This can be used to poll for input
  125. * data and send the data through the midi_device_input function.
  126. * You'll probably only use this if you're creating a custom device.
  127. *
  128. * \param device the midi device to associate this callback with
  129. * \param midi_no_byte_func_t the actual callback function
  130. */
  131. void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func);
  132. /**@}*/
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif