Skip to content

Commit

Permalink
Merge branch 'feature/sample' into 'develop'
Browse files Browse the repository at this point in the history
update sample code

See merge request OrbbecSDK/openorbbecsdk!30
  • Loading branch information
戴因 committed Jul 5, 2024
2 parents 1e51fae + 575d540 commit d7cd3ca
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 174 deletions.
24 changes: 12 additions & 12 deletions examples/0.basic.enumerate/enumerate.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include <libobsensor/ObSensor.hpp>

#include "utils.hpp"
#include "utils_opencv.hpp"

#include <iostream>

// get input option
int getInputOption() {
std::string selected;
std::cin >> selected;
if(selected == "q" || selected == "Q") {
char inputOption = ob_sample_utils::waitForKeyPressed();
if(inputOption == ESC_KEY) {
return -1;
}
return std::stoul(selected);
return inputOption - '0';
}

// Print stream profile information.
Expand All @@ -27,7 +27,7 @@ void printStreamProfile(std::shared_ptr<ob::StreamProfile> profile, uint32_t ind
// Get the fps.
auto fps = videoProfile->getFps();
std::cout << index << "."
<< "format: " << formatName << ", "
<< "format: " << ob::TypeHelper::convertOBFormatTypeToString(formatName) << ", "
<< "revolution: " << width << "*" << height << ", "
<< "fps: " << fps << std::endl;
}
Expand All @@ -39,7 +39,7 @@ void printAccelProfile(std::shared_ptr<ob::StreamProfile> profile, uint32_t inde
// Get the rate of accel.
auto accRate = accProfile->getSampleRate();
std::cout << index << "."
<< "acc rate: " << accRate << std::endl;
<< "acc rate: " << ob::TypeHelper::convertOBIMUSampleRateTypeToString(accRate) << std::endl;
}

// Print gyro profile information.
Expand Down Expand Up @@ -87,13 +87,13 @@ void enumerateSensors(std::shared_ptr<ob::Device> device) {
// Get the sensor type.
auto sensorType = sensorList->getSensorType(index);
std::cout << " - " << index << "."
<< "sensor type: " << sensorType << std::endl;
<< "sensor type: " << ob::TypeHelper::convertOBSensorTypeToString(sensorType) << std::endl;
}

std::cout << "Select a sensor to enumerate its streams(input sensor index or \'q\' to enumerate device): " << std::endl;
std::cout << "Select a sensor to enumerate its streams(input sensor index or \'ESC\' to enumerate device): " << std::endl;

// Select a sensor.
int sensorSelected = getInputOption();
int sensorSelected = ob_sample_utils::getInputOption();
if(sensorSelected == -1) {
break;
}
Expand All @@ -119,7 +119,7 @@ int main(void) try {

std::cout << "enumerated devices: " << std::endl;

std::shared_ptr<ob::Device> device = nullptr;
std::shared_ptr<ob::Device> device = nullptr;
std::shared_ptr<ob::DeviceInfo> deviceInfo = nullptr;
for(uint32_t index = 0; index < deviceList->getCount(); index++) {
// Get device from deviceList.
Expand All @@ -130,10 +130,10 @@ int main(void) try {
<< std::endl;
}

std::cout << "enumerate sensors of device (input device index or \'q\' to exit program):" << std::endl;
std::cout << "enumerate sensors of device (input device index or \'ESC\' to exit program):" <<std::endl;

// select a device.
int deviceSelected = getInputOption();
int deviceSelected = ob_sample_utils::getInputOption();
if(deviceSelected == -1) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/0.basic.quick_start/quick_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main(void) try {
pipe.start();

// Create a window for rendering, and set the size of the window.
Window app("quick start", 1280, 480, RENDER_ONE_ROW);
Window app("quick start", 1280, 720, RENDER_ONE_ROW);

while(app) {
// Wait for frameSet from the pipeline, the default timeout is 1000ms.
Expand Down
2 changes: 1 addition & 1 deletion examples/1.stream.callback/callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(void) try {
if(sensorType == OB_SENSOR_IR || sensorType == OB_SENSOR_IR_LEFT || sensorType == OB_SENSOR_IR_RIGHT || sensorType == OB_SENSOR_COLOR
|| sensorType == OB_SENSOR_DEPTH) {
// Enable the stream with specified requirements.
config->enableVideoStream(convertSensorTypeToStreamType(sensorType));
config->enableVideoStream(ob::TypeHelper::convertSensorTypeToStreamType(sensorType));
}
}

Expand Down
5 changes: 3 additions & 2 deletions examples/1.stream.imu/imu.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include <libobsensor/ObSensor.hpp>

#include "utils.hpp"
#include "utils_key.h"

#include <mutex>
#include <iostream>

void printImuValue(OBFloat3D obFloat3d, uint64_t index, uint64_t timeStampUs, float temperature, OBFrameType type, const std::string &unitStr) {
std::cout << index << std::endl;
std::cout << "frame index: " <<index << std::endl;
std::cout << type << " Frame: \n\r{\n\r"
<< " tsp = " << timeStampUs << "\n\r"
<< " temperature = " << temperature << "\n\r"
Expand Down Expand Up @@ -38,7 +39,7 @@ int main() try {

while(true) {
auto key = ob_sample_utils::waitForKeyPressed(1);
if(key == 27) { // Esc key to exit.
if(key == ESC_KEY) { // Esc key to exit.
break;
}
auto frameSet = pipe.waitForFrameset();
Expand Down
2 changes: 1 addition & 1 deletion examples/1.stream.infrared/infrared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main() try {
OBSensorType sensorType = sensorList->getSensorType(index);
if(sensorType == OB_SENSOR_IR || sensorType == OB_SENSOR_IR_LEFT || sensorType == OB_SENSOR_IR_RIGHT) {
// Enable the stream with specified requirements.
config->enableVideoStream(convertSensorTypeToStreamType(sensorType), OB_WIDTH_ANY, OB_HEIGHT_ANY, 30, OB_FORMAT_ANY);
config->enableVideoStream(ob::TypeHelper::convertSensorTypeToStreamType(sensorType), OB_WIDTH_ANY, OB_HEIGHT_ANY, 30, OB_FORMAT_ANY);
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/1.stream.multi_streams/multi_streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int main(void) try {
continue;
}
// Get frame type based on sensor type.
auto streamType = convertSensorTypeToStreamType(sensorType);
auto streamType = ob::TypeHelper::convertSensorTypeToStreamType(sensorType);
// enable the stream.
config->enableVideoStream(streamType);
}
Expand Down
52 changes: 12 additions & 40 deletions examples/3.advanced.common_usages/common_usages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ void handleDeviceConnected(std::shared_ptr<ob::DeviceList> devices) {
pipeline = std::make_shared<ob::Pipeline>(device);
std::cout << "Open device success, SN: " << devices->getSerialNumber(0) << std::endl;

// try to switch depth work mode
switchDepthWorkMode();
// // try to switch depth work mode
// switchDepthWorkMode();

// try turn off hardware disparity to depth converter (switch to software d2d)
turnOffHwD2d();
// // try turn off hardware disparity to depth converter (switch to software d2d)
// turnOffHwD2d();

// set depth unit
setDepthUnit();
// // set depth unit
// setDepthUnit();

// set depth value range
setDepthValueRange();
// // set depth value range
// setDepthValueRange();

// set depth soft filter
setDepthSoftFilter();
// // set depth soft filter
// setDepthSoftFilter();

// start stream
// startStream();
startStream();
}
}

Expand Down Expand Up @@ -297,30 +297,6 @@ void startStream() {
}
}

// // Configure which streams to enable or disable for the Pipeline by creating a Config.
// std::shared_ptr<ob::Config> config = std::make_shared<ob::Config>();

// // Get device from pipeline.
// device = pipeline->getDevice();

// // Get sensorList from device.
// auto sensorList = device->getSensorList();

// for(uint32_t index = 0; index < sensorList->getCount(); index++) {
// // Query all supported infrared sensor type and enable the infrared stream.
// // For dual infrared device, enable the left and right infrared streams.
// // For single infrared device, enable the infrared stream.
// OBSensorType sensorType = sensorList->getSensorType(index);
// if(sensorType == OB_SENSOR_IR || sensorType == OB_SENSOR_IR_LEFT || sensorType == OB_SENSOR_IR_RIGHT || sensorType == OB_SENSOR_COLOR
// || sensorType == OB_SENSOR_DEPTH) {
// // Enable the stream with specified requirements.
// config->enableVideoStream(convertSensorTypeToStreamType(sensorType));

// // // colorProfile
// // auto sensor = sensorList->getSensor();
// }
// }

// start pipeline
pipeline->start(config, handleFrameset);
streamStarted = true;
Expand Down Expand Up @@ -798,7 +774,6 @@ void setColorGainValue(bool increase) {

void printUsage() {
std::cout << "Support commands:" << std::endl;
std::cout << " stream / s - (re)start stream" << std::endl;
std::cout << " info / i - get device information" << std::endl;
std::cout << " param / p - get camera parameter" << std::endl;
std::cout << " laser / l - on/off laser" << std::endl;
Expand All @@ -824,10 +799,7 @@ void printUsage() {
}

void commandProcess(std::string cmd) {
if(cmd == "stream" || cmd == "s") {
startStream();
}
else if(cmd == "info" || cmd == "i") {
if(cmd == "info" || cmd == "i") {
getDeviceInformation();
}
else if(cmd == "param" || cmd == "p") {
Expand Down
2 changes: 1 addition & 1 deletion examples/3.advanced.post_processing/post_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main() try {
pipe.start(config);

// Create a window for rendering, and set the resolution of the window
Window app("PostProcessing", 640, 480);
Window app("PostProcessing", 1280, 720);

while(app) {
// Wait for up to 1000ms for a frameset in blocking mode.
Expand Down
13 changes: 8 additions & 5 deletions examples/3.advanced.preset/preset.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <libobsensor/ObSensor.hpp>

#include <iostream>

#include "utils.hpp"

#include <iostream>

int main() try {
// Create a pipeline with default device.
ob::Pipeline pipe;
Expand All @@ -27,9 +27,8 @@ int main() try {
std::cout << "Enter index of preset to load: ";

// Select preset to load.
int loadIndex;
std::cin >> loadIndex;
auto presetName = presetLists->getName(loadIndex);
int inputOption = ob_sample_utils::getInputOption();
auto presetName = presetLists->getName(inputOption);

// Load preset.
device->loadPreset(presetName);
Expand All @@ -41,6 +40,10 @@ int main() try {
// Stop Pipeline.
pipe.stop();

printf("\nProgram ended successfully. Press any key to exit.");
getchar();
getchar();

return 0;
}
catch(ob::Error &e) {
Expand Down
1 change: 1 addition & 0 deletions examples/c_examples/0.c_quick_start/quick_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <libobsensor/ObSensor.h>

#include "utils_c.h"
#include "utils_key.h"

void calculate_and_print_frame_rate(const ob_frame *frameset) {
// Initialize variables
Expand Down
1 change: 1 addition & 0 deletions examples/c_examples/2.c_depth/depth.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <libobsensor/ObSensor.h>

#include "utils_c.h"
#include "utils_key.h"

// helper function to check for errors and exit if there is one
void check_ob_error(ob_error **err) {
Expand Down
22 changes: 21 additions & 1 deletion examples/common/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
#include "utils.hpp"
#include "utils_c.h"
#include "utils_key.h"

#include <chrono>

namespace ob_sample_utils {
char waitForKeyPressed(uint32_t timeout_ms) {
return ob_sample_utils_wait_for_key_press(timeout_ms);
}
} // namespace ob_sample_utils

uint64_t getNowTimesMs() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

int getInputOption() {
char inputOption = ob_sample_utils::waitForKeyPressed();
if(inputOption == ESC_KEY) {
return -1;
}
return inputOption - '0';
}

} // namespace ob_sample_utils

uint64_t ob_sample_utils_get_current_timestamp_ms(){
return ob_sample_utils::getNowTimesMs();
}
5 changes: 5 additions & 0 deletions examples/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@

namespace ob_sample_utils {
char waitForKeyPressed(uint32_t timeout_ms = 0);

uint64_t getNowTimesMs();

int getInputOption();

}
32 changes: 16 additions & 16 deletions examples/common/utils_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ int kbhit(void) {
}

#include <sys/time.h>
uint64_t ob_sample_utils_get_current_timestamp_ms() {
struct timeval te;
gettimeofday(&te, NULL); // 获取当前时间
long long milliseconds = te.tv_sec * 1000LL + te.tv_usec / 1000; // 计算毫秒
return milliseconds;
}
// uint64_t ob_sample_utils_get_current_timestamp_ms() {
// struct timeval te;
// gettimeofday(&te, NULL); // 获取当前时间
// long long milliseconds = te.tv_sec * 1000LL + te.tv_usec / 1000; // 计算毫秒
// return milliseconds;
// }
#else // Windows
#include <conio.h>
#include <windows.h>
uint64_t ob_sample_utils_get_current_timestamp_ms() {
FILETIME ft;
LARGE_INTEGER li;
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
long long milliseconds = li.QuadPart / 10000LL;
return milliseconds;
}
// uint64_t ob_sample_utils_get_current_timestamp_ms() {
// FILETIME ft;
// LARGE_INTEGER li;
// GetSystemTimeAsFileTime(&ft);
// li.LowPart = ft.dwLowDateTime;
// li.HighPart = ft.dwHighDateTime;
// long long milliseconds = li.QuadPart / 10000LL;
// return milliseconds;
// }

char ob_sample_utils_wait_for_key_press(uint32_t timeout_ms) {
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
Expand All @@ -96,7 +96,7 @@ char ob_sample_utils_wait_for_key_press(uint32_t timeout_ms) {
DWORD start_time = GetTickCount();
while(true) {
if(_kbhit()) {
char ch = (char)getch();
char ch = (char)_getch();
SetConsoleMode(hStdin, mode);
return ch;
}
Expand Down
2 changes: 0 additions & 2 deletions examples/common/utils_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
extern "C" {
#endif

#define ESC_KEY 27

/**
* @brief Get the current system timestamp in milliseconds.
*
Expand Down
10 changes: 10 additions & 0 deletions examples/common/utils_key.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#ifdef __cplusplus
extern "C" {
#endif

#define ESC_KEY 27

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit d7cd3ca

Please sign in to comment.