How do I create member function to represent embedded task in C++

The following code is an example of how task are created with micro cos III in c. I am trying to figure how to create similliar code in C++. My problem is how do I instantiate objects and how to use member functions to represent task. Within the create task routine the address of function is passed as argument. How do I do this in C++? Will I need more than one class? New to embedded C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265


/*!
* @brief LED Flasher Task
*/
void
led5_task (void * p_arg)
{
    OS_ERR  err;


    (void)p_arg;    // NOTE: Silence compiler warning about unused param.

    for (;;)
    {
        // Flash LED at 1 Hz.
	    protectedLED_Toggle(5);
	    OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, &err);
    }
}

/*!
*
* @brief LED Flasher Task
*
*/
void
led6_task (void * p_arg)
{
    OS_ERR  err;


    (void)p_arg;    // NOTE: Silence compiler warning about unused param.

    for (;;)
    {
        // Flash LED at 3 Hz.
	    protectedLED_Toggle(6);
	    OSTimeDlyHMSM(0, 0, 0, 167, OS_OPT_TIME_HMSM_STRICT, &err);
    }
}

/*!
* @brief Button SW1 Catcher Task
*/
void
sw1_task (void * p_arg)
{
    uint16_t    sw1_counter = 0;
    char	    p_str[LCD_CHARS_PER_LINE+1];
    OS_ERR	    err;
	

    (void)p_arg;    // NOTE: Silence compiler warning about unused param.

    // Draw the initial display.
	sprintf(p_str, "SW1: % 4u", sw1_counter);
    protectedDisplayLCD(LCD_LINE1, (uint8_t *) p_str);

    for (;;)
    {
        // Wait for a signal from the button debouncer.
	    OSSemPend(&g_sw1_sem, 0, OS_OPT_PEND_BLOCKING, 0, &err);

        // Check for errors.
	    assert(OS_ERR_NONE == err);
		
        // Increment button press counter.
	    sw1_counter++;

        // Format and display current count.
	    sprintf(p_str, "SW1: % 4u", sw1_counter);
        protectedDisplayLCD(LCD_LINE1, (uint8_t *) p_str);
    }
}

/*!
* @brief Button SW2 Catcher Task
*/
void
sw2_task (void * p_arg)
{
    uint16_t    sw2_counter = 0;
    char	    p_str[LCD_CHARS_PER_LINE+1];
    OS_ERR	    err;
	

    (void)p_arg;    // NOTE: Silence compiler warning about unused param.

    // Draw the initial display.
	sprintf(p_str, "SW2: % 4u", sw2_counter);
    protectedDisplayLCD(LCD_LINE2, (uint8_t *) p_str);

    for (;;)
    {
        // Wait for a signal from the button debouncer.
	    OSSemPend(&g_sw2_sem, 0, OS_OPT_PEND_BLOCKING, 0, &err);

        // Check for errors.
	    assert(OS_ERR_NONE == err);
		
        // Increment button press counter.
	    sw2_counter++;

        // Format and display current count.
	    sprintf(p_str, "SW2: % 4u", sw2_counter);
        protectedDisplayLCD(LCD_LINE2, (uint8_t *) p_str);
    }
}

/*!
* @brief A task to create all of the other tasks and their shared objects.
*/
void
startup_task (void * p_arg)
{
    OS_ERR   err;


    (void)p_arg;    // NOTE: Silence compiler warning about unused param.

    // Perform hardware initializations that should be after multitasking.
    BSP_Init();
    CPU_Init();
    OS_CPU_TickInit();

    // Initialize the reentrant LED driver.
    protectedLED_Init();

    // Create the LED flasher tasks.
    OSTaskCreate((OS_TCB     *)&g_led5_tcb,
                 (CPU_CHAR   *)"LED5 Flasher",
                 (OS_TASK_PTR ) led5_task,
                 (void       *) 0,
                 (OS_PRIO     ) LED5_PRIO,
                 (CPU_STK    *)&g_led5_stack[0],
                 (CPU_STK_SIZE) TASK_STACK_SIZE / 10u,
                 (CPU_STK_SIZE) TASK_STACK_SIZE,
                 (OS_MSG_QTY  ) 0u,
                 (OS_TICK     ) 0u,
                 (void       *) 0,
                 (OS_OPT      ) 0,
                 (OS_ERR     *)&err);
    assert(OS_ERR_NONE == err);

    OSTaskCreate((OS_TCB     *)&g_led6_tcb,
                 (CPU_CHAR   *)"LED6 Flasher",
                 (OS_TASK_PTR ) led6_task,
                 (void       *) 0,
                 (OS_PRIO     ) LED6_PRIO,
                 (CPU_STK    *)&g_led6_stack[0],
                 (CPU_STK_SIZE) TASK_STACK_SIZE / 10u,
                 (CPU_STK_SIZE) TASK_STACK_SIZE,
                 (OS_MSG_QTY  ) 0u,
                 (OS_TICK     ) 0u,
                 (void       *) 0,
                 (OS_OPT      ) 0,
                 (OS_ERR     *)&err);
    assert(OS_ERR_NONE == err);

    // Create the semaphores signaled by the button debouncer.
    OSSemCreate(&g_sw1_sem, "Switch 1", 0, &err);
    assert(OS_ERR_NONE == err);	

    OSSemCreate(&g_sw2_sem, "Switch 2", 0, &err);
    assert(OS_ERR_NONE == err);	

    // Create the button debouncer.
    OSTaskCreate((OS_TCB     *)&g_debounce_tcb,
                 (CPU_CHAR   *)"Button Debouncer",
                 (OS_TASK_PTR ) debounce_task,
                 (void       *) 0,
                 (OS_PRIO     ) DEBOUNCE_PRIO,
                 (CPU_STK    *)&g_debounce_stack[0],
                 (CPU_STK_SIZE) TASK_STACK_SIZE / 10u,
                 (CPU_STK_SIZE) TASK_STACK_SIZE,
                 (OS_MSG_QTY  ) 0u,
                 (OS_TICK     ) 0u,
                 (void       *) 0,
                 (OS_OPT      ) 0,
                 (OS_ERR     *)&err);
    assert(OS_ERR_NONE == err);

    // Initialise the LCD driver.
    protectedInitialiseLCD();

    // Create the tasks to catch the button semaphores.
    OSTaskCreate((OS_TCB     *)&g_sw1_tcb,
                 (CPU_CHAR   *)"Button 1 Catcher",
                 (OS_TASK_PTR ) sw1_task,
                 (void       *) 0,
                 (OS_PRIO     ) SW1_PRIO,
                 (CPU_STK    *)&g_sw1_stack[0],
                 (CPU_STK_SIZE) TASK_STACK_SIZE / 10u,
                 (CPU_STK_SIZE) TASK_STACK_SIZE,
                 (OS_MSG_QTY  ) 0u,
                 (OS_TICK     ) 0u,
                 (void       *) 0,
                 (OS_OPT      ) 0,
                 (OS_ERR     *)&err);
    assert(OS_ERR_NONE == err);

    OSTaskCreate((OS_TCB     *)&g_sw2_tcb,
                 (CPU_CHAR   *)"Button 2 Catcher",
                 (OS_TASK_PTR ) sw2_task,
                 (void       *) 0,
                 (OS_PRIO     ) SW2_PRIO,
                 (CPU_STK    *)&g_sw2_stack[0],
                 (CPU_STK_SIZE) TASK_STACK_SIZE / 10u,
                 (CPU_STK_SIZE) TASK_STACK_SIZE,
                 (OS_MSG_QTY  ) 0u,
                 (OS_TICK     ) 0u,
                 (void       *) 0,
                 (OS_OPT      ) 0,
                 (OS_ERR     *)&err);
    assert(OS_ERR_NONE == err);

    // Delete the startup task (or enter an infinite loop like other tasks).
    OSTaskDel(NULL, &err);

    // We should never get here.
    assert(0);
}

/*!
* @brief The starting point for the entire C program.
*/
void
main (void)
{
    OS_ERR  err;


    // Disable all interrupts.
    CPU_IntDis();

    // Initialize the operating system's internal data structures.
    OSInit(&err);
    assert(OS_ERR_NONE == err);

    // Install application-specific OS hooks.
    App_OS_SetAllHooks();

    // Create the alarm task.
    OSTaskCreate((OS_TCB     *)&g_startup_tcb,
                 (CPU_CHAR   *)"Startup Task",
                 (OS_TASK_PTR ) startup_task,
                 (void       *) 0,
                 (OS_PRIO     ) STARTUP_PRIO,
                 (CPU_STK    *)&g_startup_stack[0],
                 (CPU_STK_SIZE) TASK_STACK_SIZE / 10u,
                 (CPU_STK_SIZE) TASK_STACK_SIZE ,
                 (OS_MSG_QTY  ) 0u,
                 (OS_TICK     ) 0u,
                 (void       *) 0,
                 (OS_OPT      ) 0,
                 (OS_ERR     *)&err);
    assert(OS_ERR_NONE == err);

    // Start multitasking.
    OSStart(&err);

    // We should never get here.
    assert(0);
}
How do I do this in C++?


It would appear you're using a C API so you would do it the same way.
What I would like to know is how do I create member functions to represent task in C++ what type of objects will I need?
Topic archived. No new replies allowed.