예제 #1
0
 private void processDTFrame(DTFrame currentFrame)
 {
     for (int i = 0; i < currentFrame.pixels.Length; i++)
     {
         this.pixels[i].setColor(currentFrame.pixels[i]);
     }
 }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        DTFrame currentFrame = commReceiver.getLatestFrame();

        if (currentFrame != null)
        {
            this.processDTFrame(currentFrame);
        }
    }
    public DTFrame getLatestFrame()
    {
        DTFrame returningFrame = null;

        if (Monitor.TryEnter(this.lockObject))
        {
            if (this.framesBuffer.Count > 0)
            {
                returningFrame = this.framesBuffer[framesBuffer.Count - 1];
                framesBuffer.Clear();
            }

            Monitor.Exit(lockObject);
        }

        return(returningFrame);
    }
    // receive thread
    private void ReceiveData()
    {
        client = new UdpClient(port);
        while (true)
        {

            try
            {
                if(client.Available > 0){
                    // Bytes
                    IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                    byte[] data = client.Receive(ref anyIP);

                    if((this.dataBufferIndex + data.Length)<BUFF_LEN){
                        //SHA_ToDo find memcpy in C#
                        //memcpy(&(this->binaryBuffer)+this->intBinaryBuffIndex,&buff,receivedBytes);
                        Array.Copy(data,0,this.dataBuffer, this.dataBufferIndex,data.Length);
                        //aumento el index del buffer
                        this.dataBufferIndex += data.Length;
                    }

                    //Cuando hay OVERFLOW, se pierden paquetes--> se da el mismo tratamiento que frente a un error
                    //--> se resincroniza mandando mensaje de error

                    if(Monitor.TryEnter(this.lockObject)){
                    // intento parsear el primer paquete que figura en el buffer principal
                        int firstOcurrence = -1;
                        firstOcurrence = searchPacketDelimiter(0);

                        if(firstOcurrence == 0){

                            int secondOccurence = searchPacketDelimiter(1);

                            // packet starts at index 8
                            // packet header is
                            //
                            /*
                            *
                            uint8_t  crc; //1 bytes
                            uint8_t  id[8]; //9 bytes
                            uint8_t  ver; // 10 bytes
                            uint8_t  sequenceHi; //11 bytes
                            uint8_t  sequence; //12 bytes
                            uint8_t  data;
                         	*
                         	*/
                            if(secondOccurence>0){
                                //We have a whole packet
                                DTFrame newFrame = new DTFrame();
                                //parsing frame

                                int cont = 0;
                                int dataIndex = firstOcurrence + 8 + 12;
                                for (int q=dataIndex; q<secondOccurence; q+=3){
                                    byte cR= this.dataBuffer[q];
                                    byte cG= this.dataBuffer[q + 1];
                                    byte cB= this.dataBuffer[q + 2];

                                    //we pass 0 as ledTypeId since is dummy information
                                    Color32 newPixel = new Color32(cR, cG, cB, 255);
                                    newFrame.addPixel (newPixel);
                                    cont++;
                                }

                                this.framesBuffer.Add(newFrame);

                                clearBinaryBuffer(secondOccurence + 1);
                            }

                        }
                        else{
                            if(firstOcurrence>0){
                                clearBinaryBuffer(firstOcurrence + 1);
                            }
                        }
                        Monitor.Exit(lockObject);
                    }
                    // Bytes UTF8
                    //string text = Encoding.UTF8.GetString(data);

                    //print(">> " + text);
                }

                Thread.Sleep(20);

            }
            catch (Exception err)
            {
                print(err.ToString());
            }
        }
    }
    // receive thread
    private void ReceiveData()
    {
        client = new UdpClient(port);
        while (true)
        {
            try
            {
                if (client.Available > 0)
                {
                    // Bytes
                    IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     data  = client.Receive(ref anyIP);

                    if ((this.dataBufferIndex + data.Length) < BUFF_LEN)
                    {
                        //SHA_ToDo find memcpy in C#
                        //memcpy(&(this->binaryBuffer)+this->intBinaryBuffIndex,&buff,receivedBytes);
                        Array.Copy(data, 0, this.dataBuffer, this.dataBufferIndex, data.Length);
                        //aumento el index del buffer
                        this.dataBufferIndex += data.Length;
                    }

                    //Cuando hay OVERFLOW, se pierden paquetes--> se da el mismo tratamiento que frente a un error
                    //--> se resincroniza mandando mensaje de error

                    if (Monitor.TryEnter(this.lockObject))
                    {
                        // intento parsear el primer paquete que figura en el buffer principal
                        int firstOcurrence = -1;
                        firstOcurrence = searchPacketDelimiter(0);

                        if (firstOcurrence == 0)
                        {
                            int secondOccurence = searchPacketDelimiter(1);

                            // packet starts at index 8
                            // packet header is
                            //

                            /*
                             *
                             * uint8_t  crc; //1 bytes
                             * uint8_t  id[8]; //9 bytes
                             * uint8_t  ver; // 10 bytes
                             * uint8_t  sequenceHi; //11 bytes
                             * uint8_t  sequence; //12 bytes
                             * uint8_t  data;
                             *
                             */
                            if (secondOccurence > 0)
                            {
                                //We have a whole packet
                                DTFrame newFrame = new DTFrame();
                                //parsing frame

                                int cont      = 0;
                                int dataIndex = firstOcurrence + 8 + 12;
                                for (int q = dataIndex; q < secondOccurence; q += 3)
                                {
                                    byte cR = this.dataBuffer[q];
                                    byte cG = this.dataBuffer[q + 1];
                                    byte cB = this.dataBuffer[q + 2];

                                    //we pass 0 as ledTypeId since is dummy information
                                    Color32 newPixel = new Color32(cR, cG, cB, 255);
                                    newFrame.addPixel(newPixel);
                                    cont++;
                                }

                                this.framesBuffer.Add(newFrame);

                                clearBinaryBuffer(secondOccurence + 1);
                            }
                        }
                        else
                        {
                            if (firstOcurrence > 0)
                            {
                                clearBinaryBuffer(firstOcurrence + 1);
                            }
                        }
                        Monitor.Exit(lockObject);
                    }
                    // Bytes UTF8
                    //string text = Encoding.UTF8.GetString(data);

                    //print(">> " + text);
                }

                Thread.Sleep(20);
            }
            catch (Exception err)
            {
                print(err.ToString());
            }
        }
    }
예제 #6
0
 private void processDTFrame(DTFrame currentFrame)
 {
     for (int i = 0; i<currentFrame.pixels.Length; i++) {
         this.pixels[i].setColor(currentFrame.pixels[i]);
     }
 }