From 33e75b2e6af9235343a1a292425616e3753081dd Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 25 Aug 2024 06:19:31 -0700 Subject: [PATCH] Removed unused functions, Added new structure + Added new structure for holding a byte of LEDS. + Added new function to creating structure of Led array. - Removed older functions for half-bytes. - Removed the older function prototypes. --- src/LedController/LedController.c | 61 ++++------------------ src/LedController/LedController.h | 31 ++++------- tests/LedController/test_LedController.cpp | 15 ++++++ 3 files changed, 35 insertions(+), 72 deletions(-) diff --git a/src/LedController/LedController.c b/src/LedController/LedController.c index 4bdb819..f4e1146 100644 --- a/src/LedController/LedController.c +++ b/src/LedController/LedController.c @@ -21,63 +21,24 @@ #define HALF_BYTE_BM -// static LedController controller; - -void LedControler_SetPortADefault(void); - -/* - * Uses pins: 9, 8, 12, 13 - */ -void LedControler_SetPortBDefault(void) +LedByte LedController_New(uint8_t *port) { - // controller.port = &PORTA; + LedByte led_byte; + for (int i = 0; i < 8; i++) + { + led_byte.leds[i].pin_num = 1; + led_byte.leds[i].state = false; + led_byte.leds[i].port = port; + } + return led_byte; } -void LedController_SetLedBitNum(void *port, uint8_t pin, uint8_t bit); - -void LedController_ShowByte(uint8_t byte); - -void LedControler_ShowHalfByte(uint8_t byte) +void LedController_ShowByte(LedByte *led_byte) { - byte = byte & 0x0F; - - // PORTA.DIR |= (1<<2)|(1<<3)|(1<<6)|(1<<7); - PORTA.DIR |= PA_B1; - PORTA.DIR |= PA_B2; - PORTA.DIR |= PA_B3; - PORTA.DIR |= PA_B4; - // PORTA.DIR |= HALF_BYTE_BM; - - // 15 = 0b1111 - // PORTA.OUT |= (byte & 0x0F); - - if (byte & 0x01) - { - PORTA.OUT |= PA_B1; - } - if (byte & 0x02) - { - PORTA.OUT |= PA_B2; - } - if (byte & 0x04) - { - PORTA.OUT |= PA_B3; - } - if (byte & 0x08) - { - PORTA.OUT |= PA_B4; - } } -void LedControler_ClearHalfByte(void) +void LedControler_ClearByte(LedByte *led_byte) { - // PORTA.OUT &= HALF_BYTE_BM; - // PORTA.OUT &= ~((1<<2)|(1<<3)|(1<<6)|(1<<7)); - PORTA.OUT &= ~PA_B1; - PORTA.OUT &= ~PA_B2; - PORTA.OUT &= ~PA_B3; - PORTA.OUT &= ~PA_B4; - // PORTA.OUT = 0x00; } void LedController_SetHigh(Led *led) diff --git a/src/LedController/LedController.h b/src/LedController/LedController.h index c941e02..0db8b42 100644 --- a/src/LedController/LedController.h +++ b/src/LedController/LedController.h @@ -23,38 +23,25 @@ typedef struct Led bool state; } Led; -/** - * Sets the default PORTB pins for output. - */ -void LedControler_SetPortADefault(void); +typedef struct LedByte +{ + Led leds[8]; +} LedByte; /** - * Sets the default PORTA pins for output. + * Returns a instance of the LedByte structure. */ -void LedControler_SetPortBDefault(void); - -/** - * Allows the setting or changing of which pins represent which bits. - * @param port The address of the port. - * @param pin The pin number for the port. - * @param bit The bit that the pin should represent. - */ -void LedController_SetLedBitNum(void *port, uint8_t pin, uint8_t bit); +LedByte LedController_New(uint8_t *port); /** * Displays the byte of data via led pins. */ -void LedController_ShowByte(uint8_t byte); +void LedController_ShowByte(LedByte *led_byte); /** - * Displays the the first 4 bits of a byte. + * Clears out the byte led representation */ -void LedControler_ShowHalfByte(uint8_t byte); - -/** - * Clears out the halfbyte led representation - */ -void LedControler_ClearHalfByte(void); +void LedControler_ClearByte(LedByte *led_byte); /** * Sets a AVR Led to High/on. diff --git a/tests/LedController/test_LedController.cpp b/tests/LedController/test_LedController.cpp index 6f5f61b..d009dbb 100644 --- a/tests/LedController/test_LedController.cpp +++ b/tests/LedController/test_LedController.cpp @@ -87,4 +87,19 @@ TEST(test_LedController, LedSetLow) CHECK_EQUAL(false, fake_led.state); } +/* + * what's the best way to handle an array of 8 leds. Should they + * reside in a structure? Or should it just be an array that the user defines? + */ +TEST(test_LedController, NewLedByte) +{ + uint8_t fake_port = 0x00; + LedByte led_byte = LedController_New(&fake_port); + + for(int i = 0; i < 8; i++){ + CHECK_TRUE(led_byte.leds[i].state == false); + CHECK_EQUAL(&fake_port, led_byte.leds[i].port); + CHECK_EQUAL(false, led_byte.leds[i].state); + } +}