Advertisement
Guest User

Untitled

a guest
May 30th, 2020
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <WiFiUdp.h>
  3. #define UDP_TX_PACKET_MAX_SIZE 1024
  4.  
  5. const char * networkName = "foo";
  6. const char * networkPswd = "bar";
  7.  
  8. #define INCLUDE_vTaskSuspend 1
  9.  
  10. WiFiUDP Udp;
  11. const int udpPort = 8888;
  12.  
  13. char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
  14. #define LED_PER_PACKET 340
  15. #define INCLUDE_vTaskSuspend 1
  16.  
  17. IPAddress remote_address;
  18. int remote_port = NULL;
  19. int led_number = NULL;
  20.  
  21. unsigned short int frame_number;
  22. unsigned short int packet_number;
  23.  
  24. portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
  25.  
  26. #include <dcll.h>
  27. Dcll *dcll;
  28.  
  29. TaskHandle_t Receive_Udp_Task;
  30.  
  31. void write_ack(){
  32.     if(remote_address && remote_port){
  33.         Serial.println("WRITE ACK");
  34.         Udp.beginPacket(remote_address, remote_port);
  35.         Udp.print("ACK");
  36.         Udp.endPacket();
  37.     }
  38. }
  39.  
  40. void write_frame(){
  41.     Udp.beginPacket(remote_address, remote_port);
  42.     Udp.print("FRAME");
  43.     Udp.endPacket();
  44. }
  45.  
  46. void handle_registration_packet(char* packetBuffer){
  47.   led_number = *((unsigned int*)(packetBuffer+1));
  48.   remote_address = Udp.remoteIP();
  49.   remote_port = Udp.remotePort();
  50. }
  51.  
  52. void handle_frame_packet(char* packetBuffer){
  53.   write_ack();
  54.   frame_number = (unsigned short int)*((char*)(packetBuffer+1));
  55.   packet_number = (unsigned short int)*((char*)(packetBuffer+2));
  56.  
  57.   int led_offset = 340 * packet_number;
  58.   int r_off = 3;
  59.   for(int i=0;i<340;i++){
  60.     if(led_offset+i < led_number){
  61.       dcll->leds[led_offset+i].setRGB(
  62.         packetBuffer[r_off],
  63.         packetBuffer[r_off+1],
  64.         packetBuffer[r_off+2]
  65.       );
  66.     }
  67.  
  68.     r_off = r_off + 3;
  69.   }
  70.  
  71.   if(packet_number == 0){
  72.   }else if(packet_number == ceil(led_number / LED_PER_PACKET)){
  73.     dcll->swap_buffer();
  74.     write_frame();
  75.   }
  76. }
  77.  
  78. void setup() {
  79.   Serial.begin(115200);
  80.   // put your setup code here, to run once:
  81.   WiFi.begin(networkName, networkPswd);
  82.   while (WiFi.status() != WL_CONNECTED) {
  83.     delay(500);
  84.     Serial.print(".");
  85.   }
  86.   // Print local IP address and start web server
  87.   Serial.println("");
  88.   Serial.println("WiFi connected.");
  89.   Serial.println("IP address: ");
  90.   Serial.println(WiFi.localIP());
  91.  
  92.   Udp.begin(udpPort);
  93.  
  94.   //delay(1000);
  95.   dcll = new Dcll(1);
  96.  
  97.   xTaskCreatePinnedToCore(
  98.     Receive_Udp_Task_Code,
  99.     "Receive_Udp_Task",
  100.     1000,
  101.     NULL,
  102.     0,
  103.     &Receive_Udp_Task,
  104.     0
  105.   );
  106. }
  107.  
  108. void Receive_Udp_Task_Code(void * parameter) {
  109.   while(true){
  110.     //Serial.println("RECEIVE UDP TASK");
  111.     int packetSize = Udp.parsePacket();
  112.  
  113.     if(packetSize){
  114.       //Serial.println("GOT PACKET");
  115.       Udp.read(packetBuffer, packetSize);
  116.  
  117.       vTaskDelay(40 / portTICK_PERIOD_MS);
  118.    
  119.       if(packetBuffer[0] == 1){
  120.         handle_registration_packet(packetBuffer);
  121.       }else if(packetBuffer[0] == 2){
  122.         Serial.println("HANDLE PACKET FRAME");
  123.         handle_frame_packet(packetBuffer);
  124.       }
  125.  
  126.       /*
  127.       if(packetBuffer[0] == 1){
  128.         handle_registration_packet(packetBuffer);
  129.       }else if(packetBuffer[0] == 2){
  130.         handle_frame_packet(packetBuffer);
  131.       }
  132.       */
  133.       write_ack();
  134.       vTaskDelay(40 / portTICK_PERIOD_MS);
  135.     }
  136.   }
  137. }
  138.  
  139.  
  140. void loop() {
  141.   /*
  142.   int packetSize = Udp.parsePacket();
  143.  
  144.   if(packetSize){
  145.     Udp.read(packetBuffer, packetSize);
  146.  
  147.     if(packetBuffer[0] == 1){
  148.       handle_registration_packet(packetBuffer);
  149.     }else if(packetBuffer[0] == 2){
  150.       handle_frame_packet(packetBuffer);
  151.     }
  152.   }
  153.   */
  154.   delay(100000);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement