-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hal,wpilib] Add function to control "Radio" LED (#6073)
- Loading branch information
Showing
22 changed files
with
531 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.hal; | ||
|
||
public class LEDJNI extends JNIWrapper { | ||
public static final int RADIO_LED_STATE_OFF = 0; | ||
public static final int RADIO_LED_STATE_GREEN = 1; | ||
public static final int RADIO_LED_STATE_RED = 2; | ||
public static final int RADIO_LED_STATE_ORANGE = 3; | ||
|
||
/** | ||
* Set the state of the "Radio" LED. | ||
* | ||
* @param state The state to set the LED to. | ||
* @see "HAL_SetRadioLEDState" | ||
*/ | ||
public static native void setRadioLEDState(int state); | ||
|
||
/** | ||
* Get the state of the "Radio" LED. | ||
* | ||
* @return The state of the LED. | ||
* @see "HAL_GetRadioLEDState" | ||
*/ | ||
public static native int getRadioLEDState(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#include "hal/LEDs.h" | ||
|
||
#include <unistd.h> | ||
|
||
#include <fstream> | ||
|
||
#include <wpi/fs.h> | ||
|
||
#include "hal/Errors.h" | ||
|
||
namespace hal::init { | ||
|
||
void InitializeLEDs() { | ||
int32_t status = 0; | ||
HAL_SetRadioLEDState(HAL_RadioLED_kOff, &status); | ||
} | ||
} // namespace hal::init | ||
|
||
static const fs::path radioLEDGreenFilePath = | ||
"/sys/class/leds/nilrt:wifi:primary/brightness"; | ||
static const fs::path radioLEDRedFilePath = | ||
"/sys/class/leds/nilrt:wifi:secondary/brightness"; | ||
|
||
static const char* onStr = "1"; | ||
static const char* offStr = "0"; | ||
|
||
extern "C" { | ||
void HAL_SetRadioLEDState(HAL_RadioLEDState state, int32_t* status) { | ||
std::error_code ec; | ||
fs::file_t greenFile = fs::OpenFileForWrite(radioLEDGreenFilePath, ec, | ||
fs::CD_OpenExisting, fs::OF_Text); | ||
if (ec) { | ||
*status = INCOMPATIBLE_STATE; | ||
return; | ||
} | ||
fs::file_t redFile = fs::OpenFileForWrite(radioLEDRedFilePath, ec, | ||
fs::CD_OpenExisting, fs::OF_Text); | ||
if (ec) { | ||
*status = INCOMPATIBLE_STATE; | ||
return; | ||
} | ||
|
||
write(greenFile, state & HAL_RadioLED_kGreen ? onStr : offStr, 1); | ||
write(redFile, state & HAL_RadioLED_kRed ? onStr : offStr, 1); | ||
|
||
fs::CloseFile(greenFile); | ||
fs::CloseFile(redFile); | ||
} | ||
|
||
bool ReadStateFromFile(fs::path path, int32_t* status) { | ||
std::error_code ec; | ||
fs::file_t file = fs::OpenFileForRead(path, ec, fs::OF_Text); | ||
if (ec) { | ||
*status = INCOMPATIBLE_STATE; | ||
return false; | ||
} | ||
// We only need to read one byte because the file won't have leading zeros. | ||
char buf[1]{}; | ||
size_t count = read(file, buf, 1); | ||
if (count == 0) { | ||
*status = INCOMPATIBLE_STATE; | ||
return false; | ||
} | ||
// If the brightness is not zero, the LED is on. | ||
return buf[0] != '0'; | ||
} | ||
|
||
HAL_RadioLEDState HAL_GetRadioLEDState(int32_t* status) { | ||
bool green = ReadStateFromFile(radioLEDGreenFilePath, status); | ||
bool red = ReadStateFromFile(radioLEDRedFilePath, status); | ||
if (*status == 0) { | ||
if (green && red) { | ||
return HAL_RadioLED_kOrange; | ||
} else if (green) { | ||
return HAL_RadioLED_kGreen; | ||
} else if (red) { | ||
return HAL_RadioLED_kRed; | ||
} else { | ||
return HAL_RadioLED_kOff; | ||
} | ||
} else { | ||
return HAL_RadioLED_kOff; | ||
} | ||
} | ||
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#include <fmt/core.h> | ||
|
||
#include "HALUtil.h" | ||
#include "edu_wpi_first_hal_LEDJNI.h" | ||
#include "hal/LEDs.h" | ||
|
||
static_assert(edu_wpi_first_hal_LEDJNI_RADIO_LED_STATE_OFF == | ||
HAL_RadioLEDState::HAL_RadioLED_kOff); | ||
static_assert(edu_wpi_first_hal_LEDJNI_RADIO_LED_STATE_GREEN == | ||
HAL_RadioLEDState::HAL_RadioLED_kGreen); | ||
static_assert(edu_wpi_first_hal_LEDJNI_RADIO_LED_STATE_RED == | ||
HAL_RadioLEDState::HAL_RadioLED_kRed); | ||
static_assert(edu_wpi_first_hal_LEDJNI_RADIO_LED_STATE_ORANGE == | ||
HAL_RadioLEDState::HAL_RadioLED_kOrange); | ||
|
||
using namespace hal; | ||
|
||
extern "C" { | ||
/* | ||
* Class: edu_wpi_first_hal_LEDJNI | ||
* Method: setRadioLEDState | ||
* Signature: (I)V | ||
*/ | ||
JNIEXPORT void JNICALL | ||
Java_edu_wpi_first_hal_LEDJNI_setRadioLEDState | ||
(JNIEnv* env, jclass, jint state) | ||
{ | ||
int32_t status = 0; | ||
HAL_SetRadioLEDState(static_cast<HAL_RadioLEDState>(state), &status); | ||
CheckStatus(env, status); | ||
} | ||
|
||
/* | ||
* Class: edu_wpi_first_hal_LEDJNI | ||
* Method: getRadioLEDState | ||
* Signature: ()I | ||
*/ | ||
JNIEXPORT jint JNICALL | ||
Java_edu_wpi_first_hal_LEDJNI_getRadioLEDState | ||
(JNIEnv* env, jclass) | ||
{ | ||
int32_t status = 0; | ||
auto retVal = HAL_GetRadioLEDState(&status); | ||
CheckStatus(env, status); | ||
return retVal; | ||
} | ||
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#pragma once | ||
#include "hal/Types.h" | ||
|
||
HAL_ENUM(HAL_RadioLEDState){HAL_RadioLED_kOff = 0, HAL_RadioLED_kGreen = 1, | ||
HAL_RadioLED_kRed = 2, HAL_RadioLED_kOrange = 3}; | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
/** | ||
* Set the state of the "Radio" LED. | ||
* @param state The state to set the LED to. | ||
* @param[out] status the error code, or 0 for success | ||
*/ | ||
void HAL_SetRadioLEDState(HAL_RadioLEDState state, int32_t* status); | ||
|
||
/** | ||
* Get the state of the "Radio" LED. | ||
* | ||
* @param[out] status the error code, or 0 for success | ||
* @return The state of the LED. | ||
*/ | ||
HAL_RadioLEDState HAL_GetRadioLEDState(int32_t* status); | ||
#ifdef __cplusplus | ||
} // extern "C" | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#include "hal/LEDs.h" | ||
|
||
#include "hal/simulation/RoboRioData.h" | ||
|
||
namespace hal::init { | ||
void InitializeLEDs() {} | ||
} // namespace hal::init | ||
|
||
extern "C" { | ||
|
||
void HAL_SetRadioLEDState(HAL_RadioLEDState state, int32_t* status) { | ||
HALSIM_SetRoboRioRadioLEDState(state); | ||
} | ||
HAL_RadioLEDState HAL_GetRadioLEDState(int32_t* status) { | ||
return HALSIM_GetRoboRioRadioLEDState(); | ||
} | ||
} // extern "C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.