예제 #1
0
        private static AppSettings Load()
        {
            try
            {
                throw new Exception();

                byte[] settingsSize = new byte[4];
                BatteryRAM.Read(0, settingsSize, 0, 4);

                int size = settingsSize[0] * 256 + settingsSize[1];
                if (size == 0 || size > 2044)
                {
                    throw new Exception();
                }

                int pages = size / 4 + (size % 4 == 0 ? 0 : 1);

                byte[] aux = new byte[pages * 4];
                BatteryRAM.Read(4, aux, 0, pages * 4);

                byte[] settings = new byte[size];
                Array.Copy(aux, settings, size);

                return((AppSettings)Reflection.Deserialize(settings, typeof(AppSettings)));
            }
            catch
            {
                return(new AppSettings());
            }
        }
예제 #2
0
        public void Save()
        {
            byte[] settings = Reflection.Serialize(this, typeof(AppSettings));
            if (settings.Length > 2044)
            {
                //EMX has 2048 bytes of battery backed-up RAM. 4 bytes are reserved for size
                throw new Exception("Settings size exceed memory limit");
            }

            byte[] settingsSize = new byte[4] {
                (byte)(settings.Length / 256), (byte)(settings.Length % 256), 0, 0
            };
            int pages = settings.Length / 4 + (settings.Length % 4 == 0 ? 0 : 1);

            byte[] settingsPages = new byte[4 * pages];
            Array.Copy(settings, settingsPages, settings.Length);

            try
            {
                BatteryRAM.Write(0, settingsSize, 0, 4);
                BatteryRAM.Write(4, settingsPages, 0, pages * 4);
            }
            catch (Exception)
            {
                //TODO: emulator throws exception
            }
        }
예제 #3
0
        public static void SetDateTime(DateTime newTime)
        {
            var buf = new byte[4];

            Utility.InsertValueIntoArray(buf, 0, 4, RtcValidMagic);
            RealTimeClock.SetTime(newTime);     // Update RTC.
            BatteryRAM.Write(0, buf, 0, 4);     // Mark it valid in BatteryRam.
            RtcIsValid = true;                  // Mark it valid in code.
            Utility.SetLocalTime(newTime);      // Don't forget to update the framework clock too!

            var evt = OnRtcUpdate;

            if (evt != null)
            {
                evt(newTime);
            }
        }
예제 #4
0
        public const UInt32 RtcValidMagic = 0x6d605ea1;        // This is set in BatteryRam when the RTC is valid.

        static WallClock()
        {
            // Establish if the RTC is valid or not based on a magic value set in battery RAM.
            var buf = new byte[4];

            BatteryRAM.Read(0, buf, 0, buf.Length);
            var batteryRamClockVal = Utility.ExtractValueFromArray(buf, 0, 4);

            RtcIsValid = batteryRamClockVal == RtcValidMagic;

            if (RtcIsValid)
            {
                Utility.SetLocalTime(RealTimeClock.GetTime());
            }
            else
            {
                Utility.SetLocalTime(new DateTime(2011, 01, 01, 0, 0, 0));      // Fake the start of 2011.
            }
        }