summaryrefslogtreecommitdiff
path: root/project/Core/Src/Project/project.c
blob: 379f380f08db943b90b49c6f92fe592ee4fd994d (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * project.c
 *
 *  Created on: Jan 8, 2023
 *      Author: bruce
 */

#include "project.h"
#include "LED.h"
#include "systick.h"
#include <stdio.h>
#include "hw4.h"
#include <string.h>

#define PLAYING_STATE (1)
#define PAUSED_STATE (2)
#define STOPPED_STATE (3)

void handle_input(char buffer[], uint8_t *state);
void help();
void next();
void play();
void pause();
void pause_loop();
void stop();

void project_run() {
	char buffer[8];
	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;
		}
		*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);
	}
}

void help() {
	printf("\r***REMOTE LED CONTROL MENU***\r\n");
	printf("Available User Commands:\r\n");
	printf("NEXT - Show next song info\r\n");
	printf("PLAY - Play the song (LED on)\r\n");
	printf("PAUSE - Pause the song (LED flash)\r\n");
	printf("STOP - Stop the song (LED off)\r\n");
}

void next() {
	// TODO
}

void play() {
	LED_On();
}

void pause() {
	init_systick();
}

void pause_loop() {
	if (check_systick()) {
		LED_Toggle();
	}
}

void stop() {
	LED_Off();
}