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.

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