mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
SPI and I2C simulator (#662)
* Creating SPI and I2C simulation abilities * Update the callback helpers to support different function callback types * Create callback type that uses a buffer * Created I2C/SPI data classes that manage the callbacks and don't do much of anything else * Ran format, cleaned up some issues
This commit is contained in:
committed by
Peter Johnson
parent
be77f9cb26
commit
3c842d8234
151
hal/src/main/native/sim/MockData/I2CData.cpp
Normal file
151
hal/src/main/native/sim/MockData/I2CData.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "../PortsInternal.h"
|
||||
#include "I2CDataInternal.h"
|
||||
#include "NotifyCallbackHelpers.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
I2CData hal::SimI2CData[2];
|
||||
|
||||
void I2CData::ResetData() {
|
||||
m_initialized = false;
|
||||
m_initializedCallbacks = nullptr;
|
||||
m_readCallbacks = nullptr;
|
||||
}
|
||||
|
||||
I2CData::I2CData() {}
|
||||
I2CData::~I2CData() {}
|
||||
|
||||
///////////////////////////////////////////
|
||||
// Initialize
|
||||
///////////////////////////////////////////
|
||||
int32_t I2CData::RegisterInitializedCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_registerMutex);
|
||||
m_initializedCallbacks = RegisterCallback(
|
||||
m_initializedCallbacks, "Initialized", callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeBoolean(GetInitialized());
|
||||
callback("Initialized", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void I2CData::CancelInitializedCallback(int32_t uid) {
|
||||
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
|
||||
}
|
||||
|
||||
void I2CData::InvokeInitializedCallback(HAL_Value value) {
|
||||
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
|
||||
}
|
||||
|
||||
HAL_Bool I2CData::GetInitialized() { return m_initialized; }
|
||||
|
||||
void I2CData::SetInitialized(HAL_Bool initialized) {
|
||||
HAL_Bool oldValue = m_initialized.exchange(initialized);
|
||||
if (oldValue != initialized) {
|
||||
InvokeInitializedCallback(MakeBoolean(initialized));
|
||||
}
|
||||
}
|
||||
|
||||
int32_t I2CData::RegisterReadCallback(HAL_BufferCallback callback,
|
||||
void* param) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_registerMutex);
|
||||
m_readCallbacks =
|
||||
RegisterCallback(m_readCallbacks, "Read", callback, param, &newUid);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void I2CData::CancelReadCallback(int32_t uid) {
|
||||
m_readCallbacks = CancelCallback(m_readCallbacks, uid);
|
||||
}
|
||||
|
||||
int32_t I2CData::RegisterWriteCallback(HAL_BufferCallback callback,
|
||||
void* param) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_registerMutex);
|
||||
m_writeCallbacks =
|
||||
RegisterCallback(m_writeCallbacks, "Write", callback, param, &newUid);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void I2CData::CancelWriteCallback(int32_t uid) {
|
||||
m_writeCallbacks = CancelCallback(m_writeCallbacks, uid);
|
||||
}
|
||||
|
||||
void I2CData::Write(int32_t deviceAddress, uint8_t* dataToSend,
|
||||
int32_t sendSize) {
|
||||
std::lock_guard<std::mutex> lock(m_dataMutex);
|
||||
InvokeCallback(m_writeCallbacks, "Write", dataToSend, sendSize);
|
||||
}
|
||||
void I2CData::Read(int32_t deviceAddress, uint8_t* buffer, int32_t count) {
|
||||
std::lock_guard<std::mutex> lock(m_dataMutex);
|
||||
InvokeCallback(m_readCallbacks, "Read", buffer, count);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetI2CData(int32_t index) { SimI2CData[index].ResetData(); }
|
||||
|
||||
int32_t HALSIM_RegisterI2CInitializedCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
return SimI2CData[index].RegisterInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
|
||||
void HALSIM_CancelI2CInitializedCallback(int32_t index, int32_t uid) {
|
||||
SimI2CData[index].CancelInitializedCallback(uid);
|
||||
}
|
||||
|
||||
HAL_Bool HALSIM_GetI2CInitialized(int32_t index) {
|
||||
return SimI2CData[index].GetInitialized();
|
||||
}
|
||||
|
||||
void HALSIM_SetI2CInitialized(int32_t index, HAL_Bool initialized) {
|
||||
SimI2CData[index].SetInitialized(initialized);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterI2CReadCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param) {
|
||||
return SimI2CData[index].RegisterReadCallback(callback, param);
|
||||
}
|
||||
void HALSIM_CancelI2CReadCallback(int32_t index, int32_t uid) {
|
||||
SimI2CData[index].CancelReadCallback(uid);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterI2CWriteCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param) {
|
||||
return SimI2CData[index].RegisterWriteCallback(callback, param);
|
||||
}
|
||||
void HALSIM_CancelI2CWriteCallback(int32_t index, int32_t uid) {
|
||||
SimI2CData[index].CancelWriteCallback(uid);
|
||||
}
|
||||
|
||||
} // extern c
|
||||
50
hal/src/main/native/sim/MockData/I2CDataInternal.h
Normal file
50
hal/src/main/native/sim/MockData/I2CDataInternal.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
|
||||
#include "MockData/I2CData.h"
|
||||
#include "MockData/NotifyListenerVector.h"
|
||||
|
||||
namespace hal {
|
||||
class I2CData {
|
||||
public:
|
||||
I2CData();
|
||||
~I2CData();
|
||||
|
||||
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelInitializedCallback(int32_t uid);
|
||||
void InvokeInitializedCallback(HAL_Value value);
|
||||
HAL_Bool GetInitialized();
|
||||
void SetInitialized(HAL_Bool initialized);
|
||||
|
||||
int32_t RegisterReadCallback(HAL_BufferCallback callback, void* param);
|
||||
void CancelReadCallback(int32_t uid);
|
||||
|
||||
int32_t RegisterWriteCallback(HAL_BufferCallback callback, void* param);
|
||||
void CancelWriteCallback(int32_t uid);
|
||||
|
||||
void Write(int32_t deviceAddress, uint8_t* dataToSend, int32_t sendSize);
|
||||
void Read(int32_t deviceAddress, uint8_t* buffer, int32_t count);
|
||||
|
||||
void ResetData();
|
||||
|
||||
private:
|
||||
std::mutex m_registerMutex;
|
||||
std::mutex m_dataMutex;
|
||||
std::atomic<HAL_Bool> m_initialized{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
|
||||
std::shared_ptr<BufferListenerVector> m_readCallbacks = nullptr;
|
||||
std::shared_ptr<BufferListenerVector> m_writeCallbacks = nullptr;
|
||||
};
|
||||
extern I2CData SimI2CData[];
|
||||
} // namespace hal
|
||||
@@ -9,12 +9,13 @@
|
||||
|
||||
using namespace hal;
|
||||
|
||||
std::shared_ptr<NotifyListenerVector> RegisterCallback(
|
||||
std::shared_ptr<NotifyListenerVector> currentVector, const char* name,
|
||||
HAL_NotifyCallback callback, void* param, int32_t* newUid) {
|
||||
std::shared_ptr<NotifyListenerVector> newCallbacks;
|
||||
template <typename VectorType, typename CallbackType>
|
||||
std::shared_ptr<VectorType> RegisterCallbackImpl(
|
||||
std::shared_ptr<VectorType> currentVector, const char* name,
|
||||
CallbackType callback, void* param, int32_t* newUid) {
|
||||
std::shared_ptr<VectorType> newCallbacks;
|
||||
if (currentVector == nullptr) {
|
||||
newCallbacks = std::make_shared<NotifyListenerVector>(
|
||||
newCallbacks = std::make_shared<VectorType>(
|
||||
param, callback, reinterpret_cast<unsigned int*>(newUid));
|
||||
} else {
|
||||
newCallbacks = currentVector->emplace_back(
|
||||
@@ -23,13 +24,27 @@ std::shared_ptr<NotifyListenerVector> RegisterCallback(
|
||||
return newCallbacks;
|
||||
}
|
||||
|
||||
std::shared_ptr<NotifyListenerVector> CancelCallback(
|
||||
std::shared_ptr<NotifyListenerVector> currentVector, int32_t uid) {
|
||||
template <typename VectorType, typename CallbackType>
|
||||
std::shared_ptr<VectorType> CancelCallbackImpl(
|
||||
std::shared_ptr<VectorType> currentVector, int32_t uid) {
|
||||
// Create a copy of the callbacks to erase from
|
||||
auto newCallbacks = currentVector->erase(uid);
|
||||
return newCallbacks;
|
||||
}
|
||||
|
||||
std::shared_ptr<NotifyListenerVector> RegisterCallback(
|
||||
std::shared_ptr<NotifyListenerVector> currentVector, const char* name,
|
||||
HAL_NotifyCallback callback, void* param, int32_t* newUid) {
|
||||
return RegisterCallbackImpl<NotifyListenerVector, HAL_NotifyCallback>(
|
||||
currentVector, name, callback, param, newUid);
|
||||
}
|
||||
|
||||
std::shared_ptr<NotifyListenerVector> CancelCallback(
|
||||
std::shared_ptr<NotifyListenerVector> currentVector, int32_t uid) {
|
||||
return CancelCallbackImpl<NotifyListenerVector, HAL_NotifyCallback>(
|
||||
currentVector, uid);
|
||||
}
|
||||
|
||||
void InvokeCallback(std::shared_ptr<NotifyListenerVector> currentVector,
|
||||
const char* name, const HAL_Value* value) {
|
||||
// Return if no callbacks are assigned
|
||||
@@ -42,3 +57,29 @@ void InvokeCallback(std::shared_ptr<NotifyListenerVector> currentVector,
|
||||
listener.callback(name, listener.param, value);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<BufferListenerVector> RegisterCallback(
|
||||
std::shared_ptr<BufferListenerVector> currentVector, const char* name,
|
||||
HAL_BufferCallback callback, void* param, int32_t* newUid) {
|
||||
return RegisterCallbackImpl<BufferListenerVector, HAL_BufferCallback>(
|
||||
currentVector, name, callback, param, newUid);
|
||||
}
|
||||
|
||||
std::shared_ptr<BufferListenerVector> CancelCallback(
|
||||
std::shared_ptr<BufferListenerVector> currentVector, int32_t uid) {
|
||||
return CancelCallbackImpl<BufferListenerVector, HAL_BufferCallback>(
|
||||
currentVector, uid);
|
||||
}
|
||||
|
||||
void InvokeCallback(std::shared_ptr<BufferListenerVector> currentVector,
|
||||
const char* name, uint8_t* buffer, int32_t count) {
|
||||
// Return if no callbacks are assigned
|
||||
if (currentVector == nullptr) return;
|
||||
// Get a copy of the shared_ptr, then iterate and callback listeners
|
||||
auto newCallbacks = currentVector;
|
||||
for (size_t i = 0; i < newCallbacks->size(); ++i) {
|
||||
if (!(*newCallbacks)[i]) continue; // removed
|
||||
auto listener = (*newCallbacks)[i];
|
||||
listener.callback(name, listener.param, buffer, count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,3 +20,13 @@ std::shared_ptr<hal::NotifyListenerVector> CancelCallback(
|
||||
|
||||
void InvokeCallback(std::shared_ptr<hal::NotifyListenerVector> currentVector,
|
||||
const char* name, const HAL_Value* value);
|
||||
|
||||
std::shared_ptr<hal::BufferListenerVector> RegisterCallback(
|
||||
std::shared_ptr<hal::BufferListenerVector> currentVector, const char* name,
|
||||
HAL_BufferCallback callback, void* param, int32_t* newUid);
|
||||
|
||||
std::shared_ptr<hal::BufferListenerVector> CancelCallback(
|
||||
std::shared_ptr<hal::BufferListenerVector> currentVector, int32_t uid);
|
||||
|
||||
void InvokeCallback(std::shared_ptr<hal::BufferListenerVector> currentVector,
|
||||
const char* name, uint8_t* buffer, int32_t count);
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "MockData/NotifyListenerVector.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
NotifyListenerVector::NotifyListenerVector(const NotifyListenerVector* copyFrom,
|
||||
const private_init&)
|
||||
: m_vector(copyFrom->m_vector), m_free(copyFrom->m_free) {}
|
||||
|
||||
NotifyListenerVector::NotifyListenerVector(void* param,
|
||||
HAL_NotifyCallback callback,
|
||||
unsigned int* newUid) {
|
||||
*newUid = emplace_back_impl(param, callback);
|
||||
}
|
||||
|
||||
std::shared_ptr<NotifyListenerVector> NotifyListenerVector::emplace_back(
|
||||
void* param, HAL_NotifyCallback callback, unsigned int* newUid) {
|
||||
auto newVector = std::make_shared<NotifyListenerVector>(this, private_init());
|
||||
newVector->m_vector = m_vector;
|
||||
newVector->m_free = m_free;
|
||||
*newUid = newVector->emplace_back_impl(param, callback);
|
||||
return newVector;
|
||||
}
|
||||
|
||||
std::shared_ptr<NotifyListenerVector> NotifyListenerVector::erase(
|
||||
unsigned int uid) {
|
||||
auto newVector = std::make_shared<NotifyListenerVector>(this, private_init());
|
||||
newVector->m_vector = m_vector;
|
||||
newVector->m_free = m_free;
|
||||
newVector->erase_impl(uid);
|
||||
return newVector;
|
||||
}
|
||||
|
||||
unsigned int NotifyListenerVector::emplace_back_impl(
|
||||
void* param, HAL_NotifyCallback callback) {
|
||||
unsigned int uid;
|
||||
if (m_free.empty()) {
|
||||
uid = m_vector.size();
|
||||
m_vector.emplace_back(param, callback);
|
||||
} else {
|
||||
uid = m_free.back();
|
||||
m_free.pop_back();
|
||||
m_vector[uid] = NotifyListener(param, callback);
|
||||
}
|
||||
return uid + 1;
|
||||
}
|
||||
|
||||
void NotifyListenerVector::erase_impl(unsigned int uid) {
|
||||
--uid;
|
||||
if (uid >= m_vector.size() || !m_vector[uid]) return;
|
||||
m_free.push_back(uid);
|
||||
m_vector[uid] = NotifyListener();
|
||||
}
|
||||
247
hal/src/main/native/sim/MockData/SPIData.cpp
Normal file
247
hal/src/main/native/sim/MockData/SPIData.cpp
Normal file
@@ -0,0 +1,247 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "../PortsInternal.h"
|
||||
#include "NotifyCallbackHelpers.h"
|
||||
#include "SPIDataInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
SPIData hal::SimSPIData[5];
|
||||
|
||||
void SPIData::ResetData() {
|
||||
m_initialized = false;
|
||||
m_accumulatorValue = 0;
|
||||
m_initializedCallbacks = nullptr;
|
||||
m_readCallbacks = nullptr;
|
||||
m_writeCallbacks = nullptr;
|
||||
m_resetAccumulatorCallback = nullptr;
|
||||
m_setAccumulatorCallback = nullptr;
|
||||
}
|
||||
|
||||
SPIData::SPIData() {}
|
||||
SPIData::~SPIData() {}
|
||||
|
||||
int32_t SPIData::RegisterInitializedCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_registerMutex);
|
||||
m_initializedCallbacks = RegisterCallback(
|
||||
m_initializedCallbacks, "Initialized", callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeBoolean(GetInitialized());
|
||||
callback("Initialized", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void SPIData::CancelInitializedCallback(int32_t uid) {
|
||||
m_initializedCallbacks = CancelCallback(m_initializedCallbacks, uid);
|
||||
}
|
||||
|
||||
void SPIData::InvokeInitializedCallback(HAL_Value value) {
|
||||
InvokeCallback(m_initializedCallbacks, "Initialized", &value);
|
||||
}
|
||||
|
||||
HAL_Bool SPIData::GetInitialized() { return m_initialized; }
|
||||
|
||||
void SPIData::SetInitialized(HAL_Bool initialized) {
|
||||
HAL_Bool oldValue = m_initialized.exchange(initialized);
|
||||
if (oldValue != initialized) {
|
||||
InvokeInitializedCallback(MakeBoolean(initialized));
|
||||
}
|
||||
}
|
||||
|
||||
int32_t SPIData::RegisterReadCallback(HAL_BufferCallback callback,
|
||||
void* param) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_registerMutex);
|
||||
m_readCallbacks =
|
||||
RegisterCallback(m_readCallbacks, "Read", callback, param, &newUid);
|
||||
}
|
||||
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void SPIData::CancelReadCallback(int32_t uid) {
|
||||
m_readCallbacks = CancelCallback(m_readCallbacks, uid);
|
||||
}
|
||||
|
||||
int32_t SPIData::RegisterWriteCallback(HAL_BufferCallback callback,
|
||||
void* param) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_registerMutex);
|
||||
m_writeCallbacks =
|
||||
RegisterCallback(m_writeCallbacks, "Write", callback, param, &newUid);
|
||||
}
|
||||
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void SPIData::CancelWriteCallback(int32_t uid) {
|
||||
m_writeCallbacks = CancelCallback(m_writeCallbacks, uid);
|
||||
}
|
||||
|
||||
int32_t SPIData::RegisterResetAccumulatorCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_registerMutex);
|
||||
m_resetAccumulatorCallback =
|
||||
RegisterCallback(m_resetAccumulatorCallback, "ResetAccumulator",
|
||||
callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeBoolean(GetInitialized());
|
||||
callback("ResetAccumulator", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void SPIData::CancelResetAccumulatorCallback(int32_t uid) {
|
||||
m_resetAccumulatorCallback = CancelCallback(m_resetAccumulatorCallback, uid);
|
||||
}
|
||||
|
||||
int32_t SPIData::RegisterAccumulatorCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
int32_t newUid = 0;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_registerMutex);
|
||||
m_setAccumulatorCallback = RegisterCallback(
|
||||
m_setAccumulatorCallback, "SetAccumulator", callback, param, &newUid);
|
||||
}
|
||||
if (initialNotify) {
|
||||
// We know that the callback is not null because of earlier null check
|
||||
HAL_Value value = MakeInt(GetAccumulatorValue());
|
||||
callback("SetAccumulator", param, &value);
|
||||
}
|
||||
return newUid;
|
||||
}
|
||||
|
||||
void SPIData::CancelAccumulatorCallback(int32_t uid) {
|
||||
m_setAccumulatorCallback = CancelCallback(m_setAccumulatorCallback, uid);
|
||||
}
|
||||
|
||||
void SPIData::InvokeSetAccumulatorCallback(HAL_Value value) {
|
||||
InvokeCallback(m_setAccumulatorCallback, "SetAccumulator", &value);
|
||||
}
|
||||
|
||||
void SPIData::SetAccumulatorValue(int64_t value) {
|
||||
int64_t oldValue = m_accumulatorValue.exchange(value);
|
||||
if (oldValue != value) {
|
||||
InvokeSetAccumulatorCallback(MakeLong(value));
|
||||
}
|
||||
}
|
||||
|
||||
int64_t SPIData::GetAccumulatorValue() { return m_accumulatorValue; }
|
||||
|
||||
int32_t SPIData::Read(uint8_t* buffer, int32_t count) {
|
||||
std::lock_guard<std::mutex> lock(m_dataMutex);
|
||||
InvokeCallback(m_readCallbacks, "Read", buffer, count);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int32_t SPIData::Write(uint8_t* dataToSend, int32_t sendSize) {
|
||||
std::lock_guard<std::mutex> lock(m_dataMutex);
|
||||
InvokeCallback(m_writeCallbacks, "Write", dataToSend, sendSize);
|
||||
|
||||
return sendSize;
|
||||
}
|
||||
|
||||
int32_t SPIData::Transaction(uint8_t* dataToSend, uint8_t* dataReceived,
|
||||
int32_t size) {
|
||||
std::lock_guard<std::mutex> lock(m_dataMutex);
|
||||
return size;
|
||||
}
|
||||
|
||||
void SPIData::ResetAccumulator() {
|
||||
HAL_Value value = MakeInt(0);
|
||||
InvokeCallback(m_resetAccumulatorCallback, "ResetAccumulator", &value);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetSPIData(int32_t index) { SimSPIData[index].ResetData(); }
|
||||
|
||||
int32_t HALSIM_RegisterSPIInitializedCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
return SimSPIData[index].RegisterInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
|
||||
void HALSIM_CancelSPIInitializedCallback(int32_t index, int32_t uid) {
|
||||
SimSPIData[index].CancelInitializedCallback(uid);
|
||||
}
|
||||
|
||||
HAL_Bool HALSIM_GetSPIInitialized(int32_t index) {
|
||||
return SimSPIData[index].GetInitialized();
|
||||
}
|
||||
|
||||
void HALSIM_SetSPIInitialized(int32_t index, HAL_Bool initialized) {
|
||||
SimSPIData[index].SetInitialized(initialized);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterSPIReadCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param) {
|
||||
return SimSPIData[index].RegisterReadCallback(callback, param);
|
||||
}
|
||||
void HALSIM_CancelSPIReadCallback(int32_t index, int32_t uid) {
|
||||
SimSPIData[index].CancelReadCallback(uid);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterSPIWriteCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param) {
|
||||
return SimSPIData[index].RegisterWriteCallback(callback, param);
|
||||
}
|
||||
void HALSIM_CancelSPIWriteCallback(int32_t index, int32_t uid) {
|
||||
SimSPIData[index].CancelWriteCallback(uid);
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterSPIResetAccumulatorCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
return SimSPIData[index].RegisterResetAccumulatorCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
void HALSIM_CancelSPIResetAccumulatorCallback(int32_t index, int32_t uid) {
|
||||
SimSPIData[index].CancelResetAccumulatorCallback(uid);
|
||||
}
|
||||
|
||||
void HALSIM_SetSPISetAccumulatorValue(int32_t index, int64_t value) {
|
||||
SimSPIData[index].SetAccumulatorValue(value);
|
||||
}
|
||||
int64_t HALSIM_GetSPIGetAccumulatorValue(int32_t index) {
|
||||
return SimSPIData[index].GetAccumulatorValue();
|
||||
}
|
||||
|
||||
} // extern c
|
||||
66
hal/src/main/native/sim/MockData/SPIDataInternal.h
Normal file
66
hal/src/main/native/sim/MockData/SPIDataInternal.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
|
||||
#include "MockData/NotifyListenerVector.h"
|
||||
#include "MockData/SPIData.h"
|
||||
|
||||
namespace hal {
|
||||
class SPIData {
|
||||
public:
|
||||
SPIData();
|
||||
~SPIData();
|
||||
|
||||
int32_t RegisterInitializedCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelInitializedCallback(int32_t uid);
|
||||
void InvokeInitializedCallback(HAL_Value value);
|
||||
HAL_Bool GetInitialized();
|
||||
void SetInitialized(HAL_Bool initialized);
|
||||
|
||||
int32_t RegisterReadCallback(HAL_BufferCallback callback, void* param);
|
||||
void CancelReadCallback(int32_t uid);
|
||||
|
||||
int32_t RegisterWriteCallback(HAL_BufferCallback callback, void* param);
|
||||
void CancelWriteCallback(int32_t uid);
|
||||
|
||||
int32_t RegisterResetAccumulatorCallback(HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
void CancelResetAccumulatorCallback(int32_t uid);
|
||||
|
||||
int32_t RegisterAccumulatorCallback(HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void CancelAccumulatorCallback(int32_t uid);
|
||||
void InvokeSetAccumulatorCallback(HAL_Value value);
|
||||
void SetAccumulatorValue(int64_t value);
|
||||
int64_t GetAccumulatorValue();
|
||||
|
||||
int32_t Read(uint8_t* buffer, int32_t count);
|
||||
int32_t Write(uint8_t* dataToSend, int32_t sendSize);
|
||||
int32_t Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int32_t size);
|
||||
void ResetAccumulator();
|
||||
|
||||
void ResetData();
|
||||
|
||||
private:
|
||||
std::mutex m_registerMutex;
|
||||
std::mutex m_dataMutex;
|
||||
std::atomic<HAL_Bool> m_initialized{false};
|
||||
std::atomic<int64_t> m_accumulatorValue{false};
|
||||
std::shared_ptr<NotifyListenerVector> m_initializedCallbacks = nullptr;
|
||||
std::shared_ptr<BufferListenerVector> m_readCallbacks = nullptr;
|
||||
std::shared_ptr<BufferListenerVector> m_writeCallbacks = nullptr;
|
||||
std::shared_ptr<NotifyListenerVector> m_resetAccumulatorCallback = nullptr;
|
||||
std::shared_ptr<NotifyListenerVector> m_setAccumulatorCallback = nullptr;
|
||||
};
|
||||
extern SPIData SimSPIData[];
|
||||
} // namespace hal
|
||||
Reference in New Issue
Block a user