From 650f308cf509e937a49f8504a9c7bc92c8774b14 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 9 Sep 2020 14:49:40 +0200 Subject: [PATCH] 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(); } }