-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage_channel.cpp
More file actions
57 lines (35 loc) · 1.46 KB
/
Copy pathpage_channel.cpp
File metadata and controls
57 lines (35 loc) · 1.46 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
54
55
56
57
#include "fm_radio.h"
#include "structure.h"
namespace PageChannel{
Page page;
void frequency_channel_apply(){
char *buf = page.value; // for simplisity
// double check the chars just in case again if this leads to return
// we got bigger problems at our hand
for (int i = 0; i < 4; i++){
if (!isdigit((unsigned char)buf[i])){
return;
}
}
// convert four ASCII digits to integer
int freq10 = 0;
freq10 += (buf[0] - '0') * 1000;
freq10 += (buf[1] - '0') * 100;
freq10 += (buf[2] - '0') * 10;
freq10 += (buf[3] - '0');
// again just in case we clamp the channel
if (freq10 < 875) freq10 = 875;
if (freq10 > 1080) freq10 = 1080;
// we send the channel to the function so it gets set
FM_Radio::set_channel(freq10);
}
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, "<CHANNL(MHZx10)>", title_size);
snprintf(page.value, value_size ,"%04d ", FM_Radio::get_current_channel());
page.value[value_size -1] = "\0"; // add the terminator because of snprintf
page.is_editable = true;
page.apply_function = frequency_channel_apply;
}
}