midi_device.h 5.0 KB

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