17#include "Systemic/Internal/GuardedList.h"
42#pragma warning(disable : 4100)
52 virtual void onFirmwareDateChanged(std::shared_ptr<Pixel> pixel, std::chrono::system_clock::time_point firmwareDate) {}
67 virtual void onRolled(std::shared_ptr<Pixel> pixel,
int face) {}
70 virtual void onMessageReceived(std::shared_ptr<Pixel> pixel, std::shared_ptr<const Messages::PixelMessage> message) {}
87 using StatusCallback = std::function<void(
PixelStatus)>;
88 using MessageCallback = std::function<void(std::shared_ptr<const Messages::PixelMessage>)>;
91 const std::shared_ptr<Systemic::BluetoothLE::Peripheral> _peripheral;
92 const std::shared_ptr<PixelDelegate> _delegate;
97 std::shared_ptr<Systemic::BluetoothLE::Characteristic> _notifyCharacteristic{};
98 std::shared_ptr<Systemic::BluetoothLE::Characteristic> _writeCharacteristic{};
101 std::recursive_mutex _mutex{};
140 std::shared_ptr<PixelDelegate> delegate =
nullptr)
156 std::shared_ptr<PixelDelegate> delegate =
nullptr)
158 return std::shared_ptr<Pixel>(
new Pixel{ scannedPixel, delegate });
181 return _status == PixelStatus::Ready;
199 virtual const std::wstring&
name()
const override
219 virtual int rssi()
const override
264 std::vector<uint8_t> data{
static_cast<uint8_t
>(type) };
275 template <
typename T, std::enable_if_t<std::is_base_of_v<Messages::PixelMessage, T>,
int> = 0>
278 std::vector<uint8_t> data{};
279 Messages::Serialization::serializeMessage(message, data);
292 template <
class Rep,
class Period>
296 std::chrono::duration<Rep, Period> timeout = std::chrono::seconds(5))
298 std::promise<std::shared_ptr<const Messages::PixelMessage>> responsePromise{};
300 MessageCallback callback{ [&responseType, &responsePromise](
auto msg)
302 if (msg->type == responseType)
304 responsePromise.set_value(msg);
308 const auto cbIndex = _internalMsgCbs.
add(callback);
312 std::shared_ptr<const Messages::PixelMessage> response{};
315 auto respFuture = responsePromise.get_future();
316 if (respFuture.wait_for(timeout) == std::future_status::ready)
318 response = respFuture.get();
322 _internalMsgCbs.
remove(cbIndex);
348 template <
class Rep,
class Period>
351 std::chrono::duration<Rep, Period> minInterval)
354 msg.
requestMode = activate ? Messages::TelemetryRequestMode::Automatic : Messages::TelemetryRequestMode::Off;
355 down_cast(msg.minInterval, std::chrono::milliseconds{ minInterval }.count());
385 template <
class Rep,
class Period>
387 std::chrono::duration<Rep, Period> duration,
393 down_cast(msg.count, count);
394 down_cast(msg.duration, std::chrono::milliseconds{ duration }.count());
395 msg.color = rgbColor;
396 msg.faceMask = 0xFFFF;
397 down_cast(msg.fade, 255 * fade);
404 Pixel(
const ScannedPixel& scannedPixel, std::shared_ptr<PixelDelegate> delegate);
406 std::future<ConnectResult> internalSetupAsync();
407 void onValueChanged(
const std::vector<uint8_t>& data);
409 std::future<bool>
sendMessageAsync(
const std::vector<uint8_t>& data,
bool withoutAck =
false);
411 template <
typename T1,
typename T2>
412 static T1 down_cast(T1& dst, T2 src)
414 return dst =
static_cast<T1
>(std::min<T2>(src, std::numeric_limits<T1>::max()));
Pixel messages serialization functions.
MessageType
Lists all the Pixel dice message types. The value is used for the first byte of data in a Pixel messa...
Definition: Messages.h:19
Definition of the ScannedPixel class.
A simple thread safe list of items.
Definition: GuardedList.h:16
Index add(const T &item)
Adds the given item to the list and returns its index.
Definition: GuardedList.h:39
void remove(const Index &index)
Removes the item at the given index from the list.
Definition: GuardedList.h:50
Represents a Pixels die.
Definition: Pixel.h:86
virtual const std::wstring & name() const override
Gets the Pixel name.
Definition: Pixel.h:199
virtual int rssi() const override
Gets the last RSSI value measured by the Pixel.
Definition: Pixel.h:219
virtual bluetooth_address_t address() const override
Gets the Bluetooth address of the Pixel.
Definition: Pixel.h:189
virtual Date firmwareDate() const override
Gets the firmware build date of the Pixel.
Definition: Pixel.h:214
std::future< std::shared_ptr< const Messages::PixelMessage > > sendAndWaitForResponseAsync(Messages::MessageType type, Messages::MessageType responseType, std::chrono::duration< Rep, Period > timeout=std::chrono::seconds(5))
Sends a message to the Pixel and wait for a specific reply.
Definition: Pixel.h:293
virtual PixelDesignAndColor designAndColor() const override
Gets the design and color of the Pixel.
Definition: Pixel.h:209
std::future< bool > sendMessageAsync(const T &message, bool withoutAck=false)
Sends a message to the Pixel.
Definition: Pixel.h:276
static std::shared_ptr< Pixel > create(bluetooth_address_t address, std::shared_ptr< PixelDelegate > delegate=nullptr)
Initializes a new instance of Pixel for the device with the given Bluetooth address.
Definition: Pixel.h:138
bool isReady() const
Indicates whether the Pixel status is "ready".
Definition: Pixel.h:179
std::future< bool > reportRssiAsync(bool activate, std::chrono::duration< Rep, Period > minInterval)
Requests the Pixel to regularly send its measured RSSI value.
Definition: Pixel.h:349
virtual pixel_id_t pixelId() const override
Gets the unique Pixel id of the die.
Definition: Pixel.h:194
virtual int currentFace() const override
Gets the Pixel face value that is currently facing up.
Definition: Pixel.h:239
virtual ~Pixel()=default
Default virtual destructor.
virtual int batteryLevel() const override
Gets the Pixel battery level (percentage).
Definition: Pixel.h:224
std::future< bool > reportRssiAsync(bool activate=true)
Requests the Pixel to regularly send its measured RSSI value.
Definition: Pixel.h:364
virtual bool isCharging() const override
Indicates whether the Pixel battery is charging or not. Set to 'true' if fully charged but still on c...
Definition: Pixel.h:229
ConnectResult
List of possible Pixel connection results.
Definition: Pixel.h:110
@ SubscriptionError
Connection failed because subscribing to the Bluetooth characteristic failed.
@ IdentificationMismatch
Connection failed because the die Pixel id is not the expected one.
@ IdentificationTimeout
Connection failed because the die didn't identify itself in time.
@ Success
Connection has succeeded.
@ Cancelled
Connection was canceled (usually because of a call to Pixel.disconnect()).
@ ConnectionFailed
Connection with the actual device failed.
std::future< bool > sendMessageAsync(Messages::MessageType type, bool withoutAck=false)
Sends a message to the Pixel.
Definition: Pixel.h:262
std::future< bool > turnOffAsync()
Requests the Pixel to turn itself off.
virtual PixelRollState rollState() const override
Gets the Pixel roll state.
Definition: Pixel.h:234
std::future< ConnectResult > connectAsync()
Asynchronously tries to connect to the die.
virtual int ledCount() const override
Gets the number of LEDs of the Pixel.
Definition: Pixel.h:204
virtual bluetooth_address_t systemId() const override
Gets the unique id assigned by the OS to Pixel Bluetooth peripheral.
Definition: Pixel.h:184
std::future< bool > blinkAsync(std::chrono::duration< Rep, Period > duration, uint32_t rgbColor, int count=1, float fade=1)
Requests the Pixel to blink.
Definition: Pixel.h:386
static std::shared_ptr< Pixel > create(const ScannedPixel &scannedPixel, std::shared_ptr< PixelDelegate > delegate=nullptr)
Initializes a new instance of Pixel for the device corresponding to the given scanned Pixel data.
Definition: Pixel.h:154
void disconnect()
Immediately disconnects from the die.
std::future< std::shared_ptr< const Messages::PixelMessage > > sendAndWaitForResponseAsync(Messages::MessageType type, Messages::MessageType responseType)
Sends a message to the Pixel and wait for a specific reply.
Definition: Pixel.h:333
PixelStatus status() const
Gets the last known connection status of the Pixel.
Definition: Pixel.h:170
Common accessible values between Pixel advertised data and a connected Pixel.
Definition: PixelInfo.h:20
std::chrono::system_clock::time_point Date
Type for a system Date.
Definition: PixelInfo.h:27
Data periodically emitted by a Pixel when not connected to a device.
Definition: ScannedPixel.h:59
A collection of C++ classes that provides a simplified access to Bluetooth Low Energy peripherals.
Definition: BleTypes.h:38
@ Peripheral
Disconnection was initiated by peripheral.
A collection of C++ classes and types to scan for and connect to Pixels dice.
Definition: Helpers.h:13
PixelStatus
The different possible connection statuses of a Pixel.
Definition: Pixel.h:31
PixelRollState
Pixel roll states.
Definition: PixelTypes.h:45
PixelDesignAndColor
Available combinations of Pixel designs and colors.
Definition: PixelTypes.h:27
uint32_t pixel_id_t
Type for a Pixel Id.
Definition: PixelTypes.h:23
Systemic::BluetoothLE::bluetooth_address_t bluetooth_address_t
Type for a Bluetooth address.
Definition: PixelInfo.h:16
Message send to a Pixel to have it blink its LEDs.
Definition: Messages.h:167
Definition: Messages.h:96
Interface for a class that handles Pixel events.
Definition: Pixel.h:46
virtual void onRolled(std::shared_ptr< Pixel > pixel, int face)
Called just after the Pixel was rolled.
Definition: Pixel.h:67
virtual void onRollStateChanged(std::shared_ptr< Pixel > pixel, PixelRollState state, int face)
Called when the Pixel roll state changes.
Definition: Pixel.h:64
virtual void onChargingStateChanged(std::shared_ptr< Pixel > pixel, bool isCharging)
Called when the Pixel charging state changes.
Definition: Pixel.h:61
virtual void onMessageReceived(std::shared_ptr< Pixel > pixel, std::shared_ptr< const Messages::PixelMessage > message)
Called when the Pixel instance received a message from the actual die.
Definition: Pixel.h:70
virtual void onStatusChanged(std::shared_ptr< Pixel > pixel, PixelStatus status)
Called when the Pixel status changes.
Definition: Pixel.h:48
virtual void onFirmwareDateChanged(std::shared_ptr< Pixel > pixel, std::chrono::system_clock::time_point firmwareDate)
Called when the Pixel firmware date changes.
Definition: Pixel.h:52
virtual void onBatteryLevelChanged(std::shared_ptr< Pixel > pixel, int batteryLevel)
Called when the Pixel battery level (in percent) changes.
Definition: Pixel.h:58
virtual void onRssiChanged(std::shared_ptr< Pixel > pixel, int rssi)
Called when the Pixel measured RSSI changes.
Definition: Pixel.h:55
Data for ScannedPixel class.
Definition: ScannedPixel.h:18
int currentFace
The Pixel face value that is currently facing up.
Definition: ScannedPixel.h:54
bool isCharging
Definition: ScannedPixel.h:48
PixelRollState rollState
The Pixel roll state.
Definition: ScannedPixel.h:51
pixel_id_t pixelId
The unique Pixel id of the device.
Definition: ScannedPixel.h:26
Date firmwareDate
The firmware build date of the Pixel.
Definition: ScannedPixel.h:38
int batteryLevel
The Pixel battery level (percentage).
Definition: ScannedPixel.h:44
std::wstring name
The Pixel name.
Definition: ScannedPixel.h:29
PixelDesignAndColor designAndColor
The Pixel design and color.
Definition: ScannedPixel.h:35
int ledCount
The number of LEDs of the Pixel.
Definition: ScannedPixel.h:32
int rssi
The last RSSI value measured by this Pixel.
Definition: ScannedPixel.h:41
bluetooth_address_t address
The Bluetooth address for the Pixel.
Definition: ScannedPixel.h:23