66 * Sends state, E-stop, and actuator position data while receiving RPM data.
77 */
88
9- #include " LoadComms.h"
9+ #include " LoadComms.hpp"
10+ #include < esp_log.h>
11+
12+ // Initialization of static members
13+ QueueHandle_t LoadComms::priorityDataQueue = nullptr ;
1014
1115/* *
1216 * @brief MAC address of the nacelle controller.
1317 */
1418const uint8_t NACELLE_MAC [] = {0x30 , 0xED , 0xA0 , 0xE0 , 0x6B , 0x78 };
1519
16- static constexpr char * TAG = " LoadboxComms" ;
17- constexpr uint8_t wiFiChannel = 6 ;
18-
1920/* *
2021 * @brief Pointer to instance for static callbacks.
2122 */
@@ -24,8 +25,9 @@ static LoadComms *s_instance = nullptr;
2425LoadComms::LoadComms ()
2526 : lastSendTime_(0 ),
2627 lastRxTime_(0 ),
27- linkAlive_(false ),
28- nacelleRPM_(0 .0f ) {
28+ linkAlive_(false ) // ,
29+ // nacelleRPM_(0.0f)
30+ {
2931 s_instance = this ;
3032}
3133
@@ -51,41 +53,59 @@ bool LoadComms::begin() {
5153 }
5254
5355 if (esp_now_init () != ESP_OK ) {
54- Serial.println (" ESP-NOW init failed" );
56+ ESP_LOGE (TAG , " ESP-NOW init failed" );
57+ return false ;
58+ }
59+
60+ if (esp_now_register_send_cb (onDataSent_) != ESP_OK ) {
61+ ESP_LOGE (TAG , " Failed to register tx cb" );
5562 return false ;
5663 }
5764
58- esp_now_register_send_cb (onDataSent_);
59- esp_now_register_recv_cb (onDataRecv_);
65+ if (esp_now_register_recv_cb (onDataRecv_) != ESP_OK ) {
66+ ESP_LOGE (TAG , " Failed to register rx cb" );
67+ return false ;
68+ }
6069
61- setupPeer_ ();
70+ if ( setupPeer_ () != ESP_OK ) {
71+ // Logging already handled
72+ return false ;
73+ }
6274
6375 Serial.println (" Loadbox ready" );
6476 return true ;
6577}
6678
67- void LoadComms::setupPeer_ () {
79+ esp_err_t LoadComms::setupPeer_ () {
6880 esp_now_peer_info_t peerInfo = {};
6981 memcpy (peerInfo.peer_addr , NACELLE_MAC , 6 );
7082 peerInfo.channel = 0 ;
7183 peerInfo.encrypt = false ;
7284
73- if (esp_now_add_peer (&peerInfo) != ESP_OK ) {
74- Serial.println (" Failed to add peer" );
75- return ;
85+ esp_err_t result = esp_now_add_peer (&peerInfo);
86+ if (result != ESP_OK ) {
87+ ESP_LOGE (TAG , " Failed to add peer: %d at %02x:%02x:%02x:%02x:%02x:%02x" , result,
88+ NACELLE_MAC [0 ], NACELLE_MAC [1 ], NACELLE_MAC [2 ],
89+ NACELLE_MAC [3 ], NACELLE_MAC [4 ], NACELLE_MAC [5 ]);
90+ } else {
91+ ESP_LOGI (TAG , " Peer added: %02X:%02X:%02X:%02X:%02X:%02X" ,
92+ NACELLE_MAC [0 ], NACELLE_MAC [1 ], NACELLE_MAC [2 ],
93+ NACELLE_MAC [3 ], NACELLE_MAC [4 ], NACELLE_MAC [5 ]);
7694 }
7795
78- Serial. println ( " Peer added " ) ;
96+ return result ;
7997}
8098
8199void LoadComms::onDataSent_ (const wifi_tx_info_t *tx_info, esp_now_send_status_t status) {
82- (void )tx_info;
83- Serial.print (" Send status: " );
84- Serial.println (status == ESP_NOW_SEND_SUCCESS ? " Success" : " Fail" );
100+ // (void)tx_info;
101+ // Serial.print("Send status: ");
102+ // Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
103+ // This is check elsewhere
85104}
86105
87106void LoadComms::onDataRecv_ (const esp_now_recv_info_t *recv_info, const uint8_t *data, int len) {
88107 if (s_instance == nullptr ) {
108+ ESP_LOGE (TAG , " Rx CB: Invalid instance" );
89109 return ;
90110 }
91111
@@ -95,47 +115,53 @@ void LoadComms::onDataRecv_(const esp_now_recv_info_t *recv_info, const uint8_t
95115 mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]);
96116
97117 if (len == sizeof (NacellePacket)) {
98- memcpy (&s_instance->incomingPacket_ , data, sizeof (NacellePacket));
118+ // memcpy(&s_instance->incomingPacket_, data, sizeof(NacellePacket));
99119
100120 s_instance->lastRxTime_ = millis ();
101121 s_instance->linkAlive_ = true ;
102122
103- s_instance->nacelleRPM_ = s_instance->incomingPacket_ .rpm ;
123+ // s_instance->nacelleRPM_ = s_instance->incomingPacket_.rpm;
104124
105- Serial.println (" Received NacellePacket:" );
106- printNacellePacket (s_instance->incomingPacket_ , Serial);
125+ // Serial.println("Received NacellePacket:");
126+ // printNacellePacket(s_instance->incomingPacket_, Serial);
127+
128+ (void )xQueueOverwrite (priorityDataQueue, data); // Allegedly cannot fail
107129 } else {
108- Serial.print (" Unexpected packet length: " );
109- Serial.println (len);
130+ ESP_LOGE (TAG , " Rx invalid len: %d" , len);
110131 }
111132}
112133
113- void LoadComms::sendLoadboxData (int8_t state, int8_t estop, int16_t actuatorPos) {
114- unsigned long now = millis ();
115-
116- if (now - lastSendTime_ >= LOAD_COMMS_SEND_PERIOD_MS ) {
117- lastSendTime_ = now;
118- makeLoadboxPacket (outgoingPacket_, state, estop, actuatorPos);
119- esp_now_send (NACELLE_MAC , (uint8_t *)&outgoingPacket_, sizeof (outgoingPacket_));
120- }
121-
122- if (now - lastRxTime_ > LOAD_COMMS_TIMEOUT_MS ) {
134+ bool LoadComms::sendLoadboxData (uint8_t estop) {
135+ // if (now - lastSendTime_ >= LOAD_COMMS_SEND_PERIOD_MS) {
136+ makeLoadboxPacket (outgoingPacket_, estop);
137+ esp_err_t result = esp_now_send (NACELLE_MAC , (uint8_t *)&outgoingPacket_, sizeof (outgoingPacket_));
138+ if (result == ESP_OK ) {
139+ lastSendTime_ = millis ();
140+ linkAlive_ = true ;
141+ } else {
123142 linkAlive_ = false ;
143+ ESP_LOGE (TAG , " Tx failed" );
124144 }
125145
126- if (!linkAlive_) {
127- Serial. println ( " WARNING: nacelle comms timeout " );
128- }
129- }
146+ // }
147+ // if (now - lastRxTime_ > LOAD_COMMS_TIMEOUT_MS) {
148+ // linkAlive_ = false;
149+ // }
130150
131- void LoadComms::process () {
132- // This method can be used for future expansion (e.g., periodic checks)
151+ // if (!linkAlive_) {
152+ // Serial.println("WARNING: nacelle comms timeout");
153+ // }
154+ return linkAlive_;
133155}
134156
157+ // void LoadComms::process() {
158+ // // This method can be used for future expansion (e.g., periodic checks)
159+ // }
160+
135161bool LoadComms::isLinkAlive () const {
136162 return linkAlive_;
137163}
138164
139- float LoadComms::getNacelleRPM () const {
140- return nacelleRPM_;
141- }
165+ // float LoadComms::getNacelleRPM() const {
166+ // return nacelleRPM_;
167+ // }
0 commit comments