Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Core/Inc/das/irts-api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "irts.h"

#include "eagletrt.h"
#include <stdlib.h>
#include <stdint.h>

/*!
* \brief Initializes the module
*
* \retval IRTS_RC_OK if successful
* \retval IRTS_RC_ERROR if fail
*/
enum IrtsReturnCode irts_api_init(void);

/*!
* \brief Get the current state of a irts line
*
* \param irts The irts to query
*
* \retval int16_t value of \p irts
*/
double irts_api_get_value(enum IrtsName irts);

/*!
* \brief Set the current state of a irts line
*
* \param irts The irts to be set
* \param value The value to set \p irts to
*
* \retval IRTS_RC_OK if successful
* \retval IRTS_RC_ERROR if \p irts or \p value are out of bounds
*/
enum IrtsReturnCode irts_api_set_value(enum IrtsName irts, double value);
Comment thread
samuelbravi11 marked this conversation as resolved.
Outdated
32 changes: 32 additions & 0 deletions Core/Inc/das/irts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef IRTS_H
#define IRTS_H

#include "eagletrt.h"
#include <stdint.h>

/*!
* \brief Names for checked irts
*/
enum IrtsName : uint8_t {
IRTS_NAME_LEFT, /*!< Irts LEFT */
IRTS_NAME_RIGHT, /*!< Irts RIGHT */
IRTS_NAME_COUNT /*!< Total number of irts */
};

/*!
* \brief Possible return codes for irts initialization function
*/
enum IrtsReturnCode : uint8_t {
IRTS_RC_OK, /*!< Initialization successful */
IRTS_RC_ERROR /*!< Error during initialization */
};

/*!
* \brief Struct that handles all temperature from irts
*/
struct IrtsHandler {
EAGLETRT_VOLATILE double left_temperature;
EAGLETRT_VOLATILE double right_temperature;
Comment thread
samuelbravi11 marked this conversation as resolved.
Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have the irts names you can use them as indices for an array instead of having the two independent temperatures.

};

#endif // IRTS_H
34 changes: 34 additions & 0 deletions Core/Src/das/irts-api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "irts-api.h"
#include <string.h>

EAGLETRT_STATIC struct IrtsHandler irts_api_handler;

enum IrtsReturnCode irts_api_init(void) {
// Initialize irts value to 0 to indicate no data
memset(&irts_api_handler, 0, sizeof(irts_api_handler));

return IRTS_RC_OK;
}

double irts_api_get_value(enum IrtsName irts) {
if (irts == IRTS_NAME_LEFT)
return (double)irts_api_handler.left_temperature;
else if (irts == IRTS_NAME_RIGHT)
return (double)irts_api_handler.right_temperature;
else
return -1;
}
Comment thread
samuelbravi11 marked this conversation as resolved.
Outdated

enum IrtsReturnCode irts_api_set_value(enum IrtsName irts, double value) {
if (irts >= IRTS_NAME_COUNT) {
return IRTS_RC_ERROR;
}

if (irts == IRTS_NAME_LEFT) {
irts_api_handler.left_temperature = value;
} else if (irts == IRTS_NAME_RIGHT) {
irts_api_handler.right_temperature = value;
}

return IRTS_RC_OK;
}
4 changes: 4 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ build_flags =
-ICore/Inc
-ICore/Inc/das
-DDAS_FRONT
-DUNITY_INCLUDE_DOUBLE
Comment thread
samuelbravi11 marked this conversation as resolved.
Outdated
-DEAGLETRT_STATIC=
-DEAGLETRT_STATIC_INLINE=
build_src_filter =
-<*>
+<Core/Src/das/feedback-api.c>
+<Core/Src/das/potentiometer-api.c>
+<Core/Src/das/irts-api.c>

[env:tests_rear]
platform = native
Expand All @@ -85,12 +87,14 @@ build_flags =
-ICore/Inc
-ICore/Inc/das
-DDAS_REAR
-DUNITY_INCLUDE_DOUBLE
-DEAGLETRT_STATIC=
-DEAGLETRT_STATIC_INLINE=
build_src_filter =
-<*>
+<Core/Src/das/feedback-api.c>
+<Core/Src/das/potentiometer-api.c>
+<Core/Src/das/irts-api.c>

[env:bootloader]
platform = ststm32
Expand Down
123 changes: 123 additions & 0 deletions test/test_irts_api/test_irts_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include "unity.h"

#include "irts.h"
#include "irts-api.h"

#include <stdint.h>

extern struct IrtsHandler irts_api_handler;

void setUp(void) {
(void)irts_api_init();
}

void tearDown(void) {
}

void test_irts_api_init_should_return_ok_and_set_all_irts_to_zero(void) {
double expected_value_left;
double expected_value_right;

irts_api_handler.left_temperature = 1;
irts_api_handler.right_temperature = 1;

expected_value_left = 0U;
expected_value_right = 0U;

enum IrtsReturnCode rc = irts_api_init();

TEST_ASSERT_EQUAL_UINT8_MESSAGE(
IRTS_RC_OK,
rc,
"irts_api_init() should return IRTS_RC_OK");

TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(
expected_value_left,
irts_api_handler.left_temperature,
"irts_api_init() should return IRTS_RC_OK");

TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(
expected_value_right,
irts_api_handler.right_temperature,
"irts_api_init() should return IRTS_RC_OK");
}

void test_irts_api_get_value_should_return_saved_value(void) {
enum IrtsName irts = IRTS_NAME_LEFT;

irts_api_handler.left_temperature = 1;

TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(
1,
irts_api_get_value(irts),
"irts_api_get_value() should return the saved value for each valid irts");
}

void test_irts_api_get_value_should_return_minus_one_for_invalid_irts_count(void) {
double value = irts_api_get_value(IRTS_NAME_COUNT);

TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(
-1,
value,
"irts_api_get_value(IRTS_NAME_COUNT) should return -1");
}

void test_irts_api_set_value_should_update_value_for_valid_input(void) {
enum IrtsName irts = IRTS_NAME_LEFT;

enum IrtsReturnCode rc = irts_api_set_value(
irts,
1000U);

TEST_ASSERT_EQUAL_UINT8_MESSAGE(
IRTS_RC_OK,
rc,
"feedback_api_set_state() should return FEEDBACK_RC_OK for valid input");

TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(
1000U,
irts_api_handler.left_temperature,
"irts_api_set_value() should update the requested irts value");
}

void test_irts_api_set_value_should_return_error_and_reject_invalid_irts_count(void) {
double expected_values[IRTS_NAME_COUNT];
double value = 1;

for (enum IrtsName irts = 0; irts < IRTS_NAME_COUNT; ++irts) {
expected_values[irts] = value;
}
irts_api_handler.left_temperature = value;
irts_api_handler.right_temperature = value;

enum IrtsReturnCode rc = irts_api_set_value(
IRTS_NAME_COUNT,
1000U);

TEST_ASSERT_EQUAL_UINT8_MESSAGE(
IRTS_RC_ERROR,
rc,
"irts_api_set_value(IRTS_NAME_COUNT, ...) should return IRTS_RC_ERROR");

TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(
value,
irts_api_handler.left_temperature,
"Invalid irts should not modify any valid irts value");

TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(
value,
irts_api_handler.right_temperature,
"Invalid irts should not modify any valid irts value");
}

int main(void) {
UNITY_BEGIN();

RUN_TEST(test_irts_api_init_should_return_ok_and_set_all_irts_to_zero);
RUN_TEST(test_irts_api_get_value_should_return_saved_value);
RUN_TEST(test_irts_api_get_value_should_return_minus_one_for_invalid_irts_count);
RUN_TEST(test_irts_api_set_value_should_update_value_for_valid_input);
RUN_TEST(test_irts_api_set_value_should_return_error_and_reject_invalid_irts_count);

return UNITY_END();
}
2 changes: 1 addition & 1 deletion test/test_potentiometer_api/test_potentiometer_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void test_potentiometer_api_init_should_return_ok_and_set_all_potentiometers_to_

void test_potentiometer_api_get_value_should_return_saved_value(void) {
enum PotentiometerName potentiometer = POTENTIOMETER_NAME_LEFT;

potentiometer_api_handler.potentiometer_value[potentiometer] = 1;

TEST_ASSERT_EQUAL_INT16_MESSAGE(
Expand Down
Loading