title: Program design architecture for sensors application in embedded device
body: <p>I'm not an expert with C++ since I come from an electronics background.</p>
  
  <p>I'm trying to make a Arduino program that allows anyone to add a sensors easily to a Arduino device using a standard format.</p>
  
  <p>I thought of making a HAL for each sensor, for that I have created a class named <code>Sensor</code> which is the base class for each sensor. The user has to create a subclass that inherits from the <code>Sensor</code> class. This subclass for example is called <code>Sensor_2</code> (2 is the ID) and it implements functions like:</p>
  
  <pre><code>uint16_t getID( void );
  uint8_t getAvailableChannels( void ); 
  void sampleChannel( uint8_t aChannel, uint8_t *aDataSize, uint8_t aDataBuffer[] );
  </code></pre>
  
  <p>This class is the one that calls its own sensor library which is dependent of the sensor.</p>
  
  <p>Doing it this way I can sample every sensor in an standard way without having to rewrite each sensor library.</p>
  
  <p>I don't know if that is the way to do it but I can't think of anything better.</p>
  
  <p>I have tried to implement that with the following code:</p>
  
  <p>Sensor.h</p>
  
  <pre><code>#ifndef SENSOR_H
  #define SENSOR_H
  
  #include "Arduino.h"
  
  class Sensor
  {
  public:
    Sensor();  
    virtual uint16_t getID( void );
    virtual uint8_t getAvailableChannels( void ); 
    virtual void sampleChannel( uint8_t channel, uint8_t dataSize, uint8_t dataBuffer[]);
  };
  
  #endif
  </code></pre>
  
  <p>Sensor_2.h</p>
  
  <pre><code>#ifndef Sensor_2_H
  #define Sensor_2_H
  
  #include "Arduino.h"
  
  #include "../../Sensor.h"
  
  #define ID 2
  #define CHANNELS 1
  class Sensor_2: public Sensor
  {
  public:
    Sensor_2 ( void  );  
    virtual uint16_t getID( void );
    virtual uint8_t getAvailableChannels( void ); 
    virtual void sampleChannel( uint8_t aChannel, uint8_t *aDataSize, uint8_t aDataBuffer[] );
  };
  
  #endif 
  </code></pre>
  
  <p>Sensor_2.cpp</p>
  
  <pre><code>#include "Sensor_2.h"
  
  Sensor_2::Sensor_2( void )
  {
  
  }
  
  uint16_t Sensor_2::getID( void )
  {
      return (ID);    
  }
  
  uint8_t Sensor_2::getAvailableChannels( void )
  {
      return (CHANNELS);    
  } 
  
  void sampleChannel( uint8_t aChannel, uint8_t *aDataSize, uint8_t aDataBuffer[] )
  {
      aDataSize = 1;
      aDataBuffer[0] = hallRead();    
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;Logger.h&gt;
  #include "ComHandler.h"    
  #include "./Sensors/Sensor_2/Sensor_2.h"
  
  
  ComHandler comHandler;
  
  SensorManager* sensorManager = SensorManager::getInstance();
  
  void setup()
  {
    Serial.begin(115200);
  
    Logger::setLogLevel(Logger::WARNING);
  
    Sensor_2 sensorHall = new Sensor_2;
  
    sensorManager-&gt;addSensor(sensorHall);     
  }
  
  void loop()
  {
      uint8_t* datasize;
      uint16_t buffer[4];
      comHandler.checkForData();
      sensorManager.getAvailableSensors()[0].sampleChannel(0,dataSize, buffer);
  
  }
  </code></pre>
  
  <p>The problem is that when trying to compile I get this error:</p>
  
  <pre class="lang-none prettyprint-override"><code>conversion from 'Sensor_2*' to non-scalar type 'Sensor_2' requested
     Sensor_2 sensorHall = new Sensor_2;
                           ^
  </code></pre>
  
  <p>If instead of:</p>
  
  <pre><code>Sensor_2 sensorHall = new Sensor_2; 
  </code></pre>
  
  <p>I use:</p>
  
  <pre><code>Sensor_2 sensorHall; 
  </code></pre>
  
  <p>I get:</p>
  
  <pre class="lang-none prettyprint-override"><code>sketch/Sensor.cpp.o:(.literal._ZN6SensorC2Ev+0x0): undefined reference to `vtable for Sensor'
  sketch/Winja.ino.cpp.o:(.literal._Z5setupv+0x10): undefined reference to `Sensor_2::Sensor_2()'
  sketch/Winja.ino.cpp.o: In function `setup()':
  </code></pre>
  
  <p>Do you think this is a good way to implement a solution to my problem? If so, how could I fix my error?</p>