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.

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