0

I'm currently working on a embedded system project and we are prototyping the hardware using an Arduino Uno. So far, we need to be able to get reliable temperature readings from a temperature sensor (TMP36) and I've managed to write a library/class for this specific sensor. You can find it here:
TMP36.h

#include <Arduino.h>

class TMP36
{
    public:
        TMP36(int pino);
        float getTempC();
        void setLimSup(float a);
        void setLimInf(float a);
        void testLimSup();
        void testLimInf();
    private:
        float _limSup;
        float _limInf;
        int _pino;
        float _tensao;
        float getTensao();
};

TMP36.cpp

#include "Arduino.h"
#include "TMP36.h"
#include "ArduinoUnit.h"

TMP36::TMP36 (int pino)
{
    _pino = pino;
}

float TMP36::getVoltage()
{
    return (analogRead(_pino) * 0.004882814);
}

float TMP36::getTempC ()
{  
    _tensao = getVoltage();
    return (_tensao - 0.5) * 100.0;
}

void TMP36::setLimSup(float a)
{
    _limSup = a;
}

void TMP36::setLimInf(float a)
{
    _limInf = a;
}

void TMP36::testeLimSup()
{
    test(okLimSup)
    {
        float reading = getTempC();
        AssertLessOrEqual(reading, _limSup);
    }

}

void TMP36::testeLimInf()
{

    test()
    {
        float reading = getTempC();
        assertMoreOrEqual(reading, _limInf);
    }
}

To be able to test if the getTempC is within the sensor range, I've been messing around with ArduinoUnit 2, but when compiling this code:

#include "TMP36.h"

TMP36 tmp36(0);

void setup() {
  Serial.begin(9600);

}

void loop() {
  float temp;
  temp = tmp36.getTempC();
  Serial.println(temp);
  delay(1000);

}

on Arduino IDE I get the following errors:

In file included from /media/pedro/Storage/Linux/Arduino/arduino-1.6.11/libraries/TMP36/TMP36.cpp:3:0:
/media/pedro/Storage/Linux/Arduino/arduino-1.6.11/libraries/TMP36/TMP36.cpp: In member function 'void TMP36::testeLimSup()':
/media/pedro/Storage/Linux/Arduino/arduino-1.6.11/libraries/ArduinoUnit/ArduinoUnit.h:591:164: error: qualified-id in declaration before '(' token
 #define test(name) struct test_ ## name : TestOnce { test_ ## name() : TestOnce(F(#name)) {}; void once(); } test_ ## name ## _instance; void test_ ## name :: once() 
                                                                                                                                                                    ^
/media/pedro/Storage/Linux/Arduino/arduino-1.6.11/libraries/TMP36/TMP36.cpp:34:2: note: in expansion of macro 'test'
  test(okLimSup)
  ^
/media/pedro/Storage/Linux/Arduino/arduino-1.6.11/libraries/TMP36/TMP36.cpp: In member function 'void TMP36::testeLimInf()':
/media/pedro/Storage/Linux/Arduino/arduino-1.6.11/libraries/ArduinoUnit/ArduinoUnit.h:591:164: error: qualified-id in declaration before '(' token
 #define test(name) struct test_ ## name : TestOnce { test_ ## name() : TestOnce(F(#name)) {}; void once(); } test_ ## name ## _instance; void test_ ## name :: once() 
                                                                                                                                                                    ^
/media/pedro/Storage/Linux/Arduino/arduino-1.6.11/libraries/TMP36/TMP36.cpp:45:2: note: in expansion of macro 'test'
  test()
  ^
/media/pedro/Storage/Linux/Arduino/arduino-1.6.11/libraries/TMP36/TMP36.cpp:50:1: error: expected '}' at end of input
 }
 ^
exit status 1
Error compiling for board Arduino/Genuino Uno.

Since this is my first time UnitTesting, I'm not quite sure what does errors mean, so I need help fixing them.

Thank you!

6
  • Insufficient data. This programmer will now exit. (hint: minimal reproducible example, please) Commented Oct 16, 2016 at 21:25
  • @user4581301 In order to keep visual clutter to a minimum, all the code, except the error, are linked to a pastebin link. Would it be better to post all the code here? Commented Oct 16, 2016 at 21:28
  • Links to external code are ignored by a great many. Further, as soon as the host shuffles their links or garbage collects, the links are useless and the question even more useless. That said, posting all the code here is probably just as bad. Construct a minimal-but-complete set of code that contains nothing except the code that triggers the error and would be compilable if not for the error. Rational for this: more often than not, creating the minimal code exposes the problem and makes the solution obvious, removing the need to ask the question at all. Commented Oct 16, 2016 at 21:33
  • Here it is. I'm afraid that I think this is as minimum as I think I can get it. Commented Oct 16, 2016 at 21:40
  • Size is reasonable, but still incomplete. What is an okLimSup? Clearly it is not something that can be used with test(okLimSup) at least not the way it's being used, but without knowing what okLimSup is, not much can be done. Commented Oct 16, 2016 at 21:55

2 Answers 2

1

Ok, instead of trying to make the unit tests inside the class, I managed to make them outside the setup() and loop() functions. Here's the working code with a sample output:

//TMP36.h
#include <Arduino.h>
#include <ArduinoUnit.h>

class TMP36
{
    public:
        TMP36(int pino, float limInf, float limSup); //implementado
        float getTempC(); //implementado
        void setLimSup(float a);//implementado
        void setLimInf(float a);//implementado
        void testeLimSup();
        void testeLimInf();
        float getLimSup();
        float getLimInf();
    private:
        float _limSup; //setado pelo setLimSup;
        float _limInf; //setado pelo setLimInf;
        int _pino; //setado pelo construtor
        float _tensao; //retorno do getTempC;
        float getTensao();
};

/***************************/

//TMP36.cpp
#include "Arduino.h"
#include "TMP36.h"
#include "ArduinoUnit.h"

TMP36::TMP36 (int pino, float limInf, float limSup)
{
    pinMode(pino, OUTPUT);
    _pino = pino;
    _limInf = limInf;
    _limSup = limSup;
}

float TMP36::getTensao()
{
    return (analogRead(_pino) * 0.004882814);
}

float TMP36::getTempC ()
{   
    _tensao = getTensao();
    return (_tensao - 0.5) * 100.0;
}

void TMP36::setLimSup(float a)
{
    _limSup = a;
}

void TMP36::setLimInf(float a)
{
    _limInf = a;
}

float TMP36::getLimSup() {return _limSup; }

float TMP36::getLimInf() {return _limInf; }

/***************************/

//sketch.ino
#include <ArduinoUnit.h>
#include "TMP36.h"

TMP36 tmp36(0, -40, 125);

test(upLimOK)
{
  float a = tmp36.getLimSup();
  float b = tmp36.getTempC();
  assertLessOrEqual(b, a);
}

test(downLimOK)
{
  float a = tmp36.getLimInf();
  float b = tmp36.getTempC();
  assertMoreOrEqual(b, a);
}

void setup() {
  Serial.begin(9600);
}

void loop() {

  float temp;
  temp = tmp36.getTempC();
  Serial.print("TMP36 Reading: ");
  Serial.println(temp);
  delay(1000);
  Test::run();

}

Sample Output:

TMP36 Reading: 26.66
TMP36 Reading: 26.66
Test downLimOK passed.
Test upLimOK passed.
Test summary: 2 passed, 0 failed, and 0 skipped, out of 2 test(s).
Sign up to request clarification or add additional context in comments.

Comments

0

Using arduino_ci (full disclosure, I'm the author), you can do this without hardware present. It will bear more than a passing resemblance to the test code you have now because I adopted ArduinoUnit's set of test macros.

You would end up putting your test code in a new directory called test/, so test/limits.cpp might look like this

#include <ArduinoUnitTests.h>
#include "TMP36.h"

TMP36 tmp36(0, -40, 125);

unittest(upLimOK)
{
  float a = tmp36.getLimSup();
  float b = tmp36.getTempC();
  assertLessOrEqual(b, a);
}

unittest(downLimOK)
{
  float a = tmp36.getLimInf();
  float b = tmp36.getTempC();
  assertMoreOrEqual(b, a);
}

unittest_main()

To execute the unit tests, you would need to set up the test runner locally or use the GitHub Action if you would like to make it part of your pull request process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.