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.

336 lines
11KB

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