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.

546 lines
17KB

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