Go Down

Topic: how to get a array of structs modified by different functions? (Read 252 times) previous topic - next topic


Hello everybody!

Im trying to sort out a number of things here, lots of inputs/outputs, each on different i2c device and pin number, each affecting / affected by different things, like a turn indicator right button activates the turn indicator right light function.

I would prefer a global array of struct and be able to modify it freely from a few different classes.

To examplify here; im working on a "prototype" for my main project, just focusing on the inputs here and most of the functions and members are just placeholders or rudimentary code, but this should give you an idea of what im doing here..

File: test_buttons.ino
Code: [Select]

#include "structs.h"
#include "definitions.h"
#include "testClass.h"

testClass test;

struct magicButton Button[] = { // {device,pin,vector, isPressed, clickCount, clickType.
  {0,0,_one,false,0,0},   {0,1,_two,false,0,0},   {0,2,_three,false,0,0},   {0,3,_four,false,0,0},
  {1,0,_five,false,0,0},  {1,1,_six,false,0,0},   {1,2,_seven,false,0,0},   {1,3,_eight,false,0,0}

void setup() {
  // put your setup code here, to run once:

void loop() {
  Button = test.doButtons(Button, sizeof(Button));

File: structs.h
Code: [Select]

#ifndef _structs_h
#define _structs_h

struct magicButton {
  uint8_t device;     // i2c device address
  uint8_t pin;          // the pin number
  uint8_t vector;     // what vehicle light function to affect
  bool isPressed;
  uint8_t clickCount; // counts each if(isPressed), to debounce and return clickType when !isPressed
  uint8_t clickType;   // if 1 = shortclicked, 2 = longclicked. returns to 0 after read.
#endif // end _structs_h

File: testClass.cpp
Code: [Select]

//  a test-class to manage button stats
#include "testClass.h"
#include "definitions.h"

struct magicButton testClass::doButtons(struct magicButton btn[], uint8_t numBtns) {
  for (uint8_t i = 0; i < numBtns; i++) {
    if(btn[i].isPressed) {
  return &btn;

File: testClass.h
Code: [Select]

#ifndef _testClass_h
#define _testClass_h
#include "Arduino.h"
#include "structs.h"
class testClass {
  struct magicButton doButtons(struct magicButton btn[], uint8_t numBtns);
  uint8_t _numBtns = 9;

#endif // end _testClass_h

As you can see, im playing around with the framework, focusing on how to access data containers more than on the actual data itself, and i am getting build errors when i try this exact code.

So i know i am doing it wrong and therefore im asking here, how should i go about having different classes read and/or write to/from a global array of structs?


I don't know much about struct yet, but check the last example of the following webpage, I believe it is what you're looking for:
"I don't think a success factor of a novel is determined by the tools used by the writer, it can be notepad, ms word, or just pencil and paper, but CREATIVITY IS." ~DuckfaceNinja, Construct2 Forum user


you declare magicButton as a type, use it as such:

Code: [Select]
magicButton doButtons(struct magicButton btn[], uint8_t numBtns);

But also make sure that this struct is known to your testClass.h - so import it from somewhere else or declare it there.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.


Im very grateful for the assistance here, but i eventually figured that it should be just as neat having a global struct that "authorized" classes can manipulate directly.

Im defining my struct in a separate file, then use extern to point out the instance.

That part seem to work well, now its instead some other part of the data management that isnt quite accepting all these new situations.. :P

..im trying to learn pointers as they seem to be the answer to my prayers, but progress is slow and often totally in the blind..

Go Up