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.

491 lines
16KB

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