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.

473 lines
15KB

  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. deviceVersionList = 0x06,
  39. touchStart = 0x10,
  40. touchMove = 0x11,
  41. touchEnd = 0x12,
  42. touchStartWithVelocity = 0x13,
  43. touchMoveWithVelocity = 0x14,
  44. touchEndWithVelocity = 0x15,
  45. configMessage = 0x18,
  46. controlButtonDown = 0x20,
  47. controlButtonUp = 0x21,
  48. programEventMessage = 0x28,
  49. logMessage = 0x30
  50. };
  51. /** Messages that the host may send to a device. */
  52. enum class MessageFromHost
  53. {
  54. deviceCommandMessage = 0x01,
  55. sharedDataChange = 0x02,
  56. programEventMessage = 0x03,
  57. firmwareUpdatePacket = 0x04,
  58. configMessage = 0x10
  59. };
  60. /** This is the first item in a BLOCKS message, identifying the message type. */
  61. using MessageType = IntegerWithBitSize<7>;
  62. //==============================================================================
  63. /** This is a type of index identifier used to refer to a block within a group.
  64. It refers to the index of a device in the list of devices that was most recently
  65. sent via a topology change message
  66. (It's not a global UID for a block unit).
  67. NB: to send a message to all devices, pass the getDeviceIndexForBroadcast() value.
  68. */
  69. using TopologyIndex = uint8;
  70. static constexpr int topologyIndexBits = 7;
  71. /** Use this value as the index if you want a message to be sent to all devices in
  72. the group.
  73. */
  74. static constexpr TopologyIndex topologyIndexForBroadcast = 63;
  75. using DeviceCount = IntegerWithBitSize<7>;
  76. using ConnectionCount = IntegerWithBitSize<8>;
  77. //==============================================================================
  78. /** Battery charge level. */
  79. using BatteryLevel = IntegerWithBitSize<5>;
  80. /** Battery charger connection flag. */
  81. using BatteryCharging = IntegerWithBitSize<1>;
  82. //==============================================================================
  83. /** ConnectorPort is an index, starting at 0 for the leftmost port on the
  84. top edge, and going clockwise.
  85. */
  86. using ConnectorPort = IntegerWithBitSize<5>;
  87. //==============================================================================
  88. struct BlockSerialNumber
  89. {
  90. uint8 serial[16];
  91. bool isValid() const noexcept
  92. {
  93. for (auto c : serial)
  94. if (c == 0)
  95. return false;
  96. return isAnyControlBlock() || isPadBlock() || isSeaboardBlock();
  97. }
  98. bool isPadBlock() const noexcept { return hasPrefix ("LPB"); }
  99. bool isLiveBlock() const noexcept { return hasPrefix ("LIC"); }
  100. bool isLoopBlock() const noexcept { return hasPrefix ("LOC"); }
  101. bool isDevCtrlBlock() const noexcept { return hasPrefix ("DCB"); }
  102. bool isTouchBlock() const noexcept { return hasPrefix ("TCB"); }
  103. bool isSeaboardBlock() const noexcept { return hasPrefix ("SBB"); }
  104. bool isAnyControlBlock() const noexcept { return isLiveBlock() || isLoopBlock() || isDevCtrlBlock() || isTouchBlock(); }
  105. bool hasPrefix (const char* prefix) const noexcept { return memcmp (serial, prefix, 3) == 0; }
  106. };
  107. struct VersionNumber
  108. {
  109. uint8 version[21] = {};
  110. uint8 length = 0;
  111. };
  112. struct DeviceStatus
  113. {
  114. BlockSerialNumber serialNumber;
  115. TopologyIndex index;
  116. BatteryLevel batteryLevel;
  117. BatteryCharging batteryCharging;
  118. };
  119. struct DeviceConnection
  120. {
  121. TopologyIndex device1, device2;
  122. ConnectorPort port1, port2;
  123. };
  124. struct DeviceVersion
  125. {
  126. TopologyIndex index;
  127. VersionNumber version;
  128. };
  129. static constexpr uint8 maxBlocksInTopologyPacket = 6;
  130. static constexpr uint8 maxConnectionsInTopologyPacket = 24;
  131. //==============================================================================
  132. /** Configuration Item Identifiers. */
  133. enum ConfigItemId
  134. {
  135. // MIDI
  136. midiStartChannel = 0,
  137. midiEndChannel = 1,
  138. midiUseMPE = 2,
  139. pitchBendRange = 3,
  140. octave = 4,
  141. transpose = 5,
  142. slideCC = 6,
  143. slideMode = 7,
  144. octaveTopology = 8,
  145. // Touch
  146. velocitySensitivity = 10,
  147. glideSensitivity = 11,
  148. slideSensitivity = 12,
  149. pressureSensitivity = 13,
  150. liftSensitivity = 14,
  151. fixedVelocity = 15,
  152. fixedVelocityValue = 16,
  153. pianoMode = 17,
  154. glideLock = 18,
  155. // Live
  156. mode = 20,
  157. volume = 21,
  158. scale = 22,
  159. hideMode = 23,
  160. chord = 24,
  161. arpPattern = 25,
  162. tempo = 26,
  163. // Tracking
  164. xTrackingMode = 30,
  165. yTrackingMode = 31,
  166. zTrackingMode = 32,
  167. // User
  168. user0 = 64,
  169. user1 = 65,
  170. user2 = 66,
  171. user3 = 67,
  172. user4 = 68,
  173. user5 = 69,
  174. user6 = 70,
  175. user7 = 71,
  176. user8 = 72,
  177. user9 = 73,
  178. user10 = 74,
  179. user11 = 75,
  180. user12 = 76,
  181. user13 = 77,
  182. user14 = 78,
  183. user15 = 79,
  184. user16 = 80,
  185. user17 = 81,
  186. user18 = 82,
  187. user19 = 83,
  188. user20 = 84,
  189. user21 = 85,
  190. user22 = 86,
  191. user23 = 87,
  192. user24 = 88,
  193. user25 = 89,
  194. user26 = 90,
  195. user27 = 91,
  196. user28 = 92,
  197. user29 = 93,
  198. user30 = 94,
  199. user31 = 95
  200. };
  201. static constexpr uint8 numberOfUserConfigs = 32;
  202. static constexpr uint8 maxConfigIndex = uint8 (ConfigItemId::user0) + numberOfUserConfigs;
  203. static constexpr uint8 configUserConfigNameLength = 32;
  204. static constexpr uint8 configMaxOptions = 8;
  205. static constexpr uint8 configOptionNameLength = 16;
  206. //==============================================================================
  207. /** The coordinates of a touch. */
  208. struct TouchPosition
  209. {
  210. using Xcoord = IntegerWithBitSize<12>;
  211. using Ycoord = IntegerWithBitSize<12>;
  212. using Zcoord = IntegerWithBitSize<8>;
  213. Xcoord x;
  214. Ycoord y;
  215. Zcoord z;
  216. enum { bits = Xcoord::bits + Ycoord::bits + Zcoord::bits };
  217. };
  218. /** The velocities for each dimension of a touch. */
  219. struct TouchVelocity
  220. {
  221. using VXcoord = IntegerWithBitSize<8>;
  222. using VYcoord = IntegerWithBitSize<8>;
  223. using VZcoord = IntegerWithBitSize<8>;
  224. VXcoord vx;
  225. VYcoord vy;
  226. VZcoord vz;
  227. enum { bits = VXcoord::bits + VYcoord::bits + VZcoord::bits };
  228. };
  229. /** The index of a touch, i.e. finger number. */
  230. using TouchIndex = IntegerWithBitSize<5>;
  231. using PacketCounter = IntegerWithBitSize<10>;
  232. //==============================================================================
  233. enum DeviceCommands
  234. {
  235. beginAPIMode = 0x00,
  236. requestTopologyMessage = 0x01,
  237. endAPIMode = 0x02,
  238. ping = 0x03,
  239. debugMode = 0x04,
  240. saveProgramAsDefault = 0x05
  241. };
  242. using DeviceCommand = IntegerWithBitSize<9>;
  243. //==============================================================================
  244. enum ConfigCommands
  245. {
  246. setConfig = 0x00,
  247. requestConfig = 0x01, // Request a config update
  248. requestFactorySync = 0x02, // Requests all active factory config data
  249. requestUserSync = 0x03, // Requests all active user config data
  250. updateConfig = 0x04, // Set value, min and max
  251. updateUserConfig = 0x05, // As above but contains user config metadata
  252. setConfigState = 0x06, // Set config activation state and whether it is saved in flash
  253. factorySyncEnd = 0x07
  254. };
  255. using ConfigCommand = IntegerWithBitSize<4>;
  256. using ConfigItemIndex = IntegerWithBitSize<8>;
  257. using ConfigItemValue = IntegerWithBitSize<32>;
  258. //==============================================================================
  259. /** An ID for a control-block button type */
  260. using ControlButtonID = IntegerWithBitSize<12>;
  261. //==============================================================================
  262. using RotaryDialIndex = IntegerWithBitSize<7>;
  263. using RotaryDialAngle = IntegerWithBitSize<14>;
  264. using RotaryDialDelta = IntegerWithBitSize<14>;
  265. //==============================================================================
  266. enum DataChangeCommands
  267. {
  268. endOfPacket = 0,
  269. endOfChanges = 1,
  270. skipBytesFew = 2,
  271. skipBytesMany = 3,
  272. setSequenceOfBytes = 4,
  273. setFewBytesWithValue = 5,
  274. setFewBytesWithLastValue = 6,
  275. setManyBytesWithValue = 7
  276. };
  277. using PacketIndex = IntegerWithBitSize<16>;
  278. using DataChangeCommand = IntegerWithBitSize<3>;
  279. using ByteCountFew = IntegerWithBitSize<4>;
  280. using ByteCountMany = IntegerWithBitSize<8>;
  281. using ByteValue = IntegerWithBitSize<8>;
  282. using ByteSequenceContinues = IntegerWithBitSize<1>;
  283. using FirmwareUpdateACKCode = IntegerWithBitSize<7>;
  284. using FirmwareUpdatePacketSize = IntegerWithBitSize<7>;
  285. static constexpr uint32 numProgramMessageInts = 3;
  286. static constexpr uint32 apiModeHostPingTimeoutMs = 5000;
  287. static constexpr uint32 padBlockProgramAndHeapSize = 7200;
  288. static constexpr uint32 padBlockStackSize = 800;
  289. static constexpr uint32 controlBlockProgramAndHeapSize = 3000;
  290. static constexpr uint32 controlBlockStackSize = 800;
  291. //==============================================================================
  292. /** Contains the number of bits required to encode various items in the packets */
  293. enum BitSizes
  294. {
  295. topologyMessageHeader = MessageType::bits + ProtocolVersion::bits + DeviceCount::bits + ConnectionCount::bits,
  296. topologyDeviceInfo = sizeof (BlockSerialNumber) * 7 + BatteryLevel::bits + BatteryCharging::bits,
  297. topologyConnectionInfo = topologyIndexBits + ConnectorPort::bits + topologyIndexBits + ConnectorPort::bits,
  298. typeDeviceAndTime = MessageType::bits + PacketTimestampOffset::bits,
  299. touchMessage = typeDeviceAndTime + TouchIndex::bits + TouchPosition::bits,
  300. touchMessageWithVelocity = touchMessage + TouchVelocity::bits,
  301. programEventMessage = MessageType::bits + 32 * numProgramMessageInts,
  302. packetACK = MessageType::bits + PacketCounter::bits,
  303. firmwareUpdateACK = MessageType::bits + FirmwareUpdateACKCode::bits,
  304. controlButtonMessage = typeDeviceAndTime + ControlButtonID::bits,
  305. configSetMessage = MessageType::bits + ConfigCommand::bits + ConfigItemIndex::bits + ConfigItemValue::bits,
  306. configRespMessage = MessageType::bits + ConfigCommand::bits + ConfigItemIndex::bits + (ConfigItemValue::bits * 3),
  307. configSyncEndMessage = MessageType::bits + ConfigCommand::bits,
  308. };
  309. //==============================================================================
  310. // These are the littlefoot functions provided for use in BLOCKS programs
  311. static constexpr const char* ledProgramLittleFootFunctions[] =
  312. {
  313. "min/iii",
  314. "min/fff",
  315. "max/iii",
  316. "max/fff",
  317. "clamp/iiii",
  318. "clamp/ffff",
  319. "abs/ii",
  320. "abs/ff",
  321. "map/ffffff",
  322. "map/ffff",
  323. "mod/iii",
  324. "getRandomFloat/f",
  325. "getRandomInt/ii",
  326. "getMillisecondCounter/i",
  327. "getFirmwareVersion/i",
  328. "log/vi",
  329. "logHex/vi",
  330. "getTimeInCurrentFunctionCall/i",
  331. "getBatteryLevel/f",
  332. "isBatteryCharging/b",
  333. "isMasterBlock/b",
  334. "isConnectedToHost/b",
  335. "setStatusOverlayActive/vb",
  336. "getNumBlocksInTopology/i",
  337. "getBlockIDForIndex/ii",
  338. "getBlockIDOnPort/ii",
  339. "getPortToMaster/i",
  340. "getBlockTypeForID/ii",
  341. "sendMessageToBlock/viiii",
  342. "sendMessageToHost/viii",
  343. "getHorizontalDistFromMaster/i",
  344. "getVerticalDistFromMaster/i",
  345. "getAngleFromMaster/i",
  346. "setAutoRotate/vb",
  347. "getClusterIndex/i",
  348. "getClusterWidth/i",
  349. "getClusterHeight/i",
  350. "getClusterXpos/i",
  351. "getClusterYpos/i",
  352. "getNumBlocksInCurrentCluster/i",
  353. "getBlockIdForBlockInCluster/ii",
  354. "isMasterInCurrentCluster/b",
  355. "makeARGB/iiiii",
  356. "blendARGB/iii",
  357. "fillPixel/viii",
  358. "blendPixel/viii",
  359. "fillRect/viiiii",
  360. "blendRect/viiiii",
  361. "blendGradientRect/viiiiiiii",
  362. "blendCircle/vifffb",
  363. "addPressurePoint/vifff",
  364. "drawPressureMap/v",
  365. "fadePressureMap/v",
  366. "drawNumber/viiii",
  367. "clearDisplay/v",
  368. "clearDisplay/vi",
  369. "displayBatteryLevel/v",
  370. "sendMIDI/vi",
  371. "sendMIDI/vii",
  372. "sendMIDI/viii",
  373. "sendNoteOn/viii",
  374. "sendNoteOff/viii",
  375. "sendAftertouch/viii",
  376. "sendCC/viii",
  377. "sendPitchBend/vii",
  378. "sendChannelPressure/vii",
  379. "setChannelRange/vbii",
  380. "assignChannel/ii",
  381. "deassignChannel/vii",
  382. "getControlChannel/i",
  383. "useMPEDuplicateFilter/vb",
  384. "getSensorValue/iii",
  385. "handleTouchAsSeaboard/vi",
  386. "setPowerSavingEnabled/vb",
  387. "getLocalConfig/ii",
  388. "setLocalConfig/vii",
  389. "requestRemoteConfig/vii",
  390. "setRemoteConfig/viii",
  391. "setLocalConfigItemRange/viii",
  392. "setLocalConfigActiveState/vibb",
  393. "linkBlockIDtoController/vi",
  394. "repaintControl/v",
  395. "onControlPress/vi",
  396. "onControlRelease/vi",
  397. "initControl/viiiiiiiii",
  398. nullptr
  399. };