2019-09-23 00:24:10 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-01-20 22:47:36 -08:00
|
|
|
/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
|
2019-09-23 00:24:10 -07:00
|
|
|
/* 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 "DIOGui.h"
|
|
|
|
|
|
|
|
|
|
#include <hal/Ports.h>
|
2020-06-27 22:11:24 -07:00
|
|
|
#include <hal/simulation/DIOData.h>
|
|
|
|
|
#include <hal/simulation/DigitalPWMData.h>
|
|
|
|
|
#include <hal/simulation/DutyCycleData.h>
|
|
|
|
|
#include <hal/simulation/EncoderData.h>
|
|
|
|
|
#include <hal/simulation/SimDeviceData.h>
|
2019-09-23 00:24:10 -07:00
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
|
|
#include "HALSimGui.h"
|
2020-01-20 22:47:36 -08:00
|
|
|
#include "IniSaver.h"
|
|
|
|
|
#include "IniSaverInfo.h"
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
using namespace halsimgui;
|
|
|
|
|
|
2020-01-20 22:47:36 -08:00
|
|
|
static IniSaver<NameInfo> gDIO{"DIO"};
|
|
|
|
|
|
2019-09-23 00:24:10 -07:00
|
|
|
static void LabelSimDevice(const char* name, HAL_SimDeviceHandle simDevice) {
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
|
|
|
|
ImGui::LabelText(name, "%s", HALSIM_GetSimDeviceName(simDevice));
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DisplayDIO() {
|
|
|
|
|
bool hasAny = false;
|
|
|
|
|
static int numDIO = HAL_GetNumDigitalChannels();
|
|
|
|
|
static int numPWM = HAL_GetNumDigitalPWMOutputs();
|
|
|
|
|
static int numEncoder = HAL_GetNumEncoders();
|
2019-11-02 15:26:08 -07:00
|
|
|
static int numDutyCycle = HAL_GetNumDutyCycles();
|
2019-09-23 00:24:10 -07:00
|
|
|
static auto pwmMap = std::make_unique<int[]>(numDIO);
|
|
|
|
|
static auto encoderMap = std::make_unique<int[]>(numDIO);
|
2019-11-02 15:26:08 -07:00
|
|
|
static auto dutyCycleMap = std::make_unique<int[]>(numDIO);
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
std::memset(pwmMap.get(), 0, numDIO * sizeof(pwmMap[0]));
|
|
|
|
|
std::memset(encoderMap.get(), 0, numDIO * sizeof(encoderMap[0]));
|
2019-11-02 15:26:08 -07:00
|
|
|
std::memset(dutyCycleMap.get(), 0, numDIO * sizeof(dutyCycleMap[0]));
|
2019-09-23 00:24:10 -07:00
|
|
|
|
|
|
|
|
for (int i = 0; i < numPWM; ++i) {
|
|
|
|
|
if (HALSIM_GetDigitalPWMInitialized(i)) {
|
|
|
|
|
int channel = HALSIM_GetDigitalPWMPin(i);
|
|
|
|
|
if (channel >= 0 && channel < numDIO) pwmMap[channel] = i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < numEncoder; ++i) {
|
|
|
|
|
if (HALSIM_GetEncoderInitialized(i)) {
|
|
|
|
|
int channel;
|
|
|
|
|
channel = HALSIM_GetEncoderDigitalChannelA(i);
|
|
|
|
|
if (channel >= 0 && channel < numDIO) encoderMap[channel] = i + 1;
|
|
|
|
|
channel = HALSIM_GetEncoderDigitalChannelB(i);
|
|
|
|
|
if (channel >= 0 && channel < numDIO) encoderMap[channel] = i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 15:26:08 -07:00
|
|
|
for (int i = 0; i < numDutyCycle; ++i) {
|
|
|
|
|
if (HALSIM_GetDutyCycleInitialized(i)) {
|
|
|
|
|
int channel = HALSIM_GetDutyCycleDigitalChannel(i);
|
|
|
|
|
if (channel >= 0 && channel < numDIO) dutyCycleMap[channel] = i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-13 23:25:21 -07:00
|
|
|
ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
|
2019-09-23 00:24:10 -07:00
|
|
|
for (int i = 0; i < numDIO; ++i) {
|
|
|
|
|
if (HALSIM_GetDIOInitialized(i)) {
|
|
|
|
|
hasAny = true;
|
2020-01-20 22:47:36 -08:00
|
|
|
auto& info = gDIO[i];
|
|
|
|
|
char name[128];
|
2019-09-23 00:24:10 -07:00
|
|
|
if (pwmMap[i] > 0) {
|
2020-01-20 22:47:36 -08:00
|
|
|
info.GetName(name, sizeof(name), "PWM", i);
|
2019-09-23 00:24:10 -07:00
|
|
|
if (auto simDevice = HALSIM_GetDIOSimDevice(i)) {
|
|
|
|
|
LabelSimDevice(name, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
ImGui::LabelText(name, "%0.3f",
|
|
|
|
|
HALSIM_GetDigitalPWMDutyCycle(pwmMap[i] - 1));
|
|
|
|
|
}
|
|
|
|
|
} else if (encoderMap[i] > 0) {
|
2020-01-20 22:47:36 -08:00
|
|
|
info.GetName(name, sizeof(name), " In", i);
|
2019-09-23 00:24:10 -07:00
|
|
|
if (auto simDevice = HALSIM_GetEncoderSimDevice(encoderMap[i] - 1)) {
|
|
|
|
|
LabelSimDevice(name, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
|
|
|
|
ImGui::LabelText(name, "Encoder[%d,%d]",
|
|
|
|
|
HALSIM_GetEncoderDigitalChannelA(encoderMap[i] - 1),
|
|
|
|
|
HALSIM_GetEncoderDigitalChannelB(encoderMap[i] - 1));
|
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
}
|
2019-11-02 15:26:08 -07:00
|
|
|
} else if (dutyCycleMap[i] > 0) {
|
2020-01-20 22:47:36 -08:00
|
|
|
info.GetName(name, sizeof(name), "Dty", i);
|
2019-11-02 15:26:08 -07:00
|
|
|
if (auto simDevice =
|
|
|
|
|
HALSIM_GetDutyCycleSimDevice(dutyCycleMap[i] - 1)) {
|
|
|
|
|
LabelSimDevice(name, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
double val = HALSIM_GetDutyCycleOutput(dutyCycleMap[i] - 1);
|
|
|
|
|
if (ImGui::InputDouble(name, &val))
|
|
|
|
|
HALSIM_SetDutyCycleOutput(dutyCycleMap[i] - 1, val);
|
|
|
|
|
}
|
2019-09-23 00:24:10 -07:00
|
|
|
} else if (!HALSIM_GetDIOIsInput(i)) {
|
2020-01-20 22:47:36 -08:00
|
|
|
info.GetName(name, sizeof(name), "Out", i);
|
2019-09-23 00:24:10 -07:00
|
|
|
if (auto simDevice = HALSIM_GetDIOSimDevice(i)) {
|
|
|
|
|
LabelSimDevice(name, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
ImGui::LabelText(name, "%s",
|
|
|
|
|
HALSIM_GetDIOValue(i) ? "1 (high)" : "0 (low)");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-01-20 22:47:36 -08:00
|
|
|
info.GetName(name, sizeof(name), " In", i);
|
2019-09-23 00:24:10 -07:00
|
|
|
if (auto simDevice = HALSIM_GetDIOSimDevice(i)) {
|
|
|
|
|
LabelSimDevice(name, simDevice);
|
|
|
|
|
} else {
|
|
|
|
|
static const char* options[] = {"0 (low)", "1 (high)"};
|
|
|
|
|
int val = HALSIM_GetDIOValue(i) ? 1 : 0;
|
|
|
|
|
if (ImGui::Combo(name, &val, options, 2)) HALSIM_SetDIOValue(i, val);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-20 22:47:36 -08:00
|
|
|
info.PopupEditName(i);
|
2019-09-23 00:24:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
if (!hasAny) ImGui::Text("No Digital I/O");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DIOGui::Initialize() {
|
2020-01-20 22:47:36 -08:00
|
|
|
gDIO.Initialize();
|
2019-09-23 00:24:10 -07:00
|
|
|
HALSimGui::AddWindow("DIO", DisplayDIO, ImGuiWindowFlags_AlwaysAutoResize);
|
|
|
|
|
HALSimGui::SetDefaultWindowPos("DIO", 470, 20);
|
|
|
|
|
}
|