aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ginput/touch/MCU/ginput_lld_mouse.c
blob: ad2519e453f37810f30c48b9aac093cbde96ab7f (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
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */

#include "gfx.h"

#if (GFX_USE_GINPUT && GINPUT_NEED_MOUSE) /*|| defined(__DOXYGEN__)*/

#include "src/ginput/driver_mouse.h"

#include "ginput_lld_mouse_board.h"

static uint16_t sampleBuf[7];

static void filter(void) {
	uint16_t temp;
	int i,j;

	for(i = 0; i < 4; i++) {
		for(j = i; j < 7; j++) {
			if(sampleBuf[i] > sampleBuf[j]) {
				/* Swap the values */
				temp = sampleBuf[i];
				sampleBuf[i] = sampleBuf[j];
				sampleBuf[j] = temp;
			}
		}
	}
}

void ginput_lld_mouse_init(void) {
	init_board();
}

void ginput_lld_mouse_get_reading(MouseReading *pt) {
	uint16_t i;

	// Obtain access to the bus
	aquire_bus();

	// Read the ADC for z, x, y and then z again
	while(1) {
		setup_z();
		for(i = 0; i < 7; i++)
			sampleBuf[i] = read_z();
		filter();
		pt->z = (coord_t)sampleBuf[3];

		setup_x();
		for(i = 0; i < 7; i++)
			sampleBuf[i] = read_x();
		filter();
		pt->x = (coord_t)sampleBuf[3];

		setup_y();
		for(i = 0; i < 7; i++)
			sampleBuf[i] = read_y();
		filter();
		pt->y = (coord_t)sampleBuf[3];

		pt->buttons = pt->z >= 80 ? GINPUT_TOUCH_PRESSED : 0;

		setup_z();
		for(i = 0; i < 7; i++)
			sampleBuf[i] = read_z();
		filter();
		i = (coord_t)sampleBuf[3] >= 80 ? GINPUT_TOUCH_PRESSED : 0;

		if (pt->buttons == i)
			break;
	}

	// Release the bus
	release_bus();
}

#endif /* GFX_USE_GINPUT && GINPUT_NEED_MOUSE */