-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBandFilter.cpp
157 lines (150 loc) · 3.85 KB
/
BandFilter.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "BandFilter.h"
#include "vfo.h"
void BandFilter::initFilter()
{
std::vector<std::string> devices;
std::vector<std::string> addresses;
devices = Settings_file.get_array_string("i2c", "devices");
addresses = Settings_file.get_array_string("i2c", "address");
int index = 0;
for (auto col : devices)
{
if (col == "PCF8574" || col == "PCF8574A")
{
int I2Caddress;
sscanf(addresses.at(index).c_str(),"%x",&I2Caddress);
PCF8574 i2cdevice(I2Caddress);
if (i2cdevice.begin(0))
{
i2cDevices.push_back(i2cdevice);
printf("Connected to PCF8574(A) %d \n", (int)i2cdevice.getAddress());
}
else
{
i2cDevices.push_back(i2cdevice);
printf("Cannot connect to %d \n", (int)i2cdevice.getAddress());
}
}
if (col == "TCA9548")
{
int I2Caddress;
sscanf(addresses.at(index).c_str(), "%x", &I2Caddress);
TCA9548V2 i2cdevice(I2Caddress);
if (i2cdevice.begin(0))
{
i2cDevices.push_back(i2cdevice);
printf("Connected to TCA9548 %d \n", (int)i2cdevice.getAddress());
}
else
{
i2cDevices.push_back(i2cdevice);
printf("Cannot connect to %d \n", (int)i2cdevice.getAddress());
}
}
if (col == "MCP23008")
{
int I2Caddress;
sscanf(addresses.at(index).c_str(), "%x", &I2Caddress);
MCP23008 i2cdevice(I2Caddress);
if (i2cdevice.begin())
{
i2cdevice.pinMode8(0);
i2cdevice.write8(0x00);
i2cDevices.push_back(i2cdevice);
printf("Connected to MCP23008 %d \n", (int)i2cdevice.getAddress());
}
else
{
i2cDevices.push_back(i2cdevice);
printf("Cannot connect to %d \n", (int)i2cdevice.getAddress());
}
}
index++;
}
}
void BandFilter::SetBand(int band, bool rx)
{
int index = vfo.getBandIndex(band);
char str[80];
std::vector<int> I2CCommands_rx, I2CCommands_tx;
if (!bandfilter_pass_trough)
sprintf(str, "%dm", band);
else
strcpy(str, "Through");
std::string bandMeters(str);
std::vector<std::string> bandCommands = Settings_file.get_array_string("i2c", bandMeters);
int i = 0;
for (auto col : bandCommands)
{
int I2Caddress;
sscanf(col.c_str(), "%x", &I2Caddress);
if (i < bandCommands.size() / 2)
I2CCommands_rx.push_back(I2Caddress);
else
I2CCommands_tx.push_back(I2Caddress);
i++;
}
if (i2cDevices.size() > 0)
{
const auto startTime = std::chrono::high_resolution_clock::now();
int ii = 0;
printf("%d m i2c ", band);
for (auto &col : i2cDevices)
{
if (rx)
{
if (I2CCommands_rx.size() > ii)
{
switch (col.index())
{
case 0:
if (std::get<PCF8574>(col).getConnected())
std::get<PCF8574>(col).write8(I2CCommands_rx.at(ii));
break;
case 1:
if (std::get<TCA9548V2>(col).getConnected())
std::get<TCA9548V2>(col).setChannelMask(I2CCommands_rx.at(ii));
break;
case 2:
if (std::get<MCP23008>(col).getConnected())
std::get<MCP23008>(col).write8(I2CCommands_rx.at(ii));
break;
}
printf("rx %d ", I2CCommands_rx.at(ii));
}
}
else
{
if (I2CCommands_tx.size() > ii)
{
switch (col.index())
{
case 0:
if (std::get<PCF8574>(col).getConnected())
std::get<PCF8574>(col).write8(I2CCommands_tx.at(ii));
break;
case 1:
if (std::get<TCA9548V2>(col).getConnected())
std::get<TCA9548V2>(col).setChannelMask(I2CCommands_tx.at(ii));
break;
case 2:
if (std::get<MCP23008>(col).getConnected())
std::get<MCP23008>(col).write8(I2CCommands_tx.at(ii));
break;
}
printf("tx %d ", I2CCommands_tx.at(ii));
}
}
ii++;
}
auto now = std::chrono::high_resolution_clock::now();
const auto timePassed = std::chrono::duration_cast<std::chrono::microseconds>(now - startTime);
printf(" I2C time %ld\n", timePassed.count());
return;
}
}
void BandFilter::Setpasstrough(bool b)
{
bandfilter_pass_trough = b;
SetBand(vfo.get_band_in_meters(), vfo.get_rx());
}