summaryrefslogtreecommitdiff
path: root/test/motion_sense.c
blob: ab07a41e965579538ddc047f2de8795ef4485479 (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
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test motion sense code.
 */

#include <math.h>

#include "common.h"
#include "motion_sense.h"
#include "task.h"
#include "test_util.h"
#include "timer.h"

/* Mock acceleration values for motion sense task to read in. */
int mock_x_acc[ACCEL_COUNT], mock_y_acc[ACCEL_COUNT], mock_z_acc[ACCEL_COUNT];

/*****************************************************************************/
/* Mock functions */

int accel_init(enum accel_id id)
{
	return EC_SUCCESS;
}

int accel_read(enum accel_id id, int *x_acc, int *y_acc, int *z_acc)
{
	/* Return the mock values. */
	*x_acc = mock_x_acc[id];
	*y_acc = mock_y_acc[id];
	*z_acc = mock_z_acc[id];

	return EC_SUCCESS;
}

int accel_set_range(const enum accel_id id, const int range, const int rnd)
{
	return EC_SUCCESS;
}
int accel_get_range(const enum accel_id id, int * const range)
{
	return EC_SUCCESS;
}
int accel_set_resolution(const enum accel_id id, const int res, const int rnd)
{
	return EC_SUCCESS;
}
int accel_get_resolution(const enum accel_id id, int * const res)
{
	return EC_SUCCESS;
}
int accel_set_datarate(const enum accel_id id, const int rate, const int rnd)
{
	return EC_SUCCESS;
}
int accel_get_datarate(const enum accel_id id, int * const rate)
{
	return EC_SUCCESS;
}


/*****************************************************************************/
/* Test utilities */
static int test_lid_angle(void)
{
	/*
	 * Set the base accelerometer as if it were sitting flat on a desk
	 * and set the lid to closed.
	 */
	mock_x_acc[ACCEL_BASE] = 0;
	mock_y_acc[ACCEL_BASE] = 0;
	mock_z_acc[ACCEL_BASE] = 1000;
	mock_x_acc[ACCEL_LID] = 0;
	mock_y_acc[ACCEL_LID] = 0;
	mock_z_acc[ACCEL_LID] = 1000;
	task_wake(TASK_ID_MOTIONSENSE);
	msleep(5);
	TEST_ASSERT(motion_get_lid_angle() == 0);

	/* Set lid open to 90 degrees. */
	mock_x_acc[ACCEL_LID] = -1000;
	mock_y_acc[ACCEL_LID] = 0;
	mock_z_acc[ACCEL_LID] = 0;
	task_wake(TASK_ID_MOTIONSENSE);
	msleep(5);
	TEST_ASSERT(motion_get_lid_angle() == 90);

	/* Set lid open to -135. */
	mock_x_acc[ACCEL_LID] = 500;
	mock_y_acc[ACCEL_LID] = 0;
	mock_z_acc[ACCEL_LID] = -500;
	task_wake(TASK_ID_MOTIONSENSE);
	msleep(5);
	TEST_ASSERT(motion_get_lid_angle() == -135);

	/*
	 * Align base with hinge and make sure it returns unreliable for angle.
	 * In this test it doesn't matter what the lid acceleration vector is.
	 */
	mock_x_acc[ACCEL_BASE] = 0;
	mock_y_acc[ACCEL_BASE] = 1000;
	mock_z_acc[ACCEL_BASE] = 0;
	task_wake(TASK_ID_MOTIONSENSE);
	msleep(5);
	TEST_ASSERT(motion_get_lid_angle() == LID_ANGLE_UNRELIABLE);

	/*
	 * Use all three axes and set lid to negative base and make sure
	 * angle is 180.
	 */
	mock_x_acc[ACCEL_BASE] = 500;
	mock_y_acc[ACCEL_BASE] = 400;
	mock_z_acc[ACCEL_BASE] = 300;
	mock_x_acc[ACCEL_LID] = -500;
	mock_y_acc[ACCEL_LID] = -400;
	mock_z_acc[ACCEL_LID] = -300;
	task_wake(TASK_ID_MOTIONSENSE);
	msleep(5);
	TEST_ASSERT(motion_get_lid_angle() == 180);

	return EC_SUCCESS;
}


void run_test(void)
{
	test_reset();

	RUN_TEST(test_lid_angle);

	test_print_result();
}