Compare commits

...

2 Commits
v1.1.0 ... main

5 changed files with 186 additions and 23 deletions

View File

@ -22,21 +22,23 @@
#include "MockRegEdit.h"
#endif
//These two arrays allow us to track the A and B ports to know when
//one has been disabled before.
static bool porta_disabled[8] = {0};
static bool portb_disabled[8] = {0};
static bool valid_load(uint16_t val)
{
if(val > 527 && val < 1000) {
if(val > LOWTHRESH && val < HIGHTHRESH) {
return true;
}
return false;
}
void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin)
static uint16_t sample_adc(uint8_t adc_pin)
{
ADC_Init(adc_pin);
ADC_Enable();
@ -44,6 +46,13 @@ void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin)
uint16_t val = ADC_ReadValue(adc_pin);
ADC_Disable();
return val;
}
void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin)
{
uint16_t val = sample_adc(adc_pin);
if(valid_load(val) && !porta_disabled[adc_pin]){
RegEdit_SetBit((void *) &PORTA.DIR, out_pin);
RegEdit_SetBit((void *) &PORTA.OUT, out_pin);
@ -56,12 +65,8 @@ void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin)
void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin)
{
ADC_Init(adc_pin);
ADC_Enable();
ADC_SetPin(adc_pin);
uint16_t val = ADC_ReadValue(adc_pin);
ADC_Disable();
uint16_t val = sample_adc(adc_pin);
if(valid_load(val) && !portb_disabled[adc_pin]){
RegEdit_SetBit((void *) &PORTB.DIR, out_pin);
RegEdit_SetBit((void *) &PORTB.OUT, out_pin);
@ -71,3 +76,11 @@ void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin)
portb_disabled[adc_pin] = true;
}
}
void Load_SoftResetDisabledLoads()
{
for(int i = 0; i < 8; i++){
porta_disabled[i] = false;
portb_disabled[i] = false;
}
}

View File

@ -10,23 +10,45 @@
#ifndef LOAD_H
#define LOAD_H
#include <stdint.h>
/**
* @brief Low Threshold
*
* Anything below or equal to the value will cause the pin to lock into the disabled
* state.
*/
#define LOWTHRESH 527
#define LOWTHRESH 517
/**
* @brief High Threshold
*
* Anything equal or above the value will cause the pin to lock into the disabled
* state.
*/
#define HIGHTHRESH 1000
#define HIGHTHRESH 830
/**
* @brief Checks if the adc pin is inbetween LOWTHRESH and HIGHTHRESH and then
* sets or disables the output pin on PORTA.
* @param adc_pin the pin number that the load value is read from.
* @param out_pin The pin that is set high if the adc_pin input is valid.
*/
void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin);
/**
* @brief Checks if the adc pin is inbetween LOWTHRESH and HIGHTHRESH and then
* sets or disables the output pin on PORTB.
* @param adc_pin the pin number that the load value is read from.
* @param out_pin The pin that is set high if the adc_pin input is valid.
*/
void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin);
void Load_HandlePinLoads(void);
/**
* @brief Resets the disabled array state.
*/
void Load_SoftResetDisabledLoads(void);
#endif /* LOAD_H */

View File

@ -9,6 +9,7 @@ add_subdirectory(RegEdit)
add_subdirectory(simple_test)
add_subdirectory(zero_cross_detection)
add_subdirectory(TriacOut)
add_subdirectory(load)
# TEST_RUNNER
@ -33,6 +34,7 @@ add_executable(Mock_Tests
target_link_libraries(Mock_Tests
${CPPUTEST_LIBRARIES}/libCppUTest.a
${CPPUTEST_LIBRARIES}/libCppUTestExt.a
test_load
test_MockRegEdit
test_MockADC
test_zero_cross_detection

View File

@ -2,6 +2,7 @@
//ImportTestGroups
IMPORT_TEST_GROUP(test_load);
IMPORT_TEST_GROUP(test_MockRegEdit);
IMPORT_TEST_GROUP(test_MockADC);
IMPORT_TEST_GROUP(test_zero_cross_detection);

View File

@ -23,9 +23,14 @@ extern "C"
TEST_GROUP(test_load)
{
uint8_t adc_pin;
uint8_t load_pin;
void setup()
{
adc_pin = 4;
load_pin = 7;
MockADC_ZeroIndex();
Load_SoftResetDisabledLoads();
}
void teardown()
{
@ -39,23 +44,143 @@ TEST(test_load, LoadPass)
CHECK_TRUE(true);
}
TEST(test_load, PortAHandlerSuccess)
void setup_adc_expectations(uint8_t adc_pin)
{
mock().expectOneCall("ADC_Init")
.withUnsignedIntParameter("pin_num", 4);
.withUnsignedIntParameter("pin_num", adc_pin);
mock().expectOneCall("ADC_Enable");
mock().expectOneCall("ADC_ReadValue_Impl")
.withUnsignedIntParameter("pin_num", 4);
.withUnsignedIntParameter("pin_num", adc_pin);
mock().expectOneCall("ADC_Disable");
}
void expect_porta_disabled(uint8_t load_pin)
{
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.OUT)
.withUnsignedIntParameter("bit_num", 7);
Load_HandleLoadPortA(4, 7);
.withUnsignedIntParameter("bit_num", load_pin);
}
void expect_porta_enabled(uint8_t load_pin)
{
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &PORTA.DIR)
.withUnsignedIntParameter("bit_num", load_pin);
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &PORTA.OUT)
.withUnsignedIntParameter("bit_num", load_pin);
}
void expect_portb_disabled(uint8_t load_pin)
{
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTB.OUT)
.withUnsignedIntParameter("bit_num", load_pin);
}
//Need to setup a one-shot or way for it to only enable once.
void expect_portb_enabled(uint8_t load_pin)
{
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &PORTB.DIR)
.withUnsignedIntParameter("bit_num", load_pin);
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &PORTB.OUT)
.withUnsignedIntParameter("bit_num", load_pin);
}
TEST(test_load, PortAHandlerDisabledHigh)
{
MockADC_PushValue(HIGHTHRESH);
setup_adc_expectations(adc_pin);
expect_porta_disabled(load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
}
TEST(test_load, PortAHandlerDisabledLow)
{
MockADC_PushValue(LOWTHRESH);
setup_adc_expectations(adc_pin);
expect_porta_disabled(load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
}
TEST(test_load, PortAHandlerEnabledWhenValid)
{
MockADC_PushValue(HIGHTHRESH - 1);
setup_adc_expectations(adc_pin);
expect_porta_enabled(load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
}
TEST(test_load, PortAHandlerDisblesUntilPowerReset)
{
MockADC_PushValue(HIGHTHRESH - 1);
MockADC_PushValue(HIGHTHRESH);
setup_adc_expectations(adc_pin);
expect_porta_disabled(load_pin);
setup_adc_expectations(adc_pin);
expect_porta_disabled(load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
Load_HandleLoadPortA(adc_pin, load_pin);
}
TEST(test_load, PortBHandlerDisabledHigh)
{
MockADC_PushValue(HIGHTHRESH);
setup_adc_expectations(adc_pin);
expect_portb_disabled(load_pin);
Load_HandleLoadPortB(adc_pin, load_pin);
}
TEST(test_load, PortBHandlerDisabledLow)
{
MockADC_PushValue(LOWTHRESH);
setup_adc_expectations(adc_pin);
expect_portb_disabled(load_pin);
Load_HandleLoadPortB(adc_pin, load_pin);
}
TEST(test_load, PortBHandlerEnabledWhenValid)
{
MockADC_PushValue(HIGHTHRESH - 1);
setup_adc_expectations(adc_pin);
expect_portb_enabled(load_pin);
Load_HandleLoadPortB(adc_pin, load_pin);
}
TEST(test_load, PortBHandlerDisblesUntilPowerReset)
{
MockADC_PushValue(HIGHTHRESH - 1);
MockADC_PushValue(HIGHTHRESH);
setup_adc_expectations(adc_pin);
expect_portb_disabled(load_pin);
setup_adc_expectations(adc_pin);
expect_portb_disabled(load_pin);
Load_HandleLoadPortB(adc_pin, load_pin);
Load_HandleLoadPortB(adc_pin, load_pin);
}