diff --git a/.vscode/settings.json b/.vscode/settings.json index be8f621..34bd0b5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -16,6 +16,11 @@ "servomoteur.h": "c", "moteurs.h": "c", "messagerie_applicative.h": "c", - "config_robot.h": "c" + "config_robot.h": "c", + "wifi.h": "c", + "string.h": "c", + "lwip_nosys.h": "c", + "async_context.h": "c", + "wifi_settings.h": "c" } } \ No newline at end of file diff --git a/Wifi.c b/Wifi.c index afbcdb3..f4c9fc4 100644 --- a/Wifi.c +++ b/Wifi.c @@ -1,4 +1,13 @@ +#include "Wifi.h" +#include #include "pico/cyw43_arch.h" +#include "lwip/etharp.h" +#include "lwip/ethip6.h" +#include "lwip/dns.h" +#include "lwip/igmp.h" +#include "lwip/tcpip.h" +#include "netif/ethernet.h" +#include // Si le fichier n'existe pas, créez-le à partir du modèle "wifi_settings.h.default" #include "wifi_settings.h" @@ -7,15 +16,40 @@ struct udp_pcb* pcb; struct udp_pcb * pcb_r; -ip_addr_t addr; +ip_addr_t my_ip_addr, host_ip_addr; + +char udp_buffer_r[UDP_BUFFER_LENGTH]; +unsigned int udp_buffer_r_index = 0; + +void udp_receive_put_in_buffer(char c){ + if(udp_buffer_r_index + 1< UDP_BUFFER_LENGTH){ + udp_buffer_r[udp_buffer_r_index] = c; + udp_buffer_r_index++; + } +} + +/// @brief Copie les données reçues par UDP dans le tampon et renvoie le nombre de données copiées +/// @param tampon qui va contenir les données +/// @return Nombre de données dans le tampon +int udp_receive_get_from_buffer(char* tampon){ + unsigned int taille_tampon; + if( udp_buffer_r_index > 0){ + taille_tampon = udp_buffer_r_index; + memcpy(tampon, udp_buffer_r, taille_tampon); + udp_buffer_r_index = 0; + return taille_tampon; + } + return 0; +} void udp_receive_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){ printf("UDP callback !\n"); - printf("IP: %d.%d.%d.%d, port %d\n", (addr->addr >>24) & 0xFF, (addr->addr >>16) & 0xFF, (addr->addr >>8) & 0xFF, addr->addr & 0xFF, port); + printf("IP: %d.%d.%d.%d, port %d\n", (addr->addr) & 0xFF, (addr->addr >>8) & 0xFF, (addr->addr >>16) & 0xFF, (addr->addr >>24)& 0xFF, port); printf("Taille:%d\n", p->len); printf("Contenu: "); for(int i=0; i < p->len; i++){ printf("%c", ((char*)p->payload)[i]); + udp_receive_put_in_buffer(((char*)p->payload)[i]); } printf("\n"); } @@ -48,19 +82,57 @@ int Wifi_init(void){ 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)) { + while (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"); } + printf("Connected.\n"); pcb = udp_new(); - ipaddr_aton(BEACON_TARGET, &addr); + ipaddr_aton(BEACON_TARGET, &my_ip_addr); + + UDP_client_init(); return 0; } +void Wifi_udp_send_data(char * message, unsigned int size){ + 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); + + if(size = BEACON_MSG_LEN_MAX\n"); + } + + err_t er = udp_sendto(pcb, p, &my_ip_addr, 1234); + + 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 Wifi_udp_send_string(char * message){ static int counter = 0; @@ -70,13 +142,14 @@ void Wifi_udp_send_string(char * message){ snprintf(req, BEACON_MSG_LEN_MAX, "%s", message); - err_t er = udp_sendto(pcb, p, &addr, UDP_PORT); + err_t er = udp_sendto(pcb, p, &my_ip_addr, 1234); + //er |= udp_sendto(pcb, p, &my_ip_addr, 1234); pbuf_free(p); if (er != ERR_OK) { printf("Failed to send UDP packet! error=%d", er); } else { - //printf("Sent packet %d\n", counter); + printf("Sent packet %d\n", counter); counter++; } diff --git a/Wifi.h b/Wifi.h index bd6328d..71d3947 100644 --- a/Wifi.h +++ b/Wifi.h @@ -1,2 +1,6 @@ +#define UDP_BUFFER_LENGTH 1024 + int Wifi_init(void); -void Wifi_udp_send_string(char * message); \ No newline at end of file +void Wifi_udp_send_string(char * message); +void Wifi_udp_send_data(char * message, unsigned int size); +int udp_receive_get_from_buffer(char * ); diff --git a/communication.c b/communication.c index ab79b67..3e0cf7d 100644 --- a/communication.c +++ b/communication.c @@ -2,6 +2,7 @@ #include #include "communication.h" #include "messagerie.h" +#include "Wifi.h" #include "tusb.h" #define TAMPON_TAILLE 1020 @@ -43,6 +44,7 @@ void augmente_index(unsigned int *index, unsigned int offset){ /// et analyse si un message valide a été reçu. void communication_reception_message(){ int input_char; + char tampon_reception_udp[UDP_BUFFER_LENGTH]; int chaîne_octets_reçus[TAMPON_TAILLE]; unsigned int index_chaine_recue; unsigned int index_tampon; @@ -66,8 +68,13 @@ void communication_reception_message(){ } }*/ - input_char = stdio_getchar_timeout_us(0); - while(input_char != PICO_ERROR_TIMEOUT){ + // input_char = stdio_getchar_timeout_us(0); + int nb_char = udp_receive_get_from_buffer(tampon_reception_udp); + int current_char = nb_char; + while(current_char > 0){ + input_char = tampon_reception_udp[nb_char - current_char]; + current_char--; + printf("%c %d\n", input_char, input_char); com_reception_buffer.index_tampon_ecriture++; if(com_reception_buffer.index_tampon_ecriture >= TAMPON_TAILLE){ com_reception_buffer.index_tampon_ecriture = 0; @@ -75,7 +82,7 @@ void communication_reception_message(){ com_reception_buffer.tampon[com_reception_buffer.index_tampon_ecriture] = input_char; // Caractère suivant ? - input_char = stdio_getchar_timeout_us(0); + //input_char = stdio_getchar_timeout_us(0); } // Copie du tampon tournant dans une chaine diff --git a/config_robot.h b/config_robot.h index 9c7a436..7ecba7c 100644 --- a/config_robot.h +++ b/config_robot.h @@ -1,7 +1,7 @@ // Changer le define en fonction qu'on soit sur les PAMIs ou sur le robot principal -#define ROBOT_PROPULSION_2026 -//#define ROBOT_TYPE_PAMI +//#define ROBOT_PROPULSION_2026 +#define ROBOT_TYPE_PAMI #ifndef ROBOT_PROPULSION_2026 #ifndef ROBOT_TYPE_PAMI diff --git a/main.c b/main.c index 9b31d40..30bd10c 100644 --- a/main.c +++ b/main.c @@ -82,7 +82,15 @@ void main(void) sleep_ms(50); Wifi_udp_send_string("Demarrage:1\n"); Wifi_udp_send_string("test:1\n"); - + sleep_ms(50); + Wifi_udp_send_string("Demarrage:1\n"); + Wifi_udp_send_string("test:1\n"); + sleep_ms(50); + Wifi_udp_send_string("Demarrage:1\n"); + Wifi_udp_send_string("test:1\n"); + sleep_ms(50); + Wifi_udp_send_data("\x01\x02\x03\x04\x05", 5); + Wifi_udp_send_string("test:1\n"); //i2c_maitre_init(); //Servomoteur_Init(); @@ -248,21 +256,30 @@ void main(void) } break; case 'd': - //printf("Demande donnees\n"); + char message_envoi_udp[500]; + printf("Demande donnees\n"); // Demande de données memcpy(&message_applicatif, message.donnees, message.taille_donnees); // printf("id_carte:%c %d\n", message_applicatif.id_carte, message_applicatif.id_carte); // printf("adresse registre:%d\n", message_applicatif.adresse_registre, message_applicatif.id_carte); // printf("taille:%d\n", message_applicatif.taille_donnees, message_applicatif.taille_donnees); + message_envoi_udp[0] = 0xFF; + message_envoi_udp[1] = 0xFF; + message_envoi_udp[2] = 'P'; + message_envoi_udp[3] = message_applicatif.taille_donnees+1; stdio_putchar_raw(0xFF); stdio_putchar_raw(0xFF); stdio_putchar_raw('P'); stdio_putchar_raw(message_applicatif.taille_donnees+1); for(int i=0; i