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.

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