Compare commits

...

2 commits

Author SHA1 Message Date
33e75b2e6a 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.
2024-08-25 06:19:31 -07:00
92512c257e Added functions to set leds high and low. 2024-08-24 09:42:54 -07:00
4 changed files with 118 additions and 79 deletions

View file

@ -21,62 +21,36 @@
#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)
{
*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;
}

View file

@ -10,9 +10,8 @@
#ifndef LEDCONTROLLER
#define LEDCONTROLLER
#include <stdint.h>
#include "stdbool.h"
#include <stdint.h>
/**
* A structure representing an LED
@ -24,38 +23,36 @@ typedef struct Led
bool state;
} Led;
typedef struct LedByte
{
Led leds[8];
} LedByte;
/**
* Sets the default PORTB pins for output.
* Returns a instance of the LedByte structure.
*/
void LedControler_SetPortADefault(void);
/**
* Sets the default PORTA pins for output.
*/
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);
void LedControler_ClearByte(LedByte *led_byte);
/**
* Clears out the halfbyte led representation
* Sets a AVR Led to High/on.
* @param led Pointer to an Led structure.
*/
void LedControler_ClearHalfByte(void);
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

View file

@ -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)

View file

@ -24,8 +24,13 @@ 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;
@ -34,4 +39,67 @@ TEST(test_LedController, LedController_SetHigh)
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;
Led fake_led;
fake_led.port = &fake_port;
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);
}
/*
* 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);
}
}