summaryrefslogtreecommitdiff
path: root/src/controller.c
blob: 83d2f25c2550bb2c77c542863433d6a297dfe4b0 (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
/*
 * 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)

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 : %d",state);

	return 0;
}

static void motion_interrupted_cb(uint32_t motion_value, void *user_data)
{
	_D("Detected motion value is: %u", motion_value);

	// Set LED light with value
	_change_led_state(motion_value);

	return;
}


static bool service_app_create(void *user_data)
{
	return true;
}

static void service_app_control(app_control_h app_control, void *user_data)
{
	int ret = 0;

	// Set an interrupted user callback to be invoked when interrupt is triggered on motion sensor
	ret = resource_set_interrupted_cb_infrared_motion(SENSOR_MOTION_GPIO_NUMBER, motion_interrupted_cb, NULL);
	if (ret != 0) {
		_E("cannot set interrupted callback for motion sensor");
		return;
	}
}

static void service_app_terminate(void *user_data)
{
	resource_unset_interrupted_cb_infrared_motion(SENSOR_MOTION_GPIO_NUMBER);

	// 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);
}