Home > front end >  Storing value in 2d array variable during runtime in c
Storing value in 2d array variable during runtime in c

Time:10-13

My main code is .ino file for STM32F103C8T6 with STM32 official core. I have also included my library files of .h file and .cpp file.

I want store a values in 2d array called uint32_t Page[15][14]; in .h file of my library

How to store a value in 2d array variable during runtime. I have posted my code below. For me the code below is perfect, but still did not print the value stored in the array by calling the function HatriX_Signal with correct parameters. Kindly let me know what is wrong in my code below.

// .ino file

#include "HATRIX.h"

HATRIX Hatrix;

void setup()
{
    Hatrix.HATRIX_INIT(115200);  
}

void loop()
{
    Hatrix.HatriX_Signal(Temperature_Signal, 0x66, Page[2][0]);
    Serial.println(Page[2][0]);
    Serial.println((float)Page[2][0] / 100);
}
// .h file


#ifndef HATRIX_H
#define HATRIX_H

#include "Arduino.h"
#include "Thermo_Couple_Input.h"

#define Number_of_Pages 15
#define Number_of_Bytes_Per_Page 64

static Thermo_Couple_Input TC_IN;
static uint32_t Page[Number_of_Pages][(Number_of_Bytes_Per_Page / 4) - 2];
enum Signal { Temperature_Signal,
                  Pressure_Signal,
                  Co2_Signal,
                  Analog_Industrial_Input_Signal,
                  General_Purpose_Output_Signal,
                  PWM_Power_Signal,
                  PWM_Voltage_Signal
                };


class HATRIX
{
  public:
    void HATRIX_INIT(uint32_t bauderate);
    void HatriX_Signal(uint8_t Signal, uint8_t I2C_Address, uint32_t Page);
};

#endif
// .cpp file


#include "HATRIX.h"

void HATRIX::HATRIX_INIT(uint32_t bauderate)
{
    Serial.begin(bauderate); 
}

// \brief Note: The value we get from Thermocouple is float value. So, we multiply the value with 100 and store it in specified static uint32_t Page by user.
// \param enum_Signal choose accordingly from enum signal: { Temperature_Signal ,
//                                                           Pressure_Signal ,
//                                                           Hatrix.Co2_Signal ,
//                                                           Analog_Industrial_Input_Signal ,
//                                                           General_Purpose_Output_Signal ,
//                                                           PWM_Power_Signal ,
//                                                           PWM_Voltage_Signal };
// \param I2C_Address Address of I2C to get the temperature from desired Thermocouple.
// \param Page Give the Page details to store your value. example: Hatrix.Page[4][3]
void HATRIX::HatriX_Signal(uint8_t signal, uint8_t I2C_Address, uint32_t Page) 
{
    if(signal == Temperature_Signal);
    {
        TC_IN.Thermo_Couple_Input_Channel(I2C_Address);
        Serial.println("Page");
        Page = fThermocoupleTemperature * 100;
        Serial.println(Page);
    }
}

CodePudding user response:

When calling HATRIX::HatriX_Signal(...), you pass a copy of a uint32_t of the multidimensional array Hatrix.Page to the function.

Inside the HATRIX::HatriX_Signal(...) function, you assign a new value to this variable called Page. But since it's just a copy of the value from the array, the array itself won't be effected by this.

In order to get the value of the function you can either use a pointer to the value in the array or a reference to the value in the array, preferably, return the value directly.

Your function could either look like this using a pointer:

void HATRIX::HatriX_Signal(uint8_t signal, uint8_t I2C_Address, uint32_t* Page)
{
    if(signal == Temperature_Signal);
    {
        TC_IN.Thermo_Couple_Input_Channel(I2C_Address);
        TC_IN.fThermocoupleTemperature = TC_IN.fThermocoupleTemperature * 100;
        Serial.println("Page");
        *Page = TC_IN.fThermocoupleTemperature;
        Serial.println(*Page);
    }
}

and called like:

Hatrix.HatriX_Signal(Hatrix.Temperature_Signal, 0x66, &Hatrix.Page[2][0]);

or look like this using a reference:

void HATRIX::HatriX_Signal(uint8_t signal, uint8_t I2C_Address, uint32_t& Page)
{
    if(signal == Temperature_Signal);
    {
        TC_IN.Thermo_Couple_Input_Channel(I2C_Address);
        TC_IN.fThermocoupleTemperature = TC_IN.fThermocoupleTemperature * 100;
        Serial.println("Page");
        Page = TC_IN.fThermocoupleTemperature;
        Serial.println(Page);
    }
}

and called like:

Hatrix.HatriX_Signal(Hatrix.Temperature_Signal, 0x66, Hatrix.Page[2][0]);

or alternatively, as I would advice, look like this:

uint32_t HATRIX::HatriX_Signal(uint8_t signal, uint8_t I2C_Address)
{
    if(signal == Temperature_Signal);
    {
        TC_IN.Thermo_Couple_Input_Channel(I2C_Address);
        TC_IN.fThermocoupleTemperature = TC_IN.fThermocoupleTemperature * 100;
        Serial.println("Page");
        uint32_t page = TC_IN.fThermocoupleTemperature;
        Serial.println(page);
        return page;
    }
}

and called like:

Hatrix.Page[2][0] = Hatrix.HatriX_Signal(Hatrix.Temperature_Signal, 0x66);

CodePudding user response:

How to fix this program:

  • Raw data such as this 2D array should be made private, as per the fundamental OO principle of private encapsulation.
  • Should you wish the user to access this array it should be done through a "setter" function taking (x,y) coordinates.
  • Provide a similar "getter" function for read/print access.
  • HatriX_Signal and similar should not pass along a pointer to data in a class member, because that's nonsense. You already have access to that data through the this pointer.

Generally, I'd advise to read a beginner-level book about C and OO, or alternatively ditch C completely if you have no intention of using OO program design anyhow.

  • Related