Arduino Forum

Using Arduino => Project Guidance => Topic started by: xarvox on May 24, 2019, 06:38 pm

Title: how to get a array of structs modified by different functions?
Post by: xarvox on May 24, 2019, 06:38 pm
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) {
      btn[i].clickCount++;
    }
  }
  return &btn;
}



File: testClass.h
Code: [Select]

#ifndef _testClass_h
#define _testClass_h
#include "Arduino.h"
#include "structs.h"
class testClass {
  public:
  struct magicButton doButtons(struct magicButton btn[], uint8_t numBtns);
 
  private:
  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?
Title: Re: how to get a array of structs modified by different functions?
Post by: F1_ on May 24, 2019, 09:52 pm
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:
https://www.programiz.com/c-programming/c-structures (https://www.programiz.com/c-programming/c-structures)
Title: Re: how to get a array of structs modified by different functions?
Post by: wvmarle on May 25, 2019, 11:29 am
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.
Title: Re: how to get a array of structs modified by different functions?
Post by: xarvox on May 25, 2019, 10:58 pm
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..