Proper code design

Hello,
i'm not sure how to properly architect the code in this situation.

This is rtos type system, where i want to create a few objects that perform actions based on the events.
I think that once "setup" function completes, the "eventGroup" variable becomes inaccessible from "outlet" object.

xEventGroupCreate() is the API function described here:
https://www.freertos.org/xEventGroupCreate.html
event_groups.h

EventGroupHandle_t xEventGroupCreate( void );
Creates a new RTOS event group, and returns a handle by which the newly created event group can be referenced.


Question:
what is the proper way of proceeding in this situation?

1
2
3
4
5
6
7
8
void setup() {
...
//	Create event Group for use by all tasks
	eventGroup = xEventGroupCreate(); //check if NULL NOT returned.
...
	Zap* outlet = new Zap(DATA_PIN, &eventGroup,bit_time, "Task UNO") ; // make a new  Zap instance
...
}

Last edited on
I think that once "setup" function completes, the "eventGroup" variable becomes inaccessible from "outlet" object.
I couldn't say. I don't see in which scope eventGroup is declared. If it's a local then yeah, obviously the object from line 6 will point to an object that no longer exists, if it continues to exist after the function returns. It's the same as doing this:
1
2
3
4
5
6
7
8
9
10
struct A{
    int *a;
};

A *f(){
    int x = 42;
    return new A{&x};
}

std::cout << *(f()->a) << std::endl;


Is Zip your class? Why are you passing a pointer to handle? Just pass the handle.
Helios,
thank you for the reply.
To answer your question:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
// other includes
#include <zap.h>

EventGroupHandle_t eventGroup;

void setup() {
...
//	Create event Group for use by all tasks
	eventGroup = xEventGroupCreate(); //check if NULL NOT returned.
...
	Zap* outlet = new Zap(DATA_PIN, &eventGroup,bit_time, "Task UNO") ; // make a new  Zap instance
...
}


Zap is declared in zap.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <Outlet.h>

class Zap : public Outlet {
  private:
	int _channel = 1 ; //CHANNEL 1
	//int DATA_PIN = 4;
	EventGroupHandle_t _evntgrp ;
	int _time_bit ; // = BIT0 ;
	TickType_t _t_outlet_cycle_ticks = 10000; //t_outlet_cycle/portTICK_RATE_MS ;
	int _stackDepth = 4048;

 protected:
	TimerHandle_t _handle;

 public:
	Zap( int pin, EventGroupHandle_t evgrp, int time_bit,  const char* TaskName, int ch=0, TickType_t cycle_ms = 10000 );

	static void 	stTask(void* param) ;
	void 			ToggleTask();
	void 			callback();


"outlet.h" is the c library

as you see im referring to eventgroup in the object.

Is Zip your class? Why are you passing a pointer to handle? Just pass the handle.

I was thinking that it might not be available, since "setup" has completed.


<removed the ZAP implementation as irrelevant to the discussion


Last edited on
If that's how Zap is declared then this new Zap(DATA_PIN, &eventGroup,bit_time, "Task UNO") ; is clearly wrong. Zap is expecting an EventGroupHandle_t as the second parameter, yet you're passing a EventGroupHandle_t *.

I was thinking that it might not be available, since "setup" has completed.
How would whatever framework you're using know that one of your functions has returned?
yes, it was clearly wrong. Thank you for pointing out. The code works now.

How would whatever framework you're using know that one of your functions has returned?

Sorry, i'm not following... Mind to rephrase, please ?
Last edited on
Topic archived. No new replies allowed.