138 lines
3.9 KiB
C
138 lines
3.9 KiB
C
#include "pico/cyw43_arch.h"
|
|
#include "Teleplot.h"
|
|
|
|
// Si le fichier n'existe pas, créez-le à partir du modèle "wifi_settings.h.default"
|
|
#include "wifi_settings.h"
|
|
|
|
#define BEACON_MSG_LEN_MAX 500
|
|
|
|
char teleplot_tampon[BEACON_MSG_LEN_MAX]="";
|
|
|
|
struct udp_pcb* pcb;
|
|
ip_addr_t addr;
|
|
|
|
long teleplote_temps_ms;
|
|
bool teleplot_temps_fige;
|
|
|
|
int Teleplot_init(void){
|
|
if (cyw43_arch_init()) {
|
|
printf("failed to initialise\n");
|
|
return 1;
|
|
}
|
|
|
|
cyw43_arch_enable_sta_mode();
|
|
|
|
printf("Connecting to Wi-Fi...\n");
|
|
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);
|
|
teleplot_temps_fige = false;
|
|
return 0;
|
|
}
|
|
|
|
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();
|
|
#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);
|
|
#endif
|
|
}
|
|
|
|
void Teleplot_send_string(){
|
|
#ifdef WIFI_ENABLE
|
|
teleplot_udp_send_string(teleplot_tampon);
|
|
#else
|
|
printf(teleplot_tampon);
|
|
#endif
|
|
}
|
|
|
|
/// @brief Renvoi le temps en milliseconde, le temps réel ou le temps figé
|
|
/// @param
|
|
/// @return temps en millisecondes
|
|
long Teleplot_get_temps(void){
|
|
if(teleplot_temps_fige){
|
|
return teleplote_temps_ms;
|
|
}
|
|
return (long) (time_us_64()/1000);
|
|
}
|
|
|
|
/// @brief Permet de "bloquer le temps" pour recevoir toutes les données datées à la même milliseconde
|
|
/// Simplifie beaucoup le traitement des données en CSV lors d'un import dans un tableur.
|
|
void Teleplot_fige_temps(void){
|
|
teleplot_temps_fige = false;
|
|
teleplote_temps_ms = Teleplot_get_temps();
|
|
teleplot_temps_fige = true;
|
|
}
|
|
void Teleplot_relache_temps(void){
|
|
teleplot_temps_fige = false;
|
|
}
|
|
|
|
|
|
void Teleplot_envoie_tampon(void){
|
|
Teleplot_send_string(teleplot_tampon);
|
|
teleplot_tampon[0]='\0'; // On "vide" le tampon
|
|
}
|
|
|
|
|
|
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_envoie_tampon();
|
|
}
|
|
// On ajoute le message au tampon
|
|
strcat(teleplot_tampon, message);
|
|
|
|
}
|
|
|
|
void Teleplot_add_variable_float_2decimal(char * nom_variable, float valeur){
|
|
char tampon[100];
|
|
#ifdef WIFI_ENABLE
|
|
sprintf(tampon, "%s:%lu:%.2f\n", nom_variable, Teleplot_get_temps(), valeur);
|
|
#else
|
|
sprintf(tampon, ">%s:%lu:%.2f\n", nom_variable, Teleplot_get_temps(), valeur);
|
|
#endif
|
|
Teleplot_ajout_ou_envoie_tampon(tampon);
|
|
}
|
|
|
|
void Teleplot_add_variable_int(char * nom_variable, int valeur){
|
|
char tampon[100];
|
|
#ifdef WIFI_ENABLE
|
|
sprintf(tampon, "%s:%lu:%d\n", nom_variable, Teleplot_get_temps(), valeur);
|
|
#else
|
|
sprintf(tampon, ">%s:%lu:%d\n", nom_variable, Teleplot_get_temps(), valeur);
|
|
#endif
|
|
Teleplot_ajout_ou_envoie_tampon(tampon);
|
|
} |