summaryrefslogtreecommitdiff
path: root/project/Core/Src/Project/project.c
blob: 428dc545f05c6023904cae4948408ab7607c6e00 (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
/*
 * project.c
 *
 *  Created on: Jan 8, 2023
 *      Author: bruce
 */

#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include "project.h"
#include "player_actions.h"
#include "hw4.h"
#include "player_actions.h"

void project_run() {
	char buffer[80];
	char* cur = buffer;
	uint8_t state = STOPPED_STATE;

	help();
	for (;;) {
		if (state == PAUSED_STATE)
			pause_loop();
		char ch = read_and_print_char_nonblocking();
		if (!ch)
			continue;
		if (ch == '\r') {
			*cur = 0;
			handle_input(buffer, &state);
			cur = buffer;
			continue;
		}
		if (ch == 0x08 || ch == 0x7F) {
			if (cur <= buffer)
				continue;
			cur--;
			printf("\b \b");
			continue;
		}
		*cur++ = ch;
	}
}

void handle_input(char buffer[], uint8_t *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);
	}
}