11#include "lora_information.h"
2+ #include <math.h>
23#include <string.h>
34#include "bsp/device.h"
45#include "bsp/input.h"
@@ -36,7 +37,8 @@ extern lora_handle_t* lora_get_handle(void);
3637#define TEXT_SIZE 9
3738#endif
3839
39- static void render (bool partial , bool icons , size_t num_packets , lora_protocol_lora_packet_t * packets ) {
40+ static void render (bool partial , bool icons , size_t num_packets , lora_protocol_lora_packet_t * packets ,
41+ float frequency_error , float frequency_error_avg ) {
4042 pax_buf_t * buffer = display_get_buffer ();
4143 gui_theme_t * theme = get_theme ();
4244
@@ -136,6 +138,10 @@ static void render(bool partial, bool icons, size_t num_packets, lora_protocol_l
136138 config .low_data_rate_optimization ? "Yes" : "No" );
137139 pax_draw_text (buffer , theme -> palette .color_foreground , TEXT_FONT , TEXT_SIZE , position .x0 ,
138140 position .y0 + (TEXT_SIZE + 2 ) * (line ++ ), text_buffer );
141+ snprintf (text_buffer , sizeof (text_buffer ), "Frequency error : %0.2f Hz (avg. %0.2f Hz)" , frequency_error ,
142+ frequency_error_avg );
143+ pax_draw_text (buffer , theme -> palette .color_foreground , TEXT_FONT , TEXT_SIZE , position .x0 ,
144+ position .y0 + (TEXT_SIZE + 2 ) * (line ++ ), text_buffer );
139145
140146 if (status .errors & SX126X_ERROR_RC64K_CALIB_ERR )
141147 pax_draw_text (buffer , 0xFFFF0000 , TEXT_FONT , TEXT_SIZE , position .x0 ,
@@ -206,7 +212,11 @@ void menu_lora_information(void) {
206212
207213 lora_protocol_lora_packet_t lora_packets [10 ] = {0 };
208214
209- render (false, true, (sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t )) - 1 , lora_packets );
215+ render (false, true, (sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t )) - 1 , lora_packets , 0 , 0 );
216+
217+ float frequency_error = 0 ;
218+ double frequency_error_accumulator = 0 ;
219+ uint32_t frequency_error_count = 0 ;
210220
211221 while (1 ) {
212222 bool received = false;
@@ -216,8 +226,12 @@ void menu_lora_information(void) {
216226 for (size_t i = 1 ; i < sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t ); i ++ ) {
217227 memcpy (& lora_packets [i - 1 ], & lora_packets [i ], sizeof (lora_protocol_lora_packet_t ));
218228 }
219- printf ("Received LoRa packet: length: %d, data: " ,
220- lora_packets [sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t ) - 1 ].length );
229+ lora_get_frequency_error (lora_get_handle (), & frequency_error );
230+ frequency_error_accumulator += frequency_error ;
231+ frequency_error_count ++ ;
232+ printf ("Received LoRa packet: length: %d, frequency error: %0.2f, data: " ,
233+ lora_packets [sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t ) - 1 ].length ,
234+ frequency_error );
221235 for (size_t j = 0 ;
222236 j < lora_packets [sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t ) - 1 ].length &&
223237 j < sizeof (lora_packets [sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t ) - 1 ].data );
@@ -238,6 +252,10 @@ void menu_lora_information(void) {
238252 case BSP_INPUT_NAVIGATION_KEY_F1 :
239253 case BSP_INPUT_NAVIGATION_KEY_GAMEPAD_B :
240254 return ;
255+ case BSP_INPUT_NAVIGATION_KEY_F5 :
256+ frequency_error_accumulator = 0 ;
257+ frequency_error_count = 0 ;
258+ break ;
241259 case BSP_INPUT_NAVIGATION_KEY_F6 :
242260 test_tx ();
243261 break ;
@@ -252,6 +270,10 @@ void menu_lora_information(void) {
252270 }
253271 }
254272
255- render (true, true, (sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t )) - 1 , lora_packets );
273+ double frequency_error_avg =
274+ frequency_error_count > 0 ? (float )(frequency_error_accumulator / (double )(frequency_error_count )) : NAN ;
275+
276+ render (true, true, (sizeof (lora_packets ) / sizeof (lora_protocol_lora_packet_t )) - 1 , lora_packets ,
277+ frequency_error , frequency_error_avg );
256278 }
257279}
0 commit comments