diff options
Diffstat (limited to 'tmk_core/protocol/midi')
| -rwxr-xr-x | tmk_core/protocol/midi/Config/LUFAConfig.h | 93 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/bytequeue/COPYING | 674 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/bytequeue/bytequeue.c | 65 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/bytequeue/bytequeue.h | 59 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/bytequeue/interrupt_setting.c | 36 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/bytequeue/interrupt_setting.h | 39 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/midi.c | 277 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/midi.h | 498 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/midi_device.c | 291 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/midi_device.h | 156 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/midi_function_types.h | 50 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/sysex_tools.c | 99 | ||||
| -rwxr-xr-x | tmk_core/protocol/midi/sysex_tools.h | 95 |
13 files changed, 2432 insertions, 0 deletions
diff --git a/tmk_core/protocol/midi/Config/LUFAConfig.h b/tmk_core/protocol/midi/Config/LUFAConfig.h new file mode 100755 index 000000000..fa9404498 --- /dev/null +++ b/tmk_core/protocol/midi/Config/LUFAConfig.h | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | /* | ||
| 2 | LUFA Library | ||
| 3 | Copyright (C) Dean Camera, 2012. | ||
| 4 | |||
| 5 | dean [at] fourwalledcubicle [dot] com | ||
| 6 | www.lufa-lib.org | ||
| 7 | */ | ||
| 8 | |||
| 9 | /* | ||
| 10 | Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com) | ||
| 11 | |||
| 12 | Permission to use, copy, modify, distribute, and sell this | ||
| 13 | software and its documentation for any purpose is hereby granted | ||
| 14 | without fee, provided that the above copyright notice appear in | ||
| 15 | all copies and that both that the copyright notice and this | ||
| 16 | permission notice and warranty disclaimer appear in supporting | ||
| 17 | documentation, and that the name of the author not be used in | ||
| 18 | advertising or publicity pertaining to distribution of the | ||
| 19 | software without specific, written prior permission. | ||
| 20 | |||
| 21 | The author disclaim all warranties with regard to this | ||
| 22 | software, including all implied warranties of merchantability | ||
| 23 | and fitness. In no event shall the author be liable for any | ||
| 24 | special, indirect or consequential damages or any damages | ||
| 25 | whatsoever resulting from loss of use, data or profits, whether | ||
| 26 | in an action of contract, negligence or other tortious action, | ||
| 27 | arising out of or in connection with the use or performance of | ||
| 28 | this software. | ||
| 29 | */ | ||
| 30 | |||
| 31 | /** \file | ||
| 32 | * \brief LUFA Library Configuration Header File | ||
| 33 | * | ||
| 34 | * This header file is used to configure LUFA's compile time options, | ||
| 35 | * as an alternative to the compile time constants supplied through | ||
| 36 | * a makefile. | ||
| 37 | * | ||
| 38 | * For information on what each token does, refer to the LUFA | ||
| 39 | * manual section "Summary of Compile Tokens". | ||
| 40 | */ | ||
| 41 | |||
| 42 | #ifndef _LUFA_CONFIG_H_ | ||
| 43 | #define _LUFA_CONFIG_H_ | ||
| 44 | |||
| 45 | #if (ARCH == ARCH_AVR8) | ||
| 46 | |||
| 47 | /* Non-USB Related Configuration Tokens: */ | ||
| 48 | // #define DISABLE_TERMINAL_CODES | ||
| 49 | |||
| 50 | /* USB Class Driver Related Tokens: */ | ||
| 51 | // #define HID_HOST_BOOT_PROTOCOL_ONLY | ||
| 52 | // #define HID_STATETABLE_STACK_DEPTH {Insert Value Here} | ||
| 53 | // #define HID_USAGE_STACK_DEPTH {Insert Value Here} | ||
| 54 | // #define HID_MAX_COLLECTIONS {Insert Value Here} | ||
| 55 | // #define HID_MAX_REPORTITEMS {Insert Value Here} | ||
| 56 | // #define HID_MAX_REPORT_IDS {Insert Value Here} | ||
| 57 | // #define NO_CLASS_DRIVER_AUTOFLUSH | ||
| 58 | |||
| 59 | /* General USB Driver Related Tokens: */ | ||
| 60 | // #define ORDERED_EP_CONFIG | ||
| 61 | #define USE_STATIC_OPTIONS (USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL) | ||
| 62 | #define USB_DEVICE_ONLY | ||
| 63 | // #define USB_HOST_ONLY | ||
| 64 | // #define USB_STREAM_TIMEOUT_MS {Insert Value Here} | ||
| 65 | // #define NO_LIMITED_CONTROLLER_CONNECT | ||
| 66 | // #define NO_SOF_EVENTS | ||
| 67 | |||
| 68 | /* USB Device Mode Driver Related Tokens: */ | ||
| 69 | // #define USE_RAM_DESCRIPTORS | ||
| 70 | #define USE_FLASH_DESCRIPTORS | ||
| 71 | // #define USE_EEPROM_DESCRIPTORS | ||
| 72 | // #define NO_INTERNAL_SERIAL | ||
| 73 | #define FIXED_CONTROL_ENDPOINT_SIZE 8 | ||
| 74 | // #define DEVICE_STATE_AS_GPIOR {Insert Value Here} | ||
| 75 | #define FIXED_NUM_CONFIGURATIONS 1 | ||
| 76 | // #define CONTROL_ONLY_DEVICE | ||
| 77 | // #define INTERRUPT_CONTROL_ENDPOINT | ||
| 78 | // #define NO_DEVICE_REMOTE_WAKEUP | ||
| 79 | // #define NO_DEVICE_SELF_POWER | ||
| 80 | |||
| 81 | /* USB Host Mode Driver Related Tokens: */ | ||
| 82 | // #define HOST_STATE_AS_GPIOR {Insert Value Here} | ||
| 83 | // #define USB_HOST_TIMEOUT_MS {Insert Value Here} | ||
| 84 | // #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here} | ||
| 85 | // #define NO_AUTO_VBUS_MANAGEMENT | ||
| 86 | // #define INVERTED_VBUS_ENABLE_LINE | ||
| 87 | |||
| 88 | #else | ||
| 89 | |||
| 90 | #error Unsupported architecture for this LUFA configuration file. | ||
| 91 | |||
| 92 | #endif | ||
| 93 | #endif | ||
diff --git a/tmk_core/protocol/midi/bytequeue/COPYING b/tmk_core/protocol/midi/bytequeue/COPYING new file mode 100755 index 000000000..94a9ed024 --- /dev/null +++ b/tmk_core/protocol/midi/bytequeue/COPYING | |||
| @@ -0,0 +1,674 @@ | |||
| 1 | GNU GENERAL PUBLIC LICENSE | ||
| 2 | Version 3, 29 June 2007 | ||
| 3 | |||
| 4 | Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> | ||
| 5 | Everyone is permitted to copy and distribute verbatim copies | ||
| 6 | of this license document, but changing it is not allowed. | ||
| 7 | |||
| 8 | Preamble | ||
| 9 | |||
| 10 | The GNU General Public License is a free, copyleft license for | ||
| 11 | software and other kinds of works. | ||
| 12 | |||
| 13 | The licenses for most software and other practical works are designed | ||
| 14 | to take away your freedom to share and change the works. By contrast, | ||
| 15 | the GNU General Public License is intended to guarantee your freedom to | ||
| 16 | share and change all versions of a program--to make sure it remains free | ||
| 17 | software for all its users. We, the Free Software Foundation, use the | ||
| 18 | GNU General Public License for most of our software; it applies also to | ||
| 19 | any other work released this way by its authors. You can apply it to | ||
| 20 | your programs, too. | ||
| 21 | |||
| 22 | When we speak of free software, we are referring to freedom, not | ||
| 23 | price. Our General Public Licenses are designed to make sure that you | ||
| 24 | have the freedom to distribute copies of free software (and charge for | ||
| 25 | them if you wish), that you receive source code or can get it if you | ||
| 26 | want it, that you can change the software or use pieces of it in new | ||
| 27 | free programs, and that you know you can do these things. | ||
| 28 | |||
| 29 | To protect your rights, we need to prevent others from denying you | ||
| 30 | these rights or asking you to surrender the rights. Therefore, you have | ||
| 31 | certain responsibilities if you distribute copies of the software, or if | ||
| 32 | you modify it: responsibilities to respect the freedom of others. | ||
| 33 | |||
| 34 | For example, if you distribute copies of such a program, whether | ||
| 35 | gratis or for a fee, you must pass on to the recipients the same | ||
| 36 | freedoms that you received. You must make sure that they, too, receive | ||
| 37 | or can get the source code. And you must show them these terms so they | ||
| 38 | know their rights. | ||
| 39 | |||
| 40 | Developers that use the GNU GPL protect your rights with two steps: | ||
| 41 | (1) assert copyright on the software, and (2) offer you this License | ||
| 42 | giving you legal permission to copy, distribute and/or modify it. | ||
| 43 | |||
| 44 | For the developers' and authors' protection, the GPL clearly explains | ||
| 45 | that there is no warranty for this free software. For both users' and | ||
| 46 | authors' sake, the GPL requires that modified versions be marked as | ||
| 47 | changed, so that their problems will not be attributed erroneously to | ||
| 48 | authors of previous versions. | ||
| 49 | |||
| 50 | Some devices are designed to deny users access to install or run | ||
| 51 | modified versions of the software inside them, although the manufacturer | ||
| 52 | can do so. This is fundamentally incompatible with the aim of | ||
| 53 | protecting users' freedom to change the software. The systematic | ||
| 54 | pattern of such abuse occurs in the area of products for individuals to | ||
| 55 | use, which is precisely where it is most unacceptable. Therefore, we | ||
| 56 | have designed this version of the GPL to prohibit the practice for those | ||
| 57 | products. If such problems arise substantially in other domains, we | ||
| 58 | stand ready to extend this provision to those domains in future versions | ||
| 59 | of the GPL, as needed to protect the freedom of users. | ||
| 60 | |||
| 61 | Finally, every program is threatened constantly by software patents. | ||
| 62 | States should not allow patents to restrict development and use of | ||
| 63 | software on general-purpose computers, but in those that do, we wish to | ||
| 64 | avoid the special danger that patents applied to a free program could | ||
| 65 | make it effectively proprietary. To prevent this, the GPL assures that | ||
| 66 | patents cannot be used to render the program non-free. | ||
| 67 | |||
| 68 | The precise terms and conditions for copying, distribution and | ||
| 69 | modification follow. | ||
| 70 | |||
| 71 | TERMS AND CONDITIONS | ||
| 72 | |||
| 73 | 0. Definitions. | ||
| 74 | |||
| 75 | "This License" refers to version 3 of the GNU General Public License. | ||
| 76 | |||
| 77 | "Copyright" also means copyright-like laws that apply to other kinds of | ||
| 78 | works, such as semiconductor masks. | ||
| 79 | |||
| 80 | "The Program" refers to any copyrightable work licensed under this | ||
| 81 | License. Each licensee is addressed as "you". "Licensees" and | ||
| 82 | "recipients" may be individuals or organizations. | ||
| 83 | |||
| 84 | To "modify" a work means to copy from or adapt all or part of the work | ||
| 85 | in a fashion requiring copyright permission, other than the making of an | ||
| 86 | exact copy. The resulting work is called a "modified version" of the | ||
| 87 | earlier work or a work "based on" the earlier work. | ||
| 88 | |||
| 89 | A "covered work" means either the unmodified Program or a work based | ||
| 90 | on the Program. | ||
| 91 | |||
| 92 | To "propagate" a work means to do anything with it that, without | ||
| 93 | permission, would make you directly or secondarily liable for | ||
| 94 | infringement under applicable copyright law, except executing it on a | ||
| 95 | computer or modifying a private copy. Propagation includes copying, | ||
| 96 | distribution (with or without modification), making available to the | ||
| 97 | public, and in some countries other activities as well. | ||
| 98 | |||
| 99 | To "convey" a work means any kind of propagation that enables other | ||
| 100 | parties to make or receive copies. Mere interaction with a user through | ||
| 101 | a computer network, with no transfer of a copy, is not conveying. | ||
| 102 | |||
| 103 | An interactive user interface displays "Appropriate Legal Notices" | ||
| 104 | to the extent that it includes a convenient and prominently visible | ||
| 105 | feature that (1) displays an appropriate copyright notice, and (2) | ||
| 106 | tells the user that there is no warranty for the work (except to the | ||
| 107 | extent that warranties are provided), that licensees may convey the | ||
| 108 | work under this License, and how to view a copy of this License. If | ||
| 109 | the interface presents a list of user commands or options, such as a | ||
| 110 | menu, a prominent item in the list meets this criterion. | ||
| 111 | |||
| 112 | 1. Source Code. | ||
| 113 | |||
| 114 | The "source code" for a work means the preferred form of the work | ||
| 115 | for making modifications to it. "Object code" means any non-source | ||
| 116 | form of a work. | ||
| 117 | |||
| 118 | A "Standard Interface" means an interface that either is an official | ||
| 119 | standard defined by a recognized standards body, or, in the case of | ||
| 120 | interfaces specified for a particular programming language, one that | ||
| 121 | is widely used among developers working in that language. | ||
| 122 | |||
| 123 | The "System Libraries" of an executable work include anything, other | ||
| 124 | than the work as a whole, that (a) is included in the normal form of | ||
| 125 | packaging a Major Component, but which is not part of that Major | ||
| 126 | Component, and (b) serves only to enable use of the work with that | ||
| 127 | Major Component, or to implement a Standard Interface for which an | ||
| 128 | implementation is available to the public in source code form. A | ||
| 129 | "Major Component", in this context, means a major essential component | ||
| 130 | (kernel, window system, and so on) of the specific operating system | ||
| 131 | (if any) on which the executable work runs, or a compiler used to | ||
| 132 | produce the work, or an object code interpreter used to run it. | ||
| 133 | |||
| 134 | The "Corresponding Source" for a work in object code form means all | ||
| 135 | the source code needed to generate, install, and (for an executable | ||
| 136 | work) run the object code and to modify the work, including scripts to | ||
| 137 | control those activities. However, it does not include the work's | ||
| 138 | System Libraries, or general-purpose tools or generally available free | ||
| 139 | programs which are used unmodified in performing those activities but | ||
| 140 | which are not part of the work. For example, Corresponding Source | ||
| 141 | includes interface definition files associated with source files for | ||
| 142 | the work, and the source code for shared libraries and dynamically | ||
| 143 | linked subprograms that the work is specifically designed to require, | ||
| 144 | such as by intimate data communication or control flow between those | ||
| 145 | subprograms and other parts of the work. | ||
| 146 | |||
| 147 | The Corresponding Source need not include anything that users | ||
| 148 | can regenerate automatically from other parts of the Corresponding | ||
| 149 | Source. | ||
| 150 | |||
| 151 | The Corresponding Source for a work in source code form is that | ||
| 152 | same work. | ||
| 153 | |||
| 154 | 2. Basic Permissions. | ||
| 155 | |||
| 156 | All rights granted under this License are granted for the term of | ||
| 157 | copyright on the Program, and are irrevocable provided the stated | ||
| 158 | conditions are met. This License explicitly affirms your unlimited | ||
| 159 | permission to run the unmodified Program. The output from running a | ||
| 160 | covered work is covered by this License only if the output, given its | ||
| 161 | content, constitutes a covered work. This License acknowledges your | ||
| 162 | rights of fair use or other equivalent, as provided by copyright law. | ||
| 163 | |||
| 164 | You may make, run and propagate covered works that you do not | ||
| 165 | convey, without conditions so long as your license otherwise remains | ||
| 166 | in force. You may convey covered works to others for the sole purpose | ||
| 167 | of having them make modifications exclusively for you, or provide you | ||
| 168 | with facilities for running those works, provided that you comply with | ||
| 169 | the terms of this License in conveying all material for which you do | ||
| 170 | not control copyright. Those thus making or running the covered works | ||
| 171 | for you must do so exclusively on your behalf, under your direction | ||
| 172 | and control, on terms that prohibit them from making any copies of | ||
| 173 | your copyrighted material outside their relationship with you. | ||
| 174 | |||
| 175 | Conveying under any other circumstances is permitted solely under | ||
| 176 | the conditions stated below. Sublicensing is not allowed; section 10 | ||
| 177 | makes it unnecessary. | ||
| 178 | |||
| 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. | ||
| 180 | |||
| 181 | No covered work shall be deemed part of an effective technological | ||
| 182 | measure under any applicable law fulfilling obligations under article | ||
| 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or | ||
| 184 | similar laws prohibiting or restricting circumvention of such | ||
| 185 | measures. | ||
| 186 | |||
| 187 | When you convey a covered work, you waive any legal power to forbid | ||
| 188 | circumvention of technological measures to the extent such circumvention | ||
| 189 | is effected by exercising rights under this License with respect to | ||
| 190 | the covered work, and you disclaim any intention to limit operation or | ||
| 191 | modification of the work as a means of enforcing, against the work's | ||
| 192 | users, your or third parties' legal rights to forbid circumvention of | ||
| 193 | technological measures. | ||
| 194 | |||
| 195 | 4. Conveying Verbatim Copies. | ||
| 196 | |||
| 197 | You may convey verbatim copies of the Program's source code as you | ||
| 198 | receive it, in any medium, provided that you conspicuously and | ||
| 199 | appropriately publish on each copy an appropriate copyright notice; | ||
| 200 | keep intact all notices stating that this License and any | ||
| 201 | non-permissive terms added in accord with section 7 apply to the code; | ||
| 202 | keep intact all notices of the absence of any warranty; and give all | ||
| 203 | recipients a copy of this License along with the Program. | ||
| 204 | |||
| 205 | You may charge any price or no price for each copy that you convey, | ||
| 206 | and you may offer support or warranty protection for a fee. | ||
| 207 | |||
| 208 | 5. Conveying Modified Source Versions. | ||
| 209 | |||
| 210 | You may convey a work based on the Program, or the modifications to | ||
| 211 | produce it from the Program, in the form of source code under the | ||
| 212 | terms of section 4, provided that you also meet all of these conditions: | ||
| 213 | |||
| 214 | a) The work must carry prominent notices stating that you modified | ||
| 215 | it, and giving a relevant date. | ||
| 216 | |||
| 217 | b) The work must carry prominent notices stating that it is | ||
| 218 | released under this License and any conditions added under section | ||
| 219 | 7. This requirement modifies the requirement in section 4 to | ||
| 220 | "keep intact all notices". | ||
| 221 | |||
| 222 | c) You must license the entire work, as a whole, under this | ||
| 223 | License to anyone who comes into possession of a copy. This | ||
| 224 | License will therefore apply, along with any applicable section 7 | ||
| 225 | additional terms, to the whole of the work, and all its parts, | ||
| 226 | regardless of how they are packaged. This License gives no | ||
| 227 | permission to license the work in any other way, but it does not | ||
| 228 | invalidate such permission if you have separately received it. | ||
| 229 | |||
| 230 | d) If the work has interactive user interfaces, each must display | ||
| 231 | Appropriate Legal Notices; however, if the Program has interactive | ||
| 232 | interfaces that do not display Appropriate Legal Notices, your | ||
| 233 | work need not make them do so. | ||
| 234 | |||
| 235 | A compilation of a covered work with other separate and independent | ||
| 236 | works, which are not by their nature extensions of the covered work, | ||
| 237 | and which are not combined with it such as to form a larger program, | ||
| 238 | in or on a volume of a storage or distribution medium, is called an | ||
| 239 | "aggregate" if the compilation and its resulting copyright are not | ||
| 240 | used to limit the access or legal rights of the compilation's users | ||
| 241 | beyond what the individual works permit. Inclusion of a covered work | ||
| 242 | in an aggregate does not cause this License to apply to the other | ||
| 243 | parts of the aggregate. | ||
| 244 | |||
| 245 | 6. Conveying Non-Source Forms. | ||
| 246 | |||
| 247 | You may convey a covered work in object code form under the terms | ||
| 248 | of sections 4 and 5, provided that you also convey the | ||
| 249 | machine-readable Corresponding Source under the terms of this License, | ||
| 250 | in one of these ways: | ||
| 251 | |||
| 252 | a) Convey the object code in, or embodied in, a physical product | ||
| 253 | (including a physical distribution medium), accompanied by the | ||
| 254 | Corresponding Source fixed on a durable physical medium | ||
| 255 | customarily used for software interchange. | ||
| 256 | |||
| 257 | b) Convey the object code in, or embodied in, a physical product | ||
| 258 | (including a physical distribution medium), accompanied by a | ||
| 259 | written offer, valid for at least three years and valid for as | ||
| 260 | long as you offer spare parts or customer support for that product | ||
| 261 | model, to give anyone who possesses the object code either (1) a | ||
| 262 | copy of the Corresponding Source for all the software in the | ||
| 263 | product that is covered by this License, on a durable physical | ||
| 264 | medium customarily used for software interchange, for a price no | ||
| 265 | more than your reasonable cost of physically performing this | ||
| 266 | conveying of source, or (2) access to copy the | ||
| 267 | Corresponding Source from a network server at no charge. | ||
| 268 | |||
| 269 | c) Convey individual copies of the object code with a copy of the | ||
| 270 | written offer to provide the Corresponding Source. This | ||
| 271 | alternative is allowed only occasionally and noncommercially, and | ||
| 272 | only if you received the object code with such an offer, in accord | ||
| 273 | with subsection 6b. | ||
| 274 | |||
| 275 | d) Convey the object code by offering access from a designated | ||
| 276 | place (gratis or for a charge), and offer equivalent access to the | ||
| 277 | Corresponding Source in the same way through the same place at no | ||
| 278 | further charge. You need not require recipients to copy the | ||
| 279 | Corresponding Source along with the object code. If the place to | ||
| 280 | copy the object code is a network server, the Corresponding Source | ||
| 281 | may be on a different server (operated by you or a third party) | ||
| 282 | that supports equivalent copying facilities, provided you maintain | ||
| 283 | clear directions next to the object code saying where to find the | ||
| 284 | Corresponding Source. Regardless of what server hosts the | ||
| 285 | Corresponding Source, you remain obligated to ensure that it is | ||
| 286 | available for as long as needed to satisfy these requirements. | ||
| 287 | |||
| 288 | e) Convey the object code using peer-to-peer transmission, provided | ||
| 289 | you inform other peers where the object code and Corresponding | ||
| 290 | Source of the work are being offered to the general public at no | ||
| 291 | charge under subsection 6d. | ||
| 292 | |||
| 293 | A separable portion of the object code, whose source code is excluded | ||
| 294 | from the Corresponding Source as a System Library, need not be | ||
| 295 | included in conveying the object code work. | ||
| 296 | |||
| 297 | A "User Product" is either (1) a "consumer product", which means any | ||
| 298 | tangible personal property which is normally used for personal, family, | ||
| 299 | or household purposes, or (2) anything designed or sold for incorporation | ||
| 300 | into a dwelling. In determining whether a product is a consumer product, | ||
| 301 | doubtful cases shall be resolved in favor of coverage. For a particular | ||
| 302 | product received by a particular user, "normally used" refers to a | ||
| 303 | typical or common use of that class of product, regardless of the status | ||
| 304 | of the particular user or of the way in which the particular user | ||
| 305 | actually uses, or expects or is expected to use, the product. A product | ||
| 306 | is a consumer product regardless of whether the product has substantial | ||
| 307 | commercial, industrial or non-consumer uses, unless such uses represent | ||
| 308 | the only significant mode of use of the product. | ||
| 309 | |||
| 310 | "Installation Information" for a User Product means any methods, | ||
| 311 | procedures, authorization keys, or other information required to install | ||
| 312 | and execute modified versions of a covered work in that User Product from | ||
| 313 | a modified version of its Corresponding Source. The information must | ||
| 314 | suffice to ensure that the continued functioning of the modified object | ||
| 315 | code is in no case prevented or interfered with solely because | ||
| 316 | modification has been made. | ||
| 317 | |||
| 318 | If you convey an object code work under this section in, or with, or | ||
| 319 | specifically for use in, a User Product, and the conveying occurs as | ||
| 320 | part of a transaction in which the right of possession and use of the | ||
| 321 | User Product is transferred to the recipient in perpetuity or for a | ||
| 322 | fixed term (regardless of how the transaction is characterized), the | ||
| 323 | Corresponding Source conveyed under this section must be accompanied | ||
| 324 | by the Installation Information. But this requirement does not apply | ||
| 325 | if neither you nor any third party retains the ability to install | ||
| 326 | modified object code on the User Product (for example, the work has | ||
| 327 | been installed in ROM). | ||
| 328 | |||
| 329 | The requirement to provide Installation Information does not include a | ||
| 330 | requirement to continue to provide support service, warranty, or updates | ||
| 331 | for a work that has been modified or installed by the recipient, or for | ||
| 332 | the User Product in which it has been modified or installed. Access to a | ||
| 333 | network may be denied when the modification itself materially and | ||
| 334 | adversely affects the operation of the network or violates the rules and | ||
| 335 | protocols for communication across the network. | ||
| 336 | |||
| 337 | Corresponding Source conveyed, and Installation Information provided, | ||
| 338 | in accord with this section must be in a format that is publicly | ||
| 339 | documented (and with an implementation available to the public in | ||
| 340 | source code form), and must require no special password or key for | ||
| 341 | unpacking, reading or copying. | ||
| 342 | |||
| 343 | 7. Additional Terms. | ||
| 344 | |||
| 345 | "Additional permissions" are terms that supplement the terms of this | ||
| 346 | License by making exceptions from one or more of its conditions. | ||
| 347 | Additional permissions that are applicable to the entire Program shall | ||
| 348 | be treated as though they were included in this License, to the extent | ||
| 349 | that they are valid under applicable law. If additional permissions | ||
| 350 | apply only to part of the Program, that part may be used separately | ||
| 351 | under those permissions, but the entire Program remains governed by | ||
| 352 | this License without regard to the additional permissions. | ||
| 353 | |||
| 354 | When you convey a copy of a covered work, you may at your option | ||
| 355 | remove any additional permissions from that copy, or from any part of | ||
| 356 | it. (Additional permissions may be written to require their own | ||
| 357 | removal in certain cases when you modify the work.) You may place | ||
| 358 | additional permissions on material, added by you to a covered work, | ||
| 359 | for which you have or can give appropriate copyright permission. | ||
| 360 | |||
| 361 | Notwithstanding any other provision of this License, for material you | ||
| 362 | add to a covered work, you may (if authorized by the copyright holders of | ||
| 363 | that material) supplement the terms of this License with terms: | ||
| 364 | |||
| 365 | a) Disclaiming warranty or limiting liability differently from the | ||
| 366 | terms of sections 15 and 16 of this License; or | ||
| 367 | |||
| 368 | b) Requiring preservation of specified reasonable legal notices or | ||
| 369 | author attributions in that material or in the Appropriate Legal | ||
| 370 | Notices displayed by works containing it; or | ||
| 371 | |||
| 372 | c) Prohibiting misrepresentation of the origin of that material, or | ||
| 373 | requiring that modified versions of such material be marked in | ||
| 374 | reasonable ways as different from the original version; or | ||
| 375 | |||
| 376 | d) Limiting the use for publicity purposes of names of licensors or | ||
| 377 | authors of the material; or | ||
| 378 | |||
| 379 | e) Declining to grant rights under trademark law for use of some | ||
| 380 | trade names, trademarks, or service marks; or | ||
| 381 | |||
| 382 | f) Requiring indemnification of licensors and authors of that | ||
| 383 | material by anyone who conveys the material (or modified versions of | ||
| 384 | it) with contractual assumptions of liability to the recipient, for | ||
| 385 | any liability that these contractual assumptions directly impose on | ||
| 386 | those licensors and authors. | ||
| 387 | |||
| 388 | All other non-permissive additional terms are considered "further | ||
| 389 | restrictions" within the meaning of section 10. If the Program as you | ||
| 390 | received it, or any part of it, contains a notice stating that it is | ||
| 391 | governed by this License along with a term that is a further | ||
| 392 | restriction, you may remove that term. If a license document contains | ||
| 393 | a further restriction but permits relicensing or conveying under this | ||
| 394 | License, you may add to a covered work material governed by the terms | ||
| 395 | of that license document, provided that the further restriction does | ||
| 396 | not survive such relicensing or conveying. | ||
| 397 | |||
| 398 | If you add terms to a covered work in accord with this section, you | ||
| 399 | must place, in the relevant source files, a statement of the | ||
| 400 | additional terms that apply to those files, or a notice indicating | ||
| 401 | where to find the applicable terms. | ||
| 402 | |||
| 403 | Additional terms, permissive or non-permissive, may be stated in the | ||
| 404 | form of a separately written license, or stated as exceptions; | ||
| 405 | the above requirements apply either way. | ||
| 406 | |||
| 407 | 8. Termination. | ||
| 408 | |||
| 409 | You may not propagate or modify a covered work except as expressly | ||
| 410 | provided under this License. Any attempt otherwise to propagate or | ||
| 411 | modify it is void, and will automatically terminate your rights under | ||
| 412 | this License (including any patent licenses granted under the third | ||
| 413 | paragraph of section 11). | ||
| 414 | |||
| 415 | However, if you cease all violation of this License, then your | ||
| 416 | license from a particular copyright holder is reinstated (a) | ||
| 417 | provisionally, unless and until the copyright holder explicitly and | ||
| 418 | finally terminates your license, and (b) permanently, if the copyright | ||
| 419 | holder fails to notify you of the violation by some reasonable means | ||
| 420 | prior to 60 days after the cessation. | ||
| 421 | |||
| 422 | Moreover, your license from a particular copyright holder is | ||
| 423 | reinstated permanently if the copyright holder notifies you of the | ||
| 424 | violation by some reasonable means, this is the first time you have | ||
| 425 | received notice of violation of this License (for any work) from that | ||
| 426 | copyright holder, and you cure the violation prior to 30 days after | ||
| 427 | your receipt of the notice. | ||
| 428 | |||
| 429 | Termination of your rights under this section does not terminate the | ||
| 430 | licenses of parties who have received copies or rights from you under | ||
| 431 | this License. If your rights have been terminated and not permanently | ||
| 432 | reinstated, you do not qualify to receive new licenses for the same | ||
| 433 | material under section 10. | ||
| 434 | |||
| 435 | 9. Acceptance Not Required for Having Copies. | ||
| 436 | |||
| 437 | You are not required to accept this License in order to receive or | ||
| 438 | run a copy of the Program. Ancillary propagation of a covered work | ||
| 439 | occurring solely as a consequence of using peer-to-peer transmission | ||
| 440 | to receive a copy likewise does not require acceptance. However, | ||
| 441 | nothing other than this License grants you permission to propagate or | ||
| 442 | modify any covered work. These actions infringe copyright if you do | ||
| 443 | not accept this License. Therefore, by modifying or propagating a | ||
| 444 | covered work, you indicate your acceptance of this License to do so. | ||
| 445 | |||
| 446 | 10. Automatic Licensing of Downstream Recipients. | ||
| 447 | |||
| 448 | Each time you convey a covered work, the recipient automatically | ||
| 449 | receives a license from the original licensors, to run, modify and | ||
| 450 | propagate that work, subject to this License. You are not responsible | ||
| 451 | for enforcing compliance by third parties with this License. | ||
| 452 | |||
| 453 | An "entity transaction" is a transaction transferring control of an | ||
| 454 | organization, or substantially all assets of one, or subdividing an | ||
| 455 | organization, or merging organizations. If propagation of a covered | ||
| 456 | work results from an entity transaction, each party to that | ||
| 457 | transaction who receives a copy of the work also receives whatever | ||
| 458 | licenses to the work the party's predecessor in interest had or could | ||
| 459 | give under the previous paragraph, plus a right to possession of the | ||
| 460 | Corresponding Source of the work from the predecessor in interest, if | ||
| 461 | the predecessor has it or can get it with reasonable efforts. | ||
| 462 | |||
| 463 | You may not impose any further restrictions on the exercise of the | ||
| 464 | rights granted or affirmed under this License. For example, you may | ||
| 465 | not impose a license fee, royalty, or other charge for exercise of | ||
| 466 | rights granted under this License, and you may not initiate litigation | ||
| 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that | ||
| 468 | any patent claim is infringed by making, using, selling, offering for | ||
| 469 | sale, or importing the Program or any portion of it. | ||
| 470 | |||
| 471 | 11. Patents. | ||
| 472 | |||
| 473 | A "contributor" is a copyright holder who authorizes use under this | ||
| 474 | License of the Program or a work on which the Program is based. The | ||
| 475 | work thus licensed is called the contributor's "contributor version". | ||
| 476 | |||
| 477 | A contributor's "essential patent claims" are all patent claims | ||
| 478 | owned or controlled by the contributor, whether already acquired or | ||
| 479 | hereafter acquired, that would be infringed by some manner, permitted | ||
| 480 | by this License, of making, using, or selling its contributor version, | ||
| 481 | but do not include claims that would be infringed only as a | ||
| 482 | consequence of further modification of the contributor version. For | ||
| 483 | purposes of this definition, "control" includes the right to grant | ||
| 484 | patent sublicenses in a manner consistent with the requirements of | ||
| 485 | this License. | ||
| 486 | |||
| 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free | ||
| 488 | patent license under the contributor's essential patent claims, to | ||
| 489 | make, use, sell, offer for sale, import and otherwise run, modify and | ||
| 490 | propagate the contents of its contributor version. | ||
| 491 | |||
| 492 | In the following three paragraphs, a "patent license" is any express | ||
| 493 | agreement or commitment, however denominated, not to enforce a patent | ||
| 494 | (such as an express permission to practice a patent or covenant not to | ||
| 495 | sue for patent infringement). To "grant" such a patent license to a | ||
| 496 | party means to make such an agreement or commitment not to enforce a | ||
| 497 | patent against the party. | ||
| 498 | |||
| 499 | If you convey a covered work, knowingly relying on a patent license, | ||
| 500 | and the Corresponding Source of the work is not available for anyone | ||
| 501 | to copy, free of charge and under the terms of this License, through a | ||
| 502 | publicly available network server or other readily accessible means, | ||
| 503 | then you must either (1) cause the Corresponding Source to be so | ||
| 504 | available, or (2) arrange to deprive yourself of the benefit of the | ||
| 505 | patent license for this particular work, or (3) arrange, in a manner | ||
| 506 | consistent with the requirements of this License, to extend the patent | ||
| 507 | license to downstream recipients. "Knowingly relying" means you have | ||
| 508 | actual knowledge that, but for the patent license, your conveying the | ||
| 509 | covered work in a country, or your recipient's use of the covered work | ||
| 510 | in a country, would infringe one or more identifiable patents in that | ||
| 511 | country that you have reason to believe are valid. | ||
| 512 | |||
| 513 | If, pursuant to or in connection with a single transaction or | ||
| 514 | arrangement, you convey, or propagate by procuring conveyance of, a | ||
| 515 | covered work, and grant a patent license to some of the parties | ||
| 516 | receiving the covered work authorizing them to use, propagate, modify | ||
| 517 | or convey a specific copy of the covered work, then the patent license | ||
| 518 | you grant is automatically extended to all recipients of the covered | ||
| 519 | work and works based on it. | ||
| 520 | |||
| 521 | A patent license is "discriminatory" if it does not include within | ||
| 522 | the scope of its coverage, prohibits the exercise of, or is | ||
| 523 | conditioned on the non-exercise of one or more of the rights that are | ||
| 524 | specifically granted under this License. You may not convey a covered | ||
| 525 | work if you are a party to an arrangement with a third party that is | ||
| 526 | in the business of distributing software, under which you make payment | ||
| 527 | to the third party based on the extent of your activity of conveying | ||
| 528 | the work, and under which the third party grants, to any of the | ||
| 529 | parties who would receive the covered work from you, a discriminatory | ||
| 530 | patent license (a) in connection with copies of the covered work | ||
| 531 | conveyed by you (or copies made from those copies), or (b) primarily | ||
| 532 | for and in connection with specific products or compilations that | ||
| 533 | contain the covered work, unless you entered into that arrangement, | ||
| 534 | or that patent license was granted, prior to 28 March 2007. | ||
| 535 | |||
| 536 | Nothing in this License shall be construed as excluding or limiting | ||
| 537 | any implied license or other defenses to infringement that may | ||
| 538 | otherwise be available to you under applicable patent law. | ||
| 539 | |||
| 540 | 12. No Surrender of Others' Freedom. | ||
| 541 | |||
| 542 | If conditions are imposed on you (whether by court order, agreement or | ||
| 543 | otherwise) that contradict the conditions of this License, they do not | ||
| 544 | excuse you from the conditions of this License. If you cannot convey a | ||
| 545 | covered work so as to satisfy simultaneously your obligations under this | ||
| 546 | License and any other pertinent obligations, then as a consequence you may | ||
| 547 | not convey it at all. For example, if you agree to terms that obligate you | ||
| 548 | to collect a royalty for further conveying from those to whom you convey | ||
| 549 | the Program, the only way you could satisfy both those terms and this | ||
| 550 | License would be to refrain entirely from conveying the Program. | ||
| 551 | |||
| 552 | 13. Use with the GNU Affero General Public License. | ||
| 553 | |||
| 554 | Notwithstanding any other provision of this License, you have | ||
| 555 | permission to link or combine any covered work with a work licensed | ||
| 556 | under version 3 of the GNU Affero General Public License into a single | ||
| 557 | combined work, and to convey the resulting work. The terms of this | ||
| 558 | License will continue to apply to the part which is the covered work, | ||
| 559 | but the special requirements of the GNU Affero General Public License, | ||
| 560 | section 13, concerning interaction through a network will apply to the | ||
| 561 | combination as such. | ||
| 562 | |||
| 563 | 14. Revised Versions of this License. | ||
| 564 | |||
| 565 | The Free Software Foundation may publish revised and/or new versions of | ||
| 566 | the GNU General Public License from time to time. Such new versions will | ||
| 567 | be similar in spirit to the present version, but may differ in detail to | ||
| 568 | address new problems or concerns. | ||
| 569 | |||
| 570 | Each version is given a distinguishing version number. If the | ||
| 571 | Program specifies that a certain numbered version of the GNU General | ||
| 572 | Public License "or any later version" applies to it, you have the | ||
| 573 | option of following the terms and conditions either of that numbered | ||
| 574 | version or of any later version published by the Free Software | ||
| 575 | Foundation. If the Program does not specify a version number of the | ||
| 576 | GNU General Public License, you may choose any version ever published | ||
| 577 | by the Free Software Foundation. | ||
| 578 | |||
| 579 | If the Program specifies that a proxy can decide which future | ||
| 580 | versions of the GNU General Public License can be used, that proxy's | ||
| 581 | public statement of acceptance of a version permanently authorizes you | ||
| 582 | to choose that version for the Program. | ||
| 583 | |||
| 584 | Later license versions may give you additional or different | ||
| 585 | permissions. However, no additional obligations are imposed on any | ||
| 586 | author or copyright holder as a result of your choosing to follow a | ||
| 587 | later version. | ||
| 588 | |||
| 589 | 15. Disclaimer of Warranty. | ||
| 590 | |||
| 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY | ||
| 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT | ||
| 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY | ||
| 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, | ||
| 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM | ||
| 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF | ||
| 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. | ||
| 599 | |||
| 600 | 16. Limitation of Liability. | ||
| 601 | |||
| 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | ||
| 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS | ||
| 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY | ||
| 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE | ||
| 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF | ||
| 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD | ||
| 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), | ||
| 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF | ||
| 610 | SUCH DAMAGES. | ||
| 611 | |||
| 612 | 17. Interpretation of Sections 15 and 16. | ||
| 613 | |||
| 614 | If the disclaimer of warranty and limitation of liability provided | ||
| 615 | above cannot be given local legal effect according to their terms, | ||
| 616 | reviewing courts shall apply local law that most closely approximates | ||
| 617 | an absolute waiver of all civil liability in connection with the | ||
| 618 | Program, unless a warranty or assumption of liability accompanies a | ||
| 619 | copy of the Program in return for a fee. | ||
| 620 | |||
| 621 | END OF TERMS AND CONDITIONS | ||
| 622 | |||
| 623 | How to Apply These Terms to Your New Programs | ||
| 624 | |||
| 625 | If you develop a new program, and you want it to be of the greatest | ||
| 626 | possible use to the public, the best way to achieve this is to make it | ||
| 627 | free software which everyone can redistribute and change under these terms. | ||
| 628 | |||
| 629 | To do so, attach the following notices to the program. It is safest | ||
| 630 | to attach them to the start of each source file to most effectively | ||
| 631 | state the exclusion of warranty; and each file should have at least | ||
| 632 | the "copyright" line and a pointer to where the full notice is found. | ||
| 633 | |||
| 634 | <one line to give the program's name and a brief idea of what it does.> | ||
| 635 | Copyright (C) <year> <name of author> | ||
| 636 | |||
| 637 | This program is free software: you can redistribute it and/or modify | ||
| 638 | it under the terms of the GNU General Public License as published by | ||
| 639 | the Free Software Foundation, either version 3 of the License, or | ||
| 640 | (at your option) any later version. | ||
| 641 | |||
| 642 | This program is distributed in the hope that it will be useful, | ||
| 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 645 | GNU General Public License for more details. | ||
| 646 | |||
| 647 | You should have received a copy of the GNU General Public License | ||
| 648 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 649 | |||
| 650 | Also add information on how to contact you by electronic and paper mail. | ||
| 651 | |||
| 652 | If the program does terminal interaction, make it output a short | ||
| 653 | notice like this when it starts in an interactive mode: | ||
| 654 | |||
| 655 | <program> Copyright (C) <year> <name of author> | ||
| 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. | ||
| 657 | This is free software, and you are welcome to redistribute it | ||
| 658 | under certain conditions; type `show c' for details. | ||
| 659 | |||
| 660 | The hypothetical commands `show w' and `show c' should show the appropriate | ||
| 661 | parts of the General Public License. Of course, your program's commands | ||
| 662 | might be different; for a GUI interface, you would use an "about box". | ||
| 663 | |||
| 664 | You should also get your employer (if you work as a programmer) or school, | ||
| 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. | ||
| 666 | For more information on this, and how to apply and follow the GNU GPL, see | ||
| 667 | <http://www.gnu.org/licenses/>. | ||
| 668 | |||
| 669 | The GNU General Public License does not permit incorporating your program | ||
| 670 | into proprietary programs. If your program is a subroutine library, you | ||
| 671 | may consider it more useful to permit linking proprietary applications with | ||
| 672 | the library. If this is what you want to do, use the GNU Lesser General | ||
| 673 | Public License instead of this License. But first, please read | ||
| 674 | <http://www.gnu.org/philosophy/why-not-lgpl.html>. | ||
diff --git a/tmk_core/protocol/midi/bytequeue/bytequeue.c b/tmk_core/protocol/midi/bytequeue/bytequeue.c new file mode 100755 index 000000000..e43495632 --- /dev/null +++ b/tmk_core/protocol/midi/bytequeue/bytequeue.c | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | //this is a single reader [maybe multiple writer?] byte queue | ||
| 2 | //Copyright 2008 Alex Norman | ||
| 3 | //writen by Alex Norman | ||
| 4 | // | ||
| 5 | //This file is part of avr-bytequeue. | ||
| 6 | // | ||
| 7 | //avr-bytequeue is free software: you can redistribute it and/or modify | ||
| 8 | //it under the terms of the GNU General Public License as published by | ||
| 9 | //the Free Software Foundation, either version 3 of the License, or | ||
| 10 | //(at your option) any later version. | ||
| 11 | // | ||
| 12 | //avr-bytequeue is distributed in the hope that it will be useful, | ||
| 13 | //but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | //GNU General Public License for more details. | ||
| 16 | // | ||
| 17 | //You should have received a copy of the GNU General Public License | ||
| 18 | //along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | #include "bytequeue.h" | ||
| 21 | #include "interrupt_setting.h" | ||
| 22 | |||
| 23 | void bytequeue_init(byteQueue_t * queue, uint8_t * dataArray, byteQueueIndex_t arrayLen){ | ||
| 24 | queue->length = arrayLen; | ||
| 25 | queue->data = dataArray; | ||
| 26 | queue->start = queue->end = 0; | ||
| 27 | } | ||
| 28 | |||
| 29 | bool bytequeue_enqueue(byteQueue_t * queue, uint8_t item){ | ||
| 30 | interrupt_setting_t setting = store_and_clear_interrupt(); | ||
| 31 | //full | ||
| 32 | if(((queue->end + 1) % queue->length) == queue->start){ | ||
| 33 | restore_interrupt_setting(setting); | ||
| 34 | return false; | ||
| 35 | } else { | ||
| 36 | queue->data[queue->end] = item; | ||
| 37 | queue->end = (queue->end + 1) % queue->length; | ||
| 38 | restore_interrupt_setting(setting); | ||
| 39 | return true; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | byteQueueIndex_t bytequeue_length(byteQueue_t * queue){ | ||
| 44 | byteQueueIndex_t len; | ||
| 45 | interrupt_setting_t setting = store_and_clear_interrupt(); | ||
| 46 | if(queue->end >= queue->start) | ||
| 47 | len = queue->end - queue->start; | ||
| 48 | else | ||
| 49 | len = (queue->length - queue->start) + queue->end; | ||
| 50 | restore_interrupt_setting(setting); | ||
| 51 | return len; | ||
| 52 | } | ||
| 53 | |||
| 54 | //we don't need to avoid interrupts if there is only one reader | ||
| 55 | uint8_t bytequeue_get(byteQueue_t * queue, byteQueueIndex_t index){ | ||
| 56 | return queue->data[(queue->start + index) % queue->length]; | ||
| 57 | } | ||
| 58 | |||
| 59 | //we just update the start index to remove elements | ||
| 60 | void bytequeue_remove(byteQueue_t * queue, byteQueueIndex_t numToRemove){ | ||
| 61 | interrupt_setting_t setting = store_and_clear_interrupt(); | ||
| 62 | queue->start = (queue->start + numToRemove) % queue->length; | ||
| 63 | restore_interrupt_setting(setting); | ||
| 64 | } | ||
| 65 | |||
diff --git a/tmk_core/protocol/midi/bytequeue/bytequeue.h b/tmk_core/protocol/midi/bytequeue/bytequeue.h new file mode 100755 index 000000000..e4a286134 --- /dev/null +++ b/tmk_core/protocol/midi/bytequeue/bytequeue.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | //this is a single reader [maybe multiple writer?] byte queue | ||
| 2 | //Copyright 2008 Alex Norman | ||
| 3 | //writen by Alex Norman | ||
| 4 | // | ||
| 5 | //This file is part of avr-bytequeue. | ||
| 6 | // | ||
| 7 | //avr-bytequeue is free software: you can redistribute it and/or modify | ||
| 8 | //it under the terms of the GNU General Public License as published by | ||
| 9 | //the Free Software Foundation, either version 3 of the License, or | ||
| 10 | //(at your option) any later version. | ||
| 11 | // | ||
| 12 | //avr-bytequeue is distributed in the hope that it will be useful, | ||
| 13 | //but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | //GNU General Public License for more details. | ||
| 16 | // | ||
| 17 | //You should have received a copy of the GNU General Public License | ||
| 18 | //along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | #ifndef BYTEQUEUE_H | ||
| 21 | #define BYTEQUEUE_H | ||
| 22 | |||
| 23 | #ifdef __cplusplus | ||
| 24 | extern "C" { | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <inttypes.h> | ||
| 28 | #include <stdbool.h> | ||
| 29 | |||
| 30 | typedef uint8_t byteQueueIndex_t; | ||
| 31 | |||
| 32 | typedef struct { | ||
| 33 | byteQueueIndex_t start; | ||
| 34 | byteQueueIndex_t end; | ||
| 35 | byteQueueIndex_t length; | ||
| 36 | uint8_t * data; | ||
| 37 | } byteQueue_t; | ||
| 38 | |||
| 39 | //you must have a queue, an array of data which the queue will use, and the length of that array | ||
| 40 | void bytequeue_init(byteQueue_t * queue, uint8_t * dataArray, byteQueueIndex_t arrayLen); | ||
| 41 | |||
| 42 | //add an item to the queue, returns false if the queue is full | ||
| 43 | bool bytequeue_enqueue(byteQueue_t * queue, uint8_t item); | ||
| 44 | |||
| 45 | //get the length of the queue | ||
| 46 | byteQueueIndex_t bytequeue_length(byteQueue_t * queue); | ||
| 47 | |||
| 48 | //this grabs data at the index given [starting at queue->start] | ||
| 49 | uint8_t bytequeue_get(byteQueue_t * queue, byteQueueIndex_t index); | ||
| 50 | |||
| 51 | //update the index in the queue to reflect data that has been dealt with | ||
| 52 | void bytequeue_remove(byteQueue_t * queue, byteQueueIndex_t numToRemove); | ||
| 53 | |||
| 54 | #ifdef __cplusplus | ||
| 55 | } | ||
| 56 | #endif | ||
| 57 | |||
| 58 | #endif | ||
| 59 | |||
diff --git a/tmk_core/protocol/midi/bytequeue/interrupt_setting.c b/tmk_core/protocol/midi/bytequeue/interrupt_setting.c new file mode 100755 index 000000000..eafef527c --- /dev/null +++ b/tmk_core/protocol/midi/bytequeue/interrupt_setting.c | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | //Copyright 20010 Alex Norman | ||
| 2 | //writen by Alex Norman | ||
| 3 | // | ||
| 4 | //This file is part of avr-bytequeue. | ||
| 5 | // | ||
| 6 | //avr-bytequeue 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-bytequeue 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-bytequeue. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | |||
| 20 | //AVR specific code | ||
| 21 | //should be able to port to other systems by simply providing chip specific | ||
| 22 | //implementations of the typedef and these functions | ||
| 23 | |||
| 24 | #include "interrupt_setting.h" | ||
| 25 | #include <avr/interrupt.h> | ||
| 26 | |||
| 27 | interrupt_setting_t store_and_clear_interrupt(void) { | ||
| 28 | uint8_t sreg = SREG; | ||
| 29 | cli(); | ||
| 30 | return sreg; | ||
| 31 | } | ||
| 32 | |||
| 33 | void restore_interrupt_setting(interrupt_setting_t setting) { | ||
| 34 | SREG = setting; | ||
| 35 | } | ||
| 36 | |||
diff --git a/tmk_core/protocol/midi/bytequeue/interrupt_setting.h b/tmk_core/protocol/midi/bytequeue/interrupt_setting.h new file mode 100755 index 000000000..053d02c9d --- /dev/null +++ b/tmk_core/protocol/midi/bytequeue/interrupt_setting.h | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | //Copyright 20010 Alex Norman | ||
| 2 | //writen by Alex Norman | ||
| 3 | // | ||
| 4 | //This file is part of avr-bytequeue. | ||
| 5 | // | ||
| 6 | //avr-bytequeue 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-bytequeue 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-bytequeue. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | #ifndef INTERRUPT_SETTING_H | ||
| 20 | #define INTERRUPT_SETTING_H | ||
| 21 | |||
| 22 | #ifdef __cplusplus | ||
| 23 | extern "C" { | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #include <inttypes.h> | ||
| 27 | |||
| 28 | //AVR specific typedef | ||
| 29 | typedef uint8_t interrupt_setting_t; | ||
| 30 | |||
| 31 | interrupt_setting_t store_and_clear_interrupt(void); | ||
| 32 | void restore_interrupt_setting(interrupt_setting_t setting); | ||
| 33 | |||
| 34 | #ifdef __cplusplus | ||
| 35 | } | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #endif | ||
| 39 | |||
diff --git a/tmk_core/protocol/midi/midi.c b/tmk_core/protocol/midi/midi.c new file mode 100755 index 000000000..11a589078 --- /dev/null +++ b/tmk_core/protocol/midi/midi.c | |||
| @@ -0,0 +1,277 @@ | |||
| 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 | #include "midi.h" | ||
| 20 | #include <string.h> //for memcpy | ||
| 21 | |||
| 22 | #define MIN(x,y) (((x) < (y)) ? (x) : (y)) | ||
| 23 | |||
| 24 | #ifndef NULL | ||
| 25 | #define NULL 0 | ||
| 26 | #endif | ||
| 27 | |||
| 28 | bool midi_is_statusbyte(uint8_t theByte){ | ||
| 29 | return (bool)(theByte & MIDI_STATUSMASK); | ||
| 30 | } | ||
| 31 | |||
| 32 | bool midi_is_realtime(uint8_t theByte){ | ||
| 33 | return (theByte >= MIDI_CLOCK); | ||
| 34 | } | ||
| 35 | |||
| 36 | midi_packet_length_t midi_packet_length(uint8_t status){ | ||
| 37 | switch(status & 0xF0){ | ||
| 38 | case MIDI_CC: | ||
| 39 | case MIDI_NOTEON: | ||
| 40 | case MIDI_NOTEOFF: | ||
| 41 | case MIDI_AFTERTOUCH: | ||
| 42 | case MIDI_PITCHBEND: | ||
| 43 | return THREE; | ||
| 44 | case MIDI_PROGCHANGE: | ||
| 45 | case MIDI_CHANPRESSURE: | ||
| 46 | case MIDI_SONGSELECT: | ||
| 47 | return TWO; | ||
| 48 | case 0xF0: | ||
| 49 | switch(status) { | ||
| 50 | case MIDI_CLOCK: | ||
| 51 | case MIDI_TICK: | ||
| 52 | case MIDI_START: | ||
| 53 | case MIDI_CONTINUE: | ||
| 54 | case MIDI_STOP: | ||
| 55 | case MIDI_ACTIVESENSE: | ||
| 56 | case MIDI_RESET: | ||
| 57 | case MIDI_TUNEREQUEST: | ||
| 58 | return ONE; | ||
| 59 | case MIDI_SONGPOSITION: | ||
| 60 | return THREE; | ||
| 61 | case MIDI_TC_QUARTERFRAME: | ||
| 62 | case MIDI_SONGSELECT: | ||
| 63 | return TWO; | ||
| 64 | case SYSEX_END: | ||
| 65 | case SYSEX_BEGIN: | ||
| 66 | default: | ||
| 67 | return UNDEFINED; | ||
| 68 | } | ||
| 69 | default: | ||
| 70 | return UNDEFINED; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val){ | ||
| 75 | //CC Status: 0xB0 to 0xBF where the low nibble is the MIDI channel. | ||
| 76 | //CC Data: Controller Num, Controller Val | ||
| 77 | device->send_func(device, 3, | ||
| 78 | MIDI_CC | (chan & MIDI_CHANMASK), | ||
| 79 | num & 0x7F, | ||
| 80 | val & 0x7F); | ||
| 81 | } | ||
| 82 | |||
| 83 | void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel){ | ||
| 84 | //Note Data: Note Num, Note Velocity | ||
| 85 | device->send_func(device, 3, | ||
| 86 | MIDI_NOTEON | (chan & MIDI_CHANMASK), | ||
| 87 | num & 0x7F, | ||
| 88 | vel & 0x7F); | ||
| 89 | } | ||
| 90 | |||
| 91 | void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel){ | ||
| 92 | //Note Data: Note Num, Note Velocity | ||
| 93 | device->send_func(device, 3, | ||
| 94 | MIDI_NOTEOFF | (chan & MIDI_CHANMASK), | ||
| 95 | num & 0x7F, | ||
| 96 | vel & 0x7F); | ||
| 97 | } | ||
| 98 | |||
| 99 | void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, uint8_t amt){ | ||
| 100 | device->send_func(device, 3, | ||
| 101 | MIDI_AFTERTOUCH | (chan & MIDI_CHANMASK), | ||
| 102 | note_num & 0x7F, | ||
| 103 | amt & 0x7F); | ||
| 104 | } | ||
| 105 | |||
| 106 | //XXX does this work right? | ||
| 107 | //amt in range -0x2000, 0x1fff | ||
| 108 | //uAmt should be in range.. | ||
| 109 | //0x0000 to 0x3FFF | ||
| 110 | void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt){ | ||
| 111 | uint16_t uAmt; | ||
| 112 | //check range | ||
| 113 | if(amt > 0x1fff){ | ||
| 114 | uAmt = 0x3FFF; | ||
| 115 | } else if(amt < -0x2000){ | ||
| 116 | uAmt = 0; | ||
| 117 | } else { | ||
| 118 | uAmt = amt + 0x2000; | ||
| 119 | } | ||
| 120 | device->send_func(device, 3, | ||
| 121 | MIDI_PITCHBEND | (chan & MIDI_CHANMASK), | ||
| 122 | uAmt & 0x7F, | ||
| 123 | (uAmt >> 7) & 0x7F); | ||
| 124 | } | ||
| 125 | |||
| 126 | void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num){ | ||
| 127 | device->send_func(device, 2, | ||
| 128 | MIDI_PROGCHANGE | (chan & MIDI_CHANMASK), | ||
| 129 | num & 0x7F, | ||
| 130 | 0); | ||
| 131 | } | ||
| 132 | |||
| 133 | void midi_send_channelpressure(MidiDevice * device, uint8_t chan, uint8_t amt){ | ||
| 134 | device->send_func(device, 2, | ||
| 135 | MIDI_CHANPRESSURE | (chan & MIDI_CHANMASK), | ||
| 136 | amt & 0x7F, | ||
| 137 | 0); | ||
| 138 | } | ||
| 139 | |||
| 140 | void midi_send_clock(MidiDevice * device){ | ||
| 141 | device->send_func(device, 1, MIDI_CLOCK, 0, 0); | ||
| 142 | } | ||
| 143 | |||
| 144 | void midi_send_tick(MidiDevice * device){ | ||
| 145 | device->send_func(device, 1, MIDI_TICK, 0, 0); | ||
| 146 | } | ||
| 147 | |||
| 148 | void midi_send_start(MidiDevice * device){ | ||
| 149 | device->send_func(device, 1, MIDI_START, 0, 0); | ||
| 150 | } | ||
| 151 | |||
| 152 | void midi_send_continue(MidiDevice * device){ | ||
| 153 | device->send_func(device, 1, MIDI_CONTINUE, 0, 0); | ||
| 154 | } | ||
| 155 | |||
| 156 | void midi_send_stop(MidiDevice * device){ | ||
| 157 | device->send_func(device, 1, MIDI_STOP, 0, 0); | ||
| 158 | } | ||
| 159 | |||
| 160 | void midi_send_activesense(MidiDevice * device){ | ||
| 161 | device->send_func(device, 1, MIDI_ACTIVESENSE, 0, 0); | ||
| 162 | } | ||
| 163 | |||
| 164 | void midi_send_reset(MidiDevice * device){ | ||
| 165 | device->send_func(device, 1, MIDI_RESET, 0, 0); | ||
| 166 | } | ||
| 167 | |||
| 168 | void midi_send_tcquarterframe(MidiDevice * device, uint8_t time){ | ||
| 169 | device->send_func(device, 2, | ||
| 170 | MIDI_TC_QUARTERFRAME, | ||
| 171 | time & 0x7F, | ||
| 172 | 0); | ||
| 173 | } | ||
| 174 | |||
| 175 | //XXX is this right? | ||
| 176 | void midi_send_songposition(MidiDevice * device, uint16_t pos){ | ||
| 177 | device->send_func(device, 3, | ||
| 178 | MIDI_SONGPOSITION, | ||
| 179 | pos & 0x7F, | ||
| 180 | (pos >> 7) & 0x7F); | ||
| 181 | } | ||
| 182 | |||
| 183 | void midi_send_songselect(MidiDevice * device, uint8_t song){ | ||
| 184 | device->send_func(device, 2, | ||
| 185 | MIDI_SONGSELECT, | ||
| 186 | song & 0x7F, | ||
| 187 | 0); | ||
| 188 | } | ||
| 189 | |||
| 190 | void midi_send_tunerequest(MidiDevice * device){ | ||
| 191 | device->send_func(device, 1, MIDI_TUNEREQUEST, 0, 0); | ||
| 192 | } | ||
| 193 | |||
| 194 | void midi_send_byte(MidiDevice * device, uint8_t b){ | ||
| 195 | device->send_func(device, 1, b, 0, 0); | ||
| 196 | } | ||
| 197 | |||
| 198 | void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2){ | ||
| 199 | //ensure that the count passed along is always 3 or lower | ||
| 200 | if (count > 3) { | ||
| 201 | //TODO how to do this correctly? | ||
| 202 | } | ||
| 203 | device->send_func(device, count, byte0, byte1, byte2); | ||
| 204 | } | ||
| 205 | |||
| 206 | void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array) { | ||
| 207 | uint16_t i; | ||
| 208 | for (i = 0; i < count; i += 3) { | ||
| 209 | uint8_t b[3] = { 0, 0, 0 }; | ||
| 210 | uint16_t to_send = count - i; | ||
| 211 | to_send = (to_send > 3) ? 3 : to_send; | ||
| 212 | memcpy(b, array + i, to_send); | ||
| 213 | midi_send_data(device, to_send, b[0], b[1], b[2]); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func){ | ||
| 219 | device->input_cc_callback = func; | ||
| 220 | } | ||
| 221 | |||
| 222 | void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t func){ | ||
| 223 | device->input_noteon_callback = func; | ||
| 224 | } | ||
| 225 | |||
| 226 | void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t func){ | ||
| 227 | device->input_noteoff_callback = func; | ||
| 228 | } | ||
| 229 | |||
| 230 | void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func_t func){ | ||
| 231 | device->input_aftertouch_callback = func; | ||
| 232 | } | ||
| 233 | |||
| 234 | void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_t func){ | ||
| 235 | device->input_pitchbend_callback = func; | ||
| 236 | } | ||
| 237 | |||
| 238 | void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_func_t func){ | ||
| 239 | device->input_songposition_callback = func; | ||
| 240 | } | ||
| 241 | |||
| 242 | void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t func) { | ||
| 243 | device->input_progchange_callback = func; | ||
| 244 | } | ||
| 245 | |||
| 246 | void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func_t func) { | ||
| 247 | device->input_chanpressure_callback = func; | ||
| 248 | } | ||
| 249 | |||
| 250 | void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t func) { | ||
| 251 | device->input_songselect_callback = func; | ||
| 252 | } | ||
| 253 | |||
| 254 | void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_func_t func) { | ||
| 255 | device->input_tc_quarterframe_callback = func; | ||
| 256 | } | ||
| 257 | |||
| 258 | void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t func){ | ||
| 259 | device->input_realtime_callback = func; | ||
| 260 | } | ||
| 261 | |||
| 262 | void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_t func){ | ||
| 263 | device->input_tunerequest_callback = func; | ||
| 264 | } | ||
| 265 | |||
| 266 | void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func) { | ||
| 267 | device->input_sysex_callback = func; | ||
| 268 | } | ||
| 269 | |||
| 270 | void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_t func){ | ||
| 271 | device->input_fallthrough_callback = func; | ||
| 272 | } | ||
| 273 | |||
| 274 | void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t func){ | ||
| 275 | device->input_catchall_callback = func; | ||
| 276 | } | ||
| 277 | |||
diff --git a/tmk_core/protocol/midi/midi.h b/tmk_core/protocol/midi/midi.h new file mode 100755 index 000000000..1a36737df --- /dev/null +++ b/tmk_core/protocol/midi/midi.h | |||
| @@ -0,0 +1,498 @@ | |||
| 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 | /** | ||
| 20 | * @file | ||
| 21 | * @brief The main midi functions | ||
| 22 | * | ||
| 23 | * This file includes all of the functions you need to set up and process a | ||
| 24 | * midi device, send midi, and register midi callbacks. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | #ifndef XNOR_MIDI_H | ||
| 29 | #define XNOR_MIDI_H | ||
| 30 | |||
| 31 | #ifdef __cplusplus | ||
| 32 | extern "C" { | ||
| 33 | #endif | ||
| 34 | |||
| 35 | #include "midi_device.h" | ||
| 36 | #include "midi_function_types.h" | ||
| 37 | |||
| 38 | /** | ||
| 39 | * @defgroup midi_device_setup_process Device initialization and processing | ||
| 40 | * @brief These are method that you must use to initialize and run a device | ||
| 41 | * | ||
| 42 | * @{ | ||
| 43 | */ | ||
| 44 | |||
| 45 | /** | ||
| 46 | * @brief Initialize a device | ||
| 47 | * | ||
| 48 | * You must call this before using the device in question. | ||
| 49 | * | ||
| 50 | * @param device the device to initialize | ||
| 51 | */ | ||
| 52 | void midi_device_init(MidiDevice * device); // [implementation in midi_device.c] | ||
| 53 | |||
| 54 | /** | ||
| 55 | * @brief Process input data | ||
| 56 | * | ||
| 57 | * This method drives the input processing, you must call this method frequently | ||
| 58 | * if you expect to have your input callbacks called. | ||
| 59 | * | ||
| 60 | * @param device the device to process | ||
| 61 | */ | ||
| 62 | void midi_device_process(MidiDevice * device); // [implementation in midi_device.c] | ||
| 63 | |||
| 64 | /**@}*/ | ||
| 65 | |||
| 66 | /** | ||
| 67 | * @defgroup send_functions Midi send functions | ||
| 68 | * @brief These are the functions you use to send midi data through a device. | ||
| 69 | * @{ | ||
| 70 | */ | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @brief Send a control change message (cc) via the given device. | ||
| 74 | * | ||
| 75 | * @param device the device to use for sending | ||
| 76 | * @param chan the channel to send on, 0-15 | ||
| 77 | * @param num the cc num | ||
| 78 | * @param val the value of that cc num | ||
| 79 | */ | ||
| 80 | void midi_send_cc(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t val); | ||
| 81 | |||
| 82 | /** | ||
| 83 | * @brief Send a note on message via the given device. | ||
| 84 | * | ||
| 85 | * @param device the device to use for sending | ||
| 86 | * @param chan the channel to send on, 0-15 | ||
| 87 | * @param num the note number | ||
| 88 | * @param vel the note velocity | ||
| 89 | */ | ||
| 90 | void midi_send_noteon(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel); | ||
| 91 | |||
| 92 | /** | ||
| 93 | * @brief Send a note off message via the given device. | ||
| 94 | * | ||
| 95 | * @param device the device to use for sending | ||
| 96 | * @param chan the channel to send on, 0-15 | ||
| 97 | * @param num the note number | ||
| 98 | * @param vel the note velocity | ||
| 99 | */ | ||
| 100 | void midi_send_noteoff(MidiDevice * device, uint8_t chan, uint8_t num, uint8_t vel); | ||
| 101 | |||
| 102 | /** | ||
| 103 | * @brief Send an after touch message via the given device. | ||
| 104 | * | ||
| 105 | * @param device the device to use for sending | ||
| 106 | * @param chan the channel to send on, 0-15 | ||
| 107 | * @param note_num the note number | ||
| 108 | * @param amt the after touch amount | ||
| 109 | */ | ||
| 110 | void midi_send_aftertouch(MidiDevice * device, uint8_t chan, uint8_t note_num, uint8_t amt); | ||
| 111 | |||
| 112 | /** | ||
| 113 | * @brief Send a pitch bend message via the given device. | ||
| 114 | * | ||
| 115 | * @param device the device to use for sending | ||
| 116 | * @param chan the channel to send on, 0-15 | ||
| 117 | * @param amt the bend amount range: -8192..8191, 0 means no bend | ||
| 118 | */ | ||
| 119 | void midi_send_pitchbend(MidiDevice * device, uint8_t chan, int16_t amt); //range -8192, 8191 | ||
| 120 | |||
| 121 | /** | ||
| 122 | * @brief Send a program change message via the given device. | ||
| 123 | * | ||
| 124 | * @param device the device to use for sending | ||
| 125 | * @param chan the channel to send on, 0-15 | ||
| 126 | * @param num the program to change to | ||
| 127 | */ | ||
| 128 | void midi_send_programchange(MidiDevice * device, uint8_t chan, uint8_t num); | ||
| 129 | |||
| 130 | /** | ||
| 131 | * @brief Send a channel pressure message via the given device. | ||
| 132 | * | ||
| 133 | * @param device the device to use for sending | ||
| 134 | * @param chan the channel to send on, 0-15 | ||
| 135 | * @param amt the amount of channel pressure | ||
| 136 | */ | ||
| 137 | void midi_send_channelpressure(MidiDevice * device, uint8_t chan, uint8_t amt); | ||
| 138 | |||
| 139 | /** | ||
| 140 | * @brief Send a clock message via the given device. | ||
| 141 | * | ||
| 142 | * @param device the device to use for sending | ||
| 143 | */ | ||
| 144 | void midi_send_clock(MidiDevice * device); | ||
| 145 | |||
| 146 | /** | ||
| 147 | * @brief Send a tick message via the given device. | ||
| 148 | * | ||
| 149 | * @param device the device to use for sending | ||
| 150 | */ | ||
| 151 | void midi_send_tick(MidiDevice * device); | ||
| 152 | |||
| 153 | /** | ||
| 154 | * @brief Send a start message via the given device. | ||
| 155 | * | ||
| 156 | * @param device the device to use for sending | ||
| 157 | */ | ||
| 158 | void midi_send_start(MidiDevice * device); | ||
| 159 | |||
| 160 | /** | ||
| 161 | * @brief Send a continue message via the given device. | ||
| 162 | * | ||
| 163 | * @param device the device to use for sending | ||
| 164 | */ | ||
| 165 | void midi_send_continue(MidiDevice * device); | ||
| 166 | |||
| 167 | /** | ||
| 168 | * @brief Send a stop message via the given device. | ||
| 169 | * | ||
| 170 | * @param device the device to use for sending | ||
| 171 | */ | ||
| 172 | void midi_send_stop(MidiDevice * device); | ||
| 173 | |||
| 174 | /** | ||
| 175 | * @brief Send an active sense message via the given device. | ||
| 176 | * | ||
| 177 | * @param device the device to use for sending | ||
| 178 | */ | ||
| 179 | void midi_send_activesense(MidiDevice * device); | ||
| 180 | |||
| 181 | /** | ||
| 182 | * @brief Send a reset message via the given device. | ||
| 183 | * | ||
| 184 | * @param device the device to use for sending | ||
| 185 | */ | ||
| 186 | void midi_send_reset(MidiDevice * device); | ||
| 187 | |||
| 188 | |||
| 189 | /** | ||
| 190 | * @brief Send a tc quarter frame message via the given device. | ||
| 191 | * | ||
| 192 | * @param device the device to use for sending | ||
| 193 | * @param time the time of this quarter frame, range 0..16383 | ||
| 194 | */ | ||
| 195 | void midi_send_tcquarterframe(MidiDevice * device, uint8_t time); | ||
| 196 | |||
| 197 | /** | ||
| 198 | * @brief Send a song position message via the given device. | ||
| 199 | * | ||
| 200 | * @param device the device to use for sending | ||
| 201 | * @param pos the song position | ||
| 202 | */ | ||
| 203 | void midi_send_songposition(MidiDevice * device, uint16_t pos); | ||
| 204 | |||
| 205 | /** | ||
| 206 | * @brief Send a song select message via the given device. | ||
| 207 | * | ||
| 208 | * @param device the device to use for sending | ||
| 209 | * @param song the song to select | ||
| 210 | */ | ||
| 211 | void midi_send_songselect(MidiDevice * device, uint8_t song); | ||
| 212 | |||
| 213 | /** | ||
| 214 | * @brief Send a tune request message via the given device. | ||
| 215 | * | ||
| 216 | * @param device the device to use for sending | ||
| 217 | */ | ||
| 218 | void midi_send_tunerequest(MidiDevice * device); | ||
| 219 | |||
| 220 | /** | ||
| 221 | * @brief Send a byte via the given device. | ||
| 222 | * | ||
| 223 | * This is a generic method for sending data via the given midi device. | ||
| 224 | * This would be useful for sending sysex data or messages that are not | ||
| 225 | * implemented in this API, if there are any. Please contact the author | ||
| 226 | * if you find some so we can add them. | ||
| 227 | * | ||
| 228 | * @param device the device to use for sending | ||
| 229 | * @param b the byte to send | ||
| 230 | */ | ||
| 231 | void midi_send_byte(MidiDevice * device, uint8_t b); | ||
| 232 | |||
| 233 | /** | ||
| 234 | * @brief Send up to 3 bytes of data | ||
| 235 | * | ||
| 236 | * % 4 is applied to count so that you can use this to pass sysex through | ||
| 237 | * | ||
| 238 | * @param device the device to use for sending | ||
| 239 | * @param count the count of bytes to send, %4 is applied | ||
| 240 | * @param byte0 the first byte | ||
| 241 | * @param byte1 the second byte, ignored if cnt % 4 != 2 | ||
| 242 | * @param byte2 the third byte, ignored if cnt % 4 != 3 | ||
| 243 | */ | ||
| 244 | void midi_send_data(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2); | ||
| 245 | |||
| 246 | /** | ||
| 247 | * @brief Send an array of formatted midi data. | ||
| 248 | * | ||
| 249 | * Can be used for sysex. | ||
| 250 | * | ||
| 251 | * @param device the device to use for sending | ||
| 252 | * @param count the count of bytes to send | ||
| 253 | * @param array the array of bytes | ||
| 254 | */ | ||
| 255 | void midi_send_array(MidiDevice * device, uint16_t count, uint8_t * array); | ||
| 256 | |||
| 257 | /**@}*/ | ||
| 258 | |||
| 259 | |||
| 260 | /** | ||
| 261 | * @defgroup input_callback_reg Input callback registration functions | ||
| 262 | * | ||
| 263 | * @brief These are the functions you use to register your input callbacks. | ||
| 264 | * | ||
| 265 | * The functions are called when the appropriate midi message is matched on the | ||
| 266 | * associated device's input. | ||
| 267 | * | ||
| 268 | * @{ | ||
| 269 | */ | ||
| 270 | |||
| 271 | //three byte funcs | ||
| 272 | |||
| 273 | /** | ||
| 274 | * @brief Register a control change message (cc) callback. | ||
| 275 | * | ||
| 276 | * @param device the device associate with | ||
| 277 | * @param func the callback function to register | ||
| 278 | */ | ||
| 279 | void midi_register_cc_callback(MidiDevice * device, midi_three_byte_func_t func); | ||
| 280 | |||
| 281 | /** | ||
| 282 | * @brief Register a note on callback. | ||
| 283 | * | ||
| 284 | * @param device the device associate with | ||
| 285 | * @param func the callback function to register | ||
| 286 | */ | ||
| 287 | void midi_register_noteon_callback(MidiDevice * device, midi_three_byte_func_t func); | ||
| 288 | |||
| 289 | /** | ||
| 290 | * @brief Register a note off callback. | ||
| 291 | * | ||
| 292 | * @param device the device associate with | ||
| 293 | * @param func the callback function to register | ||
| 294 | */ | ||
| 295 | void midi_register_noteoff_callback(MidiDevice * device, midi_three_byte_func_t func); | ||
| 296 | |||
| 297 | /** | ||
| 298 | * @brief Register an after touch callback. | ||
| 299 | * | ||
| 300 | * @param device the device associate with | ||
| 301 | * @param func the callback function to register | ||
| 302 | */ | ||
| 303 | |||
| 304 | void midi_register_aftertouch_callback(MidiDevice * device, midi_three_byte_func_t func); | ||
| 305 | |||
| 306 | /** | ||
| 307 | * @brief Register a pitch bend callback. | ||
| 308 | * | ||
| 309 | * @param device the device associate with | ||
| 310 | * @param func the callback function to register | ||
| 311 | */ | ||
| 312 | void midi_register_pitchbend_callback(MidiDevice * device, midi_three_byte_func_t func); | ||
| 313 | |||
| 314 | /** | ||
| 315 | * @brief Register a song position callback. | ||
| 316 | * | ||
| 317 | * @param device the device associate with | ||
| 318 | * @param func the callback function to register | ||
| 319 | */ | ||
| 320 | void midi_register_songposition_callback(MidiDevice * device, midi_three_byte_func_t func); | ||
| 321 | |||
| 322 | //two byte funcs | ||
| 323 | |||
| 324 | /** | ||
| 325 | * @brief Register a program change callback. | ||
| 326 | * | ||
| 327 | * @param device the device associate with | ||
| 328 | * @param func the callback function to register | ||
| 329 | */ | ||
| 330 | void midi_register_progchange_callback(MidiDevice * device, midi_two_byte_func_t func); | ||
| 331 | |||
| 332 | /** | ||
| 333 | * @brief Register a channel pressure callback. | ||
| 334 | * | ||
| 335 | * @param device the device associate with | ||
| 336 | * @param func the callback function to register | ||
| 337 | */ | ||
| 338 | void midi_register_chanpressure_callback(MidiDevice * device, midi_two_byte_func_t func); | ||
| 339 | |||
| 340 | /** | ||
| 341 | * @brief Register a song select callback. | ||
| 342 | * | ||
| 343 | * @param device the device associate with | ||
| 344 | * @param func the callback function to register | ||
| 345 | */ | ||
| 346 | void midi_register_songselect_callback(MidiDevice * device, midi_two_byte_func_t func); | ||
| 347 | |||
| 348 | /** | ||
| 349 | * @brief Register a tc quarter frame callback. | ||
| 350 | * | ||
| 351 | * @param device the device associate with | ||
| 352 | * @param func the callback function to register | ||
| 353 | */ | ||
| 354 | void midi_register_tc_quarterframe_callback(MidiDevice * device, midi_two_byte_func_t func); | ||
| 355 | |||
| 356 | //one byte funcs | ||
| 357 | |||
| 358 | /** | ||
| 359 | * @brief Register a realtime callback. | ||
| 360 | * | ||
| 361 | * The callback will be called for all of the real time message types. | ||
| 362 | * | ||
| 363 | * @param device the device associate with | ||
| 364 | * @param func the callback function to register | ||
| 365 | */ | ||
| 366 | void midi_register_realtime_callback(MidiDevice * device, midi_one_byte_func_t func); | ||
| 367 | |||
| 368 | /** | ||
| 369 | * @brief Register a tune request callback. | ||
| 370 | * | ||
| 371 | * @param device the device associate with | ||
| 372 | * @param func the callback function to register | ||
| 373 | */ | ||
| 374 | void midi_register_tunerequest_callback(MidiDevice * device, midi_one_byte_func_t func); | ||
| 375 | |||
| 376 | /** | ||
| 377 | * @brief Register a sysex callback. | ||
| 378 | * | ||
| 379 | * @param device the device associate with | ||
| 380 | * @param func the callback function to register | ||
| 381 | */ | ||
| 382 | void midi_register_sysex_callback(MidiDevice * device, midi_sysex_func_t func); | ||
| 383 | |||
| 384 | /** | ||
| 385 | * @brief Register fall through callback. | ||
| 386 | * | ||
| 387 | * This is only called if a more specific callback is not matched and called. | ||
| 388 | * For instance, if you don't register a note on callback but you get a note on message | ||
| 389 | * the fall through callback will be called, if it is registered. | ||
| 390 | * | ||
| 391 | * @param device the device associate with | ||
| 392 | * @param func the callback function to register | ||
| 393 | */ | ||
| 394 | void midi_register_fallthrough_callback(MidiDevice * device, midi_var_byte_func_t func); | ||
| 395 | |||
| 396 | |||
| 397 | /** | ||
| 398 | * @brief Register a catch all callback. | ||
| 399 | * | ||
| 400 | * If registered, the catch all callback is called for every message that is | ||
| 401 | * matched, even if a more specific or the fallthrough callback is registered. | ||
| 402 | * | ||
| 403 | * @param device the device associate with | ||
| 404 | * @param func the callback function to register | ||
| 405 | */ | ||
| 406 | void midi_register_catchall_callback(MidiDevice * device, midi_var_byte_func_t func); | ||
| 407 | |||
| 408 | /**@}*/ | ||
| 409 | |||
| 410 | /** | ||
| 411 | * @defgroup midi_util Device independent utility functions. | ||
| 412 | * @{ | ||
| 413 | */ | ||
| 414 | |||
| 415 | /** | ||
| 416 | * \enum midi_packet_length_t | ||
| 417 | * | ||
| 418 | * An enumeration of the possible packet length values. | ||
| 419 | */ | ||
| 420 | typedef enum { | ||
| 421 | UNDEFINED = 0, | ||
| 422 | ONE = 1, | ||
| 423 | TWO = 2, | ||
| 424 | THREE = 3} midi_packet_length_t; | ||
| 425 | |||
| 426 | /** | ||
| 427 | * @brief Test to see if the byte given is a status byte | ||
| 428 | * @param theByte the byte to test | ||
| 429 | * @return true if the byte given is a midi status byte | ||
| 430 | */ | ||
| 431 | bool midi_is_statusbyte(uint8_t theByte); | ||
| 432 | |||
| 433 | /** | ||
| 434 | * @brief Test to see if the byte given is a realtime message | ||
| 435 | * @param theByte the byte to test | ||
| 436 | * @return true if it is a realtime message, false otherwise | ||
| 437 | */ | ||
| 438 | bool midi_is_realtime(uint8_t theByte); | ||
| 439 | |||
| 440 | /** | ||
| 441 | * @brief Find the length of the packet associated with the status byte given | ||
| 442 | * @param status the status byte | ||
| 443 | * @return the length of the packet, will return UNDEFINED if the byte is not | ||
| 444 | * a status byte or if it is a sysex status byte | ||
| 445 | */ | ||
| 446 | midi_packet_length_t midi_packet_length(uint8_t status); | ||
| 447 | |||
| 448 | /**@}*/ | ||
| 449 | |||
| 450 | /** | ||
| 451 | * @defgroup defines Midi status and miscellaneous utility #defines | ||
| 452 | * | ||
| 453 | * @{ | ||
| 454 | */ | ||
| 455 | |||
| 456 | #define SYSEX_BEGIN 0xF0 | ||
| 457 | #define SYSEX_END 0xF7 | ||
| 458 | |||
| 459 | //if you and this with a byte and you get anything non-zero | ||
| 460 | //it is a status message | ||
| 461 | #define MIDI_STATUSMASK 0x80 | ||
| 462 | //if you and this with a status message that contains channel info, | ||
| 463 | //you'll get the channel | ||
| 464 | #define MIDI_CHANMASK 0x0F | ||
| 465 | |||
| 466 | #define MIDI_CC 0xB0 | ||
| 467 | #define MIDI_NOTEON 0x90 | ||
| 468 | #define MIDI_NOTEOFF 0x80 | ||
| 469 | #define MIDI_AFTERTOUCH 0xA0 | ||
| 470 | #define MIDI_PITCHBEND 0xE0 | ||
| 471 | #define MIDI_PROGCHANGE 0xC0 | ||
| 472 | #define MIDI_CHANPRESSURE 0xD0 | ||
| 473 | |||
| 474 | //midi realtime | ||
| 475 | #define MIDI_CLOCK 0xF8 | ||
| 476 | #define MIDI_TICK 0xF9 | ||
| 477 | #define MIDI_START 0xFA | ||
| 478 | #define MIDI_CONTINUE 0xFB | ||
| 479 | #define MIDI_STOP 0xFC | ||
| 480 | #define MIDI_ACTIVESENSE 0xFE | ||
| 481 | #define MIDI_RESET 0xFF | ||
| 482 | |||
| 483 | #define MIDI_TC_QUARTERFRAME 0xF1 | ||
| 484 | #define MIDI_SONGPOSITION 0xF2 | ||
| 485 | #define MIDI_SONGSELECT 0xF3 | ||
| 486 | #define MIDI_TUNEREQUEST 0xF6 | ||
| 487 | |||
| 488 | //This ID is for educational or development use only | ||
| 489 | #define SYSEX_EDUMANUFID 0x7D | ||
| 490 | |||
| 491 | /**@}*/ | ||
| 492 | |||
| 493 | #ifdef __cplusplus | ||
| 494 | } | ||
| 495 | #endif | ||
| 496 | |||
| 497 | #endif | ||
| 498 | |||
diff --git a/tmk_core/protocol/midi/midi_device.c b/tmk_core/protocol/midi/midi_device.c new file mode 100755 index 000000000..3215a007d --- /dev/null +++ b/tmk_core/protocol/midi/midi_device.c | |||
| @@ -0,0 +1,291 @@ | |||
| 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 | #include "midi_device.h" | ||
| 20 | #include "midi.h" | ||
| 21 | |||
| 22 | #ifndef NULL | ||
| 23 | #define NULL 0 | ||
| 24 | #endif | ||
| 25 | |||
| 26 | //forward declarations, internally used to call the callbacks | ||
| 27 | void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2); | ||
| 28 | void midi_process_byte(MidiDevice * device, uint8_t input); | ||
| 29 | |||
| 30 | void midi_device_init(MidiDevice * device){ | ||
| 31 | device->input_state = IDLE; | ||
| 32 | device->input_count = 0; | ||
| 33 | bytequeue_init(&device->input_queue, device->input_queue_data, MIDI_INPUT_QUEUE_LENGTH); | ||
| 34 | |||
| 35 | //three byte funcs | ||
| 36 | device->input_cc_callback = NULL; | ||
| 37 | device->input_noteon_callback = NULL; | ||
| 38 | device->input_noteoff_callback = NULL; | ||
| 39 | device->input_aftertouch_callback = NULL; | ||
| 40 | device->input_pitchbend_callback = NULL; | ||
| 41 | device->input_songposition_callback = NULL; | ||
| 42 | |||
| 43 | //two byte funcs | ||
| 44 | device->input_progchange_callback = NULL; | ||
| 45 | device->input_chanpressure_callback = NULL; | ||
| 46 | device->input_songselect_callback = NULL; | ||
| 47 | device->input_tc_quarterframe_callback = NULL; | ||
| 48 | |||
| 49 | //one byte funcs | ||
| 50 | device->input_realtime_callback = NULL; | ||
| 51 | device->input_tunerequest_callback = NULL; | ||
| 52 | |||
| 53 | //var byte functions | ||
| 54 | device->input_sysex_callback = NULL; | ||
| 55 | device->input_fallthrough_callback = NULL; | ||
| 56 | device->input_catchall_callback = NULL; | ||
| 57 | |||
| 58 | device->pre_input_process_callback = NULL; | ||
| 59 | } | ||
| 60 | |||
| 61 | void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input) { | ||
| 62 | uint8_t i; | ||
| 63 | for (i = 0; i < cnt; i++) | ||
| 64 | bytequeue_enqueue(&device->input_queue, input[i]); | ||
| 65 | } | ||
| 66 | |||
| 67 | void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func){ | ||
| 68 | device->send_func = send_func; | ||
| 69 | } | ||
| 70 | |||
| 71 | void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func){ | ||
| 72 | device->pre_input_process_callback = pre_process_func; | ||
| 73 | } | ||
| 74 | |||
| 75 | void midi_device_process(MidiDevice * device) { | ||
| 76 | //call the pre_input_process_callback if there is one | ||
| 77 | if(device->pre_input_process_callback) | ||
| 78 | device->pre_input_process_callback(device); | ||
| 79 | |||
| 80 | //pull stuff off the queue and process | ||
| 81 | byteQueueIndex_t len = bytequeue_length(&device->input_queue); | ||
| 82 | uint16_t i; | ||
| 83 | //TODO limit number of bytes processed? | ||
| 84 | for(i = 0; i < len; i++) { | ||
| 85 | uint8_t val = bytequeue_get(&device->input_queue, 0); | ||
| 86 | midi_process_byte(device, val); | ||
| 87 | bytequeue_remove(&device->input_queue, 1); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | void midi_process_byte(MidiDevice * device, uint8_t input) { | ||
| 92 | if (midi_is_realtime(input)) { | ||
| 93 | //call callback, store and restore state | ||
| 94 | input_state_t state = device->input_state; | ||
| 95 | device->input_state = ONE_BYTE_MESSAGE; | ||
| 96 | midi_input_callbacks(device, 1, input, 0, 0); | ||
| 97 | device->input_state = state; | ||
| 98 | } else if (midi_is_statusbyte(input)) { | ||
| 99 | //store the byte | ||
| 100 | if (device->input_state != SYSEX_MESSAGE) { | ||
| 101 | device->input_buffer[0] = input; | ||
| 102 | device->input_count = 1; | ||
| 103 | } | ||
| 104 | switch (midi_packet_length(input)) { | ||
| 105 | case ONE: | ||
| 106 | device->input_state = ONE_BYTE_MESSAGE;; | ||
| 107 | midi_input_callbacks(device, 1, input, 0, 0); | ||
| 108 | device->input_state = IDLE; | ||
| 109 | break; | ||
| 110 | case TWO: | ||
| 111 | device->input_state = TWO_BYTE_MESSAGE; | ||
| 112 | break; | ||
| 113 | case THREE: | ||
| 114 | device->input_state = THREE_BYTE_MESSAGE; | ||
| 115 | break; | ||
| 116 | case UNDEFINED: | ||
| 117 | switch(input) { | ||
| 118 | case SYSEX_BEGIN: | ||
| 119 | device->input_state = SYSEX_MESSAGE; | ||
| 120 | device->input_buffer[0] = input; | ||
| 121 | device->input_count = 1; | ||
| 122 | break; | ||
| 123 | case SYSEX_END: | ||
| 124 | //send what is left in the input buffer, set idle | ||
| 125 | device->input_buffer[device->input_count % 3] = input; | ||
| 126 | device->input_count += 1; | ||
| 127 | //call the callback | ||
| 128 | midi_input_callbacks(device, device->input_count, | ||
| 129 | device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); | ||
| 130 | device->input_state = IDLE; | ||
| 131 | break; | ||
| 132 | default: | ||
| 133 | device->input_state = IDLE; | ||
| 134 | device->input_count = 0; | ||
| 135 | } | ||
| 136 | |||
| 137 | break; | ||
| 138 | default: | ||
| 139 | device->input_state = IDLE; | ||
| 140 | device->input_count = 0; | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | } else { | ||
| 144 | if (device->input_state != IDLE) { | ||
| 145 | //store the byte | ||
| 146 | device->input_buffer[device->input_count % 3] = input; | ||
| 147 | //increment count | ||
| 148 | uint16_t prev = device->input_count; | ||
| 149 | device->input_count += 1; | ||
| 150 | |||
| 151 | switch(prev % 3) { | ||
| 152 | case 2: | ||
| 153 | //call callback | ||
| 154 | midi_input_callbacks(device, device->input_count, | ||
| 155 | device->input_buffer[0], device->input_buffer[1], device->input_buffer[2]); | ||
| 156 | if (device->input_state != SYSEX_MESSAGE) { | ||
| 157 | //set to 1, keeping status byte, allowing for running status | ||
| 158 | device->input_count = 1; | ||
| 159 | } | ||
| 160 | break; | ||
| 161 | case 1: | ||
| 162 | if (device->input_state == TWO_BYTE_MESSAGE) { | ||
| 163 | //call callback | ||
| 164 | midi_input_callbacks(device, device->input_count, | ||
| 165 | device->input_buffer[0], device->input_buffer[1], 0); | ||
| 166 | if (device->input_state != SYSEX_MESSAGE) { | ||
| 167 | //set to 1, keeping status byte, allowing for running status | ||
| 168 | device->input_count = 1; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | break; | ||
| 172 | case 0: | ||
| 173 | default: | ||
| 174 | //one byte messages are dealt with directly | ||
| 175 | break; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | void midi_input_callbacks(MidiDevice * device, uint16_t cnt, uint8_t byte0, uint8_t byte1, uint8_t byte2) { | ||
| 182 | //did we end up calling a callback? | ||
| 183 | bool called = false; | ||
| 184 | if (device->input_state == SYSEX_MESSAGE) { | ||
| 185 | if (device->input_sysex_callback) { | ||
| 186 | const uint16_t start = ((cnt - 1) / 3) * 3; | ||
| 187 | const uint8_t length = (cnt - start); | ||
| 188 | uint8_t data[3]; | ||
| 189 | data[0] = byte0; | ||
| 190 | data[1] = byte1; | ||
| 191 | data[2] = byte2; | ||
| 192 | device->input_sysex_callback(device, start, length, data); | ||
| 193 | called = true; | ||
| 194 | } | ||
| 195 | } else { | ||
| 196 | switch (cnt) { | ||
| 197 | case 3: | ||
| 198 | { | ||
| 199 | midi_three_byte_func_t func = NULL; | ||
| 200 | switch (byte0 & 0xF0) { | ||
| 201 | case MIDI_CC: | ||
| 202 | func = device->input_cc_callback; | ||
| 203 | break; | ||
| 204 | case MIDI_NOTEON: | ||
| 205 | func = device->input_noteon_callback; | ||
| 206 | break; | ||
| 207 | case MIDI_NOTEOFF: | ||
| 208 | func = device->input_noteoff_callback; | ||
| 209 | break; | ||
| 210 | case MIDI_AFTERTOUCH: | ||
| 211 | func = device->input_aftertouch_callback; | ||
| 212 | break; | ||
| 213 | case MIDI_PITCHBEND: | ||
| 214 | func = device->input_pitchbend_callback; | ||
| 215 | break; | ||
| 216 | case 0xF0: | ||
| 217 | if (byte0 == MIDI_SONGPOSITION) | ||
| 218 | func = device->input_songposition_callback; | ||
| 219 | break; | ||
| 220 | default: | ||
| 221 | break; | ||
| 222 | } | ||
| 223 | if(func) { | ||
| 224 | //mask off the channel for non song position functions | ||
| 225 | if (byte0 == MIDI_SONGPOSITION) | ||
| 226 | func(device, byte0, byte1, byte2); | ||
| 227 | else | ||
| 228 | func(device, byte0 & 0x0F, byte1, byte2); | ||
| 229 | called = true; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | break; | ||
| 233 | case 2: | ||
| 234 | { | ||
| 235 | midi_two_byte_func_t func = NULL; | ||
| 236 | switch (byte0 & 0xF0) { | ||
| 237 | case MIDI_PROGCHANGE: | ||
| 238 | func = device->input_progchange_callback; | ||
| 239 | break; | ||
| 240 | case MIDI_CHANPRESSURE: | ||
| 241 | func = device->input_chanpressure_callback; | ||
| 242 | break; | ||
| 243 | case 0xF0: | ||
| 244 | if (byte0 == MIDI_SONGSELECT) | ||
| 245 | func = device->input_songselect_callback; | ||
| 246 | else if (byte0 == MIDI_TC_QUARTERFRAME) | ||
| 247 | func = device->input_tc_quarterframe_callback; | ||
| 248 | break; | ||
| 249 | default: | ||
| 250 | break; | ||
| 251 | } | ||
| 252 | if(func) { | ||
| 253 | //mask off the channel | ||
| 254 | if (byte0 == MIDI_SONGSELECT || byte0 == MIDI_TC_QUARTERFRAME) | ||
| 255 | func(device, byte0, byte1); | ||
| 256 | else | ||
| 257 | func(device, byte0 & 0x0F, byte1); | ||
| 258 | called = true; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | break; | ||
| 262 | case 1: | ||
| 263 | { | ||
| 264 | midi_one_byte_func_t func = NULL; | ||
| 265 | if (midi_is_realtime(byte0)) | ||
| 266 | func = device->input_realtime_callback; | ||
| 267 | else if (byte0 == MIDI_TUNEREQUEST) | ||
| 268 | func = device->input_tunerequest_callback; | ||
| 269 | if (func) { | ||
| 270 | func(device, byte0); | ||
| 271 | called = true; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | break; | ||
| 275 | default: | ||
| 276 | //just in case | ||
| 277 | if (cnt > 3) | ||
| 278 | cnt = 0; | ||
| 279 | break; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | //if there is fallthrough default callback and we haven't called a more specific one, | ||
| 284 | //call the fallthrough | ||
| 285 | if (!called && device->input_fallthrough_callback) | ||
| 286 | device->input_fallthrough_callback(device, cnt, byte0, byte1, byte2); | ||
| 287 | //always call the catch all if it exists | ||
| 288 | if (device->input_catchall_callback) | ||
| 289 | device->input_catchall_callback(device, cnt, byte0, byte1, byte2); | ||
| 290 | } | ||
| 291 | |||
diff --git a/tmk_core/protocol/midi/midi_device.h b/tmk_core/protocol/midi/midi_device.h new file mode 100755 index 000000000..088995286 --- /dev/null +++ b/tmk_core/protocol/midi/midi_device.h | |||
| @@ -0,0 +1,156 @@ | |||
| 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 | /** | ||
| 20 | * @file | ||
| 21 | * @brief Device implementation functions | ||
| 22 | */ | ||
| 23 | |||
| 24 | #ifndef MIDI_DEVICE_H | ||
| 25 | #define MIDI_DEVICE_H | ||
| 26 | |||
| 27 | #ifdef __cplusplus | ||
| 28 | extern "C" { | ||
| 29 | #endif | ||
| 30 | |||
| 31 | /** | ||
| 32 | * @defgroup midi_device Functions used when implementing your own midi device. | ||
| 33 | * | ||
| 34 | * You use the functions when you are implementing your own midi device. | ||
| 35 | * | ||
| 36 | * You set a send function to actually send bytes via your device, this method | ||
| 37 | * is called when you call a send function with this device, for instance | ||
| 38 | * midi_send_cc | ||
| 39 | * | ||
| 40 | * You use the midi_device_input to process input data from the device and pass | ||
| 41 | * it through the device's associated callbacks. | ||
| 42 | * | ||
| 43 | * You use the midi_device_set_pre_input_process_func if you want to have a | ||
| 44 | * function called at the beginning of the device's process function, generally | ||
| 45 | * to poll for input and pass that into midi_device_input | ||
| 46 | * | ||
| 47 | * @{ | ||
| 48 | */ | ||
| 49 | |||
| 50 | #include "midi_function_types.h" | ||
| 51 | #include "bytequeue/bytequeue.h" | ||
| 52 | #define MIDI_INPUT_QUEUE_LENGTH 192 | ||
| 53 | |||
| 54 | typedef enum { | ||
| 55 | IDLE, | ||
| 56 | ONE_BYTE_MESSAGE = 1, | ||
| 57 | TWO_BYTE_MESSAGE = 2, | ||
| 58 | THREE_BYTE_MESSAGE = 3, | ||
| 59 | SYSEX_MESSAGE} input_state_t; | ||
| 60 | |||
| 61 | typedef void (* midi_no_byte_func_t)(MidiDevice * device); | ||
| 62 | |||
| 63 | /** | ||
| 64 | * \struct _midi_device | ||
| 65 | * | ||
| 66 | * @brief This structure represents the input and output functions and | ||
| 67 | * processing data for a midi device. | ||
| 68 | * | ||
| 69 | * A device can represent an actual physical device [serial port, usb port] or | ||
| 70 | * something virtual. | ||
| 71 | * You should not need to modify this structure directly. | ||
| 72 | */ | ||
| 73 | struct _midi_device { | ||
| 74 | //output send function | ||
| 75 | midi_var_byte_func_t send_func; | ||
| 76 | |||
| 77 | //********input callbacks | ||
| 78 | //three byte funcs | ||
| 79 | midi_three_byte_func_t input_cc_callback; | ||
| 80 | midi_three_byte_func_t input_noteon_callback; | ||
| 81 | midi_three_byte_func_t input_noteoff_callback; | ||
| 82 | midi_three_byte_func_t input_aftertouch_callback; | ||
| 83 | midi_three_byte_func_t input_pitchbend_callback; | ||
| 84 | midi_three_byte_func_t input_songposition_callback; | ||
| 85 | //two byte funcs | ||
| 86 | midi_two_byte_func_t input_progchange_callback; | ||
| 87 | midi_two_byte_func_t input_chanpressure_callback; | ||
| 88 | midi_two_byte_func_t input_songselect_callback; | ||
| 89 | midi_two_byte_func_t input_tc_quarterframe_callback; | ||
| 90 | //one byte funcs | ||
| 91 | midi_one_byte_func_t input_realtime_callback; | ||
| 92 | midi_one_byte_func_t input_tunerequest_callback; | ||
| 93 | |||
| 94 | //sysex | ||
| 95 | midi_sysex_func_t input_sysex_callback; | ||
| 96 | |||
| 97 | //only called if more specific callback is not matched | ||
| 98 | midi_var_byte_func_t input_fallthrough_callback; | ||
| 99 | //called if registered, independent of other callbacks | ||
| 100 | midi_var_byte_func_t input_catchall_callback; | ||
| 101 | |||
| 102 | //pre input processing function | ||
| 103 | midi_no_byte_func_t pre_input_process_callback; | ||
| 104 | |||
| 105 | //for internal input processing | ||
| 106 | uint8_t input_buffer[3]; | ||
| 107 | input_state_t input_state; | ||
| 108 | uint16_t input_count; | ||
| 109 | |||
| 110 | //for queueing data between the input and the processing functions | ||
| 111 | uint8_t input_queue_data[MIDI_INPUT_QUEUE_LENGTH]; | ||
| 112 | byteQueue_t input_queue; | ||
| 113 | }; | ||
| 114 | |||
| 115 | /** | ||
| 116 | * @brief Process input bytes. This function parses bytes and calls the | ||
| 117 | * appropriate callbacks associated with the given device. You use this | ||
| 118 | * function if you are creating a custom device and you want to have midi | ||
| 119 | * input. | ||
| 120 | * | ||
| 121 | * @param device the midi device to associate the input with | ||
| 122 | * @param cnt the number of bytes you are processing | ||
| 123 | * @param input the bytes to process | ||
| 124 | */ | ||
| 125 | void midi_device_input(MidiDevice * device, uint8_t cnt, uint8_t * input); | ||
| 126 | |||
| 127 | /** | ||
| 128 | * @brief Set the callback function that will be used for sending output | ||
| 129 | * data bytes. This is only used if you're creating a custom device. | ||
| 130 | * You'll most likely want the callback function to disable interrupts so | ||
| 131 | * that you can call the various midi send functions without worrying about | ||
| 132 | * locking. | ||
| 133 | * | ||
| 134 | * \param device the midi device to associate this callback with | ||
| 135 | * \param send_func the callback function that will do the sending | ||
| 136 | */ | ||
| 137 | void midi_device_set_send_func(MidiDevice * device, midi_var_byte_func_t send_func); | ||
| 138 | |||
| 139 | /** | ||
| 140 | * @brief Set a callback which is called at the beginning of the | ||
| 141 | * midi_device_process call. This can be used to poll for input | ||
| 142 | * data and send the data through the midi_device_input function. | ||
| 143 | * You'll probably only use this if you're creating a custom device. | ||
| 144 | * | ||
| 145 | * \param device the midi device to associate this callback with | ||
| 146 | * \param midi_no_byte_func_t the actual callback function | ||
| 147 | */ | ||
| 148 | void midi_device_set_pre_input_process_func(MidiDevice * device, midi_no_byte_func_t pre_process_func); | ||
| 149 | |||
| 150 | /**@}*/ | ||
| 151 | |||
| 152 | #ifdef __cplusplus | ||
| 153 | } | ||
| 154 | #endif | ||
| 155 | |||
| 156 | #endif | ||
diff --git a/tmk_core/protocol/midi/midi_function_types.h b/tmk_core/protocol/midi/midi_function_types.h new file mode 100755 index 000000000..35c4601b2 --- /dev/null +++ b/tmk_core/protocol/midi/midi_function_types.h | |||
| @@ -0,0 +1,50 @@ | |||
| 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 | /** | ||
| 20 | * @file | ||
| 21 | * @brief Function signature definitions | ||
| 22 | */ | ||
| 23 | |||
| 24 | #ifndef MIDI_FUNCTION_TYPES_H | ||
| 25 | #define MIDI_FUNCTION_TYPES_H | ||
| 26 | |||
| 27 | #ifdef __cplusplus | ||
| 28 | extern "C" { | ||
| 29 | #endif | ||
| 30 | |||
| 31 | #include <inttypes.h> | ||
| 32 | #include <stdbool.h> | ||
| 33 | |||
| 34 | //forward declaration | ||
| 35 | typedef struct _midi_device MidiDevice; | ||
| 36 | |||
| 37 | typedef void (* midi_one_byte_func_t)(MidiDevice * device, uint8_t byte); | ||
| 38 | typedef void (* midi_two_byte_func_t)(MidiDevice * device, uint8_t byte0, uint8_t byte1); | ||
| 39 | typedef void (* midi_three_byte_func_t)(MidiDevice * device, uint8_t byte0, uint8_t byte1, uint8_t byte2); | ||
| 40 | //all bytes after count bytes should be ignored | ||
| 41 | typedef void (* midi_var_byte_func_t)(MidiDevice * device, uint16_t count, uint8_t byte0, uint8_t byte1, uint8_t byte2); | ||
| 42 | |||
| 43 | //the start byte tells you how far into the sysex message you are, the data_length tells you how many bytes data is | ||
| 44 | typedef void (* midi_sysex_func_t)(MidiDevice * device, uint16_t start_byte, uint8_t data_length, uint8_t *data); | ||
| 45 | |||
| 46 | #ifdef __cplusplus | ||
| 47 | } | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #endif | ||
diff --git a/tmk_core/protocol/midi/sysex_tools.c b/tmk_core/protocol/midi/sysex_tools.c new file mode 100755 index 000000000..7563a3e2a --- /dev/null +++ b/tmk_core/protocol/midi/sysex_tools.c | |||
| @@ -0,0 +1,99 @@ | |||
| 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 | #include "sysex_tools.h" | ||
| 20 | |||
| 21 | uint16_t sysex_encoded_length(uint16_t decoded_length){ | ||
| 22 | uint8_t remainder = decoded_length % 7; | ||
| 23 | if (remainder) | ||
| 24 | return (decoded_length / 7) * 8 + remainder + 1; | ||
| 25 | else | ||
| 26 | return (decoded_length / 7) * 8; | ||
| 27 | } | ||
| 28 | |||
| 29 | uint16_t sysex_decoded_length(uint16_t encoded_length){ | ||
| 30 | uint8_t remainder = encoded_length % 8; | ||
| 31 | if (remainder) | ||
| 32 | return (encoded_length / 8) * 7 + remainder - 1; | ||
| 33 | else | ||
| 34 | return (encoded_length / 8) * 7; | ||
| 35 | } | ||
| 36 | |||
| 37 | uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, const uint16_t length){ | ||
| 38 | uint16_t encoded_full = length / 7; //number of full 8 byte sections from 7 bytes of input | ||
| 39 | uint16_t i,j; | ||
| 40 | |||
| 41 | //fill out the fully encoded sections | ||
| 42 | for(i = 0; i < encoded_full; i++) { | ||
| 43 | uint16_t encoded_msb_idx = i * 8; | ||
| 44 | uint16_t input_start_idx = i * 7; | ||
| 45 | encoded[encoded_msb_idx] = 0; | ||
| 46 | for(j = 0; j < 7; j++){ | ||
| 47 | uint8_t current = source[input_start_idx + j]; | ||
| 48 | encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j); | ||
| 49 | encoded[encoded_msb_idx + 1 + j] = 0x7F & current; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | //fill out the rest if there is any more | ||
| 54 | uint8_t remainder = length % 7; | ||
| 55 | if (remainder) { | ||
| 56 | uint16_t encoded_msb_idx = encoded_full * 8; | ||
| 57 | uint16_t input_start_idx = encoded_full * 7; | ||
| 58 | encoded[encoded_msb_idx] = 0; | ||
| 59 | for(j = 0; j < remainder; j++){ | ||
| 60 | uint8_t current = source[input_start_idx + j]; | ||
| 61 | encoded[encoded_msb_idx] |= (0x80 & current) >> (1 + j); | ||
| 62 | encoded[encoded_msb_idx + 1 + j] = 0x7F & current; | ||
| 63 | } | ||
| 64 | return encoded_msb_idx + remainder + 1; | ||
| 65 | } else { | ||
| 66 | return encoded_full * 8; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, const uint16_t length){ | ||
| 71 | uint16_t decoded_full = length / 8; | ||
| 72 | uint16_t i,j; | ||
| 73 | |||
| 74 | if (length < 2) | ||
| 75 | return 0; | ||
| 76 | |||
| 77 | //fill out the fully encoded sections | ||
| 78 | for(i = 0; i < decoded_full; i++) { | ||
| 79 | uint16_t encoded_msb_idx = i * 8; | ||
| 80 | uint16_t output_start_index = i * 7; | ||
| 81 | for(j = 0; j < 7; j++){ | ||
| 82 | decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1]; | ||
| 83 | decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j))); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | uint8_t remainder = length % 8; | ||
| 87 | if (remainder) { | ||
| 88 | uint16_t encoded_msb_idx = decoded_full * 8; | ||
| 89 | uint16_t output_start_index = decoded_full * 7; | ||
| 90 | for(j = 0; j < (remainder - 1); j++) { | ||
| 91 | decoded[output_start_index + j] = 0x7F & source[encoded_msb_idx + j + 1]; | ||
| 92 | decoded[output_start_index + j] |= (0x80 & (source[encoded_msb_idx] << (1 + j))); | ||
| 93 | } | ||
| 94 | return decoded_full * 7 + remainder - 1; | ||
| 95 | } else { | ||
| 96 | return decoded_full * 7; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
diff --git a/tmk_core/protocol/midi/sysex_tools.h b/tmk_core/protocol/midi/sysex_tools.h new file mode 100755 index 000000000..3654d0114 --- /dev/null +++ b/tmk_core/protocol/midi/sysex_tools.h | |||
| @@ -0,0 +1,95 @@ | |||
| 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 | #ifndef SYSEX_TOOLS_H | ||
| 20 | #define SYSEX_TOOLS_H | ||
| 21 | |||
| 22 | #ifdef __cplusplus | ||
| 23 | extern "C" { | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #include <inttypes.h> | ||
| 27 | |||
| 28 | /** | ||
| 29 | * @file | ||
| 30 | * @brief Sysex utility functions | ||
| 31 | * | ||
| 32 | * These functions are for converting data to and from a "midi-safe" format, | ||
| 33 | * which can be use to send data with sysex messages. Sysex messages may only | ||
| 34 | * contain data where the to bit is not set. | ||
| 35 | * | ||
| 36 | * An "encoded" midi message is one that contains all of the data from its | ||
| 37 | * original state, but does not have any of the top bits set. | ||
| 38 | * | ||
| 39 | * Every 7 bytes of decoded data is converted into 8 bytes of encoded data and | ||
| 40 | * visa-versa. If you'd like to operate on small segments, make sure that you | ||
| 41 | * encode in 7 byte increments and decode in 8 byte increments. | ||
| 42 | * | ||
| 43 | */ | ||
| 44 | |||
| 45 | /** @defgroup sysex_tools Sysex utility functions | ||
| 46 | * @{ | ||
| 47 | */ | ||
| 48 | |||
| 49 | /** | ||
| 50 | * @brief Compute the length of a message after it is encoded. | ||
| 51 | * | ||
| 52 | * @param decoded_length The length, in bytes, of the message to encode. | ||
| 53 | * | ||
| 54 | * @return The length, in bytes, of the message after encodeing. | ||
| 55 | */ | ||
| 56 | uint16_t sysex_encoded_length(uint16_t decoded_length); | ||
| 57 | |||
| 58 | /** | ||
| 59 | * @brief Compute the length of a message after it is decoded. | ||
| 60 | * | ||
| 61 | * @param encoded_length The length, in bytes, of the encoded message. | ||
| 62 | * | ||
| 63 | * @return The length, in bytes, of the message after it is decoded. | ||
| 64 | */ | ||
| 65 | uint16_t sysex_decoded_length(uint16_t encoded_length); | ||
| 66 | |||
| 67 | /** | ||
| 68 | * @brief Encode data so that it can be transmitted safely in a sysex message. | ||
| 69 | * | ||
| 70 | * @param encoded The output data buffer, must be at least sysex_encoded_length(length) bytes long. | ||
| 71 | * @param source The input buffer of data to be encoded. | ||
| 72 | * @param length The number of bytes from the input buffer to encode. | ||
| 73 | * | ||
| 74 | * @return number of bytes encoded. | ||
| 75 | */ | ||
| 76 | uint16_t sysex_encode(uint8_t *encoded, const uint8_t *source, uint16_t length); | ||
| 77 | |||
| 78 | /** | ||
| 79 | * @brief Decode encoded data. | ||
| 80 | * | ||
| 81 | * @param decoded The output data buffer, must be at least sysex_decoded_length(length) bytes long. | ||
| 82 | * @param source The input buffer of data to be decoded. | ||
| 83 | * @param length The number of bytes from the input buffer to decode. | ||
| 84 | * | ||
| 85 | * @return number of bytes decoded. | ||
| 86 | */ | ||
| 87 | uint16_t sysex_decode(uint8_t *decoded, const uint8_t *source, uint16_t length); | ||
| 88 | |||
| 89 | /**@}*/ | ||
| 90 | |||
| 91 | #ifdef __cplusplus | ||
| 92 | } | ||
| 93 | #endif | ||
| 94 | |||
| 95 | #endif | ||
