Compare commits

...

29 Commits

Author SHA1 Message Date
Thad House
efc7770e9b Fixes NPE in DriverStation initialization (#694)
Introduced by #626.
2017-10-27 23:16:42 -07:00
Tyler Veness
21585f70a8 Added functions for detecting button press and release events (#626)
I also shuffled around the HID interfaces to be more intuitive, deprecated some
Joystick and XboxController member functions, and deprecated the JoystickBase
and GamepadBase classes.

Supersedes #89.
2017-10-27 21:45:56 -07:00
Tyler Veness
c33fca34e9 Added TimedRobot template (#673) 2017-10-27 21:33:04 -07:00
PJ Reiniger
de95f08a10 Adding call to notify program started (#692)
Causes the HALSIM_WaitForProgramStart loop to break, essentially
notifying simulators the robot is good to go.
2017-10-27 21:00:52 -07:00
Thad House
90f99dc571 Adds PWM to LoopTiming and CycleStart HAL calls (#693) 2017-10-27 19:44:40 -07:00
Thad House
f34332643a Uses new FPGA calls to get 64 bit FPGA time and 64 bit PWM cycle start time. (#687) 2017-10-27 18:03:10 -07:00
Tyler Veness
4ab095e9c9 Fix formatting in CameraServer.cpp (#689)
A PR didn't rerun the formatter before merging.
2017-10-27 11:35:57 -07:00
Thad House
541753c814 Updates to 2018 v9 image (#686) 2017-10-27 00:47:56 -07:00
Jeremy White
f02bb058bd Set the llvm standard output stream to be unbuffered. (#678)
This is particularly useful for the simulation when invokved
inside Eclipse.  Otherwise, you won't see the robot starting
message.
2017-10-27 00:46:56 -07:00
sciencewhiz
12e96c6f13 Add usage reporting to CameraServer (#682) 2017-10-27 00:45:54 -07:00
Austin Shalit
b65447b6f5 Fix spelling typos (#595) 2017-10-27 00:44:51 -07:00
Tyler Veness
9945a5b3c6 Ran formatter (#681) 2017-10-26 19:28:59 -07:00
Thad House
ce4c9edd1f Fixes halsim_print to not try and build when the 'onlyAthena' flag has been set (#685)
Building a non-athena library in athena only is not possible.
2017-10-25 10:52:35 -07:00
PJ Reiniger
a6e6ae41b9 Add function that can register all the HAL callbacks at once (#646)
Since all of the callbacks issue a string identifier, it makes it
possible and easy to have one function callback, and differentiate the
path to take based on the string.  Hooking up all the callbacks at once
makes it easier for the simulator developer to know when something was
added to wpilib rather than looking at the commits.
2017-10-21 22:45:41 -07:00
Austin Shalit
f4e2e41aaf Remove leading '*' from license and rename to LICENSE.txt (#596) 2017-10-21 16:04:57 -07:00
Thad House
1e528669ff Adds publishing for examples and templates so they can be grabbed from eclipse (#674) 2017-10-21 15:33:42 -07:00
Thad House
a1ea448406 Adds JNI call to get CANStatus (#677)
Call already existed in the HAL.
2017-10-21 15:32:05 -07:00
Tyler Veness
9dc1de1d09 Specified angle units for Vector2d rotate() function. (#679)
Fixes #676.
2017-10-21 15:29:39 -07:00
Tyler Veness
0521d85048 Moved comment after include line so include order is determined properly (#680) 2017-10-21 15:28:46 -07:00
PJ Reiniger
3c842d8234 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
2017-10-18 23:01:58 -07:00
Jeremy White
be77f9cb26 Pull request for the Extensions interface only (#655)
* Modify halsim to be able to load extension libraries if they are available.

It will read the list of libraries to try from the HALSIM_EXTENSIONS
environment variable.  Multiple libraries can be given if separated
by ';' (Windows) or ':' (Unix).

The library must have an 'HALSIM_InitExtension' method that returns >= 0 on success.

The library is expected to use the interface expressed by
  hal/src/src/main/native/include/MockData

* Add a simple halsim library that just prints robot values.

This makes a good test bed for cross platform purposes,
and provides the ultimate in light weight simulators.

This initial version only prints PWM values.
2017-10-18 00:27:55 -07:00
Austin Shalit
2fc60680f4 Remove RedundantModifiers (#578) 2017-10-17 21:47:55 -07:00
Tyler Veness
0291a95f68 Add cpp examples (#659)
* Added C++ robot project examples and set up sub .clang-format for them

* Ran formatter
2017-10-17 21:37:58 -07:00
Austin Shalit
66002d6cac [WIP] Move examples to allwpilib (Java) (#569)
* Move examples to allwpilib

* Add checkstyle config to examples project

* Ran wpiformat

* Run checkstyle on examples

* Change maximum line length for examples to 80 chars

This number was chosen based on testing of the number of characters shown by default in Eclipse done by @Kevin-OConnor: 51 chars by default on an E09 @ 1024x600 (which has the welcome window open on the right), 71 with welcome closed, 95 with the right-hand outline pane closed

* Add mavenCentral repository

* Rename subproject & error on deprecated API use

* Remove deprecated API usage
2017-10-16 22:30:21 -07:00
Tyler Veness
a7e9ac1062 Fix capitalization of class names in PIDToleranceTest.cpp (#588) 2017-10-16 21:16:33 -07:00
Thad House
434d60592c Adds HAL_Initialize to ErrorBase constructor, and makes HAL_Initialize properly reentrant (#668)
Partial fix to #663, and most likely the best we are going to be able to
get.
2017-10-16 20:00:32 -07:00
Thad House
ee20747255 Adds WPILib JNI shared debug file publish. (#671) 2017-10-16 19:59:05 -07:00
Tyler Veness
f4779379c3 Added brace comments (#670)
Enforced by wpilibsuite/styleguide#80.
2017-10-16 19:56:08 -07:00
Austin Shalit
877a9eae1f Add SpeedControllerGroup (#362) 2017-10-16 19:54:36 -07:00
366 changed files with 12021 additions and 1190 deletions

View File

@@ -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
View 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.

View File

@@ -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")

View File

@@ -108,6 +108,11 @@ model {
}
}
}
binaries.all { binary ->
if (binary.targetPlatform.operatingSystem.linux) {
linker.args "-ldl"
}
}
}
if (project.hasProperty('buildHalStaticDeps')) {
halSimStaticDeps(NativeLibrarySpec) {

View File

@@ -46,4 +46,4 @@ void HAL_CAN_GetCANStatus(float* percentBusUtilization, uint32_t* busOffCount,
percentBusUtilization, busOffCount, txFullCount, receiveErrorCount,
transmitErrorCount, status);
}
}
} // extern "C"

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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"

View File

@@ -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())) {

View File

@@ -24,5 +24,5 @@ double HAL_GetAccelerometerX(void);
double HAL_GetAccelerometerY(void);
double HAL_GetAccelerometerZ(void);
#ifdef __cplusplus
}
} // extern "C"
#endif

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -43,5 +43,5 @@ HAL_Bool HAL_GetAnalogTriggerOutput(HAL_AnalogTriggerHandle analogTriggerHandle,
HAL_AnalogTriggerType type,
int32_t* status);
#ifdef __cplusplus
}
} // extern "C"
#endif

View File

@@ -56,5 +56,5 @@ void HAL_CAN_GetCANStatus(float* percentBusUtilization, uint32_t* busOffCount,
uint32_t* transmitErrorCount, int32_t* status);
#ifdef __cplusplus
}
} // extern "C"
#endif

View File

@@ -44,5 +44,5 @@ HAL_Bool HAL_GetCompressorNotConnectedStickyFault(
HAL_Bool HAL_GetCompressorNotConnectedFault(
HAL_CompressorHandle compressorHandle, int32_t* status);
#ifdef __cplusplus
}
} // extern "C"
#endif

View File

@@ -15,5 +15,5 @@ extern "C" {
int32_t HAL_GetSystemClockTicksPerMicrosecond(void);
#ifdef __cplusplus
}
} // extern "C"
#endif

View File

@@ -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

View File

@@ -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

View File

@@ -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

View 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"

View File

@@ -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

View File

@@ -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

View File

@@ -46,5 +46,5 @@ void HAL_SetInterruptUpSourceEdge(HAL_InterruptHandle interruptHandle,
HAL_Bool risingEdge, HAL_Bool fallingEdge,
int32_t* status);
#ifdef __cplusplus
}
} // extern "C"
#endif

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -32,5 +32,5 @@ int32_t HAL_GetNumSolenoidChannels(void);
int32_t HAL_GetNumPDPModules(void);
int32_t HAL_GetNumPDPChannels(void);
#ifdef __cplusplus
}
} // extern "C"
#endif

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"

View File

@@ -82,6 +82,7 @@ namespace nUsageReporting
kResourceType_PDP,
kResourceType_PCM, // 60
kResourceType_PigeonIMU,
kResourceType_NidecBrushless,
} tResourceType;
typedef enum

View File

@@ -12,4 +12,4 @@
extern "C" {
HAL_NotifierHandle HAL_InitializeNotifierNonThreadedUnsafe(
HAL_NotifierProcessFunction process, void* param, int32_t* status);
}
} // extern "C"

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View 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

View File

@@ -11,4 +11,4 @@ extern "C" {
void HALSIM_WaitForProgramStart(void);
void HALSIM_SetProgramStarted(void);
void HALSIM_RestartTiming(void);
}
} // extern "C"

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View 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

View File

@@ -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"

View File

@@ -103,4 +103,4 @@ void HAL_GetAccumulatorOutput(HAL_AnalogInputHandle analogPortHandle,
*count = SimAnalogInData[port->channel].GetAccumulatorCount();
*value = SimAnalogInData[port->channel].GetAccumulatorValue();
}
}
} // extern "C"

View File

@@ -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"

View File

@@ -14,4 +14,4 @@ namespace hal {
IndexedHandleResource<HAL_AnalogInputHandle, hal::AnalogPort, kNumAnalogInputs,
HAL_HandleEnum::AnalogInput>
analogInputHandles;
}
} // namespace hal

View File

@@ -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"

View File

@@ -22,7 +22,7 @@ struct AnalogTrigger {
uint8_t index;
HAL_Bool trigState;
};
}
} // namespace
using namespace hal;

View File

@@ -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"

View File

@@ -15,4 +15,4 @@ extern "C" {
int32_t HAL_GetSystemClockTicksPerMicrosecond(void) {
return kSystemClockTicksPerMicrosecond;
}
}
} // extern "C"

View File

@@ -11,4 +11,4 @@
namespace hal {
constexpr int32_t kSystemClockTicksPerMicrosecond = 40;
}
} // namespace hal

View File

@@ -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"

View File

@@ -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"

View File

@@ -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

View File

@@ -25,7 +25,7 @@ struct Encoder {
uint8_t index;
};
struct Empty {};
}
} // namespace
static LimitedHandleResource<HAL_EncoderHandle, Encoder,
kNumEncoders + kNumCounters,

View 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"

View File

@@ -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
}

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View 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"

View 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

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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();
}

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View File

@@ -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"

View 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"

View 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

View File

@@ -51,4 +51,4 @@ void HALSIM_WaitForProgramStart(void) {
void HALSIM_SetProgramStarted(void) { SetProgramStarted(); }
void HALSIM_RestartTiming(void) { RestartTiming(); }
}
} // extern "C"

View File

@@ -19,4 +19,4 @@ int64_t GetFPGATime();
double GetFPGATimestamp();
void SetProgramStarted();
}
} // namespace hal

View File

@@ -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"

View File

@@ -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