summaryrefslogtreecommitdiff
path: root/src/controller.c
blob: 6595ea2e27ebad9d1bb65deb1e1e744f1fcccfc7 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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);
}