buttons.c 937 Bytes
Newer Older
Rahix's avatar
Rahix committed
1
#include "epicardium.h"
2
3
#include "modules/modules.h"
#include "modules/log.h"
Rahix's avatar
Rahix committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include "portexpander.h"
#include "MAX77650-Arduino-Library.h"

#include <stdint.h>

static const uint8_t pin_mask[] = {
	[BUTTON_LEFT_BOTTOM]  = 1 << 5,
	[BUTTON_RIGHT_BOTTOM] = 1 << 3,
	[BUTTON_RIGHT_TOP]    = 1 << 6,
};

uint8_t epic_buttons_read(uint8_t mask)
{
	uint8_t ret = 0;
koalo's avatar
koalo committed
19
	if (portexpander_detected() && (mask & 0x7)) {
20
21
22
23
24
		if (hwlock_acquire(HWLOCK_I2C, pdMS_TO_TICKS(100)) < 0) {
			LOG_ERR("buttons", "Can't acquire I2C bus");
			return 0;
		}

25
26
27
28
		/*
		 * Not using PB_Get() here as that performs one I2C transcation
		 * per button.
		 */
29
		uint8_t pin_status = ~portexpander_in_get(0xFF);
Rahix's avatar
Rahix committed
30

31
32
		hwlock_release(HWLOCK_I2C);

Rahix's avatar
Rahix committed
33
34
35
36
37
38
39
40
41
42
43
44
45
		for (uint8_t m = 1; m < 0x8; m <<= 1) {
			if (mask & m && pin_status & pin_mask[m]) {
				ret |= m;
			}
		}
	}

	if (mask & BUTTON_RESET && MAX77650_getDebounceStatusnEN0()) {
		ret |= BUTTON_RESET;
	}

	return ret;
}