123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using ANT_Managed_Library;
- using System;
- /*
- Frequencies such as 4.06 Hz (ANT+ Heart
- Rate) and 4.005 Hz (ANT+ Bike Power) will
- periodically “drift” into each other, or into
- other channel periods that may be present
- in the vicinity. During this overlap, channel
- collisions may occur as the radio can only
- service channel at a time.
- */
- public class PowerMeterReceiver
- {
- private int deviceID = 0; //set this to connect to a specific device ID
- private bool connected = false; //will be set to true once connected
- private List<AntDevice> scanResult;
- private int instantaneousPower; // the instantaneous power in watt
- private int instantaneousCadence; // crank cadence in RPM if available ( 255 indicates invalid)
- private AntChannel backgroundScanChannel;
- private AntChannel deviceChannel;
- public int DeviceID => deviceID;
- public bool Connected => connected;
- public List<AntDevice> ScanResult => scanResult;
- public int InstantaneousPower => instantaneousPower;
- public int InstantaneousCadence => instantaneousCadence;
- private byte[] pageToSend;
- public PowerMeterReceiver()
- {
- }
- public PowerMeterReceiver(int deviceID) => this.deviceID = deviceID;
- public void StartScan()
- {
- Debug.Log("Looking for ANT + power meter sensor");
- AntManager.Instance.Init();
- scanResult = new List<AntDevice>();
- backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
- backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
- }
- void ReceivedBackgroundScanData(Byte[] data)
- {
- byte deviceType = (data[12]); // extended info Device Type byte
- switch (deviceType)
- {
- case AntplusDeviceType.BikePower:
- {
- int deviceNumber = (data[10]) | data[11] << 8;
- byte transType = data[13];
- foreach (AntDevice d in scanResult)
- {
- if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
- return;
- }
- AntDevice foundDevice = new AntDevice();
- foundDevice.deviceType = deviceType;
- foundDevice.deviceNumber = deviceNumber;
- foundDevice.transType = transType;
- foundDevice.period = 8182;
- foundDevice.radiofreq = 57;
- foundDevice.name = "Powermeter(" + foundDevice.deviceNumber + ")";
- scanResult.Add(foundDevice);
- if (deviceNumber == deviceID)
- {
- Debug.Log($"Desired Power Sensor with id {deviceNumber} found!");
- ConnectToDevice(foundDevice);
- }
- else
- {
- Debug.Log($"Power sensor ({deviceNumber}) found and added to ScanResult");
- }
- break;
- }
- }
- }
- void ConnectToDevice(AntDevice device)
- {
- AntManager.Instance.CloseBackgroundScanChannel();
- byte channelID = AntManager.Instance.GetFreeChannelID();
- deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00,
- channelID, (ushort) device.deviceNumber, device.deviceType, device.transType, (byte) device.radiofreq,
- (ushort) device.period, false);
- connected = true;
- deviceChannel.onReceiveData += Data;
- deviceChannel.onChannelResponse += ChannelResponse;
- deviceChannel.hideRXFAIL = true;
- }
- int update_event_count = 0;
- int sameEventCounter = 0;
- //Deal with the received Data
- private void Data(Byte[] data)
- {
- if (data[0] == 0x10)
- {
- if (data[1] == update_event_count)
- sameEventCounter++;
- else
- sameEventCounter = 0;
- update_event_count = data[1];
- if (sameEventCounter > 3)
- {
- instantaneousPower = 0;
- instantaneousCadence = 0;
- }
- else
- {
- instantaneousPower = (data[6]) | data[7] << 8;
- instantaneousCadence = data[3];
- }
- }
- }
- public void Calibrate()
- {
- Debug.Log("Sending : Manual Zero Calibration request");
- pageToSend = new byte[8] {0x01, 0xAA, 0x0FF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- deviceChannel.sendAcknowledgedData(pageToSend);
- }
- void ChannelResponse(ANT_Response response)
- {
- if (response.getChannelEventCode() == ANT_ReferenceLibrary.ANTEventID.EVENT_TRANSFER_TX_FAILED_0x06)
- {
- deviceChannel.sendAcknowledgedData(pageToSend); //send the page again if the transfer failed
- }
- }
- }
|