Esempio n. 1
0
 public int SetBrightness(int NewBrightness)
 {
     if (DiscoMode == 0) //DiscoMode 0 means normal, DiscoMode 1 is disco
     {                   //If mode is not zero, brightness is irrelivent.
         if (NewBrightness == 0)
         {
             Send(codes["Off"]);
         }
         else if (NewBrightness == 1)
         {
             Send(codes["Night"]);
         }
         else if (NewBrightness >= 2)
         {
             if (ColourChangePending)
             {
                 this.SetColour(Colour);
             }
             Send(new byte[3] {
                 codes["On"][0], 0x4E, BitConverter.GetBytes(NewBrightness)[0]
             });                                                                                  //Change Brightness
         }
         else
         {
             Log.WriteLine("Illegal brighness change detected. Brightness cannot be a negative number. Continuing without throwing error.")
         }
     }
     else
     {
         BrightnessChangePending = true;//If something else changes that would cause this brightness change to be important (like it turns on
     }
     Brightness = NewBrightness;
     return(Brightness);
 }
Esempio n. 2
0
        private void SendMessageList()
        {
            //This function manages the message list and ensures all messages are sent
            //in the most efficient and orderly fashion. It ensures some commands do
            //not interrupt others, and that all commands are sent in the correct order
            //with minimum delay
            //--------------------------------------------------------------------
            //Opens a socket with the server

            Log.WriteLine("trying to Connect");
            System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient(this.IP, this.port);
            Log.WriteLine("Successfully Connected");
            //delay is the time interval in between messages being set. It ensures the connection does not time out.
            int delay = 0;

            while (true)
            {
                //If tdhe delay is too lare, it resets the connection
                if (delay >= 60000)
                {
                    udpClient.Close();
                    udpClient = new System.Net.Sockets.UdpClient(this.IP, this.port);
                    delay     = 0;
                }
                //If there is nothing in the mesaage list, it waits for a new message alert, which signals
                //A new item in the message list
                if (MessageList.Count == 0)
                {
                    //Setting the delay
                    delay = WaitHandle.WaitAny(newMessageAlert);
                }
                //if there IS something in the list, it sends the bytes and removes the item.
                else if (MessageList[0] != null)
                {
                    this.SendSingleMessage(udpClient, MessageList[0]);
                    for (int i = 0; i < 2 && MessageList.Count < 2; i++)
                    {
                        //It is preferable to send the message 3 times. This ensures it will send it once, and then
                        //send it two more times if there are no other pending messages.
                        this.SendSingleMessage(udpClient, MessageList[0]);
                    }
                    MessageList.RemoveAt(0);
                }
            }
        }