3 Replies Latest reply on Jun 21, 2018 7:15 AM by user_342122993

    Write code independent of component name

    davide.marzorati_3386091

      Hi all,

       

      I'm still new to PSoC (and PSoC Creator) world: I would like to port some libraries that I wrote for STM32 MCU to PSoC, but I have some doubts about it.

       

      The libraries that I wrote are intended to be used with MEMS sensors (temperature, pressure, and humidity) which communicate with the MCU over I2C. When I wrote them for STM32 MCU, I wanted them to be independent of the I2C interface chosen (I2C1, I2C2, I2C3,..), so that the programmer could choose which I2C to be used. Therefore, my code looked like this:

      // Function prototype - I2C_TypeDef pointer
      void readTemperature(I2C_TypeDef *I2C, SensorStruct sensor);
      
      int main(void)
      {
           SensorStruct mySensor;
           I2C_Init(I2C1);
           
           while(1)
           {
                read_temperature(I2C1,&mySensor);
                // Do some stuff
           }
      }
      

      In PSoC, can I do something similar? My problem right now is that, if I write a library, I'll need to use some I2C methods, but these methods have to be called with the name of the component. So if I change the name of the component, I need to change it everywhere in my code, as follows:

      // Programmer used I2C component naming it I2C
      I2C_MasterSendStart(SLAVE_ADDRESS, READ);
      
      // Programmer used I2C component naming it I2C_1
      I2C_1_MasterSendStart(SLAVE_ADDRESS, READ);
      
      

      What kind of approach do you suggest me?

       

      Thank you in advance,

       

      Davide