static void Main() { var rtc = RtcController.GetDefault(); if (rtc.IsValid) { Debug.WriteLine("RTC is Valid"); // RTC is good so let's use it SystemTime.SetTime(rtc.Now); } else { Debug.WriteLine("RTC is Invalid"); // RTC is not valid. Get user input to set it // This example will simply set it to January 1st 2019 at 11:11:11 var MyTime = new DateTime(2019, 1, 1, 11, 11, 11); rtc.Now = MyTime; SystemTime.SetTime(MyTime); } while (true) { Debug.WriteLine("Current Time : " + DateTime.Now); Debug.WriteLine("Current RTC Time: " + rtc.Now); Thread.Sleep(1000); } }
static void Main() { RtcController rtc = RtcController.GetDefault(); try { var t = rtc.GetTime(); if (t.Year == 1980) { rtc.SetTime(RtcDateTime.FromDateTime(new DateTime(2018, 10, 12, 17, 51, 00))); } } catch (Exception) { rtc.SetTime(RtcDateTime.FromDateTime(new DateTime(2018, 10, 11, 11, 51, 00))); } //string COM = "GHIElectronics.TinyCLR.NativeApis.STM32F7.UartController\\2"; UartController ser = UartController.FromName(STM32F7.UartPort.Usart1); // DISCO-F769 //UartController ser = UartController.FromName(STM32F7.UartPort.Usart3); // NUCLEO-F767 ser.SetActiveSettings(38400, 8, UartParity.None, UartStopBitCount.One, UartHandshake.None); Debug.WriteLine("Starting Program ...."); BlinkLed blink = new BlinkLed(STM32F7.GpioPin.PJ5, 500); // Disco 769 //BlinkLed blink = new BlinkLed(STM32F7.GpioPin.PB7, 500); // Nucleo 144 - F767 Thread run = new Thread(blink.Blink); run.Start(); var r = new HeapAllocTest(10); r.Allocate(); //var d = new DisplayTest(); string sallocated = ""; //Debug.WriteLine("Allocated TOTAL Memory:" + GC.GetTotalMemory(false).ToString() + " bytes!"); //d.DrawSomething("Test String...", 50,50); int x = 10, y = 10; //SDTest sd = new SDTest(); sallocated = "Memory:" + GC.GetTotalMemory(true).ToString() + " bytes!"; while (true) { x += 2; y += 2; if (x > (800 - 160) || y > 480 - 160) { x = 10; y = 10; GC.Collect(); } Thread.Sleep(1000); //d.DrawSomething(sallocated, x, y); var dt = rtc.Now; byte[] b = System.Text.Encoding.UTF8.GetBytes("Program Running !! .." + dt.ToString("dd/MM/yyyy HH:mm:ss") + "\r\n"); ser.Write(b); ser.Flush(); } }
private static void Main() { // Indicator LED var led1 = GpioController.GetDefault().OpenPin(G30.GpioPin.PC7); led1.SetDriveMode(GpioPinDriveMode.Output); led1.Write(GpioPinValue.Low); var sd = StorageController.FromName(@"GHIElectronics.TinyCLR.NativeApis.STM32F4.SdCardStorageController\0"); var drive = FileSystem.Mount(sd.Hdc); // While not required, having the right time is needed // The File System saves the file with current time var rtcControl = RtcController.GetDefault(); // We will use this time if RTC is not valid - 2019/January/1 10:5:0 var CurrentTime = new DateTime(2019, 1, 1, 10, 5, 0); if (rtcControl.IsValid) { CurrentTime = rtcControl.Now; } else { rtcControl.Now = CurrentTime; } SystemTime.SetTime(CurrentTime); // Add a file var file = new FileStream($@"{drive.Name}Test.txt", FileMode.Append); var bytes = Encoding.UTF8.GetBytes("Hello is recorded at: " + DateTime.UtcNow.ToString() + Environment.NewLine); file.Write(bytes, 0, bytes.Length); file.Close(); // Show a list of files on root directory var directory = new DirectoryInfo(drive.Name); var files = directory.GetFiles(); foreach (var f in files) { Debug.WriteLine(f.Name); } FileSystem.Flush(sd.Hdc); // Turn an LED on indicating the operation is complete! // Reset the board a few times and check the file content. led1.Write(GpioPinValue.High); }
const int ChargeVbatTimeout = 30; // 30 seconds public RtcWindow(Bitmap icon, string text, int width, int height) : base(icon, text, width, height) { this.font = Resources.GetFont(Resources.FontResources.droid_reg11); this.testButton = new Button() { Child = new GHIElectronics.TinyCLR.UI.Controls.Text(this.font, "Test") { ForeColor = Colors.Black, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, }, Width = 100, Height = 30, }; this.testButton.Click += this.TestButton_Click; this.rtc = RtcController.GetDefault(); this.rtc.SetChargeMode(BatteryChargeMode.None); }
private bool DoTestRtc() { this.UpdateStatusText("Testing real time clock... ", true); try_again: try { var rtc = RtcController.GetDefault(); var m = new DateTime(2020, 7, 7, 00, 00, 00); if (rtc.IsValid && rtc.Now >= m) { return(true); } else { var newDt = RtcDateTime.FromDateTime(m); rtc.SetTime(newDt); if (rtc.IsValid && rtc.Now >= m) { return(true); } } } catch { this.UpdateStatusText("Try Rtc again... ", true); } if (this.isRunning) { goto try_again; } return(false); }
public static void SetAppTime(int pTimeZoneOffset, string pTimeServer_1, string pTimeServer_2) { // Set parameters of the TimeService TimeServiceSettings timeSettings = new TimeServiceSettings() { //RefreshTime = 60, // every 60 sec (for tests) RefreshTime = 2 * 60 * 60, // every 2 hours (2 x 60 x 60) default: 300000 sec AutoDayLightSavings = false, // We use our own timeshift calculation ForceSyncAtWakeUp = true, Tolerance = 10000 // deviation may be up to 10 sec }; int loopCounter = 1; while (loopCounter < 3) { IPAddress[] address = null; IPAddress[] address_2 = null; try { address = System.Net.Dns.GetHostEntry(pTimeServer_1).AddressList; } catch { }; try { address_2 = System.Net.Dns.GetHostEntry(pTimeServer_2).AddressList; } catch { }; try { timeSettings.PrimaryServer = address[0].GetAddressBytes(); } catch { }; try { timeSettings.AlternateServer = address_2[0].GetAddressBytes(); } catch { }; TimeService.Settings = timeSettings; TimeService.SetTimeZoneOffset(pTimeZoneOffset); Debug.WriteLine("Starting Timeservice"); TimeService.Start(); Debug.WriteLine("Returned from Starting Timeservice"); Thread.Sleep(100); if (DateTime.Now > new DateTime(2018, 7, 1)) { timeServiceIsRunning = true; Debug.WriteLine("Timeserver intialized on try: " + loopCounter); Debug.WriteLine("Synchronization Interval = " + timeSettings.RefreshTime); break; } else { timeServiceIsRunning = false; Debug.WriteLine("Timeserver could not be intialized on try: " + loopCounter); } loopCounter++; } var rtc = RtcController.GetDefault(); if (timeServiceIsRunning) { rtc.Now = DateTime.UtcNow; } else { Debug.WriteLine("No success to get time over internet"); // Get time from Rtc if (rtc.IsValid) { SystemTime.SetTime(rtc.Now, timeZoneOffset); } } // SystemTime.SetTime(new DateTime(2000, 1, 1, 1, 1, 1)); //For tests, to see what happens when wrong date if (DateTime.Now < new DateTime(2016, 7, 1)) { timeIsSet = false; Debug.WriteLine("Restarting Program"); Power.Reset(true); } else { Debug.WriteLine("Could get Time from Internet or RealTime Clock"); timeIsSet = true; } }
const int ChargeVbatTimeout = 30; // 30 seconds public RtcWindow(Bitmap icon, string text, int width, int height) : base(icon, text, width, height) { this.rtc = RtcController.GetDefault(); this.rtc.SetChargeMode(BatteryChargeMode.None); }