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.

613 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. deviceVersion = 0x06,
  43. deviceName = 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. /** Messages that the host may send to a device that do not have the usual message format */
  69. namespace SpecialMessageFromHost
  70. {
  71. constexpr uint8 resetMaster[6] = { 0xf0, 0x00, 0x21, 0x10, 0x49, 0xf7 };
  72. }
  73. /** This is the first item in a BLOCKS message, identifying the message type. */
  74. using MessageType = IntegerWithBitSize<7>;
  75. //==============================================================================
  76. /** This is a type of index identifier used to refer to a block within a group.
  77. It refers to the index of a device in the list of devices that was most recently
  78. sent via a topology change message
  79. (It's not a global UID for a block unit).
  80. NB: to send a message to all devices, pass the getDeviceIndexForBroadcast() value.
  81. */
  82. using TopologyIndex = uint8;
  83. static constexpr int topologyIndexBits = 7;
  84. /** Use this value as the index if you want a message to be sent to all devices in
  85. the group.
  86. */
  87. static constexpr TopologyIndex topologyIndexForBroadcast = 63;
  88. using DeviceCount = IntegerWithBitSize<7>;
  89. using ConnectionCount = IntegerWithBitSize<8>;
  90. //==============================================================================
  91. /** Battery charge level. */
  92. using BatteryLevel = IntegerWithBitSize<5>;
  93. /** Battery charger connection flag. */
  94. using BatteryCharging = IntegerWithBitSize<1>;
  95. //==============================================================================
  96. /** ConnectorPort is an index, starting at 0 for the leftmost port on the
  97. top edge, and going clockwise.
  98. */
  99. using ConnectorPort = IntegerWithBitSize<5>;
  100. //==============================================================================
  101. /** Structure for generic block data
  102. @tags{Blocks}
  103. */
  104. template <size_t MaxSize>
  105. struct BlockStringData
  106. {
  107. uint8 data[MaxSize] = {};
  108. uint8 length = 0;
  109. static const size_t maxLength { MaxSize };
  110. bool isNotEmpty() const
  111. {
  112. return length > 0;
  113. }
  114. String asString() const
  115. {
  116. return String ((const char*) data, length);
  117. }
  118. bool operator== (const BlockStringData& other) const
  119. {
  120. if (length != other.length)
  121. return false;
  122. for (int i = 0; i < length; ++i)
  123. if (data[i] != other.data[i])
  124. return false;
  125. return true;
  126. }
  127. bool operator!= (const BlockStringData& other) const
  128. {
  129. return ! ( *this == other );
  130. }
  131. };
  132. using VersionNumber = BlockStringData<21>;
  133. using BlockName = BlockStringData<33>;
  134. //==============================================================================
  135. /** Structure describing a block's serial number
  136. @tags{Blocks}
  137. */
  138. struct BlockSerialNumber : public BlockStringData<16>
  139. {
  140. bool isValid() const noexcept
  141. {
  142. for (auto c : data)
  143. if (c == 0)
  144. return false;
  145. return isAnyControlBlock() || isPadBlock() || isSeaboardBlock() || isLumiKeysBlock();
  146. }
  147. bool isPadBlock() const noexcept { return hasPrefix ("LPB") || hasPrefix ("LPM"); }
  148. bool isLiveBlock() const noexcept { return hasPrefix ("LIC"); }
  149. bool isLoopBlock() const noexcept { return hasPrefix ("LOC"); }
  150. bool isDevCtrlBlock() const noexcept { return hasPrefix ("DCB"); }
  151. bool isTouchBlock() const noexcept { return hasPrefix ("TCB"); }
  152. bool isSeaboardBlock() const noexcept { return hasPrefix ("SBB"); }
  153. bool isLumiKeysBlock() const noexcept { return hasPrefix ("LKB"); }
  154. bool isAnyControlBlock() const noexcept { return isLiveBlock() || isLoopBlock() || isDevCtrlBlock() || isTouchBlock(); }
  155. bool hasPrefix (const char* prefix) const noexcept { return memcmp (data, prefix, 3) == 0; }
  156. };
  157. //==============================================================================
  158. /** Structure for the device status
  159. @tags{Blocks}
  160. */
  161. struct DeviceStatus
  162. {
  163. BlockSerialNumber serialNumber;
  164. TopologyIndex index;
  165. BatteryLevel batteryLevel;
  166. BatteryCharging batteryCharging;
  167. };
  168. //==============================================================================
  169. /** Structure for the device connection
  170. @tags{Blocks}
  171. */
  172. struct DeviceConnection
  173. {
  174. TopologyIndex device1, device2;
  175. ConnectorPort port1, port2;
  176. bool operator== (const DeviceConnection& other) const
  177. {
  178. return isEqual (other);
  179. }
  180. bool operator!= (const DeviceConnection& other) const
  181. {
  182. return ! isEqual (other);
  183. }
  184. private:
  185. bool isEqual (const DeviceConnection& other) const
  186. {
  187. return device1 == other.device1
  188. && device2 == other.device2
  189. && port1 == other.port1
  190. && port2 == other.port2;
  191. }
  192. };
  193. //==============================================================================
  194. /** Structure for the device version
  195. @tags{Blocks}
  196. */
  197. struct DeviceVersion
  198. {
  199. TopologyIndex index;
  200. VersionNumber version;
  201. };
  202. //==============================================================================
  203. /** Structure used for the device name
  204. @tags{Blocks}
  205. */
  206. struct DeviceName
  207. {
  208. TopologyIndex index;
  209. BlockName name;
  210. };
  211. static constexpr uint8 maxBlocksInTopologyPacket = 6;
  212. static constexpr uint8 maxConnectionsInTopologyPacket = 24;
  213. //==============================================================================
  214. /** Configuration Item Identifiers. */
  215. enum ConfigItemId
  216. {
  217. // MIDI
  218. midiStartChannel = 0,
  219. midiEndChannel = 1,
  220. midiUseMPE = 2,
  221. pitchBendRange = 3,
  222. octave = 4,
  223. transpose = 5,
  224. slideCC = 6,
  225. slideMode = 7,
  226. octaveTopology = 8,
  227. midiChannelRange = 9,
  228. MPEZone = 40,
  229. // Touch
  230. velocitySensitivity = 10,
  231. glideSensitivity = 11,
  232. slideSensitivity = 12,
  233. pressureSensitivity = 13,
  234. liftSensitivity = 14,
  235. fixedVelocity = 15,
  236. fixedVelocityValue = 16,
  237. pianoMode = 17,
  238. glideLock = 18,
  239. glideLockEnable = 19,
  240. // Live
  241. mode = 20,
  242. volume = 21,
  243. scale = 22,
  244. hideMode = 23,
  245. chord = 24,
  246. arpPattern = 25,
  247. tempo = 26,
  248. key = 27,
  249. autoTransposeToKey = 28,
  250. // Tracking
  251. xTrackingMode = 30,
  252. yTrackingMode = 31,
  253. zTrackingMode = 32,
  254. // Graphics
  255. gammaCorrection = 33,
  256. globalKeyColour = 34,
  257. rootKeyColour = 35,
  258. brightness = 36,
  259. // User
  260. user0 = 64,
  261. user1 = 65,
  262. user2 = 66,
  263. user3 = 67,
  264. user4 = 68,
  265. user5 = 69,
  266. user6 = 70,
  267. user7 = 71,
  268. user8 = 72,
  269. user9 = 73,
  270. user10 = 74,
  271. user11 = 75,
  272. user12 = 76,
  273. user13 = 77,
  274. user14 = 78,
  275. user15 = 79,
  276. user16 = 80,
  277. user17 = 81,
  278. user18 = 82,
  279. user19 = 83,
  280. user20 = 84,
  281. user21 = 85,
  282. user22 = 86,
  283. user23 = 87,
  284. user24 = 88,
  285. user25 = 89,
  286. user26 = 90,
  287. user27 = 91,
  288. user28 = 92,
  289. user29 = 93,
  290. user30 = 94,
  291. user31 = 95
  292. };
  293. static constexpr uint8 numberOfUserConfigs = 32;
  294. static constexpr uint8 maxConfigIndex = uint8 (ConfigItemId::user0) + numberOfUserConfigs;
  295. static constexpr uint8 configUserConfigNameLength = 32;
  296. static constexpr uint8 configMaxOptions = 16;
  297. static constexpr uint8 configOptionNameLength = 16;
  298. //==============================================================================
  299. /** The coordinates of a touch.
  300. @tags{Blocks}
  301. */
  302. struct TouchPosition
  303. {
  304. using Xcoord = IntegerWithBitSize<12>;
  305. using Ycoord = IntegerWithBitSize<12>;
  306. using Zcoord = IntegerWithBitSize<8>;
  307. Xcoord x;
  308. Ycoord y;
  309. Zcoord z;
  310. enum { bits = Xcoord::bits + Ycoord::bits + Zcoord::bits };
  311. };
  312. /** The velocities for each dimension of a touch.
  313. @tags{Blocks}
  314. */
  315. struct TouchVelocity
  316. {
  317. using VXcoord = IntegerWithBitSize<8>;
  318. using VYcoord = IntegerWithBitSize<8>;
  319. using VZcoord = IntegerWithBitSize<8>;
  320. VXcoord vx;
  321. VYcoord vy;
  322. VZcoord vz;
  323. enum { bits = VXcoord::bits + VYcoord::bits + VZcoord::bits };
  324. };
  325. /** The index of a touch, i.e. finger number. */
  326. using TouchIndex = IntegerWithBitSize<5>;
  327. using PacketCounter = IntegerWithBitSize<10>;
  328. //==============================================================================
  329. enum DeviceCommands
  330. {
  331. beginAPIMode = 0x00,
  332. requestTopologyMessage = 0x01,
  333. endAPIMode = 0x02,
  334. ping = 0x03,
  335. debugMode = 0x04,
  336. saveProgramAsDefault = 0x05
  337. };
  338. using DeviceCommand = IntegerWithBitSize<9>;
  339. //==============================================================================
  340. enum ConfigCommands
  341. {
  342. setConfig = 0x00,
  343. requestConfig = 0x01, // Request a config update
  344. requestFactorySync = 0x02, // Requests all active factory config data
  345. requestUserSync = 0x03, // Requests all active user config data
  346. updateConfig = 0x04, // Set value, min and max
  347. updateUserConfig = 0x05, // As above but contains user config metadata
  348. setConfigState = 0x06, // Set config activation state and whether it is saved in flash
  349. factorySyncEnd = 0x07,
  350. clusterConfigSync = 0x08,
  351. factorySyncReset = 0x09
  352. };
  353. using ConfigCommand = IntegerWithBitSize<4>;
  354. using ConfigItemIndex = IntegerWithBitSize<8>;
  355. using ConfigItemValue = IntegerWithBitSize<32>;
  356. //==============================================================================
  357. /** An ID for a control-block button type */
  358. using ControlButtonID = IntegerWithBitSize<12>;
  359. //==============================================================================
  360. using RotaryDialIndex = IntegerWithBitSize<7>;
  361. using RotaryDialAngle = IntegerWithBitSize<14>;
  362. using RotaryDialDelta = IntegerWithBitSize<14>;
  363. //==============================================================================
  364. enum DataChangeCommands
  365. {
  366. endOfPacket = 0,
  367. endOfChanges = 1,
  368. skipBytesFew = 2,
  369. skipBytesMany = 3,
  370. setSequenceOfBytes = 4,
  371. setFewBytesWithValue = 5,
  372. setFewBytesWithLastValue = 6,
  373. setManyBytesWithValue = 7
  374. };
  375. using PacketIndex = IntegerWithBitSize<16>;
  376. using DataChangeCommand = IntegerWithBitSize<3>;
  377. using ByteCountFew = IntegerWithBitSize<4>;
  378. using ByteCountMany = IntegerWithBitSize<8>;
  379. using ByteValue = IntegerWithBitSize<8>;
  380. using ByteSequenceContinues = IntegerWithBitSize<1>;
  381. using FirmwareUpdateACKCode = IntegerWithBitSize<7>;
  382. using FirmwareUpdateACKDetail = IntegerWithBitSize<32>;
  383. using FirmwareUpdatePacketSize = IntegerWithBitSize<7>;
  384. static constexpr uint32 numProgramMessageInts = 3;
  385. static constexpr uint32 apiModeHostPingTimeoutMs = 5000;
  386. static constexpr uint32 padBlockProgramAndHeapSize = 7200;
  387. static constexpr uint32 padBlockStackSize = 800;
  388. static constexpr uint32 controlBlockProgramAndHeapSize = 3000;
  389. static constexpr uint32 controlBlockStackSize = 800;
  390. //==============================================================================
  391. /** Contains the number of bits required to encode various items in the packets */
  392. enum BitSizes
  393. {
  394. topologyMessageHeader = (int) MessageType::bits + (int) ProtocolVersion::bits + (int) DeviceCount::bits + (int) ConnectionCount::bits,
  395. topologyDeviceInfo = (int) BlockSerialNumber::maxLength * 7 + (int) BatteryLevel::bits + (int) BatteryCharging::bits,
  396. topologyConnectionInfo = topologyIndexBits + (int) ConnectorPort::bits + topologyIndexBits + (int) ConnectorPort::bits,
  397. typeDeviceAndTime = (int) MessageType::bits + (int) PacketTimestampOffset::bits,
  398. touchMessage = (int) typeDeviceAndTime + (int) TouchIndex::bits + (int) TouchPosition::bits,
  399. touchMessageWithVelocity = (int) touchMessage + (int) TouchVelocity::bits,
  400. programEventMessage = (int) MessageType::bits + 32 * numProgramMessageInts,
  401. packetACK = (int) MessageType::bits + (int) PacketCounter::bits,
  402. firmwareUpdateACK = (int) MessageType::bits + (int) FirmwareUpdateACKCode::bits + (int) FirmwareUpdateACKDetail::bits,
  403. controlButtonMessage = (int) typeDeviceAndTime + (int) ControlButtonID::bits,
  404. configSetMessage = (int) MessageType::bits + (int) ConfigCommand::bits + (int) ConfigItemIndex::bits + (int) ConfigItemValue::bits,
  405. configRespMessage = (int) MessageType::bits + (int) ConfigCommand::bits + (int) ConfigItemIndex::bits + ((int) ConfigItemValue::bits * 3),
  406. configSyncEndMessage = (int) MessageType::bits + (int) ConfigCommand::bits,
  407. };
  408. //==============================================================================
  409. // These are the littlefoot functions provided for use in BLOCKS programs
  410. static constexpr const char* ledProgramLittleFootFunctions[] =
  411. {
  412. "min/iii",
  413. "min/fff",
  414. "max/iii",
  415. "max/fff",
  416. "clamp/iiii",
  417. "clamp/ffff",
  418. "abs/ii",
  419. "abs/ff",
  420. "map/ffffff",
  421. "map/ffff",
  422. "mod/iii",
  423. "getRandomFloat/f",
  424. "getRandomInt/ii",
  425. "log/vi",
  426. "logHex/vi",
  427. "getMillisecondCounter/i",
  428. "getFirmwareVersion/i",
  429. "getTimeInCurrentFunctionCall/i",
  430. "getBatteryLevel/f",
  431. "isBatteryCharging/b",
  432. "isMasterBlock/b",
  433. "isConnectedToHost/b",
  434. "setStatusOverlayActive/vb",
  435. "getNumBlocksInTopology/i",
  436. "getBlockIDForIndex/ii",
  437. "getBlockIDOnPort/ii",
  438. "getPortToMaster/i",
  439. "getBlockTypeForID/ii",
  440. "sendMessageToBlock/viiii",
  441. "sendMessageToHost/viii",
  442. "getHorizontalDistFromMaster/i",
  443. "getVerticalDistFromMaster/i",
  444. "getAngleFromMaster/i",
  445. "setAutoRotate/vb",
  446. "getClusterIndex/i",
  447. "getClusterWidth/i",
  448. "getClusterHeight/i",
  449. "getClusterXpos/i",
  450. "getClusterYpos/i",
  451. "getNumBlocksInCurrentCluster/i",
  452. "getBlockIdForBlockInCluster/ii",
  453. "isMasterInCurrentCluster/b",
  454. "setClusteringActive/vb",
  455. "makeARGB/iiiii",
  456. "blendARGB/iii",
  457. "fillPixel/viii",
  458. "blendPixel/viii",
  459. "fillRect/viiiii",
  460. "blendRect/viiiii",
  461. "blendGradientRect/viiiiiiii",
  462. "blendCircle/vifffb",
  463. "addPressurePoint/vifff",
  464. "drawPressureMap/v",
  465. "fadePressureMap/v",
  466. "drawNumber/viiii",
  467. "clearDisplay/v",
  468. "clearDisplay/vi",
  469. "displayBatteryLevel/v",
  470. "sendMIDI/vi",
  471. "sendMIDI/vii",
  472. "sendMIDI/viii",
  473. "sendNoteOn/viii",
  474. "sendNoteOff/viii",
  475. "sendAftertouch/viii",
  476. "sendCC/viii",
  477. "sendPitchBend/vii",
  478. "sendPitchBend/viii",
  479. "sendChannelPressure/vii",
  480. "addPitchCorrectionPad/viiffff",
  481. "setPitchCorrectionEnabled/vb",
  482. "getPitchCorrectionPitchBend/iii",
  483. "setChannelRange/vbii",
  484. "assignChannel/ii",
  485. "deassignChannel/vii",
  486. "getControlChannel/i",
  487. "useMPEDuplicateFilter/vb",
  488. "getSensorValue/iii",
  489. "handleTouchAsSeaboard/vi",
  490. "setPowerSavingEnabled/vb",
  491. "getLocalConfig/ii",
  492. "setLocalConfig/vii",
  493. "requestRemoteConfig/vii",
  494. "setRemoteConfig/viii",
  495. "setLocalConfigItemRange/viii",
  496. "setLocalConfigActiveState/vibb",
  497. "linkBlockIDtoController/vi",
  498. "repaintControl/v",
  499. "onControlPress/vi",
  500. "onControlRelease/vi",
  501. "initControl/viiiiiiiii",
  502. "setButtonMode/vii",
  503. "setButtonType/viii",
  504. "setButtonMinMaxDefault/viiii",
  505. "setButtonColours/viii",
  506. "setButtonTriState/vii",
  507. "padControllerInitDefault/vb",
  508. "padControllerReset/v",
  509. "padControllerRegenDefault/v",
  510. "padControllerRepaint/v",
  511. "padControllerDrawPad/vi",
  512. "setUseDefaultKeyHandler/vb",
  513. "setUseDefaultKeyHandler/vbb",
  514. nullptr
  515. };
  516. } // namespace BlocksProtocol
  517. } // namespace juce