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.

303 lines
12KB

  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. seaboardBlock // on-screen seaboard view
  37. };
  38. /** The Block class is reference-counted, so always use a Block::Ptr when
  39. you are keeping references to them.
  40. */
  41. using Ptr = juce::ReferenceCountedObjectPtr<Block>;
  42. /** The Block class is reference-counted, so Block::Array is useful when
  43. you are storing lists of them.
  44. */
  45. using Array = juce::ReferenceCountedArray<Block>;
  46. /** The Block's serial number. */
  47. const juce::String serialNumber;
  48. using UID = uint64;
  49. /** This Block's UID.
  50. This will be globally unique, and remains constant for a particular device.
  51. */
  52. const UID uid;
  53. //==============================================================================
  54. /** Returns the type of this device.
  55. @see Block::Type
  56. */
  57. virtual Type getType() const = 0;
  58. /** Returns a human-readable description of this device type. */
  59. virtual juce::String getDeviceDescription() const = 0;
  60. /** Returns the battery level in the range 0.0 to 1.0. */
  61. virtual float getBatteryLevel() const = 0;
  62. /** Returns true if the battery is charging. */
  63. virtual bool isBatteryCharging() const = 0;
  64. //==============================================================================
  65. /** Returns true if this block is connected and active. */
  66. virtual bool isConnected() const = 0;
  67. /** Returns true if this block is directly connected to the application,
  68. as opposed to only being connected to a different block via a connection port.
  69. @see ConnectionPort
  70. */
  71. virtual bool isMasterBlock() const = 0;
  72. //==============================================================================
  73. /** Returns the width of the device in logical device units. */
  74. virtual int getWidth() const = 0;
  75. /** Returns the height of the device in logical device units. */
  76. virtual int getHeight() const = 0;
  77. /** Returns true if the device is a physical hardware block (i.e. not a virtual block). */
  78. virtual bool isHardwareBlock() const = 0;
  79. /** Returns the length of one logical device unit as physical millimeters. */
  80. virtual float getMillimetersPerUnit() const = 0;
  81. //==============================================================================
  82. /** If this block has a grid of LEDs, this will return an object to control it.
  83. Note that the pointer that is returned belongs to this object, and the caller must
  84. neither delete it or use it after the lifetime of this Block object has finished.
  85. If there are no LEDs, then this method will return nullptr.
  86. */
  87. virtual LEDGrid* getLEDGrid() const = 0;
  88. /** If this block has a row of LEDs, this will return an object to control it.
  89. Note that the pointer that is returned belongs to this object, and the caller must
  90. neither delete it or use it after the lifetime of this Block object has finished.
  91. If there are no LEDs, then this method will return nullptr.
  92. */
  93. virtual LEDRow* getLEDRow() const = 0;
  94. /** If this block has any status LEDs, this will return an array of objects to control them.
  95. Note that the objects in the array belong to this Block object, and the caller must
  96. neither delete them or use them after the lifetime of this Block object has finished.
  97. */
  98. virtual juce::Array<StatusLight*> getStatusLights() const = 0;
  99. /** If this block has a pressure-sensitive surface, this will return an object to
  100. access its data.
  101. Note that the pointer returned does is owned by this object, and the caller must
  102. neither delete it or use it after the lifetime of this Block object has finished.
  103. If the device is not touch-sensitive, then this method will return nullptr.
  104. */
  105. virtual TouchSurface* getTouchSurface() const = 0;
  106. /** If this block has any control buttons, this will return an array of objects to control them.
  107. Note that the objects in the array belong to this Block object, and the caller must
  108. neither delete them or use them after the lifetime of this Block object has finished.
  109. */
  110. virtual juce::Array<ControlButton*> getButtons() const = 0;
  111. //==============================================================================
  112. /** This returns true if the block supports generating graphics by drawing into a JUCE
  113. Graphics context. This should only be true for virtual on-screen blocks; hardware
  114. blocks will instead use the LED Grid for visuals.
  115. */
  116. virtual bool supportsGraphics() const = 0;
  117. //==============================================================================
  118. /** These are the edge-connectors that a device may have. */
  119. struct ConnectionPort
  120. {
  121. enum class DeviceEdge
  122. {
  123. north,
  124. south,
  125. east,
  126. west
  127. };
  128. /** The side of the device on which this port is located. */
  129. DeviceEdge edge;
  130. /** The index of this port along the device edge.
  131. For north and south edges, index 0 is the left-most port.
  132. For east and west edges, index 0 is the top-most port.
  133. */
  134. int index;
  135. bool operator== (const ConnectionPort&) const noexcept;
  136. bool operator!= (const ConnectionPort&) const noexcept;
  137. };
  138. /** Returns a list of the connectors that this device has. */
  139. virtual juce::Array<ConnectionPort> getPorts() const = 0;
  140. //==============================================================================
  141. /** A program that can be loaded onto a block. */
  142. struct Program
  143. {
  144. /** Creates a Program for the corresponding LEDGrid. */
  145. Program (Block&);
  146. /** Destructor. */
  147. virtual ~Program();
  148. /** Returns the LittleFoot program to execute on the BLOCKS device. */
  149. virtual juce::String getLittleFootProgram() = 0;
  150. Block& block;
  151. };
  152. /** Sets the Program to run on this block.
  153. The supplied Program's lifetime will be managed by this class, so do not
  154. use the Program in other places in your code.
  155. */
  156. virtual juce::Result setProgram (Program*) = 0;
  157. /** Returns a pointer to the currently loaded program. */
  158. virtual Program* getProgram() const = 0;
  159. //==============================================================================
  160. /** A message that can be sent to the currently loaded program. */
  161. struct ProgramEventMessage
  162. {
  163. int32 values[3];
  164. };
  165. /** Sends a message to the currently loaded program.
  166. To receive the message the program must provide a littlefoot function called
  167. handleMessage with the following form:
  168. @code
  169. void handleMessage (int param1, int param2, int param3)
  170. {
  171. // Do something with the two integer parameters that the app has sent...
  172. }
  173. @endcode
  174. */
  175. virtual void sendProgramEvent (const ProgramEventMessage&) = 0;
  176. /** Interface for objects listening to custom program events. */
  177. struct ProgramEventListener
  178. {
  179. virtual ~ProgramEventListener() {}
  180. /** Called whenever a message from a block is received. */
  181. virtual void handleProgramEvent (Block& source, const ProgramEventMessage&) = 0;
  182. };
  183. /** Adds a new listener for custom program events from the block. */
  184. virtual void addProgramEventListener (ProgramEventListener*);
  185. /** Removes a listener for custom program events from the block. */
  186. virtual void removeProgramEventListener (ProgramEventListener*);
  187. //==============================================================================
  188. /** Returns the size of the data block that setDataByte and other functions can write to. */
  189. virtual uint32 getMemorySize() = 0;
  190. /** Sets a single byte on the littlefoot heap. */
  191. virtual void setDataByte (size_t offset, uint8 value) = 0;
  192. /** Sets multiple bytes on the littlefoot heap. */
  193. virtual void setDataBytes (size_t offset, const void* data, size_t num) = 0;
  194. /** Sets multiple bits on the littlefoot heap. */
  195. virtual void setDataBits (uint32 startBit, uint32 numBits, uint32 value) = 0;
  196. /** Gets a byte from the littlefoot heap. */
  197. virtual uint8 getDataByte (size_t offset) = 0;
  198. /** Sets the current program as the block's default state. */
  199. virtual void saveProgramAsDefault() = 0;
  200. //==============================================================================
  201. /** Allows the user to provide a function that will receive log messages from the block. */
  202. virtual void setLogger (std::function<void(const String&)> loggingCallback) = 0;
  203. /** Sends a firmware update packet to a block, and waits for a reply. Returns an error code. */
  204. virtual bool sendFirmwareUpdatePacket (const uint8* data, uint8 size,
  205. std::function<void (uint8)> packetAckCallback) = 0;
  206. //==============================================================================
  207. /** Interface for objects listening to input data port. */
  208. struct DataInputPortListener
  209. {
  210. virtual ~DataInputPortListener() {}
  211. /** Called whenever a message from a block is received. */
  212. virtual void handleIncomingDataPortMessage (Block& source, const void* messageData, size_t messageSize) = 0;
  213. };
  214. /** Adds a new listener for the data input port. */
  215. virtual void addDataInputPortListener (DataInputPortListener*);
  216. /** Removes a listener for the data input port. */
  217. virtual void removeDataInputPortListener (DataInputPortListener*);
  218. /** Sends a message to the block. */
  219. virtual void sendMessage (const void* messageData, size_t messageSize) = 0;
  220. //==============================================================================
  221. /** This type is used for timestamping events. It represents a number of milliseconds since the block
  222. device was booted.
  223. */
  224. using Timestamp = uint32;
  225. protected:
  226. //==============================================================================
  227. Block (const juce::String& serialNumberToUse);
  228. juce::ListenerList<DataInputPortListener> dataInputPortListeners;
  229. juce::ListenerList<ProgramEventListener> programEventListeners;
  230. private:
  231. //==============================================================================
  232. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Block)
  233. };