Skip to content

Commit

Permalink
add more event code
Browse files Browse the repository at this point in the history
  • Loading branch information
kkguo committed Jan 27, 2022
1 parent fd4de06 commit 4944a71
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
71 changes: 64 additions & 7 deletions JoyRoot/Root.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public enum ModelType {
#endregion

public ModelType Model;

#region BT connection
public ulong Address {
private set;
get;
}

#region BT connection
private BluetoothLEDevice _device;
public BluetoothLEDevice Bluetooth {
set {
Expand All @@ -59,8 +59,7 @@ public async Task<bool> isAvailible() {
if (Bluetooth == null) {
Debug.WriteLine("seeing root missing, tring to reconnect");
return await query(Address);
} else {
Debug.WriteLine("seeing root availible");
} else {
return true;
}
}
Expand Down Expand Up @@ -198,9 +197,6 @@ public void navigate(int x, int y) {

}

public event EventHandler ResponseRecieved;
public event EventHandler StatusChanged;

private async Task<GattDeviceService> getGattServices(Guid ServiceGuid) {
if (BTServices.ContainsKey(ServiceGuid)) {
return BTServices[ServiceGuid];
Expand Down Expand Up @@ -251,5 +247,66 @@ public async Task<string> readGattCharString(Guid service, Guid guid) {
} else
return "";
}

private async void subscribeUartTx() {
var cha = await getGattCharacteristic(guidUARTService, guidTxCharacteristic);
var status = await cha.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Indicate);
if (status == GattCommunicationStatus.Success) {
cha.ValueChanged += UARTTx_Recieved;
}
}

#region Response from robot
public class RootEventArgs : EventArgs
{
public byte[] packet;
public RootEventArgs(byte[] bytes) : base() {
packet = bytes;
}
}

public delegate void RootEventHandler(RootDevice root, RootEventArgs e);

public event RootEventHandler ResponseRecieved;
public event RootEventHandler StatusChanged;

public event RootEventHandler MoveFinished;
public event RootEventHandler BumperEvent;
public event RootEventHandler MotorStallEvent;

private void UARTTx_Recieved(GattCharacteristic sender, GattValueChangedEventArgs args) {
byte[] bytes = new byte[args.CharacteristicValue.Length];
DataReader.FromBuffer(args.CharacteristicValue).ReadBytes(bytes);
var cmd = new RootCommand(bytes);
switch (bytes[0]) { // Dev
case 1:
switch(bytes[1]) { // cmd
case 8:
case 12:
case 17:
case 27:
MoveFinished.Invoke(this, new RootEventArgs(bytes));
break;
case 16:
ResponseRecieved.Invoke(this, new RootEventArgs(bytes));
break;
case 29:
MotorStallEvent.Invoke(this, new RootEventArgs(bytes));
break;
}
break;
case 2:
MoveFinished(this, new RootEventArgs(bytes));
break;
case 12: // Bumper
BumperEvent.Invoke(this, new RootEventArgs(bytes));
break;
default:
ResponseRecieved.Invoke(this, new RootEventArgs(bytes));
break;
}
}

#endregion
}
}
11 changes: 0 additions & 11 deletions JoyRoot/mainform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ private async void connect(RootDevice root)
btnStop.Enabled = true;
}

private async void subscribeTX(RootDevice root)
{
//var ser = await getRootService(root, RootDevice.guidUARTService);
//var cha = await getRootCharacteristic(root, RootDevice.guidUARTService, RootDevice.guidTxCharacteristic);
//var status = await cha.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Indicate);
//if (status == GattCommunicationStatus.Success)
//{
// cha.ValueChanged += Characteristic_ValueChanged;
//}
}

private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{

Expand Down

0 comments on commit 4944a71

Please sign in to comment.