1 /*
2 * Copyright (C) 2017 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 #include <stdlib.h>
18 #include <string.h>
19
20 #include <eventnums.h>
21 #include <gpio.h>
22 #include <hostIntf.h>
23 #include <leds_gpio.h>
24 #include <nanohubPacket.h>
25 #include <sensors.h>
26 #include <seos.h>
27 #include <plat/gpio.h>
28 #include <plat/exti.h>
29 #include <plat/syscfg.h>
30 #include <variant/variant.h>
31
32 #define LEDS_GPIO_APP_ID APP_ID_MAKE(NANOHUB_VENDOR_GOOGLE, 20)
33 #define LEDS_GPIO_APP_VERSION 1
34
35 #define DBG_ENABLE 0
36
37 static struct LedsTask
38 {
39 struct Gpio *led[LEDS_GPIO_MAX];
40 uint32_t num;
41
42 uint32_t id;
43 uint32_t sHandle;
44 } mTask;
45
sensorConfigLedsGpio(void * cfgData,void * buf)46 static bool sensorConfigLedsGpio(void *cfgData, void *buf)
47 {
48 struct LedsCfg *lcfg = (struct LedsCfg *)cfgData;
49
50 if (lcfg->led_num >= mTask.num) {
51 osLog(LOG_INFO, "Wrong led number %"PRIu32"\n", lcfg->led_num);
52 return false;
53 }
54 gpioSet(mTask.led[lcfg->led_num], lcfg->value ? 1 : 0);
55 osLog(LOG_INFO, "Set led[%"PRIu32"]=%"PRIu32"\n", lcfg->led_num, lcfg->value);
56 return true;
57 }
58
sensorSelfTestLedsGpio(void * buf)59 static bool sensorSelfTestLedsGpio(void *buf)
60 {
61 uint32_t i;
62
63 gpioSet(mTask.led[0], 1);
64 for (i=1; i < mTask.num; i++) {
65 gpioSet(mTask.led[i-1], 0);
66 gpioSet(mTask.led[i], 1);
67 }
68 gpioSet(mTask.led[i-1], 0);
69 return true;
70 }
71
72 static const struct SensorInfo sensorInfoLedsGpio = {
73 .sensorName = "Leds-Gpio",
74 .sensorType = SENS_TYPE_LEDS,
75 };
76
77 static const struct SensorOps sensorOpsLedsGpio = {
78 .sensorCfgData = sensorConfigLedsGpio,
79 .sensorSelfTest = sensorSelfTestLedsGpio,
80 };
81
handleEvent(uint32_t evtType,const void * evtData)82 static void handleEvent(uint32_t evtType, const void *evtData)
83 {
84 switch (evtType) {
85 case EVT_APP_START:
86 osEventUnsubscribe(mTask.id, EVT_APP_START);
87 /* Test leds */
88 if (DBG_ENABLE)
89 sensorSelfTest(mTask.sHandle);
90 osLog(LOG_INFO, "[Leds-Gpio] detected\n");
91 break;
92 }
93 }
94
startTask(uint32_t taskId)95 static bool startTask(uint32_t taskId)
96 {
97 const struct LedsGpio *leds;
98 struct Gpio *led;
99 uint32_t i;
100
101 mTask.id = taskId;
102 mTask.num = 0;
103
104 leds = ledsGpioBoardCfg();
105 for (i=0; i < leds->num && i < LEDS_GPIO_MAX; i++) {
106 led = gpioRequest(leds->leds_array[i]);
107 if (!led)
108 continue;
109 mTask.led[mTask.num++] = led;
110 gpioConfigOutput(led, GPIO_SPEED_LOW, GPIO_PULL_NONE, GPIO_OUT_PUSH_PULL, 0);
111 }
112 if (mTask.num == 0)
113 return false;
114 mTask.sHandle = sensorRegister(&sensorInfoLedsGpio, &sensorOpsLedsGpio, NULL, true);
115 osEventSubscribe(taskId, EVT_APP_START);
116 return true;
117 }
118
endTask(void)119 static void endTask(void)
120 {
121 uint32_t i;
122
123 sensorUnregister(mTask.sHandle);
124 for (i=0; i < mTask.num; i++) {
125 gpioSet(mTask.led[i], 0);
126 gpioRelease(mTask.led[i]);
127 }
128 }
129
130 INTERNAL_APP_INIT(LEDS_GPIO_APP_ID, LEDS_GPIO_APP_VERSION, startTask, endTask, handleEvent);
131