Skip to content

Commit a6687e2

Browse files
TD01TD01
TD01
authored and
TD01
committed
callback function examples added for VC++ SDK
1 parent 1e9a3cb commit a6687e2

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

VC++/MPLibCode.cpp

+65-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,76 @@
77
// Function Prorotypes
88
s32 func1(const s32 A1, const s32 A2);
99

10-
// 主step函数,执行周期 5 ms
11-
void step(void) { // 周期 = 5 ms
10+
// Variables defintions
11+
TMPVarInt NewVariable1;
1212

13+
// Timers defintions
14+
TMPTimerMS NewTimer1;
15+
16+
// 主step函数,执行周期 500 ms
17+
void step(void) { // 周期 = 500 ms
18+
log("step function every 500ms");
19+
}
20+
21+
// CAN报文接收事件 "NewOn_CAN_Rx1" 针对标识符 = 0x123 (FD)
22+
void on_canfd_rx_NewOn_CAN_Rx1(const PCANFD ACANFD) { // 针对标识符 = 0x123 (FD)
23+
log("CAN frame 0x123 has been received");
24+
}
25+
26+
// CAN报文发送成功事件 "NewOn_CAN_Tx1" 针对标识符 = 0x123 (FD)
27+
void on_canfd_tx_NewOn_CAN_Tx1(const PCANFD ACANFD) { // 针对标识符 = 0x123 (FD)
28+
log("CAN frame 0x123 has been transmitted successfully");
29+
}
30+
31+
// CAN报文预发送事件 "NewOn_CAN_PreTx1" 针对标识符 = 0x123 (FD)
32+
void on_canfd_pretx_NewOn_CAN_PreTx1(const PCANFD ACANFD) { // 针对标识符 = 0x123 (FD)
33+
log("CAN frame 0x123 is being transmitted, you can modify its content before sending out");
34+
}
35+
36+
// LIN报文接收事件 "NewOn_LIN_Rx1" 针对标识符 = 0x12
37+
void on_lin_rx_NewOn_LIN_Rx1(const PLIN ALIN) { // 针对标识符 = 0x12
38+
log("LIN frame 0x12 has been received");
39+
}
40+
41+
// LIN报文发送成功事件 "NewOn_LIN_Tx1" 针对标识符 = 0x12
42+
void on_lin_tx_NewOn_LIN_Tx1(const PLIN ALIN) { // 针对标识符 = 0x12
43+
log("LIN frame 0x12 has been transmitted successfully");
44+
}
45+
46+
// LIN报文预发送事件 "NewOn_LIN_PreTx1" 针对标识符 = 0x12
47+
void on_lin_pretx_NewOn_LIN_PreTx1(const PLIN ALIN) { // 针对标识符 = 0x12
48+
log("LIN frame 0x12 is being transmitted, you can modify its content before sending out");
49+
}
50+
51+
// 变量变化事件 "NewOn_Var_Change1" 针对变量 "NewVariable1"
52+
void on_var_change_NewOn_Var_Change1(void) { // 变量 = NewVariable1
53+
log("NewVariable1 has been changed to %d", NewVariable1.get());
54+
}
55+
56+
// 定时器触发事件 "NewOn_Timer1" for Timer NewTimer1
57+
void on_timer_NewOn_Timer1(void) { // 定时器 = NewTimer1
58+
log("Timer 100ms fired");
59+
}
60+
61+
// 启动事件 "NewOn_Start1"
62+
void on_start_NewOn_Start1(void) { // 程序启动事件
63+
log("TSMaster mini program is starting...");
64+
NewTimer1.start();
65+
}
66+
67+
// 停止事件 "NewOn_Stop1"
68+
void on_stop_NewOn_Stop1(void) { // 程序停止事件
69+
log("TSMaster mini program is stopped");
70+
}
71+
72+
// 快捷键事件 "NewOn_Shortcut1" 快捷键 = Ctrl+R
73+
void on_shortcut_NewOn_Shortcut1(const s32 AShortcut) { // 快捷键事件 = Ctrl+R
74+
log("You have pressed Ctrl + R short-cut key");
1375
}
1476

1577
// 自定义函数 "func1"
1678
s32 func1(const s32 A1, const s32 A2) { // 自定义函数
17-
79+
log("Custom function is called with result = %d", A1 + A2);
1880
return A1 + A2;
1981

2082
}

VC++/MPLibCodeExtern.cpp

+47-2
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,30 @@ DLLEXPORT s32 __stdcall finalize_miniprogram(void)
118118

119119
// MP library functions definition
120120

121+
// Variables defintions
122+
extern TMPVarInt NewVariable1;
123+
124+
// Timers defintions
125+
extern TMPTimerMS NewTimer1;
126+
121127
// Retrieve TSMP abilities
122128
typedef s32 (__stdcall* TRegTSMasterFunction)(const void* AObj, const char* AFuncType, const char* AFuncName, const char* AData, const void* AFuncPointer, const char* ADescription);
123129
extern void step(void);
130+
extern void on_canfd_rx_NewOn_CAN_Rx1(const PCANFD ACANFD);
131+
extern void on_canfd_tx_NewOn_CAN_Tx1(const PCANFD ACANFD);
132+
extern void on_canfd_pretx_NewOn_CAN_PreTx1(const PCANFD ACANFD);
133+
extern void on_lin_rx_NewOn_LIN_Rx1(const PLIN ALIN);
134+
extern void on_lin_tx_NewOn_LIN_Tx1(const PLIN ALIN);
135+
extern void on_lin_pretx_NewOn_LIN_PreTx1(const PLIN ALIN);
136+
extern void on_var_change_NewOn_Var_Change1(void);
137+
extern void on_timer_NewOn_Timer1(void);
138+
extern void on_start_NewOn_Start1(void);
139+
extern void on_stop_NewOn_Stop1(void);
140+
extern void on_shortcut_NewOn_Shortcut1(const s32 AShortcut);
124141
// extern your custom function definitions here
125142
extern s32 func1(const s32 A1, const s32 A2);
126143
DLLEXPORT s32 __stdcall retrieve_mp_abilities(const void* AObj, const TRegTSMasterFunction AReg) {
144+
// struct size check, do not modify ======================================================================================================================================
127145
#define TSMASTER_VERSION "2021.6.22.581" // do not modify this version as it depends on TSMaster implementation !
128146
if (!AReg(AObj, "check_mp_internal", "version", TSMASTER_VERSION, 0, "")) return -1;
129147
if (!AReg(AObj, "check_mp_internal", "struct_size", "struct_size_app", (void *)sizeof(TTSMasterConfiguration), "")) return -1;
@@ -138,8 +156,35 @@ DLLEXPORT s32 __stdcall retrieve_mp_abilities(const void* AObj, const TRegTSMast
138156
if (!AReg(AObj, "check_mp_internal", "struct_size", "struct_size_TMPVarLIN", (void *)sizeof(TMPVarLIN), "")) return -1;
139157
if (!AReg(AObj, "check_mp_internal", "struct_size", "struct_size_TLIBTSMapping", (void *)sizeof(TLIBTSMapping), "")) return -1;
140158
if (!AReg(AObj, "check_mp_internal", "struct_size", "struct_size_TLIBSystemVarDef", (void *)sizeof(TLIBSystemVarDef), "")) return -1;
141-
if (!AReg(AObj, "step_function", "step", "5", &step, "")) return -1;
142-
// add your custom functions here
159+
// step function will run with 500 ms interval ============================================================================================================================
160+
if (!AReg(AObj, "step_function", "step", "500", &step, "")) return -1;
161+
// Mini program variables will be registered here, 0: integer; 1: double; 2: string; 3: TCAN; 4: TCANFD; 5: TLIN ==========================================================
162+
if (!AReg(AObj, "var", "NewVariable1", "0", &NewVariable1, "")) return -1;
163+
// Timers can be defined here, 100 means interval = 100ms =================================================================================================================
164+
if (!AReg(AObj, "timer", "NewTimer1", "100", &NewTimer1, "")) return -1;
165+
// CAN receive done callback will be registered here, 291: identifier; -1: standard frame and 0 means extended frame; -1: can fd frame and 0 means classical can frame ====
166+
if (!AReg(AObj, "on_can_rx_callback", "on_canfd_rx_NewOn_CAN_Rx1", "291,-1,-1", &on_canfd_rx_NewOn_CAN_Rx1, "")) return -1;
167+
// CAN transmit done callback will be registered here, 291: identifier; -1: standard frame and 0 means extended frame; -1: can fd frame and 0 means classical can frame ===
168+
if (!AReg(AObj, "on_can_tx_callback", "on_canfd_tx_NewOn_CAN_Tx1", "291,-1,-1", &on_canfd_tx_NewOn_CAN_Tx1, "")) return -1;
169+
// CAN pre-transmit callback will be registered here, 291: identifier; -1: standard frame and 0 means extended frame; -1: can fd frame and 0 means classical can frame ====
170+
if (!AReg(AObj, "on_can_pretx_callback", "on_canfd_pretx_NewOn_CAN_PreTx1", "291,-1,-1", &on_canfd_pretx_NewOn_CAN_PreTx1, "")) return -1;
171+
// LIN receive done callback will be registered here, 18: identifier ======================================================================================================
172+
if (!AReg(AObj, "on_lin_rx_callback", "on_lin_rx_NewOn_LIN_Rx1", "18", &on_lin_rx_NewOn_LIN_Rx1, "")) return -1;
173+
// LIN transmit done callback will be registered here, 18: identifier =====================================================================================================
174+
if (!AReg(AObj, "on_lin_tx_callback", "on_lin_tx_NewOn_LIN_Tx1", "18", &on_lin_tx_NewOn_LIN_Tx1, "")) return -1;
175+
// LIN pre-transmit callback will be registered here, 18: identifier ======================================================================================================
176+
if (!AReg(AObj, "on_lin_pretx_callback", "on_lin_pretx_NewOn_LIN_PreTx1", "18", &on_lin_pretx_NewOn_LIN_PreTx1, "")) return -1;
177+
// Mini program variable change callback will be registered here ==========================================================================================================
178+
if (!AReg(AObj, "on_var_change_callback", "on_var_change_NewOn_Var_Change1", "NewVariable1", &on_var_change_NewOn_Var_Change1, "")) return -1;
179+
// Timer overflow callback will be registered here ========================================================================================================================
180+
if (!AReg(AObj, "on_timer_callback", "on_timer_NewOn_Timer1", "NewTimer1", &on_timer_NewOn_Timer1, "")) return -1;
181+
// Mini program start callback will be registered here ====================================================================================================================
182+
if (!AReg(AObj, "on_start_callback", "on_start_NewOn_Start1", "", &on_start_NewOn_Start1, "")) return -1;
183+
// Mini program stop callback will be registered here =====================================================================================================================
184+
if (!AReg(AObj, "on_stop_callback", "on_stop_NewOn_Stop1", "", &on_stop_NewOn_Stop1, "")) return -1;
185+
// Short-cut key callback will be registered here =========================================================================================================================
186+
if (!AReg(AObj, "on_shortcut_callback", "on_shortcut_NewOn_Shortcut1", "Ctrl+R", &on_shortcut_NewOn_Shortcut1, "")) return -1;
187+
// add your custom functions here =========================================================================================================================================
143188
if (!AReg(AObj, "on_custom_callback", "func1", "const s32 A1, const s32 A2", &func1, "this is a demo api")) return -1;
144189
// MP library functions
145190

0 commit comments

Comments
 (0)