Veethree D7 SDK
Loading...
Searching...
No Matches
System library examples

Using the vsdk::sys::board_info class

The example code in cli-board-info.cpp demonstrates getting the serial number of the SoC.

CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(
board-info-app
VERSION 0.1
LANGUAGES CXX)
# Set C++ standard to C++17
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
# Project name
set(APPNAME cli-board-info-app)
# Find VSDK packages
find_package(vsdk REQUIRED CONFIG COMPONENTS system utils)
# Add executable and link against VSDK libraries
add_executable(${APPNAME} cli-board-info.cpp)
target_link_libraries(${APPNAME} PRIVATE vsdk::system vsdk::utils)
## Deploy to target from Qt Creator
include(GNUInstallDirs)
install(TARGETS ${APPNAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
@ system
System module.
Definition log.h:39
Any entities that belong to the utils library.
Any entities that belong to the Veethree SDK libraries.
Definition can_adapter.h:12
cli-board-info.cpp
using namespace vsdk::sys;
int main() {
fmt::println("Serial number: {}", board_info::soc_serial_number());
}
board_info interface
static std::string soc_serial_number()
Get the board SoC serial number.
Add include this file to your file so you have access to fmtlib header-only implementation.
Any entities that belong to the system library.

Using the display backlight manager

The example code in cli-brightness.cpp demonstrates:

  • getting the current display brightness value as a percentage
  • setting the display brightness value as a percentage
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(
brightness-app
VERSION 0.1
LANGUAGES CXX)
# Set C++ standard to C++17
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
# Project name
set(APPNAME cli-brightness-app)
# Find VSDK packages
find_package(vsdk REQUIRED CONFIG COMPONENTS system utils)
# Add executable and link against VSDK libraries
add_executable(${APPNAME} cli-brightness.cpp)
target_link_libraries(${APPNAME} PRIVATE vsdk::system vsdk::utils)
## Deploy to target from Qt Creator
include(GNUInstallDirs)
install(TARGETS ${APPNAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
cli-brightness.cpp
#include <charconv>
#include <string_view>
#include <system_error>
using namespace vsdk::sys;
void print_usage(const std::string &program_name) {
fmt::println("Usage: {} [ brightness 0-100 ]", program_name);
fmt::println("Example: {} 50 # Sets display brightness to 50%", program_name);
}
int main(int argc, char **argv) {
if (argc < 2) {
print_usage(argv[0]);
return EXIT_FAILURE;
}
// The range of acceptable values is 0-100,
// but the set_brightness() call won't fail with other values.
//
// Try with values > 100.
// Note: Negative values cannot be used at all since the type is unsigned.
uint32_t new_brightness = 0;
std::string_view arg(argv[1]);
if (const auto res =
std::from_chars(arg.data(), arg.data() + arg.size(), new_brightness);
res.ec != std::errc{}) {
fmt::println("Invalid brightness value: {}",
std::make_error_code(res.ec).message());
print_usage(argv[0]);
return EXIT_FAILURE;
}
// Getting the current brightness
const auto current_brightness = backlight_manager::brightness();
fmt::println("Current brightness is: {}", current_brightness);
// Setting the new brightness
fmt::println("New brightness set: {}", backlight_manager::brightness());
}
static void set_brightness(uint32_t value)
Set the brightness level.
static uint32_t brightness()
Get the current brightness level.
backlight_manager interface.

Using the onboard buzzer

The example code in cli-buzzer.cpp demonstrates:

  • using the buzzer's default configuration to make a single 200ms long noise (void ringBuzzer)
  • changing the pitch for the buzzer by providing a custom configuration (void playCMajor)
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(
cli-buzzer-app
VERSION 0.1
LANGUAGES CXX)
# Set C++ standard to C++17
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
# Project name
set(APPNAME cli-buzzer-app)
# Find VSDK packages
find_package(vsdk REQUIRED CONFIG COMPONENTS system utils)
# Add executable and link against VSDK libraries
add_executable(${APPNAME} cli-buzzer.cpp)
target_link_libraries(${APPNAME} PRIVATE vsdk::system vsdk::utils)
## Deploy to target from Qt Creator
include(GNUInstallDirs)
install(TARGETS ${APPNAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
cli-buzzer.cpp
#include <cmath>
#include <string>
#include <system/buzzer.h>
#include <thread>
#include <vector>
#include <fmt/format.h>
using namespace std::chrono_literals;
using namespace vsdk::sys;
// Rings the buzzer for 200ms using the default configuration
void ringBuzzer() {
std::this_thread::sleep_for(200ms);
}
uint32_t freqToPeriod(double freq) {
// Hz to ns
return static_cast<uint32_t>(std::pow(freq, -1.0) * 1'000'000'000);
}
// Plays the C Major arpeggio to demonstrate how to change the pitch
// of the buzzer
void playCMajor() {
// c_maj contains frequencies (Hz)
const auto c_maj = std::vector{261.625, 329.628, 391.995, 523.25};
for (const auto note : c_maj) {
const auto period = freqToPeriod(note);
// the frequency are specified in ns
buzzer::buzzer_enable(true, {.period = period,
.duty_cycle = 0.5, // 50% duty cycle
.boost = buzzer_boost::no_boost});
std::this_thread::sleep_for(100ms);
}
}
int main(int argc, char **argv) {
if (argc > 1 && std::string("-c") == std::string(argv[1])) {
playCMajor();
fmt::println("Played C Major");
} else {
ringBuzzer();
fmt::println("Rang buzzer for 200ms");
}
}
buzzer interface
static void buzzer_enable(bool enable, buzzer_boost level=buzzer_boost::no_boost)
Enable or disable the buzzer, using default definitions for buzzer_config.

Using the vsdk::sys::bootloader_manager class

The example code in cli-uboot-env-vars.cpp demonstrates setting and getting a bootloader environment variable with an arbitrary string value.

CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(
uboot-env-vars-app
VERSION 0.1
LANGUAGES CXX)
# Set C++ standard to C++17
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
# Project name
set(APPNAME cli-uboot-env-vars-app)
# Find VSDK packages
find_package(vsdk REQUIRED CONFIG COMPONENTS system utils)
# Add executable and link against VSDK libraries
add_executable(${APPNAME} cli-uboot-env-vars.cpp)
target_link_libraries(${APPNAME} PRIVATE vsdk::system vsdk::utils)
## Deploy to target from Qt Creator
include(GNUInstallDirs)
install(TARGETS ${APPNAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
cli-uboot-env-vars.cpp
#include <cstdio>
#include <fmt/core.h>
using namespace vsdk::sys;
int main() {
// Setting and getting an arbitrary variable with an arbitrary value. I.e.
// an_arbitrary_var="an arbitrary value"
constexpr auto var_name = "an_abitrary_var";
bootloader_manager::set_value(var_name, "an arbitrary value");
std::string value;
fmt::println("{}: {}", var_name, value);
}
bootloader_manager interface
static void get_value(std::string_view var, uint64_t &value)
Get a bootloader variable value.
static void set_value(std::string_view var, uint64_t value, bool hex=false)
Set a bootloader variable value.

Using the vsdk::sys::otp_manager class

The example code in cli-otp.cpp demonstrates reading OTP (One-Time Programmable) registers using the VSDK system library.

Features

  • Read OTP registers: Read single or multiple 32-bit words from OTP memory

Usage Examples

# Read a single word (e.g., MAC address at 0x2c4)
./cli-otp-app read 0x2c4 2 # (8 Bytes)
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(
cli-otp-app
VERSION 0.1
LANGUAGES CXX)
# Set C++ standard to C++17
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
# Project name
set(APPNAME cli-otp-app)
# Find VSDK packages
find_package(vsdk REQUIRED CONFIG COMPONENTS system utils)
# Add executable and link against VSDK libraries
add_executable(${APPNAME} cli-otp.cpp)
target_link_libraries(${APPNAME} PRIVATE vsdk::system vsdk::utils)
## Deploy to target from Qt Creator
include(GNUInstallDirs)
install(TARGETS ${APPNAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
cli-otp.cpp
#include <vector>
#include <iostream>
#include <string>
using namespace vsdk::sys;
bool parse_hex_or_dec(const std::string& str, uint32_t& value) {
try {
value = std::stoul(str, nullptr, 0); // Auto-detect hex/decimal
return true;
} catch (const std::exception&) {
return false;
}
}
int cmd_read(int argc, char* argv[]) {
if (argc < 4) {
fmt::println("Error: read requires <word_addr> <num_words>");
return 1;
}
uint32_t word_addr, num_words;
if (!parse_hex_or_dec(argv[2], word_addr) || !parse_hex_or_dec(argv[3], num_words)) {
fmt::println("Error: Invalid address or count format");
return 1;
}
if (num_words == 0 || num_words > 64) {
fmt::println("Error: num_words must be between 1 and 64");
return 1;
}
fmt::println("Reading {} words starting from address 0x{:x}...", num_words, word_addr);
std::vector<uint32_t> data;
auto result = otp_manager::read(word_addr, num_words, data);
if (result == otp_result::success) {
fmt::println("Read successful:");
for (size_t i = 0; i < data.size(); i++) {
fmt::println(" Word[0x{:x}] = 0x{:08x}", word_addr + i, data[i]);
}
return 0;
} else {
fmt::println("Read failed: {}", to_string(result));
return 1;
}
}
int main(int argc, char* argv[]) {
fmt::println("VSDK OTP Register Example");
fmt::println("==================================");
if (argc < 2) {
fmt::println("Error: No command provided");
return 1;
}
std::string command = argv[1];
try {
if (command == "read") {
return cmd_read(argc, argv);
} else {
fmt::println("Error: Unknown command '{}'", command);
return 1;
}
} catch (const std::exception& e) {
fmt::println("Error: {}", e.what());
return 1;
}
}
static otp_result read(uint32_t word_address, size_t num_words, std::vector< uint32_t > &data)
Read OTP register words.
std::string to_string(otp_result result)
Convert OTP result to string representation.
Definition otp_common.h:34
OTP Common Definitions.
OTP register manager interface.