Пример #1
0
 /// <summary>
 /// Raise event telling rest of program that a command was received over the serial port
 /// </summary>
 /// <param name="strCommand">Command text (format is x:[xxxx...])</param>
 /// <returns>true if command was handled by one of the event recipients</returns>
 private bool raiseCommand(string strCommand)
 {
     if (Command == null)
     {
         // there are no event handlers out there
         return false;
     }
     CommandArgs args = new CommandArgs(strCommand);
     // raise event using Command delegate
     Command(this, args);
     return args.blnHandled;
 }
Пример #2
0
        static void uart_Command(object sender, CommandArgs e)
        {
            try
            {
                Debug.Print("in uart_Command: " + e.StrCommand);
                char command = e.StrCommand[0];
                string[] strParts;
                if (command == 'B')
                {
                    if (lcd != null)
                    {
                        bytBrightness = byte.Parse(e.StrCommand.Substring(2));

                        lcd.SetBrightness(bytBrightness);
                        blnManualBrightness = true; // ignore pot setting
                        byte value = bytBrightness;
                        if (bytBrightness < 255)
                            value += 1; // add 1, since 0 value means not saved
                        SaveSettingToEEPROM(ADDR_LCD_BRITE, (byte)(value));
                    }
                    e.blnHandled = true;
                }
                else if (command == 'F')
                {
                    if (e.StrCommand[2] == 'F')
                    {
                        blnFahr = true;
                        SaveSettingToEEPROM(ADDR_TEMP_FORMAT, 1);
                    }
                    else
                    {
                        blnFahr = false;
                        SaveSettingToEEPROM(ADDR_TEMP_FORMAT, 2);
                    }
                    // update Temperature right away so user knows it worked
                    displayTemperature(null);
                    e.blnHandled = true;
                }
                // backlight L:ON|OFF
                else if (command == 'L')
                {
                    if (e.StrCommand.Substring(2).ToUpper() == "ON")
                    {
                        blnBacklight = true;
                        lcd.SetBacklight(true);
                    }
                    else
                    {
                        blnBacklight = false;
                        lcd.SetBacklight(false);
                    }
                    e.blnHandled = true;
                // turn light on +:hh:mm
                }
                else if (command == '+')
                {
                    e.blnHandled = setLightTime(e.StrCommand.Substring(2), true);
                    // if Light On time manually set, then always use it, not sunset
                    blnLightOnAtSunset = false;
                    //TODO - save to EEPROM
                }
                // turn light on +:hh:mm               
                else if (command == '-')
                {
                    e.blnHandled = setLightTime(e.StrCommand.Substring(2), false);
                    //TODO - save to EEPROM
                }
                else if (command == 'D')
                {
                    blnDateSet = true;
                    // set default light timer if date and time have been set for the first time
                    setDefaultTimers(); 
                }
                else if (command == 'T')
                {
                    blnTimeSet = true;
                    // set default light timer if date and time have been set for the first time
                    setDefaultTimers();
                }
                else if (command == 'H')
                {
                    // got humidity and temperature via Arduino
                    // format is H:nn.n;nn.n, where 1st reading is humidity and 2nd is temperature
                    strParts = e.StrCommand.Split(new char[] { ';', ':' });
                    strHumidity = strParts[1];
                    blnHumidityUpdated = true;
                    strTempDHT11 = strParts[2];
                    blnTempUpdated = true;
                    datlastArduinoRead = DateTime.Now;
                }
                else if (command == 'S')
                {
                    // setting loaded from EEPROM
                    // format is S:<address>,<value>, where address is the EEPROM address (1-255) and value is the value stored
                    //  in that address (0-255)
                    strParts = e.StrCommand.Substring(2).Split(new char[] {','});
                    byte addr = Byte.Parse(strParts[0]);
                    byte value = Byte.Parse(strParts[1]);
                    if (value > 0) // our values are always > 0, so 0 means setting has never been saved
                    {
                        handleSetting(addr, value);
                    }
                }
            }
            catch (Exception ex)
            {
                // catch and release
                Debug.Assert(false);
                Debug.Print("Exception in uart_Command: " + ex.Message);
            }
        }