summaryrefslogtreecommitdiff
path: root/project/Core/Src/Project/remote_control.c
blob: 2153a4a6c60ad41d11e6c5afa943c57d7d1847fe (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
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
/*
 * remote_control.c
 *
 *  Created on: Nov 4, 2025
 *      Author: sowgro
 */

#include <stm32l4xx.h>
#include <stdio.h>
#include <string.h>
#include "player_actions.h"
#include "UART.h"
#include "project.h"

static char buffer[80];
static char* cur = buffer;

void remote_control_init() {
    NVIC_EnableIRQ (USART2_IRQn);
    USART2->CR1 |= USART_CR1_RXNEIE; // 1 << 5

	help();
}

void remote_control_loop() {

}

static void parse_input(char buffer[]) {
	uint8_t *state = project_get_state();
	if (!strcmp(buffer, "HELP")) {
		help();
	}
	else if (!strcmp(buffer, "NEXT")) {
		next();
	}
	else if (!strcmp(buffer, "PLAY")) {
		play();
		*state = PLAYING_STATE;
	}
	else if (!strcmp(buffer, "PAUSE")) {
		pause();
		*state = PAUSED_STATE;
	}
	else if (!strcmp(buffer, "STOP")) {
		stop();
		*state = STOPPED_STATE;
	}
	else {
		printf("Unknown command \"%s\"\n\r", buffer);
	}
}

#include "systick.h"
void USART2_IRQHandler() {
	int ch = USART_Read(USART2);

	if (project_get_mode() != REMOTE_MODE)
		return;

	putchar(ch);
	if (!ch)
		return;

	if (ch == '\r') {
		putchar('\n');
		*cur = 0;
		parse_input(buffer);
		cur = buffer;
		return;
	}

	if (ch == 0x08 || ch == 0x7F) {
		if (cur <= buffer)
			return;
		cur--;
		printf("\b \b");
		return;
	}

	*cur++ = ch;
}