Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
212 changes: 104 additions & 108 deletions PowerMate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//Pull Requests Please!!

#include "pch.h"

#include "VolumeController.h"

using namespace winrt;
Expand All @@ -20,7 +19,6 @@ using namespace Windows::Web::Syndication;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace Windows::Storage::Streams;


enum class STATE_MACHINE
{
INIT,
Expand All @@ -34,53 +32,53 @@ enum class STATE_MACHINE
CHARACTERISTICS_FOUND
};

STATE_MACHINE myStatus{ STATE_MACHINE::INIT };
static STATE_MACHINE myStatus { STATE_MACHINE::INIT };

Windows::Devices::Enumeration::DeviceWatcher deviceWatcher{ nullptr };
Windows::Devices::Bluetooth::BluetoothLEDevice bluetoothLeDevice{ nullptr };
static DeviceWatcher deviceWatcher { nullptr };
static BluetoothLEDevice bluetoothLeDevice { nullptr };

event_token deviceWatcherAddedToken;
event_token deviceWatcherUpdatedToken;
static event_token deviceWatcherAddedToken;
static event_token deviceWatcherUpdatedToken;
//event_token deviceWatcherRemovedToken;
event_token deviceWatcherEnumerationCompletedToken;

std::wstring powerMateDeviceId;
static event_token deviceWatcherEnumerationCompletedToken;

static std::wstring powerMateDeviceId;
std::map<GattDeviceService, std::set<GattCharacteristic>> powerMateServices;
static std::wstring uuid_read { L"{9cf53570-ddd9-47f3-ba63-09acefc60415}" };
static std::wstring uuid_led { L"847d189e-86ee-4bd2-966f-800832b1259d" };

std::wstring uuid_read{ L"{9cf53570-ddd9-47f3-ba63-09acefc60415}" };
std::wstring uuid_led{ L"847d189e-86ee-4bd2-966f-800832b1259d" };

VolumeController masterVolume;
static VolumeController masterVolume;

void deviceAdded(DeviceWatcher sender, DeviceInformation deviceInfo)
static void deviceAdded(DeviceWatcher sender, const DeviceInformation& deviceInfo)
{
std::wstring desiredDevice{ L"PowerMate Bluetooth" };
std::wstring deviceName{ deviceInfo.Name().c_str() };
std::wstring deviceId{ deviceInfo.Id().c_str() };
std::wcout << "Added\n" << deviceId << ": " << deviceName << "\n";

if (deviceName.compare(desiredDevice) == 0)
{
std::wcout << "Added\n" << deviceId << ": " << deviceName << "\n";

powerMateDeviceId = deviceId;
myStatus = STATE_MACHINE::DEVICE_FOUND;
}
}

void deviceUpdated(DeviceWatcher sender, DeviceInformationUpdate deviceInfo)
static void deviceUpdated(DeviceWatcher sender, DeviceInformationUpdate deviceInfo)
{
std::wcout << "Updated\n" << deviceInfo.Id().c_str() << "\n";
//std::wcout << "Updated\n" << deviceInfo.Id().c_str() << "\n";
}

void enumComplete(DeviceWatcher sender, IInspectable args)
static void enumComplete(DeviceWatcher sender, IInspectable args)
{
std::wcout << "Enumeration Complet\n";
std::wcout << "Enumeration Complete\n";
}

/// <summary>
/// Starts a device watcher that looks for all nearby Bluetooth devices (paired or unpaired).
/// Attaches event handlers to populate the device collection.
/// </summary>
void StartBleDeviceWatcher()
static void StartBleDeviceWatcher()
{
// Additional properties we would like about the device.
// Property strings are documented here https://msdn.microsoft.com/en-us/library/windows/desktop/ff521659(v=vs.85).aspx
Expand All @@ -90,7 +88,7 @@ void StartBleDeviceWatcher()
hstring aqsAllBluetoothLEDevices = L"(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";

deviceWatcher =
Windows::Devices::Enumeration::DeviceInformation::CreateWatcher(
DeviceInformation::CreateWatcher(
aqsAllBluetoothLEDevices,
requestedProperties,
DeviceInformationKind::AssociationEndpoint);
Expand Down Expand Up @@ -119,7 +117,7 @@ void StartBleDeviceWatcher()
/// <summary>
/// Stops watching for all nearby Bluetooth devices.
/// </summary>
void StopBleDeviceWatcher()
static void StopBleDeviceWatcher()
{
if (deviceWatcher != nullptr)
{
Expand All @@ -136,7 +134,7 @@ void StopBleDeviceWatcher()
}
}

fire_and_forget selectDevice(std::wstring deviceId)
static fire_and_forget selectDevice(std::wstring deviceId)
{
try
{
Expand Down Expand Up @@ -164,16 +162,14 @@ fire_and_forget selectDevice(std::wstring deviceId)
throw;
}
}


}

/// <summary>
/// Determines whether the UUID was assigned by the Bluetooth SIG.
/// If so, extracts the assigned number.
/// </summary>

bool TryParseSigDefinedUuid(guid const& uuid, uint16_t& shortId)
static bool TryParseSigDefinedUuid(guid const& uuid, uint16_t& shortId)
{
// UUIDs defined by the Bluetooth SIG are of the form
// 0000xxxx-0000-1000-8000-00805F9B34FB.
Expand All @@ -185,7 +181,7 @@ bool TryParseSigDefinedUuid(guid const& uuid, uint16_t& shortId)
return possibleBluetoothGuid == BluetoothGuid;
}

std::wstring GetServiceName(GattDeviceService const& service)
static std::wstring GetServiceName(GattDeviceService const& service)
{
uint16_t shortId;

Expand Down Expand Up @@ -227,7 +223,7 @@ std::wstring GetServiceName(GattDeviceService const& service)
return L"Custom service: " + std::wstring(to_hstring(uuid));
}

fire_and_forget getServices()
static fire_and_forget getServices()
{
// Note: BluetoothLEDevice.GattServices property will return an empty list for unpaired devices. For all uses we recommend using the GetGattServicesAsync method.
// BT_Code: GetGattServicesAsync returns a list of all the supported services of the device (even if it's not paired to the system).
Expand All @@ -252,8 +248,7 @@ fire_and_forget getServices()
}
}


std::wstring GetCharacteristicName(GattCharacteristic const& characteristic)
static std::wstring GetCharacteristicName(GattCharacteristic const& characteristic)
{
uint16_t shortId;

Expand Down Expand Up @@ -383,14 +378,14 @@ std::wstring GetCharacteristicName(GattCharacteristic const& characteristic)
return L"Custom Characteristic: " + std::wstring{ to_hstring(uuid) };
}

hstring to_hstring(GattCommunicationStatus status)
static hstring to_hstring(GattCommunicationStatus status)
{
switch (status)
{
case GattCommunicationStatus::Success: return L"Success";
case GattCommunicationStatus::Unreachable: return L"Unreachable";
case GattCommunicationStatus::ProtocolError: return L"ProtocolError";
case GattCommunicationStatus::AccessDenied: return L"AccessDenied";
case GattCommunicationStatus::Success: return L"Success";
case GattCommunicationStatus::Unreachable: return L"Unreachable";
case GattCommunicationStatus::ProtocolError: return L"ProtocolError";
case GattCommunicationStatus::AccessDenied: return L"AccessDenied";
}
return to_hstring(static_cast<int>(status));
}
Expand All @@ -411,42 +406,42 @@ enum class POWERMATE_ACTIONS : uint8_t
HOLD_6 = 0x77
};

void actOnPowerMate(const uint8_t value)
static auto actOnPowerMate(const uint8_t value)
{
switch (static_cast<POWERMATE_ACTIONS>(value))
{
case(POWERMATE_ACTIONS::PRESS):
masterVolume.mute();
break;
case(POWERMATE_ACTIONS::LONG_RELEASE):
break;
case(POWERMATE_ACTIONS::LEFT):
masterVolume.volumeDown();
break;
case(POWERMATE_ACTIONS::RIGHT):
masterVolume.volumeUp();
break;
case(POWERMATE_ACTIONS::PRESSED_LEFT):
break;
case(POWERMATE_ACTIONS::PRESSED_RIGHT):
break;
case(POWERMATE_ACTIONS::HOLD_1):
break;
case(POWERMATE_ACTIONS::HOLD_2):
break;
case(POWERMATE_ACTIONS::HOLD_3):
break;
case(POWERMATE_ACTIONS::HOLD_4):
break;
case(POWERMATE_ACTIONS::HOLD_5):
break;
case(POWERMATE_ACTIONS::HOLD_6):
break;
default:
std::cerr << "UNKNOWN PowerMate Action" << std::hex << value << "\n";
case POWERMATE_ACTIONS::PRESS:
masterVolume.mute();
break;
case POWERMATE_ACTIONS::LONG_RELEASE:
break;
case POWERMATE_ACTIONS::LEFT:
masterVolume.volume_down();
break;
case POWERMATE_ACTIONS::RIGHT:
masterVolume.volume_up();
break;
case POWERMATE_ACTIONS::PRESSED_LEFT:
break;
case POWERMATE_ACTIONS::PRESSED_RIGHT:
break;
case POWERMATE_ACTIONS::HOLD_1:
break;
case POWERMATE_ACTIONS::HOLD_2:
break;
case POWERMATE_ACTIONS::HOLD_3:
break;
case POWERMATE_ACTIONS::HOLD_4:
break;
case POWERMATE_ACTIONS::HOLD_5:
break;
case POWERMATE_ACTIONS::HOLD_6:
break;
default:
std::cerr << "UNKNOWN PowerMate Action" << std::hex << value << "\n";
}
}
void Characteristic_ValueChanged(GattCharacteristic const& c, GattValueChangedEventArgs args)
static void Characteristic_ValueChanged(GattCharacteristic const& c, GattValueChangedEventArgs args)
{
// BT_Code: An Indicate or Notify reported that the value has changed.
// Display the new value with a timestamp.
Expand All @@ -458,14 +453,14 @@ void Characteristic_ValueChanged(GattCharacteristic const& c, GattValueChangedEv
actOnPowerMate(value);
}

void AddValueChangedHandler(GattCharacteristic c)
static void AddValueChangedHandler(GattCharacteristic c)
{
c.ValueChanged(Characteristic_ValueChanged);
}

fire_and_forget subscribeToValueChange(GattCharacteristic c)
static fire_and_forget subscribeToValueChange(GattCharacteristic c)
{
GattClientCharacteristicConfigurationDescriptorValue cccdValue = GattClientCharacteristicConfigurationDescriptorValue::None;
GattClientCharacteristicConfigurationDescriptorValue cccdValue;
if ((c.CharacteristicProperties() & GattCharacteristicProperties::Indicate) != GattCharacteristicProperties::None)
{
cccdValue = GattClientCharacteristicConfigurationDescriptorValue::Indicate;
Expand Down Expand Up @@ -503,7 +498,7 @@ fire_and_forget subscribeToValueChange(GattCharacteristic c)
}
}

fire_and_forget readCharacteristic(GattCharacteristic c)
static fire_and_forget readCharacteristic(GattCharacteristic c)
{
// BT_Code: Read the actual value from the device by using Uncached.
GattReadResult result = co_await c.ReadValueAsync(BluetoothCacheMode::Uncached);
Expand All @@ -519,12 +514,12 @@ fire_and_forget readCharacteristic(GattCharacteristic c)
}
}

int uuid_equal(std::wstring left, guid right)
static int uuid_equal(std::wstring left, guid right)
{
return (std::wstring(to_hstring(right)).compare(left) == 0);
return std::wstring(to_hstring(right)).compare(left) == 0;
}

fire_and_forget getCharacteristics(GattDeviceService service)
static fire_and_forget getCharacteristics(GattDeviceService service)
{
IVectorView<GattCharacteristic> characteristics{ nullptr };
try
Expand Down Expand Up @@ -580,49 +575,50 @@ fire_and_forget getCharacteristics(GattDeviceService service)
}
}


int main()
{
StartBleDeviceWatcher();
bool waiting{ true };
bool waiting { true };
std::cout << "0 <ENTER> to stop/quit\n";

while (waiting)
{
Sleep(50);

switch (myStatus)
{
case(STATE_MACHINE::INIT):
break;
case(STATE_MACHINE::DEVICE_SEARCHING):
break;
case(STATE_MACHINE::DEVICE_FOUND):
myStatus = STATE_MACHINE::DEVICE_SELECTING;
selectDevice(powerMateDeviceId);
break;
case(STATE_MACHINE::DEVICE_SELECTING):
break;
case(STATE_MACHINE::DEVICE_SELECTED):
if (bluetoothLeDevice != nullptr)
{
myStatus = STATE_MACHINE::SERVICE_SEARCHING;
getServices();
}
break;
case(STATE_MACHINE::SERVICE_SEARCHING):
break;
case(STATE_MACHINE::SERVICE_FOUND):
for (auto&& service : powerMateServices)
{
getCharacteristics(service.first);
}
break;
case(STATE_MACHINE::CHARACTERISTICS_SEARCHING):
break;
case(STATE_MACHINE::CHARACTERISTICS_FOUND):
//waiting = false;
break;
default:
std::cerr << "Unknown State" << "\n;";
case STATE_MACHINE::INIT:
break;
case STATE_MACHINE::DEVICE_SEARCHING:
break;
case STATE_MACHINE::DEVICE_FOUND:
myStatus = STATE_MACHINE::DEVICE_SELECTING;
selectDevice(powerMateDeviceId);
break;
case STATE_MACHINE::DEVICE_SELECTING:
break;
case STATE_MACHINE::DEVICE_SELECTED:
if (bluetoothLeDevice != nullptr)
{
myStatus = STATE_MACHINE::SERVICE_SEARCHING;
getServices();
}
break;
case STATE_MACHINE::SERVICE_SEARCHING:
break;
case STATE_MACHINE::SERVICE_FOUND:
for (auto&& service : powerMateServices)
{
getCharacteristics(service.first);
}
break;
case STATE_MACHINE::CHARACTERISTICS_SEARCHING:
break;
case STATE_MACHINE::CHARACTERISTICS_FOUND:
//waiting = false;
break;
default:
std::cerr << "Unknown State" << "\n;";
}
}
StopBleDeviceWatcher();
Expand Down
Loading