-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage_volume.cpp
More file actions
53 lines (31 loc) · 1.36 KB
/
Copy pathpage_volume.cpp
File metadata and controls
53 lines (31 loc) · 1.36 KB
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
#include "fm_radio.h"
#include "structure.h"
namespace PageVolume{
Page page;
void volume_apply(){
char *buf = page.value; // for simplisty
if (!isdigit((unsigned char)buf[0]) || !isdigit((unsigned char)buf[1])){
// invalid input ignore or handle error this
// error should not occure unless something
// goes really really wrong
return;
}
// convert two ASCII digits to integer
int volume = (buf[0] - '0') * 10 + (buf[1] - '0');
// we double check the numbers just in case even though the clock already
// checks them for us but just in case something goes wrong
if (volume < 0) volume = 0;
if (volume > 15) volume = 15;
// apply the volume
FM_Radio::set_volume(volume);
}
void init(){
size_t value_size = sizeof(page.value); // get the size of the string array
size_t title_size = sizeof(page.title); // get the size of the string array
strncpy(page.title, "< VOLUME(1-15) >", title_size);
snprintf(page.value, value_size, "%02d ", FM_Radio::get_current_volume());
page.value[value_size - 1] = "\0"; // add the terminator
page.is_editable = true;
page.apply_function = volume_apply;
}
}