Home Arduino i2c – Use class object inside a ISR

i2c – Use class object inside a ISR

0
i2c – Use class object inside a ISR

[ad_1]

I’ve to simulate an ADC that behaves as a I2C slave. The simulated values that the ADC shall expose are despatched over serial, saved on an attribute and are requested by a grasp system over I2C.

A simplified answer is the next. I’ve a struct ADC that describes the converter:

struct ADC 
{
  uint32_t register_1;

  void write(uint32_t information){
    register_1 = information;
  }

  uint8_t learn(){
    return register_1;
  }

  // another strategies to setup...
};

In loop(), I learn the serial with the intention to replace ADC::register worth and within the onRequest handler I finally ship the requested information by accessing ADC::register:

ADC adc{};

void setup(){
  // setup adc, Wire and Serial right here
  Wire.onRequest(slaveTransmitterHandler)
}

void loop(){
  if (Serial.out there() > 0)
  {
     // learn serial, parse acquired information and verify them
     uint32_t information = parse_serial(); // do something is required to verify information...

     // replace adc attribute
     uint8_t oldSREG = SREG;
     noInterrupts(); // disable interrupts to replace uint32
     adc.write(information);
     SREG = oldSREG;
  }

}

void slaveTransmitterHandler(){
  Wire.beginTransmission(ADDRESS);
  Wire.write(adc.learn());
  Wire.endTransmission();
}

I am unsure if I’ve to declare the adc object as risky (this is able to imply to declare all ADC strategies and variables as risky). Is it protected to make use of an object contained in the I2C handler known as by the I2C interrupt? So far as I do know, it’s not (I can discover no motive why it must be completely different from non class variables), however I’ve all the time labored with non class variables and by no means with cpp objects. Additionally, I am unable to discover any details about this matter.

It could possibly be potential that I am fallacious with the entire design, even when it appears to me to be very constant. I am open to vary it for one thing higher, so please if this design is nonsense, touch upon that! I am right here to be taught 🙂

Thanks.

Luca

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here