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.

422 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. /**
  18. Represents an individual BLOCKS device.
  19. */
  20. class Block : public juce::ReferenceCountedObject
  21. {
  22. public:
  23. //==============================================================================
  24. /** Destructor. */
  25. virtual ~Block();
  26. /** The different block types.
  27. @see Block::getType()
  28. */
  29. enum Type
  30. {
  31. unknown = 0,
  32. lightPadBlock,
  33. liveBlock,
  34. loopBlock,
  35. developerControlBlock,
  36. touchBlock,
  37. seaboardBlock // on-screen seaboard view
  38. };
  39. /** The Block class is reference-counted, so always use a Block::Ptr when
  40. you are keeping references to them.
  41. */
  42. using Ptr = juce::ReferenceCountedObjectPtr<Block>;
  43. /** The Block class is reference-counted, so Block::Array is useful when
  44. you are storing lists of them.
  45. */
  46. using Array = juce::ReferenceCountedArray<Block>;
  47. /** The Block's serial number. */
  48. const juce::String serialNumber;
  49. /** The Block's version number */
  50. juce::String versionNumber;
  51. using UID = uint64;
  52. /** This Block's UID.
  53. This will be globally unique, and remains constant for a particular device.
  54. */
  55. const UID uid;
  56. //==============================================================================
  57. /** Returns the type of this device.
  58. @see Block::Type
  59. */
  60. virtual Type getType() const = 0;
  61. /** Returns a human-readable description of this device type. */
  62. virtual juce::String getDeviceDescription() const = 0;
  63. /** Returns the battery level in the range 0.0 to 1.0. */
  64. virtual float getBatteryLevel() const = 0;
  65. /** Returns true if the battery is charging. */
  66. virtual bool isBatteryCharging() const = 0;
  67. //==============================================================================
  68. /** Returns true if this block is connected and active. */
  69. virtual bool isConnected() const = 0;
  70. /** Returns true if this block is directly connected to the application,
  71. as opposed to only being connected to a different block via a connection port.
  72. @see ConnectionPort
  73. */
  74. virtual bool isMasterBlock() const = 0;
  75. //==============================================================================
  76. /** Returns the width of the device in logical device units. */
  77. virtual int getWidth() const = 0;
  78. /** Returns the height of the device in logical device units. */
  79. virtual int getHeight() const = 0;
  80. /** Returns true if the device is a physical hardware block (i.e. not a virtual block). */
  81. virtual bool isHardwareBlock() const = 0;
  82. /** Returns the length of one logical device unit as physical millimeters. */
  83. virtual float getMillimetersPerUnit() const = 0;
  84. //==============================================================================
  85. /** If this block has a grid of LEDs, this will return an object to control it.
  86. Note that the pointer that is returned belongs to this object, and the caller must
  87. neither delete it or use it after the lifetime of this Block object has finished.
  88. If there are no LEDs, then this method will return nullptr.
  89. */
  90. virtual LEDGrid* getLEDGrid() const = 0;
  91. /** If this block has a row of LEDs, this will return an object to control it.
  92. Note that the pointer that is returned belongs to this object, and the caller must
  93. neither delete it or use it after the lifetime of this Block object has finished.
  94. If there are no LEDs, then this method will return nullptr.
  95. */
  96. virtual LEDRow* getLEDRow() const = 0;
  97. /** If this block has any status LEDs, this will return an array of objects to control them.
  98. Note that the objects in the array belong to this Block object, and the caller must
  99. neither delete them or use them after the lifetime of this Block object has finished.
  100. */
  101. virtual juce::Array<StatusLight*> getStatusLights() const = 0;
  102. /** If this block has a pressure-sensitive surface, this will return an object to
  103. access its data.
  104. Note that the pointer returned does is owned by this object, and the caller must
  105. neither delete it or use it after the lifetime of this Block object has finished.
  106. If the device is not touch-sensitive, then this method will return nullptr.
  107. */
  108. virtual TouchSurface* getTouchSurface() const = 0;
  109. /** If this block has any control buttons, this will return an array of objects to control them.
  110. Note that the objects in the array belong to this Block object, and the caller must
  111. neither delete them or use them after the lifetime of this Block object has finished.
  112. */
  113. virtual juce::Array<ControlButton*> getButtons() const = 0;
  114. //==============================================================================
  115. /** This returns true if the block supports generating graphics by drawing into a JUCE
  116. Graphics context. This should only be true for virtual on-screen blocks; hardware
  117. blocks will instead use the LED Grid for visuals.
  118. */
  119. virtual bool supportsGraphics() const = 0;
  120. //==============================================================================
  121. /** These are the edge-connectors that a device may have. */
  122. struct ConnectionPort
  123. {
  124. enum class DeviceEdge
  125. {
  126. north,
  127. south,
  128. east,
  129. west
  130. };
  131. /** The side of the device on which this port is located. */
  132. DeviceEdge edge;
  133. /** The index of this port along the device edge.
  134. For north and south edges, index 0 is the left-most port.
  135. For east and west edges, index 0 is the top-most port.
  136. */
  137. int index;
  138. bool operator== (const ConnectionPort&) const noexcept;
  139. bool operator!= (const ConnectionPort&) const noexcept;
  140. };
  141. /** Returns a list of the connectors that this device has. */
  142. virtual juce::Array<ConnectionPort> getPorts() const = 0;
  143. //==============================================================================
  144. /** A program that can be loaded onto a block. */
  145. struct Program
  146. {
  147. /** Creates a Program for the corresponding LEDGrid. */
  148. Program (Block&);
  149. /** Destructor. */
  150. virtual ~Program();
  151. /** Returns the LittleFoot program to execute on the BLOCKS device. */
  152. virtual juce::String getLittleFootProgram() = 0;
  153. Block& block;
  154. };
  155. /** Sets the Program to run on this block.
  156. The supplied Program's lifetime will be managed by this class, so do not
  157. use the Program in other places in your code.
  158. */
  159. virtual juce::Result setProgram (Program*) = 0;
  160. /** Returns a pointer to the currently loaded program. */
  161. virtual Program* getProgram() const = 0;
  162. //==============================================================================
  163. /** A message that can be sent to the currently loaded program. */
  164. struct ProgramEventMessage
  165. {
  166. int32 values[3];
  167. };
  168. /** Sends a message to the currently loaded program.
  169. To receive the message the program must provide a littlefoot function called
  170. handleMessage with the following form:
  171. @code
  172. void handleMessage (int param1, int param2, int param3)
  173. {
  174. // Do something with the two integer parameters that the app has sent...
  175. }
  176. @endcode
  177. */
  178. virtual void sendProgramEvent (const ProgramEventMessage&) = 0;
  179. /** Interface for objects listening to custom program events. */
  180. struct ProgramEventListener
  181. {
  182. virtual ~ProgramEventListener() {}
  183. /** Called whenever a message from a block is received. */
  184. virtual void handleProgramEvent (Block& source, const ProgramEventMessage&) = 0;
  185. };
  186. /** Adds a new listener for custom program events from the block. */
  187. virtual void addProgramEventListener (ProgramEventListener*);
  188. /** Removes a listener for custom program events from the block. */
  189. virtual void removeProgramEventListener (ProgramEventListener*);
  190. //==============================================================================
  191. /** Returns the size of the data block that setDataByte and other functions can write to. */
  192. virtual uint32 getMemorySize() = 0;
  193. /** Sets a single byte on the littlefoot heap. */
  194. virtual void setDataByte (size_t offset, uint8 value) = 0;
  195. /** Sets multiple bytes on the littlefoot heap. */
  196. virtual void setDataBytes (size_t offset, const void* data, size_t num) = 0;
  197. /** Sets multiple bits on the littlefoot heap. */
  198. virtual void setDataBits (uint32 startBit, uint32 numBits, uint32 value) = 0;
  199. /** Gets a byte from the littlefoot heap. */
  200. virtual uint8 getDataByte (size_t offset) = 0;
  201. /** Sets the current program as the block's default state. */
  202. virtual void saveProgramAsDefault() = 0;
  203. //==============================================================================
  204. /** Metadata for a given config item */
  205. struct ConfigMetaData
  206. {
  207. static constexpr int32 numOptionNames = 8;
  208. ConfigMetaData() {}
  209. // Constructor to work around VS2015 bugs...
  210. ConfigMetaData (uint32 itemIndex,
  211. int32 itemValue,
  212. juce::Range<int32> rangeToUse,
  213. bool active,
  214. const char* itemName,
  215. uint32 itemType,
  216. const char* options[ConfigMetaData::numOptionNames],
  217. const char* groupName)
  218. : item (itemIndex),
  219. value (itemValue),
  220. range (rangeToUse),
  221. isActive (active),
  222. name (itemName),
  223. type (itemType),
  224. group (groupName)
  225. {
  226. for (int i = 0; i < numOptionNames; ++i)
  227. optionNames[i] = options[i];
  228. }
  229. ConfigMetaData (const ConfigMetaData& other)
  230. {
  231. *this = other;
  232. }
  233. const ConfigMetaData& operator= (const ConfigMetaData& other)
  234. {
  235. if (this != &other)
  236. {
  237. item = other.item;
  238. value = other.value;
  239. range = other.range;
  240. isActive = other.isActive;
  241. name = other.name;
  242. type = other.type;
  243. group = other.group;
  244. for (int i = 0; i < numOptionNames; ++i)
  245. optionNames[i] = other.optionNames[i];
  246. }
  247. return *this;
  248. }
  249. bool operator== (const ConfigMetaData& other) const
  250. {
  251. for (int32 optionIndex = 0; optionIndex < numOptionNames; ++optionIndex)
  252. if (optionNames[optionIndex] != other.optionNames[optionIndex])
  253. return false;
  254. return item == other.item
  255. && value == other.value
  256. && range == other.range
  257. && isActive == other.isActive
  258. && name == other.name
  259. && group == other.group;
  260. }
  261. bool operator != (const ConfigMetaData& other) const
  262. {
  263. return ! (*this == other);
  264. }
  265. uint32 item = 0;
  266. int32 value = 0;
  267. juce::Range<int32> range;
  268. bool isActive = false;
  269. juce::String name;
  270. uint32 type = 0;
  271. juce::String optionNames[numOptionNames] = {};
  272. juce::String group;
  273. };
  274. /** Returns the maximum number of config items available */
  275. virtual uint32 getMaxConfigIndex() = 0;
  276. /** Determine if this is a valid config item index */
  277. virtual bool isValidUserConfigIndex (uint32 item) = 0;
  278. /** Get local config item value */
  279. virtual int32 getLocalConfigValue (uint32 item) = 0;
  280. /** Set local config item value */
  281. virtual void setLocalConfigValue (uint32 item, int32 value) = 0;
  282. /** Set local config item range */
  283. virtual void setLocalConfigRange (uint32 item, int32 min, int32 max) = 0;
  284. /** Set if config item is active or not */
  285. virtual void setLocalConfigItemActive (uint32 item, bool isActive) = 0;
  286. /** Determine if config item is active or not */
  287. virtual bool isLocalConfigItemActive (uint32 item) = 0;
  288. /** Get config item metadata */
  289. virtual ConfigMetaData getLocalConfigMetaData (uint32 item) = 0;
  290. /** Request sync of factory config with block */
  291. virtual void requestFactoryConfigSync() = 0;
  292. /** Reset all items active status */
  293. virtual void resetConfigListActiveStatus() = 0;
  294. //==============================================================================
  295. /** Allows the user to provide a function that will receive log messages from the block. */
  296. virtual void setLogger (std::function<void(const String&)> loggingCallback) = 0;
  297. /** Sends a firmware update packet to a block, and waits for a reply. Returns an error code. */
  298. virtual bool sendFirmwareUpdatePacket (const uint8* data, uint8 size,
  299. std::function<void (uint8)> packetAckCallback) = 0;
  300. /** Provides a callback that will be called when a config changes. */
  301. virtual void setConfigChangedCallback (std::function<void(Block&, const ConfigMetaData&, uint32)>) = 0;
  302. //==============================================================================
  303. /** Interface for objects listening to input data port. */
  304. struct DataInputPortListener
  305. {
  306. virtual ~DataInputPortListener() {}
  307. /** Called whenever a message from a block is received. */
  308. virtual void handleIncomingDataPortMessage (Block& source, const void* messageData, size_t messageSize) = 0;
  309. };
  310. /** Adds a new listener for the data input port. */
  311. virtual void addDataInputPortListener (DataInputPortListener*);
  312. /** Removes a listener for the data input port. */
  313. virtual void removeDataInputPortListener (DataInputPortListener*);
  314. /** Sends a message to the block. */
  315. virtual void sendMessage (const void* messageData, size_t messageSize) = 0;
  316. //==============================================================================
  317. /** This type is used for timestamping events. It represents a number of milliseconds since the block
  318. device was booted.
  319. */
  320. using Timestamp = uint32;
  321. protected:
  322. //==============================================================================
  323. Block (const juce::String& serialNumberToUse);
  324. Block (const juce::String& serial, const juce::String& version);
  325. juce::ListenerList<DataInputPortListener> dataInputPortListeners;
  326. juce::ListenerList<ProgramEventListener> programEventListeners;
  327. private:
  328. //==============================================================================
  329. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Block)
  330. };