From 3cc1bc5a51b1e29748a2e068351e775f3760412d Mon Sep 17 00:00:00 2001 From: Markus Kalkbrenner Date: Thu, 13 Feb 2025 16:39:23 +0100 Subject: [PATCH] added Lamp, Coil and Switch Test --- src/PPUC.cpp | 144 ++++++++++++++++++++++++++++++++++++++++++++++ src/PPUC.h | 37 ++++++++++++ src/RS485Comm.cpp | 2 +- 3 files changed, 182 insertions(+), 1 deletion(-) diff --git a/src/PPUC.cpp b/src/PPUC.cpp index d556a59..2960cc0 100644 --- a/src/PPUC.cpp +++ b/src/PPUC.cpp @@ -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(), + n_item["description"].as())); } } @@ -177,6 +180,10 @@ bool PPUC::Connect() { n_switch["board"].as(), (uint8_t)CONFIG_TOPIC_SWITCHES, index++, (uint8_t)CONFIG_TOPIC_NUMBER, n_switch["number"].as())); + + m_switches.push_back( + PPUCSwitch(n_switch["number"].as(), + n_switch["description"].as())); } } @@ -285,6 +292,10 @@ bool PPUC::Connect() { m_pRS485Comm->SendConfigEvent(new ConfigEvent( n_pwmOutput["board"].as(), (uint8_t)CONFIG_TOPIC_PWM, index++, (uint8_t)CONFIG_TOPIC_TYPE, type)); + + m_coils.push_back( + PPUCCoil((uint8_t)type, n_pwmOutput["number"].as(), + n_pwmOutput["description"].as())); } } @@ -379,3 +390,136 @@ void PPUC::StartUpdates() { void PPUC::StopUpdates() { m_pRS485Comm->QueueEvent(new Event(EVENT_RUN, 1, 0)); } + +std::vector 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 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 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)); + } +} diff --git a/src/PPUC.h b/src/PPUC.h index 6699835..3dbeed8 100644 --- a/src/PPUC.h +++ b/src/PPUC.h @@ -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 { @@ -63,10 +89,21 @@ class PPUCAPI PPUC { void SetLampState(int number, int state); PPUCSwitchState* GetNextSwitchState(); + void CoilTest(); + void LampTest(); + void SwitchTest(); + + std::vector GetCoils(); + std::vector GetLamps(); + std::vector GetSwitches(); + private: YAML::Node m_ppucConfig; RS485Comm* m_pRS485Comm; uint8_t ResolveLedType(std::string type); + std::vector m_coils; + std::vector m_lamps; + std::vector m_switches; bool m_debug = false; char* m_rom; diff --git a/src/RS485Comm.cpp b/src/RS485Comm.cpp index c8a511b..efe0d4f 100644 --- a/src/RS485Comm.cpp +++ b/src/RS485Comm.cpp @@ -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);