diff --git a/src/LedController/LedController.c b/src/LedController/LedController.c index b7900f3..4bdb819 100644 --- a/src/LedController/LedController.c +++ b/src/LedController/LedController.c @@ -21,8 +21,7 @@ #define HALF_BYTE_BM - -//static LedController controller; +// static LedController controller; void LedControler_SetPortADefault(void); @@ -80,3 +79,17 @@ void LedControler_ClearHalfByte(void) PORTA.OUT &= ~PA_B4; // PORTA.OUT = 0x00; } + +void LedController_SetHigh(Led *led) +{ + *led->port |= (1 << led->pin_num); + led->state = true; + return; +} + +void LedController_SetLow(Led *led) +{ + *led->port &= ~(1 << led->pin_num); + led->state = false; + return; +} diff --git a/src/LedController/LedController.h b/src/LedController/LedController.h index 36ccf0c..c941e02 100644 --- a/src/LedController/LedController.h +++ b/src/LedController/LedController.h @@ -10,20 +10,18 @@ #ifndef LEDCONTROLLER #define LEDCONTROLLER -#include #include "stdbool.h" - +#include /** * A structure representing an LED */ -typedef struct Led +typedef struct Led { uint8_t *port; - uint8_t pin_num; - bool state; -}Led; - + uint8_t pin_num; + bool state; +} Led; /** * Sets the default PORTB pins for output. @@ -58,4 +56,16 @@ void LedControler_ShowHalfByte(uint8_t byte); */ void LedControler_ClearHalfByte(void); +/** + * Sets a AVR Led to High/on. + * @param led Pointer to an Led structure. + */ +void LedController_SetHigh(Led *led); + +/** + * Sets a AVR Led to Low/off. + * @param led Pointer to an Led structure. + */ +void LedController_SetLow(Led *led); + #endif // LEDCONTROLLER diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 932bde3..ea030d6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,12 +1,12 @@ project(Tests) # TEST_DIRS -add_subdirectory(LedController) #add_subdirectory(timer) #add_subdirectory(usart) add_subdirectory(MockRegEdit) add_subdirectory(RegEdit) add_subdirectory(simple_test) +add_subdirectory(LedController) diff --git a/tests/LedController/test_LedController.cpp b/tests/LedController/test_LedController.cpp index 0b44bab..6f5f61b 100644 --- a/tests/LedController/test_LedController.cpp +++ b/tests/LedController/test_LedController.cpp @@ -24,8 +24,34 @@ TEST_GROUP(test_LedController) } }; +TEST(test_LedController, selftest) +{ + CHECK_TRUE(true); +} -TEST(test_LedController, LedController_SetHigh) + +TEST(test_LedController, LedSetHighWorks) +{ + uint8_t fake_port = 0x00; + + Led fake_led; + fake_led.port = &fake_port; + fake_led.pin_num = 0; + fake_led.state = false; + + LedController_SetHigh(&fake_led); + + //Check that it sets the correct bit. + CHECK_TRUE(fake_port & 0x01); + + //It should set the state to true. + CHECK_TRUE(fake_led.state); + + //Only the first bit should have been set + CHECK_TRUE(fake_port == 0x01); +} + +TEST(test_LedController, LedSetHighMaintainsPortState) { uint8_t fake_port = 0x00; @@ -34,4 +60,31 @@ TEST(test_LedController, LedController_SetHigh) fake_led.pin_num = 0; fake_led.state = false; + Led led_two; + led_two.pin_num = 1; + led_two.port = &fake_port; + led_two.state = false; + + LedController_SetHigh(&fake_led); + LedController_SetHigh(&led_two); + + CHECK_TRUE(fake_port & 0x01); + CHECK_TRUE(fake_port & 0x02); } + +TEST(test_LedController, LedSetLow) +{ + uint8_t fake_port = 0x01; + + Led fake_led; + fake_led.port = &fake_port; + fake_led.pin_num = 0; + fake_led.state = true; + + LedController_SetLow(&fake_led); + + CHECK_EQUAL(0x00, fake_port); + CHECK_EQUAL(false, fake_led.state); +} + +