NbusSlave.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * @file AppBridge.cpp
  3. * @brief Implementation of nBus Slave.
  4. * @date Nov 27, 2025
  5. * @author Juraj Dudak, Matus Necas
  6. */
  7. #include <NbusSlave.h>
  8. void NbusSlave::setCommunicator(NbusCommunicator *nbus_comm)
  9. {
  10. _communicator = nbus_comm;
  11. }
  12. void NbusSlave::setAddress(uint8_t addr)
  13. {
  14. _pdu.ma = addr;
  15. }
  16. uint8_t NbusSlave::getModuleAddress()
  17. {
  18. return _pdu.ma;
  19. }
  20. bool NbusSlave::isActive()
  21. {
  22. return _communicator != nullptr && _pdu.ma != 0;
  23. }
  24. DataFrame *NbusSlave::cmdProcessAnyRequest(DataFrame *slave_frame)
  25. {
  26. return _communicator->sendAndReceiveSlave(slave_frame);
  27. }
  28. DataFrame *NbusSlave::cmdGetData()
  29. {
  30. return _communicator->sendAndReceiveSlave(_makePacket(FC_DATA));
  31. }
  32. uint8_t NbusSlave::nbusSlaveCmd_getSensorCnt(bool check_hw)
  33. {
  34. if (_sensor_count == 0 || check_hw == true)
  35. {
  36. DataFrame *df = _communicator->sendAndReceiveSlave(_makePacket(FC_SENSOR_CNT));
  37. _sensor_count = df->GetFrame()[3];
  38. }
  39. return _sensor_count;
  40. }
  41. DataFrame* NbusSlave::_makePacket(Nbus_FC_e fc)
  42. {
  43. _cache_frame.Init();
  44. _cache_frame.AddUint8(_pdu.ma);
  45. _cache_frame.AddUint8(NBUS_SLAVE_ADDRESS_MODULE);
  46. _cache_frame.AddUint8(fc);
  47. _cache_frame.Commit();
  48. return &_cache_frame;
  49. }