First define a simplified Event Data Structure
Typedef struct {
unsigned char event_flg;
unsigned char event_cnt_buf[event_flg_wide];
void (*pEventCallback[event_flg_wide])(void);
} Event;
If your events are complex, you can continue to add object members to the data structure, but I will not do this. I will abstract an event class and then derive richer child classes by inheritance.
Event triggering is implemented as follows:
Event. cpp
# Include <iostream> # include "event. H "using namespace STD; event msg_event; void msg_event_init (void) {}// register the message response function void event_install_callback (unsigned char fn_index, void PFN (void )) {If (fn_index <event_flg_wide) {response [fn_index] = PFN ;}// uninstall the message response function void event_uninstall_callback (unsigned char fn_index) {If (fn_index <event_flg_wide) {msg_event.peventcallback [fn_index] = NULL ;}} void Close_all_msg (event event_t) {unsigned char I = 0; event_t.event_flg = 0; for (I = 0; I <event_flg_wide; I ++) event_t.event_cnt_buf [I] = 0; for (I = 0; I <event_flg_wide; I ++) event_t.peventcallback [I] = NULL;} void msg_event_rountine (void) {cout <"msg_event_rountine! "<Endl;} // message event main thread void msg_event_main_thread (void) {unsigned char I; // If (msg_event.event_flg) {// accumulated message entries for (I = 0; I <event_flg_wide; I ++) {If (msg_event.event_flg & (1 <I) {msg_event.event_flg & = (~ (1 <I); msg_event.event_cnt_buf [I] ++ ;}}} else {// turn off the load} // execute the message response for (I = 0; I <event_flg_wide; I ++) {If (msg_event.event_cnt_buf [I]) {If (msg_event.peventcallback! = NULL) {msg_event.peventcallback [I] (); msg_event.event_cnt_buf [I] --; break ;}}}}
Header file event. h
#ifndef EVENT_H#define EVENT_H#ifndef FALSE #define FALSE 0x00u#endif#ifndef TRUE #define TRUE 0x01u#endif#ifndef NULL #define NULL 0x00u#endif#define event_flg_wide 8typedef struct{ unsigned char event_flg; unsigned char event_cnt_buf[event_flg_wide]; void (*pEventCallback[event_flg_wide])(void);}event;extern event msg_event;void msg_event_init(void);void msg_event_main_thread(void);void event_install_callback(unsigned char fn_index, void pfn(void));void event_uninstall_callback(unsigned char fn_index);void msg_event_rountine(void);void close_all_msg(event event_t );#endif // EVENT_H
Register two events in the main function and trigger it.
Msg_event_init ();
event_install_callback(0,msg_event_rountine);
event_install_callback(1,msg_event_rountine);
msg_event.event_flg |= (1<<0)|(1<<1);
while(1){
msg_event_main_thread();
}
Test Engineering placement on Network Disk http://pan.baidu.com/s/1gdy2dhT