forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In order to prevent the battery and the Victron MPPT to use the same hw serial ports, this class keeps track of the used ports and their owners.
- Loading branch information
1 parent
18e3b12
commit 0cbdb44
Showing
5 changed files
with
118 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <map> | ||
|
||
class SerialPortManager { | ||
public: | ||
bool allocateMpptPort(int port); | ||
bool allocateBatteryPort(int port); | ||
void invalidateBatteryPort(); | ||
void invalidateMpptPorts(); | ||
|
||
private: | ||
enum Owner { | ||
BATTERY, | ||
MPPT | ||
}; | ||
|
||
std::map<uint8_t, Owner> allocatedPorts; | ||
|
||
bool allocatePort(uint8_t port, Owner owner); | ||
void invalidate(Owner owner); | ||
|
||
static const char* print(Owner owner); | ||
}; | ||
|
||
extern SerialPortManager PortManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#include "SerialPortManager.h" | ||
#include "MessageOutput.h" | ||
|
||
#define MAX_CONTROLLERS 3 | ||
|
||
SerialPortManager PortManager; | ||
|
||
bool SerialPortManager::allocateBatteryPort(int port) | ||
{ | ||
return allocatePort(port, Owner::BATTERY); | ||
} | ||
|
||
bool SerialPortManager::allocateMpptPort(int port) | ||
{ | ||
return allocatePort(port, Owner::MPPT); | ||
} | ||
|
||
bool SerialPortManager::allocatePort(uint8_t port, Owner owner) | ||
{ | ||
if (port > MAX_CONTROLLERS) { | ||
MessageOutput.printf("[SerialPortManager] Invalid serial port = %d \r\n", port); | ||
return false; | ||
} | ||
|
||
return allocatedPorts.insert({port, owner}).second; | ||
} | ||
|
||
void SerialPortManager::invalidateBatteryPort() | ||
{ | ||
invalidate(Owner::BATTERY); | ||
} | ||
|
||
void SerialPortManager::invalidateMpptPorts() | ||
{ | ||
invalidate(Owner::MPPT); | ||
} | ||
|
||
void SerialPortManager::invalidate(Owner owner) | ||
{ | ||
for (auto it = allocatedPorts.begin(); it != allocatedPorts.end();) { | ||
if (it->second == owner) { | ||
MessageOutput.printf("[SerialPortManager] Removing port = %d, owner = %s \r\n", it->first, print(owner)); | ||
it = allocatedPorts.erase(it); | ||
} else { | ||
++it; | ||
} | ||
} | ||
} | ||
|
||
const char* SerialPortManager::print(Owner owner) | ||
{ | ||
switch (owner) { | ||
case BATTERY: | ||
return "BATTERY"; | ||
case MPPT: | ||
return "MPPT"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters