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 1/6] 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 2854684b86610a5ca9279c3f9c2e571763dab252 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 17:09:08 +0200 Subject: [PATCH 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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);