/** * @file RailCom.h * @brief RailCom Feedback Controller * * Implements RailCom channel 1 and 2 for bidirectional communication * with the command station. Sends locomotive address and status information. */ #ifndef RAILCOM_H #define RAILCOM_H #include // RailCom timing (in microseconds) #define RAILCOM_CHANNEL1_START 26 #define RAILCOM_CHANNEL1_END 177 #define RAILCOM_CHANNEL2_START 193 #define RAILCOM_CHANNEL2_END 454 // RailCom 4bit to 8bit encoding table #define RAILCOM_4BIT_TO_8BIT_SIZE 16 class RailCom { public: RailCom(); /** * @brief Initialize RailCom * @param txPin GPIO for RailCom transmit (UART TX) * @param cutoutDetectPin GPIO to detect DCC cutout (optional, 255 = disabled) * @return true if successful */ bool begin(uint8_t txPin, uint8_t cutoutDetectPin = 255); /** * @brief Enable/disable RailCom * @param enable true to enable */ void setEnabled(bool enable); /** * @brief Check if RailCom is enabled */ bool isEnabled() const { return enabled; } /** * @brief Send RailCom data during cutout window * This should be called when DCC cutout is detected */ void sendRailComData(); /** * @brief Set locomotive address for RailCom reporting * @param address Locomotive address */ void setAddress(uint16_t address); /** * @brief Set decoder state information * @param speed Current speed * @param direction Current direction */ void setDecoderState(uint8_t speed, bool direction); /** * @brief Update RailCom (call regularly from loop) */ void update(); private: uint8_t txPin; uint8_t cutoutPin; bool enabled; uint16_t locoAddress; uint8_t currentSpeed; bool currentDirection; HardwareSerial* railcomSerial; // RailCom encoding uint8_t encode4to8(uint8_t data); void sendChannel1(); void sendChannel2(); // Timing unsigned long lastCutoutTime; bool inCutout; static const uint8_t railcom4to8[16]; }; #endif // RAILCOM_H