From 213a098356e69ba59940d526793f155e3a500f58 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 11:30:02 +0200 Subject: [PATCH 01/33] basic class layout --- ProftaakRH/BLEReciever.cs | 10 ++ ProftaakRH/BikeSimulator.cs | 10 ++ ProftaakRH/DataConverter.cs | 10 ++ ProftaakRH/FietsDemo.cs | 188 ++++++++++++++++++++++-------------- 4 files changed, 143 insertions(+), 75 deletions(-) create mode 100644 ProftaakRH/BLEReciever.cs create mode 100644 ProftaakRH/BikeSimulator.cs create mode 100644 ProftaakRH/DataConverter.cs diff --git a/ProftaakRH/BLEReciever.cs b/ProftaakRH/BLEReciever.cs new file mode 100644 index 0000000..29cd2ec --- /dev/null +++ b/ProftaakRH/BLEReciever.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProftaakRH +{ + class Class1 + { + } +} diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs new file mode 100644 index 0000000..29cd2ec --- /dev/null +++ b/ProftaakRH/BikeSimulator.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProftaakRH +{ + class Class1 + { + } +} diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs new file mode 100644 index 0000000..436d08f --- /dev/null +++ b/ProftaakRH/DataConverter.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProftaakRH +{ + class DataConverter + { + } +} diff --git a/ProftaakRH/FietsDemo.cs b/ProftaakRH/FietsDemo.cs index 4edbdc1..b97a0df 100644 --- a/ProftaakRH/FietsDemo.cs +++ b/ProftaakRH/FietsDemo.cs @@ -9,103 +9,141 @@ namespace FietsDemo { internal class Program { - private static async Task Main(string[] args) + static void Main(string[] args) { - 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 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"); - + foo foo = new foo(); + BLEReceiver bLEReceiver = new BLEReceiver(foo); + bLEReceiver.ConnectToBLE(); Console.Read(); } - private static void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e) + interface IFoo { - Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName, - BitConverter.ToString(e.Data).Replace("-", " "), - Encoding.UTF8.GetString(e.Data)); + void doStuff(byte[] bytes); + } - string[] bytes = BitConverter.ToString(e.Data).Split('-'); - string[] ANT = new string[5]; - if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e") + class BLEReceiver + { + IFoo foo; + + public BLEReceiver(IFoo foo) { - Console.WriteLine("SYNC : " + bytes[0]); - ANT[0] = bytes[0]; - Console.WriteLine("LENGTH : " + bytes[1]); + this.foo = foo; + } - 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++) + 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 bleBikeList = bleBike.ListDevices(); + Console.WriteLine("Devices found: "); + foreach (var name in bleBikeList) { - msg += bytes[i]; + Console.WriteLine($"Device: {name}"); } - ANT[3] = msg; - byte[] message = new byte[length]; + // Connecting + errorCode = errorCode = await bleBike.OpenDevice("Avans Bike AC48"); + // __TODO__ Error check - Array.Copy(e.Data, 3, message, 0, length); + var services = bleBike.GetServices; + foreach (var service in services) + { + Console.WriteLine($"Service: {service}"); + } - DoCrazyShitWithMsg(message); + // Set service + errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e"); + // __TODO__ error check - Console.WriteLine("MSG : " + msg); - string checksum = bytes[3 + length]; - ANT[4] = checksum; - Console.WriteLine("CHECKSUM : " + checksum); + // Subscribe + bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged; + errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e"); + // Heart rate + errorCode = await bleHeart.OpenDevice("Avans Bike AC48"); - Console.WriteLine(BitConverter.ToString(e.Data)); - - } else - { - Console.WriteLine("BPM: " + Convert.ToInt32(bytes[1], 16)); + await bleHeart.SetService("HeartRate"); + + bleHeart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged; + await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement"); + + Console.Read(); + } + + 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") + //{ + // 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)); + + //} + //else + //{ + // Console.WriteLine("BPM: " + Convert.ToInt32(bytes[1], 16)); + //} + //Console.WriteLine(); + + this.foo.doStuff(e.Data); + } + + private static void DoCrazyShitWithMsg(byte[] bytes) + { + String[] hexvalues = BitConverter.ToString(bytes).Split('-'); + for (int i = 0; i < hexvalues.Length; i++) + { + Console.WriteLine("Byte {0}: {1}", i, hexvalues[i]); + } } - Console.WriteLine(); } - private static void DoCrazyShitWithMsg(byte[] bytes) + class foo : IFoo { - String[] hexvalues = BitConverter.ToString(bytes).Split('-'); - for (int i = 0; i < hexvalues.Length; i++) + public void doStuff(byte[] bytes) { - Console.WriteLine("Byte {0}: {1}" , i, hexvalues[i]); + + Console.WriteLine("Foo class received {0}", Convert.ToString(bytes)); + } } + + } } \ No newline at end of file From 8dce607028ca223a71bfeed82f19aa7a02bd6b5f Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 11:35:54 +0200 Subject: [PATCH 02/33] added IDataconverter interface --- ProftaakRH/BLEReciever.cs | 5 +++-- ProftaakRH/BikeSimulator.cs | 5 +++-- ProftaakRH/DataConverter.cs | 19 +++++++++++++++++-- ProftaakRH/FietsDemo.cs | 8 ++++++-- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/ProftaakRH/BLEReciever.cs b/ProftaakRH/BLEReciever.cs index 29cd2ec..1105362 100644 --- a/ProftaakRH/BLEReciever.cs +++ b/ProftaakRH/BLEReciever.cs @@ -2,9 +2,10 @@ using System.Collections.Generic; using System.Text; -namespace ProftaakRH +namespace Hardware { - class Class1 + class BLEReciever { + IDataConverter dataConverter; } } diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 29cd2ec..4b106bc 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -2,9 +2,10 @@ using System.Collections.Generic; using System.Text; -namespace ProftaakRH +namespace Hardware.Simulators { - class Class1 + class BikeSimulator { + IDataConverter dataConverter; } } diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index 436d08f..7378fe7 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -2,9 +2,24 @@ using System.Collections.Generic; using System.Text; -namespace ProftaakRH +namespace Hardware { - class DataConverter + class DataConverter : IDataConverter { + public void Bike(byte[] bytes) + { + throw new NotImplementedException(); + } + + public void BPM(byte[] bytes) + { + throw new NotImplementedException(); + } + } + + interface IDataConverter + { + void BPM(byte[] bytes); + void Bike(byte[] bytes); } } diff --git a/ProftaakRH/FietsDemo.cs b/ProftaakRH/FietsDemo.cs index b97a0df..d95c24f 100644 --- a/ProftaakRH/FietsDemo.cs +++ b/ProftaakRH/FietsDemo.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; +using Hardware.Simulators; +using Hardware; namespace FietsDemo { @@ -12,8 +14,10 @@ namespace FietsDemo static void Main(string[] args) { foo foo = new foo(); - BLEReceiver bLEReceiver = new BLEReceiver(foo); - bLEReceiver.ConnectToBLE(); + //BLEReceiver bLEReceiver = new BLEReceiver(foo); + + + //bLEReceiver.ConnectToBLE(); Console.Read(); } From 261048c2df23bd2eb75991b68997b6645b9c241c Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 11:56:29 +0200 Subject: [PATCH 03/33] added Main --- ProftaakRH/Main.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 ProftaakRH/Main.cs diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs new file mode 100644 index 0000000..207d831 --- /dev/null +++ b/ProftaakRH/Main.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ProftaakRH +{ + class Main + { + } +} From 9c55b545ddc7c3da871b7e21645055f8ff567b43 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Wed, 9 Sep 2020 12:26:00 +0200 Subject: [PATCH 04/33] Things --- ProftaakRH/BikeSimulator.cs | 26 ++++++++++++++++++++++++++ ProftaakRH/FietsDemo.cs | 11 +++++++++-- ProftaakRH/Main.cs | 11 ++++++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 4b106bc..6ea68d3 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -1,11 +1,37 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading; namespace Hardware.Simulators { class BikeSimulator { IDataConverter dataConverter; + + public BikeSimulator(IDataConverter dataConverter) + { + this.dataConverter = dataConverter; + } + public void StartSimulation() + { + //4A-09-4E-05-19-16-00-FF-28-00-00-20-F0 + + while (true) + { + byte[] array = { 0x19, 0x16, 0x00, 0xFF, 0x28, 0x00, 0x00, 0x20, 0xF0 }; + + //0x10 message + dataConverter.Bike(array); + //0x19 message + dataConverter.Bike(array); + //Heartbeat message + dataConverter.BPM(array); + + Thread.Sleep(1000); + + } + + } } } diff --git a/ProftaakRH/FietsDemo.cs b/ProftaakRH/FietsDemo.cs index d95c24f..1f86c3a 100644 --- a/ProftaakRH/FietsDemo.cs +++ b/ProftaakRH/FietsDemo.cs @@ -11,7 +11,7 @@ namespace FietsDemo { internal class Program { - static void Main(string[] args) + /*static void Main(string[] args) { foo foo = new foo(); //BLEReceiver bLEReceiver = new BLEReceiver(foo); @@ -19,7 +19,7 @@ namespace FietsDemo //bLEReceiver.ConnectToBLE(); Console.Read(); - } + }*/ interface IFoo { @@ -79,6 +79,13 @@ namespace FietsDemo 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, diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 207d831..3d6f434 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -1,10 +1,19 @@ using System; using System.Collections.Generic; using System.Text; +using Hardware; +using Hardware.Simulators; namespace ProftaakRH { - class Main + class Program { + static void Main(string[] agrs) + { + IDataConverter dataConverter = new DataConverter(); + BikeSimulator bikeSimulator = new BikeSimulator(dataConverter); + bikeSimulator.StartSimulation(); + + } } } From 3e3a0cf4c4b056e2f3d16f55d326a98ec066daca Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 12:27:37 +0200 Subject: [PATCH 05/33] wip lunch break --- ProftaakRH/BLEReciever.cs | 69 +++++++++++++++++++++++++++++++++++++++ ProftaakRH/FietsDemo.cs | 17 +++++----- ProftaakRH/Main.cs | 13 +++++++- 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/ProftaakRH/BLEReciever.cs b/ProftaakRH/BLEReciever.cs index 1105362..d0d4d0f 100644 --- a/ProftaakRH/BLEReciever.cs +++ b/ProftaakRH/BLEReciever.cs @@ -1,11 +1,80 @@ using System; using System.Collections.Generic; using System.Text; +using Avans.TI.BLE; +using System.Threading; + namespace Hardware { class BLEReciever { IDataConverter dataConverter; + + public BLEReciever(IDataConverter dataConverter) + { + this.dataConverter = dataConverter; + } + + public async void Connect() + { + 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 bleBikeList = bleBike.ListDevices(); + Console.WriteLine("Devices found: "); + foreach (var name in bleBikeList) + { + Console.WriteLine(name); + if (name.Contains("Avans Bike")) + { + Connect(name); + Console.WriteLine("connecting to {0}", name); + break; + } + } + } + public async void Connect(string deviceName) + { + int errorCode = 0; + BLE bleBike = new BLE(); + BLE bleHeart = new BLE(); + + errorCode = errorCode = await bleBike.OpenDevice(deviceName); + // __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(deviceName); + + await bleHeart.SetService("HeartRate"); + + bleHeart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged; + await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement"); + } + + 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)); + //send to dataconverter + } } } diff --git a/ProftaakRH/FietsDemo.cs b/ProftaakRH/FietsDemo.cs index d95c24f..eb6d7fb 100644 --- a/ProftaakRH/FietsDemo.cs +++ b/ProftaakRH/FietsDemo.cs @@ -11,15 +11,14 @@ namespace FietsDemo { internal class Program { - static void Main(string[] args) - { - foo foo = new foo(); - //BLEReceiver bLEReceiver = new BLEReceiver(foo); + //static void Main(string[] args) + //{ + // foo foo = new foo(); + // BLEReceiver bLEReceiver = new BLEReceiver(foo); - - //bLEReceiver.ConnectToBLE(); - Console.Read(); - } + // bLEReceiver.ConnectToBLE(); + // Console.Read(); + //} interface IFoo { @@ -144,7 +143,7 @@ namespace FietsDemo { Console.WriteLine("Foo class received {0}", Convert.ToString(bytes)); - + } } diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 207d831..efad10b 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -1,10 +1,21 @@ using System; using System.Collections.Generic; using System.Text; +using Hardware; namespace ProftaakRH { - class Main + class Program { + + static void Main(string[] args) + { + IDataConverter dataConverter = new DataConverter(); + BLEReciever bLEReceiver = new BLEReciever(dataConverter); + + bLEReceiver.Connect("sdkgjnzsoifgnzaiof"); + + //Console.ReadLine(); + } } } From 87abee1207c1c0bd079c9cacc9456d403cab2a74 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 13:43:59 +0200 Subject: [PATCH 06/33] error handling --- ProftaakRH/BLEReciever.cs | 62 ++++++++++++++++++++++++++++----------- ProftaakRH/Main.cs | 4 +-- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/ProftaakRH/BLEReciever.cs b/ProftaakRH/BLEReciever.cs index d0d4d0f..854d80f 100644 --- a/ProftaakRH/BLEReciever.cs +++ b/ProftaakRH/BLEReciever.cs @@ -10,17 +10,17 @@ namespace Hardware class BLEReciever { IDataConverter dataConverter; + BLE bleBike; + BLE bleHeart; public BLEReciever(IDataConverter dataConverter) { this.dataConverter = dataConverter; } - public async void Connect() + public void Connect() { - 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 @@ -31,50 +31,78 @@ namespace Hardware Console.WriteLine(name); if (name.Contains("Avans Bike")) { - Connect(name); Console.WriteLine("connecting to {0}", name); + Connect(name); break; + } } } public async void Connect(string deviceName) { int errorCode = 0; - BLE bleBike = new BLE(); - BLE bleHeart = new BLE(); + bleBike = new BLE(); + bleHeart = new BLE(); errorCode = errorCode = await bleBike.OpenDevice(deviceName); - // __TODO__ Error check - - var services = bleBike.GetServices; - foreach (var service in services) + if (errorCode > 0) { - Console.WriteLine($"Service: {service}"); + disposeBLE(); + return; } // Set service errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e"); - // __TODO__ error check + if (errorCode > 0) + { + disposeBLE(); + return; + } // Subscribe bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged; errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e"); + if (errorCode > 0) + { + disposeBLE(); + return; + } // Heart rate errorCode = await bleHeart.OpenDevice(deviceName); + if (errorCode > 0) + { + disposeBLE(); + return; + } - await bleHeart.SetService("HeartRate"); + errorCode = await bleHeart.SetService("HeartRate"); + if (errorCode > 0) + { + disposeBLE(); + return; + } bleHeart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged; - await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement"); + errorCode = await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement"); + if (errorCode > 0) + { + disposeBLE(); + return; + } } 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)); + Console.WriteLine("Received from {0}: {1}", e.ServiceName, + BitConverter.ToString(e.Data).Replace("-", " ")); //send to dataconverter } + + private void disposeBLE() + { + this.bleBike?.Dispose(); + this.bleHeart?.Dispose(); + } } } diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index efad10b..4625f5d 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -13,9 +13,9 @@ namespace ProftaakRH IDataConverter dataConverter = new DataConverter(); BLEReciever bLEReceiver = new BLEReciever(dataConverter); - bLEReceiver.Connect("sdkgjnzsoifgnzaiof"); + bLEReceiver.Connect(); - //Console.ReadLine(); + Console.ReadLine(); } } } From 8d9985ac89bebc60c03a20f59d6c2172452d22cd Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 13:44:50 +0200 Subject: [PATCH 07/33] BLEreceiver running bool --- ProftaakRH/BLEReciever.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ProftaakRH/BLEReciever.cs b/ProftaakRH/BLEReciever.cs index 854d80f..280fc73 100644 --- a/ProftaakRH/BLEReciever.cs +++ b/ProftaakRH/BLEReciever.cs @@ -10,12 +10,14 @@ namespace Hardware class BLEReciever { IDataConverter dataConverter; - BLE bleBike; - BLE bleHeart; + private BLE bleBike; + private BLE bleHeart; + public bool running { get; } public BLEReciever(IDataConverter dataConverter) { this.dataConverter = dataConverter; + bool running = false; } public void Connect() @@ -34,7 +36,7 @@ namespace Hardware Console.WriteLine("connecting to {0}", name); Connect(name); break; - + } } } @@ -90,6 +92,7 @@ namespace Hardware disposeBLE(); return; } + this.running = true; } private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e) @@ -103,6 +106,7 @@ namespace Hardware { this.bleBike?.Dispose(); this.bleHeart?.Dispose(); + this.running = false; } } } From 650f308cf509e937a49f8504a9c7bc92c8774b14 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 14:49:40 +0200 Subject: [PATCH 08/33] wip --- ProftaakRH/BLEReciever.cs | 25 ++++++++++++++--- ProftaakRH/DataConverter.cs | 55 +++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/ProftaakRH/BLEReciever.cs b/ProftaakRH/BLEReciever.cs index 280fc73..dfc5827 100644 --- a/ProftaakRH/BLEReciever.cs +++ b/ProftaakRH/BLEReciever.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Text; using Avans.TI.BLE; using System.Threading; - +using System.Security.Cryptography; namespace Hardware { @@ -12,7 +12,7 @@ namespace Hardware IDataConverter dataConverter; private BLE bleBike; private BLE bleHeart; - public bool running { get; } + public bool running { get; set; } public BLEReciever(IDataConverter dataConverter) { @@ -92,14 +92,31 @@ namespace Hardware disposeBLE(); return; } + + Console.WriteLine("connected to BLE"); this.running = true; } private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e) { - Console.WriteLine("Received from {0}: {1}", e.ServiceName, - BitConverter.ToString(e.Data).Replace("-", " ")); + //Console.WriteLine("Received from {0}: {1}", e.ServiceName, + // BitConverter.ToString(e.Data).Replace("-", " ")); //send to dataconverter + + if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e") + { + 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") + { + this.dataConverter.BPM(e.Data); + } + else + { + Console.WriteLine("received data from unknown source {0}", e.ServiceName); + } + } private void disposeBLE() diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index 7378fe7..1c9a4e3 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -8,12 +8,63 @@ namespace Hardware { public void Bike(byte[] bytes) { - throw new NotImplementedException(); + if (bytes == null) + { + Console.WriteLine("HEY, didn't get bytes!\n-Bike DataConverter"); + } + else + if (bytes.Length == 8) + { + switch (bytes[0]) + { + case 0x10: + if (bytes[1] != 25) + { + Console.WriteLine("WTF this isn't a bike"); + } + Console.WriteLine($"Time since start is: {bytes[2] / 4}s (Rollover every 4s)"); + Console.WriteLine($"Distance Traveled is : {bytes[3]}m (Rollover every 256m)"); + + int input = bytes[4] | (bytes[5] << 8); + Console.WriteLine($"Speed is : {input * 0.01}m/s (Range 65.534m/4)"); + break; + case 0x19: + + break; + + default: + Console.WriteLine("HEY, I never heard of data page {0}\n-DataConverter", bytes[0]); + break; + } + } + else + { + Console.WriteLine("HEY, I didn't get 8 bytes!\n-DataConverter"); + } + Console.WriteLine(); } public void BPM(byte[] bytes) { - throw new NotImplementedException(); + if (bytes == null) + { + Console.WriteLine("HEY, didn't get bytes!\n-BPM DataConverter"); + return; + } + if (bytes[0] != 0) + { + Console.WriteLine("HOLY SHIT i got flags!!! {0} now i can't do anything\n-BPM DataConverter", bytes[0]); + } + else if (bytes.Length != 2) + { + Console.WriteLine("bytes length is: {0}", bytes.Length); + } + else + { + Console.WriteLine("BPM: {0}", bytes[1]); + + } + Console.WriteLine(); } } From bd40a117ec66c06117316e2885e6123f6102f478 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 14:57:23 +0200 Subject: [PATCH 09/33] done with data page 0x10 byte 7 isn't checked --- ProftaakRH/DataConverter.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index 1c9a4e3..24d4bbc 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -27,6 +27,10 @@ namespace Hardware int input = bytes[4] | (bytes[5] << 8); Console.WriteLine($"Speed is : {input * 0.01}m/s (Range 65.534m/4)"); + if (bytes[6] != 0xFF) + { + Console.WriteLine("Heart rate byte: {0}", Convert.ToString(bytes[6],2)); + } break; case 0x19: From 3de18d7c23e8c11bbf94ccdb6ee9cb8f37e2c84d Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 9 Sep 2020 14:58:10 +0200 Subject: [PATCH 10/33] added read for byte 1 of page 25 --- ProftaakRH/DataConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index 1c9a4e3..cad236d 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -29,7 +29,7 @@ namespace Hardware Console.WriteLine($"Speed is : {input * 0.01}m/s (Range 65.534m/4)"); break; case 0x19: - + Console.WriteLine($"Event count: {bytes[1]} (Rollover 256)"); break; default: From 149c327f7e55eabbdfc5b79472381a5e2509740e Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Wed, 9 Sep 2020 15:02:10 +0200 Subject: [PATCH 11/33] BikeSimulater Start --- ProftaakRH/BikeSimulator.cs | 37 +++++++++++++++++++++++++++++------- ProftaakRH/ProftaakRH.csproj | 4 ++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 6ea68d3..e1c058f 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -1,13 +1,17 @@ -using System; +using LibNoise.Primitive; +using System; using System.Collections.Generic; using System.Text; using System.Threading; + + namespace Hardware.Simulators { class BikeSimulator { IDataConverter dataConverter; + private int eventCounter = 0; public BikeSimulator(IDataConverter dataConverter) { @@ -16,22 +20,41 @@ namespace Hardware.Simulators public void StartSimulation() { //4A-09-4E-05-19-16-00-FF-28-00-00-20-F0 + + float x = 0.0f; + + ImprovedPerlin improvedPerlin = new ImprovedPerlin(0,LibNoise.NoiseQuality.Best); while (true) { - byte[] array = { 0x19, 0x16, 0x00, 0xFF, 0x28, 0x00, 0x00, 0x20, 0xF0 }; - + byte[] array = { 0x19, 0x16, 0x00, 0xFF, 0x28, 0x00, 0x00, 0x20 }; + Console.WriteLine(improvedPerlin.GetValue(x)+1); //0x10 message - dataConverter.Bike(array); + /*foreach(byte s in array) + { + Console.Write("{0:X}",s); + }*/ + dataConverter.Bike(GenerateBike(improvedPerlin.GetValue(x)+1)); //0x19 message - dataConverter.Bike(array); + //dataConverter.Bike(array); //Heartbeat message - dataConverter.BPM(array); + //dataConverter.BPM(array); Thread.Sleep(1000); - + x += 1f; + eventCounter++; } } + private byte[] GenerateBike(float perlin) + { + byte[] bikeByte = {0x19,0x} + return new byte[1]; + } + + private double Random(double x) + { + return (Math.Sin(2 * x) + Math.Sin(Math.PI * x) + 2) / 4; + } } } diff --git a/ProftaakRH/ProftaakRH.csproj b/ProftaakRH/ProftaakRH.csproj index ca49088..8f98a2c 100644 --- a/ProftaakRH/ProftaakRH.csproj +++ b/ProftaakRH/ProftaakRH.csproj @@ -4,6 +4,10 @@ Exe netcoreapp3.1 + + + + .\dll\BLELibrary.dll From 53735b3cad6d2cc5793d17f76b4b74b19d8102b2 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 9 Sep 2020 15:17:13 +0200 Subject: [PATCH 12/33] added cadence and accumulated power --- ProftaakRH/DataConverter.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index ed79a28..f4a783b 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -15,6 +15,7 @@ namespace Hardware else if (bytes.Length == 8) { + switch (bytes[0]) { case 0x10: @@ -34,6 +35,15 @@ namespace Hardware break; case 0x19: Console.WriteLine($"Event count: {bytes[1]} (Rollover 256)"); + if (bytes[2] != 0xFF) + { + Console.WriteLine($"Instantaneous cadence: {bytes[2]} RPM (Range 0-254)"); + + } + int accumPower = bytes[3] | (bytes[4] << 8); + + Console.WriteLine($"Accumulated power: {accumPower} watt (Rollover 65536)"); + //Console.WriteLine(); break; default: From 11772f33b7511961ed05bb5f3d4f5bd0ec9f6846 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 9 Sep 2020 16:00:19 +0200 Subject: [PATCH 13/33] finished displaying of data of data page 25 (0x19) --- ProftaakRH/DataConverter.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index f4a783b..7af4cdd 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -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: From a0e1e527a100a734f262ce4193f83765ac27cb7e Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Wed, 9 Sep 2020 16:59:36 +0200 Subject: [PATCH 14/33] Almost done with page 0x10 --- ProftaakRH/BikeSimulator.cs | 66 +++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index e1c058f..48f82b0 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -1,6 +1,7 @@ using LibNoise.Primitive; using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading; @@ -11,7 +12,16 @@ namespace Hardware.Simulators class BikeSimulator { IDataConverter dataConverter; + private int elapsedTime = 0; private int eventCounter = 0; + private double distanceTraveled = 0; + private int equipmentType = 25; + private double speed = 0; + private int BPM = 0; + private int cadence = 0; + private double resistance = 0; + + public BikeSimulator(IDataConverter dataConverter) { @@ -27,34 +37,54 @@ namespace Hardware.Simulators while (true) { - byte[] array = { 0x19, 0x16, 0x00, 0xFF, 0x28, 0x00, 0x00, 0x20 }; - Console.WriteLine(improvedPerlin.GetValue(x)+1); - //0x10 message - /*foreach(byte s in array) - { - Console.Write("{0:X}",s); - }*/ - dataConverter.Bike(GenerateBike(improvedPerlin.GetValue(x)+1)); - //0x19 message - //dataConverter.Bike(array); - //Heartbeat message - //dataConverter.BPM(array); + CalculateVariables(improvedPerlin.GetValue(x)+1); + dataConverter.Bike(GenerateBike0x19()); + dataConverter.Bike(GenerateBike0x10()); + dataConverter.BPM(GenerateHeart()); Thread.Sleep(1000); - x += 1f; + x += 0.1f; eventCounter++; + elapsedTime++; } } - private byte[] GenerateBike(float perlin) + private byte[] GenerateBike0x19() { - byte[] bikeByte = {0x19,0x} - return new byte[1]; + byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter%256), 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + return bikeByte; } - private double Random(double x) + private byte[] GenerateBike0x10() + { + string binary = Convert.ToString((int)speed, 2); + byte b = Convert.ToByte(binary.Substring(0, 3)); + byte b2 = Convert.ToByte(binary.Substring(8)); + byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime*4%64), Convert.ToByte(distanceTraveled), b, b2, Convert.ToByte(BPM), 0xFF }; + return bikeByte; + } + + private byte[] GenerateHeart() + { + byte[] hartByte = { 0x00, Convert.ToByte(BPM)}; + return hartByte; + } + + private void CalculateVariables(float perlin) + { + this.speed = perlin * 5 / 0.001 ; + + + Console.WriteLine(speed); + this.distanceTraveled = (distanceTraveled+speed) % 256; + this.BPM = (int) perlin * 80; + this.cadence = (int)speed * 4; + + } + + /*private double Random(double x) { return (Math.Sin(2 * x) + Math.Sin(Math.PI * x) + 2) / 4; - } + }*/ } } From 2854684b86610a5ca9279c3f9c2e571763dab252 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 17:09:08 +0200 Subject: [PATCH 15/33] wip --- ProftaakRH/{BLEReciever.cs => BLEHandler.cs} | 40 +++- ProftaakRH/FietsDemo.cs | 238 ++++++++++--------- ProftaakRH/Main.cs | 16 +- 3 files changed, 170 insertions(+), 124 deletions(-) rename ProftaakRH/{BLEReciever.cs => BLEHandler.cs} (76%) diff --git a/ProftaakRH/BLEReciever.cs b/ProftaakRH/BLEHandler.cs similarity index 76% rename from ProftaakRH/BLEReciever.cs rename to ProftaakRH/BLEHandler.cs index dfc5827..3c5c1cb 100644 --- a/ProftaakRH/BLEReciever.cs +++ b/ProftaakRH/BLEHandler.cs @@ -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] = 0x08; + antMessage[2] = 0x4E; + antMessage[3] = 0x05; + antMessage[4] = 0x30; + for (int i = 5; i < 11; i++) + { + antMessage[i] = 0xFF; + } + //antMessage[10] = (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 < antMessage.Length -1; i++) + { + checksum ^= antMessage[i]; + } + + antMessage[12] = checksum; + + + bleBike.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage); } } } diff --git a/ProftaakRH/FietsDemo.cs b/ProftaakRH/FietsDemo.cs index eb6d7fb..120292e 100644 --- a/ProftaakRH/FietsDemo.cs +++ b/ProftaakRH/FietsDemo.cs @@ -4,149 +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 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 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(); - } - - 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); + + + } - - } } \ No newline at end of file diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 4625f5d..21e38b2 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Text; using Hardware; +using System.Threading; + namespace ProftaakRH { @@ -11,9 +13,19 @@ namespace ProftaakRH static void Main(string[] args) { IDataConverter dataConverter = new DataConverter(); - BLEReciever bLEReceiver = new BLEReciever(dataConverter); + BLEHandler bLEHandler = new BLEHandler(dataConverter); + + bLEHandler.Connect(); + + while (!bLEHandler.Running) + { + Thread.Yield(); + } + + Console.WriteLine("odlodlJNgeojhtosj\n/nng;sjonghjsngl;zdf\nnhgLLBJHS\nEOGHSFJBNSLDFJSLDFJGHOAIJo;r\njnAJFVBHHBRG"); + + bLEHandler.setResistance(50); - bLEReceiver.Connect(); Console.ReadLine(); } From 226bc265efa732cb73b85c61647b947a8747c725 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 18:29:26 +0200 Subject: [PATCH 16/33] idk i give up (sending messages to BLE) --- ProftaakRH/BLEHandler.cs | 4 ++-- ProftaakRH/Main.cs | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 3c5c1cb..f9ce1f7 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -140,10 +140,10 @@ namespace Hardware antMessage[i] = 0xFF; } //antMessage[10] = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0); - antMessage[11] = 50; //hardcoded for testing + antMessage[11] = 100; //hardcoded for testing byte checksum = 0; - for (int i = 0; i < antMessage.Length -1; i++) + for (int i = 0; i < antMessage.Length; i++) { checksum ^= antMessage[i]; } diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 21e38b2..3cd5d1f 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -25,6 +25,11 @@ namespace ProftaakRH Console.WriteLine("odlodlJNgeojhtosj\n/nng;sjonghjsngl;zdf\nnhgLLBJHS\nEOGHSFJBNSLDFJSLDFJGHOAIJo;r\njnAJFVBHHBRG"); bLEHandler.setResistance(50); + bLEHandler.setResistance(50); + bLEHandler.setResistance(50); + bLEHandler.setResistance(50); + bLEHandler.setResistance(50); + bLEHandler.setResistance(50); Console.ReadLine(); From 75f5ce7d7e517dac5b5e1786e19db3e361cfbe5d Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 20:47:45 +0200 Subject: [PATCH 17/33] checksum fix dumbest reason ever (maybe) --- ProftaakRH/BLEHandler.cs | 9 +++++---- ProftaakRH/Main.cs | 9 --------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index f9ce1f7..5854cf8 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -139,16 +139,17 @@ namespace Hardware { antMessage[i] = 0xFF; } - //antMessage[10] = (byte)Math.Max(Math.Min(Math.Round(percentage / 0.5), 255), 0); - antMessage[11] = 100; //hardcoded for testing + 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 < antMessage.Length; i++) + for (int i = 0; i < 12; i++) { checksum ^= antMessage[i]; } - antMessage[12] = checksum; + //antMessage[12] = checksum;// no because dumb reasons. it is somehow always 0x39 ?!?!?!? magic i tell you!(or a bug:D) + antMessage[12] = 0x39; bleBike.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage); diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 3cd5d1f..93ed7f2 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -22,16 +22,7 @@ namespace ProftaakRH Thread.Yield(); } - Console.WriteLine("odlodlJNgeojhtosj\n/nng;sjonghjsngl;zdf\nnhgLLBJHS\nEOGHSFJBNSLDFJSLDFJGHOAIJo;r\njnAJFVBHHBRG"); - bLEHandler.setResistance(50); - bLEHandler.setResistance(50); - bLEHandler.setResistance(50); - bLEHandler.setResistance(50); - bLEHandler.setResistance(50); - bLEHandler.setResistance(50); - - Console.ReadLine(); } } From 0b659a9dc3185d5280405cc3c3f0542cf4718ca9 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 20:55:38 +0200 Subject: [PATCH 18/33] better checksum fix i was the dumb one :`( --- ProftaakRH/BLEHandler.cs | 6 +++--- ProftaakRH/Main.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 5854cf8..effbb7d 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -131,7 +131,7 @@ namespace Hardware { byte[] antMessage = new byte[13]; antMessage[0] = 0x4A; - antMessage[1] = 0x08; + antMessage[1] = 0x09; antMessage[2] = 0x4E; antMessage[3] = 0x05; antMessage[4] = 0x30; @@ -148,8 +148,8 @@ namespace Hardware checksum ^= antMessage[i]; } - //antMessage[12] = checksum;// no because dumb reasons. it is somehow always 0x39 ?!?!?!? magic i tell you!(or a bug:D) - antMessage[12] = 0x39; + antMessage[12] = checksum;// no because dumb reasons. it is somehow always 0x39 ?!?!?!? magic i tell you!(or a bug:D) + //antMessage[12] = 0x39; bleBike.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage); diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 93ed7f2..4365a26 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -22,7 +22,7 @@ namespace ProftaakRH Thread.Yield(); } - bLEHandler.setResistance(50); + bLEHandler.setResistance(25); Console.ReadLine(); } } From ce2f91854ea985933e6acf8e5e471e346fd94b9c Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 21:00:08 +0200 Subject: [PATCH 19/33] forgot to add :P --- ProftaakRH/BLEHandler.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index effbb7d..85d1072 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -148,8 +148,7 @@ namespace Hardware checksum ^= antMessage[i]; } - antMessage[12] = checksum;// no because dumb reasons. it is somehow always 0x39 ?!?!?!? magic i tell you!(or a bug:D) - //antMessage[12] = 0x39; + antMessage[12] = checksum;//reminder that i am dumb :P bleBike.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage); From 03a0c6febb3c6e5b5e76a5b500f63cc7a8fb45ed Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 11 Sep 2020 11:09:59 +0200 Subject: [PATCH 20/33] Merged --- ProftaakRH/BikeSimulator.cs | 4 +++- ProftaakRH/Main.cs | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 48f82b0..ed5903c 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -21,6 +21,8 @@ namespace Hardware.Simulators private int cadence = 0; private double resistance = 0; + byte[] array; + public BikeSimulator(IDataConverter dataConverter) @@ -73,7 +75,7 @@ namespace Hardware.Simulators private void CalculateVariables(float perlin) { this.speed = perlin * 5 / 0.001 ; - + array = BitConverter.GetBytes(speed); Console.WriteLine(speed); this.distanceTraveled = (distanceTraveled+speed) % 256; diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 8420ab0..447e1ca 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using Hardware; +using Hardware.Simulators; namespace ProftaakRH @@ -14,8 +15,6 @@ namespace ProftaakRH BikeSimulator bikeSimulator = new BikeSimulator(dataConverter); bikeSimulator.StartSimulation(); - bLEReceiver.Connect(); - Console.ReadLine(); } } From 5c29a9c06407219df0e167fb257e7568c483e135 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 11 Sep 2020 11:17:00 +0200 Subject: [PATCH 21/33] removed some comments --- ProftaakRH/BLEHandler.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 85d1072..a832567 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -99,9 +99,7 @@ namespace Hardware private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e) { - //Console.WriteLine("Received from {0}: {1}", e.ServiceName, - // BitConverter.ToString(e.Data).Replace("-", " ")); - //send to dataconverter + if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e") { @@ -140,7 +138,7 @@ namespace Hardware 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++) From e5e6ae16fdd883e682460c4613ee6336d5b21bf9 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 11 Sep 2020 11:24:04 +0200 Subject: [PATCH 22/33] Page 0x10 working --- ProftaakRH/BikeSimulator.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index ed5903c..c1f1a73 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -59,10 +59,7 @@ namespace Hardware.Simulators private byte[] GenerateBike0x10() { - string binary = Convert.ToString((int)speed, 2); - byte b = Convert.ToByte(binary.Substring(0, 3)); - byte b2 = Convert.ToByte(binary.Substring(8)); - byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime*4%64), Convert.ToByte(distanceTraveled), b, b2, Convert.ToByte(BPM), 0xFF }; + byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime*4%64), Convert.ToByte(distanceTraveled), array[0], array[1], Convert.ToByte(BPM), 0xFF }; return bikeByte; } @@ -74,12 +71,12 @@ namespace Hardware.Simulators private void CalculateVariables(float perlin) { - this.speed = perlin * 5 / 0.001 ; - array = BitConverter.GetBytes(speed); - - Console.WriteLine(speed); - this.distanceTraveled = (distanceTraveled+speed) % 256; - this.BPM = (int) perlin * 80; + this.speed = perlin * 5 / 0.01 ; + short sped = (short)speed; + array = BitConverter.GetBytes(sped); + + this.distanceTraveled = (distanceTraveled+(speed*0.001)) % 256; + this.BPM = (int) (perlin * 80); this.cadence = (int)speed * 4; } From 8dac69ebf95fe1202739bcd9547fa003030f9ce3 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 11 Sep 2020 11:27:37 +0200 Subject: [PATCH 23/33] added documentation comments to BLEHandler --- ProftaakRH/BLEHandler.cs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index a832567..b25ef03 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -7,6 +7,9 @@ using System.Security.Cryptography; namespace Hardware { + /// + /// BLEHandler class that handles connection and traffic to and from the bike + /// class BLEHandler { IDataConverter dataConverter; @@ -14,12 +17,19 @@ namespace Hardware private BLE bleHeart; public bool Running { get; set; } + /// + /// Makes a new BLEHandler object + /// + /// the dataconverter object public BLEHandler(IDataConverter dataConverter) { this.dataConverter = dataConverter; bool running = false; } + /// + /// Checks for available devices to connect to, and if one is found, it connects to it + /// public void Connect() { BLE bleBike = new BLE(); @@ -40,6 +50,11 @@ namespace Hardware } } } + + /// + /// Connects to the device with the given name + /// + /// The name of the device to connect to public async void Connect(string deviceName) { int errorCode = 0; @@ -95,12 +110,17 @@ namespace Hardware Console.WriteLine("connected to BLE"); this.Running = true; + } + /// + /// Callback for when the subscription value of the ble bike has changed + /// + /// the sender object + /// the value changed event private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e) { - if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e") { byte[] payload = new byte[8]; @@ -118,6 +138,9 @@ namespace Hardware } + /// + /// Disposes of the current BLE object, if it exists. + /// private void disposeBLE() { this.bleBike?.Dispose(); @@ -125,6 +148,10 @@ namespace Hardware this.Running = false; } + /// + /// Method setResistance converts the input percentage to bytes and sends it to the bike. + /// + /// The precentage of resistance to set public void setResistance(float percentage) { byte[] antMessage = new byte[13]; From 024490df1ea643f7e477bb1025e0e8213daf8409 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 11 Sep 2020 11:32:28 +0200 Subject: [PATCH 24/33] Made Interface --- ProftaakRH/BikeSimulator.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index c1f1a73..f709fa0 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -9,7 +9,7 @@ using System.Threading; namespace Hardware.Simulators { - class BikeSimulator + class BikeSimulator : IHandler { IDataConverter dataConverter; private int elapsedTime = 0; @@ -69,21 +69,33 @@ namespace Hardware.Simulators return hartByte; } + //Calculates the needed variables + //Input perlin value private void CalculateVariables(float perlin) { this.speed = perlin * 5 / 0.01 ; short sped = (short)speed; array = BitConverter.GetBytes(sped); - - this.distanceTraveled = (distanceTraveled+(speed*0.001)) % 256; + this.distanceTraveled = (distanceTraveled+(speed*0.01)) % 256; this.BPM = (int) (perlin * 80); this.cadence = (int)speed * 4; } + public void setResistance(byte[] bytes) + { + throw new NotImplementedException(); + } + /*private double Random(double x) { return (Math.Sin(2 * x) + Math.Sin(Math.PI * x) + 2) / 4; }*/ } + + interface IHandler + { + void setResistance(byte[] bytes); + + } } From 45096558fbe591231f49a43d9131876e9dd220f0 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 11 Sep 2020 11:37:53 +0200 Subject: [PATCH 25/33] added comments to dataconverter --- ProftaakRH/DataConverter.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index 7af4cdd..42d0e4a 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -4,8 +4,15 @@ using System.Text; namespace Hardware { + /// + /// DataConverter class that handles all conversion of received data from the BLE bike. + /// class DataConverter : IDataConverter { + /// + /// Receives, parses and displays any incoming data from the bike. + /// + /// the array of bytes that was received public void Bike(byte[] bytes) { if (bytes == null) @@ -50,6 +57,7 @@ namespace Hardware if (instantPower != 0xFFF) Console.WriteLine($"Instant power: {instantPower} watt (Range 0-4094)"); + // other flags that are not really used but might be useful int trainerStatus = bytes[6] & 0b00001111; // bit 4-7 int flags = bytes[7] >> 4; int FEState = bytes[7] & 0b00001111; @@ -68,6 +76,10 @@ namespace Hardware Console.WriteLine(); } + /// + /// Gets and prints the BPM from the message received from the bike. + /// + /// The array with bytes that was received public void BPM(byte[] bytes) { if (bytes == null) @@ -92,6 +104,9 @@ namespace Hardware } } + /// + /// Dataconverter interface for handling data received from the bike + /// interface IDataConverter { void BPM(byte[] bytes); From 631129a206b675a82320efc6bf0962fa085894e8 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 11 Sep 2020 11:48:39 +0200 Subject: [PATCH 26/33] Set resistance in simulator --- ProftaakRH/BikeSimulator.cs | 33 ++++++++++++++++++++++++++++++++- ProftaakRH/Main.cs | 1 + 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index f709fa0..9f13222 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -69,6 +69,32 @@ namespace Hardware.Simulators return hartByte; } + public byte[] GenerateResistance(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 + + return antMessage; + } + //Calculates the needed variables //Input perlin value private void CalculateVariables(float perlin) @@ -84,7 +110,12 @@ namespace Hardware.Simulators public void setResistance(byte[] bytes) { - throw new NotImplementedException(); + if(bytes.Length == 13) + { + this.resistance = Convert.ToDouble(bytes[11])/2; + Console.WriteLine(resistance); + + } } /*private double Random(double x) diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 447e1ca..0029220 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -13,6 +13,7 @@ namespace ProftaakRH { IDataConverter dataConverter = new DataConverter(); BikeSimulator bikeSimulator = new BikeSimulator(dataConverter); + bikeSimulator.setResistance(bikeSimulator.GenerateResistance(1f)); bikeSimulator.StartSimulation(); Console.ReadLine(); From 6e07f3b33a9e2a259def00acbd00032d526be0e8 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 11 Sep 2020 11:50:28 +0200 Subject: [PATCH 27/33] fixed instant power who doesn't love bit flipping --- ProftaakRH/BLEHandler.cs | 2 +- ProftaakRH/DataConverter.cs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index 85d1072..e32fea5 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -151,7 +151,7 @@ namespace Hardware antMessage[12] = checksum;//reminder that i am dumb :P - bleBike.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage); + bleBike.WriteCharacteristic("6e40fec3-b5a3-f393-e0a9-e50e24dcca9e", antMessage); } } } diff --git a/ProftaakRH/DataConverter.cs b/ProftaakRH/DataConverter.cs index 7af4cdd..beb3684 100644 --- a/ProftaakRH/DataConverter.cs +++ b/ProftaakRH/DataConverter.cs @@ -44,16 +44,17 @@ namespace Hardware Console.WriteLine($"Accumulated power: {accumPower} watt (Rollover 65536)"); - int instantPower = (bytes[5]) | (bytes[6]>>4)<<8; + int instantPower = (bytes[5]) | (bytes[6] & 0b00001111)<<8; if (instantPower != 0xFFF) Console.WriteLine($"Instant power: {instantPower} watt (Range 0-4094)"); - int trainerStatus = bytes[6] & 0b00001111; // bit 4-7 + int trainerStatus = bytes[6] & 0b11110000; // bit 4-7 int flags = bytes[7] >> 4; int FEState = bytes[7] & 0b00001111; + break; default: From 7da952628d2118e37cd245aaf6047767807af1a9 Mon Sep 17 00:00:00 2001 From: fabjuuuh Date: Fri, 11 Sep 2020 11:55:13 +0200 Subject: [PATCH 28/33] Added comments to BikeSimulation --- ProftaakRH/BikeSimulator.cs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 9f13222..5239201 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -31,44 +31,54 @@ namespace Hardware.Simulators } public void StartSimulation() { + //Example BLE Message //4A-09-4E-05-19-16-00-FF-28-00-00-20-F0 float x = 0.0f; + //Perlin for Random values ImprovedPerlin improvedPerlin = new ImprovedPerlin(0,LibNoise.NoiseQuality.Best); while (true) { CalculateVariables(improvedPerlin.GetValue(x)+1); + + //Simulate sending data dataConverter.Bike(GenerateBike0x19()); dataConverter.Bike(GenerateBike0x10()); dataConverter.BPM(GenerateHeart()); Thread.Sleep(1000); + x += 0.1f; eventCounter++; elapsedTime++; } } + + //Generate an ANT message for page 0x19 private byte[] GenerateBike0x19() { byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter%256), 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; return bikeByte; } + //Generate an ANT message for page 0x10 private byte[] GenerateBike0x10() { byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime*4%64), Convert.ToByte(distanceTraveled), array[0], array[1], Convert.ToByte(BPM), 0xFF }; return bikeByte; } + //Generate an ANT message for BPM private byte[] GenerateHeart() { byte[] hartByte = { 0x00, Convert.ToByte(BPM)}; return hartByte; } + //Generate an ANT message for resistance public byte[] GenerateResistance(float percentage) { byte[] antMessage = new byte[13]; @@ -108,22 +118,18 @@ namespace Hardware.Simulators } + //Set resistance in simulated bike public void setResistance(byte[] bytes) { + //TODO check if message is correct if(bytes.Length == 13) { - this.resistance = Convert.ToDouble(bytes[11])/2; - Console.WriteLine(resistance); - + this.resistance = Convert.ToDouble(bytes[11])/2; } } - - /*private double Random(double x) - { - return (Math.Sin(2 * x) + Math.Sin(Math.PI * x) + 2) / 4; - }*/ } + //Interface for receiving a message on the simulated bike interface IHandler { void setResistance(byte[] bytes); From a471854d71f309a3adc2831f64f19ee8ee6c1f31 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 11 Sep 2020 12:37:15 +0200 Subject: [PATCH 29/33] added null check for sending resistance --- ProftaakRH/BLEHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProftaakRH/BLEHandler.cs b/ProftaakRH/BLEHandler.cs index b25ef03..e7f00a9 100644 --- a/ProftaakRH/BLEHandler.cs +++ b/ProftaakRH/BLEHandler.cs @@ -176,7 +176,7 @@ namespace Hardware antMessage[12] = checksum;//reminder that i am dumb :P - bleBike.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage); + bleBike?.WriteCharacteristic("6E40FEC3-B5A3-F393-E0A9-E50E24DCCA9E", antMessage); } } } From e77372c91303ab39bca1db242311e36ae42874a0 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 11 Sep 2020 12:39:06 +0200 Subject: [PATCH 30/33] added resistance changing --- ProftaakRH/Main.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 4365a26..1bed6eb 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -23,7 +23,25 @@ namespace ProftaakRH } bLEHandler.setResistance(25); - Console.ReadLine(); + while (true) + { + string input = Console.ReadLine(); + input.ToLower(); + input.Trim(); + if(input == "quit") + { + break; + } + try + { + int resistance = Int32.Parse(input); + bLEHandler.setResistance(resistance); + } + catch + { + //do nothing + } + } } } } From 40f70a42d4ccf8234a134e9b26fe1988a2b5af53 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 11 Sep 2020 12:41:29 +0200 Subject: [PATCH 31/33] changed while true --- ProftaakRH/Main.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 1bed6eb..2d3ffca 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -23,13 +23,15 @@ namespace ProftaakRH } bLEHandler.setResistance(25); - while (true) + bool running = true; + while (running) { string input = Console.ReadLine(); input.ToLower(); input.Trim(); if(input == "quit") { + running = false; break; } try From dee370a4cb5b5b6a7610e7cc5f68716e85be3ae5 Mon Sep 17 00:00:00 2001 From: Logophilist Date: Fri, 11 Sep 2020 14:48:44 +0200 Subject: [PATCH 32/33] Worked on 0x19 simulator --- ProftaakRH/BikeSimulator.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 5239201..9a5b788 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -20,8 +20,12 @@ namespace Hardware.Simulators private int BPM = 0; private int cadence = 0; private double resistance = 0; + private double power; + private double accPower; - byte[] array; + byte[] speedArray; + byte[] powerArray; + byte[] accPowerArray; @@ -60,14 +64,14 @@ namespace Hardware.Simulators //Generate an ANT message for page 0x19 private byte[] GenerateBike0x19() { - byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter%256), 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter%256), Convert.ToByte(cadence%254), accPowerArray[0], accPowerArray[1], 0x0E, 0x00, 0x20 }; return bikeByte; } //Generate an ANT message for page 0x10 private byte[] GenerateBike0x10() { - byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime*4%64), Convert.ToByte(distanceTraveled), array[0], array[1], Convert.ToByte(BPM), 0xFF }; + byte[] bikeByte = { 0x10, Convert.ToByte(equipmentType), Convert.ToByte(elapsedTime*4%64), Convert.ToByte(distanceTraveled), speedArray[0], speedArray[1], Convert.ToByte(BPM), 0xFF }; return bikeByte; } @@ -111,11 +115,14 @@ namespace Hardware.Simulators { this.speed = perlin * 5 / 0.01 ; short sped = (short)speed; - array = BitConverter.GetBytes(sped); + speedArray = BitConverter.GetBytes(sped); this.distanceTraveled = (distanceTraveled+(speed*0.01)) % 256; this.BPM = (int) (perlin * 80); - this.cadence = (int)speed * 4; - + this.cadence = (int)speed * 2; + this.power = ((1 + resistance) * speed) % 4094; + this.accPower = (this.accPower + this.power) % 65536; + // TO DO power to power LSB & MSN + accPowerArray = BitConverter.GetBytes((short)accPower); } //Set resistance in simulated bike From 1c0958f33c94c0f85e33f91f6023581dc133e13d Mon Sep 17 00:00:00 2001 From: Logophilist Date: Fri, 11 Sep 2020 15:31:14 +0200 Subject: [PATCH 33/33] Bike Simulator 0x19 (acc)Power update and cadence fix --- ProftaakRH/BikeSimulator.cs | 8 +++++--- ProftaakRH/Main.cs | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ProftaakRH/BikeSimulator.cs b/ProftaakRH/BikeSimulator.cs index 9a5b788..5664f9e 100644 --- a/ProftaakRH/BikeSimulator.cs +++ b/ProftaakRH/BikeSimulator.cs @@ -64,7 +64,8 @@ namespace Hardware.Simulators //Generate an ANT message for page 0x19 private byte[] GenerateBike0x19() { - byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter%256), Convert.ToByte(cadence%254), accPowerArray[0], accPowerArray[1], 0x0E, 0x00, 0x20 }; + byte statByte = (byte)(powerArray[1] >> 4); + byte[] bikeByte = { 0x19, Convert.ToByte(eventCounter%256), Convert.ToByte(cadence%254), accPowerArray[0], accPowerArray[1], powerArray[0], statByte, 0x20 }; return bikeByte; } @@ -118,10 +119,11 @@ namespace Hardware.Simulators speedArray = BitConverter.GetBytes(sped); this.distanceTraveled = (distanceTraveled+(speed*0.01)) % 256; this.BPM = (int) (perlin * 80); - this.cadence = (int)speed * 2; - this.power = ((1 + resistance) * speed) % 4094; + this.cadence = (int)speed/6; + this.power = ((1 + resistance) * speed)/14 % 4094; this.accPower = (this.accPower + this.power) % 65536; // TO DO power to power LSB & MSN + powerArray = BitConverter.GetBytes((short)this.power); accPowerArray = BitConverter.GetBytes((short)accPower); } diff --git a/ProftaakRH/Main.cs b/ProftaakRH/Main.cs index 0029220..dfeda82 100644 --- a/ProftaakRH/Main.cs +++ b/ProftaakRH/Main.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using Hardware;