The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. /** This value is incremented when the format of the API changes in a way which
  24. breaks compatibility.
  25. */
  26. static constexpr uint32 currentProtocolVersion = 1;
  27. using ProtocolVersion = IntegerWithBitSize<8>;
  28. //==============================================================================
  29. /** A timestamp for a packet, in milliseconds since device boot-up */
  30. using PacketTimestamp = IntegerWithBitSize<32>;
  31. /** This relative timestamp is for use inside a packet, and it represents a
  32. number of milliseconds that should be added to the packet's timestamp.
  33. */
  34. using PacketTimestampOffset = IntegerWithBitSize<5>;
  35. //==============================================================================
  36. /** Messages that a device may send to the host. */
  37. enum class MessageFromDevice
  38. {
  39. deviceTopology = 0x01,
  40. packetACK = 0x02,
  41. firmwareUpdateACK = 0x03,
  42. deviceTopologyExtend = 0x04,
  43. deviceTopologyEnd = 0x05,
  44. touchStart = 0x10,
  45. touchMove = 0x11,
  46. touchEnd = 0x12,
  47. touchStartWithVelocity = 0x13,
  48. touchMoveWithVelocity = 0x14,
  49. touchEndWithVelocity = 0x15,
  50. controlButtonDown = 0x20,
  51. controlButtonUp = 0x21,
  52. programEventMessage = 0x28,
  53. logMessage = 0x30
  54. };
  55. /** Messages that the host may send to a device. */
  56. enum class MessageFromHost
  57. {
  58. deviceCommandMessage = 0x01,
  59. sharedDataChange = 0x02,
  60. programEventMessage = 0x03,
  61. firmwareUpdatePacket = 0x04
  62. };
  63. /** This is the first item in a BLOCKS message, identifying the message type. */
  64. using MessageType = IntegerWithBitSize<7>;
  65. //==============================================================================
  66. /** This is a type of index identifier used to refer to a block within a group.
  67. It refers to the index of a device in the list of devices that was most recently
  68. sent via a topology change message
  69. (It's not a global UID for a block unit).
  70. NB: to send a message to all devices, pass the getDeviceIndexForBroadcast() value.
  71. */
  72. using TopologyIndex = uint8;
  73. static constexpr int topologyIndexBits = 7;
  74. /** Use this value as the index if you want a message to be sent to all devices in
  75. the group.
  76. */
  77. static constexpr TopologyIndex topologyIndexForBroadcast = 63;
  78. using DeviceCount = IntegerWithBitSize<7>;
  79. using ConnectionCount = IntegerWithBitSize<8>;
  80. //==============================================================================
  81. /** Battery charge level. */
  82. using BatteryLevel = IntegerWithBitSize<5>;
  83. /** Battery charger connection flag. */
  84. using BatteryCharging = IntegerWithBitSize<1>;
  85. //==============================================================================
  86. /** ConnectorPort is an index, starting at 0 for the leftmost port on the
  87. top edge, and going clockwise.
  88. */
  89. using ConnectorPort = IntegerWithBitSize<5>;
  90. //==============================================================================
  91. struct BlockSerialNumber
  92. {
  93. uint8 serial[16];
  94. bool isValid() const noexcept
  95. {
  96. for (auto c : serial)
  97. if (c == 0)
  98. return false;
  99. return isAnyControlBlock() || isPadBlock();
  100. }
  101. bool isPadBlock() const noexcept { return hasPrefix ("LPB"); }
  102. bool isLiveBlock() const noexcept { return hasPrefix ("LIC"); }
  103. bool isLoopBlock() const noexcept { return hasPrefix ("LOC"); }
  104. bool isDevCtrlBlock() const noexcept { return hasPrefix ("DCB"); }
  105. bool isAnyControlBlock() const noexcept { return isLiveBlock() || isLoopBlock() || isDevCtrlBlock(); }
  106. bool hasPrefix (const char* prefix) const noexcept { return memcmp (serial, prefix, 3) == 0; }
  107. };
  108. struct DeviceStatus
  109. {
  110. BlockSerialNumber serialNumber;
  111. TopologyIndex index;
  112. BatteryLevel batteryLevel;
  113. BatteryCharging batteryCharging;
  114. };
  115. struct DeviceConnection
  116. {
  117. TopologyIndex device1, device2;
  118. ConnectorPort port1, port2;
  119. };
  120. static constexpr uint8 maxBlocksInTopologyPacket = 6;
  121. static constexpr uint8 maxConnectionsInTopologyPacket = 24;
  122. //==============================================================================
  123. /** The coordinates of a touch. */
  124. struct TouchPosition
  125. {
  126. using Xcoord = IntegerWithBitSize<12>;
  127. using Ycoord = IntegerWithBitSize<12>;
  128. using Zcoord = IntegerWithBitSize<8>;
  129. Xcoord x;
  130. Ycoord y;
  131. Zcoord z;
  132. enum { bits = Xcoord::bits + Ycoord::bits + Zcoord::bits };
  133. };
  134. /** The velocities for each dimension of a touch. */
  135. struct TouchVelocity
  136. {
  137. using VXcoord = IntegerWithBitSize<8>;
  138. using VYcoord = IntegerWithBitSize<8>;
  139. using VZcoord = IntegerWithBitSize<8>;
  140. VXcoord vx;
  141. VYcoord vy;
  142. VZcoord vz;
  143. enum { bits = VXcoord::bits + VYcoord::bits + VZcoord::bits };
  144. };
  145. /** The index of a touch, i.e. finger number. */
  146. using TouchIndex = IntegerWithBitSize<5>;
  147. using PacketCounter = IntegerWithBitSize<10>;
  148. //==============================================================================
  149. enum DeviceCommands
  150. {
  151. beginAPIMode = 0x00,
  152. requestTopologyMessage = 0x01,
  153. endAPIMode = 0x02,
  154. ping = 0x03,
  155. debugMode = 0x04,
  156. saveProgramAsDefault = 0x05
  157. };
  158. using DeviceCommand = IntegerWithBitSize<9>;
  159. //==============================================================================
  160. /** An ID for a control-block button type */
  161. using ControlButtonID = IntegerWithBitSize<12>;
  162. //==============================================================================
  163. using RotaryDialIndex = IntegerWithBitSize<7>;
  164. using RotaryDialAngle = IntegerWithBitSize<14>;
  165. using RotaryDialDelta = IntegerWithBitSize<14>;
  166. //==============================================================================
  167. enum DataChangeCommands
  168. {
  169. endOfPacket = 0,
  170. endOfChanges = 1,
  171. skipBytesFew = 2,
  172. skipBytesMany = 3,
  173. setSequenceOfBytes = 4,
  174. setFewBytesWithValue = 5,
  175. setFewBytesWithLastValue = 6,
  176. setManyBytesWithValue = 7
  177. };
  178. using PacketIndex = IntegerWithBitSize<16>;
  179. using DataChangeCommand = IntegerWithBitSize<3>;
  180. using ByteCountFew = IntegerWithBitSize<4>;
  181. using ByteCountMany = IntegerWithBitSize<8>;
  182. using ByteValue = IntegerWithBitSize<8>;
  183. using ByteSequenceContinues = IntegerWithBitSize<1>;
  184. using FirmwareUpdateACKCode = IntegerWithBitSize<7>;
  185. using FirmwareUpdatePacketSize = IntegerWithBitSize<7>;
  186. static constexpr uint32 numProgramMessageInts = 3;
  187. static constexpr uint32 apiModeHostPingTimeoutMs = 5000;
  188. static constexpr uint32 padBlockProgramAndHeapSize = 7200;
  189. static constexpr uint32 padBlockStackSize = 800;
  190. static constexpr uint32 controlBlockProgramAndHeapSize = 3000;
  191. static constexpr uint32 controlBlockStackSize = 800;
  192. //==============================================================================
  193. /** Contains the number of bits required to encode various items in the packets */
  194. enum BitSizes
  195. {
  196. topologyMessageHeader = MessageType::bits + ProtocolVersion::bits + DeviceCount::bits + ConnectionCount::bits,
  197. topologyDeviceInfo = sizeof (BlockSerialNumber) * 7 + BatteryLevel::bits + BatteryCharging::bits,
  198. topologyConnectionInfo = topologyIndexBits + ConnectorPort::bits + topologyIndexBits + ConnectorPort::bits,
  199. typeDeviceAndTime = MessageType::bits + PacketTimestampOffset::bits,
  200. touchMessage = typeDeviceAndTime + TouchIndex::bits + TouchPosition::bits,
  201. touchMessageWithVelocity = touchMessage + TouchVelocity::bits,
  202. programEventMessage = MessageType::bits + 32 * numProgramMessageInts,
  203. packetACK = MessageType::bits + PacketCounter::bits,
  204. firmwareUpdateACK = MessageType::bits + FirmwareUpdateACKCode::bits,
  205. controlButtonMessage = typeDeviceAndTime + ControlButtonID::bits,
  206. };
  207. //==============================================================================
  208. // These are the littlefoot functions provided for use in BLOCKS programs
  209. static constexpr const char* ledProgramLittleFootFunctions[] =
  210. {
  211. "min/iii",
  212. "min/fff",
  213. "max/iii",
  214. "max/fff",
  215. "clamp/iiii",
  216. "clamp/ffff",
  217. "abs/ii",
  218. "abs/ff",
  219. "map/ffffff",
  220. "map/ffff",
  221. "mod/iii",
  222. "getRandomFloat/f",
  223. "getRandomInt/ii",
  224. "getMillisecondCounter/i",
  225. "getFirmwareVersion/i",
  226. "log/vi",
  227. "logHex/vi",
  228. "getTimeInCurrentFunctionCall/i",
  229. "getBatteryLevel/f",
  230. "isBatteryCharging/b",
  231. "isMasterBlock/b",
  232. "isConnectedToHost/b",
  233. "setStatusOverlayActive/vb",
  234. "getNumBlocksInTopology/i",
  235. "getBlockIDForIndex/ii",
  236. "getBlockIDOnPort/ii",
  237. "getPortToMaster/i",
  238. "getBlockTypeForID/ii",
  239. "sendMessageToBlock/viiii",
  240. "sendMessageToHost/viii",
  241. "getHorizontalDistFromMaster/i",
  242. "getVerticalDistFromMaster/i",
  243. "getAngleFromMaster/i",
  244. "setAutoRotate/vb",
  245. "getClusterWidth/i",
  246. "getClusterHeight/i",
  247. "getClusterXpos/i",
  248. "getClusterYpos/i",
  249. "makeARGB/iiiii",
  250. "blendARGB/iii",
  251. "fillPixel/viii",
  252. "blendPixel/viii",
  253. "fillRect/viiiii",
  254. "blendRect/viiiii",
  255. "blendGradientRect/viiiiiiii",
  256. "blendCircle/vifffb",
  257. "addPressurePoint/vifff",
  258. "drawPressureMap/v",
  259. "fadePressureMap/v",
  260. "drawNumber/viiii",
  261. "clearDisplay/v",
  262. "clearDisplay/vi",
  263. "sendMIDI/vi",
  264. "sendMIDI/vii",
  265. "sendMIDI/viii",
  266. "sendNoteOn/viii",
  267. "sendNoteOff/viii",
  268. "sendAftertouch/viii",
  269. "sendCC/viii",
  270. "sendPitchBend/vii",
  271. "sendChannelPressure/vii",
  272. "setChannelRange/vbii",
  273. "assignChannel/ii",
  274. "deassignChannel/vii",
  275. "getControlChannel/i",
  276. "useMPEDuplicateFilter/vb",
  277. nullptr
  278. };