From d8708d1bd586c755e7dc6b24002ff79e0af28a8c Mon Sep 17 00:00:00 2001 From: Gamenight77 Date: Fri, 25 Apr 2025 09:17:22 +0200 Subject: [PATCH] Refactor ultrasonic FPGA module: replace sig_in with sig_ok for improved signal handling and update ESP32 command processing to support new client list command --- .../Ultrasonic/ultrasonic_fpga.v | 9 ++- .../esp32_code/Projet_code/ESP32/ESP32.ino | 25 +++++--- .../esp32_code/Projet_code/test_command.py | 60 +++++++++++++++++++ 3 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 Semaine_2/Projet_esp32/esp32_code/Projet_code/test_command.py diff --git a/Semaine_1/Capteur_recule_bidirectionel/Ultrasonic/ultrasonic_fpga.v b/Semaine_1/Capteur_recule_bidirectionel/Ultrasonic/ultrasonic_fpga.v index 88b3592..9b03f2b 100644 --- a/Semaine_1/Capteur_recule_bidirectionel/Ultrasonic/ultrasonic_fpga.v +++ b/Semaine_1/Capteur_recule_bidirectionel/Ultrasonic/ultrasonic_fpga.v @@ -13,7 +13,10 @@ module ultrasonic_fpga #( reg sig_dir; // 1: output, 0: input assign sig = sig_dir ? sig_out : 1'bz; // bz pour dire que le fpga laisse le fils libre et n'oblige pas de valeur - wire sig_in = sig; + + reg sig_int, sig_ok; + + always_ff(@posedge clk) {sig_ok, sig_int} = {sig_int, sig}; localparam IDLE = 3'd0, TRIG_HIGH = 3'd1, @@ -65,7 +68,7 @@ module ultrasonic_fpga #( end WAIT_ECHO: begin - if (sig_in) begin + if (sig_ok) begin echo_counter <= 0; state <= MEASURE_ECHO; end else if (echo_counter >= TIMEOUT_CYCLES) begin @@ -77,7 +80,7 @@ module ultrasonic_fpga #( end MEASURE_ECHO: begin - if (sig_in) begin + if (sig_ok) begin if (echo_counter < TIMEOUT_CYCLES) begin echo_counter <= echo_counter + 1; end else begin diff --git a/Semaine_2/Projet_esp32/esp32_code/Projet_code/ESP32/ESP32.ino b/Semaine_2/Projet_esp32/esp32_code/Projet_code/ESP32/ESP32.ino index 2ef7e29..bfdb35b 100644 --- a/Semaine_2/Projet_esp32/esp32_code/Projet_code/ESP32/ESP32.ino +++ b/Semaine_2/Projet_esp32/esp32_code/Projet_code/ESP32/ESP32.ino @@ -114,22 +114,29 @@ void processCommand(uint8_t* data, int length) { case 0x03: { // Request Connected Devices wifi_sta_list_t staList; if (esp_wifi_ap_get_sta_list(&staList) == ESP_OK) { + byte packet[3 + 1 + 6 * 10 + 2]; // max 10 clients + uint8_t index = 0; + packet[index++] = 0x02; + packet[index++] = 0x04; + packet[index++] = staList.num; // LEN + for (int i = 0; i < staList.num; i++) { - byte packet[12] = {0x02, 0x02, 0x01}; // 0x01 = connect - memcpy(packet + 3, staList.sta[i].mac, 6); - packet[9] = 0x1B; - packet[10] = 0x03; - Serial.write(packet, 11); + memcpy(&packet[index], staList.sta[i].mac, 6); + index += 6; } + + packet[index++] = 0x1B; + packet[index++] = 0x03; + + Serial.write(packet, index); } else { - byte packet[] = {0x02, 0x00, 0x02, 0x1B, 0x03}; // Erreur : args - Serial.write(packet, sizeof(packet)); + byte error[] = {0x02, 0x00, 0x02, 0x1B, 0x03}; // Erreur : args + Serial.write(error, sizeof(error)); } break; } - - case 0x04: { // Send Message + case 0x05: { // Send Message if (length < 12) { byte packet[] = {0x02, 0x00, 0x02, 0x1B, 0x03}; // args error Serial.write(packet, sizeof(packet)); diff --git a/Semaine_2/Projet_esp32/esp32_code/Projet_code/test_command.py b/Semaine_2/Projet_esp32/esp32_code/Projet_code/test_command.py new file mode 100644 index 0000000..5cb2760 --- /dev/null +++ b/Semaine_2/Projet_esp32/esp32_code/Projet_code/test_command.py @@ -0,0 +1,60 @@ +import serial +import time + +START_BYTE = 0x02 +END_BYTES = [0x1B, 0x03] + +def build_command(opcode, payload=b''): + frame = bytearray() + frame.append(START_BYTE) + frame.append(opcode) + frame.extend(payload) + frame.extend(END_BYTES) + return frame + +def send_command(ser, opcode, payload=b''): + frame = build_command(opcode, payload) + print(f"Envoi : {[hex(b) for b in frame]}") + ser.write(frame) + +def main(): + port = 'COM5' + baud = 115200 + + try: + with serial.Serial(port, baud, timeout=2) as ser: + while True: + print("\nCommandes disponibles :") + print("1. État du Wi-Fi") + print("2. Liste des clients connectés") + print("3. Envoyer un message") + print("4. Quitter") + + choix = input("Choix (1-4) : ") + + if choix == "1": + send_command(ser, 0x01) + elif choix == "2": + send_command(ser, 0x03) + elif choix == "3": + msg = input("Message à envoyer : ") + msg_bytes = msg.encode('utf-8') + send_command(ser, 0x05, msg_bytes) + elif choix == "4": + print("Fermeture.") + break + else: + print("Choix invalide.") + + time.sleep(0.5) + + print("Réponse reçue :") + while ser.in_waiting: + byte = ser.read(1) + print(f"Reçu : 0x{byte[0]:02X}") + + except serial.SerialException as e: + print(f"Erreur de port série : {e}") + +if __name__ == "__main__": + main()