mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Compare commits
29 Commits
v2018.1.1-
...
v2018.1.1-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efc7770e9b | ||
|
|
21585f70a8 | ||
|
|
c33fca34e9 | ||
|
|
de95f08a10 | ||
|
|
90f99dc571 | ||
|
|
f34332643a | ||
|
|
4ab095e9c9 | ||
|
|
541753c814 | ||
|
|
f02bb058bd | ||
|
|
12e96c6f13 | ||
|
|
b65447b6f5 | ||
|
|
9945a5b3c6 | ||
|
|
ce4c9edd1f | ||
|
|
a6e6ae41b9 | ||
|
|
f4e2e41aaf | ||
|
|
1e528669ff | ||
|
|
a1ea448406 | ||
|
|
9dc1de1d09 | ||
|
|
0521d85048 | ||
|
|
3c842d8234 | ||
|
|
be77f9cb26 | ||
|
|
2fc60680f4 | ||
|
|
0291a95f68 | ||
|
|
66002d6cac | ||
|
|
a7e9ac1062 | ||
|
|
434d60592c | ||
|
|
ee20747255 | ||
|
|
f4779379c3 | ||
|
|
877a9eae1f |
@@ -72,7 +72,7 @@ PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 200
|
||||
PointerAlignment: Left
|
||||
ReflowComments: true
|
||||
SortIncludes: true
|
||||
SortIncludes: false
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
|
||||
24
LICENSE.txt
Normal file
24
LICENSE.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
Copyright (c) 2009-2017 FIRST
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the FIRST nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY FIRST AND CONTRIBUTORS``AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY NONINFRINGEMENT AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FIRST OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -18,7 +18,7 @@ plugins {
|
||||
id 'edu.wpi.first.wpilib.versioning.WPILibVersioningPlugin' version '2.0'
|
||||
}
|
||||
|
||||
ext.licenseFile = file("$rootDir/license.txt")
|
||||
ext.licenseFile = file("$rootDir/LICENSE.txt")
|
||||
|
||||
ext.getJNIHeadersClass = {
|
||||
return JNIHeaders
|
||||
@@ -174,6 +174,10 @@ subprojects {
|
||||
apply plugin: 'idea'
|
||||
apply plugin: 'checkstyle'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
checkstyle {
|
||||
toolVersion = "8.1"
|
||||
configFile = new File(rootDir, "styleguide/checkstyle.xml")
|
||||
|
||||
@@ -108,6 +108,11 @@ model {
|
||||
}
|
||||
}
|
||||
}
|
||||
binaries.all { binary ->
|
||||
if (binary.targetPlatform.operatingSystem.linux) {
|
||||
linker.args "-ldl"
|
||||
}
|
||||
}
|
||||
}
|
||||
if (project.hasProperty('buildHalStaticDeps')) {
|
||||
halSimStaticDeps(NativeLibrarySpec) {
|
||||
|
||||
@@ -46,4 +46,4 @@ void HAL_CAN_GetCANStatus(float* percentBusUtilization, uint32_t* busOffCount,
|
||||
percentBusUtilization, busOffCount, txFullCount, receiveErrorCount,
|
||||
transmitErrorCount, status);
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -35,11 +35,6 @@ using namespace hal;
|
||||
static std::unique_ptr<tGlobal> global;
|
||||
static std::unique_ptr<tSysWatchdog> watchdog;
|
||||
|
||||
static std::mutex timeMutex;
|
||||
static uint32_t timeEpoch = 0;
|
||||
static uint32_t prevFPGATime = 0;
|
||||
static HAL_NotifierHandle rolloverNotifier = 0;
|
||||
|
||||
using namespace hal;
|
||||
|
||||
extern "C" {
|
||||
@@ -224,14 +219,16 @@ uint64_t HAL_GetFPGATime(int32_t* status) {
|
||||
*status = NiFpga_Status_ResourceNotInitialized;
|
||||
return 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(timeMutex);
|
||||
uint32_t fpgaTime = global->readLocalTime(status);
|
||||
uint64_t upper1 = global->readLocalTimeUpper(status);
|
||||
uint32_t lower = global->readLocalTime(status);
|
||||
uint64_t upper2 = global->readLocalTimeUpper(status);
|
||||
if (*status != 0) return 0;
|
||||
// check for rollover
|
||||
if (fpgaTime < prevFPGATime) ++timeEpoch;
|
||||
prevFPGATime = fpgaTime;
|
||||
return static_cast<uint64_t>(timeEpoch) << 32 |
|
||||
static_cast<uint64_t>(fpgaTime);
|
||||
if (upper1 != upper2) {
|
||||
// Rolled over between the lower call, reread lower
|
||||
lower = global->readLocalTime(status);
|
||||
if (*status != 0) return 0;
|
||||
}
|
||||
return (upper2 << 32) + lower;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,12 +259,6 @@ HAL_Bool HAL_GetBrownedOut(int32_t* status) {
|
||||
return !(watchdog->readStatus_PowerAlive(status));
|
||||
}
|
||||
|
||||
static void timerRollover(uint64_t currentTime, HAL_NotifierHandle handle) {
|
||||
// reschedule timer for next rollover
|
||||
int32_t status = 0;
|
||||
HAL_UpdateNotifierAlarm(handle, currentTime + 0x80000000ULL, &status);
|
||||
}
|
||||
|
||||
void HAL_BaseInitialize(int32_t* status) {
|
||||
static std::atomic_bool initialized{false};
|
||||
static std::mutex initializeMutex;
|
||||
@@ -331,8 +322,18 @@ static bool killExistingProgram(int timeout, int mode) {
|
||||
* Call this to start up HAL. This is required for robot programs.
|
||||
*/
|
||||
HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
static std::atomic_bool initialized{false};
|
||||
static std::mutex initializeMutex;
|
||||
// Initial check, as if it's true initialization has finished
|
||||
if (initialized) return true;
|
||||
|
||||
std::lock_guard<std::mutex> lock(initializeMutex);
|
||||
// Second check in case another thread was waiting
|
||||
if (initialized) return true;
|
||||
|
||||
setlinebuf(stdin);
|
||||
setlinebuf(stdout);
|
||||
llvm::outs().SetUnbuffered();
|
||||
|
||||
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
||||
|
||||
@@ -351,25 +352,9 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
int32_t status = 0;
|
||||
HAL_BaseInitialize(&status);
|
||||
|
||||
if (!rolloverNotifier)
|
||||
rolloverNotifier = HAL_InitializeNotifierNonThreadedUnsafe(
|
||||
timerRollover, nullptr, &status);
|
||||
if (status == 0) {
|
||||
uint64_t curTime = HAL_GetFPGATime(&status);
|
||||
if (status == 0) {
|
||||
HAL_UpdateNotifierAlarm(rolloverNotifier, curTime + 0x80000000ULL,
|
||||
&status);
|
||||
} else {
|
||||
// return false if status failed.
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// return false if status failed.
|
||||
return false;
|
||||
}
|
||||
|
||||
HAL_InitializeDriverStation();
|
||||
|
||||
initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,20 +7,19 @@
|
||||
|
||||
#include "HAL/Notifier.h"
|
||||
|
||||
// For std::atexit()
|
||||
#include <cstdlib>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdlib> // For std::atexit()
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include <support/SafeThread.h>
|
||||
|
||||
#include "HAL/ChipObject.h"
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/HAL.h"
|
||||
#include "HAL/cpp/NotifierInternal.h"
|
||||
#include "HAL/cpp/make_unique.h"
|
||||
#include "HAL/handles/UnlimitedHandleResource.h"
|
||||
#include "support/SafeThread.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ void HAL_SetPWMConfig(HAL_DigitalHandle pwmPortHandle, double max,
|
||||
|
||||
// calculate the loop time in milliseconds
|
||||
double loopTime =
|
||||
HAL_GetLoopTiming(status) / (kSystemClockTicksPerMicrosecond * 1e3);
|
||||
HAL_GetPWMLoopTiming(status) / (kSystemClockTicksPerMicrosecond * 1e3);
|
||||
if (*status != 0) return;
|
||||
|
||||
int32_t maxPwm = static_cast<int32_t>((max - kDefaultPwmCenter) / loopTime +
|
||||
@@ -444,10 +444,30 @@ void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
|
||||
*
|
||||
* @return The loop time
|
||||
*/
|
||||
int32_t HAL_GetLoopTiming(int32_t* status) {
|
||||
int32_t HAL_GetPWMLoopTiming(int32_t* status) {
|
||||
initializeDigital(status);
|
||||
if (*status != 0) return 0;
|
||||
return pwmSystem->readLoopTiming(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pwm starting cycle time
|
||||
*
|
||||
* @return The pwm cycle start time.
|
||||
*/
|
||||
uint64_t HAL_GetPWMCycleStartTime(int32_t* status) {
|
||||
initializeDigital(status);
|
||||
if (*status != 0) return 0;
|
||||
uint64_t upper1 = pwmSystem->readCycleStartTimeUpper(status);
|
||||
uint32_t lower = pwmSystem->readCycleStartTime(status);
|
||||
uint64_t upper2 = pwmSystem->readCycleStartTimeUpper(status);
|
||||
if (*status != 0) return 0;
|
||||
if (upper1 != upper2) {
|
||||
// Rolled over between the lower call, reread lower
|
||||
lower = pwmSystem->readCycleStartTime(status);
|
||||
if (*status != 0) return 0;
|
||||
}
|
||||
return (upper2 << 32) + lower;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -226,7 +226,7 @@ void SerialHelper::QueryHubPaths(int32_t* status) {
|
||||
// Using std::string because this is guarenteed to be large
|
||||
std::string output = "";
|
||||
|
||||
std::shared_ptr<FILE> pipe(popen(val.c_str(), "r"), pclose);
|
||||
std::shared_ptr<std::FILE> pipe(popen(val.c_str(), "r"), pclose);
|
||||
// Just check the next item on a pipe failure
|
||||
if (!pipe) continue;
|
||||
while (!feof(pipe.get())) {
|
||||
|
||||
@@ -24,5 +24,5 @@ double HAL_GetAccelerometerX(void);
|
||||
double HAL_GetAccelerometerY(void);
|
||||
double HAL_GetAccelerometerZ(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -32,5 +32,5 @@ int64_t HAL_GetAccumulatorCount(HAL_AnalogInputHandle analogPortHandle,
|
||||
void HAL_GetAccumulatorOutput(HAL_AnalogInputHandle analogPortHandle,
|
||||
int64_t* value, int64_t* count, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -34,5 +34,5 @@ double HAL_GetAnalogGyroRate(HAL_GyroHandle handle, int32_t* status);
|
||||
double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status);
|
||||
int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -46,5 +46,5 @@ int32_t HAL_GetAnalogLSBWeight(HAL_AnalogInputHandle analogPortHandle,
|
||||
int32_t HAL_GetAnalogOffset(HAL_AnalogInputHandle analogPortHandle,
|
||||
int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -24,5 +24,5 @@ double HAL_GetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle,
|
||||
int32_t* status);
|
||||
HAL_Bool HAL_CheckAnalogOutputChannel(int32_t channel);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -43,5 +43,5 @@ HAL_Bool HAL_GetAnalogTriggerOutput(HAL_AnalogTriggerHandle analogTriggerHandle,
|
||||
HAL_AnalogTriggerType type,
|
||||
int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -56,5 +56,5 @@ void HAL_CAN_GetCANStatus(float* percentBusUtilization, uint32_t* busOffCount,
|
||||
uint32_t* transmitErrorCount, int32_t* status);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -44,5 +44,5 @@ HAL_Bool HAL_GetCompressorNotConnectedStickyFault(
|
||||
HAL_Bool HAL_GetCompressorNotConnectedFault(
|
||||
HAL_CompressorHandle compressorHandle, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -15,5 +15,5 @@ extern "C" {
|
||||
|
||||
int32_t HAL_GetSystemClockTicksPerMicrosecond(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -69,5 +69,5 @@ HAL_Bool HAL_GetCounterDirection(HAL_CounterHandle counterHandle,
|
||||
void HAL_SetCounterReverseDirection(HAL_CounterHandle counterHandle,
|
||||
HAL_Bool reverseDirection, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -41,5 +41,5 @@ int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status);
|
||||
void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status);
|
||||
int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -75,5 +75,5 @@ double HAL_GetEncoderDistancePerPulse(HAL_EncoderHandle encoderHandle,
|
||||
HAL_EncoderEncodingType HAL_GetEncoderEncodingType(
|
||||
HAL_EncoderHandle encoderHandle, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
25
hal/src/main/native/include/HAL/Extensions.h
Normal file
25
hal/src/main/native/include/HAL/Extensions.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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
|
||||
|
||||
/**
|
||||
* HAL Simulator Extensions are libraries that provide additional simulator
|
||||
* functionality, such as a Gazebo interface, or a more light weight simulation.
|
||||
*
|
||||
* An extension must expose the HALSIM_InitExtension entry point which is
|
||||
* invoked after the library is loaded.
|
||||
*
|
||||
* The entry point is expected to return < 0 for errors that should stop
|
||||
* the HAL completely, 0 for success, and > 0 for a non fatal error.
|
||||
*/
|
||||
typedef int halsim_extension_init_func_t(void);
|
||||
|
||||
extern "C" {
|
||||
int HAL_LoadOneExtension(const char* library);
|
||||
int HAL_LoadExtensions(void);
|
||||
} // extern "C"
|
||||
@@ -82,5 +82,5 @@ int64_t HAL_Report(int32_t resource, int32_t instanceNumber, int32_t context,
|
||||
|
||||
#endif // HAL_USE_LABVIEW
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -25,5 +25,5 @@ int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
|
||||
int32_t count);
|
||||
void HAL_CloseI2C(HAL_I2CPort port);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -46,5 +46,5 @@ void HAL_SetInterruptUpSourceEdge(HAL_InterruptHandle interruptHandle,
|
||||
HAL_Bool risingEdge, HAL_Bool fallingEdge,
|
||||
int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -26,5 +26,5 @@ void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle,
|
||||
uint64_t triggerTime, int32_t* status);
|
||||
void HAL_StopNotifierAlarm(HAL_NotifierHandle notifierHandle, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -46,5 +46,5 @@ void HAL_FlushOSSerial(HAL_SerialPort port, int32_t* status);
|
||||
void HAL_ClearOSSerial(HAL_SerialPort port, int32_t* status);
|
||||
void HAL_CloseOSSerial(HAL_SerialPort port, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -28,5 +28,5 @@ double HAL_GetPDPTotalEnergy(int32_t module, int32_t* status);
|
||||
void HAL_ResetPDPTotalEnergy(int32_t module, int32_t* status);
|
||||
void HAL_ClearPDPStickyFaults(int32_t module, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -49,7 +49,8 @@ double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status);
|
||||
void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status);
|
||||
void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
|
||||
int32_t* status);
|
||||
int32_t HAL_GetLoopTiming(int32_t* status);
|
||||
int32_t HAL_GetPWMLoopTiming(int32_t* status);
|
||||
uint64_t HAL_GetPWMCycleStartTime(int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -32,5 +32,5 @@ int32_t HAL_GetNumSolenoidChannels(void);
|
||||
int32_t HAL_GetNumPDPModules(void);
|
||||
int32_t HAL_GetNumPDPChannels(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -30,5 +30,5 @@ double HAL_GetUserCurrent3V3(int32_t* status);
|
||||
HAL_Bool HAL_GetUserActive3V3(int32_t* status);
|
||||
int32_t HAL_GetUserCurrentFaults3V3(int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -25,5 +25,5 @@ void HAL_SetRelay(HAL_RelayHandle relayPortHandle, HAL_Bool on,
|
||||
int32_t* status);
|
||||
HAL_Bool HAL_GetRelay(HAL_RelayHandle relayPortHandle, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -55,5 +55,5 @@ double HAL_GetSPIAccumulatorAverage(HAL_SPIPort port, int32_t* status);
|
||||
void HAL_GetSPIAccumulatorOutput(HAL_SPIPort port, int64_t* value,
|
||||
int64_t* count, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -46,5 +46,5 @@ void HAL_FlushSerial(HAL_SerialPort port, int32_t* status);
|
||||
void HAL_ClearSerial(HAL_SerialPort port, int32_t* status);
|
||||
void HAL_CloseSerial(HAL_SerialPort port, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -31,5 +31,5 @@ HAL_Bool HAL_GetPCMSolenoidVoltageStickyFault(int32_t module, int32_t* status);
|
||||
HAL_Bool HAL_GetPCMSolenoidVoltageFault(int32_t module, int32_t* status);
|
||||
void HAL_ClearAllPCMStickyFaults(int32_t module, int32_t* status);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -25,4 +25,4 @@ HAL_Bool HAL_SetThreadPriority(NativeThreadHandle handle, HAL_Bool realTime,
|
||||
int32_t priority, int32_t* status);
|
||||
HAL_Bool HAL_SetCurrentThreadPriority(HAL_Bool realTime, int32_t priority,
|
||||
int32_t* status);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -82,6 +82,7 @@ namespace nUsageReporting
|
||||
kResourceType_PDP,
|
||||
kResourceType_PCM, // 60
|
||||
kResourceType_PigeonIMU,
|
||||
kResourceType_NidecBrushless,
|
||||
} tResourceType;
|
||||
|
||||
typedef enum
|
||||
|
||||
@@ -12,4 +12,4 @@
|
||||
extern "C" {
|
||||
HAL_NotifierHandle HAL_InitializeNotifierNonThreadedUnsafe(
|
||||
HAL_NotifierProcessFunction process, void* param, int32_t* status);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -55,6 +55,11 @@ void HALSIM_CancelAccelerometerZCallback(int32_t index, int32_t uid);
|
||||
double HALSIM_GetAccelerometerZ(int32_t index);
|
||||
void HALSIM_SetAccelerometerZ(int32_t index, double z);
|
||||
|
||||
void HALSIM_RegisterAccelerometerAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -38,6 +38,10 @@ void HALSIM_CancelAnalogGyroInitializedCallback(int32_t index, int32_t uid);
|
||||
HAL_Bool HALSIM_GetAnalogGyroInitialized(int32_t index);
|
||||
void HALSIM_SetAnalogGyroInitialized(int32_t index, HAL_Bool initialized);
|
||||
|
||||
void HALSIM_RegisterAnalogGyroAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -88,6 +88,10 @@ int32_t HALSIM_GetAnalogInAccumulatorDeadband(int32_t index);
|
||||
void HALSIM_SetAnalogInAccumulatorDeadband(int32_t index,
|
||||
int32_t accumulatorDeadband);
|
||||
|
||||
void HALSIM_RegisterAnalogInAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -31,6 +31,10 @@ void HALSIM_CancelAnalogOutInitializedCallback(int32_t index, int32_t uid);
|
||||
HAL_Bool HALSIM_GetAnalogOutInitialized(int32_t index);
|
||||
void HALSIM_SetAnalogOutInitialized(int32_t index, HAL_Bool initialized);
|
||||
|
||||
void HALSIM_RegisterAnalogOutAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -54,6 +54,11 @@ HALSIM_AnalogTriggerMode HALSIM_GetAnalogTriggerTriggerMode(int32_t index);
|
||||
void HALSIM_SetAnalogTriggerTriggerMode(int32_t index,
|
||||
HALSIM_AnalogTriggerMode triggerMode);
|
||||
|
||||
void HALSIM_RegisterAnalogTriggerAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -53,6 +53,9 @@ void HALSIM_CancelDIOFilterIndexCallback(int32_t index, int32_t uid);
|
||||
int32_t HALSIM_GetDIOFilterIndex(int32_t index);
|
||||
void HALSIM_SetDIOFilterIndex(int32_t index, int32_t filterIndex);
|
||||
|
||||
void HALSIM_RegisterDIOAllCallbacks(int32_t index, HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -38,6 +38,10 @@ void HALSIM_CancelDigitalPWMPinCallback(int32_t index, int32_t uid);
|
||||
int32_t HALSIM_GetDigitalPWMPin(int32_t index);
|
||||
void HALSIM_SetDigitalPWMPin(int32_t index, int32_t pin);
|
||||
|
||||
void HALSIM_RegisterDigitalPWMAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -69,6 +69,10 @@ void HALSIM_SetDriverStationMatchTime(double matchTime);
|
||||
|
||||
void HALSIM_NotifyDriverStationNewData(void);
|
||||
|
||||
void HALSIM_RegisterDriverStationAllCallbacks(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -78,6 +78,10 @@ void HALSIM_CancelEncoderSamplesToAverageCallback(int32_t index, int32_t uid);
|
||||
int32_t HALSIM_GetEncoderSamplesToAverage(int32_t index);
|
||||
void HALSIM_SetEncoderSamplesToAverage(int32_t index, int32_t samplesToAverage);
|
||||
|
||||
void HALSIM_RegisterEncoderAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
39
hal/src/main/native/include/MockData/I2CData.h
Normal file
39
hal/src/main/native/include/MockData/I2CData.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "HAL/HAL.h"
|
||||
#include "NotifyListener.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void HALSIM_ResetI2CData(int32_t index);
|
||||
|
||||
int32_t HALSIM_RegisterI2CInitializedCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void HALSIM_CancelI2CInitializedCallback(int32_t index, int32_t uid);
|
||||
HAL_Bool HALSIM_GetI2CInitialized(int32_t index);
|
||||
void HALSIM_SetI2CInitialized(int32_t index, HAL_Bool initialized);
|
||||
|
||||
int32_t HALSIM_RegisterI2CReadCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param);
|
||||
void HALSIM_CancelI2CReadCallback(int32_t index, int32_t uid);
|
||||
|
||||
int32_t HALSIM_RegisterI2CWriteCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param);
|
||||
void HALSIM_CancelI2CWriteCallback(int32_t index, int32_t uid);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
@@ -11,4 +11,4 @@ extern "C" {
|
||||
void HALSIM_WaitForProgramStart(void);
|
||||
void HALSIM_SetProgramStarted(void);
|
||||
void HALSIM_RestartTiming(void);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -12,15 +12,21 @@
|
||||
typedef void (*HAL_NotifyCallback)(const char* name, void* param,
|
||||
const struct HAL_Value* value);
|
||||
|
||||
typedef void (*HAL_BufferCallback)(const char* name, void* param,
|
||||
unsigned char* buffer, unsigned int count);
|
||||
|
||||
namespace hal {
|
||||
struct NotifyListener {
|
||||
NotifyListener() = default;
|
||||
NotifyListener(void* param_, HAL_NotifyCallback callback_)
|
||||
|
||||
template <typename CallbackFunction>
|
||||
struct HalCallbackListener {
|
||||
HalCallbackListener() = default;
|
||||
HalCallbackListener(void* param_, CallbackFunction callback_)
|
||||
: callback(callback_), param(param_) {}
|
||||
|
||||
explicit operator bool() const { return callback != nullptr; }
|
||||
|
||||
HAL_NotifyCallback callback;
|
||||
CallbackFunction callback;
|
||||
void* param;
|
||||
};
|
||||
|
||||
} // namespace hal
|
||||
|
||||
@@ -17,52 +17,121 @@
|
||||
namespace hal {
|
||||
// Vector which provides an integrated freelist for removal and reuse of
|
||||
// individual elements.
|
||||
class NotifyListenerVector {
|
||||
|
||||
template <typename ListenerType>
|
||||
class HalCallbackListenerVectorImpl {
|
||||
struct private_init {};
|
||||
|
||||
public:
|
||||
typedef typename std::vector<NotifyListener>::size_type size_type;
|
||||
typedef typename std::vector<HalCallbackListener<ListenerType>>::size_type
|
||||
size_type;
|
||||
|
||||
// Constructor for creating copies of the vector
|
||||
NotifyListenerVector(const NotifyListenerVector*, const private_init&);
|
||||
HalCallbackListenerVectorImpl(const HalCallbackListenerVectorImpl* copyFrom,
|
||||
const private_init&);
|
||||
|
||||
// Delete all default constructors so they cannot be used
|
||||
NotifyListenerVector& operator=(const NotifyListenerVector&) = delete;
|
||||
NotifyListenerVector() = delete;
|
||||
NotifyListenerVector(const NotifyListenerVector&) = delete;
|
||||
HalCallbackListenerVectorImpl& operator=(
|
||||
const HalCallbackListenerVectorImpl&) = delete;
|
||||
HalCallbackListenerVectorImpl() = delete;
|
||||
HalCallbackListenerVectorImpl(const HalCallbackListenerVectorImpl&) = delete;
|
||||
|
||||
// Create a new vector with a single callback inside of it
|
||||
NotifyListenerVector(void* param, HAL_NotifyCallback callback,
|
||||
unsigned int* newUid);
|
||||
HalCallbackListenerVectorImpl(void* param, ListenerType callback,
|
||||
unsigned int* newUid) {
|
||||
*newUid = emplace_back_impl(param, callback);
|
||||
}
|
||||
|
||||
size_type size() const { return m_vector.size(); }
|
||||
NotifyListener& operator[](size_type i) { return m_vector[i]; }
|
||||
const NotifyListener& operator[](size_type i) const { return m_vector[i]; }
|
||||
HalCallbackListener<ListenerType>& operator[](size_type i) {
|
||||
return m_vector[i];
|
||||
}
|
||||
const HalCallbackListener<ListenerType>& operator[](size_type i) const {
|
||||
return m_vector[i];
|
||||
}
|
||||
|
||||
// Add a new NotifyListener to a copy of the vector. If there are elements on
|
||||
// the freelist,
|
||||
// reuses the last one; otherwise adds to the end of the vector.
|
||||
// Returns the resulting element index (+1).
|
||||
std::shared_ptr<NotifyListenerVector> emplace_back(
|
||||
void* param, HAL_NotifyCallback callback, unsigned int* newUid);
|
||||
std::shared_ptr<HalCallbackListenerVectorImpl<ListenerType>> emplace_back(
|
||||
void* param, ListenerType callback, unsigned int* newUid);
|
||||
|
||||
// Removes the identified element by replacing it with a default-constructed
|
||||
// one. The element is added to the freelist for later reuse. Returns a copy
|
||||
std::shared_ptr<NotifyListenerVector> erase(unsigned int uid);
|
||||
std::shared_ptr<HalCallbackListenerVectorImpl<ListenerType>> erase(
|
||||
unsigned int uid);
|
||||
|
||||
private:
|
||||
std::vector<NotifyListener> m_vector;
|
||||
std::vector<HalCallbackListener<ListenerType>> m_vector;
|
||||
std::vector<unsigned int> m_free;
|
||||
|
||||
// Add a new NotifyListener to the vector. If there are elements on the
|
||||
// freelist,
|
||||
// reuses the last one; otherwise adds to the end of the vector.
|
||||
// Returns the resulting element index (+1).
|
||||
unsigned int emplace_back_impl(void* param, HAL_NotifyCallback callback);
|
||||
unsigned int emplace_back_impl(void* param, ListenerType callback);
|
||||
|
||||
// Removes the identified element by replacing it with a default-constructed
|
||||
// one. The element is added to the freelist for later reuse.
|
||||
void erase_impl(unsigned int uid);
|
||||
};
|
||||
|
||||
template <typename ListenerType>
|
||||
HalCallbackListenerVectorImpl<ListenerType>::HalCallbackListenerVectorImpl(
|
||||
const HalCallbackListenerVectorImpl<ListenerType>* copyFrom,
|
||||
const private_init&)
|
||||
: m_vector(copyFrom->m_vector), m_free(copyFrom->m_free) {}
|
||||
|
||||
template <typename ListenerType>
|
||||
std::shared_ptr<HalCallbackListenerVectorImpl<ListenerType>>
|
||||
HalCallbackListenerVectorImpl<ListenerType>::emplace_back(
|
||||
void* param, ListenerType callback, unsigned int* newUid) {
|
||||
auto newVector =
|
||||
std::make_shared<HalCallbackListenerVectorImpl<ListenerType>>(
|
||||
this, private_init());
|
||||
newVector->m_vector = m_vector;
|
||||
newVector->m_free = m_free;
|
||||
*newUid = newVector->emplace_back_impl(param, callback);
|
||||
return newVector;
|
||||
}
|
||||
|
||||
template <typename ListenerType>
|
||||
std::shared_ptr<HalCallbackListenerVectorImpl<ListenerType>>
|
||||
HalCallbackListenerVectorImpl<ListenerType>::erase(unsigned int uid) {
|
||||
auto newVector =
|
||||
std::make_shared<HalCallbackListenerVectorImpl<ListenerType>>(
|
||||
this, private_init());
|
||||
newVector->m_vector = m_vector;
|
||||
newVector->m_free = m_free;
|
||||
newVector->erase_impl(uid);
|
||||
return newVector;
|
||||
}
|
||||
|
||||
template <typename ListenerType>
|
||||
unsigned int HalCallbackListenerVectorImpl<ListenerType>::emplace_back_impl(
|
||||
void* param, ListenerType 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] = HalCallbackListener<ListenerType>(param, callback);
|
||||
}
|
||||
return uid + 1;
|
||||
}
|
||||
|
||||
template <typename ListenerType>
|
||||
void HalCallbackListenerVectorImpl<ListenerType>::erase_impl(unsigned int uid) {
|
||||
--uid;
|
||||
if (uid >= m_vector.size() || !m_vector[uid]) return;
|
||||
m_free.push_back(uid);
|
||||
m_vector[uid] = HalCallbackListener<ListenerType>();
|
||||
}
|
||||
|
||||
typedef HalCallbackListenerVectorImpl<HAL_NotifyCallback> NotifyListenerVector;
|
||||
typedef HalCallbackListenerVectorImpl<HAL_BufferCallback> BufferListenerVector;
|
||||
|
||||
} // namespace hal
|
||||
|
||||
@@ -74,6 +74,16 @@ void HALSIM_CancelPCMCompressorCurrentCallback(int32_t index, int32_t uid);
|
||||
double HALSIM_GetPCMCompressorCurrent(int32_t index);
|
||||
void HALSIM_SetPCMCompressorCurrent(int32_t index, double compressorCurrent);
|
||||
|
||||
void HALSIM_RegisterPCMAllNonSolenoidCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
|
||||
void HALSIM_RegisterPCMAllSolenoidCallbacks(int32_t index, int32_t channel,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -46,6 +46,11 @@ void HALSIM_CancelPDPCurrentCallback(int32_t index, int32_t channel,
|
||||
double HALSIM_GetPDPCurrent(int32_t index, int32_t channel);
|
||||
void HALSIM_SetPDPCurrent(int32_t index, int32_t channel, double current);
|
||||
|
||||
void HALSIM_RegisterPDPAllNonCurrentCallbacks(int32_t index, int32_t channel,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -60,6 +60,9 @@ void HALSIM_CancelPWMZeroLatchCallback(int32_t index, int32_t uid);
|
||||
HAL_Bool HALSIM_GetPWMZeroLatch(int32_t index);
|
||||
void HALSIM_SetPWMZeroLatch(int32_t index, HAL_Bool zeroLatch);
|
||||
|
||||
void HALSIM_RegisterPWMAllCallbacks(int32_t index, HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -47,6 +47,10 @@ void HALSIM_CancelRelayReverseCallback(int32_t index, int32_t uid);
|
||||
HAL_Bool HALSIM_GetRelayReverse(int32_t index);
|
||||
void HALSIM_SetRelayReverse(int32_t index, HAL_Bool reverse);
|
||||
|
||||
void HALSIM_RegisterRelayAllCallcbaks(int32_t index,
|
||||
HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -133,6 +133,10 @@ void HALSIM_CancelRoboRioUserFaults3V3Callback(int32_t index, int32_t uid);
|
||||
int32_t HALSIM_GetRoboRioUserFaults3V3(int32_t index);
|
||||
void HALSIM_SetRoboRioUserFaults3V3(int32_t index, int32_t userFaults3V3);
|
||||
|
||||
void HALSIM_RegisterRoboRioAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
@@ -53,6 +53,11 @@ void HALSIM_CancelSPIAccelerometerZCallback(int32_t index, int32_t uid);
|
||||
double HALSIM_GetSPIAccelerometerZ(int32_t index);
|
||||
void HALSIM_SetSPIAccelerometerZ(int32_t index, double z);
|
||||
|
||||
void HALSIM_RegisterSPIAccelerometerAllCallbcaks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
52
hal/src/main/native/include/MockData/SPIData.h
Normal file
52
hal/src/main/native/include/MockData/SPIData.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "HAL/HAL.h"
|
||||
#include "NotifyListener.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void HALSIM_ResetSPIData(int32_t index);
|
||||
|
||||
int32_t HALSIM_RegisterSPIInitializedCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void HALSIM_CancelSPIInitializedCallback(int32_t index, int32_t uid);
|
||||
HAL_Bool HALSIM_GetSPIInitialized(int32_t index);
|
||||
void HALSIM_SetSPIInitialized(int32_t index, HAL_Bool initialized);
|
||||
|
||||
int32_t HALSIM_RegisterSPIReadCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param);
|
||||
void HALSIM_CancelSPIReadCallback(int32_t index, int32_t uid);
|
||||
|
||||
int32_t HALSIM_RegisterSPIWriteCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param);
|
||||
void HALSIM_CancelSPIWriteCallback(int32_t index, int32_t uid);
|
||||
|
||||
int32_t HALSIM_RegisterSPIResetAccumulatorCallback(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void HALSIM_CancelSPIResetAccumulatorCallback(int32_t index, int32_t uid);
|
||||
|
||||
int32_t HALSIM_RegisterSPISetAccumulatorCallback(int32_t index,
|
||||
HAL_BufferCallback callback,
|
||||
void* param);
|
||||
void HALSIM_CancelSPISetAccumulatorCallback(int32_t index, int32_t uid);
|
||||
void HALSIM_SetSPISetAccumulatorValue(int32_t index, int64_t value);
|
||||
int64_t HALSIM_GetSPIGetAccumulatorValue(int32_t index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
@@ -22,4 +22,4 @@ void HAL_SetAccelerometerRange(HAL_AccelerometerRange range) {
|
||||
double HAL_GetAccelerometerX(void) { return SimAccelerometerData[0].GetX(); }
|
||||
double HAL_GetAccelerometerY(void) { return SimAccelerometerData[0].GetY(); }
|
||||
double HAL_GetAccelerometerZ(void) { return SimAccelerometerData[0].GetZ(); }
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -103,4 +103,4 @@ void HAL_GetAccumulatorOutput(HAL_AnalogInputHandle analogPortHandle,
|
||||
*count = SimAnalogInData[port->channel].GetAccumulatorCount();
|
||||
*value = SimAnalogInData[port->channel].GetAccumulatorValue();
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -21,7 +21,7 @@ struct AnalogGyro {
|
||||
HAL_AnalogInputHandle handle;
|
||||
uint8_t index;
|
||||
};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
using namespace hal;
|
||||
|
||||
@@ -132,4 +132,4 @@ double HAL_GetAnalogGyroOffset(HAL_GyroHandle handle, int32_t* status) {
|
||||
int32_t HAL_GetAnalogGyroCenter(HAL_GyroHandle handle, int32_t* status) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -14,4 +14,4 @@ namespace hal {
|
||||
IndexedHandleResource<HAL_AnalogInputHandle, hal::AnalogPort, kNumAnalogInputs,
|
||||
HAL_HandleEnum::AnalogInput>
|
||||
analogInputHandles;
|
||||
}
|
||||
} // namespace hal
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace {
|
||||
struct AnalogOutput {
|
||||
uint8_t channel;
|
||||
};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
static IndexedHandleResource<HAL_AnalogOutputHandle, AnalogOutput,
|
||||
kNumAnalogOutputs, HAL_HandleEnum::AnalogOutput>
|
||||
@@ -85,4 +85,4 @@ double HAL_GetAnalogOutput(HAL_AnalogOutputHandle analogOutputHandle,
|
||||
|
||||
return SimAnalogOutData[port->channel].GetVoltage();
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -22,7 +22,7 @@ struct AnalogTrigger {
|
||||
uint8_t index;
|
||||
HAL_Bool trigState;
|
||||
};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
using namespace hal;
|
||||
|
||||
|
||||
@@ -25,4 +25,4 @@ void HAL_CAN_ReadStreamSession(uint32_t sessionHandle,
|
||||
void HAL_CAN_GetCANStatus(float* percentBusUtilization, uint32_t* busOffCount,
|
||||
uint32_t* txFullCount, uint32_t* receiveErrorCount,
|
||||
uint32_t* transmitErrorCount, int32_t* status) {}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -15,4 +15,4 @@ extern "C" {
|
||||
int32_t HAL_GetSystemClockTicksPerMicrosecond(void) {
|
||||
return kSystemClockTicksPerMicrosecond;
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -11,4 +11,4 @@
|
||||
|
||||
namespace hal {
|
||||
constexpr int32_t kSystemClockTicksPerMicrosecond = 40;
|
||||
}
|
||||
} // namespace hal
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace hal {
|
||||
LimitedHandleResource<HAL_CounterHandle, Counter, kNumCounters,
|
||||
HAL_HandleEnum::Counter>
|
||||
counterHandles;
|
||||
}
|
||||
} // namespace hal
|
||||
|
||||
extern "C" {
|
||||
HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t* index,
|
||||
@@ -83,4 +83,4 @@ HAL_Bool HAL_GetCounterDirection(HAL_CounterHandle counterHandle,
|
||||
void HAL_SetCounterReverseDirection(HAL_CounterHandle counterHandle,
|
||||
HAL_Bool reverseDirection,
|
||||
int32_t* status) {}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -333,4 +333,4 @@ void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) {
|
||||
int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status) {
|
||||
return 0; // TODO(Thad) figure this out
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "MockData/DriverStationDataInternal.h"
|
||||
#include "MockData/MockHooks.h"
|
||||
|
||||
static std::mutex msgMutex;
|
||||
static std::condition_variable newDSDataAvailableCond;
|
||||
@@ -152,9 +153,7 @@ double HAL_GetMatchTime(int32_t* status) {
|
||||
return SimDriverStationData.GetMatchTime();
|
||||
}
|
||||
|
||||
void HAL_ObserveUserProgramStarting(void) {
|
||||
// TODO
|
||||
}
|
||||
void HAL_ObserveUserProgramStarting(void) { HALSIM_SetProgramStarted(); }
|
||||
|
||||
void HAL_ObserveUserProgramDisabled(void) {
|
||||
// TODO
|
||||
|
||||
@@ -25,7 +25,7 @@ struct Encoder {
|
||||
uint8_t index;
|
||||
};
|
||||
struct Empty {};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
static LimitedHandleResource<HAL_EncoderHandle, Encoder,
|
||||
kNumEncoders + kNumCounters,
|
||||
|
||||
83
hal/src/main/native/sim/Extensions.cpp
Normal file
83
hal/src/main/native/sim/Extensions.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "HAL/Extensions.h"
|
||||
|
||||
#include <llvm/SmallString.h>
|
||||
#include <llvm/StringRef.h>
|
||||
|
||||
#include "HAL/HAL.h"
|
||||
|
||||
#if defined(WIN32) || defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
#if defined(WIN32) || defined(_WIN32)
|
||||
#define DELIM ';'
|
||||
#define HTYPE HMODULE
|
||||
#define DLOPEN(a) LoadLibrary(a)
|
||||
#define DLSYM GetProcAddress
|
||||
#define DLCLOSE FreeLibrary
|
||||
#else
|
||||
#define DELIM ':'
|
||||
#define HTYPE void*
|
||||
#define PREFIX "lib"
|
||||
#define DLOPEN(a) dlopen(a, RTLD_LAZY)
|
||||
#define DLSYM dlsym
|
||||
#define DLCLOSE dlclose
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
|
||||
int HAL_LoadOneExtension(const char* library) {
|
||||
int rc = 1; // It is expected and reasonable not to find an extra simulation
|
||||
HTYPE handle = DLOPEN(library);
|
||||
#if !defined(WIN32) && !defined(_WIN32)
|
||||
if (!handle) {
|
||||
llvm::SmallString<128> libraryName("lib");
|
||||
libraryName += library;
|
||||
#if defined(__APPLE__)
|
||||
libraryName += ".dylib";
|
||||
#else
|
||||
libraryName += ".so";
|
||||
#endif
|
||||
handle = DLOPEN(libraryName.c_str());
|
||||
}
|
||||
#endif
|
||||
if (!handle) return rc;
|
||||
|
||||
auto init = reinterpret_cast<halsim_extension_init_func_t*>(
|
||||
DLSYM(handle, "HALSIM_InitExtension"));
|
||||
|
||||
if (init) rc = (*init)();
|
||||
|
||||
if (rc != 0) DLCLOSE(handle);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load any extra halsim libraries provided in the HALSIM_EXTENSIONS
|
||||
* environment variable.
|
||||
*/
|
||||
int HAL_LoadExtensions(void) {
|
||||
int rc = 1;
|
||||
llvm::SmallVector<llvm::StringRef, 2> libraries;
|
||||
const char* e = std::getenv("HALSIM_EXTENSIONS");
|
||||
if (!e) return rc;
|
||||
llvm::StringRef env{e};
|
||||
env.split(libraries, DELIM, -1, false);
|
||||
for (auto& libref : libraries) {
|
||||
llvm::SmallString<128> library(libref);
|
||||
rc = HAL_LoadOneExtension(library.c_str());
|
||||
if (rc < 0) break;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -7,9 +7,12 @@
|
||||
|
||||
#include "HAL/HAL.h"
|
||||
|
||||
#include <llvm/raw_ostream.h>
|
||||
|
||||
#include "ErrorsInternal.h"
|
||||
#include "HAL/DriverStation.h"
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/Extensions.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "MockData/RoboRioDataInternal.h"
|
||||
#include "MockHooksInternal.h"
|
||||
@@ -194,8 +197,21 @@ HAL_Bool HAL_GetBrownedOut(int32_t* status) {
|
||||
}
|
||||
|
||||
HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
static std::atomic_bool initialized{false};
|
||||
static std::mutex initializeMutex;
|
||||
// Initial check, as if it's true initialization has finished
|
||||
if (initialized) return true;
|
||||
|
||||
std::lock_guard<std::mutex> lock(initializeMutex);
|
||||
// Second check in case another thread was waiting
|
||||
if (initialized) return true;
|
||||
|
||||
llvm::outs().SetUnbuffered();
|
||||
if (HAL_LoadExtensions() < 0) return false;
|
||||
hal::RestartTiming();
|
||||
HAL_InitializeDriverStation();
|
||||
|
||||
initialized = true;
|
||||
return true; // Add initialization if we need to at a later point
|
||||
}
|
||||
|
||||
|
||||
@@ -7,20 +7,30 @@
|
||||
|
||||
#include "HAL/I2C.h"
|
||||
|
||||
#include "MockData/I2CDataInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
extern "C" {
|
||||
void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {}
|
||||
void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
|
||||
SimI2CData[port].SetInitialized(true);
|
||||
}
|
||||
int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
|
||||
uint8_t* dataToSend, int32_t sendSize,
|
||||
uint8_t* dataReceived, int32_t receiveSize) {
|
||||
SimI2CData[port].Write(deviceAddress, dataToSend, sendSize);
|
||||
SimI2CData[port].Read(deviceAddress, dataReceived, receiveSize);
|
||||
return 0;
|
||||
}
|
||||
int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress,
|
||||
uint8_t* dataToSend, int32_t sendSize) {
|
||||
SimI2CData[port].Write(deviceAddress, dataToSend, sendSize);
|
||||
return 0;
|
||||
}
|
||||
int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
|
||||
int32_t count) {
|
||||
SimI2CData[port].Read(deviceAddress, buffer, count);
|
||||
return 0;
|
||||
}
|
||||
void HAL_CloseI2C(HAL_I2CPort port) {}
|
||||
}
|
||||
void HAL_CloseI2C(HAL_I2CPort port) { SimI2CData[port].SetInitialized(false); }
|
||||
} // extern "C"
|
||||
|
||||
@@ -309,4 +309,17 @@ double HALSIM_GetAccelerometerZ(int32_t index) {
|
||||
void HALSIM_SetAccelerometerZ(int32_t index, double z) {
|
||||
SimAccelerometerData[index].SetZ(z);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterAccelerometerAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimAccelerometerData[index].RegisterActiveCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAccelerometerData[index].RegisterRangeCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAccelerometerData[index].RegisterXCallback(callback, param, initialNotify);
|
||||
SimAccelerometerData[index].RegisterYCallback(callback, param, initialNotify);
|
||||
SimAccelerometerData[index].RegisterZCallback(callback, param, initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -192,4 +192,15 @@ HAL_Bool HALSIM_GetAnalogGyroInitialized(int32_t index) {
|
||||
void HALSIM_SetAnalogGyroInitialized(int32_t index, HAL_Bool initialized) {
|
||||
SimAnalogGyroData[index].SetInitialized(initialized);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterAnalogGyroAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimAnalogGyroData[index].RegisterAngleCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogGyroData[index].RegisterRateCallback(callback, param, initialNotify);
|
||||
SimAnalogGyroData[index].RegisterInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -550,4 +550,27 @@ void HALSIM_SetAnalogInAccumulatorDeadband(int32_t index,
|
||||
int32_t accumulatorDeadband) {
|
||||
SimAnalogInData[index].SetAccumulatorDeadband(accumulatorDeadband);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterAnalogInAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify) {
|
||||
SimAnalogInData[index].RegisterInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogInData[index].RegisterAverageBitsCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogInData[index].RegisterOversampleBitsCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogInData[index].RegisterVoltageCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogInData[index].RegisterAccumulatorInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogInData[index].RegisterAccumulatorValueCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogInData[index].RegisterAccumulatorCountCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogInData[index].RegisterAccumulatorCenterCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogInData[index].RegisterAccumulatorDeadbandCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -135,4 +135,13 @@ HAL_Bool HALSIM_GetAnalogOutInitialized(int32_t index) {
|
||||
void HALSIM_SetAnalogOutInitialized(int32_t index, HAL_Bool initialized) {
|
||||
SimAnalogOutData[index].SetInitialized(initialized);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterAnalogOutAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify) {
|
||||
SimAnalogOutData[index].RegisterVoltageCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogOutData[index].RegisterInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -254,4 +254,18 @@ void HALSIM_SetAnalogTriggerTriggerMode(int32_t index,
|
||||
HALSIM_AnalogTriggerMode triggerMode) {
|
||||
SimAnalogTriggerData[index].SetTriggerMode(triggerMode);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterAnalogTriggerAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimAnalogTriggerData[index].RegisterInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogTriggerData[index].RegisterTriggerLowerBoundCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogTriggerData[index].RegisterTriggerUpperBoundCallback(callback, param,
|
||||
initialNotify);
|
||||
SimAnalogTriggerData[index].RegisterTriggerModeCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -303,4 +303,13 @@ int32_t HALSIM_GetDIOFilterIndex(int32_t index) {
|
||||
void HALSIM_SetDIOFilterIndex(int32_t index, int32_t filterIndex) {
|
||||
SimDIOData[index].SetFilterIndex(filterIndex);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterDIOAllCallbacks(int32_t index, HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify) {
|
||||
SimDIOData[index].RegisterInitializedCallback(callback, param, initialNotify);
|
||||
SimDIOData[index].RegisterValueCallback(callback, param, initialNotify);
|
||||
SimDIOData[index].RegisterPulseLengthCallback(callback, param, initialNotify);
|
||||
SimDIOData[index].RegisterIsInputCallback(callback, param, initialNotify);
|
||||
SimDIOData[index].RegisterFilterIndexCallback(callback, param, initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -192,4 +192,15 @@ int32_t HALSIM_GetDigitalPWMPin(int32_t index) {
|
||||
void HALSIM_SetDigitalPWMPin(int32_t index, int32_t pin) {
|
||||
SimDigitalPWMData[index].SetPin(pin);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterDigitalPWMAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimDigitalPWMData[index].RegisterInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
SimDigitalPWMData[index].RegisterDutyCycleCallback(callback, param,
|
||||
initialNotify);
|
||||
SimDigitalPWMData[index].RegisterPinCallback(callback, param, initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -448,4 +448,22 @@ void HALSIM_SetDriverStationMatchTime(double matchTime) {
|
||||
void HALSIM_NotifyDriverStationNewData(void) {
|
||||
SimDriverStationData.NotifyNewData();
|
||||
}
|
||||
|
||||
void HALSIM_RegisterDriverStationAllCallbacks(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimDriverStationData.RegisterEnabledCallback(callback, param, initialNotify);
|
||||
SimDriverStationData.RegisterAutonomousCallback(callback, param,
|
||||
initialNotify);
|
||||
SimDriverStationData.RegisterTestCallback(callback, param, initialNotify);
|
||||
SimDriverStationData.RegisterEStopCallback(callback, param, initialNotify);
|
||||
SimDriverStationData.RegisterFmsAttachedCallback(callback, param,
|
||||
initialNotify);
|
||||
SimDriverStationData.RegisterDsAttachedCallback(callback, param,
|
||||
initialNotify);
|
||||
SimDriverStationData.RegisterAllianceStationIdCallback(callback, param,
|
||||
initialNotify);
|
||||
SimDriverStationData.RegisterMatchTimeCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -485,4 +485,22 @@ void HALSIM_SetEncoderSamplesToAverage(int32_t index,
|
||||
int32_t samplesToAverage) {
|
||||
SimEncoderData[index].SetSamplesToAverage(samplesToAverage);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterEncoderAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify) {
|
||||
SimEncoderData[index].RegisterInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
SimEncoderData[index].RegisterCountCallback(callback, param, initialNotify);
|
||||
SimEncoderData[index].RegisterPeriodCallback(callback, param, initialNotify);
|
||||
SimEncoderData[index].RegisterResetCallback(callback, param, initialNotify);
|
||||
SimEncoderData[index].RegisterMaxPeriodCallback(callback, param,
|
||||
initialNotify);
|
||||
SimEncoderData[index].RegisterDirectionCallback(callback, param,
|
||||
initialNotify);
|
||||
SimEncoderData[index].RegisterReverseDirectionCallback(callback, param,
|
||||
initialNotify);
|
||||
SimEncoderData[index].RegisterSamplesToAverageCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -448,4 +448,30 @@ double HALSIM_GetPCMCompressorCurrent(int32_t index) {
|
||||
void HALSIM_SetPCMCompressorCurrent(int32_t index, double compressorCurrent) {
|
||||
SimPCMData[index].SetCompressorCurrent(compressorCurrent);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterPCMAllNonSolenoidCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimPCMData[index].RegisterCompressorInitializedCallback(callback, param,
|
||||
initialNotify);
|
||||
SimPCMData[index].RegisterCompressorOnCallback(callback, param,
|
||||
initialNotify);
|
||||
SimPCMData[index].RegisterClosedLoopEnabledCallback(callback, param,
|
||||
initialNotify);
|
||||
SimPCMData[index].RegisterPressureSwitchCallback(callback, param,
|
||||
initialNotify);
|
||||
SimPCMData[index].RegisterCompressorCurrentCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterPCMAllSolenoidCallbacks(int32_t index, int32_t channel,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimPCMData[index].RegisterSolenoidInitializedCallback(channel, callback,
|
||||
param, initialNotify);
|
||||
SimPCMData[index].RegisterSolenoidOutputCallback(channel, callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -250,4 +250,13 @@ double HALSIM_GetPDPCurrent(int32_t index, int32_t channel) {
|
||||
void HALSIM_SetPDPCurrent(int32_t index, int32_t channel, double current) {
|
||||
SimPDPData[index].SetCurrent(channel, current);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterPDPAllNonCurrentCallbacks(int32_t index, int32_t channel,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimPDPData[index].RegisterInitializedCallback(callback, param, initialNotify);
|
||||
SimPDPData[index].RegisterTemperatureCallback(callback, param, initialNotify);
|
||||
SimPDPData[index].RegisterVoltageCallback(callback, param, initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -361,4 +361,14 @@ HAL_Bool HALSIM_GetPWMZeroLatch(int32_t index) {
|
||||
void HALSIM_SetPWMZeroLatch(int32_t index, HAL_Bool zeroLatch) {
|
||||
SimPWMData[index].SetZeroLatch(zeroLatch);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterPWMAllCallbacks(int32_t index, HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify) {
|
||||
SimPWMData[index].RegisterInitializedCallback(callback, param, initialNotify);
|
||||
SimPWMData[index].RegisterRawValueCallback(callback, param, initialNotify);
|
||||
SimPWMData[index].RegisterSpeedCallback(callback, param, initialNotify);
|
||||
SimPWMData[index].RegisterPositionCallback(callback, param, initialNotify);
|
||||
SimPWMData[index].RegisterPeriodScaleCallback(callback, param, initialNotify);
|
||||
SimPWMData[index].RegisterZeroLatchCallback(callback, param, initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -251,4 +251,15 @@ HAL_Bool HALSIM_GetRelayReverse(int32_t index) {
|
||||
void HALSIM_SetRelayReverse(int32_t index, HAL_Bool reverse) {
|
||||
SimRelayData[index].SetReverse(reverse);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterRelayAllCallcbaks(int32_t index,
|
||||
HAL_NotifyCallback callback, void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimRelayData[index].RegisterInitializedForwardCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRelayData[index].RegisterInitializedReverseCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRelayData[index].RegisterForwardCallback(callback, param, initialNotify);
|
||||
SimRelayData[index].RegisterReverseCallback(callback, param, initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -887,4 +887,39 @@ int32_t HALSIM_GetRoboRioUserFaults3V3(int32_t index) {
|
||||
void HALSIM_SetRoboRioUserFaults3V3(int32_t index, int32_t userFaults3V3) {
|
||||
SimRoboRioData[index].SetUserFaults3V3(userFaults3V3);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterRoboRioAllCallbacks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify) {
|
||||
SimRoboRioData[index].RegisterFPGAButtonCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterVInVoltageCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterVInCurrentCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserVoltage6VCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserCurrent6VCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserActive6VCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserVoltage5VCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserCurrent5VCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserActive5VCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserVoltage3V3Callback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserCurrent3V3Callback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserActive3V3Callback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserFaults6VCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserFaults5VCallback(callback, param,
|
||||
initialNotify);
|
||||
SimRoboRioData[index].RegisterUserFaults3V3Callback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -306,4 +306,20 @@ double HALSIM_GetSPIAccelerometerZ(int32_t index) {
|
||||
void HALSIM_SetSPIAccelerometerZ(int32_t index, double z) {
|
||||
SimSPIAccelerometerData[index].SetZ(z);
|
||||
}
|
||||
|
||||
void HALSIM_RegisterSPIAccelerometerAllCallbcaks(int32_t index,
|
||||
HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
SimSPIAccelerometerData[index].RegisterActiveCallback(callback, param,
|
||||
initialNotify);
|
||||
SimSPIAccelerometerData[index].RegisterRangeCallback(callback, param,
|
||||
initialNotify);
|
||||
SimSPIAccelerometerData[index].RegisterXCallback(callback, param,
|
||||
initialNotify);
|
||||
SimSPIAccelerometerData[index].RegisterYCallback(callback, param,
|
||||
initialNotify);
|
||||
SimSPIAccelerometerData[index].RegisterZCallback(callback, param,
|
||||
initialNotify);
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
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
|
||||
@@ -51,4 +51,4 @@ void HALSIM_WaitForProgramStart(void) {
|
||||
void HALSIM_SetProgramStarted(void) { SetProgramStarted(); }
|
||||
|
||||
void HALSIM_RestartTiming(void) { RestartTiming(); }
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -19,4 +19,4 @@ int64_t GetFPGATime();
|
||||
double GetFPGATimestamp();
|
||||
|
||||
void SetProgramStarted();
|
||||
}
|
||||
} // namespace hal
|
||||
|
||||
@@ -44,4 +44,4 @@ int32_t HAL_WriteOSSerial(HAL_SerialPort port, const char* buffer,
|
||||
void HAL_FlushOSSerial(HAL_SerialPort port, int32_t* status) {}
|
||||
void HAL_ClearOSSerial(HAL_SerialPort port, int32_t* status) {}
|
||||
void HAL_CloseOSSerial(HAL_SerialPort port, int32_t* status) {}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -38,4 +38,4 @@ double HAL_GetPDPTotalPower(int32_t module, int32_t* status) { return 0.0; }
|
||||
double HAL_GetPDPTotalEnergy(int32_t module, int32_t* status) { return 0.0; }
|
||||
void HAL_ResetPDPTotalEnergy(int32_t module, int32_t* status) {}
|
||||
void HAL_ClearPDPStickyFaults(int32_t module, int32_t* status) {}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user