1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* LEDArray implementation */
18
19#include "sles_allinclusive.h"
20
21
22static SLresult ILEDArray_ActivateLEDArray(SLLEDArrayItf self, SLuint32 lightMask)
23{
24    SL_ENTER_INTERFACE
25
26    ILEDArray *thiz = (ILEDArray *) self;
27    interface_lock_poke(thiz);
28    thiz->mLightMask = lightMask;
29    interface_unlock_poke(thiz);
30    result = SL_RESULT_SUCCESS;
31
32    SL_LEAVE_INTERFACE
33}
34
35
36static SLresult ILEDArray_IsLEDArrayActivated(SLLEDArrayItf self, SLuint32 *pLightMask)
37{
38    SL_ENTER_INTERFACE
39
40    if (NULL == pLightMask) {
41        result = SL_RESULT_PARAMETER_INVALID;
42    } else {
43        ILEDArray *thiz = (ILEDArray *) self;
44        interface_lock_peek(thiz);
45        SLuint32 lightMask = thiz->mLightMask;
46        interface_unlock_peek(thiz);
47        *pLightMask = lightMask;
48        result = SL_RESULT_SUCCESS;
49    }
50
51    SL_LEAVE_INTERFACE
52}
53
54
55static SLresult ILEDArray_SetColor(SLLEDArrayItf self, SLuint8 index, const SLHSL *pColor)
56{
57    SL_ENTER_INTERFACE
58
59    result = SL_RESULT_PARAMETER_INVALID;
60    do {
61        if (!(index < MAX_LED_COUNT) || NULL == pColor)
62            break;
63        SLHSL color = *pColor;
64        if (!(0 <= color.hue && color.hue <= 360000))
65            break;
66        if (!(0 <= color.saturation && color.saturation <= 1000))
67            break;
68        if (!(0 <= color.lightness && color.lightness <= 1000))
69            break;
70        ILEDArray *thiz = (ILEDArray *) self;
71        // can't use poke because struct copy might not be atomic
72        interface_lock_exclusive(thiz);
73        thiz->mColors[index] = color;
74        interface_unlock_exclusive(thiz);
75        result = SL_RESULT_SUCCESS;
76    } while (0);
77
78    SL_LEAVE_INTERFACE
79}
80
81
82static SLresult ILEDArray_GetColor(SLLEDArrayItf self, SLuint8 index, SLHSL *pColor)
83{
84    SL_ENTER_INTERFACE
85
86    if (!(index < MAX_LED_COUNT) || NULL == pColor) {
87        result = SL_RESULT_PARAMETER_INVALID;
88    } else {
89        ILEDArray *thiz = (ILEDArray *) self;
90        // can't use peek because struct copy might not be atomic
91        interface_lock_shared(thiz);
92        SLHSL color = thiz->mColors[index];
93        interface_unlock_shared(thiz);
94        *pColor = color;
95        result = SL_RESULT_SUCCESS;
96    }
97
98    SL_LEAVE_INTERFACE
99}
100
101
102static const struct SLLEDArrayItf_ ILEDArray_Itf = {
103    ILEDArray_ActivateLEDArray,
104    ILEDArray_IsLEDArrayActivated,
105    ILEDArray_SetColor,
106    ILEDArray_GetColor
107};
108
109void ILEDArray_init(void *self)
110{
111    ILEDArray *thiz = (ILEDArray *) self;
112    thiz->mItf = &ILEDArray_Itf;
113    thiz->mLightMask = 0;
114    SLHSL *color = thiz->mColors;
115    SLuint8 index;
116    for (index = 0; index < MAX_LED_COUNT; ++index, ++color) {
117        color->hue = 0; // red, but per specification 1.0.1 pg. 259: "Default color is undefined."
118        color->saturation = 1000;
119        color->lightness = 1000;
120    }
121    // const
122    thiz->mCount = MAX_LED_COUNT;
123}
124