summaryrefslogtreecommitdiff
path: root/project/Core/Src/Project/local_control.c
blob: ce40c22b20a32f2d5e6f4c5ab58c76a352cf7496 (plain) (blame)
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
/*
 * local_control.c
 *
 *  Created on: Nov 4, 2025
 *      Author: sowgro
 */

#include <stm32l4xx.h>
#include <stdio.h>
#include <stdint.h>
#include "GPIO.h"
#include "project.h"

void local_control_init() {
	GPIO_Init();
	NVIC_EnableIRQ(EXTI9_5_IRQn);
}

void local_control_loop() {

}

void EXTI9_5_IRQHandler() {
	static int prevStatus = -1;
    if (~EXTI->PR1 & EXTI_PR1_PIF9)
    	return;
    EXTI->PR1 |= EXTI_PR1_PIF9;

	if (project_get_mode() != LOCAL_MODE)
		return;

    int status = !!(GPIOA->IDR & GPIO_PIN_9);
    if (prevStatus == status)
    	return;

    handle_press(status);
}

uint32_t time_down = 0;
void handle_press(int pressed) {
	uint32_t now = systick_get_count();
	if (pressed) {
		time_down = now;
	} else {
		printf("now: %i, time_down: %i diff: %i\r\n", now, time_down, now - time_down);
		if (now - time_down < 10) {
			//ignore
		} else if (now - time_down < 10000) {
			printf("SHORT PRESS\r\n");
		} else {
			printf("LONG_PRESS\r\n");
		}
		time_down = 0;
	}
}