Deleting and re instantiating an arrayed struct globally from within a function

Full code here:
Edit: Due to members of this forum admonishing me for providing a link to my code on an external site I have removed it. I have asked my question elsewhere and will not be monitoring this page or using this forum in general.

Demo of early version, graphs not updating asynchronously:
Link removed.

Background: My goal is to write a library to allow a master Arduino to drive a slave Arduino via serial and print to a master-defined graph or graphs on an SSD1306 I2C display(no SPI version to test with). The graphing code is finished. Currently I can have 4 graphs that can update synchronously or asynchronously, there is no blanking and only writes the portions that need updating.

Both arduinos currently run the same sketch and determine their role via a pullup_input tied to ground, however in later versions the sketch will compile using if statements with a #defined boolean to greatly save on program space for the master arduino.

So far:
The actual graphing is working and the graph updates whenever a graphAdd(graphNumber, newVal); is called.

The graphs are to be defined in the master code as such:

struct graphStruct {
  uint8_t gx;
  uint8_t gy;
  uint8_t gw;
  uint8_t gh;
  int gVal;
  int graphBuffer[graphBufferSize];
  bool isReady;
};


#define graphNum 4                    //Set this to the number of Graphs you want. 
graphStruct graph[graphNum] = {       //Each set of brackets is an instance of a graph, one for each specified in graphNum
  //Graph 1                           //Usage: {LeftX, TopY, width, height}
  {0, 0, 31, 32},
  //Graph 2
  {32, 0, 31, 32},
  //Graph 3
  {64, 0, 31, 32},
  //Graph 4
  {96, 0, 31, 32},
};

This currently works to define the graphs when the program is running essentially "demo mode" (just doing a graphAdd(graphNumber, random(1024)); on the slave side).

Currently Working:

  • Slave awaits connection indefinitely
  • Master sends ready byte
  • Slave acks
  • Master sends number of graphs wanted
  • My problem is now instantiating a globally accessible array of structs from within a function as I don't want to have to pre-code the number of graphs.

Next Steps:
Writing structs over serial for graph position and dimension setup (I've done this before in the past, not too hard)
Setting up packets to send the graph data over. Again not to hard, essentially sending two ints like (graph#, graphData)
Add graph "titling" (like "ACC" or "Light intensity")

Implemented:
Graphing system
A serial "call - response" system and acknowledgement system.
Error Handling
Loops/Second counter

So what you're after is the ability to dynamically increase the size of your array based on user input or the run-time equivalent of plug-and-play? Or will the number of graphs be known at compile-time and you'd like to see the array constructed automatically based on some defined constant?

DKWatson:
So what you're after is the ability to dynamically increase the size of your array based on user input or the run-time equivalent of plug-and-play? Or will the number of graphs be known at compile-time and you'd like to see the array constructed automatically based on some defined constant?

Plug and play is exactly what I was thinking. In fact I plan on including the master code in the majority of my future personal projects for on-the-fly data output. I have a bunch of micro-usb breakouts that I will connect like:

USB->Master
VCC->VCC
D+ ->TX
D- ->RX
GND->GND
and I have a breadboard friendly USB header to go to the slave like
USB->SLAVE
VCC->VCC
D+ ->RX
D- ->TX
GND->GND

And have a conceptually disgusting but nicely shielded UART over USB-Cable setup.

Okay so you want to tread in the waters of dynamically allocating memory on a device with 2K SRAM? Your struct is 7 bytes plus 2 x graphBufferSize. How big is graphBufferSize and how many entries to expect in the array? Nad if you are limited by the size of SRAM, why don’t you just pre-allocate it at compile time and don’t get caught up in the mess that malloc will create?

DKWatson:
Okay so you want to tread in the waters of dynamically allocating memory on a device with 2K SRAM? Your struct is 7 bytes plus 2 x graphBufferSize. How big is graphBufferSize and how many entries to expect in the array? Nad if you are limited by the size of SRAM, why don't you just pre-allocate it at compile time and don't get caught up in the mess that malloc will create?

The total size of the graph buffers is equal to sum of all (graph widths + 2) in chars, even with four graphs that's ~180 chars. once the slave is running almost all of its dynamic memory can be used for the graph data as that's basically all it does and there aren't a lot of places where we are using so many local variables that the ram usage spikes. On the master the buffer array will not be compiled in. There is a lot that I'm going to be paring down long term.
As it is I have about 25% of my dynamic memory available at compile time and that's with both the slave and master globals living on them.

I have tested this with eight 15x30 pixel graphs and it runs fine.

Lol. I just figured out a stupid easy solution, I have the whole preamble as a function in setup and have it pass values and just instantiate the whole thing before I close setup()
I'll let you know how it works tomorrow.

Full code here:
Master-Slave Serial Grapher - Pastebin.com

That's where your question belongs, then.

If you want to ask your question here, where it is far more likely that you'll get answers, then post you code HERE. I, for one, will not fish your code out of the rubbish bin. I value my computer security more than that.

PaulS:
That's where your question belongs, then.

If you want to ask your question here, where it is far more likely that you'll get answers, then post you code HERE. I, for one, will not fish your code out of the rubbish bin. I value my computer security more than that.

Constructive criticism is appreciated. This is not. Unless you have something helpful to add, go somewhere else. My apologies for making ~500 lines of code available via a well known platform that allows easy viewing without a download.

LyfeOnEdge:
My apologies for making ~500 lines of code available via a well known platform that allows easy viewing without a download.

I think maybe the point is that those whose choose to expend their free time to assist others, do so more readily if that which needs reviewing is in front of them. If that time must however, be spent link-hopping in pursuit of offering assistance, well, that free time is probably better spent elsewhere or for other purposes.

DKWatson:
I think maybe the point is that those whose choose to expend their free time to assist others, do so more readily if that which needs reviewing is in front of them. If that time must however, be spent link-hopping in pursuit of offering assistance, well, that free time is probably better spent elsewhere or for other purposes.

It is one link, and it brings you directly to a web page with the full code with C++ Sytax highlighting. How is this worse than having to download the entire sketch, (which to counter Paul's argument is actually less safe than viewing it online) and opening an entirely separate text editor or the Arduino IDE?

LyfeOnEdge:
It is one link, and it brings you directly to a web page with the full code with C++ Sytax highlighting. How is this worse than having to download the entire sketch, (which to counter Paul's argument is actually less safe than viewing it online) and opening an entirely separate text editor or the Arduino IDE?

For me there are two issues

  • The site you have placed the code into is an ad driven site filled with additional tracking etc. I'm not in favour of opening a page and getting that sort of stuff in my face
  • If this issue gets resolved, people in the future will have the opportunity to see where in the code the issue is, and what the solution was. This remains true only as long as the original code is available. There are examples here that go back years, and the code is accessible and viewable even now. In the case where the code is stored elsewhere, the original will become unavailable it the site/account disappears.

Just my opinion :wink:

Fine. I'll go back to S/E where people don't get hung up that you posted your code to a publicly available website that has been around longer than Arduinos. About 3 years ago I had the misfortune of downloading a .ino from the arduino forums that was actually a disguised exe fork bomb. I for one don't like having to download other people's code to my local machine in order to view it. Peace.

How is this worse than having to download the entire sketch

I’d show you, except that you removed your incorrect link.

While at work, or connected to the company network at home, which I mostly am, I use a proxy server to access the internet. That proxy server disallows access to some sites. Mostly to porn sites, but also to sites KNOWN to host viruses, like pastebin.

So, the point is if you attach/post your code here, I can see it, because it has been scanned for viruses. If you post on pastebin, you might as well have posted on the back side of the moon, because I can not see your code.

How about this Paul? In the future if you see me post something: keep scrolling. I don’t want your help. Have fun in your work-proxy bubble. I would rather post code via pastebin and let people easily access it than require them to download it to their local machines. Your claim that pastebin hosts viruses is true. Many of them are in source-code format still. I’ve never seen plaintext execute itself through a web browser. Unlike the arduino forum, pastebin doesn’t require a copy of the code to live on your machine in order to view it. No one is obligated to help me. If you don’t want to help me, rather than be preachy about why you don’t want to help me you should go spend your time helping someone else.

How about this Paul? In the future if you see me post something: keep scrolling.

I can do that.

I would rather post code via pastebin and let some people easily access it

I fixed that for you.

No one is obligated to help me.

You've got that right. If you want help, you'll make it easy, though.