You are on page 1of 4

/****************************************************************************

Module
LEDService.c
Description
This service keeps track of each strip and its color
Notes
History
When
Who
What/Why
-------------- ---------11/12/16 02:03 rdb
created it for ME218A project
****************************************************************************/
#include "LEDService.h"
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority;
//static LEDState_t CurrentState;
static uint32_t StripColors[] = { 0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
LED strip

0, 0, 0}; // Array to keep track of the color in each

//static uint16_t DelayTime = 10;


/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
Init_LEDService
Parameters
uint8_t : the priority of this service
Returns
bool, false if error in initialization, true otherwise
Description
Saves away the priority, sets up the initial transition and does any
other required initialization for this state machine
Notes
Author
R. D. Born, 11/12/16
****************************************************************************/
bool Init_LEDService(uint8_t Priority)
{
MyPriority = Priority;
// Initialize the LED hardware
LED_HWInit();
return true;

}
/****************************************************************************
Function
Post_LEDService
Parameters
EF_Event ThisEvent , the event to post to the queue
Returns
boolean False if the Enqueue operation failed, True otherwise
Description
Posts an event to this state machine's queue
Notes
Author
R. D. Born, 11/16/16
02:35
****************************************************************************/
bool Post_LEDService(ES_Event ThisEvent)
{
// Return success/failure
return ES_PostToService( MyPriority, ThisEvent);
}
/****************************************************************************
Function
Run_LEDService
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
Notes
uses nested switch/case to implement the machine.
Author
R. D. Born
11/12/16
****************************************************************************/
ES_Event Run_LEDService(ES_Event ThisEvent)
{
// Set ReturnEvent to ES_NO_EVENT
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
return ReturnEvent;
}
/****************************************************************************
Function
GetColor
Parameters
integer that represents the strip number
Returns
a 32 bit integer representing the colors of the strip
Description
Gets the current color of an LED strip
Notes

Author
R. D. Born 11/18/2016
****************************************************************************/
uint32_t GetColor(uint8_t STRIPidx)
{
// IF STRIPix is not a valid strip index
if(STRIPidx > NUM_STRIPS_TOTAL)
{
printf("WARNING: Index out of bounds in LED_GetColor, returning
0\n\r");
return 0;
}
// return the colors of the strip
return StripColors[STRIPidx];
}
/****************************************************************************
Function
LED_SetColor
Parameters
integer that represents the strip number and an integer representing the
color to turn the strip
Returns
None
Description
Turns a strip of LEDs a color
Notes
Author
R. D. Born 11/18/2016
****************************************************************************/
void LED_SetColor(uint8_t STRIPidx, uint32_t RGB)
{
static uint8_t R = 0;
static uint8_t G = 0;
static uint8_t B = 0;
static uint8_t idx = 0;
static uint8_t R_idx = 0;
static uint8_t G_idx = 0;
static uint8_t B_idx = 0;
// Determine the R, G, and B values
R = (RGB >> R_BIT_OFFSET) & MAX_RGB;
G = (RGB >> G_BIT_OFFSET) & MAX_RGB;
B = (RGB >> B_BIT_OFFSET) & MAX_RGB;
// Determine the indices of the LEDs we want to change
idx = STRIPidx;//NUM_STRIPS_TOTAL - STRIPidx;
R_idx = idx*LEDS_PER_STRIP+0;//-1;
G_idx = idx*LEDS_PER_STRIP+1;//-2;
B_idx = idx*LEDS_PER_STRIP+2;//-3;
// Set PWM for each appropriate LED (LED_SetPWM)
LED_SetPWM(R*MAX_PULSE_WIDTH/MAX_RGB, R_idx);
LED_SetPWM(G*MAX_PULSE_WIDTH/MAX_RGB, G_idx);
LED_SetPWM(B*MAX_PULSE_WIDTH/MAX_RGB, B_idx);
}
/****************************************************************************
Function

LED_SetPWM
Parameters
integer that represents the desired PulseWidth and the stip number
Returns
None
Description
sets the pulse width of the strip of LEDs for a given color
Notes
Author
R. D. Born 11/18/2016
****************************************************************************/
void LED_SetPWM(uint8_t PulseWidth, uint8_t LEDidx)
{
// Encode PulseWidth and LEDidx into an Event Parameter
ES_Event ThisEvent;
ThisEvent.EventType = CHANGE_PULSEWIDTH;
ThisEvent.EventParam = Encode_Idx_PW(PulseWidth, LEDidx);

// Post CHANGE_PULSEWIDTH event to SRPWMService with encoded parameter


Post_SRPWMService(ThisEvent);