121 lines
3.1 KiB
C
121 lines
3.1 KiB
C
// Necessary includes from the Pico SDK
|
|
#include <pico/stdio.h>
|
|
#include <stdio.h>
|
|
#include "pico/cyw43_arch.h"
|
|
|
|
// Custom libraries includes
|
|
#include "wifi_operator/wifi_operator.h"
|
|
#include "udp/udp_client.h"
|
|
#include "udp/udp_server.h"
|
|
|
|
// Multicore includes to limit latency on packet reception on core 0
|
|
#include "pico/multicore.h"
|
|
#include "hardware/irq.h"
|
|
#include "hardware/sync.h"
|
|
#include "pico/mutex.h"
|
|
|
|
#define UART_ID uart0
|
|
#define BAUD_RATE 115200
|
|
#define UART_TX_PIN 0 // GP0
|
|
#define UART_RX_PIN 1 // GP1
|
|
#define UDP_SERVER_PORT 4242
|
|
#define UDP_CLIENT_PORT 4243
|
|
|
|
// Ajouter ces defines en haut du fichier
|
|
#define TARGET_IP "192.168.128.2" // IP de votre PC
|
|
#define TARGET_PORT 10101 // Port sur lequel votre PC écoute
|
|
|
|
// Access Point configuration
|
|
#define AP_SSID "PicoW_AP"
|
|
#define AP_PASSWORD "password123"
|
|
#define AP_CHANNEL 13
|
|
|
|
// Déclarer le mutex comme variable globale
|
|
auto_init_mutex(wifi_mutex);
|
|
|
|
// En haut du fichier, après les includes existants
|
|
#define BUFFER_SIZE 1024
|
|
|
|
void custom_message_callback(uint8_t packet_number, uint8_t instruction, const ip_addr_t *addr, u16_t port)
|
|
{
|
|
printf("Core0: Received: packet=%d, instruction=%d, from %s:%d\n", packet_number, instruction, ipaddr_ntoa(addr), port);
|
|
|
|
//making the led blink twice in 0.1s
|
|
for (int i = 0; i < 2; i++)
|
|
{
|
|
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true);
|
|
sleep_ms(25);
|
|
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, false);
|
|
sleep_ms(25);
|
|
}
|
|
}
|
|
|
|
void core1_entry()
|
|
{
|
|
int diodeState = 0;
|
|
udp_server_init(UDP_SERVER_PORT); // Uniquement le serveur sur core 1
|
|
|
|
uint8_t buffer[2];
|
|
uint8_t *packet_number = &buffer[0];
|
|
uint8_t *instruction = &buffer[1];
|
|
|
|
printf("Core1: Starting UDP server on port %d\n", UDP_SERVER_PORT);
|
|
|
|
while(true)
|
|
{
|
|
mutex_enter_blocking(&wifi_mutex);
|
|
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, diodeState); // Update LED
|
|
udp_server_send(buffer, sizeof(buffer), TARGET_IP, TARGET_PORT); // Send message
|
|
mutex_exit(&wifi_mutex);
|
|
|
|
// Increment counters
|
|
*packet_number = (*packet_number + 1) % 256;
|
|
*instruction = (*instruction + 1) % 256;
|
|
|
|
diodeState = !diodeState;
|
|
|
|
sleep_ms(2000);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
stdio_init_all();
|
|
|
|
sleep_ms(2000);
|
|
|
|
puts("Pi Starting...");
|
|
|
|
if(cyw43_arch_init())
|
|
{
|
|
puts("WiFi init failed !");
|
|
return -1;
|
|
}
|
|
|
|
multicore_launch_core1(core1_entry);
|
|
|
|
puts("WiFi is configuring...");
|
|
|
|
mutex_enter_blocking(&wifi_mutex);
|
|
wifi_operator_init_ap_mode(AP_SSID, AP_PASSWORD, AP_CHANNEL);
|
|
mutex_exit(&wifi_mutex);
|
|
|
|
puts("WiFi configured !");
|
|
|
|
// Starts UDP client on core 0
|
|
udp_client_init(UDP_CLIENT_PORT, custom_message_callback);
|
|
// UDP client receive data on port 4243 automatically and execute the message callback on each receptions
|
|
puts("UDP client started on core 0 !");
|
|
|
|
while(true)
|
|
{
|
|
mutex_enter_blocking(&wifi_mutex);
|
|
cyw43_arch_poll();
|
|
mutex_exit(&wifi_mutex);
|
|
|
|
tight_loop_contents();
|
|
}
|
|
|
|
udp_client_exit(); // Hehehe, I know you could forgot this one
|
|
}
|