summaryrefslogtreecommitdiff
path: root/src/controller.c
blob: bbb21106005cb56d3beb35a1511d12ec60e50a77 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * 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_sensor.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)

typedef struct app_data_s {
	Ecore_Timer *getter_timer;
} app_data;

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 Eina_Bool _get_motion_set_led()
{
	int ret = 0;
	uint32_t value = 0;

	if (!ad) {
		_E("failed to get app_data");
		return ECORE_CALLBACK_CANCEL;
	}

	// Get value from motion sensor
	ret = resource_read_infrared_motion_sensor(SENSOR_MOTION_GPIO_NUMBER, &value);
	if (ret != 0) {
		_E("cannot read data from the infrared motion sensor");
		return ECORE_CALLBACK_CANCEL;
	}

	_D("Detected motion value is: %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)
{
	app_data *ad = user_data;

	// Delete old timer if there is one
	if (ad->getter_timer)
		ecore_timer_del(ad->getter_timer);

	// Create a timer to call the given function in given period of time
	ad->getter_timer = ecore_timer_add(TIMER_GATHER_INTERVAL, _get_motion_set_led, ad);
	if (!ad->getter_timer) {
		_E("Failed to add getter timer");
		return;
	}
}

static void service_app_terminate(void *user_data)
{
	app_data *ad = user_data;

	// Delete timer
	if (ad->getter_timer)
		ecore_timer_del(ad->getter_timer);

	// Turn off LED light with __set_led()
	_change_led_state(0);

	// Close Motion and LED resources
	resource_close_infrared_motion_sensor();
	resource_close_led();

	// Free app data
	free(ad);

	FN_END;
}

int main(int argc, char *argv[])
{
	app_data *ad = NULL;
	service_app_lifecycle_callback_s event_callback;

	ad = calloc(1, sizeof(app_data));
	retv_if(!ad, -1);

	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, ad);
}