summaryrefslogtreecommitdiff
path: root/src/controller.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/controller.c')
-rwxr-xr-xsrc/controller.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/controller.c b/src/controller.c
new file mode 100755
index 0000000..6595ea2
--- /dev/null
+++ b/src/controller.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <tizen.h>
+#include <service_app.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <Ecore.h>
+
+#include "log.h"
+#include "resource/resource_infrared_motion.h"
+#include "resource/resource_led.h"
+
+// Timer duration
+#define TIMER_GATHER_INTERVAL (2.0f)
+
+// Motion sensor information
+#define SENSOR_MOTION_GPIO_NUMBER (18)
+
+// LED sensor information
+#define SENSOR_LED_GPIO_NUMBER (24)
+
+Ecore_Timer *getter_timer = NULL;
+
+static int _change_led_state(int state) {
+ int ret = 0;
+
+ // Write state to LED light
+ ret = resource_write_led(SENSOR_LED_GPIO_NUMBER, state);
+ if (ret != 0) {
+ _E("cannot write led data");
+ return -1;
+ }
+
+ _I("LED State : %s", state ? "ON":"OFF");
+
+ return 0;
+}
+
+static Eina_Bool _get_motion_set_led(void *user_data)
+{
+ int ret = 0;
+ uint32_t value = 0;
+
+ // Get value from motion sensor
+ ret = resource_read_infrared_motion(SENSOR_MOTION_GPIO_NUMBER, &value);
+ if (ret != 0) {
+ _E("cannot read data from the infrared motion sensor");
+ return ECORE_CALLBACK_CANCEL;
+ }
+
+ _I("Detected motion value : %u", value);
+
+ // Set LED light with value
+ _change_led_state(value);
+
+ return ECORE_CALLBACK_RENEW;
+}
+
+static bool service_app_create(void *user_data)
+{
+ return true;
+}
+
+static void service_app_control(app_control_h app_control, void *user_data)
+{
+ // Delete old timer if there is one
+ if (getter_timer) {
+ ecore_timer_del(getter_timer);
+ getter_timer = NULL;
+ }
+
+ // Create a timer to call the given function in given period of time
+ getter_timer = ecore_timer_add(TIMER_GATHER_INTERVAL, _get_motion_set_led, user_data);
+ if (!getter_timer) {
+ _E("Failed to add getter timer");
+ return;
+ }
+}
+
+static void service_app_terminate(void *user_data)
+{
+ // Delete timer
+ if (getter_timer) {
+ ecore_timer_del(getter_timer);
+ getter_timer = NULL;
+ }
+
+ // Turn off LED light with __set_led()
+ _change_led_state(0);
+
+ // Close Motion and LED resources
+ resource_close_infrared_motion();
+ resource_close_led();
+
+ FN_END;
+}
+
+int main(int argc, char *argv[])
+{
+ service_app_lifecycle_callback_s event_callback;
+
+ event_callback.create = service_app_create;
+ event_callback.terminate = service_app_terminate;
+ event_callback.app_control = service_app_control;
+
+ return service_app_main(argc, argv, &event_callback, NULL);
+}