nbus_impl.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "nbus_impl.h"
  2. nBus_functionCode_t* nbus_unicastToSensor(nBus_TypeDef *nbus, nBus_functionCode_t *code){
  3. return code;
  4. }
  5. nBus_functionCode_t* nbus_unicastToModule(nBus_TypeDef *nbus, nBus_functionCode_t *code){
  6. switch(nbus->function_code.function){
  7. case CMD_VERSION:
  8. {
  9. nbus->tx_buffer[4] = VERSION_MAJOR;
  10. nbus->tx_buffer[5] = '.';
  11. nbus->tx_buffer[6] = VERSION_MINOR;
  12. nbus->tx_length += 3;
  13. }
  14. break;
  15. case CMD_ECHO:
  16. {
  17. for(uint8_t i=3 ; i<nbus->rx_length-1 ; i++){
  18. nbus->tx_buffer[i+1] = nbus->rx_buffer[i];
  19. }
  20. nbus->tx_length += (nbus->rx_length-4);
  21. }
  22. break;
  23. default:
  24. {
  25. code->error = 1;
  26. nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
  27. nbus->tx_length += 1;
  28. }
  29. }
  30. return code;
  31. }
  32. void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
  33. if(request_type == BROADCAST_SPECIFIC_SENSORS) {
  34. }
  35. if(request_type == BROADCAST_GLOBAL) {
  36. switch(nbus->function_code.function) {
  37. case CMD_SYNC:
  38. {
  39. if(nbus->rx_length < 11){
  40. return;
  41. }
  42. RTC_TimeTypeDef sTime = {0};
  43. RTC_DateTypeDef sDate = {0};
  44. sTime.Hours = nbus->rx_buffer[7];
  45. sTime.Minutes = nbus->rx_buffer[8];
  46. sTime.Seconds = nbus->rx_buffer[9];
  47. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  48. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  49. HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
  50. sDate.WeekDay = nbus->rx_buffer[6];
  51. sDate.Month = nbus->rx_buffer[4];
  52. sDate.Date = nbus->rx_buffer[5];
  53. sDate.Year = nbus->rx_buffer[3];
  54. HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
  55. }
  56. break;
  57. case CMD_START:
  58. {
  59. nbus->measure_active = MEASURE_RUNNING;
  60. }
  61. break;
  62. case CMD_STOP:
  63. {
  64. nbus->measure_active = MEASURE_STOPPED;
  65. }
  66. break;
  67. default:
  68. ; // nothing
  69. }
  70. }
  71. nbus->send_response = NO_RESPONSE;
  72. }