-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLightBinding.c
48 lines (43 loc) · 1.58 KB
/
LightBinding.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "LightBinding.h"
static const int kAnalogInMax = 1023;
static const int kAnalogInMin = 0;
static const int kAnalogOutMax = 255;
static const int kAnalogOutMin = 0;
/*!
* \brief Performs any initialization needed for a LightBinding
* \param pLightBinding a LightBinding to initialize
*/
void LightBinding_setup(struct LightBinding* pLightBinding)
{
}
/*!
* \brief updates a \a pLightBinding, determining the current color
* \param pLightBinding the LightBinding to update
* \return the current color of the provided \a pLightBinding
*/
struct Color LightBinding_update(struct LightBinding* pLightBinding)
{
float inputPercent = pLightBinding->analogReadCallback(pLightBinding->pAnalogReadUserData);
struct Color colorDelta = {
pLightBinding->colorHigh.r - pLightBinding->colorLow.r,
pLightBinding->colorHigh.g - pLightBinding->colorLow.g,
pLightBinding->colorHigh.b - pLightBinding->colorLow.b
};
struct Color outColor = {
pLightBinding->colorLow.r + inputPercent*colorDelta.r,
pLightBinding->colorLow.g + inputPercent*colorDelta.g,
pLightBinding->colorLow.b + inputPercent*colorDelta.b
};
return outColor;
}
/*!
* \brief takes a ReadAnalogPinData as a user data pointer and returns a vlaue from 0 to 1 based on how high the pin is
* \param pUserData user data to read, should be a ReadAnalogPin
* \return 0 if the pin is low through to a 1 if the pin is high
*/
float readAnalogPin(void* pUserData)
{
struct ReadAnalogPinData* pPinData = (struct ReadAnalogPinData*)pUserData;
int inputValue = analogRead(pPinData->analogPin);
return (float)inputValue/(float)kAnalogInMax;
}