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

#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)
};

static inline void init_board(void) {
	adcStart(&ADCD1, 0);
}

static inline void aquire_bus(void) {

}

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 */