Skip to content

Commit

Permalink
Add radio LED control to HAL
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Nov 3, 2023
1 parent ddc8db6 commit 2407faf
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
22 changes: 22 additions & 0 deletions hal/src/main/java/edu/wpi/first/hal/LEDJNI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 enum RadioLEDState {
kOff(0),
kGreen(1),
kRed(2),
kOrange(3);

final int m_value;

RadioLEDState(int value) {
m_value = value;
}
}

public static native void setRadioLEDState(int state);
}
42 changes: 42 additions & 0 deletions hal/src/main/native/athena/LEDs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 <unistd.h>

#include <fstream>

#include <wpi/fs.h>

#include "hal/Errors.h"
#include "hal/HALBase.h"

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

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);
}
24 changes: 24 additions & 0 deletions hal/src/main/native/cpp/jni/LEDJNI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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/HALBase.h"

using namespace hal;
/*
* 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((HAL_RadioLEDState)state, &status);
CheckStatus(env, status);
}
5 changes: 5 additions & 0 deletions hal/src/main/native/include/hal/HALBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ void HAL_SimPeriodicBefore(void);
*/
void HAL_SimPeriodicAfter(void);

HAL_ENUM(HAL_RadioLEDState){HAL_RadioLED_kOFF = 0, HAL_RadioLED_kGreen = 1,
HAL_RadioLED_kRed = 2, HAL_RadioLED_kOrange = 3};

void HAL_SetRadioLEDState(HAL_RadioLEDState state, int32_t* status);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
7 changes: 7 additions & 0 deletions hal/src/main/native/sim/LEDs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// 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/HALBase.h"

void HAL_SetRadioLEDState(HAL_RadioLEDState state, int32_t* status) {}

0 comments on commit 2407faf

Please sign in to comment.