aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ginput/dial/GADC/ginput_lld_dial.c
blob: ced71a273d3195b72f099c8e60ba3a012e0587b9 (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
/*
    ChibiOS/GFX - Copyright (C) 2012, 2013
                 Joel Bodenmann aka Tectu <joel@unormal.org>

    This file is part of ChibiOS/GFX.

    ChibiOS/GFX is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/GFX is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    drivers/ginput/dial/GADC/ginput_lld_dial.c
 * @brief   GINPUT Dial low level driver source for GADC hardware.
 *
 * @defgroup Dial Dial
 * @ingroup GINPUT
 * @{
 */

#include "ch.h"
#include "hal.h"
#include "gfx.h"

#if GFX_USE_GINPUT && GINPUT_NEED_DIAL

#include "ginput/lld/dial.h"

#if GINPUT_DIAL_NUM_PORTS >= 5
	#error "GINPUT: Dial - GADC driver currently only supports 4 devices"
#endif

static adcsample_t samplebuf[GINPUT_DIAL_NUM_PORTS];

#if GINPUT_DIAL_CYCLE_POLL
	static uint16_t		cycle;
#endif

static void GADCCallback(adcsample_t *buffer, void *param) {
	((DialCallbackFn)param)(buffer-samplebuf, buffer[0]);
}

void ginput_lld_dial_init(void) {
	#if GINPUT_DIAL_CYCLE_POLL
		cycle = 0;
	#endif
}

void ginput_lld_dial_poll(DialCallbackFn fn) {
	#if GINPUT_DIAL_CYCLE_POLL
		switch(cycle++) {
		#if GINPUT_DIAL_NUM_PORTS >= 1
			case 0:
				gadcLowSpeedStart(GINPUT_DIAL_DEVICE0, samplebuf+0, GADCCallback, (void *)fn);
				break;
		#endif
		#if GINPUT_DIAL_NUM_PORTS >= 2
			case 1:
				gadcLowSpeedStart(GINPUT_DIAL_DEVICE1, samplebuf+1, GADCCallback, (void *)fn);
				break;
		#endif
		#if GINPUT_DIAL_NUM_PORTS >= 3
			case 2:
				gadcLowSpeedStart(GINPUT_DIAL_DEVICE2, samplebuf+2, GADCCallback, (void *)fn);
				break;
		#endif
		#if GINPUT_DIAL_NUM_PORTS >= 4
			case 3:
				gadcLowSpeedStart(GINPUT_DIAL_DEVICE3, samplebuf+3, GADCCallback, (void *)fn);
				break;
		#endif
		}
		if (cycle >= GINPUT_DIAL_NUM_PORTS)
			cycle = 0;
	#else
		#if GINPUT_DIAL_NUM_PORTS >= 1
			gadcLowSpeedStart(GINPUT_DIAL_DEVICE0, samplebuf+0, GADCCallback, (void *)fn);
		#endif
		#if GINPUT_DIAL_NUM_PORTS >= 2
			gadcLowSpeedStart(GINPUT_DIAL_DEVICE1, samplebuf+1, GADCCallback, (void *)fn);
		#endif
		#if GINPUT_DIAL_NUM_PORTS >= 3
			gadcLowSpeedStart(GINPUT_DIAL_DEVICE2, samplebuf+2, GADCCallback, (void *)fn);
		#endif
		#if GINPUT_DIAL_NUM_PORTS >= 4
			gadcLowSpeedStart(GINPUT_DIAL_DEVICE3, samplebuf+3, GADCCallback, (void *)fn);
		#endif
	#endif
}

#endif /* GFX_USE_GINPUT && GINPUT_NEED_DIAL */
/** @} */