|
2 | 2 | #include "SerialPortManager.h"
|
3 | 3 | #include "MessageOutput.h"
|
4 | 4 |
|
5 |
| -#define MAX_CONTROLLERS 3 |
6 |
| - |
7 | 5 | SerialPortManagerClass SerialPortManager;
|
8 | 6 |
|
9 | 7 | void SerialPortManagerClass::init()
|
10 | 8 | {
|
11 | 9 | if (ARDUINO_USB_CDC_ON_BOOT != 1) {
|
12 |
| - allocatePort(0, Owner::Console); |
| 10 | + _ports[0] = "Serial Console"; |
| 11 | + MessageOutput.println("[SerialPortManager] HW UART port 0 now in use " |
| 12 | + "by 'Serial Console'"); |
13 | 13 | }
|
14 | 14 | }
|
15 | 15 |
|
16 |
| -bool SerialPortManagerClass::allocateBatteryPort(uint8_t port) |
| 16 | +std::optional<uint8_t> SerialPortManagerClass::allocatePort(std::string const& owner) |
17 | 17 | {
|
18 |
| - return allocatePort(port, Owner::Battery); |
19 |
| -} |
20 |
| - |
21 |
| -bool SerialPortManagerClass::allocateMpptPort(uint8_t port) |
22 |
| -{ |
23 |
| - return allocatePort(port, Owner::MPPT); |
24 |
| -} |
| 18 | + for (size_t i = 0; i < _ports.size(); ++i) { |
| 19 | + if (_ports[i] != "") { |
| 20 | + MessageOutput.printf("[SerialPortManager] HW UART %d already " |
| 21 | + "in use by '%s'\r\n", i, _ports[i].c_str()); |
| 22 | + continue; |
| 23 | + } |
25 | 24 |
|
26 |
| -bool SerialPortManagerClass::allocatePort(uint8_t port, Owner owner) |
27 |
| -{ |
28 |
| - if (port >= MAX_CONTROLLERS) { |
29 |
| - MessageOutput.printf("[SerialPortManager] Invalid serial port: %d\r\n", port); |
30 |
| - return false; |
31 |
| - } |
| 25 | + _ports[i] = owner; |
32 | 26 |
|
33 |
| - auto res = allocatedPorts.insert({port, owner}); |
| 27 | + MessageOutput.printf("[SerialPortManager] HW UART %d now in use " |
| 28 | + "by '%s'\r\n", i, owner.c_str()); |
34 | 29 |
|
35 |
| - if (!res.second) { |
36 |
| - MessageOutput.printf("[SerialPortManager] Cannot assign HW UART " |
37 |
| - "port %d to %s: already in use by %s\r\n", |
38 |
| - port, print(owner), print(res.first->second)); |
39 |
| - return false; |
| 30 | + return i; |
40 | 31 | }
|
41 | 32 |
|
42 |
| - MessageOutput.printf("[SerialPortManager] HW UART port %d now in use " |
43 |
| - "by %s\r\n", port, print(owner)); |
44 |
| - return true; |
45 |
| -} |
46 |
| - |
47 |
| -void SerialPortManagerClass::invalidateBatteryPort() |
48 |
| -{ |
49 |
| - invalidate(Owner::Battery); |
| 33 | + MessageOutput.printf("[SerialPortManager] Cannot assign another HW " |
| 34 | + "UART port to '%s'\r\n", owner.c_str()); |
| 35 | + return std::nullopt; |
50 | 36 | }
|
51 | 37 |
|
52 |
| -void SerialPortManagerClass::invalidateMpptPorts() |
| 38 | +void SerialPortManagerClass::freePort(std::string const& owner) |
53 | 39 | {
|
54 |
| - invalidate(Owner::MPPT); |
55 |
| -} |
| 40 | + for (size_t i = 0; i < _ports.size(); ++i) { |
| 41 | + if (_ports[i] != owner) { continue; } |
56 | 42 |
|
57 |
| -void SerialPortManagerClass::invalidate(Owner owner) |
58 |
| -{ |
59 |
| - for (auto it = allocatedPorts.begin(); it != allocatedPorts.end();) { |
60 |
| - if (it->second == owner) { |
61 |
| - MessageOutput.printf("[SerialPortManager] Removing port = %d, owner = %s \r\n", it->first, print(owner)); |
62 |
| - it = allocatedPorts.erase(it); |
63 |
| - } else { |
64 |
| - ++it; |
65 |
| - } |
66 |
| - } |
67 |
| -} |
68 |
| - |
69 |
| -const char* SerialPortManagerClass::print(Owner owner) |
70 |
| -{ |
71 |
| - switch (owner) { |
72 |
| - case Owner::Console: |
73 |
| - return "Serial Console"; |
74 |
| - case Owner::Battery: |
75 |
| - return "Battery Interface"; |
76 |
| - case Owner::MPPT: |
77 |
| - return "Victron MPPT"; |
| 43 | + MessageOutput.printf("[SerialPortManager] Freeing HW UART %d, owner " |
| 44 | + "was '%s'\r\n", i, owner.c_str()); |
| 45 | + _ports[i] = ""; |
78 | 46 | }
|
79 |
| - return "unknown"; |
80 | 47 | }
|
0 commit comments