Début des fonctions Teleplot - on va commencer la mise en tampon avant envoi pour gagner du temps d'execution

This commit is contained in:
Samuel 2024-07-18 20:44:36 +02:00
parent 6e912dbd4a
commit 8f41cd4cf0
3 changed files with 103 additions and 33 deletions

View File

@ -11,7 +11,7 @@ pico_enable_stdio_usb(test 1)
pico_enable_stdio_uart(test 1)
pico_add_extra_outputs(test)
target_include_directories(test PRIVATE ${CMAKE_CURRENT_LIST_DIR} )
target_link_libraries(test pico_cyw43_arch_lwip_threadsafe_background pico_stdlib)
target_link_libraries(test pico_cyw43_arch_lwip_poll pico_stdlib)
target_compile_definitions(test PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"

View File

@ -18,45 +18,112 @@
#include "wifi_settings.h"
#define UDP_PORT 47269
#define BEACON_MSG_LEN_MAX 127
#define BEACON_MSG_LEN_MAX 500
#define BEACON_TARGET "192.168.1.58"
#define BEACON_INTERVAL_MS 25
void run_udp_beacon() {
struct udp_pcb* pcb = udp_new();
struct udp_pcb* pcb;
ip_addr_t addr;
ip_addr_t addr;
ipaddr_aton(BEACON_TARGET, &addr);
int teleplot_counter = 0;
int counter = 0;
while (true) {
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, BEACON_MSG_LEN_MAX+1, PBUF_RAM);
char *req = (char *)p->payload;
memset(req, 0, BEACON_MSG_LEN_MAX+1);
snprintf(req, BEACON_MSG_LEN_MAX, "t:%d\nsin:%.2f\n", counter, sinf(counter/10.));
err_t er = udp_sendto(pcb, p, &addr, UDP_PORT);
pbuf_free(p);
if (er != ERR_OK) {
printf("Failed to send UDP packet! error=%d", er);
} else {
printf("Sent packet %d\n", counter);
counter++;
}
char teleplot_tampon[BEACON_MSG_LEN_MAX]="";
// Note in practice for this simple UDP transmitter,
// the end result for both background and poll is the same
void teleplot_udp_send_string(char * message);
void Teleplot_ajout_ou_envoie_tampon(char * message){
// Si le tampon ne peut pas accueillir le prochain message
// On envoie et on vide le tampon
if(strlen(message) + strlen(teleplot_tampon) > BEACON_MSG_LEN_MAX){
teleplot_udp_send_string(teleplot_tampon);
teleplot_tampon[0]='\0'; // On "vide" le tampon
teleplot_counter++;
}
// On ajoute le message au tampon
strcat(teleplot_tampon, message);
}
void Teleplot_add_variable_float_2decimal(char * nom_variable, float valeur){
char tampon[100];
sprintf(tampon, "%s:%lu:%.2f\n", nom_variable, (long)time_us_64()/1000, valeur);
teleplot_udp_send_string(tampon);
}
void Teleplot_add_variable_int(char * nom_variable, int valeur){
char tampon[100];
sprintf(tampon, "%s:%lu:%d\n", nom_variable, (long)time_us_64()/1000, valeur);
teleplot_udp_send_string(tampon);
}
void teleplot_udp_send_string(char * message){
static int counter = 0;
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, BEACON_MSG_LEN_MAX+1, PBUF_RAM);
char *req = (char *)p->payload;
memset(req, 0, BEACON_MSG_LEN_MAX+1);
snprintf(req, BEACON_MSG_LEN_MAX, "%s", message);
err_t er = udp_sendto(pcb, p, &addr, UDP_PORT);
pbuf_free(p);
if (er != ERR_OK) {
printf("Failed to send UDP packet! error=%d", er);
} else {
printf("Sent packet %d\n", counter);
counter++;
}
// Note in practice for this simple UDP transmitter,
// the end result for both background and poll is the same
#if PICO_CYW43_ARCH_POLL
// if you are using pico_cyw43_arch_poll, then you must poll periodically from your
// main loop (not from a timer) to check for Wi-Fi driver or lwIP work that needs to be done.
cyw43_arch_poll();
sleep_ms(BEACON_INTERVAL_MS);
// if you are using pico_cyw43_arch_poll, then you must poll periodically from your
// main loop (not from a timer) to check for Wi-Fi driver or lwIP work that needs to be done.
cyw43_arch_poll();
#else
// if you are not using pico_cyw43_arch_poll, then WiFI driver and lwIP work
// is done via interrupt in the background. This sleep is just an example of some (blocking)
// work you might be doing.
sleep_ms(BEACON_INTERVAL_MS);
// if you are not using pico_cyw43_arch_poll, then WiFI driver and lwIP work
// is done via interrupt in the background. This sleep is just an example of some (blocking)
// work you might be doing.
// sleep_ms(BEACON_INTERVAL_MS);
#endif
}
void run_udp_beacon() {
static int counter = 0;
uint32_t old_time = time_us_32();
uint32_t time, driver_time;
uint32_t delta_time = 0, delta_driver = 0, teleplot_time=0, delta_teleplot=0;
int teleplot_counter_old = teleplot_counter;
while (true) {
char tampon[100];
snprintf(tampon, BEACON_MSG_LEN_MAX, "temps_total:%d\ntemps_driver:%d\nsin_b:%.2f\n",
delta_time, delta_driver, sinf(counter/10.));
time = time_us_32();
teleplot_udp_send_string(tampon);
delta_time = time_us_32() - time;
teleplot_time = time_us_32();
Teleplot_add_variable_float_2decimal("cos", cosf(counter/10.));
Teleplot_add_variable_float_2decimal("sin", sinf(counter/10.));
if(teleplot_counter_old != teleplot_counter){
Teleplot_add_variable_int("m_time", delta_teleplot / teleplot_counter);
delta_teleplot = 0;
teleplot_counter_old = teleplot_counter;
}
delta_teleplot += time_us_32() - teleplot_time;
counter++;
sleep_ms(BEACON_INTERVAL_MS);
}
}
@ -69,12 +136,15 @@ int init_wifi(void){
cyw43_arch_enable_sta_mode();
printf("Connecting to Wi-Fi...\n");
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
if (cyw43_arch_wifi_connect_timeout_ms(MY_WIFI_SSID, MY_WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
printf("failed to connect.\n");
return 1;
} else {
printf("Connected.\n");
}
pcb = udp_new();
ipaddr_aton(BEACON_TARGET, &addr);
return 0;
}

View File

@ -1,4 +1,4 @@
// Copier ce fichier en "wifi_settings.h" et renseignez vos identifiants WiFi.
#define WIFI_SSID "My_SSID"
#define WIFI_PASSWORD "My_WiFi_Password"
#define MY_WIFI_SSID "My_SSID"
#define MY_WIFI_PASSWORD "My_WiFi_Password"