Merge remote-tracking branch 'origin/DataConverter' into simulation
This commit is contained in:
@@ -7,14 +7,14 @@ using System.Security.Cryptography;
|
||||
|
||||
namespace Hardware
|
||||
{
|
||||
class BLEReciever
|
||||
class BLEHandler
|
||||
{
|
||||
IDataConverter dataConverter;
|
||||
private BLE bleBike;
|
||||
private BLE bleHeart;
|
||||
public bool running { get; set; }
|
||||
public bool Running { get; set; }
|
||||
|
||||
public BLEReciever(IDataConverter dataConverter)
|
||||
public BLEHandler(IDataConverter dataConverter)
|
||||
{
|
||||
this.dataConverter = dataConverter;
|
||||
bool running = false;
|
||||
@@ -94,7 +94,7 @@ namespace Hardware
|
||||
}
|
||||
|
||||
Console.WriteLine("connected to BLE");
|
||||
this.running = true;
|
||||
this.Running = true;
|
||||
}
|
||||
|
||||
private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
|
||||
@@ -108,7 +108,8 @@ namespace Hardware
|
||||
byte[] payload = new byte[8];
|
||||
Array.Copy(e.Data, 4, payload, 0, 8);
|
||||
this.dataConverter.Bike(payload);
|
||||
}else if(e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb")
|
||||
}
|
||||
else if (e.ServiceName == "00002a37-0000-1000-8000-00805f9b34fb")
|
||||
{
|
||||
this.dataConverter.BPM(e.Data);
|
||||
}
|
||||
@@ -123,7 +124,34 @@ namespace Hardware
|
||||
{
|
||||
this.bleBike?.Dispose();
|
||||
this.bleHeart?.Dispose();
|
||||
this.running = false;
|
||||
this.Running = false;
|
||||
}
|
||||
|
||||
public void setResistance(float percentage)
|
||||
{
|
||||
byte[] antMessage = new byte[13];
|
||||
antMessage[0] = 0x4A;
|
||||
antMessage[1] = 0x09;
|
||||
antMessage[2] = 0x4E;
|
||||
antMessage[3] = 0x05;
|
||||
antMessage[4] = 0x30;
|
||||
for (int i = 5; i < 11; i++)
|
||||
{
|
||||
antMessage[i] = 0xFF;
|
||||
}
|
||||
antMessage[11] = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0);
|
||||
//antMessage[11] = 50; //hardcoded for testing
|
||||
|
||||
byte checksum = 0;
|
||||
for (int i = 0; i < 12; i++)
|
||||
{
|
||||
checksum ^= antMessage[i];
|
||||
}
|
||||
|
||||
antMessage[12] = checksum;//reminder that i am dumb :P
|
||||
|
||||
|
||||
bleBike.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,17 @@ namespace Hardware
|
||||
int accumPower = bytes[3] | (bytes[4] << 8);
|
||||
|
||||
Console.WriteLine($"Accumulated power: {accumPower} watt (Rollover 65536)");
|
||||
//Console.WriteLine();
|
||||
|
||||
int instantPower = (bytes[5]) | (bytes[6]>>4)<<8;
|
||||
|
||||
|
||||
if (instantPower != 0xFFF)
|
||||
Console.WriteLine($"Instant power: {instantPower} watt (Range 0-4094)");
|
||||
|
||||
int trainerStatus = bytes[6] & 0b00001111; // bit 4-7
|
||||
int flags = bytes[7] >> 4;
|
||||
int FEState = bytes[7] & 0b00001111;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -4,156 +4,155 @@ using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Hardware.Simulators;
|
||||
using Hardware;
|
||||
|
||||
namespace FietsDemo
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
//static void Main(string[] args)
|
||||
//{
|
||||
// foo foo = new foo();
|
||||
// BLEReceiver bLEReceiver = new BLEReceiver(foo);
|
||||
|
||||
// bLEReceiver.ConnectToBLE();
|
||||
// Console.Read();
|
||||
//}
|
||||
|
||||
interface IFoo
|
||||
private static async Task Main(string[] args)
|
||||
{
|
||||
void doStuff(byte[] bytes);
|
||||
int errorCode = 0;
|
||||
BLE bleBike = new BLE();
|
||||
BLE bleHeart = new BLE();
|
||||
Thread.Sleep(1000); // We need some time to list available devices
|
||||
|
||||
// List available devices
|
||||
List<String> bleBikeList = bleBike.ListDevices();
|
||||
Console.WriteLine("Devices found: ");
|
||||
foreach (var name in bleBikeList)
|
||||
{
|
||||
Console.WriteLine($"Device: {name}");
|
||||
}
|
||||
|
||||
// Connecting
|
||||
errorCode = errorCode = await bleBike.OpenDevice("Avans Bike AC48");
|
||||
// __TODO__ Error check
|
||||
|
||||
var services = bleBike.GetServices;
|
||||
foreach (var service in services)
|
||||
{
|
||||
Console.WriteLine($"Service: {service}");
|
||||
}
|
||||
|
||||
// Set service
|
||||
errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e");
|
||||
// __TODO__ error check
|
||||
|
||||
// Subscribe
|
||||
bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
|
||||
errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e");
|
||||
|
||||
// Heart rate
|
||||
errorCode = await bleHeart.OpenDevice("Avans Bike AC48");
|
||||
|
||||
await bleHeart.SetService("HeartRate");
|
||||
|
||||
bleHeart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
|
||||
await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement");
|
||||
|
||||
Console.Read();
|
||||
}
|
||||
|
||||
class BLEReceiver
|
||||
private static void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
|
||||
{
|
||||
IFoo foo;
|
||||
//Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName,
|
||||
// BitConverter.ToString(e.Data).Replace("-", " "),
|
||||
// Encoding.UTF8.GetString(e.Data));
|
||||
|
||||
public BLEReceiver(IFoo foo)
|
||||
string[] bytes = BitConverter.ToString(e.Data).Split('-');
|
||||
string[] ANT = new string[5];
|
||||
if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e")
|
||||
{
|
||||
this.foo = foo;
|
||||
}
|
||||
//Console.WriteLine("SYNC : " + bytes[0]);
|
||||
//ANT[0] = bytes[0];
|
||||
//Console.WriteLine("LENGTH : " + bytes[1]);
|
||||
|
||||
public async void ConnectToBLE()
|
||||
{
|
||||
int errorCode = 0;
|
||||
BLE bleBike = new BLE();
|
||||
BLE bleHeart = new BLE();
|
||||
Thread.Sleep(1000); // We need some time to list available devices
|
||||
|
||||
// List available devices
|
||||
List<String> bleBikeList = bleBike.ListDevices();
|
||||
Console.WriteLine("Devices found: ");
|
||||
foreach (var name in bleBikeList)
|
||||
{
|
||||
Console.WriteLine($"Device: {name}");
|
||||
}
|
||||
|
||||
// Connecting
|
||||
errorCode = errorCode = await bleBike.OpenDevice("Avans Bike AC48");
|
||||
// __TODO__ Error check
|
||||
|
||||
var services = bleBike.GetServices;
|
||||
foreach (var service in services)
|
||||
{
|
||||
Console.WriteLine($"Service: {service}");
|
||||
}
|
||||
|
||||
// Set service
|
||||
errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e");
|
||||
// __TODO__ error check
|
||||
|
||||
// Subscribe
|
||||
bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
|
||||
errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e");
|
||||
|
||||
// Heart rate
|
||||
errorCode = await bleHeart.OpenDevice("Avans Bike AC48");
|
||||
|
||||
await bleHeart.SetService("HeartRate");
|
||||
|
||||
bleHeart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
|
||||
await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement");
|
||||
|
||||
Console.Read();
|
||||
}
|
||||
|
||||
/* public async void ConnectToSimulation()
|
||||
{
|
||||
BikeSimulator bikeSimulator = new BikeSimulator();
|
||||
bikeSimulator.StartSimulation(foo);
|
||||
|
||||
}*/
|
||||
|
||||
private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
|
||||
{
|
||||
//Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName,
|
||||
// BitConverter.ToString(e.Data).Replace("-", " "),
|
||||
// Encoding.UTF8.GetString(e.Data));
|
||||
|
||||
//string[] bytes = BitConverter.ToString(e.Data).Split('-');
|
||||
//string[] ANT = new string[5];
|
||||
//if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e")
|
||||
int length = Convert.ToInt32(bytes[1], 16);
|
||||
//ANT[1] = length.ToString();
|
||||
//Console.WriteLine("MSG ID : " + bytes[2]);
|
||||
//ANT[2] = bytes[2];
|
||||
//string msg = string.Empty;
|
||||
//for (int i = 3; i < 3 + length; i++)
|
||||
//{
|
||||
// Console.WriteLine("SYNC : " + bytes[0]);
|
||||
// ANT[0] = bytes[0];
|
||||
// Console.WriteLine("LENGTH : " + bytes[1]);
|
||||
|
||||
// int length = Convert.ToInt32(bytes[1], 16);
|
||||
// ANT[1] = length.ToString();
|
||||
// Console.WriteLine("MSG ID : " + bytes[2]);
|
||||
// ANT[2] = bytes[2];
|
||||
// string msg = string.Empty;
|
||||
// for (int i = 3; i < 3 + length; i++)
|
||||
// {
|
||||
// msg += bytes[i];
|
||||
// }
|
||||
// ANT[3] = msg;
|
||||
|
||||
// byte[] message = new byte[length];
|
||||
|
||||
// Array.Copy(e.Data, 3, message, 0, length);
|
||||
|
||||
// DoCrazyShitWithMsg(message);
|
||||
|
||||
// Console.WriteLine("MSG : " + msg);
|
||||
// string checksum = bytes[3 + length];
|
||||
// ANT[4] = checksum;
|
||||
// Console.WriteLine("CHECKSUM : " + checksum);
|
||||
|
||||
|
||||
// Console.WriteLine(BitConverter.ToString(e.Data));
|
||||
|
||||
// msg += bytes[i];
|
||||
//}
|
||||
//else
|
||||
//ANT[3] = msg;
|
||||
|
||||
byte[] message = new byte[length - 1];
|
||||
|
||||
Array.Copy(e.Data, 4, message, 0, length - 1);
|
||||
|
||||
DoCrazyShitWithMsg(message);
|
||||
|
||||
//Console.WriteLine("MSG : " + msg);
|
||||
//string checksum = bytes[3 + length];
|
||||
//ANT[4] = checksum;
|
||||
//Console.WriteLine("CHECKSUM : " + checksum);
|
||||
|
||||
//byte calcChecksum = 0;
|
||||
|
||||
//for (int i = 0; i < e.Data.Length - 1; i++)
|
||||
//{
|
||||
// Console.WriteLine("BPM: " + Convert.ToInt32(bytes[1], 16));
|
||||
// calcChecksum ^= e.Data[i];
|
||||
//}
|
||||
//Console.WriteLine();
|
||||
|
||||
this.foo.doStuff(e.Data);
|
||||
//Console.WriteLine("Calculated checksum : " + Convert.ToString(calcChecksum,16).ToUpper());
|
||||
|
||||
|
||||
//Console.WriteLine(BitConverter.ToString(e.Data));
|
||||
|
||||
}
|
||||
|
||||
private static void DoCrazyShitWithMsg(byte[] bytes)
|
||||
else
|
||||
{
|
||||
String[] hexvalues = BitConverter.ToString(bytes).Split('-');
|
||||
for (int i = 0; i < hexvalues.Length; i++)
|
||||
{
|
||||
Console.WriteLine("Byte {0}: {1}", i, hexvalues[i]);
|
||||
}
|
||||
//Console.WriteLine("BPM: " + Convert.ToInt32(bytes[1], 16));
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
class foo : IFoo
|
||||
private static void DoCrazyShitWithMsg(byte[] bytes)
|
||||
{
|
||||
public void doStuff(byte[] bytes)
|
||||
Console.WriteLine("doing crazy shit with {0}", bytes);
|
||||
String[] hexvalues = BitConverter.ToString(bytes).Split('-');
|
||||
for (int i = 0; i < hexvalues.Length; i++)
|
||||
{
|
||||
Console.WriteLine("Byte {0}: {1}", i, hexvalues[i]);
|
||||
}
|
||||
switch (bytes[0])
|
||||
{
|
||||
case 0x10:
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Data Page Number\t\t: " + hexvalues[0]);
|
||||
Console.WriteLine("Equipment Type Bit Field\t: " + hexvalues[1]);
|
||||
Console.WriteLine("Elapsed Time\t\t\t: " + hexvalues[2]);
|
||||
Console.WriteLine("Distance Traveled\t\t: " + hexvalues[3]);
|
||||
Console.WriteLine("Speed LSB\t\t\t: " + hexvalues[4]);
|
||||
Console.WriteLine("Speed LSB\t\t\t: " + hexvalues[5]);
|
||||
Console.WriteLine("Heart Rate\t\t\t: " + hexvalues[6]);
|
||||
Console.WriteLine("Capabilities FE State Bit Field\t: " + Convert.ToString(bytes[7], 2));
|
||||
break;
|
||||
case 0x19:
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Data Page Number\t\t: " + hexvalues[0]);
|
||||
Console.WriteLine("Update Event Count\t\t: " + hexvalues[1]);
|
||||
Console.WriteLine("Instantaneous Cadence\t\t: " + hexvalues[2]);
|
||||
Console.WriteLine("Accumulated Power LSB\t\t: " + hexvalues[3]);
|
||||
Console.WriteLine("Accumulated Power MSB\t\t: " + hexvalues[4]);
|
||||
Console.WriteLine("Instant Power LSB\t\t: " + hexvalues[5]);
|
||||
Console.WriteLine("Instant Power MSN and status\t: " + Convert.ToString(bytes[6], 2));
|
||||
Console.WriteLine("Flags and FE state BitField\t: " + Convert.ToString(bytes[7], 2));
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("data page number not recognized");
|
||||
break;
|
||||
|
||||
Console.WriteLine("Foo class received {0}", Convert.ToString(bytes));
|
||||
|
||||
}
|
||||
|
||||
|
||||
//Console.WriteLine("Speed might be {0} m/s", bytes[3] * 0.093294);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Hardware;
|
||||
using Hardware.Simulators;
|
||||
|
||||
|
||||
namespace ProftaakRH
|
||||
{
|
||||
@@ -14,6 +14,9 @@ namespace ProftaakRH
|
||||
BikeSimulator bikeSimulator = new BikeSimulator(dataConverter);
|
||||
bikeSimulator.StartSimulation();
|
||||
|
||||
bLEReceiver.Connect();
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user