Skip to content

Commit c6983ac

Browse files
author
masira
committed
add gdb c++ example
1 parent 0463328 commit c6983ac

20 files changed

+1990
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(skanjet)
3+
4+
#set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_CXX_STANDARD 14)
6+
set(CMAKE_CXX_FLAGS -pthread)
7+
#find_package(Boost REQUIRED COMPONENTS
8+
## system
9+
## thread
10+
## ${USE_Boost_LIBRARIES}
11+
## log
12+
# )
13+
14+
15+
set(PROJECT_SOURCE_PATH ${CMAKE_SOURCE_DIR})
16+
17+
#---------------------------------------#
18+
# set(SRC_FILES_FOR_CONFIG
19+
# ${PROJECT_SOURCE_PATH}/configuration/mahda_modbus_config.cpp)
20+
21+
# set(SRC_FILES_FOR_LIB_MODBUS
22+
# ${PROJECT_SOURCE_PATH}/lib/modbus/mahda_modbus_master.cpp
23+
# ${PROJECT_SOURCE_PATH}/lib/modbus/mahda_modbus_slave.cpp)
24+
25+
26+
# set(SRC_FILES_FOR_LIB_MAHDA_LOGGER
27+
# ${PROJECT_SOURCE_PATH}/lib/mahda_logger/mahda_logger.cpp)
28+
29+
#---------------------------------------#
30+
# set(INCLUDE_PATH_FOR_CONFIG
31+
# ${PROJECT_SOURCE_PATH}/configuration/)
32+
33+
# set(INCLUDE_PATH_FOR_LIB_MAHDA_CORE
34+
# ${PROJECT_SOURCE_PATH}/lib/mahda_core
35+
# )
36+
37+
# set(INCLUDE_PATH_FOR_LIB_MAHDA_LOGGER
38+
# ${PROJECT_SOURCE_PATH}/lib/mahda_logger)
39+
40+
set(INCLUDE_PATH_FOR_LIB_MODBUS
41+
${PROJECT_SOURCE_PATH}/lib/modbus
42+
)
43+
44+
45+
macro(createCmakeSettingsForMainTarget target)
46+
47+
target_sources(${target} PRIVATE
48+
49+
${SRC_FILES_FOR_CONFIG}
50+
${SRC_FILES_FOR_LIB_MODBUS}
51+
${SRC_FILES_FOR_LIB_MAHDA_LOGGER}
52+
# applications/app0_timers/app_timers.cpp
53+
app4.cpp
54+
# applications/app2_modbus_slave/app_modbus_slave.cpp
55+
# applications/app3_modbus_master/app_modbus_master.cpp
56+
)
57+
58+
59+
target_include_directories(${target} PRIVATE
60+
# ${Boost_INCLUDE_DIRS}
61+
.
62+
${INCLUDE_PATH_FOR_CONFIG}
63+
${INCLUDE_PATH_FOR_LIB_MODBUS}
64+
${INCLUDE_PATH_FOR_LIB_MAHDA_LOGGER}
65+
# applications/app_timers
66+
67+
# applications/app2_modbus_slave
68+
# applications/app3_modbus_master
69+
# lib/lib_matrix
70+
71+
72+
)
73+
74+
target_include_directories(${target} PUBLIC
75+
$<BUILD_INTERFACE:/usr/local/include/modbus
76+
77+
# -DBOOST_LOG_DYN_LINK
78+
)
79+
80+
target_compile_options(${target} PUBLIC
81+
-L/usr/local/lib
82+
# -Llib_matrix
83+
# -lfahw
84+
# -w # Disables all warnings
85+
# -Wno-pointer-to-int-cast # Disables only "pointer-to-int-cast" warning
86+
)
87+
88+
89+
target_link_libraries(${target}
90+
modbus
91+
# fahw
92+
# ${Boost_SYSTEM_LIBRARY}
93+
# ${Boost_THREAD_LIBRARY}
94+
# ${Boost_LOG_LIBRARY}
95+
# boost_log_setup
96+
# boost_log
97+
)
98+
endmacro()
99+
100+
set(main_target skanjet_demo)
101+
add_executable(${main_target} main.cpp)
102+
createCmakeSettingsForMainTarget(${main_target})
103+
#target_sources(${main_target} PRIVATE
104+
105+
# )
106+
107+
108+
109+

example01_cpp_threading_gdb/app4.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "app4.h"
2+
3+
4+
5+
AppType4::AppType4(const char *app_name) :
6+
app_name_(const_cast<char *>(app_name)) {
7+
std::cout << "Application started executing init function." << std::endl;
8+
app_init();
9+
}
10+
11+
AppType4::~AppType4() {
12+
std::cout << "Application started executing exit function." << std::endl;
13+
app_exit();
14+
}
15+
16+
void AppType4::app_init() {
17+
18+
}
19+
20+
void AppType4::app_loop(std::atomic<bool> &running) {
21+
while(running) {
22+
std::cout << "This is loop function." << std::endl;
23+
std::this_thread::sleep_for(std::chrono::seconds(1));
24+
}
25+
}
26+
27+
void AppType4::app_exit() {
28+
29+
}

example01_cpp_threading_gdb/app4.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#ifndef _APP4_H
3+
#define _APP4_H
4+
5+
#include "main.h"
6+
7+
class AppType4 {
8+
private:
9+
char *app_name_;
10+
11+
private:
12+
void app_init();
13+
14+
void app_exit();
15+
16+
public:
17+
void app_loop(std::atomic<bool> &running);
18+
19+
AppType4(const char *app_name);
20+
21+
~AppType4();
22+
23+
};
24+
25+
26+
#endif //_MAHDA_USER_APP1_H
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "app_timers.h"
2+
3+
MahdaMainSharedData *shared_data_;
4+
5+
6+
//uint8_t dest1 = 0;
7+
//bool gg1 = false;
8+
//uint8_t dest2 = 0;
9+
//bool gg2 = false;
10+
11+
void check_function2() {
12+
// if(master->modbus_read_bits_by_address(0x01, 1, &dest2)) {
13+
// std::cout << "bit1=" << (int) dest2 << std::endl;
14+
// }
15+
// gg2 = !gg2;
16+
// master->modbus_write_bit_by_address(0x01, gg2);
17+
18+
if (shared_data_->modbus_master != nullptr) {
19+
if (shared_data_->modbus_master->modbus_write_float_registers_by_address(0x160, 1.05)) {
20+
std::cout << "write ok" << std::endl;
21+
}
22+
}
23+
24+
}
25+
26+
void check_function1() {
27+
// if (master->modbus_read_bits_by_address(0x00, 1, &dest1)) {
28+
// std::cout << "bit0=" << (int) dest1 << std::endl;
29+
// }
30+
// gg1 = !gg1;
31+
// master->modbus_write_bit_by_address(0x00, gg1);
32+
33+
// master->test_illegal_address();
34+
35+
if (shared_data_->modbus_master != nullptr) {
36+
auto out = shared_data_->modbus_master->modbus_read_float_registers_by_address(0x160);
37+
std::cout << "read float=" << out << std::endl;
38+
}
39+
40+
}
41+
42+
AppType0::AppType0(const char *app_name, MahdaLogger *log_ptr, MahdaMainSharedData *shared_data) :
43+
log_ptr_(log_ptr),
44+
app_name_(const_cast<char *>(app_name)) {
45+
shared_data_ = shared_data;
46+
log_ptr_->log_info(app_name_, "Application started executing init function.");
47+
app_init();
48+
log_ptr_->log_info(app_name_, "Application finished executing init function.");
49+
}
50+
51+
AppType0::~AppType0() {
52+
log_ptr_->log_info(app_name_, "Application started executing exit function.");
53+
app_exit();
54+
log_ptr_->log_info(app_name_, "Application finished executing exit function.");
55+
}
56+
57+
void AppType0::app_init() {
58+
timer1 = new MahdaSafeTimer(10, 1000, check_function1);
59+
timer2 = new MahdaSafeTimer(100, 2000, check_function2);
60+
timer1->start();
61+
timer2->start();
62+
}
63+
64+
void AppType0::app_exit() {
65+
delete timer1;
66+
delete timer2;
67+
std::cout << "disconnected!" << std::endl;
68+
}
69+
70+
71+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#ifndef APP0_TIMERS_H
3+
#define APP0_TIMERS_H
4+
5+
#include "main.h"
6+
7+
void check_function1();
8+
void check_function2();
9+
10+
/*
11+
* Modbus Master App
12+
*/
13+
14+
class AppType0 {
15+
private:
16+
MahdaLogger *log_ptr_;
17+
char *app_name_;
18+
19+
20+
21+
private:
22+
void app_init();
23+
24+
void app_exit();
25+
26+
27+
public:
28+
MahdaSafeTimer *timer1;
29+
MahdaSafeTimer *timer2;
30+
31+
32+
33+
AppType0(const char *app_name,
34+
MahdaLogger *log_ptr,
35+
MahdaMainSharedData *shared_data);
36+
37+
~AppType0();
38+
39+
};
40+
#endif //_MODBUS_MASTER_APP_H
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "mahda_user_app1.h"
2+
3+
4+
5+
AppType1::AppType1(const char *app_name, MahdaLogger *log_ptr, MahdaMainSharedData *shared_data) :
6+
log_ptr_(log_ptr),
7+
app_name_(const_cast<char *>(app_name)),
8+
shared_data_(shared_data) {
9+
log_ptr_->log_info(app_name_, "Application started executing init function.");
10+
app_init();
11+
log_ptr_->log_info(app_name_, "Application finished executing init function.");
12+
}
13+
14+
AppType1::~AppType1() {
15+
log_ptr_->log_info(app_name_, "Application started executing exit function.");
16+
app_exit();
17+
log_ptr_->log_info(app_name_, "Application finished executing exit function.");
18+
}
19+
20+
void AppType1::app_init() {
21+
22+
}
23+
24+
void AppType1::app_loop(std::atomic<bool> &running) {
25+
while(running) {
26+
log_ptr_->log_info(app_name_, "This is loop function.");
27+
std::this_thread::sleep_for(std::chrono::seconds(1));
28+
}
29+
}
30+
31+
void AppType1::app_exit() {
32+
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#ifndef _MAHDA_USER_APP1_H
3+
#define _MAHDA_USER_APP1_H
4+
5+
#include "main.h"
6+
7+
class AppType1 {
8+
private:
9+
MahdaLogger *log_ptr_;
10+
char *app_name_;
11+
MahdaMainSharedData *shared_data_;
12+
13+
private:
14+
void app_init();
15+
16+
void app_exit();
17+
18+
public:
19+
void app_loop(std::atomic<bool> &running);
20+
21+
AppType1(const char *app_name,
22+
MahdaLogger *log_ptr,
23+
MahdaMainSharedData *shared_data);
24+
25+
~AppType1();
26+
27+
};
28+
29+
30+
#endif //_MAHDA_USER_APP1_H

0 commit comments

Comments
 (0)