Merge remote-tracking branch 'origin/DataConverter' into simulation

This commit is contained in:
fabjuuuh
2020-09-11 11:03:08 +02:00
4 changed files with 171 additions and 131 deletions

View File

@@ -7,14 +7,14 @@ using System.Security.Cryptography;
namespace Hardware namespace Hardware
{ {
class BLEReciever class BLEHandler
{ {
IDataConverter dataConverter; IDataConverter dataConverter;
private BLE bleBike; private BLE bleBike;
private BLE bleHeart; private BLE bleHeart;
public bool running { get; set; } public bool Running { get; set; }
public BLEReciever(IDataConverter dataConverter) public BLEHandler(IDataConverter dataConverter)
{ {
this.dataConverter = dataConverter; this.dataConverter = dataConverter;
bool running = false; bool running = false;
@@ -94,7 +94,7 @@ namespace Hardware
} }
Console.WriteLine("connected to BLE"); Console.WriteLine("connected to BLE");
this.running = true; this.Running = true;
} }
private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e) private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
@@ -108,7 +108,8 @@ namespace Hardware
byte[] payload = new byte[8]; byte[] payload = new byte[8];
Array.Copy(e.Data, 4, payload, 0, 8); Array.Copy(e.Data, 4, payload, 0, 8);
this.dataConverter.Bike(payload); 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); this.dataConverter.BPM(e.Data);
} }
@@ -123,7 +124,34 @@ namespace Hardware
{ {
this.bleBike?.Dispose(); this.bleBike?.Dispose();
this.bleHeart?.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);
} }
} }
} }

View File

@@ -43,7 +43,17 @@ namespace Hardware
int accumPower = bytes[3] | (bytes[4] << 8); int accumPower = bytes[3] | (bytes[4] << 8);
Console.WriteLine($"Accumulated power: {accumPower} watt (Rollover 65536)"); 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; break;
default: default:

View File

@@ -4,37 +4,12 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Hardware.Simulators;
using Hardware;
namespace FietsDemo namespace FietsDemo
{ {
internal class Program internal class Program
{ {
//static void Main(string[] args) private static async Task Main(string[] args)
//{
// foo foo = new foo();
// BLEReceiver bLEReceiver = new BLEReceiver(foo);
// bLEReceiver.ConnectToBLE();
// Console.Read();
//}
interface IFoo
{
void doStuff(byte[] bytes);
}
class BLEReceiver
{
IFoo foo;
public BLEReceiver(IFoo foo)
{
this.foo = foo;
}
public async void ConnectToBLE()
{ {
int errorCode = 0; int errorCode = 0;
BLE bleBike = new BLE(); BLE bleBike = new BLE();
@@ -78,82 +53,106 @@ namespace FietsDemo
Console.Read(); Console.Read();
} }
/* public async void ConnectToSimulation() private static void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
{
BikeSimulator bikeSimulator = new BikeSimulator();
bikeSimulator.StartSimulation(foo);
}*/
private void BleBike_SubscriptionValueChanged(object sender, BLESubscriptionValueChangedEventArgs e)
{ {
//Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName, //Console.WriteLine("Received from {0}: {1}, {2}", e.ServiceName,
// BitConverter.ToString(e.Data).Replace("-", " "), // BitConverter.ToString(e.Data).Replace("-", " "),
// Encoding.UTF8.GetString(e.Data)); // Encoding.UTF8.GetString(e.Data));
//string[] bytes = BitConverter.ToString(e.Data).Split('-'); string[] bytes = BitConverter.ToString(e.Data).Split('-');
//string[] ANT = new string[5]; string[] ANT = new string[5];
//if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e") if (e.ServiceName == "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e")
//{ {
// Console.WriteLine("SYNC : " + bytes[0]); //Console.WriteLine("SYNC : " + bytes[0]);
// ANT[0] = bytes[0]; //ANT[0] = bytes[0];
// Console.WriteLine("LENGTH : " + bytes[1]); //Console.WriteLine("LENGTH : " + bytes[1]);
// int length = Convert.ToInt32(bytes[1], 16); int length = Convert.ToInt32(bytes[1], 16);
// ANT[1] = length.ToString(); //ANT[1] = length.ToString();
// Console.WriteLine("MSG ID : " + bytes[2]); //Console.WriteLine("MSG ID : " + bytes[2]);
// ANT[2] = bytes[2]; //ANT[2] = bytes[2];
// string msg = string.Empty; //string msg = string.Empty;
// for (int i = 3; i < 3 + length; i++) //for (int i = 3; i < 3 + length; i++)
// { //{
// msg += bytes[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 //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));
}
else
{
//Console.WriteLine("BPM: " + Convert.ToInt32(bytes[1], 16));
}
Console.WriteLine();
} }
private static void DoCrazyShitWithMsg(byte[] bytes) private static void DoCrazyShitWithMsg(byte[] bytes)
{ {
Console.WriteLine("doing crazy shit with {0}", bytes);
String[] hexvalues = BitConverter.ToString(bytes).Split('-'); String[] hexvalues = BitConverter.ToString(bytes).Split('-');
for (int i = 0; i < hexvalues.Length; i++) for (int i = 0; i < hexvalues.Length; i++)
{ {
Console.WriteLine("Byte {0}: {1}", i, hexvalues[i]); Console.WriteLine("Byte {0}: {1}", i, hexvalues[i]);
} }
} switch (bytes[0])
}
class foo : IFoo
{
public void doStuff(byte[] bytes)
{ {
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);
}
} }
} }

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Hardware; using Hardware;
using Hardware.Simulators;
namespace ProftaakRH namespace ProftaakRH
{ {
@@ -14,6 +14,9 @@ namespace ProftaakRH
BikeSimulator bikeSimulator = new BikeSimulator(dataConverter); BikeSimulator bikeSimulator = new BikeSimulator(dataConverter);
bikeSimulator.StartSimulation(); bikeSimulator.StartSimulation();
bLEReceiver.Connect();
Console.ReadLine();
} }
} }
} }