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.

324 lines
9.9KB

  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. /**
  22. Helper class for constructing a packet for sending to a BLOCKS device
  23. */
  24. template <int maxPacketBytes>
  25. struct HostPacketBuilder
  26. {
  27. HostPacketBuilder() noexcept {}
  28. HostPacketBuilder (const HostPacketBuilder&) = delete;
  29. HostPacketBuilder (HostPacketBuilder&&) = default;
  30. const void* getData() const noexcept { return data.getData(); }
  31. int size() const noexcept { return data.size(); }
  32. //==============================================================================
  33. void writePacketSysexHeaderBytes (TopologyIndex deviceIndex) noexcept
  34. {
  35. static_assert (maxPacketBytes > 10, "Not enough bytes for a sensible message!");
  36. jassert ((deviceIndex & 64) == 0);
  37. data.writeHeaderSysexBytes (deviceIndex);
  38. }
  39. void writePacketSysexFooter() noexcept
  40. {
  41. data.writePacketSysexFooter();
  42. }
  43. //==============================================================================
  44. bool deviceControlMessage (DeviceCommand command) noexcept
  45. {
  46. if (! data.hasCapacity (MessageType::bits + DeviceCommand::bits))
  47. return false;
  48. writeMessageType (MessageFromHost::deviceCommandMessage);
  49. data << command;
  50. return true;
  51. }
  52. //==============================================================================
  53. bool beginDataChanges (PacketIndex packetIndex) noexcept
  54. {
  55. if (! data.hasCapacity (MessageType::bits + PacketIndex::bits + DataChangeCommand::bits))
  56. return false;
  57. writeMessageType (MessageFromHost::sharedDataChange);
  58. data << packetIndex;
  59. return true;
  60. }
  61. bool endDataChanges (bool isLastChange) noexcept
  62. {
  63. if (! data.hasCapacity (DataChangeCommand::bits))
  64. return false;
  65. data << DataChangeCommand ((uint32) isLastChange ? endOfChanges : endOfPacket);
  66. return true;
  67. }
  68. bool skipBytes (int numToSkip) noexcept
  69. {
  70. if (numToSkip <= 0)
  71. return true;
  72. auto state = data.getState();
  73. while (numToSkip > ByteCountMany::maxValue)
  74. {
  75. if (! skipBytes (ByteCountMany::maxValue))
  76. {
  77. data.restore (state);
  78. return false;
  79. }
  80. numToSkip -= ByteCountMany::maxValue;
  81. }
  82. if (numToSkip > ByteCountFew::maxValue)
  83. {
  84. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountMany::bits))
  85. {
  86. data.restore (state);
  87. return false;
  88. }
  89. data << DataChangeCommand ((uint32) skipBytesMany) << ByteCountMany ((uint32) numToSkip);
  90. return true;
  91. }
  92. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountFew::bits))
  93. {
  94. data.restore (state);
  95. return false;
  96. }
  97. data << DataChangeCommand ((uint32) skipBytesFew) << ByteCountFew ((uint32) numToSkip);
  98. return true;
  99. }
  100. bool setMultipleBytes (const uint8* values, int num) noexcept
  101. {
  102. if (num <= 0)
  103. return true;
  104. if (! data.hasCapacity (DataChangeCommand::bits * 2 + num * (1 + ByteValue::bits)))
  105. return false;
  106. data << DataChangeCommand ((uint32) setSequenceOfBytes);
  107. for (int i = 0; i < num; ++i)
  108. data << ByteValue ((uint32) values[i])
  109. << ByteSequenceContinues (i < num - 1 ? 1 : 0);
  110. return true;
  111. }
  112. bool setMultipleBytes (uint8 value, uint8 lastValue, int num) noexcept
  113. {
  114. if (num <= 0)
  115. return true;
  116. if (num == 1)
  117. return setMultipleBytes (&value, 1); // (this is a more compact message)
  118. auto state = data.getState();
  119. if (num > ByteCountMany::maxValue)
  120. {
  121. if (! setMultipleBytes (value, lastValue, ByteCountMany::maxValue))
  122. {
  123. data.restore (state);
  124. return false;
  125. }
  126. return setMultipleBytes (value, lastValue, num - ByteCountMany::maxValue);
  127. }
  128. if (num > ByteCountFew::maxValue)
  129. {
  130. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountMany::bits + ByteValue::bits))
  131. {
  132. data.restore (state);
  133. return false;
  134. }
  135. data << DataChangeCommand ((uint32) setManyBytesWithValue)
  136. << ByteCountMany ((uint32) num)
  137. << ByteValue ((uint32) value);
  138. return true;
  139. }
  140. if (value == lastValue)
  141. {
  142. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountFew::bits))
  143. {
  144. data.restore (state);
  145. return false;
  146. }
  147. data << DataChangeCommand ((uint32) setFewBytesWithLastValue) << ByteCountFew ((uint32) num);
  148. return true;
  149. }
  150. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountFew::bits + ByteValue::bits))
  151. {
  152. data.restore (state);
  153. return false;
  154. }
  155. data << DataChangeCommand ((uint32) setFewBytesWithValue) << ByteCountFew ((uint32) num)
  156. << ByteValue ((uint32) value);
  157. return true;
  158. }
  159. bool addProgramEventMessage (const int32* messageData)
  160. {
  161. if (! data.hasCapacity (BitSizes::programEventMessage))
  162. return false;
  163. writeMessageType (MessageFromHost::programEventMessage);
  164. for (uint32 i = 0; i < numProgramMessageInts; ++i)
  165. data << IntegerWithBitSize<32> ((uint32) messageData[i]);
  166. return true;
  167. }
  168. bool addFirmwareUpdatePacket (const uint8* packetData, uint8 size)
  169. {
  170. if (! data.hasCapacity (MessageType::bits + FirmwareUpdatePacketSize::bits + 7 * size))
  171. return false;
  172. writeMessageType (MessageFromHost::firmwareUpdatePacket);
  173. data << FirmwareUpdatePacketSize (size);
  174. for (uint8 i = 0; i < size; ++i)
  175. data << IntegerWithBitSize<7> ((uint32) packetData[i]);
  176. return true;
  177. }
  178. //==============================================================================
  179. bool addConfigSetMessage (int32 item, int32 value)
  180. {
  181. if (! data.hasCapacity (BitSizes::configSetMessage))
  182. return false;
  183. writeMessageType(MessageFromHost::configMessage);
  184. ConfigCommand type = ConfigCommands::setConfig;
  185. data << type << IntegerWithBitSize<8> ((uint32) item) << IntegerWithBitSize<32>((uint32) value);
  186. return true;
  187. }
  188. bool addRequestMessage (int32 item)
  189. {
  190. if (! data.hasCapacity (BitSizes::configSetMessage))
  191. return false;
  192. writeMessageType(MessageFromHost::configMessage);
  193. ConfigCommand type = ConfigCommands::requestConfig;
  194. data << type << IntegerWithBitSize<32> (0) << IntegerWithBitSize<8> ((uint32) item);
  195. return true;
  196. }
  197. bool addRequestFactorySyncMessage()
  198. {
  199. if (! data.hasCapacity (MessageType::bits + ConfigCommand::bits))
  200. return false;
  201. writeMessageType (MessageFromHost::configMessage);
  202. ConfigCommand type = ConfigCommands::requestFactorySync;
  203. data << type;
  204. return true;
  205. }
  206. bool addRequestUserSyncMessage()
  207. {
  208. if (! data.hasCapacity (MessageType::bits + ConfigCommand::bits))
  209. return false;
  210. writeMessageType (MessageFromHost::configMessage);
  211. ConfigCommand type = ConfigCommands::requestUserSync;
  212. data << type;
  213. return true;
  214. }
  215. //==============================================================================
  216. bool addFactoryReset()
  217. {
  218. if (! data.hasCapacity (MessageType::bits))
  219. return false;
  220. writeMessageType(MessageFromHost::factoryReset);
  221. return true;
  222. }
  223. bool addBlockReset()
  224. {
  225. if (! data.hasCapacity (MessageType::bits))
  226. return false;
  227. writeMessageType(MessageFromHost::blockReset);
  228. return true;
  229. }
  230. bool addSetBlockName (const juce::String& name)
  231. {
  232. if (name.length() > 32 || ! data.hasCapacity (MessageType::bits + 7 + (7 * name.length())))
  233. return false;
  234. writeMessageType (MessageFromHost::setName);
  235. data << IntegerWithBitSize<7> ((uint32) name.length());
  236. for (auto i = 0; i < name.length(); ++i)
  237. data << IntegerWithBitSize<7> ((uint32) name.toRawUTF8()[i]);
  238. data << IntegerWithBitSize<7> (0);
  239. return true;
  240. }
  241. //==============================================================================
  242. private:
  243. Packed7BitArrayBuilder<maxPacketBytes> data;
  244. void writeMessageType (MessageFromHost type) noexcept
  245. {
  246. data << MessageType ((uint32) type);
  247. }
  248. };
  249. } // namespace BlocksProtocol
  250. } // namespace juce