|
|
@ -4,6 +4,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
enum {ALL_LEDS_ON = ~0x0000, ALL_LEDS_OFF = ~ALL_LEDS_ON}; |
|
|
|
enum {FIRST_LED = 1, LAST_LED = 16}; |
|
|
|
|
|
|
|
static uint16_t *ledsAddress = NULL; // LEDs memory register location |
|
|
|
static uint16_t ledsImage; // internal variable to store LEDs state |
|
|
@ -14,8 +15,24 @@ static uint16_t convertLedNumberToBit(int ledNumber) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static bool IsLedOutOfBound(int ledNumber) { |
|
|
|
return (ledNumber < 1) || (ledNumber > 16); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void SetLedImageBit(int ledNumber) { |
|
|
|
ledsImage |= convertLedNumberToBit(ledNumber); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void ClearLedImageBit(int ledNumber) { |
|
|
|
ledsImage &= ~(convertLedNumberToBit(ledNumber)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void updateHardware(void) { |
|
|
|
if(ledsAddress == NULL) { |
|
|
|
RUNTIME_ERROR("LED Driver: NULL pointer access", -1); |
|
|
|
return; |
|
|
|
} |
|
|
|
*ledsAddress = ledsImage; |
|
|
@ -23,6 +40,10 @@ static void updateHardware(void) { |
|
|
|
|
|
|
|
|
|
|
|
void LedDriver_Create(uint16_t *address) { |
|
|
|
if(address == NULL) { |
|
|
|
RUNTIME_ERROR("LED Driver: NULL pointer access", -1); |
|
|
|
return; |
|
|
|
} |
|
|
|
ledsAddress = address; |
|
|
|
ledsImage = ALL_LEDS_OFF; |
|
|
|
updateHardware(); |
|
|
@ -34,12 +55,12 @@ void LedDriver_Destroy(uint16_t *address) { |
|
|
|
|
|
|
|
|
|
|
|
void LedDriver_TurnOn(int ledNumber) { |
|
|
|
if(ledNumber < 0 || ledNumber > 16) { |
|
|
|
if (IsLedOutOfBound(ledNumber)) { |
|
|
|
RUNTIME_ERROR("LED Driver: out-of-bounds LED", -1); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
ledsImage |= convertLedNumberToBit(ledNumber); |
|
|
|
SetLedImageBit(ledNumber); |
|
|
|
updateHardware(); |
|
|
|
} |
|
|
|
|
|
|
@ -51,11 +72,30 @@ void LedDriver_TurnAllOn(void) { |
|
|
|
|
|
|
|
|
|
|
|
void LedDriver_TurnOff(int ledNumber) { |
|
|
|
if(ledNumber < 0 || ledNumber > 16) { |
|
|
|
if (IsLedOutOfBound(ledNumber)) { |
|
|
|
RUNTIME_ERROR("LED Driver: out-of-bounds LED", -1); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
ledsImage &= ~(convertLedNumberToBit(ledNumber)); |
|
|
|
ClearLedImageBit(ledNumber); |
|
|
|
updateHardware(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void LedDriver_TurnAllOff(void) { |
|
|
|
ledsImage = ALL_LEDS_OFF; |
|
|
|
updateHardware(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool LedDriver_IsOn(int ledNumber) { |
|
|
|
if(IsLedOutOfBound(ledNumber)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return ledsImage & 1 << (ledNumber - 1); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool LedDriver_IsOff(int ledNumber) { |
|
|
|
return !(LedDriver_IsOn(ledNumber)); |
|
|
|
} |