Exemplo n.º 1
0
        public RotaryEncoder(Cpu.Pin aQuad, Cpu.Pin bQuad, Cpu.Pin momentaryPushbutton)
        {
            //Keyboard kb = Keyboard.Instance;
            //kb.Clear();
            Keyboard2.Clear();
            _aQuad = new InterruptPort(aQuad, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            _bQuad = new InterruptPort(bQuad, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            _momentaryPushbutton = new InterruptPort(momentaryPushbutton, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);

            if (_momentaryPushbutton.Read() == false)
            {
                using (var rtc = new RTC())
                {
                    rtc.ClearCalibration();
                }
            }

            _distanceTraveled = 0;
            _currentDirection = Direction.RotateLeft;
            _sequencePos = 1;
            _ready = true;

            try
            {
                _momentaryPushbutton.OnInterrupt += MomentaryPushbuttonOnInterrupt;
                //_momentaryPushbutton.EnableInterrupt();

                _aQuad.OnInterrupt += AQuadOnInterrupt;
                //_aQuad.EnableInterrupt();
                _bQuad.OnInterrupt += BQuadOnInterrupt;
                //_bQuad.EnableInterrupt();
            }
            catch
            {
                DebugHelper.Print("Button Interrupt Init Failed");
            }
        }
        // ===================================================================
        // --------------------------------------------------------------------
        /// <summary>
        ///     Edit system date
        /// </summary>
        private void SetDate()
        {
            var stringBuilder = new StringBuilder(10);
            stringBuilder.Append(FormattedNumber(2, DateTime.Now.Month));
            stringBuilder.Append('/');
            stringBuilder.Append(FormattedNumber(2, DateTime.Now.Day));
            stringBuilder.Append('/');
            stringBuilder.Append(DateTime.Now.Year);

            while (true)
            {
                string newDate = EditDate("Set Date: M/D/Y", stringBuilder.ToString());
                if (newDate != "")
                {
                    string[] fields = newDate.Split('/');
                    DateTime curTime = DateTime.Now;
            #if MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3
                    Utility.SetLocalTime(new DateTime(int.Parse(fields[2]), int.Parse(fields[0]), int.Parse(fields[1]), curTime.Hour, curTime.Minute, curTime.Second, curTime.Millisecond));
            #else
                    // TODO
            #endif
                    using (var rtc = new RTC())
                    {
                        rtc.SetTime(DateTime.Now);
                    }
                }
                return;
            }
        }
        // ====================================================================
        // --------------------------------------------------------------------
        /// <summary>
        ///     Edit system time and time server settings
        /// </summary>
        private void SetTime()
        {
            string[] timemenu =
            {
                "Set ",
                "Server ",
                "Cancel"
            };

            //_display.ExtPrintString(timeString.ToString());
            switch (HmenuSelectOption("Time", timemenu))
            {
                case 0:
                    break;
                case 1:
                    TimeServer();
                    return;
                default:
                    return;
            }

            while (true)
            {
                _display.ExtPrintString("Set Time:");

                var timeString = new StringBuilder(8);
                timeString.Append(FormattedNumber(2, DateTime.Now.Hour));
                timeString.Append(':');
                timeString.Append(FormattedNumber(2, DateTime.Now.Minute));
                timeString.Append(':');
                timeString.Append(FormattedNumber(2, DateTime.Now.Second));

                string newTime = EditTimeHHMMSS("Enter time:", timeString.ToString());
                if (newTime != "")
                {
                    try
                    {
                        string[] fields = newTime.Split(':');
                        DateTime curTime = DateTime.Now;
            #if MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3
                        Utility.SetLocalTime(new DateTime(curTime.Year, curTime.Month, curTime.Day, int.Parse(fields[0]), int.Parse(fields[1]), int.Parse(fields[2])));
            #endif
                        using (var rtc = new RTC())
                        {
                            rtc.SetTime(DateTime.Now);
                        }
                        return;
                    }
                    catch (Exception)
                    {
                        _display.ExtPrintString("Invalid time\nPlease try again");
                        Thread.Sleep(2000);
                    }
                }
                else
                {
                    return;
                }
            }
        }
        private void SetSettings(HttpListenerResponse response, string requestBody,
            DateTime timeStamp)
        {
            Hashtable body = ParseBody(requestBody);
            bool reboot = false;
            DateTime newTime = DateTime.Now;
            bool doTimeUpdate = false;

            Settings.System.KnobControlEnabled = false;
            Settings.System.TimeServicesSync = false;

            foreach (DictionaryEntry entry in body)
            {
                string value = entry.Value.ToString();

                switch (entry.Key.ToString())
                {
                    case "SY_KNOB_CONTROL":
                        Settings.System.KnobControlEnabled = value.ToLower() == "on";
                        break;

                    case "WC_NETMASK":
                        if (value != Settings.Web.Netmask)
                            reboot = true;
                        Settings.Web.Netmask = value;
                        break;

                    case "WC_GATEWAY":
                        if (value != Settings.Web.Gateway)
                            reboot = true;
                        Settings.Web.Gateway = value;
                        break;

                    case "WC_IP":
                        if (value != Settings.Web.IP)
                            reboot = true;
                        Settings.Web.IP = value;
                        break;

                    case "WC_ADMIN_PASSWORD":
                        Settings.Web.AdminPassword = value;
                        break;

                    case "WC_CONTROL_PASSWORD":
                        Settings.Web.ValetPassword = value;
                        break;

                    case "SY_TIMESERVER_ALT":
                        Settings.System.TimeServerAlt = value;
                        break;

                    case "SY_TIMESERVER":
                        Settings.System.TimeServer = value;
                        break;

                    case "SY_TIMEZONE_OFFSET":
                        Settings.System.TimeZoneOffset = TextTools.StringToDouble(value);
                        break;

                    case "SY_TIMESERVER_EN":
                        Settings.System.TimeServicesSync = value.ToLower() == "on";
                        break;

                    case "SY_TIME":
                        try
                        {
                            value = TextTools.UrlDecode(value);
                            if (value.Length != 0)
                            {
                                string[] parts = value.Split('+', ':', '/');
                                int hour = int.Parse(parts[0]);
                                int minute = int.Parse(parts[1]);
                                int second = int.Parse(parts[2]);
                                newTime = new DateTime(newTime.Year, newTime.Month, newTime.Day, hour, minute, second);
                                doTimeUpdate = true;
                            }
                        }
                        catch (Exception exception)
                        {
                            Log.WriteException(exception, "related value:" + value);
                        }
                        break;

                    case "SY_DATE":
                        try
                        {
                            value = TextTools.UrlDecode(value);
                            if (value.Length != 0)
                            {
                                // format???
                                string[] parts = value.Split('+', ':', '/');
                                int month = int.Parse(parts[0]);
                                int day = int.Parse(parts[1]);
                                int year = int.Parse(parts[2]);
                                newTime = new DateTime(year, month, day, newTime.Hour, newTime.Minute, newTime.Second);
                                doTimeUpdate = true;
                            }
                        }
                        catch (Exception exception)
                        {
                            Log.WriteException(exception, "related value:" + value);
                        }
                        break;

                    case "SC_AUDIO_VOLUME":
                        try
                        {
                            Settings.System.AudioVolume = int.Parse(value);
                            _audio.AudioModule.PlayVolume = Settings.System.AudioVolume;
                        }
                        catch (Exception ex)
                        {
                            Log.WriteException(ex, "related to audio volume. value=" + value);
                        }
                        break;

                    case "SY_RTC_UPDATE_TIME":
                        Settings.System.RTCUpdateTime = value.ReplaceHtmlHexCodes();
                        break;
                }
            }

            response.StatusCode = 302;
            response.RedirectLocation = "settings.html";
            response.ContentLength64 = 0;
            #if MF_FRAMEWORK_VERSION_V4_2
            response.OutputStream.Flush();
            response.OutputStream.Close(); // B73
            #endif

            if (reboot)
                response.Close();

            Settings.Save(reboot);
            #if MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3
            /* B73 : Removed
            if (Settings.System.TimeServicesSync)
            {
                var ntp = new Ntp(Settings.System.TimeServer, Settings.System.TimeServerAlt, (int) Settings.System.TimeZoneOffset, new RTC());
                var rtcThread = new Thread(ntp.InitRtCfromTimeServer);
                rtcThread.Start();
            }
            else
            {*/
            // 2013-11-24 RvS : Skip time setting if time not manally set by user
            if (!doTimeUpdate)
                return;

            // Correct the time taken with the lag to process all settings by the netmf
            TimeSpan ts = DateTime.Now - timeStamp;
            newTime += ts;
            DebugHelper.Print("TimeSpan in mSeconds =" + ts.Milliseconds);

            using (var rtc = new RTC())
            {
                rtc.SetTime(newTime);
                Utility.SetLocalTime(newTime);
            }
            //}
            #endif
        }