Skip to content

Commit

Permalink
added Lamp, Coil and Switch Test
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner committed Feb 13, 2025
1 parent f68b890 commit 3cc1bc5
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 1 deletion.
144 changes: 144 additions & 0 deletions src/PPUC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ void PPUC::SendLedConfigBlock(const YAML::Node& items, uint32_t type,
m_pRS485Comm->SendConfigEvent(
new ConfigEvent(board, (uint8_t)CONFIG_TOPIC_LAMPS, index++,
(uint8_t)CONFIG_TOPIC_COLOR, color));

m_lamps.push_back(PPUCLamp((uint8_t)type, n_item["number"].as<uint8_t>(),
n_item["description"].as<std::string>()));
}
}

Expand Down Expand Up @@ -177,6 +180,10 @@ bool PPUC::Connect() {
n_switch["board"].as<uint8_t>(), (uint8_t)CONFIG_TOPIC_SWITCHES,
index++, (uint8_t)CONFIG_TOPIC_NUMBER,
n_switch["number"].as<uint32_t>()));

m_switches.push_back(
PPUCSwitch(n_switch["number"].as<uint8_t>(),
n_switch["description"].as<std::string>()));
}
}

Expand Down Expand Up @@ -285,6 +292,10 @@ bool PPUC::Connect() {
m_pRS485Comm->SendConfigEvent(new ConfigEvent(
n_pwmOutput["board"].as<uint8_t>(), (uint8_t)CONFIG_TOPIC_PWM,
index++, (uint8_t)CONFIG_TOPIC_TYPE, type));

m_coils.push_back(
PPUCCoil((uint8_t)type, n_pwmOutput["number"].as<uint8_t>(),
n_pwmOutput["description"].as<std::string>()));
}
}

Expand Down Expand Up @@ -379,3 +390,136 @@ void PPUC::StartUpdates() {
void PPUC::StopUpdates() {
m_pRS485Comm->QueueEvent(new Event(EVENT_RUN, 1, 0));
}

std::vector<PPUCCoil> PPUC::GetCoils() {
std::sort(
m_coils.begin(), m_coils.end(),
[](const PPUCCoil& a, const PPUCCoil& b) { return a.number < b.number; });

return m_coils;
}

std::vector<PPUCLamp> PPUC::GetLamps() {
std::sort(
m_lamps.begin(), m_lamps.end(),
[](const PPUCLamp& a, const PPUCLamp& b) { return a.number < b.number; });

return m_lamps;
}

std::vector<PPUCSwitch> PPUC::GetSwitches() {
std::sort(m_switches.begin(), m_switches.end(),
[](const PPUCSwitch& a, const PPUCSwitch& b) {
return a.number < b.number;
});

return m_switches;
}

void PPUC::CoilTest() {
printf("Coil Test\n");
printf("=========\n");

for (const auto& coil : GetCoils()) {
if (coil.type == PWM_TYPE_SOLENOID) {
printf("\nNumber: %d\nDescription: %s\n", coil.number,
coil.description.c_str());
SetSolenoidState(coil.number, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
SetSolenoidState(coil.number, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
}

void PPUC::LampTest() {
printf("Lamp Test\n");
printf("=========\n");

for (const auto& lamp : GetLamps()) {
if (lamp.type == LED_TYPE_LAMP) {
printf("\nNumber: %d\nDescription: %s\n", lamp.number,
lamp.description.c_str());
SetLampState(lamp.number, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
SetLampState(lamp.number, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}

for (const auto& coil : GetCoils()) {
if (coil.type == PWM_TYPE_LAMP) {
printf("\nNumber: %d\nDescription: %s\n", coil.number,
coil.description.c_str());
SetSolenoidState(coil.number, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
SetSolenoidState(coil.number, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
}

printf("\nFlasher Test\n");
printf("=========\n");

for (const auto& lamp : GetLamps()) {
if (lamp.type == LED_TYPE_FLASHER) {
printf("\nNumber: %d\nDescription: %s\n", lamp.number,
lamp.description.c_str());
SetLampState(lamp.number, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
SetLampState(lamp.number, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}

for (const auto& coil : GetCoils()) {
if (coil.type == PWM_TYPE_FLASHER) {
printf("\nNumber: %d\nDescription: %s\n", coil.number,
coil.description.c_str());
for (uint8_t i = 0; i < 3; i++) {
SetSolenoidState(coil.number, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
SetSolenoidState(coil.number, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
}
}

printf("\nGI Test\n");
printf("=========\n");

for (const auto& lamp : GetLamps()) {
if (lamp.type == LED_TYPE_GI) {
printf("\nNumber: %d\nDescription: %s\n", lamp.number,
lamp.description.c_str());
SetLampState(lamp.number, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
SetLampState(lamp.number, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
}

void PPUC::SwitchTest() {
printf("Switch Test\n");
printf("=========\n");

PPUCSwitchState* switchState;
while (true) {
if ((switchState = GetNextSwitchState()) != nullptr) {
auto it = std::find_if(m_switches.begin(), m_switches.end(),
[switchState](const PPUCSwitch& vswitch) {
return vswitch.number == switchState->number;
});

if (it != m_switches.end()) {
printf("Switch updated: #%d, %d\nDescription: %s", switchState->number,
switchState->state, it->description.c_str());
} else {
printf("Switch updated: #%d, %d\n", switchState->number,
switchState->state);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}
37 changes: 37 additions & 0 deletions src/PPUC.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ struct PPUCSwitchState {
}
};

struct PPUCSwitch {
u_int8_t number;
std::string description;

PPUCSwitch(u_int8_t n, const std::string& d)
: number(n), description(d) {}
};

struct PPUCCoil {
u_int8_t type;
u_int8_t number;
std::string description;

PPUCCoil(u_int8_t t, u_int8_t n,const std::string& d)
: type(t), number(n), description(d) {}
};

struct PPUCLamp {
u_int8_t type;
u_int8_t number;
std::string description;

PPUCLamp(u_int8_t t, u_int8_t n, const std::string& d)
: type(t), number(n), description(d) {}
};

class RS485Comm;

class PPUCAPI PPUC {
Expand All @@ -63,10 +89,21 @@ class PPUCAPI PPUC {
void SetLampState(int number, int state);
PPUCSwitchState* GetNextSwitchState();

void CoilTest();
void LampTest();
void SwitchTest();

std::vector<PPUCCoil> GetCoils();
std::vector<PPUCLamp> GetLamps();
std::vector<PPUCSwitch> GetSwitches();

private:
YAML::Node m_ppucConfig;
RS485Comm* m_pRS485Comm;
uint8_t ResolveLedType(std::string type);
std::vector<PPUCCoil> m_coils;
std::vector<PPUCLamp> m_lamps;
std::vector<PPUCSwitch> m_switches;

bool m_debug = false;
char* m_rom;
Expand Down
2 changes: 1 addition & 1 deletion src/RS485Comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Event* RS485Comm::receiveEvent() {
std::chrono::steady_clock::now() - start))
.count() < 8000) {
// printf("Available %d\n", m_serialPort.Available());
if (sp_input_waiting(m_pSerialPort) >= 6) {
if ((int) sp_input_waiting(m_pSerialPort) >= 6) {
uint8_t startByte;
sp_blocking_read(m_pSerialPort, &startByte, 1,
RS485_COMM_SERIAL_READ_TIMEOUT);
Expand Down

0 comments on commit 3cc1bc5

Please sign in to comment.