mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 01:18:36 +02:00
Device: RAD-Galaxy: Add support for Analog Output
This commit is contained in:
committed by
Kyle Schwarz
parent
6cda765fe0
commit
d18ca9e6eb
@@ -14,6 +14,7 @@ option(LIBICSNEO_BUILD_CPP_APP_ERROR_EXAMPLE "Build the macsec example" ON)
|
||||
option(LIBICSNEO_BUILD_CPP_FLEXRAY_EXAMPLE "Build the FlexRay example." ON)
|
||||
option(LIBICSNEO_BUILD_CPP_SPI_EXAMPLE "Build the SPI example." ON)
|
||||
option(LIBICSNEO_BUILD_CPP_MUTEX_EXAMPLE "Build the NetworkMutex example." ON)
|
||||
option(LIBICSNEO_BUILD_CPP_ANALOG_OUT_EXAMPLE "Build the analog output example." ON)
|
||||
|
||||
add_compile_options(${LIBICSNEO_COMPILER_WARNINGS})
|
||||
|
||||
@@ -80,3 +81,7 @@ endif()
|
||||
if(LIBICSNEO_BUILD_CPP_MUTEX_EXAMPLE)
|
||||
add_subdirectory(cpp/mutex)
|
||||
endif()
|
||||
|
||||
if(LIBICSNEO_BUILD_CPP_ANALOG_OUT_EXAMPLE)
|
||||
add_subdirectory(cpp/analog_out)
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
add_executable(libicsneocpp-analog-out src/analog_out.cpp)
|
||||
target_link_libraries(libicsneocpp-analog-out icsneocpp)
|
||||
@@ -0,0 +1,157 @@
|
||||
/**
|
||||
* libicsneo Analog Output example
|
||||
*
|
||||
* Demonstrates how to configure and control analog outputs on supported devices
|
||||
*
|
||||
* Usage: libicsneo-analog-out <pin> <voltage> [deviceSerial] [--yes]
|
||||
*
|
||||
* Arguments:
|
||||
* pin: Pin number (1-3 for RAD Galaxy)
|
||||
* voltage: Voltage level (0-5)
|
||||
* deviceSerial: 6 character string for device serial (optional)
|
||||
* --yes: Skip confirmation prompt
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <string_view>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "icsneo/icsneocpp.h"
|
||||
|
||||
static const std::string usage = "Usage: libicsneo-analog-out <pin> <voltage> [deviceSerial] [--yes]\n\n"
|
||||
"Arguments:\n"
|
||||
"pin: Pin number (1-3 for RAD Galaxy)\n"
|
||||
"voltage: Voltage level (0-5)\n"
|
||||
"deviceSerial: 6 character string for device serial (optional)\n"
|
||||
"--yes: Skip confirmation prompt\n";
|
||||
|
||||
int main(int argc, const char** argv) {
|
||||
std::vector<std::string_view> args(argv, argv + argc);
|
||||
|
||||
// Parse arguments
|
||||
if(args.size() < 3) {
|
||||
std::cerr << "Error: Missing required arguments\n" << std::endl;
|
||||
std::cerr << usage;
|
||||
return -1;
|
||||
}
|
||||
|
||||
char* endPtr;
|
||||
long pinNum = std::strtol(args[1].data(), &endPtr, 10);
|
||||
if(endPtr != args[1].data() + args[1].size() || pinNum < 1 || pinNum > 3) {
|
||||
std::cerr << "Error: Invalid pin number (must be 1-3)" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
long voltageLevel = std::strtol(args[2].data(), &endPtr, 10);
|
||||
if(endPtr != args[2].data() + args[2].size() || voltageLevel < 0 || voltageLevel > 5) {
|
||||
std::cerr << "Error: Invalid voltage level (must be 0-5)" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
icsneo::MiscIOAnalogVoltage voltage = static_cast<icsneo::MiscIOAnalogVoltage>(voltageLevel);
|
||||
uint8_t pin = static_cast<uint8_t>(pinNum);
|
||||
|
||||
// Check for optional arguments
|
||||
bool skipConfirm = false;
|
||||
std::string_view serial;
|
||||
for(size_t i = 3; i < args.size(); i++) {
|
||||
if(args[i] == "--yes") {
|
||||
skipConfirm = true;
|
||||
} else if(serial.empty() && args[i].size() == 6) {
|
||||
serial = args[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Confirmation prompt
|
||||
if(!skipConfirm) {
|
||||
std::cout << "WARNING: This will set analog output pin " << static_cast<int>(pin)
|
||||
<< " to " << voltageLevel << "V" << std::endl;
|
||||
std::cout << "Make sure nothing sensitive is connected to this pin." << std::endl;
|
||||
std::cout << "Continue? (yes/no): ";
|
||||
std::string response;
|
||||
std::getline(std::cin, response);
|
||||
if(response != "yes") {
|
||||
std::cout << "Aborted." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<icsneo::Device> device = nullptr;
|
||||
|
||||
if(!serial.empty()) {
|
||||
// Find device by serial
|
||||
auto devices = icsneo::FindAllDevices();
|
||||
for(auto& dev : devices) {
|
||||
if(dev->getSerial() == serial) {
|
||||
device = dev;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!device) {
|
||||
std::cerr << "Device with serial " << serial << " not found" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
// Use first available device
|
||||
auto devices = icsneo::FindAllDevices();
|
||||
if(devices.empty()) {
|
||||
std::cerr << "No devices found" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
device = devices[0];
|
||||
}
|
||||
|
||||
std::cout << "Using device: " << device->describe() << std::endl;
|
||||
|
||||
if(!device->open()) {
|
||||
std::cerr << "Failed to open device" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto settings = device->settings;
|
||||
if(!settings) {
|
||||
std::cerr << "Device settings not available" << std::endl;
|
||||
device->close();
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::cout << "Refreshing device settings..." << std::endl;
|
||||
if(!settings->refresh()) {
|
||||
std::cerr << "Failed to refresh settings" << std::endl;
|
||||
device->close();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Enable analog output on specified pin
|
||||
std::cout << "Enabling analog output on pin " << static_cast<int>(pin) << "..." << std::endl;
|
||||
if(!settings->setMiscIOAnalogOutputEnabled(pin, true)) {
|
||||
std::cerr << "Failed to enable analog output on pin " << static_cast<int>(pin) << std::endl;
|
||||
device->close();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set pin to specified voltage
|
||||
std::cout << "Setting pin " << static_cast<int>(pin) << " to " << voltageLevel << "V..." << std::endl;
|
||||
if(!settings->setMiscIOAnalogOutput(pin, voltage)) {
|
||||
std::cerr << "Failed to set voltage on pin " << static_cast<int>(pin) << std::endl;
|
||||
device->close();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Apply settings
|
||||
std::cout << "Applying settings..." << std::endl;
|
||||
if(!settings->apply()) {
|
||||
std::cerr << "Failed to apply settings" << std::endl;
|
||||
device->close();
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::cout << "Analog output configured successfully!" << std::endl;
|
||||
std::cout << "Pin " << static_cast<int>(pin) << ": Enabled at " << voltageLevel << "V" << std::endl;
|
||||
|
||||
device->close();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
"""
|
||||
Basic analog output control example using icsneopy library.
|
||||
|
||||
Demonstrates how to configure and control analog outputs on supported devices.
|
||||
|
||||
Usage: python analog_out_basic.py <pin> <voltage> [--yes]
|
||||
|
||||
Arguments:
|
||||
pin: Pin number (1-3 for RAD Galaxy)
|
||||
voltage: Voltage level (0-5)
|
||||
--yes: Skip confirmation prompt
|
||||
"""
|
||||
|
||||
import sys
|
||||
import icsneopy
|
||||
|
||||
|
||||
def analog_output_example(pin: int, voltage: int, skip_confirm: bool = False):
|
||||
"""Configure and control analog outputs."""
|
||||
# Confirmation prompt
|
||||
if not skip_confirm:
|
||||
print(f"WARNING: This will set analog output pin {pin} to {voltage}V")
|
||||
print("Make sure nothing sensitive is connected to this pin.")
|
||||
response = input("Continue? (yes/no): ")
|
||||
if response.lower() != "yes":
|
||||
print("Aborted.")
|
||||
return
|
||||
|
||||
devices = icsneopy.find_all_devices()
|
||||
if not devices:
|
||||
raise RuntimeError("No devices found")
|
||||
|
||||
device = devices[0]
|
||||
|
||||
try:
|
||||
if not device.open():
|
||||
raise RuntimeError("Failed to open device")
|
||||
|
||||
settings = device.settings
|
||||
if not settings:
|
||||
raise RuntimeError("Device settings not available")
|
||||
|
||||
print("Refreshing device settings...")
|
||||
if not settings.refresh():
|
||||
raise RuntimeError("Failed to refresh settings")
|
||||
|
||||
# Enable analog output on specified pin
|
||||
print(f"Enabling analog output on pin {pin}...")
|
||||
if not settings.set_misc_io_analog_output_enabled(pin, True):
|
||||
raise RuntimeError(f"Failed to enable analog output on pin {pin}")
|
||||
|
||||
# Map voltage level to enum
|
||||
voltage_map = {
|
||||
0: icsneopy.Settings.MiscIOAnalogVoltage.V0,
|
||||
1: icsneopy.Settings.MiscIOAnalogVoltage.V1,
|
||||
2: icsneopy.Settings.MiscIOAnalogVoltage.V2,
|
||||
3: icsneopy.Settings.MiscIOAnalogVoltage.V3,
|
||||
4: icsneopy.Settings.MiscIOAnalogVoltage.V4,
|
||||
5: icsneopy.Settings.MiscIOAnalogVoltage.V5
|
||||
}
|
||||
voltage_enum = voltage_map[voltage]
|
||||
|
||||
# Set pin to specified voltage
|
||||
print(f"Setting pin {pin} to {voltage}V...")
|
||||
if not settings.set_misc_io_analog_output(pin, voltage_enum):
|
||||
raise RuntimeError(f"Failed to set voltage on pin {pin}")
|
||||
|
||||
# Apply settings
|
||||
print("Applying settings...")
|
||||
if not settings.apply():
|
||||
raise RuntimeError("Failed to apply settings")
|
||||
|
||||
print("Analog output configured successfully!")
|
||||
print(f"Pin {pin}: Enabled at {voltage}V")
|
||||
|
||||
finally:
|
||||
device.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 3:
|
||||
print("Error: Missing required arguments\n")
|
||||
print("Usage: python analog_out_basic.py <pin> <voltage> [--yes]")
|
||||
print("\nArguments:")
|
||||
print(" pin: Pin number (1-3 for RAD Galaxy)")
|
||||
print(" voltage: Voltage level (0-5)")
|
||||
print(" --yes: Skip confirmation prompt")
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
pin = int(sys.argv[1])
|
||||
if pin < 1 or pin > 3:
|
||||
print("Error: Invalid pin number (must be 1-3)")
|
||||
sys.exit(1)
|
||||
|
||||
voltage = int(sys.argv[2])
|
||||
if voltage < 0 or voltage > 5:
|
||||
print("Error: Invalid voltage level (must be 0-5)")
|
||||
sys.exit(1)
|
||||
|
||||
skip_confirm = "--yes" in sys.argv
|
||||
|
||||
analog_output_example(pin, voltage, skip_confirm)
|
||||
|
||||
except ValueError:
|
||||
print("Error: Pin and voltage must be integers")
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user