aboutsummaryrefslogtreecommitdiffstats
path: root/boards/base/Mikromedia-STM32-M4-ILI9341/ginput_lld_mouse_board.h
blob: 7e9948a5c666cd698560effb00d51e052d132007 (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
/*
 * 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
 */

/**
 * @file    boards/base/Mikromedia-STM32-M4-ILI9341/ginput_lld_mouse_board.h
 * @brief   GINPUT Touch low level driver source for the MCU.
 */

#ifndef _GINPUT_LLD_MOUSE_BOARD_H
#define _GINPUT_LLD_MOUSE_BOARD_H

#define ADC_NUM_CHANNELS   2
#define ADC_BUF_DEPTH      1

static const ADCConversionGroup adcgrpcfg = {
  FALSE,
  ADC_NUM_CHANNELS,
  0,
  0,
  /* HW dependent part.*/
  0,
  ADC_CR2_SWSTART,
  0,
  0,
  ADC_SQR1_NUM_CH(ADC_NUM_CHANNELS),
  0,
  ADC_SQR3_SQ2_N(ADC_CHANNEL_IN8) | ADC_SQR3_SQ1_N(ADC_CHANNEL_IN9)
};

/**
 * @brief   Initialise the board for the touch.
 *
 * @notapi
 */
static inline void init_board(void) {
	adcStart(&ADCD1, 0);
}

/**
 * @brief   Aquire the bus ready for readings
 *
 * @notapi
 */
static inline void aquire_bus(void) {

}

/**
 * @brief   Release the bus after readings
 *
 * @notapi
 */
static inline void release_bus(void) {
}

static inline void setup_x(void) {
   	palSetPad(GPIOB, GPIOB_DRIVEA);
    palClearPad(GPIOB, GPIOB_DRIVEB);
    chThdSleepMilliseconds(2);
}

static inline void setup_y(void) {
    palClearPad(GPIOB, GPIOB_DRIVEA);
    palSetPad(GPIOB, GPIOB_DRIVEB);
    chThdSleepMilliseconds(2);
}

static inline void setup_z(void) {
	palClearPad(GPIOB, GPIOB_DRIVEA);
	palClearPad(GPIOB, GPIOB_DRIVEB);
    chThdSleepMilliseconds(2);
}

static inline uint16_t read_x(void) {
	adcsample_t samples[ADC_NUM_CHANNELS * ADC_BUF_DEPTH];

    adcConvert(&ADCD1, &adcgrpcfg, samples, ADC_BUF_DEPTH);
	return samples[1];
}

static inline uint16_t read_y(void) {
	adcsample_t samples[ADC_NUM_CHANNELS * ADC_BUF_DEPTH];

    adcConvert(&ADCD1, &adcgrpcfg, samples, ADC_BUF_DEPTH);
    return samples[0];
}

static inline uint16_t read_z(void) {
	adcsample_t samples[ADC_NUM_CHANNELS * ADC_BUF_DEPTH];

    adcConvert(&ADCD1, &adcgrpcfg, samples, ADC_BUF_DEPTH);
    // z will go from ~200 to ~4000 when pressed
    // auto range this back to 0 to 100
    if (samples[0] > 4000)
    	return 100;
    if (samples[0] < 400)
    	return 0;
    return (samples[0] - 400) / ((4000-400)/100);
}

#endif /* _GINPUT_LLD_MOUSE_BOARD_H */