Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2016 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/bd8c6be01ee910c5f625 to your computer and use it in GitHub Desktop.
Save anonymous/bd8c6be01ee910c5f625 to your computer and use it in GitHub Desktop.
// Fill out your copyright notice in the Description page of Project Settings.
#include "CompuTrainerPluginModulePCH.h"
#include "CompuTrainer.h"
#if WITH_COMPUTRAINER_BINDING
#include "RacerMate/racermate.h"
#endif
void UCompuTrainer::Initialize(int32 ComPort)
{
Port = ComPort - 1;
#if WITH_COMPUTRAINER_BINDING
int status = GetRacerMateDeviceID(Port);
if (status != DEVICE_COMPUTRAINER)
{
UE_LOG(LogTemp, Fatal, TEXT("Failed to find CompuTrainer on ComPort %d."), (Port + 1));
}
FirmwareVersion = (int32)GetFirmWareVersion(Port);
if (FirmwareVersion < 0)
{
const char *pErrorString = get_errstr(FirmwareVersion);
UE_LOG(LogTemp, Fatal, TEXT("Failed to get Firmware Version from CompuTrainer: %s"), pErrorString);
}
bool isCalibrated = GetIsCalibrated(Port, FirmwareVersion) == 0 ? false : true;
if (!isCalibrated)
{
UE_LOG(LogTemp, Log, TEXT("CompuTrainer is not calibrated."));
}
const char* pAPIVersion = GetAPIVersion();
UE_LOG(LogTemp, Log, TEXT("CompuTrainer API Version: %s"), pAPIVersion);
const char* pDLLVersion = get_dll_version();
UE_LOG(LogTemp, Log, TEXT("CompuTrainer DLL Version: %s"), pDLLVersion);
Calibration = (int32)GetCalibration(Port);
if (Calibration < 0)
{
const char *pErrorString = get_errstr((int)Calibration);
UE_LOG(LogTemp, Fatal, TEXT("Failed to get Calibration from CompuTrainer: %s (Try power toggling CompuTrainer!)"), pErrorString);
}
status = startTrainer(Port);
if (status != ALL_OK)
{
UE_LOG(LogTemp, Fatal, TEXT("Failed to run Start Trainer on CompuTrainer"));
}
UE_LOG(LogTemp, Log, TEXT("CompuTrainer started on port: %d"), (Port + 1));
// Maybe Sleep Here?
status = resetTrainer(Port, FirmwareVersion, Calibration);
if (status != ALL_OK)
{
const char *pErrorString = get_errstr(status);
UE_LOG(LogTemp, Fatal, TEXT("Failed to run Reset Trainer on CompuTrainer: %s"), pErrorString);
}
status = ResetAverages(Port, FirmwareVersion);
if (status != ALL_OK)
{
const char *pErrorString = get_errstr(status);
UE_LOG(LogTemp, Fatal, TEXT("Failed to run Reset Averages on CompuTrainer: %s"), pErrorString);
}
// pause
// sleep
// unpause
// SetErgModeLoad
// SetSlope
UE_LOG(LogTemp, Log, TEXT("CompuTrainer finished initialization."));
#endif
}
void UCompuTrainer::BeginDestroy()
{
UE_LOG(LogTemp, Log, TEXT("Destroying CompuTrainer %s on Port %d"), *GetOuter()->GetFullName(), Port);
#if WITH_COMPUTRAINER_BINDING
int status = stopTrainer(Port);
if (status != ALL_OK)
{
const char *pErrorString = get_errstr(status);
//UE_LOG(LogTemp, Fatal, TEXT("Failed to run Stop Trainer on CompuTrainer: %s"), pErrorString);
}
status = ResettoIdle(Port);
if (status != ALL_OK)
{
const char *pErrorString = get_errstr(status);
//UE_LOG(LogTemp, Fatal, TEXT("Failed to run Stop Trainer on CompuTrainer: %s"), pErrorString);
}
#endif
Super::BeginDestroy();
}
void UCompuTrainer::Tick(float DeltaTime)
{
#if WITH_COMPUTRAINER_BINDING
TrainerData trainerData = GetTrainerData(Port, FirmwareVersion);
// Copy the data over..
FCompuTrainerData ueData;
ueData.KilometersPerHour = trainerData.kph;
ueData.Cadence = trainerData.cadence;
ueData.HeartRate = trainerData.HR;
ueData.PowerInWatts = trainerData.Power;
if (OnDataUpdateNotify.IsBound())
{
OnDataUpdateNotify.Broadcast(ueData);
}
#endif
}
bool UCompuTrainer::IsTickable() const
{
return GWorld->HasBegunPlay();
}
TStatId UCompuTrainer::GetStatId() const
{
return GetStatID();
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Object.h"
#include "CompuTrainer.generated.h"
USTRUCT(BlueprintType)
struct FCompuTrainerData
{
GENERATED_USTRUCT_BODY();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CompuTrainer")
float KilometersPerHour; // < 0 on Error
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CompuTrainer")
float Cadence; // RPM, < 0 if not connected or errored
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CompuTrainer")
float HeartRate; // BPM, < 0 if not connected or errored
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CompuTrainer")
float PowerInWatts; // 0 on error.
};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnCompuTrainerDataUpdateDelegate, FCompuTrainerData, Data);
UCLASS(BlueprintType)
class UCompuTrainer : public UObject , public FTickableGameObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category="CompuTrainer")
void Initialize(int32 ComPort);
UPROPERTY(BlueprintAssignable, Category = "CompuTrainer")
FOnCompuTrainerDataUpdateDelegate OnDataUpdateNotify;
virtual void BeginDestroy() override;
virtual void Tick(float DeltaTime) override;
virtual bool IsTickable() const override;
virtual TStatId GetStatId() const override;
private:
int32 FirmwareVersion;
int32 Calibration;
int32 Port;
};
// You should place include statements to your module's private header files here. You only need to
// add includes for headers that are used in most of your module's source files though.
#include "Engine.h"
#include "CoreUObject.h"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment